• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     std::optional<std::vector<std::string>> allowList;
78     const bool haveAllow = data.ReadBool();
79     if (haveAllow) {
80         const int32_t allowSize = data.ReadInt32();
81         if (allowSize > DISALLOWLISTMAXSIZE) {
82             APP_LOGE("Abnormal allowList data size reading form parcel, size %{public}d", vectorSize);
83             return ERR_APPEXECFWK_PARCEL_ERROR;
84         }
85         std::vector<std::string> allowLst;
86         for (int32_t i = 0; i < allowSize; i++) {
87             allowLst.emplace_back(data.ReadString());
88         }
89         allowList = std::make_optional<std::vector<std::string>>(allowLst);
90     } else {
91         allowList = std::nullopt;
92     }
93     auto ret = CreateNewUser(userId, disallowList, allowList);
94     if (!reply.WriteInt32(ret)) {
95         APP_LOGE("write failed");
96         return ERR_APPEXECFWK_PARCEL_ERROR;
97     }
98     return ERR_OK;
99 }
100 
HandleRemoveUser(Parcel & data,Parcel & reply)101 ErrCode BundleUserMgrHost::HandleRemoveUser(Parcel &data, Parcel &reply)
102 {
103     auto ret = RemoveUser(data.ReadInt32());
104     if (!reply.WriteInt32(ret)) {
105         APP_LOGE("write failed");
106         return ERR_APPEXECFWK_PARCEL_ERROR;
107     }
108     return ERR_OK;
109 }
110 }  // namespace AppExecFwk
111 }  // namespace OHOS
112