• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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 "app_control_host.h"
17 
18 #include "securec.h"
19 #include "string_ex.h"
20 
21 #include "app_control_constants.h"
22 #include "app_log_tag_wrapper.h"
23 #include "app_log_wrapper.h"
24 #include "appexecfwk_errors.h"
25 #include "bundle_framework_core_ipc_interface_code.h"
26 #include "bundle_memory_guard.h"
27 #include "disposed_rule.h"
28 #include "ipc_types.h"
29 #include "parcel_macro.h"
30 
31 namespace OHOS {
32 namespace AppExecFwk {
33 namespace {
34 const int16_t MAX_VECTOR_NUM = 1000;
35 constexpr size_t MAX_IPC_ALLOWED_CAPACITY = 100 * 1024 * 1024; // max ipc size 100MB
GetData(size_t size,const void * data,void * & buffer)36 bool GetData(size_t size, const void *data, void *&buffer)
37 {
38     if (data == nullptr) {
39         APP_LOGE("failed due to null data");
40         return false;
41     }
42     if ((size == 0) || size > Constants::MAX_PARCEL_CAPACITY) {
43         APP_LOGE("failed due to wrong size");
44         return false;
45     }
46     buffer = malloc(size);
47     if (buffer == nullptr) {
48         APP_LOGE("failed due to malloc buffer failed");
49         return false;
50     }
51     if (memcpy_s(buffer, size, data, size) != EOK) {
52         free(buffer);
53         APP_LOGE("failed due to memcpy_s failed");
54         return false;
55     }
56     return true;
57 }
58 }
AppControlHost()59 AppControlHost::AppControlHost()
60 {
61     LOG_D(BMS_TAG_DEFAULT, "create AppControlHost");
62 }
63 
~AppControlHost()64 AppControlHost::~AppControlHost()
65 {
66     LOG_D(BMS_TAG_DEFAULT, "destroy AppControlHost");
67 }
68 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)69 int AppControlHost::OnRemoteRequest(
70     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
71 {
72     BundleMemoryGuard memoryGuard;
73     APP_LOGD("AppControlHost OnRemoteRequest, message code : %{public}u", code);
74     std::u16string descriptor = AppControlHost::GetDescriptor();
75     std::u16string remoteDescriptor = data.ReadInterfaceToken();
76     if (descriptor != remoteDescriptor) {
77         LOG_E(BMS_TAG_DEFAULT, "descriptor invalid");
78         return OBJECT_NULL;
79     }
80 
81     switch (code) {
82         case static_cast<uint32_t>(AppControlManagerInterfaceCode::ADD_APP_INSTALL_CONTROL_RULE):
83             return HandleAddAppInstallControlRule(data, reply);
84         case static_cast<uint32_t>(AppControlManagerInterfaceCode::DELETE_APP_INSTALL_CONTROL_RULE):
85             return HandleDeleteAppInstallControlRule(data, reply);
86         case static_cast<uint32_t>(AppControlManagerInterfaceCode::CLEAN_APP_INSTALL_CONTROL_RULE):
87             return HandleCleanAppInstallControlRule(data, reply);
88         case static_cast<uint32_t>(AppControlManagerInterfaceCode::GET_APP_INSTALL_CONTROL_RULE):
89             return HandleGetAppInstallControlRule(data, reply);
90         case static_cast<uint32_t>(AppControlManagerInterfaceCode::ADD_APP_RUNNING_CONTROL_RULE):
91             return HandleAddAppRunningControlRule(data, reply);
92         case static_cast<uint32_t>(AppControlManagerInterfaceCode::DELETE_APP_RUNNING_CONTROL_RULE):
93             return HandleDeleteAppRunningControlRule(data, reply);
94         case static_cast<uint32_t>(AppControlManagerInterfaceCode::CLEAN_APP_RUNNING_CONTROL_RULE):
95             return HandleCleanAppRunningControlRule(data, reply);
96         case static_cast<uint32_t>(AppControlManagerInterfaceCode::GET_APP_RUNNING_CONTROL_RULE):
97             return HandleGetAppRunningControlRule(data, reply);
98         case static_cast<uint32_t>(AppControlManagerInterfaceCode::GET_APP_RUNNING_CONTROL_RULE_RESULT):
99             return HandleGetAppRunningControlRuleResult(data, reply);
100         case static_cast<uint32_t>(AppControlManagerInterfaceCode::CONFIRM_APP_JUMP_CONTROL_RULE):
101             return HandleConfirmAppJumpControlRule(data, reply);
102         case static_cast<uint32_t>(AppControlManagerInterfaceCode::ADD_APP_JUMP_CONTROL_RULE):
103             return HandleAddAppJumpControlRule(data, reply);
104         case static_cast<uint32_t>(AppControlManagerInterfaceCode::DELETE_APP_JUMP_CONTROL_RULE):
105             return HandleDeleteAppJumpControlRule(data, reply);
106         case static_cast<uint32_t>(AppControlManagerInterfaceCode::DELETE_APP_JUMP_CONTROL_RULE_BY_CALLER):
107             return HandleDeleteRuleByCallerBundleName(data, reply);
108         case static_cast<uint32_t>(AppControlManagerInterfaceCode::DELETE_APP_JUMP_CONTROL_RULE_BY_TARGET):
109             return HandleDeleteRuleByTargetBundleName(data, reply);
110         case static_cast<uint32_t>(AppControlManagerInterfaceCode::GET_APP_JUMP_CONTROL_RULE):
111             return HandleGetAppJumpControlRule(data, reply);
112         case static_cast<uint32_t>(AppControlManagerInterfaceCode::SET_DISPOSED_STATUS):
113             return HandleSetDisposedStatus(data, reply);
114         case static_cast<uint32_t>(AppControlManagerInterfaceCode::GET_DISPOSED_STATUS):
115             return HandleGetDisposedStatus(data, reply);
116         case static_cast<uint32_t>(AppControlManagerInterfaceCode::DELETE_DISPOSED_STATUS):
117             return HandleDeleteDisposedStatus(data, reply);
118         case static_cast<uint32_t>(AppControlManagerInterfaceCode::SET_DISPOSED_RULE):
119             return HandleSetDisposedRule(data, reply);
120         case static_cast<uint32_t>(AppControlManagerInterfaceCode::SET_DISPOSED_RULES):
121             return HandleSetDisposedRules(data, reply);
122         case static_cast<uint32_t>(AppControlManagerInterfaceCode::GET_DISPOSED_RULE):
123             return HandleGetDisposedRule(data, reply);
124         case static_cast<uint32_t>(AppControlManagerInterfaceCode::GET_ABILITY_RUNNING_CONTROL_RULE):
125             return HandleGetAbilityRunningControlRule(data, reply);
126         case static_cast<uint32_t>(AppControlManagerInterfaceCode::GET_DISPOSED_RULE_FOR_CLONE_APP):
127             return HandleGetDisposedRuleForCloneApp(data, reply);
128         case static_cast<uint32_t>(AppControlManagerInterfaceCode::SET_DISPOSED_RULE_FOR_CLONE_APP):
129             return HandleSetDisposedRuleForCloneApp(data, reply);
130         case static_cast<uint32_t>(AppControlManagerInterfaceCode::DELETE_DISPOSED_RULE_FOR_CLONE_APP):
131             return HandleDeleteDisposedRuleForCloneApp(data, reply);
132         case static_cast<uint32_t>(AppControlManagerInterfaceCode::SET_UNINSTALL_DISPOSED_RULE):
133             return HandleSetUninstallDisposedRule(data, reply);
134         case static_cast<uint32_t>(AppControlManagerInterfaceCode::GET_UNINSTALL_DISPOSED_RULE):
135             return HandleGetUninstallDisposedRule(data, reply);
136         case static_cast<uint32_t>(AppControlManagerInterfaceCode::DELETE_UNINSTALL_DISPOSED_RULE):
137             return HandleDeleteUninstallDisposedRule(data, reply);
138         default:
139             LOG_W(BMS_TAG_DEFAULT, "AppControlHost receive unknown code, code = %{public}d", code);
140             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
141     }
142 }
143 
HandleAddAppInstallControlRule(MessageParcel & data,MessageParcel & reply)144 ErrCode AppControlHost::HandleAddAppInstallControlRule(MessageParcel& data, MessageParcel& reply)
145 {
146     std::vector<std::string> appIds;
147     int32_t appIdSize = data.ReadInt32();
148     if (appIdSize > AppControlConstants::LIST_MAX_SIZE || appIdSize < 0) {
149         LOG_E(BMS_TAG_DEFAULT, "HandleAddAppInstallControlRule parameter is invalid");
150         return ERR_BUNDLE_MANAGER_INVALID_PARAMETER;
151     }
152     appIds.reserve(appIdSize);
153     for (int32_t i = 0; i < appIdSize; i++) {
154         appIds.emplace_back(data.ReadString());
155     }
156     AppInstallControlRuleType controlRuleType = static_cast<AppInstallControlRuleType>(data.ReadInt32());
157     int32_t userId = data.ReadInt32();
158     int32_t ret = AddAppInstallControlRule(appIds, controlRuleType, userId);
159     if (ret != ERR_OK) {
160         LOG_E(BMS_TAG_DEFAULT, "HandleAddAppInstallControlRule failed");
161     }
162     return ret;
163 }
164 
HandleDeleteAppInstallControlRule(MessageParcel & data,MessageParcel & reply)165 ErrCode AppControlHost::HandleDeleteAppInstallControlRule(MessageParcel& data, MessageParcel& reply)
166 {
167     AppInstallControlRuleType controlRuleType = static_cast<AppInstallControlRuleType>(data.ReadInt32());
168     std::vector<std::string> appIds;
169     int32_t appIdSize = data.ReadInt32();
170     if (appIdSize > AppControlConstants::LIST_MAX_SIZE || appIdSize < 0) {
171         LOG_E(BMS_TAG_DEFAULT, "HandleDeleteAppInstallControlRule parameter is invalid");
172         return ERR_BUNDLE_MANAGER_INVALID_PARAMETER;
173     }
174     appIds.reserve(appIdSize);
175     for (int32_t i = 0; i < appIdSize; i++) {
176         appIds.emplace_back(data.ReadString());
177     }
178     int32_t userId = data.ReadInt32();
179     int32_t ret = DeleteAppInstallControlRule(controlRuleType, appIds, userId);
180     if (ret != ERR_OK) {
181         LOG_E(BMS_TAG_DEFAULT, "HandleDeleteAppInstallControlRule failed");
182     }
183     return ret;
184 }
185 
HandleCleanAppInstallControlRule(MessageParcel & data,MessageParcel & reply)186 ErrCode AppControlHost::HandleCleanAppInstallControlRule(MessageParcel& data, MessageParcel& reply)
187 {
188     AppInstallControlRuleType controlRuleType = static_cast<AppInstallControlRuleType>(data.ReadInt32());
189     int32_t userId = data.ReadInt32();
190     int32_t ret = DeleteAppInstallControlRule(controlRuleType, userId);
191     if (ret != ERR_OK) {
192         LOG_E(BMS_TAG_DEFAULT, "HandleCleanAppInstallControlRule failed");
193     }
194     return ret;
195 }
196 
HandleGetAppInstallControlRule(MessageParcel & data,MessageParcel & reply)197 ErrCode AppControlHost::HandleGetAppInstallControlRule(MessageParcel& data, MessageParcel& reply)
198 {
199     AppInstallControlRuleType controlRuleType = static_cast<AppInstallControlRuleType>(data.ReadInt32());
200     int32_t userId = data.ReadInt32();
201     std::vector<std::string> appIds;
202     int32_t ret = GetAppInstallControlRule(controlRuleType, userId, appIds);
203     if (ret != ERR_OK) {
204         LOG_E(BMS_TAG_DEFAULT, "HandleGetAppInstallControlRule failed");
205         return ret;
206     }
207     if (!WriteStringVector(appIds, reply)) {
208         LOG_E(BMS_TAG_DEFAULT, "write appIds failed");
209         return ERR_APPEXECFWK_PARCEL_ERROR;
210     }
211     return ERR_OK;
212 }
213 
HandleAddAppRunningControlRule(MessageParcel & data,MessageParcel & reply)214 ErrCode AppControlHost::HandleAddAppRunningControlRule(MessageParcel& data, MessageParcel& reply)
215 {
216     std::vector<AppRunningControlRule> controlRules;
217     auto ret = ReadParcelableVector(data, controlRules);
218     if (ret != ERR_OK) {
219         LOG_E(BMS_TAG_DEFAULT, "AddAppRunningControlRule read controlRuleParam failed");
220         return ret;
221     }
222     int32_t userId = data.ReadInt32();
223     return AddAppRunningControlRule(controlRules, userId);
224 }
225 
HandleDeleteAppRunningControlRule(MessageParcel & data,MessageParcel & reply)226 ErrCode AppControlHost::HandleDeleteAppRunningControlRule(MessageParcel& data, MessageParcel& reply)
227 {
228     std::vector<AppRunningControlRule> controlRules;
229     auto ret = ReadParcelableVector(data, controlRules);
230     if (ret != ERR_OK) {
231         LOG_E(BMS_TAG_DEFAULT, "DeleteAppRunningControlRule read controlRuleParam failed");
232         return ret;
233     }
234     int32_t userId = data.ReadInt32();
235     return DeleteAppRunningControlRule(controlRules, userId);
236 }
237 
HandleCleanAppRunningControlRule(MessageParcel & data,MessageParcel & reply)238 ErrCode AppControlHost::HandleCleanAppRunningControlRule(MessageParcel& data, MessageParcel& reply)
239 {
240     int32_t userId = data.ReadInt32();
241     int32_t ret = DeleteAppRunningControlRule(userId);
242     if (ret != ERR_OK) {
243         LOG_E(BMS_TAG_DEFAULT, "HandleCleanAppInstallControlRule failed");
244     }
245     return ret;
246 }
247 
HandleGetAppRunningControlRule(MessageParcel & data,MessageParcel & reply)248 ErrCode AppControlHost::HandleGetAppRunningControlRule(MessageParcel& data, MessageParcel& reply)
249 {
250     int32_t userId = data.ReadInt32();
251     std::vector<std::string> appIds;
252     int32_t ret = GetAppRunningControlRule(userId, appIds);
253     if (ret != ERR_OK) {
254         LOG_E(BMS_TAG_DEFAULT, "HandleGetAppRunningControlRule failed");
255         return ret;
256     }
257     if (!WriteStringVector(appIds, reply)) {
258         LOG_E(BMS_TAG_DEFAULT, "write appIds failed");
259         return ERR_APPEXECFWK_PARCEL_ERROR;
260     }
261     return ERR_OK;
262 }
263 
HandleGetAppRunningControlRuleResult(MessageParcel & data,MessageParcel & reply)264 ErrCode AppControlHost::HandleGetAppRunningControlRuleResult(MessageParcel& data, MessageParcel& reply)
265 {
266     std::string bundleName = data.ReadString();
267     int32_t userId = data.ReadInt32();
268     AppRunningControlRuleResult ruleResult;
269     int32_t ret = GetAppRunningControlRule(bundleName, userId, ruleResult);
270     if (ret != ERR_OK) {
271         LOG_E(BMS_TAG_DEFAULT, "HandleGetAppRunningControlRuleResult failed");
272     }
273     if (!reply.WriteInt32(ret)) {
274         LOG_E(BMS_TAG_DEFAULT, "write result failed");
275         return ERR_APPEXECFWK_PARCEL_ERROR;
276     }
277     if ((ret == ERR_OK) && !reply.WriteParcelable(&ruleResult)) {
278         LOG_E(BMS_TAG_DEFAULT, "write AppRunningControlRuleResult failed");
279         return ERR_APPEXECFWK_PARCEL_ERROR;
280     }
281     return ERR_OK;
282 }
283 
HandleConfirmAppJumpControlRule(MessageParcel & data,MessageParcel & reply)284 ErrCode AppControlHost::HandleConfirmAppJumpControlRule(MessageParcel& data, MessageParcel& reply)
285 {
286     std::string callerBundleName = data.ReadString();
287     std::string targetBundleName = data.ReadString();
288     int32_t userId = data.ReadInt32();
289     int32_t ret = ConfirmAppJumpControlRule(callerBundleName, targetBundleName, userId);
290     if (ret != ERR_OK) {
291         LOG_E(BMS_TAG_DEFAULT, "HandleConfirmAppJumpControlRule failed");
292     }
293     return ret;
294 }
295 
HandleAddAppJumpControlRule(MessageParcel & data,MessageParcel & reply)296 ErrCode AppControlHost::HandleAddAppJumpControlRule(MessageParcel& data, MessageParcel& reply)
297 {
298     std::vector<AppJumpControlRule> controlRules;
299     auto ret = ReadParcelableVector(data, controlRules);
300     if (ret != ERR_OK) {
301         LOG_E(BMS_TAG_DEFAULT, "HandleAddAppJumpControlRule read controlRuleParam failed");
302         return ret;
303     }
304     int32_t userId = data.ReadInt32();
305     return AddAppJumpControlRule(controlRules, userId);
306 }
307 
HandleDeleteAppJumpControlRule(MessageParcel & data,MessageParcel & reply)308 ErrCode AppControlHost::HandleDeleteAppJumpControlRule(MessageParcel& data, MessageParcel& reply)
309 {
310     std::vector<AppJumpControlRule> controlRules;
311     auto ret = ReadParcelableVector(data, controlRules);
312     if (ret != ERR_OK) {
313         LOG_E(BMS_TAG_DEFAULT, "HandleDeleteAppJumpControlRule read controlRuleParam failed");
314         return ret;
315     }
316     int32_t userId = data.ReadInt32();
317     return DeleteAppJumpControlRule(controlRules, userId);
318 }
319 
HandleDeleteRuleByCallerBundleName(MessageParcel & data,MessageParcel & reply)320 ErrCode AppControlHost::HandleDeleteRuleByCallerBundleName(MessageParcel& data, MessageParcel& reply)
321 {
322     std::string callerBundleName = data.ReadString();
323     int32_t userId = data.ReadInt32();
324     int32_t ret = DeleteRuleByCallerBundleName(callerBundleName, userId);
325     if (ret != ERR_OK) {
326         LOG_E(BMS_TAG_DEFAULT, "HandleDeleteRuleByCallerBundleName failed");
327     }
328     return ret;
329 }
330 
HandleDeleteRuleByTargetBundleName(MessageParcel & data,MessageParcel & reply)331 ErrCode AppControlHost::HandleDeleteRuleByTargetBundleName(MessageParcel& data, MessageParcel& reply)
332 {
333     std::string targetBundleName = data.ReadString();
334     int32_t userId = data.ReadInt32();
335     int32_t ret = DeleteRuleByTargetBundleName(targetBundleName, userId);
336     if (ret != ERR_OK) {
337         LOG_E(BMS_TAG_DEFAULT, "HandleDeleteRuleByTargetBundleName failed");
338     }
339     return ret;
340 }
341 
HandleGetAppJumpControlRule(MessageParcel & data,MessageParcel & reply)342 ErrCode AppControlHost::HandleGetAppJumpControlRule(MessageParcel& data, MessageParcel& reply)
343 {
344     std::string callerBundleName = data.ReadString();
345     std::string targetBundleName = data.ReadString();
346     int32_t userId = data.ReadInt32();
347     AppJumpControlRule rule;
348     int32_t ret = GetAppJumpControlRule(callerBundleName, targetBundleName, userId, rule);
349     if (ret != ERR_OK) {
350         LOG_E(BMS_TAG_DEFAULT, "HandleGetAppJumpControlRule failed");
351     }
352     if (!reply.WriteInt32(ret)) {
353         LOG_E(BMS_TAG_DEFAULT, "write result failed");
354         return ERR_APPEXECFWK_PARCEL_ERROR;
355     }
356     if ((ret == ERR_OK) && !reply.WriteParcelable(&rule)) {
357         LOG_E(BMS_TAG_DEFAULT, "write AppJumpControlRule failed");
358         return ERR_APPEXECFWK_PARCEL_ERROR;
359     }
360     return ERR_OK;
361 }
362 
HandleSetDisposedStatus(MessageParcel & data,MessageParcel & reply)363 ErrCode AppControlHost::HandleSetDisposedStatus(MessageParcel& data, MessageParcel& reply)
364 {
365     std::string appId = data.ReadString();
366     std::unique_ptr<Want> want(data.ReadParcelable<Want>());
367     int32_t userId = data.ReadInt32();
368     if (want == nullptr) {
369         LOG_E(BMS_TAG_DEFAULT, "ReadParcelable<Want> failed");
370         return ERR_APPEXECFWK_PARCEL_ERROR;
371     }
372     ErrCode ret = SetDisposedStatus(appId, *want, userId);
373     if (!reply.WriteInt32(ret)) {
374         LOG_E(BMS_TAG_DEFAULT, "write ret failed");
375         return ERR_APPEXECFWK_PARCEL_ERROR;
376     }
377     return ERR_OK;
378 }
379 
HandleDeleteDisposedStatus(MessageParcel & data,MessageParcel & reply)380 ErrCode AppControlHost::HandleDeleteDisposedStatus(MessageParcel& data, MessageParcel &reply)
381 {
382     std::string appId = data.ReadString();
383     int32_t userId = data.ReadInt32();
384     ErrCode ret = DeleteDisposedStatus(appId, userId);
385     if (!reply.WriteInt32(ret)) {
386         LOG_E(BMS_TAG_DEFAULT, "write ret failed");
387         return ERR_APPEXECFWK_PARCEL_ERROR;
388     }
389     return ERR_OK;
390 }
391 
HandleGetDisposedStatus(MessageParcel & data,MessageParcel & reply)392 ErrCode AppControlHost::HandleGetDisposedStatus(MessageParcel& data, MessageParcel &reply)
393 {
394     std::string appId = data.ReadString();
395     int32_t userId = data.ReadInt32();
396     Want want;
397     ErrCode ret = GetDisposedStatus(appId, want, userId);
398     if (!reply.WriteInt32(ret)) {
399         LOG_E(BMS_TAG_DEFAULT, "write ret failed");
400         return ERR_APPEXECFWK_PARCEL_ERROR;
401     }
402     if (ret == ERR_OK) {
403         if (!reply.WriteParcelable(&want)) {
404             LOG_E(BMS_TAG_DEFAULT, "write failed");
405             return ERR_APPEXECFWK_PARCEL_ERROR;
406         }
407     }
408     return ERR_OK;
409 }
410 
HandleGetDisposedRule(MessageParcel & data,MessageParcel & reply)411 ErrCode AppControlHost::HandleGetDisposedRule(MessageParcel& data, MessageParcel &reply)
412 {
413     std::string appId = data.ReadString();
414     int32_t userId = data.ReadInt32();
415     DisposedRule rule;
416     ErrCode ret = GetDisposedRule(appId, rule, userId);
417     if (!reply.WriteInt32(ret)) {
418         LOG_E(BMS_TAG_DEFAULT, "write ret failed");
419         return ERR_APPEXECFWK_PARCEL_ERROR;
420     }
421     if (ret == ERR_OK) {
422         if (!reply.WriteParcelable(&rule)) {
423             LOG_E(BMS_TAG_DEFAULT, "write failed");
424             return ERR_APPEXECFWK_PARCEL_ERROR;
425         }
426     }
427     return ERR_OK;
428 }
429 
HandleSetDisposedRule(MessageParcel & data,MessageParcel & reply)430 ErrCode AppControlHost::HandleSetDisposedRule(MessageParcel& data, MessageParcel& reply)
431 {
432     std::string appId = data.ReadString();
433     std::unique_ptr<DisposedRule> disposedRule(data.ReadParcelable<DisposedRule>());
434     int32_t userId = data.ReadInt32();
435     if (disposedRule == nullptr) {
436         LOG_E(BMS_TAG_DEFAULT, "ReadParcelable<disposedRule> failed");
437         return ERR_APPEXECFWK_PARCEL_ERROR;
438     }
439     ErrCode ret = SetDisposedRule(appId, *disposedRule, userId);
440     if (!reply.WriteInt32(ret)) {
441         LOG_E(BMS_TAG_DEFAULT, "write ret failed");
442         return ERR_APPEXECFWK_PARCEL_ERROR;
443     }
444     return ERR_OK;
445 }
446 
HandleSetDisposedRules(MessageParcel & data,MessageParcel & reply)447 ErrCode AppControlHost::HandleSetDisposedRules(MessageParcel& data, MessageParcel& reply)
448 {
449     std::vector<DisposedRuleConfiguration> disposedRuleConfigurations;
450     auto ret = GetVectorParcelInfo(data, disposedRuleConfigurations);
451     if (ret != ERR_OK) {
452         LOG_E(BMS_TAG_DEFAULT, "HandleSetDisposedRules read DisposedRuleConfiguration failed");
453         return ret;
454     }
455     if (disposedRuleConfigurations.empty() || disposedRuleConfigurations.size() > MAX_VECTOR_NUM) {
456         LOG_E(BMS_TAG_DEFAULT, "disposedRuleConfiguration count is error");
457         return ERR_BUNDLE_MANAGER_PARAM_ERROR;
458     }
459     int32_t userId = data.ReadInt32();
460     ret = SetDisposedRules(disposedRuleConfigurations, userId);
461     if (!reply.WriteInt32(ret)) {
462         LOG_E(BMS_TAG_DEFAULT, "write ret failed");
463         return ERR_APPEXECFWK_PARCEL_ERROR;
464     }
465     return ERR_OK;
466 }
467 
HandleGetAbilityRunningControlRule(MessageParcel & data,MessageParcel & reply)468 ErrCode AppControlHost::HandleGetAbilityRunningControlRule(MessageParcel& data, MessageParcel& reply)
469 {
470     std::string bundleName = data.ReadString();
471     int32_t userId = data.ReadInt32();
472     int32_t appIndex = data.ReadInt32();
473     std::vector<DisposedRule> rules;
474     ErrCode ret = GetAbilityRunningControlRule(bundleName, userId, rules, appIndex);
475     if (!reply.WriteInt32(ret)) {
476         LOG_E(BMS_TAG_DEFAULT, "write ret failed");
477         return ERR_APPEXECFWK_PARCEL_ERROR;
478     }
479     if (ret == ERR_OK) {
480         if (!WriteParcelableVector(rules, reply)) {
481             LOG_E(BMS_TAG_DEFAULT, "write failed");
482             return ERR_APPEXECFWK_PARCEL_ERROR;
483         }
484     }
485     return ERR_OK;
486 }
487 
HandleGetDisposedRuleForCloneApp(MessageParcel & data,MessageParcel & reply)488 ErrCode AppControlHost::HandleGetDisposedRuleForCloneApp(MessageParcel& data, MessageParcel &reply)
489 {
490     std::string appId = data.ReadString();
491     int32_t userId = data.ReadInt32();
492     int32_t appIndex = data.ReadInt32();
493     DisposedRule rule;
494     ErrCode ret = GetDisposedRuleForCloneApp(appId, rule, appIndex, userId);
495     if (!reply.WriteInt32(ret)) {
496         LOG_E(BMS_TAG_DEFAULT, "write ret failed");
497         return ERR_APPEXECFWK_PARCEL_ERROR;
498     }
499     if (ret == ERR_OK) {
500         if (!reply.WriteParcelable(&rule)) {
501             LOG_E(BMS_TAG_DEFAULT, "write failed");
502             return ERR_APPEXECFWK_PARCEL_ERROR;
503         }
504     }
505     return ERR_OK;
506 }
507 
HandleSetDisposedRuleForCloneApp(MessageParcel & data,MessageParcel & reply)508 ErrCode AppControlHost::HandleSetDisposedRuleForCloneApp(MessageParcel& data, MessageParcel& reply)
509 {
510     std::string appId = data.ReadString();
511     std::unique_ptr<DisposedRule> disposedRule(data.ReadParcelable<DisposedRule>());
512     int32_t userId = data.ReadInt32();
513     int32_t appIndex = data.ReadInt32();
514     if (disposedRule == nullptr) {
515         LOG_E(BMS_TAG_DEFAULT, "ReadParcelable<disposedRule> failed");
516         return ERR_APPEXECFWK_PARCEL_ERROR;
517     }
518     ErrCode ret = SetDisposedRuleForCloneApp(appId, *disposedRule, appIndex, userId);
519     if (!reply.WriteInt32(ret)) {
520         LOG_E(BMS_TAG_DEFAULT, "write ret failed");
521         return ERR_APPEXECFWK_PARCEL_ERROR;
522     }
523     return ERR_OK;
524 }
525 
HandleDeleteDisposedRuleForCloneApp(MessageParcel & data,MessageParcel & reply)526 ErrCode AppControlHost::HandleDeleteDisposedRuleForCloneApp(MessageParcel& data, MessageParcel& reply)
527 {
528     std::string appId = data.ReadString();
529     int32_t userId = data.ReadInt32();
530     int32_t appIndex = data.ReadInt32();
531     ErrCode ret = DeleteDisposedRuleForCloneApp(appId, appIndex, userId);
532     if (!reply.WriteInt32(ret)) {
533         LOG_E(BMS_TAG_DEFAULT, "write ret failed");
534         return ERR_APPEXECFWK_PARCEL_ERROR;
535     }
536     return ERR_OK;
537 }
538 
WriteStringVector(const std::vector<std::string> & stringVector,MessageParcel & reply)539 bool AppControlHost::WriteStringVector(const std::vector<std::string> &stringVector, MessageParcel &reply)
540 {
541     if (!reply.WriteInt32(stringVector.size())) {
542         LOG_E(BMS_TAG_DEFAULT, "write ParcelableVector failed");
543         return false;
544     }
545 
546     for (auto &string : stringVector) {
547         if (!reply.WriteString(string)) {
548             LOG_E(BMS_TAG_DEFAULT, "write string failed");
549             return false;
550         }
551     }
552     return true;
553 }
554 
555 template<typename T>
WriteParcelableVector(std::vector<T> & parcelableVector,MessageParcel & reply)556 bool AppControlHost::WriteParcelableVector(std::vector<T> &parcelableVector, MessageParcel &reply)
557 {
558     if (!reply.WriteInt32(parcelableVector.size())) {
559         LOG_E(BMS_TAG_DEFAULT, "write ParcelableVector failed");
560         return false;
561     }
562 
563     for (auto &parcelable : parcelableVector) {
564         if (!reply.WriteParcelable(&parcelable)) {
565             LOG_E(BMS_TAG_DEFAULT, "write ParcelableVector failed");
566             return false;
567         }
568     }
569     return true;
570 }
571 
572 template<typename T>
ReadParcelableVector(MessageParcel & data,std::vector<T> & parcelableInfos)573 ErrCode AppControlHost::ReadParcelableVector(MessageParcel &data, std::vector<T> &parcelableInfos)
574 {
575     int32_t infoSize = data.ReadInt32();
576     if (infoSize > AppControlConstants::LIST_MAX_SIZE) {
577         LOG_E(BMS_TAG_DEFAULT, "elements num exceeds the limit %{public}d", AppControlConstants::LIST_MAX_SIZE);
578         return ERR_BUNDLE_MANAGER_INVALID_PARAMETER;
579     }
580     for (int32_t i = 0; i < infoSize; i++) {
581         std::unique_ptr<T> info(data.ReadParcelable<T>());
582         if (info == nullptr) {
583             LOG_E(BMS_TAG_DEFAULT, "read parcelable infos failed");
584             return ERR_APPEXECFWK_PARCEL_ERROR;
585         }
586         parcelableInfos.emplace_back(*info);
587     }
588     LOG_D(BMS_TAG_DEFAULT, "read parcelable infos success");
589     return ERR_OK;
590 }
591 
HandleGetUninstallDisposedRule(MessageParcel & data,MessageParcel & reply)592 ErrCode AppControlHost::HandleGetUninstallDisposedRule(MessageParcel& data, MessageParcel& reply)
593 {
594     std::string appIdentifier = data.ReadString();
595     int32_t userId = data.ReadInt32();
596     int32_t appIndex = data.ReadInt32();
597     UninstallDisposedRule rule;
598     ErrCode ret = GetUninstallDisposedRule(appIdentifier, appIndex, userId, rule);
599     if (!reply.WriteInt32(ret)) {
600         LOG_E(BMS_TAG_DEFAULT, "write ret failed");
601         return ERR_APPEXECFWK_PARCEL_ERROR;
602     }
603     if (ret == ERR_OK) {
604         if (!reply.WriteParcelable(&rule)) {
605             LOG_E(BMS_TAG_DEFAULT, "write failed");
606             return ERR_APPEXECFWK_PARCEL_ERROR;
607         }
608     }
609     return ERR_OK;
610 }
611 
HandleSetUninstallDisposedRule(MessageParcel & data,MessageParcel & reply)612 ErrCode AppControlHost::HandleSetUninstallDisposedRule(MessageParcel& data, MessageParcel& reply)
613 {
614     std::string appIdentifier = data.ReadString();
615     std::unique_ptr<UninstallDisposedRule> uninstallDisposedRule(data.ReadParcelable<UninstallDisposedRule>());
616     if (uninstallDisposedRule == nullptr) {
617         LOG_E(BMS_TAG_DEFAULT, "ReadParcelable<uninstalldisposedRule> failed");
618         return ERR_APPEXECFWK_PARCEL_ERROR;
619     }
620     int32_t userId = data.ReadInt32();
621     int32_t appIndex = data.ReadInt32();
622     ErrCode ret = SetUninstallDisposedRule(appIdentifier, *uninstallDisposedRule, appIndex, userId);
623     if (!reply.WriteInt32(ret)) {
624         LOG_E(BMS_TAG_DEFAULT, "write ret failed");
625         return ERR_APPEXECFWK_PARCEL_ERROR;
626     }
627     return ERR_OK;
628 }
629 
HandleDeleteUninstallDisposedRule(MessageParcel & data,MessageParcel & reply)630 ErrCode AppControlHost::HandleDeleteUninstallDisposedRule(MessageParcel& data, MessageParcel& reply)
631 {
632     std::string appIdentifier = data.ReadString();
633     int32_t userId = data.ReadInt32();
634     int32_t appIndex = data.ReadInt32();
635     ErrCode ret = DeleteUninstallDisposedRule(appIdentifier, appIndex, userId);
636     if (!reply.WriteInt32(ret)) {
637         LOG_E(BMS_TAG_DEFAULT, "write ret failed");
638         return ERR_APPEXECFWK_PARCEL_ERROR;
639     }
640     return ERR_OK;
641 }
642 
643 template<typename T>
GetVectorParcelInfo(MessageParcel & data,std::vector<T> & parcelInfos)644 ErrCode AppControlHost::GetVectorParcelInfo(MessageParcel &data, std::vector<T> &parcelInfos)
645 {
646     size_t dataSize = data.ReadUint32();
647     if (dataSize == 0) {
648         APP_LOGW("Parcel no data");
649         return ERR_OK;
650     }
651 
652     void *buffer = nullptr;
653     if (dataSize > MAX_IPC_ALLOWED_CAPACITY) {
654         APP_LOGE("dataSize is too large");
655         return ERR_BUNDLE_MANAGER_PARAM_ERROR;
656     } else {
657         if (!GetData(dataSize, data.ReadRawData(dataSize), buffer)) {
658             APP_LOGE("GetData failed dataSize: %{public}zu", dataSize);
659             return ERR_APPEXECFWK_PARCEL_ERROR;
660         }
661     }
662 
663     MessageParcel tempParcel;
664     if (!tempParcel.ParseFrom(reinterpret_cast<uintptr_t>(buffer), dataSize)) {
665         APP_LOGE("Fail to ParseFrom");
666         return ERR_APPEXECFWK_PARCEL_ERROR;
667     }
668 
669     int32_t infoSize = tempParcel.ReadInt32();
670     CONTAINER_SECURITY_VERIFY(tempParcel, infoSize, &parcelInfos);
671     for (int32_t i = 0; i < infoSize; i++) {
672         std::unique_ptr<T> info(tempParcel.ReadParcelable<T>());
673         if (info == nullptr) {
674             APP_LOGE("Read Parcelable infos failed");
675             return ERR_APPEXECFWK_PARCEL_ERROR;
676         }
677         parcelInfos.emplace_back(*info);
678     }
679 
680     return ERR_OK;
681 }
682 } // AppExecFwk
683 } // OHOS
684