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 "app_control_host.h"
17 
18 #include "app_control_constants.h"
19 #include "app_log_tag_wrapper.h"
20 #include "app_log_wrapper.h"
21 #include "appexecfwk_errors.h"
22 #include "bundle_framework_core_ipc_interface_code.h"
23 #include "bundle_memory_guard.h"
24 #include "disposed_rule.h"
25 #include "ipc_types.h"
26 
27 namespace OHOS {
28 namespace AppExecFwk {
AppControlHost()29 AppControlHost::AppControlHost()
30 {
31     LOG_D(BMS_TAG_DEFAULT, "create AppControlHost");
32 }
33 
~AppControlHost()34 AppControlHost::~AppControlHost()
35 {
36     LOG_D(BMS_TAG_DEFAULT, "destroy AppControlHost");
37 }
38 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)39 int AppControlHost::OnRemoteRequest(
40     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
41 {
42     BundleMemoryGuard memoryGuard;
43     LOG_I(BMS_TAG_DEFAULT, "AppControlHost OnRemoteRequest, message code : %{public}u", code);
44     std::u16string descriptor = AppControlHost::GetDescriptor();
45     std::u16string remoteDescriptor = data.ReadInterfaceToken();
46     if (descriptor != remoteDescriptor) {
47         LOG_E(BMS_TAG_DEFAULT, "descriptor invalid");
48         return OBJECT_NULL;
49     }
50 
51     switch (code) {
52         case static_cast<uint32_t>(AppControlManagerInterfaceCode::ADD_APP_INSTALL_CONTROL_RULE):
53             return HandleAddAppInstallControlRule(data, reply);
54         case static_cast<uint32_t>(AppControlManagerInterfaceCode::DELETE_APP_INSTALL_CONTROL_RULE):
55             return HandleDeleteAppInstallControlRule(data, reply);
56         case static_cast<uint32_t>(AppControlManagerInterfaceCode::CLEAN_APP_INSTALL_CONTROL_RULE):
57             return HandleCleanAppInstallControlRule(data, reply);
58         case static_cast<uint32_t>(AppControlManagerInterfaceCode::GET_APP_INSTALL_CONTROL_RULE):
59             return HandleGetAppInstallControlRule(data, reply);
60         case static_cast<uint32_t>(AppControlManagerInterfaceCode::ADD_APP_RUNNING_CONTROL_RULE):
61             return HandleAddAppRunningControlRule(data, reply);
62         case static_cast<uint32_t>(AppControlManagerInterfaceCode::DELETE_APP_RUNNING_CONTROL_RULE):
63             return HandleDeleteAppRunningControlRule(data, reply);
64         case static_cast<uint32_t>(AppControlManagerInterfaceCode::CLEAN_APP_RUNNING_CONTROL_RULE):
65             return HandleCleanAppRunningControlRule(data, reply);
66         case static_cast<uint32_t>(AppControlManagerInterfaceCode::GET_APP_RUNNING_CONTROL_RULE):
67             return HandleGetAppRunningControlRule(data, reply);
68         case static_cast<uint32_t>(AppControlManagerInterfaceCode::GET_APP_RUNNING_CONTROL_RULE_RESULT):
69             return HandleGetAppRunningControlRuleResult(data, reply);
70         case static_cast<uint32_t>(AppControlManagerInterfaceCode::CONFIRM_APP_JUMP_CONTROL_RULE):
71             return HandleConfirmAppJumpControlRule(data, reply);
72         case static_cast<uint32_t>(AppControlManagerInterfaceCode::ADD_APP_JUMP_CONTROL_RULE):
73             return HandleAddAppJumpControlRule(data, reply);
74         case static_cast<uint32_t>(AppControlManagerInterfaceCode::DELETE_APP_JUMP_CONTROL_RULE):
75             return HandleDeleteAppJumpControlRule(data, reply);
76         case static_cast<uint32_t>(AppControlManagerInterfaceCode::DELETE_APP_JUMP_CONTROL_RULE_BY_CALLER):
77             return HandleDeleteRuleByCallerBundleName(data, reply);
78         case static_cast<uint32_t>(AppControlManagerInterfaceCode::DELETE_APP_JUMP_CONTROL_RULE_BY_TARGET):
79             return HandleDeleteRuleByTargetBundleName(data, reply);
80         case static_cast<uint32_t>(AppControlManagerInterfaceCode::GET_APP_JUMP_CONTROL_RULE):
81             return HandleGetAppJumpControlRule(data, reply);
82         case static_cast<uint32_t>(AppControlManagerInterfaceCode::SET_DISPOSED_STATUS):
83             return HandleSetDisposedStatus(data, reply);
84         case static_cast<uint32_t>(AppControlManagerInterfaceCode::GET_DISPOSED_STATUS):
85             return HandleGetDisposedStatus(data, reply);
86         case static_cast<uint32_t>(AppControlManagerInterfaceCode::DELETE_DISPOSED_STATUS):
87             return HandleDeleteDisposedStatus(data, reply);
88         case static_cast<uint32_t>(AppControlManagerInterfaceCode::SET_DISPOSED_RULE):
89             return HandleSetDisposedRule(data, reply);
90         case static_cast<uint32_t>(AppControlManagerInterfaceCode::GET_DISPOSED_RULE):
91             return HandleGetDisposedRule(data, reply);
92         case static_cast<uint32_t>(AppControlManagerInterfaceCode::GET_ABILITY_RUNNING_CONTROL_RULE):
93             return HandleGetAbilityRunningControlRule(data, reply);
94         case static_cast<uint32_t>(AppControlManagerInterfaceCode::GET_DISPOSED_RULE_FOR_CLONE_APP):
95             return HandleGetDisposedRuleForCloneApp(data, reply);
96         case static_cast<uint32_t>(AppControlManagerInterfaceCode::SET_DISPOSED_RULE_FOR_CLONE_APP):
97             return HandleSetDisposedRuleForCloneApp(data, reply);
98         case static_cast<uint32_t>(AppControlManagerInterfaceCode::DELETE_DISPOSED_RULE_FOR_CLONE_APP):
99             return HandleDeleteDisposedRuleForCloneApp(data, reply);
100         default:
101             LOG_W(BMS_TAG_DEFAULT, "AppControlHost receive unknown code, code = %{public}d", code);
102             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
103     }
104 }
105 
HandleAddAppInstallControlRule(MessageParcel & data,MessageParcel & reply)106 ErrCode AppControlHost::HandleAddAppInstallControlRule(MessageParcel& data, MessageParcel& reply)
107 {
108     std::vector<std::string> appIds;
109     int32_t appIdSize = data.ReadInt32();
110     if (appIdSize > AppControlConstants::LIST_MAX_SIZE || appIdSize < 0) {
111         LOG_E(BMS_TAG_DEFAULT, "HandleAddAppInstallControlRule parameter is invalid");
112         return ERR_BUNDLE_MANAGER_INVALID_PARAMETER;
113     }
114     for (int32_t i = 0; i < appIdSize; i++) {
115         appIds.emplace_back(data.ReadString());
116     }
117     AppInstallControlRuleType controlRuleType = static_cast<AppInstallControlRuleType>(data.ReadInt32());
118     int32_t userId = data.ReadInt32();
119     int32_t ret = AddAppInstallControlRule(appIds, controlRuleType, userId);
120     if (ret != ERR_OK) {
121         LOG_E(BMS_TAG_DEFAULT, "HandleAddAppInstallControlRule failed");
122     }
123     return ret;
124 }
125 
HandleDeleteAppInstallControlRule(MessageParcel & data,MessageParcel & reply)126 ErrCode AppControlHost::HandleDeleteAppInstallControlRule(MessageParcel& data, MessageParcel& reply)
127 {
128     AppInstallControlRuleType controlRuleType = static_cast<AppInstallControlRuleType>(data.ReadInt32());
129     std::vector<std::string> appIds;
130     int32_t appIdSize = data.ReadInt32();
131     if (appIdSize > AppControlConstants::LIST_MAX_SIZE || appIdSize < 0) {
132         LOG_E(BMS_TAG_DEFAULT, "HandleDeleteAppInstallControlRule parameter is invalid");
133         return ERR_BUNDLE_MANAGER_INVALID_PARAMETER;
134     }
135     for (int32_t i = 0; i < appIdSize; i++) {
136         appIds.emplace_back(data.ReadString());
137     }
138     int32_t userId = data.ReadInt32();
139     int32_t ret = DeleteAppInstallControlRule(controlRuleType, appIds, userId);
140     if (ret != ERR_OK) {
141         LOG_E(BMS_TAG_DEFAULT, "HandleDeleteAppInstallControlRule failed");
142     }
143     return ret;
144 }
145 
HandleCleanAppInstallControlRule(MessageParcel & data,MessageParcel & reply)146 ErrCode AppControlHost::HandleCleanAppInstallControlRule(MessageParcel& data, MessageParcel& reply)
147 {
148     AppInstallControlRuleType controlRuleType = static_cast<AppInstallControlRuleType>(data.ReadInt32());
149     int32_t userId = data.ReadInt32();
150     int32_t ret = DeleteAppInstallControlRule(controlRuleType, userId);
151     if (ret != ERR_OK) {
152         LOG_E(BMS_TAG_DEFAULT, "HandleCleanAppInstallControlRule failed");
153     }
154     return ret;
155 }
156 
HandleGetAppInstallControlRule(MessageParcel & data,MessageParcel & reply)157 ErrCode AppControlHost::HandleGetAppInstallControlRule(MessageParcel& data, MessageParcel& reply)
158 {
159     AppInstallControlRuleType controlRuleType = static_cast<AppInstallControlRuleType>(data.ReadInt32());
160     int32_t userId = data.ReadInt32();
161     std::vector<std::string> appIds;
162     int32_t ret = GetAppInstallControlRule(controlRuleType, userId, appIds);
163     if (ret != ERR_OK) {
164         LOG_E(BMS_TAG_DEFAULT, "HandleGetAppInstallControlRule failed");
165         return ret;
166     }
167     if (!WriteStringVector(appIds, reply)) {
168         LOG_E(BMS_TAG_DEFAULT, "write appIds failed");
169         return ERR_APPEXECFWK_PARCEL_ERROR;
170     }
171     return ERR_OK;
172 }
173 
HandleAddAppRunningControlRule(MessageParcel & data,MessageParcel & reply)174 ErrCode AppControlHost::HandleAddAppRunningControlRule(MessageParcel& data, MessageParcel& reply)
175 {
176     std::vector<AppRunningControlRule> controlRules;
177     auto ret = ReadParcelableVector(data, controlRules);
178     if (ret != ERR_OK) {
179         LOG_E(BMS_TAG_DEFAULT, "AddAppRunningControlRule read controlRuleParam failed");
180         return ret;
181     }
182     int32_t userId = data.ReadInt32();
183     return AddAppRunningControlRule(controlRules, userId);
184 }
185 
HandleDeleteAppRunningControlRule(MessageParcel & data,MessageParcel & reply)186 ErrCode AppControlHost::HandleDeleteAppRunningControlRule(MessageParcel& data, MessageParcel& reply)
187 {
188     std::vector<AppRunningControlRule> controlRules;
189     auto ret = ReadParcelableVector(data, controlRules);
190     if (ret != ERR_OK) {
191         LOG_E(BMS_TAG_DEFAULT, "DeleteAppRunningControlRule read controlRuleParam failed");
192         return ret;
193     }
194     int32_t userId = data.ReadInt32();
195     return DeleteAppRunningControlRule(controlRules, userId);
196 }
197 
HandleCleanAppRunningControlRule(MessageParcel & data,MessageParcel & reply)198 ErrCode AppControlHost::HandleCleanAppRunningControlRule(MessageParcel& data, MessageParcel& reply)
199 {
200     int32_t userId = data.ReadInt32();
201     int32_t ret = DeleteAppRunningControlRule(userId);
202     if (ret != ERR_OK) {
203         LOG_E(BMS_TAG_DEFAULT, "HandleCleanAppInstallControlRule failed");
204     }
205     return ret;
206 }
207 
HandleGetAppRunningControlRule(MessageParcel & data,MessageParcel & reply)208 ErrCode AppControlHost::HandleGetAppRunningControlRule(MessageParcel& data, MessageParcel& reply)
209 {
210     int32_t userId = data.ReadInt32();
211     std::vector<std::string> appIds;
212     int32_t ret = GetAppRunningControlRule(userId, appIds);
213     if (ret != ERR_OK) {
214         LOG_E(BMS_TAG_DEFAULT, "HandleGetAppRunningControlRule failed");
215         return ret;
216     }
217     if (!WriteStringVector(appIds, reply)) {
218         LOG_E(BMS_TAG_DEFAULT, "write appIds failed");
219         return ERR_APPEXECFWK_PARCEL_ERROR;
220     }
221     return ERR_OK;
222 }
223 
HandleGetAppRunningControlRuleResult(MessageParcel & data,MessageParcel & reply)224 ErrCode AppControlHost::HandleGetAppRunningControlRuleResult(MessageParcel& data, MessageParcel& reply)
225 {
226     std::string bundleName = data.ReadString();
227     int32_t userId = data.ReadInt32();
228     AppRunningControlRuleResult ruleResult;
229     int32_t ret = GetAppRunningControlRule(bundleName, userId, ruleResult);
230     if (ret != ERR_OK) {
231         LOG_E(BMS_TAG_DEFAULT, "HandleGetAppRunningControlRuleResult failed");
232     }
233     if (!reply.WriteInt32(ret)) {
234         LOG_E(BMS_TAG_DEFAULT, "write result failed");
235         return ERR_APPEXECFWK_PARCEL_ERROR;
236     }
237     if ((ret == ERR_OK) && !reply.WriteParcelable(&ruleResult)) {
238         LOG_E(BMS_TAG_DEFAULT, "write AppRunningControlRuleResult failed");
239         return ERR_APPEXECFWK_PARCEL_ERROR;
240     }
241     return ERR_OK;
242 }
243 
HandleConfirmAppJumpControlRule(MessageParcel & data,MessageParcel & reply)244 ErrCode AppControlHost::HandleConfirmAppJumpControlRule(MessageParcel& data, MessageParcel& reply)
245 {
246     std::string callerBundleName = data.ReadString();
247     std::string targetBundleName = data.ReadString();
248     int32_t userId = data.ReadInt32();
249     int32_t ret = ConfirmAppJumpControlRule(callerBundleName, targetBundleName, userId);
250     if (ret != ERR_OK) {
251         LOG_E(BMS_TAG_DEFAULT, "HandleConfirmAppJumpControlRule failed");
252     }
253     return ret;
254 }
255 
HandleAddAppJumpControlRule(MessageParcel & data,MessageParcel & reply)256 ErrCode AppControlHost::HandleAddAppJumpControlRule(MessageParcel& data, MessageParcel& reply)
257 {
258     std::vector<AppJumpControlRule> controlRules;
259     auto ret = ReadParcelableVector(data, controlRules);
260     if (ret != ERR_OK) {
261         LOG_E(BMS_TAG_DEFAULT, "HandleAddAppJumpControlRule read controlRuleParam failed");
262         return ret;
263     }
264     int32_t userId = data.ReadInt32();
265     return AddAppJumpControlRule(controlRules, userId);
266 }
267 
HandleDeleteAppJumpControlRule(MessageParcel & data,MessageParcel & reply)268 ErrCode AppControlHost::HandleDeleteAppJumpControlRule(MessageParcel& data, MessageParcel& reply)
269 {
270     std::vector<AppJumpControlRule> controlRules;
271     auto ret = ReadParcelableVector(data, controlRules);
272     if (ret != ERR_OK) {
273         LOG_E(BMS_TAG_DEFAULT, "HandleDeleteAppJumpControlRule read controlRuleParam failed");
274         return ret;
275     }
276     int32_t userId = data.ReadInt32();
277     return DeleteAppJumpControlRule(controlRules, userId);
278 }
279 
HandleDeleteRuleByCallerBundleName(MessageParcel & data,MessageParcel & reply)280 ErrCode AppControlHost::HandleDeleteRuleByCallerBundleName(MessageParcel& data, MessageParcel& reply)
281 {
282     std::string callerBundleName = data.ReadString();
283     int32_t userId = data.ReadInt32();
284     int32_t ret = DeleteRuleByCallerBundleName(callerBundleName, userId);
285     if (ret != ERR_OK) {
286         LOG_E(BMS_TAG_DEFAULT, "HandleDeleteRuleByCallerBundleName failed");
287     }
288     return ret;
289 }
290 
HandleDeleteRuleByTargetBundleName(MessageParcel & data,MessageParcel & reply)291 ErrCode AppControlHost::HandleDeleteRuleByTargetBundleName(MessageParcel& data, MessageParcel& reply)
292 {
293     std::string targetBundleName = data.ReadString();
294     int32_t userId = data.ReadInt32();
295     int32_t ret = DeleteRuleByTargetBundleName(targetBundleName, userId);
296     if (ret != ERR_OK) {
297         LOG_E(BMS_TAG_DEFAULT, "HandleDeleteRuleByTargetBundleName failed");
298     }
299     return ret;
300 }
301 
HandleGetAppJumpControlRule(MessageParcel & data,MessageParcel & reply)302 ErrCode AppControlHost::HandleGetAppJumpControlRule(MessageParcel& data, MessageParcel& reply)
303 {
304     std::string callerBundleName = data.ReadString();
305     std::string targetBundleName = data.ReadString();
306     int32_t userId = data.ReadInt32();
307     AppJumpControlRule rule;
308     int32_t ret = GetAppJumpControlRule(callerBundleName, targetBundleName, userId, rule);
309     if (ret != ERR_OK) {
310         LOG_E(BMS_TAG_DEFAULT, "HandleGetAppJumpControlRule failed");
311     }
312     if (!reply.WriteInt32(ret)) {
313         LOG_E(BMS_TAG_DEFAULT, "write result failed");
314         return ERR_APPEXECFWK_PARCEL_ERROR;
315     }
316     if ((ret == ERR_OK) && !reply.WriteParcelable(&rule)) {
317         LOG_E(BMS_TAG_DEFAULT, "write AppJumpControlRule failed");
318         return ERR_APPEXECFWK_PARCEL_ERROR;
319     }
320     return ERR_OK;
321 }
322 
HandleSetDisposedStatus(MessageParcel & data,MessageParcel & reply)323 ErrCode AppControlHost::HandleSetDisposedStatus(MessageParcel& data, MessageParcel& reply)
324 {
325     std::string appId = data.ReadString();
326     std::unique_ptr<Want> want(data.ReadParcelable<Want>());
327     int32_t userId = data.ReadInt32();
328     if (want == nullptr) {
329         LOG_E(BMS_TAG_DEFAULT, "ReadParcelable<Want> failed");
330         return ERR_APPEXECFWK_PARCEL_ERROR;
331     }
332     ErrCode ret = SetDisposedStatus(appId, *want, userId);
333     if (!reply.WriteInt32(ret)) {
334         LOG_E(BMS_TAG_DEFAULT, "write ret failed");
335         return ERR_APPEXECFWK_PARCEL_ERROR;
336     }
337     return ERR_OK;
338 }
339 
HandleDeleteDisposedStatus(MessageParcel & data,MessageParcel & reply)340 ErrCode AppControlHost::HandleDeleteDisposedStatus(MessageParcel& data, MessageParcel &reply)
341 {
342     std::string appId = data.ReadString();
343     int32_t userId = data.ReadInt32();
344     ErrCode ret = DeleteDisposedStatus(appId, userId);
345     if (!reply.WriteInt32(ret)) {
346         LOG_E(BMS_TAG_DEFAULT, "write ret failed");
347         return ERR_APPEXECFWK_PARCEL_ERROR;
348     }
349     return ERR_OK;
350 }
351 
HandleGetDisposedStatus(MessageParcel & data,MessageParcel & reply)352 ErrCode AppControlHost::HandleGetDisposedStatus(MessageParcel& data, MessageParcel &reply)
353 {
354     std::string appId = data.ReadString();
355     int32_t userId = data.ReadInt32();
356     Want want;
357     ErrCode ret = GetDisposedStatus(appId, want, userId);
358     if (!reply.WriteInt32(ret)) {
359         LOG_E(BMS_TAG_DEFAULT, "write ret failed");
360         return ERR_APPEXECFWK_PARCEL_ERROR;
361     }
362     if (ret == ERR_OK) {
363         if (!reply.WriteParcelable(&want)) {
364             LOG_E(BMS_TAG_DEFAULT, "write failed");
365             return ERR_APPEXECFWK_PARCEL_ERROR;
366         }
367     }
368     return ERR_OK;
369 }
370 
HandleGetDisposedRule(MessageParcel & data,MessageParcel & reply)371 ErrCode AppControlHost::HandleGetDisposedRule(MessageParcel& data, MessageParcel &reply)
372 {
373     std::string appId = data.ReadString();
374     int32_t userId = data.ReadInt32();
375     DisposedRule rule;
376     ErrCode ret = GetDisposedRule(appId, rule, userId);
377     if (!reply.WriteInt32(ret)) {
378         LOG_E(BMS_TAG_DEFAULT, "write ret failed");
379         return ERR_APPEXECFWK_PARCEL_ERROR;
380     }
381     if (ret == ERR_OK) {
382         if (!reply.WriteParcelable(&rule)) {
383             LOG_E(BMS_TAG_DEFAULT, "write failed");
384             return ERR_APPEXECFWK_PARCEL_ERROR;
385         }
386     }
387     return ERR_OK;
388 }
389 
HandleSetDisposedRule(MessageParcel & data,MessageParcel & reply)390 ErrCode AppControlHost::HandleSetDisposedRule(MessageParcel& data, MessageParcel& reply)
391 {
392     std::string appId = data.ReadString();
393     std::unique_ptr<DisposedRule> disposedRule(data.ReadParcelable<DisposedRule>());
394     int32_t userId = data.ReadInt32();
395     if (disposedRule == nullptr) {
396         LOG_E(BMS_TAG_DEFAULT, "ReadParcelable<disposedRule> failed");
397         return ERR_APPEXECFWK_PARCEL_ERROR;
398     }
399     ErrCode ret = SetDisposedRule(appId, *disposedRule, userId);
400     if (!reply.WriteInt32(ret)) {
401         LOG_E(BMS_TAG_DEFAULT, "write ret failed");
402         return ERR_APPEXECFWK_PARCEL_ERROR;
403     }
404     return ERR_OK;
405 }
406 
HandleGetAbilityRunningControlRule(MessageParcel & data,MessageParcel & reply)407 ErrCode AppControlHost::HandleGetAbilityRunningControlRule(MessageParcel& data, MessageParcel& reply)
408 {
409     std::string bundleName = data.ReadString();
410     int32_t userId = data.ReadInt32();
411     int32_t appIndex = data.ReadInt32();
412     std::vector<DisposedRule> rules;
413     ErrCode ret = GetAbilityRunningControlRule(bundleName, userId, rules, appIndex);
414     if (!reply.WriteInt32(ret)) {
415         LOG_E(BMS_TAG_DEFAULT, "write ret failed");
416         return ERR_APPEXECFWK_PARCEL_ERROR;
417     }
418     if (ret == ERR_OK) {
419         if (!WriteParcelableVector(rules, reply)) {
420             LOG_E(BMS_TAG_DEFAULT, "write failed");
421             return ERR_APPEXECFWK_PARCEL_ERROR;
422         }
423     }
424     return ERR_OK;
425 }
426 
HandleGetDisposedRuleForCloneApp(MessageParcel & data,MessageParcel & reply)427 ErrCode AppControlHost::HandleGetDisposedRuleForCloneApp(MessageParcel& data, MessageParcel &reply)
428 {
429     std::string appId = data.ReadString();
430     int32_t userId = data.ReadInt32();
431     int32_t appIndex = data.ReadInt32();
432     DisposedRule rule;
433     ErrCode ret = GetDisposedRuleForCloneApp(appId, rule, appIndex, userId);
434     if (!reply.WriteInt32(ret)) {
435         LOG_E(BMS_TAG_DEFAULT, "write ret failed");
436         return ERR_APPEXECFWK_PARCEL_ERROR;
437     }
438     if (ret == ERR_OK) {
439         if (!reply.WriteParcelable(&rule)) {
440             LOG_E(BMS_TAG_DEFAULT, "write failed");
441             return ERR_APPEXECFWK_PARCEL_ERROR;
442         }
443     }
444     return ERR_OK;
445 }
446 
HandleSetDisposedRuleForCloneApp(MessageParcel & data,MessageParcel & reply)447 ErrCode AppControlHost::HandleSetDisposedRuleForCloneApp(MessageParcel& data, MessageParcel& reply)
448 {
449     std::string appId = data.ReadString();
450     std::unique_ptr<DisposedRule> disposedRule(data.ReadParcelable<DisposedRule>());
451     int32_t userId = data.ReadInt32();
452     int32_t appIndex = data.ReadInt32();
453     if (disposedRule == nullptr) {
454         LOG_E(BMS_TAG_DEFAULT, "ReadParcelable<disposedRule> failed");
455         return ERR_APPEXECFWK_PARCEL_ERROR;
456     }
457     ErrCode ret = SetDisposedRuleForCloneApp(appId, *disposedRule, appIndex, userId);
458     if (!reply.WriteInt32(ret)) {
459         LOG_E(BMS_TAG_DEFAULT, "write ret failed");
460         return ERR_APPEXECFWK_PARCEL_ERROR;
461     }
462     return ERR_OK;
463 }
464 
HandleDeleteDisposedRuleForCloneApp(MessageParcel & data,MessageParcel & reply)465 ErrCode AppControlHost::HandleDeleteDisposedRuleForCloneApp(MessageParcel& data, MessageParcel& reply)
466 {
467     std::string appId = data.ReadString();
468     int32_t userId = data.ReadInt32();
469     int32_t appIndex = data.ReadInt32();
470     ErrCode ret = DeleteDisposedRuleForCloneApp(appId, appIndex, userId);
471     if (!reply.WriteInt32(ret)) {
472         LOG_E(BMS_TAG_DEFAULT, "write ret failed");
473         return ERR_APPEXECFWK_PARCEL_ERROR;
474     }
475     return ERR_OK;
476 }
477 
WriteStringVector(const std::vector<std::string> & stringVector,MessageParcel & reply)478 bool AppControlHost::WriteStringVector(const std::vector<std::string> &stringVector, MessageParcel &reply)
479 {
480     if (!reply.WriteInt32(stringVector.size())) {
481         LOG_E(BMS_TAG_DEFAULT, "write ParcelableVector failed");
482         return false;
483     }
484 
485     for (auto &string : stringVector) {
486         if (!reply.WriteString(string)) {
487             LOG_E(BMS_TAG_DEFAULT, "write string failed");
488             return false;
489         }
490     }
491     return true;
492 }
493 
494 template<typename T>
WriteParcelableVector(std::vector<T> & parcelableVector,MessageParcel & reply)495 bool AppControlHost::WriteParcelableVector(std::vector<T> &parcelableVector, MessageParcel &reply)
496 {
497     if (!reply.WriteInt32(parcelableVector.size())) {
498         LOG_E(BMS_TAG_DEFAULT, "write ParcelableVector failed");
499         return false;
500     }
501 
502     for (auto &parcelable : parcelableVector) {
503         if (!reply.WriteParcelable(&parcelable)) {
504             LOG_E(BMS_TAG_DEFAULT, "write ParcelableVector failed");
505             return false;
506         }
507     }
508     return true;
509 }
510 
511 template<typename T>
ReadParcelableVector(MessageParcel & data,std::vector<T> & parcelableInfos)512 ErrCode AppControlHost::ReadParcelableVector(MessageParcel &data, std::vector<T> &parcelableInfos)
513 {
514     int32_t infoSize = data.ReadInt32();
515     if (infoSize > AppControlConstants::LIST_MAX_SIZE) {
516         LOG_E(BMS_TAG_DEFAULT, "elements num exceeds the limit %{public}d", AppControlConstants::LIST_MAX_SIZE);
517         return ERR_BUNDLE_MANAGER_INVALID_PARAMETER;
518     }
519     for (int32_t i = 0; i < infoSize; i++) {
520         std::unique_ptr<T> info(data.ReadParcelable<T>());
521         if (info == nullptr) {
522             LOG_E(BMS_TAG_DEFAULT, "read parcelable infos failed");
523             return ERR_APPEXECFWK_PARCEL_ERROR;
524         }
525         parcelableInfos.emplace_back(*info);
526     }
527     LOG_D(BMS_TAG_DEFAULT, "read parcelable infos success");
528     return ERR_OK;
529 }
530 } // AppExecFwk
531 } // OHOS
532