• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "js_default_app.h"
17 
18 #include <string>
19 
20 #include "app_log_wrapper.h"
21 #include "bundle_errors.h"
22 #include "bundle_mgr_interface.h"
23 #include "bundle_mgr_proxy.h"
24 #include "business_error.h"
25 #include "common_func.h"
26 #include "if_system_ability_manager.h"
27 #include "ipc_skeleton.h"
28 #include "iservice_registry.h"
29 #include "napi_arg.h"
30 #include "napi_constants.h"
31 #include "system_ability_definition.h"
32 
33 namespace OHOS {
34 namespace AppExecFwk {
35 using namespace OHOS::AAFwk;
36 
37 namespace {
38 constexpr int32_t NAPI_RETURN_ZERO = 0;
39 const char* PARAM_TYPE_CHECK_ERROR_WITH_POS = "param type check error, error position : ";
40 }
41 
ParseType(napi_env env,napi_value value,std::string & result)42 static bool ParseType(napi_env env, napi_value value, std::string& result)
43 {
44     napi_valuetype valueType = napi_undefined;
45     napi_typeof(env, value, &valueType);
46     if (valueType != napi_string) {
47         APP_LOGE("type not string");
48         return false;
49     }
50     size_t size = 0;
51     if (napi_get_value_string_utf8(env, value, nullptr, NAPI_RETURN_ZERO, &size) != napi_ok) {
52         APP_LOGE("napi_get_value_string_utf8 error");
53         return false;
54     }
55     result.reserve(size + 1);
56     result.resize(size);
57     if (napi_get_value_string_utf8(env, value, result.data(), (size + 1), &size) != napi_ok) {
58         APP_LOGE("napi_get_value_string_utf8 error");
59         return false;
60     }
61     if (TYPE_MAPPING.find(result) != TYPE_MAPPING.end()) {
62         result = TYPE_MAPPING.at(result);
63     }
64     return true;
65 }
66 
ConvertAbilityInfo(napi_env env,napi_value objAbilityInfo,const AbilityInfo & abilityInfo)67 static void ConvertAbilityInfo(napi_env env, napi_value objAbilityInfo, const AbilityInfo &abilityInfo)
68 {
69     APP_LOGD("begin to ConvertAbilityInfo");
70     napi_value nBundleName;
71     NAPI_CALL_RETURN_VOID(
72         env, napi_create_string_utf8(env, abilityInfo.bundleName.c_str(), NAPI_AUTO_LENGTH, &nBundleName));
73     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, objAbilityInfo, "bundleName", nBundleName));
74 
75     napi_value nModuleName;
76     NAPI_CALL_RETURN_VOID(
77         env, napi_create_string_utf8(env, abilityInfo.moduleName.c_str(), NAPI_AUTO_LENGTH, &nModuleName));
78     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, objAbilityInfo, "moduleName", nModuleName));
79 
80     napi_value nName;
81     NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, abilityInfo.name.c_str(), NAPI_AUTO_LENGTH, &nName));
82     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, objAbilityInfo, "name", nName));
83 
84     napi_value nLabel;
85     NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, abilityInfo.label.c_str(), NAPI_AUTO_LENGTH, &nLabel));
86     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, objAbilityInfo, "label", nLabel));
87 
88     napi_value nLabelId;
89     NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, abilityInfo.labelId, &nLabelId));
90     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, objAbilityInfo, "labelId", nLabelId));
91 
92     napi_value nDescription;
93     NAPI_CALL_RETURN_VOID(
94         env, napi_create_string_utf8(env, abilityInfo.description.c_str(), NAPI_AUTO_LENGTH, &nDescription));
95     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, objAbilityInfo, "description", nDescription));
96 
97     napi_value nDescriptionId;
98     NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, abilityInfo.descriptionId, &nDescriptionId));
99     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, objAbilityInfo, "descriptionId", nDescriptionId));
100 
101     napi_value nIconPath;
102     NAPI_CALL_RETURN_VOID(
103         env, napi_create_string_utf8(env, abilityInfo.iconPath.c_str(), NAPI_AUTO_LENGTH, &nIconPath));
104     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, objAbilityInfo, "icon", nIconPath));
105 
106     napi_value nIconId;
107     NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, abilityInfo.iconId, &nIconId));
108     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, objAbilityInfo, "iconId", nIconId));
109     APP_LOGD("ConvertAbilityInfo done");
110 }
111 
ConvertExtensionInfo(napi_env env,napi_value objExtensionInfo,const ExtensionAbilityInfo & extensionInfo)112 static void ConvertExtensionInfo(napi_env env, napi_value objExtensionInfo, const ExtensionAbilityInfo& extensionInfo)
113 {
114     APP_LOGD("begin to ConvertExtensionInfo");
115     napi_value nBundleName;
116     NAPI_CALL_RETURN_VOID(
117         env, napi_create_string_utf8(env, extensionInfo.bundleName.c_str(), NAPI_AUTO_LENGTH, &nBundleName));
118     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, objExtensionInfo, "bundleName", nBundleName));
119 
120     napi_value nModuleName;
121     NAPI_CALL_RETURN_VOID(
122         env, napi_create_string_utf8(env, extensionInfo.moduleName.c_str(), NAPI_AUTO_LENGTH, &nModuleName));
123     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, objExtensionInfo, "moduleName", nModuleName));
124 
125     napi_value nName;
126     NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, extensionInfo.name.c_str(), NAPI_AUTO_LENGTH, &nName));
127     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, objExtensionInfo, "name", nName));
128 
129     napi_value nLabelId;
130     NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, extensionInfo.labelId, &nLabelId));
131     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, objExtensionInfo, "labelId", nLabelId));
132 
133     napi_value nDescriptionId;
134     NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, extensionInfo.descriptionId, &nDescriptionId));
135     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, objExtensionInfo, "descriptionId", nDescriptionId));
136 
137     napi_value nIconId;
138     NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, extensionInfo.iconId, &nIconId));
139     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, objExtensionInfo, "iconId", nIconId));
140     APP_LOGD("ConvertExtensionInfo done");
141 }
142 
ConvertBundleInfo(napi_env env,napi_value objBundleInfo,const BundleInfo & bundleInfo)143 static void ConvertBundleInfo(napi_env env, napi_value objBundleInfo, const BundleInfo &bundleInfo)
144 {
145     APP_LOGD("begin to ConvertBundleInfo");
146     napi_value nName;
147     NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, bundleInfo.name.c_str(), NAPI_AUTO_LENGTH, &nName));
148     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, objBundleInfo, "name", nName));
149 
150     napi_value nHapModulesInfo;
151     NAPI_CALL_RETURN_VOID(env, napi_create_array(env, &nHapModulesInfo));
152     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, objBundleInfo, "hapModulesInfo", nHapModulesInfo));
153 
154     napi_value nAbilityInfos;
155     NAPI_CALL_RETURN_VOID(env, napi_create_array(env, &nAbilityInfos));
156     for (size_t idx = 0; idx < bundleInfo.abilityInfos.size(); idx++) {
157         napi_value objAbilityInfo;
158         NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &objAbilityInfo));
159         ConvertAbilityInfo(env, objAbilityInfo, bundleInfo.abilityInfos[idx]);
160         NAPI_CALL_RETURN_VOID(env, napi_set_element(env, nAbilityInfos, idx, objAbilityInfo));
161     }
162 
163     napi_value nHapModuleInfo;
164     NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &nHapModuleInfo));
165     NAPI_CALL_RETURN_VOID(env, napi_set_element(env, nHapModulesInfo, 0, nHapModuleInfo));
166 
167     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, nHapModuleInfo, "abilitiesInfo", nAbilityInfos));
168 
169     napi_value nExtensionAbilityInfos;
170     NAPI_CALL_RETURN_VOID(env, napi_create_array(env, &nExtensionAbilityInfos));
171     for (size_t idx = 0; idx < bundleInfo.extensionInfos.size(); idx++) {
172         napi_value objExtensionInfo;
173         NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &objExtensionInfo));
174         ConvertExtensionInfo(env, objExtensionInfo, bundleInfo.extensionInfos[idx]);
175         NAPI_CALL_RETURN_VOID(env, napi_set_element(env, nExtensionAbilityInfos, idx, objExtensionInfo));
176     }
177 
178     NAPI_CALL_RETURN_VOID(env,
179         napi_set_named_property(env, nHapModuleInfo, "extensionAbilitiesInfo", nExtensionAbilityInfos));
180     APP_LOGD("ConvertBundleInfo done");
181 }
182 
InnerIsDefaultApplication(DefaultAppCallbackInfo * info)183 static ErrCode InnerIsDefaultApplication(DefaultAppCallbackInfo *info)
184 {
185     if (info == nullptr) {
186         APP_LOGE("info is null");
187         return ERROR_BUNDLE_SERVICE_EXCEPTION;
188     }
189     auto defaultAppProxy = CommonFunc::GetDefaultAppProxy();
190     if (defaultAppProxy == nullptr) {
191         APP_LOGE("defaultAppProxy is null");
192         return ERROR_BUNDLE_SERVICE_EXCEPTION;
193     }
194     ErrCode ret = defaultAppProxy->IsDefaultApplication(info->type, info->isDefaultApp);
195     APP_LOGD("IsDefaultApplication ErrCode : %{public}d", ret);
196     return CommonFunc::ConvertErrCode(ret);
197 }
198 
IsDefaultApplicationExec(napi_env env,void * data)199 void IsDefaultApplicationExec(napi_env env, void *data)
200 {
201     DefaultAppCallbackInfo *asyncCallbackInfo = reinterpret_cast<DefaultAppCallbackInfo *>(data);
202     if (asyncCallbackInfo == nullptr) {
203         APP_LOGE("asyncCallbackInfo is null");
204         return;
205     }
206     asyncCallbackInfo->err = InnerIsDefaultApplication(asyncCallbackInfo);
207 }
208 
IsDefaultApplicationComplete(napi_env env,napi_status status,void * data)209 void IsDefaultApplicationComplete(napi_env env, napi_status status, void *data)
210 {
211     DefaultAppCallbackInfo *asyncCallbackInfo = reinterpret_cast<DefaultAppCallbackInfo *>(data);
212     if (asyncCallbackInfo == nullptr) {
213         APP_LOGE("asyncCallbackInfo is null");
214         return;
215     }
216     std::unique_ptr<DefaultAppCallbackInfo> callbackPtr {asyncCallbackInfo};
217     napi_value result[ARGS_SIZE_TWO] = {0};
218     if (asyncCallbackInfo->err == NO_ERROR) {
219         NAPI_CALL_RETURN_VOID(env, napi_get_null(env, &result[ARGS_POS_ZERO]));
220         NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, asyncCallbackInfo->isDefaultApp, &result[ARGS_POS_ONE]));
221     } else {
222         result[ARGS_POS_ZERO] = BusinessError::CreateCommonError(env, asyncCallbackInfo->err,
223             IS_DEFAULT_APPLICATION, "");
224     }
225     CommonFunc::NapiReturnDeferred<DefaultAppCallbackInfo>(env, asyncCallbackInfo, result, ARGS_SIZE_TWO);
226 }
227 
IsDefaultApplication(napi_env env,napi_callback_info info)228 napi_value IsDefaultApplication(napi_env env, napi_callback_info info)
229 {
230     APP_LOGD("begin of IsDefaultApplication");
231     NapiArg args(env, info);
232     DefaultAppCallbackInfo *asyncCallbackInfo = new (std::nothrow) DefaultAppCallbackInfo(env);
233     if (asyncCallbackInfo == nullptr) {
234         APP_LOGE("asyncCallbackInfo is null");
235         return nullptr;
236     }
237     std::unique_ptr<DefaultAppCallbackInfo> callbackPtr {asyncCallbackInfo};
238     if (!args.Init(ARGS_SIZE_ONE, ARGS_SIZE_TWO)) {
239         APP_LOGE("param count invalid");
240         BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
241         return nullptr;
242     }
243     for (size_t i = 0; i < args.GetMaxArgc(); ++i) {
244         napi_valuetype valueType = napi_undefined;
245         napi_typeof(env, args[i], &valueType);
246         if ((i == ARGS_POS_ZERO) && (valueType == napi_string)) {
247             if (!ParseType(env, args[i], asyncCallbackInfo->type)) {
248                 APP_LOGE("type invalid");
249                 BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_TYPE_CHECK_ERROR);
250                 return nullptr;
251             }
252         } else if (i == ARGS_POS_ONE) {
253             if (valueType == napi_function) {
254                 NAPI_CALL(env, napi_create_reference(env, args[i], NAPI_RETURN_ONE, &asyncCallbackInfo->callback));
255             }
256             break;
257         } else {
258             APP_LOGE("param check error");
259             std::string errMsg = PARAM_TYPE_CHECK_ERROR_WITH_POS + std::to_string(i + 1);
260             BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, errMsg);
261             return nullptr;
262         }
263     }
264     auto promise = CommonFunc::AsyncCallNativeMethod<DefaultAppCallbackInfo>(
265         env, asyncCallbackInfo, IS_DEFAULT_APPLICATION, IsDefaultApplicationExec, IsDefaultApplicationComplete);
266     callbackPtr.release();
267     APP_LOGD("call IsDefaultApplication done");
268     return promise;
269 }
270 
ParamsProcessIsDefaultApplicationSync(napi_env env,napi_callback_info info,std::string & type)271 ErrCode ParamsProcessIsDefaultApplicationSync(napi_env env, napi_callback_info info,
272     std::string& type)
273 {
274     NapiArg args(env, info);
275     if (!args.Init(ARGS_SIZE_ONE, ARGS_SIZE_ONE)) {
276         APP_LOGE("param count invalid");
277         BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
278         return ERROR_PARAM_CHECK_ERROR;
279     }
280     for (size_t i = 0; i < args.GetMaxArgc(); ++i) {
281         if (i == ARGS_POS_ZERO) {
282             if (!ParseType(env, args[i], type)) {
283                 APP_LOGE("type %{public}s invalid", type.c_str());
284                 BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, TYPE_CHECK, TYPE_STRING);
285                 return ERROR_PARAM_CHECK_ERROR;
286             }
287         } else {
288             APP_LOGE("param check error");
289             std::string errMsg = PARAM_TYPE_CHECK_ERROR_WITH_POS + std::to_string(i + 1);
290             BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, errMsg);
291             return ERROR_PARAM_CHECK_ERROR;
292         }
293     }
294     return ERR_OK;
295 }
296 
IsDefaultApplicationSync(napi_env env,napi_callback_info info)297 napi_value IsDefaultApplicationSync(napi_env env, napi_callback_info info)
298 {
299     APP_LOGD("begin to IsDefaultApplicationSync");
300     napi_value nRet;
301     bool isDefaultApp = false;
302     napi_get_boolean(env, isDefaultApp, &nRet);
303     std::string type;
304     if (ParamsProcessIsDefaultApplicationSync(env, info, type) != ERR_OK) {
305         return nRet;
306     }
307 
308     auto defaultAppProxy = CommonFunc::GetDefaultAppProxy();
309     if (defaultAppProxy == nullptr) {
310         napi_value error = BusinessError::CreateCommonError(env, ERROR_BUNDLE_SERVICE_EXCEPTION,
311             IS_DEFAULT_APPLICATION_SYNC);
312         napi_throw(env, error);
313         return nRet;
314     }
315 
316     ErrCode ret = defaultAppProxy->IsDefaultApplication(type, isDefaultApp);
317     ret = CommonFunc::ConvertErrCode(ret);
318     if (ret != ERR_OK) {
319         APP_LOGE("ResetDefaultApplicationSync failed: %{public}d", ret);
320         napi_value businessError = BusinessError::CreateCommonError(
321             env, ret, IS_DEFAULT_APPLICATION_SYNC, "");
322         napi_throw(env, businessError);
323         return nRet;
324     }
325     NAPI_CALL(env, napi_get_boolean(env, isDefaultApp, &nRet));
326     APP_LOGD("call ResetDefaultApplicationSync done");
327     return nRet;
328 }
329 
InnerGetDefaultApplication(DefaultAppCallbackInfo * info)330 static ErrCode InnerGetDefaultApplication(DefaultAppCallbackInfo *info)
331 {
332     if (info == nullptr) {
333         APP_LOGE("info is null");
334         return ERROR_BUNDLE_SERVICE_EXCEPTION;
335     }
336     auto defaultAppProxy = CommonFunc::GetDefaultAppProxy();
337     if (defaultAppProxy == nullptr) {
338         APP_LOGE("defaultAppProxy is null");
339         return ERROR_BUNDLE_SERVICE_EXCEPTION;
340     }
341     ErrCode ret = defaultAppProxy->GetDefaultApplication(info->userId, info->type, info->bundleInfo);
342     APP_LOGD("GetDefaultApplication ErrCode : %{public}d", ret);
343     return CommonFunc::ConvertErrCode(ret);
344 }
345 
GetDefaultApplicationExec(napi_env env,void * data)346 void GetDefaultApplicationExec(napi_env env, void *data)
347 {
348     DefaultAppCallbackInfo *asyncCallbackInfo = reinterpret_cast<DefaultAppCallbackInfo *>(data);
349     if (asyncCallbackInfo == nullptr) {
350         APP_LOGE("asyncCallbackInfo is null");
351         return;
352     }
353     asyncCallbackInfo->err = InnerGetDefaultApplication(asyncCallbackInfo);
354 }
355 
GetDefaultApplicationComplete(napi_env env,napi_status status,void * data)356 void GetDefaultApplicationComplete(napi_env env, napi_status status, void *data)
357 {
358     DefaultAppCallbackInfo *asyncCallbackInfo = reinterpret_cast<DefaultAppCallbackInfo *>(data);
359     if (asyncCallbackInfo == nullptr) {
360         APP_LOGE("asyncCallbackInfo is null");
361         return;
362     }
363     std::unique_ptr<DefaultAppCallbackInfo> callbackPtr {asyncCallbackInfo};
364     napi_value result[ARGS_SIZE_TWO] = {0};
365     if (asyncCallbackInfo->err == NO_ERROR) {
366         NAPI_CALL_RETURN_VOID(env, napi_get_null(env, &result[ARGS_POS_ZERO]));
367         NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &result[ARGS_POS_ONE]));
368         ConvertBundleInfo(env, result[ARGS_POS_ONE], asyncCallbackInfo->bundleInfo);
369     } else {
370         result[ARGS_POS_ZERO] = BusinessError::CreateCommonError(env, asyncCallbackInfo->err,
371             GET_DEFAULT_APPLICATION, Constants::PERMISSION_GET_DEFAULT_APPLICATION);
372     }
373     CommonFunc::NapiReturnDeferred<DefaultAppCallbackInfo>(env, asyncCallbackInfo, result, ARGS_SIZE_TWO);
374 }
375 
GetDefaultApplication(napi_env env,napi_callback_info info)376 napi_value GetDefaultApplication(napi_env env, napi_callback_info info)
377 {
378     APP_LOGD("begin to GetDefaultApplication");
379     NapiArg args(env, info);
380     DefaultAppCallbackInfo *asyncCallbackInfo = new (std::nothrow) DefaultAppCallbackInfo(env);
381     if (asyncCallbackInfo == nullptr) {
382         APP_LOGE("asyncCallbackInfo is null");
383         return nullptr;
384     }
385     asyncCallbackInfo->userId = IPCSkeleton::GetCallingUid() / Constants::BASE_USER_RANGE;
386     std::unique_ptr<DefaultAppCallbackInfo> callbackPtr {asyncCallbackInfo};
387     if (!args.Init(ARGS_SIZE_ONE, ARGS_SIZE_THREE)) {
388         APP_LOGE("param count invalid");
389         BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
390         return nullptr;
391     }
392     for (size_t i = 0; i < args.GetMaxArgc(); ++i) {
393         napi_valuetype valueType = napi_undefined;
394         napi_typeof(env, args[i], &valueType);
395         if ((i == ARGS_POS_ZERO) && (valueType == napi_string)) {
396             if (!ParseType(env, args[i], asyncCallbackInfo->type)) {
397                 APP_LOGE("type invalid");
398                 BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_TYPE_CHECK_ERROR);
399                 return nullptr;
400             }
401         } else if (i == ARGS_POS_ONE) {
402             if (valueType == napi_function) {
403                 NAPI_CALL(env, napi_create_reference(env, args[i], NAPI_RETURN_ONE, &asyncCallbackInfo->callback));
404                 break;
405             }
406             if (!CommonFunc::ParseInt(env, args[i], asyncCallbackInfo->userId)) {
407                 APP_LOGW("Parse userId failed, set this parameter to the caller userId");
408             }
409         } else if (i == ARGS_POS_TWO) {
410             if (valueType == napi_function) {
411                 NAPI_CALL(env, napi_create_reference(env, args[i], NAPI_RETURN_ONE, &asyncCallbackInfo->callback));
412             }
413             break;
414         } else {
415             APP_LOGE("param check error");
416             std::string errMsg = PARAM_TYPE_CHECK_ERROR_WITH_POS + std::to_string(i + 1);
417             BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, errMsg);
418             return nullptr;
419         }
420     }
421     auto promise = CommonFunc::AsyncCallNativeMethod<DefaultAppCallbackInfo>(
422         env, asyncCallbackInfo, GET_DEFAULT_APPLICATION, GetDefaultApplicationExec, GetDefaultApplicationComplete);
423     callbackPtr.release();
424     APP_LOGD("call GetDefaultApplication done");
425     return promise;
426 }
427 
ParamsProcessGetDefaultApplicationSync(napi_env env,napi_callback_info info,std::string & type,int32_t & userId)428 ErrCode ParamsProcessGetDefaultApplicationSync(napi_env env, napi_callback_info info,
429     std::string& type, int32_t& userId)
430 {
431     NapiArg args(env, info);
432     if (!args.Init(ARGS_SIZE_ONE, ARGS_SIZE_TWO)) {
433         APP_LOGE("param count invalid");
434         BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
435         return ERROR_PARAM_CHECK_ERROR;
436     }
437     for (size_t i = 0; i < args.GetMaxArgc(); ++i) {
438         if (i == ARGS_POS_ZERO) {
439             if (!ParseType(env, args[ARGS_POS_ZERO], type)) {
440                 APP_LOGE("type %{public}s invalid", type.c_str());
441                 BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, TYPE_CHECK, TYPE_STRING);
442                 return ERROR_PARAM_CHECK_ERROR;
443             }
444         } else if (i == ARGS_POS_ONE) {
445             if (!CommonFunc::ParseInt(env, args[ARGS_POS_ONE], userId)) {
446                 APP_LOGE("parseInt failed");
447             }
448         } else {
449             APP_LOGE("param check error");
450             std::string errMsg = PARAM_TYPE_CHECK_ERROR_WITH_POS + std::to_string(i + 1);
451             BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, errMsg);
452             return ERROR_PARAM_CHECK_ERROR  ;
453         }
454     }
455     return ERR_OK;
456 }
457 
GetDefaultApplicationSync(napi_env env,napi_callback_info info)458 napi_value GetDefaultApplicationSync(napi_env env, napi_callback_info info)
459 {
460     APP_LOGD("begin to GetDefaultApplicationSync");
461     std::string type;
462     int32_t userId = IPCSkeleton::GetCallingUid() / Constants::BASE_USER_RANGE;
463     if (ParamsProcessGetDefaultApplicationSync(env, info, type, userId) != ERR_OK) {
464         return nullptr;
465     }
466 
467     auto defaultAppProxy = CommonFunc::GetDefaultAppProxy();
468     if (defaultAppProxy == nullptr) {
469         napi_value error = BusinessError::CreateCommonError(env, ERROR_BUNDLE_SERVICE_EXCEPTION,
470             GET_DEFAULT_APPLICATION_SYNC);
471         napi_throw(env, error);
472         return nullptr;
473     }
474 
475     BundleInfo bundleInfo;
476     ErrCode ret = defaultAppProxy->GetDefaultApplication(userId, type, bundleInfo);
477     ret = CommonFunc::ConvertErrCode(ret);
478     if (ret != ERR_OK) {
479         APP_LOGE("GetDefaultApplicationSync failed: %{public}d", ret);
480         napi_value businessError = BusinessError::CreateCommonError(
481             env, ret, GET_DEFAULT_APPLICATION_SYNC, Constants::PERMISSION_GET_DEFAULT_APPLICATION);
482         napi_throw(env, businessError);
483         return nullptr;
484     }
485 
486     napi_value nBundleInfo = nullptr;
487     NAPI_CALL(env, napi_create_object(env,  &nBundleInfo));
488     ConvertBundleInfo(env, nBundleInfo, bundleInfo);
489     APP_LOGD("call GetDefaultApplicationSync done");
490     return nBundleInfo;
491 }
492 
InnerSetDefaultApplication(const DefaultAppCallbackInfo * info)493 static ErrCode InnerSetDefaultApplication(const DefaultAppCallbackInfo *info)
494 {
495     if (info == nullptr) {
496         APP_LOGE("info is null");
497         return ERROR_BUNDLE_SERVICE_EXCEPTION;
498     }
499     auto defaultAppProxy = CommonFunc::GetDefaultAppProxy();
500     if (defaultAppProxy == nullptr) {
501         APP_LOGE("defaultAppProxy is null");
502         return ERROR_BUNDLE_SERVICE_EXCEPTION;
503     }
504     ErrCode ret = defaultAppProxy->SetDefaultApplication(info->userId, info->type, info->want);
505     APP_LOGD("SetDefaultApplication ErrCode : %{public}d", ret);
506     return CommonFunc::ConvertErrCode(ret);
507 }
508 
SetDefaultApplicationExec(napi_env env,void * data)509 void SetDefaultApplicationExec(napi_env env, void *data)
510 {
511     DefaultAppCallbackInfo *asyncCallbackInfo = reinterpret_cast<DefaultAppCallbackInfo *>(data);
512     if (asyncCallbackInfo == nullptr) {
513         APP_LOGE("asyncCallbackInfo is null");
514         return;
515     }
516     asyncCallbackInfo->err = InnerSetDefaultApplication(asyncCallbackInfo);
517 }
518 
SetDefaultApplicationComplete(napi_env env,napi_status status,void * data)519 void SetDefaultApplicationComplete(napi_env env, napi_status status, void *data)
520 {
521     DefaultAppCallbackInfo *asyncCallbackInfo = reinterpret_cast<DefaultAppCallbackInfo *>(data);
522     if (asyncCallbackInfo == nullptr) {
523         APP_LOGE("asyncCallbackInfo is null");
524         return;
525     }
526     std::unique_ptr<DefaultAppCallbackInfo> callbackPtr {asyncCallbackInfo};
527     napi_value result[ARGS_SIZE_ONE] = {0};
528     if (asyncCallbackInfo->err == NO_ERROR) {
529         NAPI_CALL_RETURN_VOID(env, napi_get_null(env, &result[ARGS_POS_ZERO]));
530     } else {
531         result[ARGS_POS_ZERO] = BusinessError::CreateCommonError(env, asyncCallbackInfo->err,
532             SET_DEFAULT_APPLICATION, Constants::PERMISSION_SET_DEFAULT_APPLICATION);
533     }
534     CommonFunc::NapiReturnDeferred<DefaultAppCallbackInfo>(env, asyncCallbackInfo, result, ARGS_SIZE_ONE);
535 }
536 
SetDefaultApplication(napi_env env,napi_callback_info info)537 napi_value SetDefaultApplication(napi_env env, napi_callback_info info)
538 {
539     APP_LOGD("begin to SetDefaultApplication");
540     NapiArg args(env, info);
541     DefaultAppCallbackInfo *asyncCallbackInfo = new (std::nothrow) DefaultAppCallbackInfo(env);
542     if (asyncCallbackInfo == nullptr) {
543         APP_LOGE("asyncCallbackInfo is null");
544         return nullptr;
545     }
546     asyncCallbackInfo->userId = IPCSkeleton::GetCallingUid() / Constants::BASE_USER_RANGE;
547     std::unique_ptr<DefaultAppCallbackInfo> callbackPtr {asyncCallbackInfo};
548     if (!args.Init(ARGS_SIZE_TWO, ARGS_SIZE_FOUR)) {
549         APP_LOGE("param count invalid");
550         BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
551         return nullptr;
552     }
553     for (size_t i = 0; i < args.GetMaxArgc(); ++i) {
554         napi_valuetype valueType = napi_undefined;
555         napi_typeof(env, args[i], &valueType);
556         if ((i == ARGS_POS_ZERO) && (valueType == napi_string)) {
557             if (!ParseType(env, args[i], asyncCallbackInfo->type)) {
558                 APP_LOGE("type invalid");
559                 BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_TYPE_CHECK_ERROR);
560                 return nullptr;
561             }
562         } else if ((i == ARGS_POS_ONE) && (valueType == napi_object)) {
563             if (!CommonFunc::ParseElementName(env, args[i], asyncCallbackInfo->want)) {
564                 APP_LOGE("invalid elementName");
565                 BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_TYPE_CHECK_ERROR);
566                 return nullptr;
567             }
568         } else if (i == ARGS_POS_TWO) {
569             if (valueType == napi_function) {
570                 NAPI_CALL(env, napi_create_reference(env, args[i], NAPI_RETURN_ONE, &asyncCallbackInfo->callback));
571                 break;
572             }
573             if (!CommonFunc::ParseInt(env, args[i], asyncCallbackInfo->userId)) {
574                 APP_LOGW("Parse userId failed, set this parameter to the caller userId");
575             }
576         } else if (i == ARGS_POS_THREE) {
577             if (valueType == napi_function) {
578                 NAPI_CALL(env, napi_create_reference(env, args[i], NAPI_RETURN_ONE, &asyncCallbackInfo->callback));
579             }
580             break;
581         } else {
582             APP_LOGE("param check error");
583             std::string errMsg = PARAM_TYPE_CHECK_ERROR_WITH_POS + std::to_string(i + 1);
584             BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, errMsg);
585             return nullptr;
586         }
587     }
588     auto promise = CommonFunc::AsyncCallNativeMethod<DefaultAppCallbackInfo>(
589         env, asyncCallbackInfo, SET_DEFAULT_APPLICATION, SetDefaultApplicationExec, SetDefaultApplicationComplete);
590     callbackPtr.release();
591     APP_LOGD("call SetDefaultApplication done");
592     return promise;
593 }
594 
ParamsProcessSetDefaultApplicationSync(napi_env env,napi_callback_info info,std::string & type,OHOS::AAFwk::Want & want,int32_t & userId)595 ErrCode ParamsProcessSetDefaultApplicationSync(napi_env env, napi_callback_info info,
596     std::string& type, OHOS::AAFwk::Want& want, int32_t& userId)
597 {
598     NapiArg args(env, info);
599     if (!args.Init(ARGS_SIZE_TWO, ARGS_SIZE_THREE)) {
600         APP_LOGE("param count invalid");
601         BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
602         return ERROR_PARAM_CHECK_ERROR;
603     }
604 
605     for (size_t i = 0; i < args.GetMaxArgc(); ++i) {
606         if (i == ARGS_POS_ZERO) {
607             if (!ParseType(env, args[i], type)) {
608                 APP_LOGE("type %{public}s invalid", type.c_str());
609                 BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, TYPE_CHECK, TYPE_STRING);
610                 return ERROR_PARAM_CHECK_ERROR;
611             }
612         } else if (i == ARGS_POS_ONE) {
613             if ((!CommonFunc::ParseElementName(env, args[i], want))) {
614                 APP_LOGE("parseElementName failed");
615                 BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, WANT_CHECK, TYPE_OBJECT);
616                 return ERROR_PARAM_CHECK_ERROR;
617             }
618         } else if (i == ARGS_POS_TWO) {
619             if ((!CommonFunc::ParseInt(env, args[i], userId))) {
620                 APP_LOGE("parseInt failed");
621             }
622         } else {
623             APP_LOGE("param check error");
624             std::string errMsg = PARAM_TYPE_CHECK_ERROR_WITH_POS + std::to_string(i + 1);
625             BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, errMsg);
626             return ERROR_PARAM_CHECK_ERROR;
627         }
628     }
629     return ERR_OK;
630 }
631 
SetDefaultApplicationSync(napi_env env,napi_callback_info info)632 napi_value SetDefaultApplicationSync(napi_env env, napi_callback_info info)
633 {
634     APP_LOGD("begin to SetDefaultApplicationSync");
635     napi_value nRet;
636     napi_get_undefined(env, &nRet);
637     std::string type;
638     OHOS::AAFwk::Want want;
639     int32_t userId = IPCSkeleton::GetCallingUid() / Constants::BASE_USER_RANGE;
640     if (ParamsProcessSetDefaultApplicationSync(env, info, type, want, userId) != ERR_OK) {
641         return nRet;
642     }
643     auto defaultAppProxy = CommonFunc::GetDefaultAppProxy();
644     if (defaultAppProxy == nullptr) {
645         napi_value error = BusinessError::CreateCommonError(env, ERROR_BUNDLE_SERVICE_EXCEPTION,
646             SET_DEFAULT_APPLICATION_SYNC);
647         napi_throw(env, error);
648         return nRet;
649     }
650 
651     ErrCode ret = defaultAppProxy->SetDefaultApplication(userId,
652         type, want);
653     ret = CommonFunc::ConvertErrCode(ret);
654     if (ret != ERR_OK) {
655         APP_LOGE("SetDefaultApplicationSync failed: %{public}d", ret);
656         napi_value businessError = BusinessError::CreateCommonError(
657             env, ret, SET_DEFAULT_APPLICATION_SYNC, Constants::PERMISSION_SET_DEFAULT_APPLICATION);
658         napi_throw(env, businessError);
659         return nRet;
660     }
661 
662     APP_LOGD("call SetDefaultApplicationSync done");
663     return nRet;
664 }
665 
InnerResetDefaultApplication(const DefaultAppCallbackInfo * info)666 static ErrCode InnerResetDefaultApplication(const DefaultAppCallbackInfo *info)
667 {
668     if (info == nullptr) {
669         APP_LOGE("info is null");
670         return ERROR_BUNDLE_SERVICE_EXCEPTION;
671     }
672     auto defaultAppProxy = CommonFunc::GetDefaultAppProxy();
673     if (defaultAppProxy == nullptr) {
674         APP_LOGE("defaultAppProxy is null");
675         return ERROR_BUNDLE_SERVICE_EXCEPTION;
676     }
677     ErrCode ret = defaultAppProxy->ResetDefaultApplication(info->userId, info->type);
678     APP_LOGD("ResetDefaultApplication ErrCode : %{public}d", ret);
679     return CommonFunc::ConvertErrCode(ret);
680 }
681 
ResetDefaultApplicationExec(napi_env env,void * data)682 void ResetDefaultApplicationExec(napi_env env, void *data)
683 {
684     DefaultAppCallbackInfo *asyncCallbackInfo = reinterpret_cast<DefaultAppCallbackInfo *>(data);
685     if (asyncCallbackInfo == nullptr) {
686         APP_LOGE("asyncCallbackInfo is null");
687         return;
688     }
689     asyncCallbackInfo->err = InnerResetDefaultApplication(asyncCallbackInfo);
690 }
691 
ResetDefaultApplicationComplete(napi_env env,napi_status status,void * data)692 void ResetDefaultApplicationComplete(napi_env env, napi_status status, void *data)
693 {
694     DefaultAppCallbackInfo *asyncCallbackInfo = reinterpret_cast<DefaultAppCallbackInfo *>(data);
695     if (asyncCallbackInfo == nullptr) {
696         APP_LOGE("asyncCallbackInfo is null");
697         return;
698     }
699     std::unique_ptr<DefaultAppCallbackInfo> callbackPtr {asyncCallbackInfo};
700     napi_value result[ARGS_SIZE_ONE] = {0};
701     if (asyncCallbackInfo->err == NO_ERROR) {
702         NAPI_CALL_RETURN_VOID(env, napi_get_null(env, &result[ARGS_POS_ZERO]));
703     } else {
704         result[ARGS_POS_ZERO] = BusinessError::CreateCommonError(env, asyncCallbackInfo->err,
705             RESET_DEFAULT_APPLICATION, Constants::PERMISSION_SET_DEFAULT_APPLICATION);
706     }
707     CommonFunc::NapiReturnDeferred<DefaultAppCallbackInfo>(env, asyncCallbackInfo, result, ARGS_SIZE_ONE);
708 }
709 
ResetDefaultApplication(napi_env env,napi_callback_info info)710 napi_value ResetDefaultApplication(napi_env env, napi_callback_info info)
711 {
712     APP_LOGD("begin to ResetDefaultApplication");
713     NapiArg args(env, info);
714     DefaultAppCallbackInfo *asyncCallbackInfo = new (std::nothrow) DefaultAppCallbackInfo(env);
715     if (asyncCallbackInfo == nullptr) {
716         APP_LOGE("asyncCallbackInfo is null");
717         return nullptr;
718     }
719     asyncCallbackInfo->userId = IPCSkeleton::GetCallingUid() / Constants::BASE_USER_RANGE;
720     std::unique_ptr<DefaultAppCallbackInfo> callbackPtr {asyncCallbackInfo};
721     if (!args.Init(ARGS_SIZE_ONE, ARGS_SIZE_THREE)) {
722         APP_LOGE("param count invalid");
723         BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
724         return nullptr;
725     }
726     for (size_t i = 0; i < args.GetMaxArgc(); ++i) {
727         napi_valuetype valueType = napi_undefined;
728         napi_typeof(env, args[i], &valueType);
729         if ((i == ARGS_POS_ZERO) && (valueType == napi_string)) {
730             if (!ParseType(env, args[i], asyncCallbackInfo->type)) {
731                 APP_LOGE("type invalid");
732                 BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_TYPE_CHECK_ERROR);
733                 return nullptr;
734             }
735         } else if (i == ARGS_POS_ONE) {
736             if (valueType == napi_function) {
737                 NAPI_CALL(env, napi_create_reference(env, args[i], NAPI_RETURN_ONE, &asyncCallbackInfo->callback));
738                 break;
739             }
740             if (!CommonFunc::ParseInt(env, args[i], asyncCallbackInfo->userId)) {
741                 APP_LOGW("Parse userId failed, set this parameter to the caller userId");
742             }
743         } else if (i == ARGS_POS_TWO) {
744             if (valueType == napi_function) {
745                 NAPI_CALL(env, napi_create_reference(env, args[i], NAPI_RETURN_ONE, &asyncCallbackInfo->callback));
746             }
747             break;
748         } else {
749             APP_LOGE("param check error");
750             std::string errMsg = PARAM_TYPE_CHECK_ERROR_WITH_POS + std::to_string(i + 1);
751             BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, errMsg);
752             return nullptr;
753         }
754     }
755     auto promise = CommonFunc::AsyncCallNativeMethod<DefaultAppCallbackInfo>(env,
756         asyncCallbackInfo, RESET_DEFAULT_APPLICATION, ResetDefaultApplicationExec, ResetDefaultApplicationComplete);
757     callbackPtr.release();
758     APP_LOGD("call ResetDefaultApplication done");
759     return promise;
760 }
761 
ParamsProcessResetDefaultApplicationSync(napi_env env,napi_callback_info info,std::string & type,int32_t & userId)762 ErrCode ParamsProcessResetDefaultApplicationSync(napi_env env, napi_callback_info info,
763     std::string& type, int32_t& userId)
764 {
765     NapiArg args(env, info);
766     if (!args.Init(ARGS_SIZE_ONE, ARGS_SIZE_TWO)) {
767         APP_LOGE("param count invalid");
768         BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
769         return ERROR_PARAM_CHECK_ERROR;
770     }
771     for (size_t i = 0; i < args.GetArgc(); ++i) {
772         if (i == ARGS_POS_ZERO) {
773             if (!ParseType(env, args[i], type)) {
774                 APP_LOGE("type %{public}s invalid", type.c_str());
775                 BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, TYPE_CHECK, TYPE_STRING);
776                 return ERROR_PARAM_CHECK_ERROR;
777             }
778         } else if (i == ARGS_POS_ONE) {
779             if (!CommonFunc::ParseInt(env, args[i], userId)) {
780                 APP_LOGE("parseInt failed");
781             }
782         } else {
783             APP_LOGE("param check error");
784             std::string errMsg = PARAM_TYPE_CHECK_ERROR_WITH_POS + std::to_string(i + 1);
785             BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, errMsg);
786             return ERROR_PARAM_CHECK_ERROR;
787         }
788     }
789     return ERR_OK;
790 }
791 
ResetDefaultApplicationSync(napi_env env,napi_callback_info info)792 napi_value ResetDefaultApplicationSync(napi_env env, napi_callback_info info)
793 {
794     APP_LOGD("begin to ResetDefaultApplicationSync");
795     napi_value nRet;
796     napi_get_undefined(env, &nRet);
797     std::string type;
798     int32_t userId = IPCSkeleton::GetCallingUid() / Constants::BASE_USER_RANGE;
799     if (ParamsProcessResetDefaultApplicationSync(env, info, type, userId) != ERR_OK) {
800         return nRet;
801     }
802 
803     auto defaultAppProxy = CommonFunc::GetDefaultAppProxy();
804     if (defaultAppProxy == nullptr) {
805         napi_value error = BusinessError::CreateCommonError(env, ERROR_BUNDLE_SERVICE_EXCEPTION,
806             RESET_DEFAULT_APPLICATION_SYNC);
807         napi_throw(env, error);
808         return nRet;
809     }
810 
811     ErrCode ret = defaultAppProxy->ResetDefaultApplication(userId, type);
812     ret = CommonFunc::ConvertErrCode(ret);
813     if (ret != ERR_OK) {
814         APP_LOGE("ResetDefaultApplicationSync failed: %{public}d", ret);
815         napi_value businessError = BusinessError::CreateCommonError(
816             env, ret, RESET_DEFAULT_APPLICATION_SYNC, Constants::PERMISSION_SET_DEFAULT_APPLICATION);
817         napi_throw(env, businessError);
818         return nRet;
819     }
820 
821     APP_LOGD("call ResetDefaultApplicationSync done");
822     return nRet;
823 }
824 }
825 }
826