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
16 #include "bundle_user_mgr_proxy.h"
17
18 #include "appexecfwk_errors.h"
19 #include "app_log_wrapper.h"
20 #include "bundle_framework_core_ipc_interface_code.h"
21 #include "ipc_types.h"
22 #include "parcel.h"
23 #include "string_ex.h"
24
25 namespace OHOS {
26 namespace AppExecFwk {
27 constexpr size_t DISALLOWLISTMAXSIZE = 1000;
28
BundleUserMgrProxy(const sptr<IRemoteObject> & object)29 BundleUserMgrProxy::BundleUserMgrProxy(const sptr<IRemoteObject> &object)
30 : IRemoteProxy<IBundleUserMgr>(object)
31 {
32 APP_LOGD("create BundleUserMgrProxy instance");
33 }
34
~BundleUserMgrProxy()35 BundleUserMgrProxy::~BundleUserMgrProxy()
36 {
37 APP_LOGD("destroy BundleUserMgrProxy instance");
38 }
39
CreateNewUser(int32_t userId,const std::vector<std::string> & disallowList,const std::optional<std::vector<std::string>> & allowList)40 ErrCode BundleUserMgrProxy::CreateNewUser(int32_t userId, const std::vector<std::string> &disallowList,
41 const std::optional<std::vector<std::string>> &allowList)
42 {
43 APP_LOGD("CreateNewUser %{public}d", userId);
44 MessageParcel data;
45 if (!data.WriteInterfaceToken(BundleUserMgrProxy::GetDescriptor())) {
46 APP_LOGE("fail to CreateNewUser due to write MessageParcel fail");
47 return ERR_APPEXECFWK_PARCEL_ERROR;
48 }
49 if (!data.WriteInt32(static_cast<int32_t>(userId))) {
50 APP_LOGE("fail to CreateNewUser due to write uid fail");
51 return ERR_APPEXECFWK_PARCEL_ERROR;
52 }
53 if (WriteStrListToData(data, disallowList, DISALLOWLISTMAXSIZE) != ERR_OK) {
54 APP_LOGE("Write disallowList failed");
55 return ERR_APPEXECFWK_PARCEL_ERROR;
56 }
57
58 if (!allowList.has_value()) {
59 data.WriteBool(false);
60 } else {
61 data.WriteBool(true);
62 std::vector<std::string> allowLst = allowList.value();
63 if (WriteStrListToData(data, allowLst, DISALLOWLISTMAXSIZE) != ERR_OK) {
64 APP_LOGE("Write allowLst failed");
65 return ERR_APPEXECFWK_PARCEL_ERROR;
66 }
67 APP_LOGI("allowLst size %{public}zu", allowLst.size());
68 }
69
70 MessageParcel reply;
71 if (!SendTransactCmd(BundleUserMgrInterfaceCode::CREATE_USER, data, reply)) {
72 APP_LOGE("fail to CreateNewUser from server");
73 return ERR_APPEXECFWK_PARCEL_ERROR;
74 }
75
76 ErrCode ret = reply.ReadInt32();
77 if (ret != ERR_OK) {
78 APP_LOGE("host reply err %{public}d", ret);
79 return ret;
80 }
81 return ERR_OK;
82 }
83
WriteStrListToData(MessageParcel & data,const std::vector<std::string> & list,size_t maxListSize)84 ErrCode BundleUserMgrProxy::WriteStrListToData(MessageParcel &data, const std::vector<std::string> &list,
85 size_t maxListSize)
86 {
87 size_t listSize =
88 (list.size() > maxListSize) ? maxListSize : list.size();
89 if (!data.WriteInt32(listSize)) {
90 return ERR_APPEXECFWK_PARCEL_ERROR;
91 }
92 for (size_t index = 0; index < listSize; ++index) {
93 if (!data.WriteString(list.at(index))) {
94 return ERR_APPEXECFWK_PARCEL_ERROR;
95 }
96 }
97 return ERR_OK;
98 }
99
RemoveUser(int32_t userId)100 ErrCode BundleUserMgrProxy::RemoveUser(int32_t userId)
101 {
102 APP_LOGD("RemoveUser %{public}d", userId);
103 MessageParcel data;
104 if (!data.WriteInterfaceToken(BundleUserMgrProxy::GetDescriptor())) {
105 APP_LOGE("fail to RemoveUser due to write MessageParcel fail");
106 return ERR_APPEXECFWK_PARCEL_ERROR;
107 }
108 if (!data.WriteInt32(static_cast<int32_t>(userId))) {
109 APP_LOGE("fail to RemoveUser due to write uid fail");
110 return ERR_APPEXECFWK_PARCEL_ERROR;
111 }
112
113 MessageParcel reply;
114 if (!SendTransactCmd(BundleUserMgrInterfaceCode::REMOVE_USER, data, reply)) {
115 APP_LOGE("fail to RemoveUser from server");
116 return ERR_APPEXECFWK_PARCEL_ERROR;
117 }
118
119 ErrCode ret = reply.ReadInt32();
120 if (ret != ERR_OK) {
121 APP_LOGE("host reply err %{public}d", ret);
122 return ret;
123 }
124 return ERR_OK;
125 }
126
SendTransactCmd(BundleUserMgrInterfaceCode code,MessageParcel & data,MessageParcel & reply)127 bool BundleUserMgrProxy::SendTransactCmd(BundleUserMgrInterfaceCode code, MessageParcel &data, MessageParcel &reply)
128 {
129 MessageOption option(MessageOption::TF_SYNC);
130
131 sptr<IRemoteObject> remote = Remote();
132 if (remote == nullptr) {
133 APP_LOGE("fail send transact cmd %{public}hhd due to remote object", code);
134 return false;
135 }
136 int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
137 if (result != NO_ERROR) {
138 APP_LOGE("receive error transact code %{public}d in transact cmd %{public}hhd", result, code);
139 return false;
140 }
141 return true;
142 }
143 } // namespace AppExecFwk
144 } // namespace OHOS
145