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