• 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 #include "js_app_control.h"
16 
17 #include <string>
18 
19 #include "app_log_wrapper.h"
20 #include "app_control_interface.h"
21 #include "bundle_constants.h"
22 #include "bundle_mgr_interface.h"
23 #include "bundle_mgr_proxy.h"
24 #include "bundle_errors.h"
25 #include "business_error.h"
26 #include "common_func.h"
27 #include "disposed_rule.h"
28 #include "ipc_skeleton.h"
29 #include "napi_arg.h"
30 #include "napi_common_want.h"
31 #include "napi_constants.h"
32 
33 namespace OHOS {
34 namespace AppExecFwk {
35 using namespace OHOS::AAFwk;
36 namespace {
37 const char* PARAM_LENGTH_ERROR = "parameter length invalid";
38 const int16_t MAX_VECTOR_NUM = 1000;
39 }
InnerGetDisposedStatus(napi_env,const std::string & appId,Want & disposedWant)40 static ErrCode InnerGetDisposedStatus(napi_env, const std::string& appId, Want& disposedWant)
41 {
42     auto appControlProxy = CommonFunc::GetAppControlProxy();
43     if (appControlProxy == nullptr) {
44         APP_LOGE("AppControlProxy is null");
45         return ERROR_SYSTEM_ABILITY_NOT_FOUND;
46     }
47     ErrCode ret = appControlProxy->GetDisposedStatus(appId, disposedWant);
48     return CommonFunc::ConvertErrCode(ret);
49 }
50 
InnerSetDisposedStatus(napi_env,const std::string & appId,Want & disposedWant)51 static ErrCode InnerSetDisposedStatus(napi_env, const std::string& appId, Want& disposedWant)
52 {
53     auto appControlProxy = CommonFunc::GetAppControlProxy();
54     if (appControlProxy == nullptr) {
55         APP_LOGE("AppControlProxy is null");
56         return ERROR_SYSTEM_ABILITY_NOT_FOUND;
57     }
58     ErrCode ret = appControlProxy->SetDisposedStatus(appId, disposedWant);
59     return CommonFunc::ConvertErrCode(ret);
60 }
61 
InnerDeleteDisposedStatus(napi_env,const std::string & appId)62 static ErrCode InnerDeleteDisposedStatus(napi_env, const std::string& appId)
63 {
64     auto appControlProxy = CommonFunc::GetAppControlProxy();
65     if (appControlProxy == nullptr) {
66         APP_LOGE("AppControlProxy is null");
67         return ERROR_SYSTEM_ABILITY_NOT_FOUND;
68     }
69     ErrCode ret = appControlProxy->DeleteDisposedStatus(appId);
70     return CommonFunc::ConvertErrCode(ret);
71 }
72 
SetDisposedStatusExec(napi_env env,void * data)73 void SetDisposedStatusExec(napi_env env, void *data)
74 {
75     DisposedStatus *asyncCallbackInfo = reinterpret_cast<DisposedStatus *>(data);
76     if (asyncCallbackInfo == nullptr) {
77         APP_LOGE("asyncCallbackInfo == nullptr");
78         return;
79     }
80     if (asyncCallbackInfo->err == NO_ERROR) {
81         asyncCallbackInfo->err = InnerSetDisposedStatus(env, asyncCallbackInfo->appId,
82             asyncCallbackInfo->want);
83     }
84 }
85 
SetDisposedStatusComplete(napi_env env,napi_status status,void * data)86 void SetDisposedStatusComplete(napi_env env, napi_status status, void *data)
87 {
88     DisposedStatus *asyncCallbackInfo = reinterpret_cast<DisposedStatus *>(data);
89     if (asyncCallbackInfo == nullptr) {
90         APP_LOGE("asyncCallbackInfo is null");
91         return;
92     }
93     std::unique_ptr<DisposedStatus> callbackPtr {asyncCallbackInfo};
94     napi_value result[1] = {0};
95     if (asyncCallbackInfo->err == NO_ERROR) {
96         NAPI_CALL_RETURN_VOID(env, napi_get_null(env, &result[0]));
97     } else {
98         APP_LOGE("SetDisposedStatus err = %{public}d", asyncCallbackInfo->err);
99         result[0] = BusinessError::CreateCommonError(
100             env, asyncCallbackInfo->err, SET_DISPOSED_STATUS, PERMISSION_DISPOSED_STATUS);
101     }
102     CommonFunc::NapiReturnDeferred<DisposedStatus>(env, asyncCallbackInfo, result, ARGS_SIZE_ONE);
103 }
104 
SetDisposedStatus(napi_env env,napi_callback_info info)105 napi_value SetDisposedStatus(napi_env env, napi_callback_info info)
106 {
107     APP_LOGD("begin to SetDisposedStatus");
108     NapiArg args(env, info);
109     DisposedStatus *asyncCallbackInfo = new (std::nothrow) DisposedStatus(env);
110     if (asyncCallbackInfo == nullptr) {
111         APP_LOGE("asyncCallbackInfo is null");
112         return nullptr;
113     }
114     std::unique_ptr<DisposedStatus> callbackPtr {asyncCallbackInfo};
115     if (!args.Init(ARGS_SIZE_TWO, ARGS_SIZE_THREE)) {
116         APP_LOGE("Napi func init failed");
117         BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
118         return nullptr;
119     }
120     for (size_t i = 0; i < args.GetMaxArgc(); ++i) {
121         napi_valuetype valueType = napi_undefined;
122         napi_typeof(env, args[i], &valueType);
123         if (i == ARGS_POS_ZERO) {
124             if (!CommonFunc::ParseString(env, args[i], asyncCallbackInfo->appId)) {
125                 APP_LOGW("appId invalid");
126                 BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, APP_ID, TYPE_STRING);
127                 return nullptr;
128             }
129             asyncCallbackInfo->err = asyncCallbackInfo->appId.empty() ? ERROR_INVALID_APPID : NO_ERROR;
130         } else if (i == ARGS_POS_ONE) {
131             if (!CommonFunc::ParseWantWithoutVerification(env, args[i], asyncCallbackInfo->want)) {
132                 APP_LOGE("disposed want invalid");
133                 BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, DISPOSED_WANT, TYPE_WANT);
134                 return nullptr;
135             }
136         } else if (i == ARGS_POS_TWO) {
137             if (valueType == napi_function) {
138                 NAPI_CALL(env, napi_create_reference(env, args[i], NAPI_RETURN_ONE, &asyncCallbackInfo->callback));
139             } else {
140                 APP_LOGD("SetDisposedStatus extra arg ignored");
141             }
142         } else {
143             APP_LOGE("SetDisposedStatus arg err");
144             BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_TYPE_CHECK_ERROR);
145             return nullptr;
146         }
147     }
148     auto promise = CommonFunc::AsyncCallNativeMethod<DisposedStatus>(
149         env, asyncCallbackInfo, "SetDisposedStatus", SetDisposedStatusExec, SetDisposedStatusComplete);
150     callbackPtr.release();
151     APP_LOGD("call SetDisposedStatus done");
152     return promise;
153 }
154 
SetDisposedStatusSync(napi_env env,napi_callback_info info)155 napi_value SetDisposedStatusSync(napi_env env, napi_callback_info info)
156 {
157     APP_LOGD("begin to SetDisposedStatusSync");
158     NapiArg args(env, info);
159     napi_value nRet;
160     napi_get_undefined(env, &nRet);
161     if (!args.Init(ARGS_SIZE_TWO, ARGS_SIZE_TWO)) {
162         APP_LOGE("Napi func init failed");
163         BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
164         return nRet;
165     }
166     std::string appId;
167     if (!CommonFunc::ParseString(env, args[ARGS_POS_ZERO], appId)) {
168         APP_LOGE("appId %{public}s invalid", appId.c_str());
169         BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, APP_ID, TYPE_STRING);
170         return nRet;
171     }
172     if (appId.empty()) {
173         napi_value businessError = BusinessError::CreateCommonError(
174             env, ERROR_INVALID_APPID, SET_DISPOSED_STATUS_SYNC);
175         napi_throw(env, businessError);
176         return nullptr;
177     }
178     OHOS::AAFwk::Want want;
179     if (!CommonFunc::ParseWantWithoutVerification(env, args[ARGS_POS_ONE], want)) {
180         APP_LOGE("want invalid");
181         BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, DISPOSED_WANT, TYPE_WANT);
182         return nRet;
183     }
184     auto appControlProxy = CommonFunc::GetAppControlProxy();
185     if (appControlProxy == nullptr) {
186         APP_LOGE("AppControlProxy is null");
187         napi_value error = BusinessError::CreateCommonError(env, ERROR_SYSTEM_ABILITY_NOT_FOUND,
188             SET_DISPOSED_STATUS_SYNC);
189         napi_throw(env, error);
190         return nRet;
191     }
192     ErrCode ret = appControlProxy->SetDisposedStatus(appId, want);
193     ret = CommonFunc::ConvertErrCode(ret);
194     if (ret != NO_ERROR) {
195         APP_LOGE("SetDisposedStatusSync err = %{public}d", ret);
196         napi_value businessError = BusinessError::CreateCommonError(
197             env, ret, SET_DISPOSED_STATUS_SYNC, PERMISSION_DISPOSED_STATUS);
198         napi_throw(env, businessError);
199     }
200     APP_LOGD("call SetDisposedStatusSync done");
201     return nRet;
202 }
203 
DeleteDisposedStatusExec(napi_env env,void * data)204 void DeleteDisposedStatusExec(napi_env env, void *data)
205 {
206     DisposedStatus *asyncCallbackInfo = reinterpret_cast<DisposedStatus *>(data);
207     if (asyncCallbackInfo == nullptr) {
208         APP_LOGE("asyncCallbackInfo is null");
209         return;
210     }
211     if (asyncCallbackInfo->err == NO_ERROR) {
212         asyncCallbackInfo->err = InnerDeleteDisposedStatus(env, asyncCallbackInfo->appId);
213     }
214 }
215 
DeleteDisposedStatusComplete(napi_env env,napi_status,void * data)216 void DeleteDisposedStatusComplete(napi_env env, napi_status, void *data)
217 {
218     DisposedStatus *asyncCallbackInfo = reinterpret_cast<DisposedStatus *>(data);
219     if (asyncCallbackInfo == nullptr) {
220         APP_LOGE("asyncCallbackInfo is null");
221         return;
222     }
223     std::unique_ptr<DisposedStatus> callbackPtr {asyncCallbackInfo};
224     napi_value result[1] = {0};
225     if (asyncCallbackInfo->err == NO_ERROR) {
226         NAPI_CALL_RETURN_VOID(env, napi_get_null(env, &result[0]));
227     } else {
228         APP_LOGE("DeleteDisposedStatus err = %{public}d", asyncCallbackInfo->err);
229         result[0] = BusinessError::CreateCommonError(
230             env, asyncCallbackInfo->err, DELETE_DISPOSED_STATUS, PERMISSION_DISPOSED_STATUS);
231     }
232     CommonFunc::NapiReturnDeferred<DisposedStatus>(env, asyncCallbackInfo, result, ARGS_SIZE_ONE);
233 }
234 
DeleteDisposedStatus(napi_env env,napi_callback_info info)235 napi_value DeleteDisposedStatus(napi_env env, napi_callback_info info)
236 {
237     APP_LOGD("begin to DeleteDisposedStatus");
238     NapiArg args(env, info);
239     DisposedStatus *asyncCallbackInfo = new (std::nothrow) DisposedStatus(env);
240     if (asyncCallbackInfo == nullptr) {
241         APP_LOGE("asyncCallbackInfo is null");
242         return nullptr;
243     }
244     std::unique_ptr<DisposedStatus> callbackPtr {asyncCallbackInfo};
245     if (!args.Init(ARGS_SIZE_ONE, ARGS_SIZE_TWO)) {
246         APP_LOGE("param count invalid");
247         BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
248         return nullptr;
249     }
250     for (size_t i = 0; i < args.GetMaxArgc(); ++i) {
251         napi_valuetype valueType = napi_undefined;
252         napi_typeof(env, args[i], &valueType);
253         if (i == ARGS_POS_ZERO) {
254             if (!CommonFunc::ParseString(env, args[i], asyncCallbackInfo->appId)) {
255                 APP_LOGW("appId invalid");
256                 BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, APP_ID, TYPE_STRING);
257                 return nullptr;
258             }
259             if (asyncCallbackInfo->appId.empty()) {
260                 asyncCallbackInfo->err = ERROR_INVALID_APPID;
261             }
262         } else if (i == ARGS_POS_ONE) {
263             if (valueType == napi_function) {
264                 NAPI_CALL(env, napi_create_reference(env, args[i], NAPI_RETURN_ONE, &asyncCallbackInfo->callback));
265             } else {
266                 APP_LOGD("DeleteDisposedStatus extra arg ignored");
267             }
268         } else {
269             APP_LOGE("DeleteDisposedStatus arg err");
270             BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_TYPE_CHECK_ERROR);
271             return nullptr;
272         }
273     }
274     auto promise = CommonFunc::AsyncCallNativeMethod<DisposedStatus>(
275         env, asyncCallbackInfo, "DeleteDisposedStatus", DeleteDisposedStatusExec, DeleteDisposedStatusComplete);
276     callbackPtr.release();
277     APP_LOGD("call DeleteDisposedStatus done");
278     return promise;
279 }
280 
InnerDeleteDisposedStatusSync(napi_env env,std::string & appId,int32_t appIndex)281 static napi_value InnerDeleteDisposedStatusSync(napi_env env, std::string &appId, int32_t appIndex)
282 {
283     napi_value nRet;
284     napi_get_undefined(env, &nRet);
285     if (appId.empty()) {
286         napi_value businessError = BusinessError::CreateCommonError(
287             env, ERROR_INVALID_APPID, DELETE_DISPOSED_STATUS_SYNC);
288         napi_throw(env, businessError);
289         return nullptr;
290     }
291     auto appControlProxy = CommonFunc::GetAppControlProxy();
292     if (appControlProxy == nullptr) {
293         APP_LOGE("AppControlProxy is null");
294         napi_value error = BusinessError::CreateCommonError(env, ERROR_SYSTEM_ABILITY_NOT_FOUND,
295             DELETE_DISPOSED_STATUS_SYNC);
296         napi_throw(env, error);
297         return nullptr;
298     }
299     ErrCode ret = ERR_OK;
300     if (appIndex == Constants::MAIN_APP_INDEX) {
301         ret = appControlProxy->DeleteDisposedStatus(appId);
302     } else {
303         ret = appControlProxy->DeleteDisposedRuleForCloneApp(appId, appIndex);
304     }
305     ret = CommonFunc::ConvertErrCode(ret);
306     if (ret != ERR_OK) {
307         APP_LOGE("DeleteDisposedStatusSync failed");
308         napi_value businessError = BusinessError::CreateCommonError(
309             env, ret, DELETE_DISPOSED_STATUS_SYNC, PERMISSION_DISPOSED_STATUS);
310         napi_throw(env, businessError);
311         return nullptr;
312     }
313     return nRet;
314 }
315 
DeleteDisposedStatusSync(napi_env env,napi_callback_info info)316 napi_value DeleteDisposedStatusSync(napi_env env, napi_callback_info info)
317 {
318     APP_LOGD("begin to DeleteDisposedStatusSync");
319     NapiArg args(env, info);
320     napi_value nRet;
321     napi_get_undefined(env, &nRet);
322     if (!args.Init(ARGS_SIZE_ONE, ARGS_SIZE_TWO)) {
323         APP_LOGE("param count invalid");
324         BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
325         return nRet;
326     }
327     std::string appId;
328     if (!CommonFunc::ParseString(env, args[ARGS_POS_ZERO], appId)) {
329         APP_LOGE("appId %{public}s invalid", appId.c_str());
330         BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, APP_ID, TYPE_STRING);
331         return nRet;
332     }
333     int32_t appIndex = Constants::MAIN_APP_INDEX;
334     if (args.GetMaxArgc() == ARGS_SIZE_ONE) {
335         return InnerDeleteDisposedStatusSync(env, appId, appIndex);
336     }
337     if (args.GetMaxArgc() == ARGS_SIZE_TWO) {
338         if (!CommonFunc::ParseInt(env, args[ARGS_POS_ONE], appIndex)) {
339             APP_LOGW("parse appIndex falied");
340         }
341         return InnerDeleteDisposedStatusSync(env, appId, appIndex);
342     }
343     APP_LOGE("parameter is invalid");
344     BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_TYPE_CHECK_ERROR);
345     return nRet;
346 }
347 
GetDisposedStatusExec(napi_env env,void * data)348 void GetDisposedStatusExec(napi_env env, void *data)
349 {
350     DisposedStatus *asyncCallbackInfo = reinterpret_cast<DisposedStatus *>(data);
351     if (asyncCallbackInfo == nullptr) {
352         APP_LOGE("asyncCallbackInfo is null");
353         return;
354     }
355     if (asyncCallbackInfo->err == NO_ERROR) {
356         asyncCallbackInfo->err = InnerGetDisposedStatus(env, asyncCallbackInfo->appId,
357             asyncCallbackInfo->want);
358     }
359 }
360 
GetDisposedStatusComplete(napi_env env,napi_status status,void * data)361 void GetDisposedStatusComplete(napi_env env, napi_status status, void *data)
362 {
363     DisposedStatus *asyncCallbackInfo = reinterpret_cast<DisposedStatus *>(data);
364     if (asyncCallbackInfo == nullptr) {
365         APP_LOGE("asyncCallbackInfo is null");
366         return;
367     }
368     std::unique_ptr<DisposedStatus> callbackPtr {asyncCallbackInfo};
369     napi_value result[2] = {0};
370     if (asyncCallbackInfo->err == NO_ERROR) {
371         NAPI_CALL_RETURN_VOID(env, napi_get_null(env, &result[0]));
372         NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &result[1]));
373         CommonFunc::ConvertWantInfo(env, result[1], asyncCallbackInfo->want);
374     } else {
375         APP_LOGE("GetDisposedStatus err = %{public}d", asyncCallbackInfo->err);
376         result[0] = BusinessError::CreateCommonError(
377             env, asyncCallbackInfo->err, GET_DISPOSED_STATUS, PERMISSION_DISPOSED_STATUS);
378     }
379     CommonFunc::NapiReturnDeferred<DisposedStatus>(env, asyncCallbackInfo, result, ARGS_SIZE_TWO);
380 }
381 
GetDisposedStatus(napi_env env,napi_callback_info info)382 napi_value GetDisposedStatus(napi_env env, napi_callback_info info)
383 {
384     APP_LOGD("NAPI GetDisposedStatus called");
385     NapiArg args(env, info);
386     DisposedStatus *asyncCallbackInfo = new (std::nothrow) DisposedStatus(env);
387     if (asyncCallbackInfo == nullptr) {
388         APP_LOGE("asyncCallbackInfo is null");
389         return nullptr;
390     }
391     std::unique_ptr<DisposedStatus> callbackPtr {asyncCallbackInfo};
392     if (!args.Init(ARGS_SIZE_ONE, ARGS_SIZE_TWO)) {
393         APP_LOGE("param count invalid");
394         BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
395         return nullptr;
396     }
397     for (size_t i = 0; i < args.GetMaxArgc(); i++) {
398         napi_valuetype valueType = napi_undefined;
399         napi_typeof(env, args[i], &valueType);
400         if (i == ARGS_POS_ZERO) {
401             if (!CommonFunc::ParseString(env, args[i], asyncCallbackInfo->appId)) {
402                 APP_LOGE("appId %{public}s invalid", asyncCallbackInfo->appId.c_str());
403                 BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, APP_ID, TYPE_STRING);
404                 return nullptr;
405             }
406             if (asyncCallbackInfo->appId.empty()) {
407                 asyncCallbackInfo->err = ERROR_INVALID_APPID;
408             }
409         } else if (i == ARGS_POS_ONE) {
410             if (valueType == napi_function) {
411                 NAPI_CALL(env, napi_create_reference(env, args[i], NAPI_RETURN_ONE, &asyncCallbackInfo->callback));
412             } else {
413                 APP_LOGD("GetDisposedStatus extra arg ignored");
414             }
415         } else {
416             APP_LOGE("GetDisposedStatus arg err");
417             BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_TYPE_CHECK_ERROR);
418             return nullptr;
419         }
420     }
421     auto promise = CommonFunc::AsyncCallNativeMethod<DisposedStatus>(
422         env, asyncCallbackInfo, "GetDisposedStatus", GetDisposedStatusExec, GetDisposedStatusComplete);
423     callbackPtr.release();
424     APP_LOGD("call GetDisposedStatus done");
425     return promise;
426 }
427 
GetDisposedStatusSync(napi_env env,napi_callback_info info)428 napi_value GetDisposedStatusSync(napi_env env, napi_callback_info info)
429 {
430     APP_LOGD("NAPI GetDisposedStatusSync called");
431     NapiArg args(env, info);
432     if (!args.Init(ARGS_SIZE_ONE, ARGS_SIZE_ONE)) {
433         APP_LOGE("param count invalid");
434         BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
435         return nullptr;
436     }
437     std::string appId;
438     if (!CommonFunc::ParseString(env, args[ARGS_POS_ZERO], appId)) {
439         APP_LOGE("appId %{public}s invalid", appId.c_str());
440         BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, APP_ID, TYPE_STRING);
441         return nullptr;
442     }
443     if (appId.empty()) {
444         napi_value businessError = BusinessError::CreateCommonError(
445             env, ERROR_INVALID_APPID, GET_DISPOSED_STATUS_SYNC);
446         napi_throw(env, businessError);
447         return nullptr;
448     }
449     auto appControlProxy = CommonFunc::GetAppControlProxy();
450     if (appControlProxy == nullptr) {
451         APP_LOGE("AppControlProxy is null");
452         napi_value error = BusinessError::CreateCommonError(env, ERROR_SYSTEM_ABILITY_NOT_FOUND,
453             GET_DISPOSED_STATUS_SYNC);
454         napi_throw(env, error);
455         return nullptr;
456     }
457     OHOS::AAFwk::Want disposedWant;
458     ErrCode ret = appControlProxy->GetDisposedStatus(appId, disposedWant);
459     ret = CommonFunc::ConvertErrCode(ret);
460     if (ret != ERR_OK) {
461         APP_LOGE("GetDisposedStatusSync failed");
462         napi_value businessError = BusinessError::CreateCommonError(
463             env, ret, GET_DISPOSED_STATUS_SYNC, PERMISSION_DISPOSED_STATUS);
464         napi_throw(env, businessError);
465         return nullptr;
466     }
467     napi_value nWant = nullptr;
468     NAPI_CALL(env, napi_create_object(env, &nWant));
469     CommonFunc::ConvertWantInfo(env, nWant, disposedWant);
470     APP_LOGD("call GetDisposedStatusSync done");
471     return nWant;
472 }
473 
ConvertRuleInfo(napi_env env,napi_value nRule,const DisposedRule & rule)474 void ConvertRuleInfo(napi_env env, napi_value nRule, const DisposedRule &rule)
475 {
476     napi_value nWant = nullptr;
477     if (rule.want != nullptr) {
478         nWant = CreateJsWant(env, *rule.want);
479     } else {
480         napi_create_object(env, &nWant);
481     }
482     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, nRule, "want", nWant));
483     napi_value nComponentType;
484     NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, static_cast<int32_t>(rule.componentType), &nComponentType));
485     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, nRule, "componentType", nComponentType));
486     napi_value nDisposedType;
487     NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, static_cast<int32_t>(rule.disposedType), &nDisposedType));
488     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, nRule, "disposedType", nDisposedType));
489     napi_value nControlType;
490     NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, static_cast<int32_t>(rule.controlType), &nControlType));
491     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, nRule, "controlType", nControlType));
492 
493     napi_value nElementList;
494     NAPI_CALL_RETURN_VOID(env, napi_create_array(env, &nElementList));
495     for (size_t idx = 0; idx < rule.elementList.size(); idx++) {
496         napi_value nElementName;
497         NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &nElementName));
498         CommonFunc::ConvertElementName(env, nElementName, rule.elementList[idx]);
499         NAPI_CALL_RETURN_VOID(env, napi_set_element(env, nElementList, idx, nElementName));
500     }
501     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, nRule, "elementList", nElementList));
502     napi_value nPriority;
503     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, rule.priority, &nPriority));
504     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, nRule, "priority", nPriority));
505 }
506 
ParseDiposedRule(napi_env env,napi_value nRule,DisposedRule & rule)507 bool ParseDiposedRule(napi_env env, napi_value nRule, DisposedRule &rule)
508 {
509     napi_valuetype valueType;
510     NAPI_CALL_BASE(env, napi_typeof(env, nRule, &valueType), false);
511     if (valueType != napi_object) {
512         APP_LOGW("nRule not object type");
513         return false;
514     }
515     napi_value prop = nullptr;
516     napi_get_named_property(env, nRule, TYPE_WANT, &prop);
517     AAFwk::Want want;
518     if (!UnwrapWant(env, prop, want)) {
519         APP_LOGW("parse want failed");
520         return false;
521     }
522     rule.want = std::make_shared<AAFwk::Want>(want);
523     napi_get_named_property(env, nRule, "componentType", &prop);
524     int32_t componentType;
525     if (!CommonFunc::ParseInt(env, prop, componentType)) {
526         APP_LOGW("componentType parseInt failed");
527         return false;
528     }
529     if (componentType > static_cast<int32_t>(ComponentType::UI_EXTENSION) ||
530         componentType < static_cast<int32_t>(ComponentType::UI_ABILITY)) {
531         APP_LOGW("componentType not valid");
532         return false;
533     }
534     rule.componentType = static_cast<ComponentType>(componentType);
535     napi_get_named_property(env, nRule, "disposedType", &prop);
536     int32_t disposedType;
537     if (!CommonFunc::ParseInt(env, prop, disposedType)) {
538         APP_LOGW("disposedType parseInt failed");
539         return false;
540     }
541     if (disposedType > static_cast<int32_t>(DisposedType::NON_BLOCK) ||
542         disposedType < static_cast<int32_t>(DisposedType::BLOCK_APPLICATION)) {
543         APP_LOGW("disposedType not valid");
544         return false;
545     }
546     rule.disposedType = static_cast<DisposedType>(disposedType);
547     napi_get_named_property(env, nRule, "controlType", &prop);
548     int32_t controlType;
549     if (!CommonFunc::ParseInt(env, prop, controlType)) {
550         APP_LOGW("disposedType parseInt failed");
551         return false;
552     }
553     if (controlType > static_cast<int32_t>(ControlType::DISALLOWED_LIST) ||
554         controlType < static_cast<int32_t>(ControlType::ALLOWED_LIST)) {
555         APP_LOGW("ControlType not valid");
556         return false;
557     }
558     rule.controlType = static_cast<ControlType>(controlType);
559 
560     napi_value nElementList = nullptr;
561     napi_get_named_property(env, nRule, "elementList", &nElementList);
562     bool isArray = false;
563     NAPI_CALL_BASE(env, napi_is_array(env, nElementList, &isArray), false);
564     if (!isArray) {
565         APP_LOGW("nElementList not array");
566         return false;
567     }
568     uint32_t arrayLength = 0;
569     NAPI_CALL_BASE(env, napi_get_array_length(env, nElementList, &arrayLength), false);
570     for (uint32_t j = 0; j < arrayLength; j++) {
571         napi_value value = nullptr;
572         NAPI_CALL_BASE(env, napi_get_element(env, nElementList, j, &value), false);
573         ElementName name;
574         if (!CommonFunc::ParseElementName(env, value, name)) {
575             APP_LOGW("parse element name failed");
576             return false;
577         }
578         rule.elementList.push_back(name);
579     }
580     napi_get_named_property(env, nRule, "priority", &prop);
581     if (!CommonFunc::ParseInt(env, prop, rule.priority)) {
582         APP_LOGW("priority parseInt failed");
583         return false;
584     }
585     return true;
586 }
587 
ParseDisposedRuleConfiguration(napi_env env,napi_value nDisposedRuleConfiguration,DisposedRuleConfiguration & disposedRuleConfiguration)588 bool ParseDisposedRuleConfiguration(napi_env env, napi_value nDisposedRuleConfiguration,
589     DisposedRuleConfiguration &disposedRuleConfiguration)
590 {
591     napi_valuetype valueType;
592     NAPI_CALL_BASE(env, napi_typeof(env, nDisposedRuleConfiguration, &valueType), false);
593     if (valueType != napi_object) {
594         APP_LOGE("parse not object");
595         return false;
596     }
597     napi_value prop = nullptr;
598     napi_get_named_property(env, nDisposedRuleConfiguration, "appId", &prop);
599     std::string appId;
600     if (!CommonFunc::ParseString(env, prop, appId)) {
601         APP_LOGW("appId invalid");
602         BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, APP_ID, TYPE_STRING);
603         return false;
604     }
605     if (appId.empty()) {
606         napi_value businessError = BusinessError::CreateCommonError(
607             env, ERROR_INVALID_APPID, SET_DISPOSED_RULES);
608         napi_throw(env, businessError);
609         return false;
610     }
611     disposedRuleConfiguration.appId = appId;
612     napi_get_named_property(env, nDisposedRuleConfiguration, "appIndex", &prop);
613     int32_t appIndex = Constants::MAIN_APP_INDEX;
614     if (!CommonFunc::ParseInt(env, prop, appIndex)) {
615         APP_LOGE("appIndex invalid");
616         BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, APP_INDEX, TYPE_NUMBER);
617         return false;
618     }
619     if (appIndex < Constants::MAIN_APP_INDEX || appIndex > Constants::CLONE_APP_INDEX_MAX) {
620         napi_value businessError = BusinessError::CreateCommonError(
621             env, ERROR_INVALID_APPINDEX, SET_DISPOSED_RULES);
622         napi_throw(env, businessError);
623         return false;
624     }
625     disposedRuleConfiguration.appIndex = appIndex;
626 
627     napi_get_named_property(env, nDisposedRuleConfiguration, "disposedRule", &prop);
628     DisposedRule disposedRule;
629     if (!ParseDiposedRule(env, prop, disposedRule)) {
630         APP_LOGE("disposedRule invalid!");
631         BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, DISPOSED_RULE, DISPOSED_RULE_TYPE);
632         return false;
633     }
634     disposedRuleConfiguration.disposedRule = disposedRule;
635     return true;
636 }
637 
ParseDisposedRuleConfigurationArray(napi_env env,napi_value nDisposedRuleConfigurations,std::vector<DisposedRuleConfiguration> & disposedRuleConfigurations)638 bool ParseDisposedRuleConfigurationArray(napi_env env, napi_value nDisposedRuleConfigurations,
639     std::vector<DisposedRuleConfiguration> &disposedRuleConfigurations)
640 {
641     bool isArray = false;
642     NAPI_CALL_BASE(env, napi_is_array(env, nDisposedRuleConfigurations, &isArray), false);
643     if (!isArray) {
644         APP_LOGE("nDisposedRuleConfigurations not array type");
645         return false;
646     }
647     uint32_t arrayLength = 0;
648     NAPI_CALL_BASE(env, napi_get_array_length(env, nDisposedRuleConfigurations, &arrayLength), false);
649     APP_LOGD("length=%{public}ud", arrayLength);
650     if (arrayLength <= 0 || arrayLength > MAX_VECTOR_NUM) {
651         APP_LOGE("disposedRuleConfigurations length invalid!");
652         BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_LENGTH_ERROR);
653         return false;
654     }
655     for (uint32_t j = 0; j < arrayLength; j++) {
656         napi_value value = nullptr;
657         NAPI_CALL_BASE(env, napi_get_element(env, nDisposedRuleConfigurations, j, &value), false);
658         napi_valuetype valueType = napi_undefined;
659         NAPI_CALL_BASE(env, napi_typeof(env, value, &valueType), false);
660         if (valueType != napi_object) {
661             APP_LOGE("disposedRuleConfiguration bot objext");
662             disposedRuleConfigurations.clear();
663             return false;
664         }
665         DisposedRuleConfiguration disposedRuleConfiguration;
666         if (!ParseDisposedRuleConfiguration(env, value, disposedRuleConfiguration)) {
667             APP_LOGE("disposedRuleConfiguration invalid");
668             return false;
669         }
670         disposedRuleConfigurations.push_back(disposedRuleConfiguration);
671     }
672     return true;
673 }
674 
InnerGetDisposedRule(napi_env env,std::string & appId,int32_t appIndex)675 static napi_value InnerGetDisposedRule(napi_env env, std::string &appId, int32_t appIndex)
676 {
677     if (appId.empty()) {
678         napi_value businessError = BusinessError::CreateCommonError(
679             env, ERROR_INVALID_APPID, GET_DISPOSED_STATUS_SYNC);
680         napi_throw(env, businessError);
681         return nullptr;
682     }
683     auto appControlProxy = CommonFunc::GetAppControlProxy();
684     if (appControlProxy == nullptr) {
685         APP_LOGE("AppControlProxy is null");
686         napi_value error = BusinessError::CreateCommonError(env, ERROR_SYSTEM_ABILITY_NOT_FOUND,
687             GET_DISPOSED_STATUS_SYNC);
688         napi_throw(env, error);
689         return nullptr;
690     }
691     DisposedRule disposedRule;
692     ErrCode ret = ERR_OK;
693     if (appIndex == Constants::MAIN_APP_INDEX) {
694         ret = appControlProxy->GetDisposedRule(appId, disposedRule);
695     } else {
696         ret = appControlProxy->GetDisposedRuleForCloneApp(appId, disposedRule, appIndex);
697     }
698     ret = CommonFunc::ConvertErrCode(ret);
699     if (ret != ERR_OK) {
700         APP_LOGE("GetDisposedStatusSync failed");
701         napi_value businessError = BusinessError::CreateCommonError(
702             env, ret, GET_DISPOSED_STATUS_SYNC, PERMISSION_DISPOSED_STATUS);
703         napi_throw(env, businessError);
704         return nullptr;
705     }
706     napi_value nRule = nullptr;
707     NAPI_CALL(env, napi_create_object(env, &nRule));
708     ConvertRuleInfo(env, nRule, disposedRule);
709     return nRule;
710 }
711 
GetDisposedRule(napi_env env,napi_callback_info info)712 napi_value GetDisposedRule(napi_env env, napi_callback_info info)
713 {
714     APP_LOGD("NAPI GetDisposedRule called");
715     NapiArg args(env, info);
716     if (!args.Init(ARGS_SIZE_ONE, ARGS_SIZE_TWO)) {
717         APP_LOGE("param count invalid");
718         BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
719         return nullptr;
720     }
721     std::string appId;
722     if (!CommonFunc::ParseString(env, args[ARGS_POS_ZERO], appId)) {
723         APP_LOGE("appId %{public}s invalid", appId.c_str());
724         BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, APP_ID, TYPE_STRING);
725         return nullptr;
726     }
727     int32_t appIndex = Constants::MAIN_APP_INDEX;
728     if (args.GetMaxArgc() == ARGS_SIZE_ONE) {
729         return InnerGetDisposedRule(env, appId, appIndex);
730     }
731     if (args.GetMaxArgc() == ARGS_SIZE_TWO) {
732         if (!CommonFunc::ParseInt(env, args[ARGS_POS_ONE], appIndex)) {
733             APP_LOGW("parse appIndex falied");
734         }
735         return InnerGetDisposedRule(env, appId, appIndex);
736     }
737     APP_LOGE("parameter is invalid");
738     BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_TYPE_CHECK_ERROR);
739     return nullptr;
740 }
741 
InnerSetDisposedRule(napi_env env,std::string & appId,DisposedRule & rule,int32_t appIndex)742 static napi_value InnerSetDisposedRule(napi_env env, std::string &appId, DisposedRule &rule, int32_t appIndex)
743 {
744     napi_value nRet;
745     napi_get_undefined(env, &nRet);
746     auto appControlProxy = CommonFunc::GetAppControlProxy();
747     if (appControlProxy == nullptr) {
748         APP_LOGE("AppControlProxy is null.");
749         napi_value error = BusinessError::CreateCommonError(env, ERROR_SYSTEM_ABILITY_NOT_FOUND,
750             SET_DISPOSED_RULE);
751         napi_throw(env, error);
752         return nRet;
753     }
754     ErrCode ret = ERR_OK;
755     if (appIndex == Constants::MAIN_APP_INDEX) {
756         ret = appControlProxy->SetDisposedRule(appId, rule);
757     } else {
758         ret = appControlProxy->SetDisposedRuleForCloneApp(appId, rule, appIndex);
759     }
760     ret = CommonFunc::ConvertErrCode(ret);
761     if (ret != NO_ERROR) {
762         APP_LOGE("SetDisposedRule err = %{public}d", ret);
763         napi_value businessError = BusinessError::CreateCommonError(
764             env, ret, SET_DISPOSED_RULE, PERMISSION_DISPOSED_STATUS);
765         napi_throw(env, businessError);
766         return nullptr;
767     }
768     return nRet;
769 }
770 
SetDisposedRule(napi_env env,napi_callback_info info)771 napi_value SetDisposedRule(napi_env env, napi_callback_info info)
772 {
773     NapiArg args(env, info);
774     napi_value nRet;
775     napi_get_undefined(env, &nRet);
776     if (!args.Init(ARGS_SIZE_TWO, ARGS_SIZE_THREE)) {
777         APP_LOGE("Napi func init failed");
778         BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
779         return nRet;
780     }
781     std::string appId;
782     if (!CommonFunc::ParseString(env, args[ARGS_POS_ZERO], appId)) {
783         APP_LOGE("appId %{public}s invalid!", appId.c_str());
784         BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, APP_ID, TYPE_STRING);
785         return nRet;
786     }
787     if (appId.empty()) {
788         napi_value businessError = BusinessError::CreateCommonError(
789             env, ERROR_INVALID_APPID, SET_DISPOSED_RULE);
790         napi_throw(env, businessError);
791         return nullptr;
792     }
793     DisposedRule rule;
794     if (!ParseDiposedRule(env, args[ARGS_POS_ONE], rule)) {
795         APP_LOGE("rule invalid!");
796         BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, DISPOSED_RULE, DISPOSED_RULE_TYPE);
797         return nRet;
798     }
799     int32_t appIndex = Constants::MAIN_APP_INDEX;
800     if (args.GetMaxArgc() == ARGS_SIZE_TWO) {
801         return InnerSetDisposedRule(env, appId, rule, appIndex);
802     }
803     if (args.GetMaxArgc() == ARGS_SIZE_THREE) {
804         if (!CommonFunc::ParseInt(env, args[ARGS_POS_TWO], appIndex)) {
805             APP_LOGW("parse appIndex falied");
806         }
807         return InnerSetDisposedRule(env, appId, rule, appIndex);
808     }
809     APP_LOGE("parameter is invalid");
810     BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_TYPE_CHECK_ERROR);
811     return nRet;
812 }
813 
SetDisposedRules(napi_env env,napi_callback_info info)814 napi_value SetDisposedRules(napi_env env, napi_callback_info info)
815 {
816     NapiArg args(env, info);
817     napi_value nRet;
818     napi_get_undefined(env, &nRet);
819     if (!args.Init(ARGS_SIZE_ONE, ARGS_SIZE_ONE)) {
820         APP_LOGE("Napi func init failed");
821         BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
822         return nRet;
823     }
824     std::vector<DisposedRuleConfiguration> disposedRuleConfigurations;
825     if (!ParseDisposedRuleConfigurationArray(env, args[ARGS_POS_ZERO], disposedRuleConfigurations)) {
826         APP_LOGE("disposedRuleConfigurations invalid!");
827         BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_TYPE_CHECK_ERROR);
828         return nRet;
829     }
830     auto appControlProxy = CommonFunc::GetAppControlProxy();
831     if (appControlProxy == nullptr) {
832         APP_LOGE("AppControlProxy is null");
833         napi_value error = BusinessError::CreateCommonError(env, ERROR_SYSTEM_ABILITY_NOT_FOUND,
834             SET_DISPOSED_RULES);
835         napi_throw(env, error);
836         return nullptr;
837     }
838     int32_t userId = OHOS::IPCSkeleton::GetCallingUid() / Constants::BASE_USER_RANGE;
839     ErrCode ret = appControlProxy->SetDisposedRules(disposedRuleConfigurations, userId);
840     ret = CommonFunc::ConvertErrCode(ret);
841     if (ret != ERR_OK) {
842         APP_LOGE("SetDisposedRules failed");
843         napi_value businessError = BusinessError::CreateCommonError(
844             env, ret, SET_DISPOSED_RULES, PERMISSION_DISPOSED_STATUS);
845         napi_throw(env, businessError);
846         return nullptr;
847     }
848     return nRet;
849 }
850 
ConvertRuleInfo(napi_env env,napi_value nRule,const UninstallDisposedRule & rule)851 void ConvertRuleInfo(napi_env env, napi_value nRule, const UninstallDisposedRule &rule)
852 {
853     napi_value nWant = nullptr;
854     if (rule.want != nullptr) {
855         nWant = CreateJsWant(env, *rule.want);
856     } else {
857         napi_create_object(env, &nWant);
858     }
859     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, nRule, "want", nWant));
860     napi_value uninstallComponentType;
861     NAPI_CALL_RETURN_VOID(
862         env, napi_create_uint32(env, static_cast<int32_t>(rule.uninstallComponentType), &uninstallComponentType));
863     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, nRule, "uninstallComponentType", uninstallComponentType));
864     napi_value nPriority;
865     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, rule.priority, &nPriority));
866     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, nRule, "priority", nPriority));
867 }
868 
869 
ParseUninstallDiposedRule(napi_env env,napi_value nRule,UninstallDisposedRule & rule)870 bool ParseUninstallDiposedRule(napi_env env, napi_value nRule, UninstallDisposedRule &rule)
871 {
872     napi_valuetype valueType;
873     NAPI_CALL_BASE(env, napi_typeof(env, nRule, &valueType), false);
874     if (valueType != napi_object) {
875         APP_LOGW("nRule not object type");
876         return false;
877     }
878     napi_value prop = nullptr;
879     napi_get_named_property(env, nRule, TYPE_WANT, &prop);
880     AAFwk::Want want;
881     if (!UnwrapWant(env, prop, want)) {
882         APP_LOGW("parse want failed");
883         return false;
884     }
885     rule.want = std::make_shared<AAFwk::Want>(want);
886     napi_get_named_property(env, nRule, "uninstallComponentType", &prop);
887     int32_t uninstallComponentType;
888     if (!CommonFunc::ParseInt(env, prop, uninstallComponentType)) {
889         APP_LOGW("uninstallComponentType parseInt failed");
890         return false;
891     }
892     if (uninstallComponentType != static_cast<int32_t>(UninstallComponentType::EXTENSION)) {
893         APP_LOGW("uninstallComponentType not valid");
894         return false;
895     }
896     rule.uninstallComponentType = static_cast<UninstallComponentType>(uninstallComponentType);
897     napi_get_named_property(env, nRule, "priority", &prop);
898     if (!CommonFunc::ParseInt(env, prop, rule.priority)) {
899         APP_LOGW("priority parseInt failed");
900         return false;
901     }
902     return true;
903 }
904 
InnerGetUninstallDisposedRule(napi_env env,std::string & appIdentifier,int32_t appIndex,int32_t userId)905 static napi_value InnerGetUninstallDisposedRule(napi_env env, std::string &appIdentifier,
906     int32_t appIndex, int32_t userId)
907 {
908     if (appIdentifier.empty()) {
909         napi_value businessError = BusinessError::CreateCommonError(
910             env, ERROR_INVALID_APPIDENTIFIER, GET_UNINSTALL_DISPOSED_RULE);
911         napi_throw(env, businessError);
912         return nullptr;
913     }
914     auto appControlProxy = CommonFunc::GetAppControlProxy();
915     if (appControlProxy == nullptr) {
916         APP_LOGE("null appControlProxy");
917         napi_value error = BusinessError::CreateCommonError(env, ERROR_SYSTEM_ABILITY_NOT_FOUND,
918             GET_UNINSTALL_DISPOSED_RULE);
919         napi_throw(env, error);
920         return nullptr;
921     }
922     UninstallDisposedRule uninstallDisposedRule;
923     ErrCode ret = ERR_OK;
924     ret = appControlProxy->GetUninstallDisposedRule(appIdentifier, appIndex, userId, uninstallDisposedRule);
925     ret = CommonFunc::ConvertErrCode(ret);
926     if (ret != ERR_OK) {
927         APP_LOGE("GetUninstallDisposedRule failed");
928         napi_value businessError = BusinessError::CreateCommonError(
929             env, ret, GET_UNINSTALL_DISPOSED_RULE, PERMISSION_DISPOSED_STATUS);
930         napi_throw(env, businessError);
931         return nullptr;
932     }
933     napi_value nRule = nullptr;
934     NAPI_CALL(env, napi_create_object(env, &nRule));
935     ConvertRuleInfo(env, nRule, uninstallDisposedRule);
936     return nRule;
937 }
938 
GetUninstallDisposedRule(napi_env env,napi_callback_info info)939 napi_value GetUninstallDisposedRule(napi_env env, napi_callback_info info)
940 {
941     APP_LOGD("begin");
942     NapiArg args(env, info);
943     if (!args.Init(ARGS_SIZE_ONE, ARGS_SIZE_TWO)) {
944         APP_LOGE("param count invalid");
945         BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
946         return nullptr;
947     }
948     std::string appIdentifier;
949     if (!CommonFunc::ParseString(env, args[ARGS_POS_ZERO], appIdentifier)) {
950         APP_LOGE("appIdentifier %{public}s invalid", appIdentifier.c_str());
951         BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, APP_IDENTIFIER, TYPE_STRING);
952         return nullptr;
953     }
954     int32_t appIndex = Constants::MAIN_APP_INDEX;
955     int32_t userId = Constants::UNSPECIFIED_USERID;
956     if (args.GetMaxArgc() == ARGS_SIZE_ONE) {
957         return InnerGetUninstallDisposedRule(env, appIdentifier, appIndex, userId);
958     }
959     if (args.GetMaxArgc() == ARGS_SIZE_TWO) {
960         if (!CommonFunc::ParseInt(env, args[ARGS_POS_ONE], appIndex)) {
961             APP_LOGW("parse appIndex falied");
962         }
963         return InnerGetUninstallDisposedRule(env, appIdentifier, appIndex, userId);
964     }
965     APP_LOGE("parameter is invalid");
966     BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_TYPE_CHECK_ERROR);
967     return nullptr;
968 }
969 
InnerSetUninstallDisposedRule(napi_env env,std::string & appIdentifier,UninstallDisposedRule & rule,int32_t appIndex,int32_t userId)970 static napi_value InnerSetUninstallDisposedRule(napi_env env, std::string &appIdentifier,
971     UninstallDisposedRule &rule, int32_t appIndex, int32_t userId)
972 {
973     napi_value nRet;
974     napi_get_undefined(env, &nRet);
975     auto appControlProxy = CommonFunc::GetAppControlProxy();
976     if (appControlProxy == nullptr) {
977         APP_LOGE("null appControlProxy");
978         napi_value error = BusinessError::CreateCommonError(env, ERROR_SYSTEM_ABILITY_NOT_FOUND,
979             SET_UNINSTALL_DISPOSED_RULE);
980         napi_throw(env, error);
981         return nRet;
982     }
983     ErrCode ret = ERR_OK;
984     ret = appControlProxy->SetUninstallDisposedRule(appIdentifier, rule, appIndex, userId);
985     ret = CommonFunc::ConvertErrCode(ret);
986     if (ret != NO_ERROR) {
987         APP_LOGE("SetUninstallDisposedRule err = %{public}d", ret);
988         napi_value businessError = BusinessError::CreateCommonError(
989             env, ret, SET_UNINSTALL_DISPOSED_RULE, PERMISSION_DISPOSED_STATUS);
990         napi_throw(env, businessError);
991         return nullptr;
992     }
993     return nRet;
994 }
995 
SetUninstallDisposedRule(napi_env env,napi_callback_info info)996 napi_value SetUninstallDisposedRule(napi_env env, napi_callback_info info)
997 {
998     APP_LOGD("begin");
999     NapiArg args(env, info);
1000     napi_value nRet;
1001     napi_get_undefined(env, &nRet);
1002     if (!args.Init(ARGS_SIZE_TWO, ARGS_SIZE_THREE)) {
1003         APP_LOGE("Napi func init failed");
1004         BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
1005         return nRet;
1006     }
1007     std::string appIdentifier;
1008     if (!CommonFunc::ParseString(env, args[ARGS_POS_ZERO], appIdentifier)) {
1009         APP_LOGE("appIdentifier %{public}s invalid!", appIdentifier.c_str());
1010         BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, APP_IDENTIFIER, TYPE_STRING);
1011         return nRet;
1012     }
1013     if (appIdentifier.empty()) {
1014         napi_value businessError = BusinessError::CreateCommonError(
1015             env, ERROR_INVALID_APPIDENTIFIER, SET_UNINSTALL_DISPOSED_RULE);
1016         napi_throw(env, businessError);
1017         return nullptr;
1018     }
1019     UninstallDisposedRule rule;
1020     if (!ParseUninstallDiposedRule(env, args[ARGS_POS_ONE], rule)) {
1021         APP_LOGE("rule invalid!");
1022         BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR,
1023             UNINSTALL_DISPOSED_RULE, UNINSTALL_DISPOSED_RULE_TYPE);
1024         return nRet;
1025     }
1026     int32_t appIndex = Constants::MAIN_APP_INDEX;
1027     int32_t userId = Constants::UNSPECIFIED_USERID;
1028     if (args.GetMaxArgc() == ARGS_SIZE_TWO) {
1029         return InnerSetUninstallDisposedRule(env, appIdentifier, rule, appIndex, userId);
1030     }
1031     if (args.GetMaxArgc() == ARGS_SIZE_THREE) {
1032         if (!CommonFunc::ParseInt(env, args[ARGS_POS_TWO], appIndex)) {
1033             APP_LOGW("parse appIndex falied");
1034         }
1035         return InnerSetUninstallDisposedRule(env, appIdentifier, rule, appIndex, userId);
1036     }
1037     APP_LOGE("parameter is invalid");
1038     BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_TYPE_CHECK_ERROR);
1039     return nRet;
1040 }
1041 
InnerDeleteUninstallDisposedRule(napi_env env,std::string & appIdentifier,int32_t appIndex,int32_t userId)1042 static napi_value InnerDeleteUninstallDisposedRule(napi_env env, std::string &appIdentifier,
1043     int32_t appIndex, int32_t userId)
1044 {
1045     napi_value nRet;
1046     napi_get_undefined(env, &nRet);
1047     if (appIdentifier.empty()) {
1048         napi_value businessError = BusinessError::CreateCommonError(
1049             env, ERROR_INVALID_APPIDENTIFIER, DELETE_UNINSTALL_DISPOSED_RULE);
1050         napi_throw(env, businessError);
1051         return nullptr;
1052     }
1053     auto appControlProxy = CommonFunc::GetAppControlProxy();
1054     if (appControlProxy == nullptr) {
1055         APP_LOGE("null appControlProxy");
1056         napi_value error = BusinessError::CreateCommonError(env, ERROR_SYSTEM_ABILITY_NOT_FOUND,
1057             DELETE_UNINSTALL_DISPOSED_RULE);
1058         napi_throw(env, error);
1059         return nullptr;
1060     }
1061     ErrCode ret = ERR_OK;
1062     ret = appControlProxy->DeleteUninstallDisposedRule(appIdentifier, appIndex, userId);
1063     ret = CommonFunc::ConvertErrCode(ret);
1064     if (ret != ERR_OK) {
1065         APP_LOGE("DeleteUninstallDisposedRule failed");
1066         napi_value businessError = BusinessError::CreateCommonError(
1067             env, ret, DELETE_UNINSTALL_DISPOSED_RULE, PERMISSION_DISPOSED_STATUS);
1068         napi_throw(env, businessError);
1069         return nullptr;
1070     }
1071     return nRet;
1072 }
1073 
DeleteUninstallDisposedRule(napi_env env,napi_callback_info info)1074 napi_value DeleteUninstallDisposedRule(napi_env env, napi_callback_info info)
1075 {
1076     APP_LOGD("begin");
1077     NapiArg args(env, info);
1078     napi_value nRet;
1079     napi_get_undefined(env, &nRet);
1080     if (!args.Init(ARGS_SIZE_ONE, ARGS_SIZE_TWO)) {
1081         APP_LOGE("param count invalid");
1082         BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
1083         return nRet;
1084     }
1085     std::string appIdentifier;
1086     if (!CommonFunc::ParseString(env, args[ARGS_POS_ZERO], appIdentifier)) {
1087         APP_LOGE("appIdentifier %{public}s invalid", appIdentifier.c_str());
1088         BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, APP_IDENTIFIER, TYPE_STRING);
1089         return nRet;
1090     }
1091     int32_t appIndex = Constants::MAIN_APP_INDEX;
1092     int32_t userId = Constants::UNSPECIFIED_USERID;
1093     if (args.GetMaxArgc() == ARGS_SIZE_ONE) {
1094         return InnerDeleteUninstallDisposedRule(env, appIdentifier, appIndex, userId);
1095     }
1096     if (args.GetMaxArgc() == ARGS_SIZE_TWO) {
1097         if (!CommonFunc::ParseInt(env, args[ARGS_POS_ONE], appIndex)) {
1098             APP_LOGW("parse appIndex falied");
1099         }
1100         return InnerDeleteUninstallDisposedRule(env, appIdentifier, appIndex, userId);
1101     }
1102     APP_LOGE("parameter is invalid");
1103     BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_TYPE_CHECK_ERROR);
1104     return nRet;
1105 }
1106 
CreateComponentType(napi_env env,napi_value value)1107 void CreateComponentType(napi_env env, napi_value value)
1108 {
1109     napi_value nUiAbilityType;
1110     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(ComponentType::UI_ABILITY),
1111         &nUiAbilityType));
1112     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "UI_ABILITY",
1113         nUiAbilityType));
1114     napi_value nExtensionAbilityType;
1115     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(ComponentType::UI_EXTENSION),
1116         &nExtensionAbilityType));
1117     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "UI_EXTENSION",
1118         nExtensionAbilityType));
1119 }
1120 
CreateUninstallComponentType(napi_env env,napi_value value)1121 void CreateUninstallComponentType(napi_env env, napi_value value)
1122 {
1123     napi_value nExtensionAbilityType;
1124     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(UninstallComponentType::EXTENSION),
1125         &nExtensionAbilityType));
1126     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "EXTENSION",
1127         nExtensionAbilityType));
1128 }
1129 
CreateDisposedType(napi_env env,napi_value value)1130 void CreateDisposedType(napi_env env, napi_value value)
1131 {
1132     napi_value nBlockApplication;
1133     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(DisposedType::BLOCK_APPLICATION),
1134         &nBlockApplication));
1135     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "BLOCK_APPLICATION",
1136         nBlockApplication));
1137     napi_value nBlockAbility;
1138     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(DisposedType::BLOCK_ABILITY),
1139         &nBlockAbility));
1140     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "BLOCK_ABILITY",
1141         nBlockAbility));
1142     napi_value nNonBlock;
1143     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(DisposedType::NON_BLOCK),
1144         &nNonBlock));
1145     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "NON_BLOCK",
1146         nNonBlock));
1147 }
1148 
CreateControlType(napi_env env,napi_value value)1149 void CreateControlType(napi_env env, napi_value value)
1150 {
1151     napi_value nAllowedList;
1152     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(ControlType::ALLOWED_LIST),
1153         &nAllowedList));
1154     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "ALLOWED_LIST",
1155         nAllowedList));
1156     napi_value nDisAllowedList;
1157     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(ControlType::DISALLOWED_LIST),
1158         &nDisAllowedList));
1159     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "DISALLOWED_LIST",
1160         nDisAllowedList));
1161 }
1162 }
1163 }