1 /*
2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "bundle_framework_core_ipc_interface_code.h"
16 #include "bundle_user_manager_adapter_proxy.h"
17 #include "account_error_no.h"
18 #include "account_log_wrapper.h"
19
20 namespace OHOS {
21 namespace AccountSA {
22 constexpr size_t DISALLOWED_HAP_LIST_MAX_SIZE = 1000;
23
BundleUserManagerAdapterProxy(const sptr<IRemoteObject> & object)24 BundleUserManagerAdapterProxy::BundleUserManagerAdapterProxy(const sptr<IRemoteObject> &object)
25 : IRemoteProxy<AppExecFwk::IBundleUserMgr>(object)
26 {}
27
~BundleUserManagerAdapterProxy()28 BundleUserManagerAdapterProxy::~BundleUserManagerAdapterProxy()
29 {}
30
CreateNewUser(int32_t userId,const std::vector<std::string> & disallowedHapList)31 ErrCode BundleUserManagerAdapterProxy::CreateNewUser(int32_t userId, const std::vector<std::string> &disallowedHapList)
32 {
33 MessageParcel data;
34 if (!data.WriteInterfaceToken(BundleUserManagerAdapterProxy::GetDescriptor())) {
35 ACCOUNT_LOGE("fail to CreateNewUser due to write MessageParcel fail");
36 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
37 }
38 if (!data.WriteInt32(static_cast<int32_t>(userId))) {
39 ACCOUNT_LOGE("fail to CreateNewUser due to write userId fail");
40 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
41 }
42 uint32_t disallowedListMatchSize = (disallowedHapList.size() > DISALLOWED_HAP_LIST_MAX_SIZE) ?
43 DISALLOWED_HAP_LIST_MAX_SIZE : disallowedHapList.size();
44 if (!data.WriteInt32(disallowedListMatchSize)) {
45 ACCOUNT_LOGE("Write BundleNameListVector failed");
46 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
47 }
48 for (uint32_t index = 0; index < disallowedListMatchSize; ++index) {
49 if (!data.WriteString(disallowedHapList.at(index))) {
50 ACCOUNT_LOGE("Write BundleNameListVector failed");
51 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
52 }
53 }
54 MessageParcel reply;
55 if (!SendTransactCmd(AppExecFwk::BundleUserMgrInterfaceCode::CREATE_USER, data, reply)) {
56 ACCOUNT_LOGE("fail to CreateNewUser from server");
57 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
58 }
59
60 ErrCode ret = reply.ReadInt32();
61 if (ret != ERR_OK) {
62 ACCOUNT_LOGE("host reply errCode : %{public}d", ret);
63 return ret;
64 }
65 return ERR_OK;
66 }
67
RemoveUser(int32_t userId)68 ErrCode BundleUserManagerAdapterProxy::RemoveUser(int32_t userId)
69 {
70 MessageParcel data;
71 if (!data.WriteInterfaceToken(BundleUserManagerAdapterProxy::GetDescriptor())) {
72 ACCOUNT_LOGE("fail to RemoveUser due to write MessageParcel fail");
73 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
74 }
75 if (!data.WriteInt32(static_cast<int32_t>(userId))) {
76 ACCOUNT_LOGE("fail to RemoveUser due to write userId fail");
77 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
78 }
79
80 MessageParcel reply;
81 if (!SendTransactCmd(AppExecFwk::BundleUserMgrInterfaceCode::REMOVE_USER, data, reply)) {
82 ACCOUNT_LOGE("fail to RemoveUser from server");
83 return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
84 }
85
86 ErrCode ret = reply.ReadInt32();
87 if (ret != ERR_OK) {
88 ACCOUNT_LOGE("host reply errCode : %{public}d", ret);
89 return ret;
90 }
91 return ERR_OK;
92 }
93
SendTransactCmd(AppExecFwk::BundleUserMgrInterfaceCode code,MessageParcel & data,MessageParcel & reply)94 bool BundleUserManagerAdapterProxy::SendTransactCmd(
95 AppExecFwk::BundleUserMgrInterfaceCode code, MessageParcel &data, MessageParcel &reply)
96 {
97 MessageOption option(MessageOption::TF_SYNC);
98
99 sptr<IRemoteObject> remote = Remote();
100 if (remote == nullptr) {
101 ACCOUNT_LOGE("fail to uninstall, for Remote() is nullptr");
102 return false;
103 }
104
105 int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
106 if (result != NO_ERROR) {
107 ACCOUNT_LOGE("fail to sendRequest, for transact is failed and error code is: %{public}d", result);
108 return false;
109 }
110 return true;
111 }
112 } // namespace AccountSA
113 } // namespace OHOS
114