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