1 /*
2 * Copyright (c) 2022 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 "app_log_wrapper.h"
19 #include "bundle_framework_core_ipc_interface_code.h"
20 #include "ipc_types.h"
21 #include "parcel.h"
22 #include "string_ex.h"
23
24 namespace OHOS {
25 namespace AppExecFwk {
BundleUserMgrProxy(const sptr<IRemoteObject> & object)26 BundleUserMgrProxy::BundleUserMgrProxy(const sptr<IRemoteObject> &object)
27 : IRemoteProxy<IBundleUserMgr>(object)
28 {
29 APP_LOGD("create BundleUserMgrProxy instance");
30 }
31
~BundleUserMgrProxy()32 BundleUserMgrProxy::~BundleUserMgrProxy()
33 {
34 APP_LOGD("destroy BundleUserMgrProxy instance");
35 }
36
CreateNewUser(int32_t userId)37 void BundleUserMgrProxy::CreateNewUser(int32_t userId)
38 {
39 APP_LOGD("CreateNewUser %{public}d", userId);
40 MessageParcel data;
41 MessageParcel reply;
42 MessageOption option(MessageOption::TF_SYNC);
43 if (!data.WriteInterfaceToken(BundleUserMgrProxy::GetDescriptor())) {
44 APP_LOGE("fail to CreateNewUser due to write MessageParcel fail");
45 return;
46 }
47
48 data.WriteInt32(static_cast<int32_t>(userId));
49 SendRequest(
50 static_cast<int32_t>(BundleUserMgrInterfaceCode::CREATE_USER), data, reply, option);
51 }
52
RemoveUser(int32_t userId)53 void BundleUserMgrProxy::RemoveUser(int32_t userId)
54 {
55 APP_LOGD("RemoveUser %{public}d", userId);
56 MessageParcel data;
57 MessageParcel reply;
58 MessageOption option(MessageOption::TF_SYNC);
59 if (!data.WriteInterfaceToken(BundleUserMgrProxy::GetDescriptor())) {
60 APP_LOGE("fail to RemoveUser due to write MessageParcel fail");
61 return;
62 }
63
64 data.WriteInt32(static_cast<int32_t>(userId));
65 SendRequest(
66 static_cast<int32_t>(BundleUserMgrInterfaceCode::REMOVE_USER), data, reply, option);
67 }
68
SendRequest(const int32_t & code,MessageParcel & data,MessageParcel & reply,MessageOption & option)69 bool BundleUserMgrProxy::SendRequest(const int32_t& code, MessageParcel& data, MessageParcel& reply,
70 MessageOption& option)
71 {
72 sptr<IRemoteObject> remote = Remote();
73 if (remote == nullptr) {
74 APP_LOGE("fail to uninstall, for Remote() is nullptr");
75 return false;
76 }
77
78 int32_t ret = remote->SendRequest(code, data, reply, option);
79 if (ret != NO_ERROR) {
80 APP_LOGE("fail to sendRequest, for transact is failed and error code is: %{public}d", ret);
81 return false;
82 }
83 return true;
84 }
85 } // namespace AppExecFwk
86 } // namespace OHOS
87