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)40 ErrCode BundleUserMgrProxy::CreateNewUser(int32_t userId, const std::vector<std::string> &disallowList)
41 {
42     APP_LOGD("CreateNewUser %{public}d", userId);
43     MessageParcel data;
44     if (!data.WriteInterfaceToken(BundleUserMgrProxy::GetDescriptor())) {
45         APP_LOGE("fail to CreateNewUser due to write MessageParcel fail");
46         return ERR_APPEXECFWK_PARCEL_ERROR;
47     }
48     if (!data.WriteInt32(static_cast<int32_t>(userId))) {
49         APP_LOGE("fail to CreateNewUser due to write uid fail");
50         return ERR_APPEXECFWK_PARCEL_ERROR;
51     }
52     size_t disallowListMatchSize =
53         (disallowList.size() > DISALLOWLISTMAXSIZE) ? DISALLOWLISTMAXSIZE : disallowList.size();
54     if (!data.WriteInt32(disallowListMatchSize)) {
55         APP_LOGE("Write BundleNameListVector failed");
56         return ERR_APPEXECFWK_PARCEL_ERROR;
57     }
58     for (size_t index = 0; index < disallowListMatchSize; ++index) {
59         if (!data.WriteString(disallowList.at(index))) {
60             APP_LOGE("Write BundleNameListVector failed");
61             return ERR_APPEXECFWK_PARCEL_ERROR;
62         }
63     }
64 
65     MessageParcel reply;
66     if (!SendTransactCmd(BundleUserMgrInterfaceCode::CREATE_USER, data, reply)) {
67         APP_LOGE("fail to CreateNewUser from server");
68         return ERR_APPEXECFWK_PARCEL_ERROR;
69     }
70 
71     ErrCode ret = reply.ReadInt32();
72     if (ret != ERR_OK) {
73         APP_LOGE("host reply err %{public}d", ret);
74         return ret;
75     }
76     return ERR_OK;
77 }
78 
RemoveUser(int32_t userId)79 ErrCode BundleUserMgrProxy::RemoveUser(int32_t userId)
80 {
81     APP_LOGD("RemoveUser %{public}d", userId);
82     MessageParcel data;
83     if (!data.WriteInterfaceToken(BundleUserMgrProxy::GetDescriptor())) {
84         APP_LOGE("fail to RemoveUser due to write MessageParcel fail");
85         return ERR_APPEXECFWK_PARCEL_ERROR;
86     }
87     if (!data.WriteInt32(static_cast<int32_t>(userId))) {
88         APP_LOGE("fail to RemoveUser due to write uid fail");
89         return ERR_APPEXECFWK_PARCEL_ERROR;
90     }
91 
92     MessageParcel reply;
93     if (!SendTransactCmd(BundleUserMgrInterfaceCode::REMOVE_USER, data, reply)) {
94         APP_LOGE("fail to RemoveUser from server");
95         return ERR_APPEXECFWK_PARCEL_ERROR;
96     }
97 
98     ErrCode ret = reply.ReadInt32();
99     if (ret != ERR_OK) {
100         APP_LOGE("host reply err %{public}d", ret);
101         return ret;
102     }
103     return ERR_OK;
104 }
105 
SendTransactCmd(BundleUserMgrInterfaceCode code,MessageParcel & data,MessageParcel & reply)106 bool BundleUserMgrProxy::SendTransactCmd(BundleUserMgrInterfaceCode code, MessageParcel &data, MessageParcel &reply)
107 {
108     MessageOption option(MessageOption::TF_SYNC);
109 
110     sptr<IRemoteObject> remote = Remote();
111     if (remote == nullptr) {
112         APP_LOGE("fail send transact cmd %{public}hhd due to remote object", code);
113         return false;
114     }
115     int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
116     if (result != NO_ERROR) {
117         APP_LOGE("receive error transact code %{public}d in transact cmd %{public}hhd", result, code);
118         return false;
119     }
120     return true;
121 }
122 }  // namespace AppExecFwk
123 }  // namespace OHOS
124