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_user_mgr_host.h"
16 
17 #include "appexecfwk_errors.h"
18 #include "app_log_wrapper.h"
19 #include "bundle_framework_core_ipc_interface_code.h"
20 #include "bundle_memory_guard.h"
21 #include "ipc_types.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
25 constexpr int32_t DISALLOWLISTMAXSIZE = 1000;
BundleUserMgrHost()26 BundleUserMgrHost::BundleUserMgrHost()
27 {
28     APP_LOGD("create BundleUserMgrHost instance");
29 }
30 
~BundleUserMgrHost()31 BundleUserMgrHost::~BundleUserMgrHost()
32 {
33     APP_LOGD("destroy BundleUserMgrHost instance");
34 }
35 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)36 int BundleUserMgrHost::OnRemoteRequest(
37     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
38 {
39     BundleMemoryGuard memoryGuard;
40     APP_LOGD("BundleUserMgrHost onReceived message, the message code is %{public}u", code);
41     std::u16string descripter = BundleUserMgrHost::GetDescriptor();
42     std::u16string remoteDescripter = data.ReadInterfaceToken();
43     if (descripter != remoteDescripter) {
44         APP_LOGE("fail to write reply message in bundle user mgr host due to the reply is nullptr");
45         return OBJECT_NULL;
46     }
47 
48     ErrCode errCode = ERR_OK;
49     switch (code) {
50         case static_cast<uint32_t>(BundleUserMgrInterfaceCode::CREATE_USER): {
51             errCode = HandleCreateNewUser(data, reply);
52             break;
53         }
54         case static_cast<uint32_t>(BundleUserMgrInterfaceCode::REMOVE_USER): {
55             errCode = HandleRemoveUser(data, reply);
56             break;
57         }
58         default:
59             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
60     }
61     APP_LOGD("BundleUserMgr host finish to process message, errCode: %{public}d", errCode);
62     return (errCode == ERR_OK) ? NO_ERROR : UNKNOWN_ERROR;
63 }
64 
HandleCreateNewUser(Parcel & data,Parcel & reply)65 ErrCode BundleUserMgrHost::HandleCreateNewUser(Parcel &data, Parcel &reply)
66 {
67     const int32_t userId = data.ReadInt32();
68     const int32_t vectorSize = data.ReadInt32();
69     if (vectorSize > DISALLOWLISTMAXSIZE) {
70         APP_LOGE("Abnormal data size reading form parcel, size %{public}d", vectorSize);
71         return ERR_APPEXECFWK_PARCEL_ERROR;
72     }
73     std::vector<std::string> disallowList;
74     for (int32_t i = 0; i < vectorSize; i++) {
75         disallowList.emplace_back(data.ReadString());
76     }
77     auto ret = CreateNewUser(userId, disallowList);
78     if (!reply.WriteInt32(ret)) {
79         APP_LOGE("write failed");
80         return ERR_APPEXECFWK_PARCEL_ERROR;
81     }
82     return ERR_OK;
83 }
84 
HandleRemoveUser(Parcel & data,Parcel & reply)85 ErrCode BundleUserMgrHost::HandleRemoveUser(Parcel &data, Parcel &reply)
86 {
87     auto ret = RemoveUser(data.ReadInt32());
88     if (!reply.WriteInt32(ret)) {
89         APP_LOGE("write failed");
90         return ERR_APPEXECFWK_PARCEL_ERROR;
91     }
92     return ERR_OK;
93 }
94 }  // namespace AppExecFwk
95 }  // namespace OHOS
96