1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "bundle_resource.h"
17
18 #include "app_log_wrapper.h"
19 #include "bundle_errors.h"
20 #include "bundle_mgr_interface.h"
21 #include "bundle_mgr_proxy.h"
22 #include "business_error.h"
23 #include "common_func.h"
24 #include "napi_arg.h"
25 #include "napi_constants.h"
26 #include "napi/native_api.h"
27 #include "napi/native_common.h"
28 #include "napi/native_node_api.h"
29
30 namespace OHOS {
31 namespace AppExecFwk {
32 namespace {
33 constexpr const char* BUNDLE_NAME = "bundleName";
34 constexpr const char* MODULE_NAME = "moduleName";
35 constexpr const char* ABILITY_NAME = "abilityName";
36 constexpr const char* LABEL = "label";
37 constexpr const char* ICON = "icon";
38 constexpr const char* PERMISSION_GET_BUNDLE_RESOURCES = "ohos.permission.GET_BUNDLE_RESOURCES";
39 constexpr const char* PERMISSION_GET_ALL_BUNDLE_RESOURCES =
40 "ohos.permission.GET_INSTALLED_BUNDLE_LIST and ohos.permission.GET_BUNDLE_RESOURCES";
41 constexpr const char* GET_BUNDLE_RESOURCE_INFO = "GetBundleResourceInfo";
42 constexpr const char* GET_LAUNCHER_ABILITY_RESOURCE_INFO = "GetLauncherAbilityResourceInfo";
43 constexpr const char* GET_ALL_BUNDLE_RESOURCE_INFO = "GetAllBundleResourceInfo";
44 constexpr const char* GET_ALL_LAUNCHER_ABILITY_RESOURCE_INFO = "GetAllLauncherAbilityResourceInfo";
45 constexpr const char* RESOURCE_FLAGS = "resourceFlags";
46 constexpr const char* GET_RESOURCE_INFO_ALL = "GET_RESOURCE_INFO_ALL";
47 constexpr const char* GET_RESOURCE_INFO_WITH_LABEL = "GET_RESOURCE_INFO_WITH_LABEL";
48 constexpr const char* GET_RESOURCE_INFO_WITH_ICON = "GET_RESOURCE_INFO_WITH_ICON";
49 constexpr const char* GET_RESOURCE_INFO_WITH_SORTED_BY_LABEL = "GET_RESOURCE_INFO_WITH_SORTED_BY_LABEL";
50
ConvertBundleResourceInfo(napi_env env,const BundleResourceInfo & bundleResourceInfo,napi_value objBundleResourceInfo)51 static void ConvertBundleResourceInfo(
52 napi_env env,
53 const BundleResourceInfo &bundleResourceInfo,
54 napi_value objBundleResourceInfo)
55 {
56 APP_LOGD("start");
57 napi_value nBundleName;
58 NAPI_CALL_RETURN_VOID(
59 env, napi_create_string_utf8(env, bundleResourceInfo.bundleName.c_str(), NAPI_AUTO_LENGTH, &nBundleName));
60 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, objBundleResourceInfo, BUNDLE_NAME, nBundleName));
61
62 napi_value nLabel;
63 NAPI_CALL_RETURN_VOID(
64 env, napi_create_string_utf8(env, bundleResourceInfo.label.c_str(),
65 NAPI_AUTO_LENGTH, &nLabel));
66 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, objBundleResourceInfo, LABEL, nLabel));
67
68 napi_value nIcon;
69 NAPI_CALL_RETURN_VOID(
70 env, napi_create_string_utf8(env, bundleResourceInfo.icon.c_str(),
71 NAPI_AUTO_LENGTH, &nIcon));
72 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, objBundleResourceInfo, ICON, nIcon));
73 APP_LOGD("end");
74 }
75
ConvertBundleResourceInfos(napi_env env,const std::vector<BundleResourceInfo> & bundleResourceInfos,napi_value objBundleResourceInfos)76 static void ConvertBundleResourceInfos(
77 napi_env env,
78 const std::vector<BundleResourceInfo> &bundleResourceInfos,
79 napi_value objBundleResourceInfos)
80 {
81 for (size_t index = 0; index < bundleResourceInfos.size(); ++index) {
82 napi_value objBundleResourceInfo = nullptr;
83 napi_create_object(env, &objBundleResourceInfo);
84 ConvertBundleResourceInfo(env, bundleResourceInfos[index], objBundleResourceInfo);
85 napi_set_element(env, objBundleResourceInfos, index, objBundleResourceInfo);
86 }
87 }
88
ConvertLauncherAbilityResourceInfo(napi_env env,const LauncherAbilityResourceInfo & launcherAbilityResourceInfo,napi_value objLauncherAbilityResourceInfo)89 static void ConvertLauncherAbilityResourceInfo(
90 napi_env env,
91 const LauncherAbilityResourceInfo &launcherAbilityResourceInfo,
92 napi_value objLauncherAbilityResourceInfo)
93 {
94 APP_LOGD("start");
95 napi_value nBundleName;
96 NAPI_CALL_RETURN_VOID(
97 env, napi_create_string_utf8(env, launcherAbilityResourceInfo.bundleName.c_str(),
98 NAPI_AUTO_LENGTH, &nBundleName));
99 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, objLauncherAbilityResourceInfo,
100 BUNDLE_NAME, nBundleName));
101
102 napi_value nModuleName;
103 NAPI_CALL_RETURN_VOID(
104 env, napi_create_string_utf8(env, launcherAbilityResourceInfo.moduleName.c_str(),
105 NAPI_AUTO_LENGTH, &nModuleName));
106 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, objLauncherAbilityResourceInfo,
107 MODULE_NAME, nModuleName));
108
109 napi_value nAbilityName;
110 NAPI_CALL_RETURN_VOID(
111 env, napi_create_string_utf8(env, launcherAbilityResourceInfo.abilityName.c_str(),
112 NAPI_AUTO_LENGTH, &nAbilityName));
113 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, objLauncherAbilityResourceInfo,
114 ABILITY_NAME, nAbilityName));
115
116 napi_value nLabel;
117 NAPI_CALL_RETURN_VOID(
118 env, napi_create_string_utf8(env, launcherAbilityResourceInfo.label.c_str(),
119 NAPI_AUTO_LENGTH, &nLabel));
120 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, objLauncherAbilityResourceInfo,
121 LABEL, nLabel));
122
123 napi_value nIcon;
124 NAPI_CALL_RETURN_VOID(
125 env, napi_create_string_utf8(env, launcherAbilityResourceInfo.icon.c_str(),
126 NAPI_AUTO_LENGTH, &nIcon));
127 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, objLauncherAbilityResourceInfo,
128 ICON, nIcon));
129 APP_LOGD("end");
130 }
131
ConvertLauncherAbilityResourceInfos(napi_env env,const std::vector<LauncherAbilityResourceInfo> & launcherAbilityResourceInfos,napi_value objLauncherAbilityResourceInfos)132 static void ConvertLauncherAbilityResourceInfos(
133 napi_env env,
134 const std::vector<LauncherAbilityResourceInfo> &launcherAbilityResourceInfos,
135 napi_value objLauncherAbilityResourceInfos)
136 {
137 for (size_t index = 0; index < launcherAbilityResourceInfos.size(); ++index) {
138 napi_value objLauncherAbilityResourceInfo = nullptr;
139 napi_create_object(env, &objLauncherAbilityResourceInfo);
140 ConvertLauncherAbilityResourceInfo(env, launcherAbilityResourceInfos[index], objLauncherAbilityResourceInfo);
141 napi_set_element(env, objLauncherAbilityResourceInfos, index, objLauncherAbilityResourceInfo);
142 }
143 }
144 }
145
InnerGetBundleResourceInfo(const std::string & bundleName,uint32_t flags,BundleResourceInfo & resourceInfo)146 static ErrCode InnerGetBundleResourceInfo(
147 const std::string &bundleName, uint32_t flags, BundleResourceInfo &resourceInfo)
148 {
149 APP_LOGD("start");
150 auto iBundleMgr = CommonFunc::GetBundleMgr();
151 if (iBundleMgr == nullptr) {
152 APP_LOGE("iBundleMgr is null");
153 return ERROR_BUNDLE_SERVICE_EXCEPTION;
154 }
155 auto bundleResourceProxy = iBundleMgr->GetBundleResourceProxy();
156 if (bundleResourceProxy == nullptr) {
157 APP_LOGE("bundleResourceProxy is null");
158 return ERROR_BUNDLE_SERVICE_EXCEPTION;
159 }
160 ErrCode ret = bundleResourceProxy->GetBundleResourceInfo(bundleName, flags, resourceInfo);
161 if (ret != ERR_OK) {
162 APP_LOGE("failed, bundleName is %{public}s, errCode: %{public}d", bundleName.c_str(), ret);
163 }
164 return CommonFunc::ConvertErrCode(ret);
165 }
166
GetBundleResourceInfo(napi_env env,napi_callback_info info)167 napi_value GetBundleResourceInfo(napi_env env, napi_callback_info info)
168 {
169 APP_LOGD("NAPI start");
170 NapiArg args(env, info);
171 if (!args.Init(ARGS_SIZE_ONE, ARGS_SIZE_TWO)) {
172 APP_LOGE("param count invalid");
173 BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
174 return nullptr;
175 }
176 std::string bundleName;
177 if (!CommonFunc::ParseString(env, args[ARGS_POS_ZERO], bundleName) || bundleName.empty()) {
178 APP_LOGE("parse bundleName failed, bundleName is %{public}s", bundleName.c_str());
179 BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, BUNDLE_NAME, TYPE_STRING);
180 return nullptr;
181 }
182 int32_t flags = 0;
183 if (args.GetMaxArgc() >= ARGS_SIZE_TWO) {
184 if (!CommonFunc::ParseInt(env, args[ARGS_POS_ONE], flags)) {
185 APP_LOGW("parse flags failed");
186 }
187 }
188 if (flags <= 0) {
189 flags = static_cast<int32_t>(ResourceFlag::GET_RESOURCE_INFO_ALL);
190 }
191 BundleResourceInfo resourceInfo;
192 auto ret = InnerGetBundleResourceInfo(bundleName, flags, resourceInfo);
193 if (ret != ERR_OK) {
194 napi_value businessError = BusinessError::CreateCommonError(
195 env, ret, GET_BUNDLE_RESOURCE_INFO, PERMISSION_GET_BUNDLE_RESOURCES);
196 napi_throw(env, businessError);
197 return nullptr;
198 }
199 napi_value nBundleResourceInfo = nullptr;
200 NAPI_CALL(env, napi_create_object(env, &nBundleResourceInfo));
201 ConvertBundleResourceInfo(env, resourceInfo, nBundleResourceInfo);
202 APP_LOGD("NAPI end");
203 return nBundleResourceInfo;
204 }
205
InnerGetLauncherAbilityResourceInfo(const std::string & bundleName,uint32_t flags,std::vector<LauncherAbilityResourceInfo> & launcherAbilityResourceInfo)206 static ErrCode InnerGetLauncherAbilityResourceInfo(
207 const std::string &bundleName, uint32_t flags,
208 std::vector<LauncherAbilityResourceInfo> &launcherAbilityResourceInfo)
209 {
210 APP_LOGD("start");
211 auto iBundleMgr = CommonFunc::GetBundleMgr();
212 if (iBundleMgr == nullptr) {
213 APP_LOGE("iBundleMgr is null");
214 return ERROR_BUNDLE_SERVICE_EXCEPTION;
215 }
216 auto bundleResourceProxy = iBundleMgr->GetBundleResourceProxy();
217 if (bundleResourceProxy == nullptr) {
218 APP_LOGE("bundleResourceProxy is null");
219 return ERROR_BUNDLE_SERVICE_EXCEPTION;
220 }
221 ErrCode ret = bundleResourceProxy->GetLauncherAbilityResourceInfo(bundleName,
222 flags, launcherAbilityResourceInfo);
223 if (ret != ERR_OK) {
224 APP_LOGE("failed, bundleName is %{public}s, errCode: %{public}d", bundleName.c_str(), ret);
225 }
226 return CommonFunc::ConvertErrCode(ret);
227 }
228
GetLauncherAbilityResourceInfo(napi_env env,napi_callback_info info)229 napi_value GetLauncherAbilityResourceInfo(napi_env env, napi_callback_info info)
230 {
231 APP_LOGD("NAPI start");
232 NapiArg args(env, info);
233 if (!args.Init(ARGS_SIZE_ONE, ARGS_SIZE_TWO)) {
234 APP_LOGE("param count invalid");
235 BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
236 return nullptr;
237 }
238 std::string bundleName;
239 if (!CommonFunc::ParseString(env, args[ARGS_POS_ZERO], bundleName) || bundleName.empty()) {
240 APP_LOGE("parse bundleName failed, bundleName is %{public}s", bundleName.c_str());
241 BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, BUNDLE_NAME, TYPE_STRING);
242 return nullptr;
243 }
244 int32_t flags = 0;
245 if (args.GetMaxArgc() >= ARGS_SIZE_TWO) {
246 if (!CommonFunc::ParseInt(env, args[ARGS_POS_ONE], flags)) {
247 APP_LOGW("parse flags failed");
248 }
249 }
250 if (flags <= 0) {
251 flags = static_cast<int32_t>(ResourceFlag::GET_RESOURCE_INFO_ALL);
252 }
253
254 std::vector<LauncherAbilityResourceInfo> launcherAbilityResourceInfos;
255 auto ret = InnerGetLauncherAbilityResourceInfo(bundleName, flags, launcherAbilityResourceInfos);
256 if (ret != ERR_OK) {
257 napi_value businessError = BusinessError::CreateCommonError(
258 env, ret, GET_LAUNCHER_ABILITY_RESOURCE_INFO, PERMISSION_GET_BUNDLE_RESOURCES);
259 napi_throw(env, businessError);
260 return nullptr;
261 }
262 napi_value nLauncherAbilityResourceInfos = nullptr;
263 NAPI_CALL(env, napi_create_array(env, &nLauncherAbilityResourceInfos));
264 ConvertLauncherAbilityResourceInfos(env, launcherAbilityResourceInfos, nLauncherAbilityResourceInfos);
265 APP_LOGD("NAPI end");
266 return nLauncherAbilityResourceInfos;
267 }
268
InnerGetAllBundleResourceInfo(uint32_t flags,std::vector<BundleResourceInfo> & bundleResourceInfos)269 static ErrCode InnerGetAllBundleResourceInfo(uint32_t flags, std::vector<BundleResourceInfo> &bundleResourceInfos)
270 {
271 auto iBundleMgr = CommonFunc::GetBundleMgr();
272 if (iBundleMgr == nullptr) {
273 APP_LOGE("iBundleMgr is null");
274 return ERROR_BUNDLE_SERVICE_EXCEPTION;
275 }
276 auto bundleResourceProxy = iBundleMgr->GetBundleResourceProxy();
277 if (bundleResourceProxy == nullptr) {
278 APP_LOGE("bundleResourceProxy is null");
279 return ERROR_BUNDLE_SERVICE_EXCEPTION;
280 }
281 ErrCode ret = bundleResourceProxy->GetAllBundleResourceInfo(flags, bundleResourceInfos);
282 if (ret != ERR_OK) {
283 APP_LOGE("failed, errCode: %{public}d", ret);
284 }
285 return CommonFunc::ConvertErrCode(ret);
286 }
287
GetAllBundleResourceInfoExec(napi_env env,void * data)288 void GetAllBundleResourceInfoExec(napi_env env, void *data)
289 {
290 AllBundleResourceInfoCallback *asyncCallbackInfo = reinterpret_cast<AllBundleResourceInfoCallback *>(data);
291 if (asyncCallbackInfo == nullptr) {
292 APP_LOGE("asyncCallbackInfo is null");
293 return;
294 }
295 asyncCallbackInfo->err = InnerGetAllBundleResourceInfo(asyncCallbackInfo->flags,
296 asyncCallbackInfo->bundleResourceInfos);
297 }
298
GetAllBundleResourceInfoComplete(napi_env env,napi_status status,void * data)299 void GetAllBundleResourceInfoComplete(napi_env env, napi_status status, void *data)
300 {
301 AllBundleResourceInfoCallback *asyncCallbackInfo = reinterpret_cast<AllBundleResourceInfoCallback *>(data);
302 if (asyncCallbackInfo == nullptr) {
303 APP_LOGE("asyncCallbackInfo is null");
304 return;
305 }
306 std::unique_ptr<AllBundleResourceInfoCallback> callbackPtr {asyncCallbackInfo};
307 napi_value result[ARGS_SIZE_TWO] = {0};
308 if (asyncCallbackInfo->err == NO_ERROR) {
309 NAPI_CALL_RETURN_VOID(env, napi_get_null(env, &result[0]));
310 NAPI_CALL_RETURN_VOID(env, napi_create_array(env, &result[1]));
311 ConvertBundleResourceInfos(env, asyncCallbackInfo->bundleResourceInfos, result[1]);
312 } else {
313 result[0] = BusinessError::CreateCommonError(env, asyncCallbackInfo->err,
314 GET_ALL_BUNDLE_RESOURCE_INFO, PERMISSION_GET_ALL_BUNDLE_RESOURCES);
315 }
316 CommonFunc::NapiReturnDeferred<AllBundleResourceInfoCallback>(env, asyncCallbackInfo, result, ARGS_SIZE_TWO);
317 }
318
GetAllBundleResourceInfo(napi_env env,napi_callback_info info)319 napi_value GetAllBundleResourceInfo(napi_env env, napi_callback_info info)
320 {
321 APP_LOGD("NAPI start");
322 NapiArg args(env, info);
323 if (!args.Init(ARGS_SIZE_ONE, ARGS_SIZE_TWO)) {
324 APP_LOGE("param count invalid");
325 BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
326 return nullptr;
327 }
328 AllBundleResourceInfoCallback *asyncCallbackInfo = new (std::nothrow) AllBundleResourceInfoCallback(env);
329 if (asyncCallbackInfo == nullptr) {
330 APP_LOGE("asyncCallbackInfo is null");
331 return nullptr;
332 }
333 std::unique_ptr<AllBundleResourceInfoCallback> callbackPtr {asyncCallbackInfo};
334 int32_t flags = 0;
335 if (!CommonFunc::ParseInt(env, args[ARGS_POS_ZERO], flags)) {
336 APP_LOGE("Flags %{public}d invalid!", flags);
337 BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, RESOURCE_FLAGS, TYPE_NUMBER);
338 return nullptr;
339 }
340 if (flags <= 0) {
341 flags = static_cast<int32_t>(ResourceFlag::GET_RESOURCE_INFO_ALL);
342 }
343 asyncCallbackInfo->flags = static_cast<uint32_t>(flags);
344 if (args.GetMaxArgc() >= ARGS_SIZE_TWO) {
345 napi_valuetype valueType = napi_undefined;
346 napi_typeof(env, args[ARGS_POS_ONE], &valueType);
347 if (valueType == napi_function) {
348 NAPI_CALL(env, napi_create_reference(env, args[ARGS_POS_ONE],
349 NAPI_RETURN_ONE, &asyncCallbackInfo->callback));
350 }
351 }
352 auto promise = CommonFunc::AsyncCallNativeMethod<AllBundleResourceInfoCallback>(
353 env, asyncCallbackInfo, GET_ALL_BUNDLE_RESOURCE_INFO, GetAllBundleResourceInfoExec,
354 GetAllBundleResourceInfoComplete);
355 callbackPtr.release();
356 APP_LOGD("NAPI end");
357 return promise;
358 }
359
InnerGetAllLauncherAbilityResourceInfo(uint32_t flags,std::vector<LauncherAbilityResourceInfo> & launcherAbilityResourceInfos)360 static ErrCode InnerGetAllLauncherAbilityResourceInfo(uint32_t flags,
361 std::vector<LauncherAbilityResourceInfo> &launcherAbilityResourceInfos)
362 {
363 auto iBundleMgr = CommonFunc::GetBundleMgr();
364 if (iBundleMgr == nullptr) {
365 APP_LOGE("iBundleMgr is null");
366 return ERROR_BUNDLE_SERVICE_EXCEPTION;
367 }
368 auto bundleResourceProxy = iBundleMgr->GetBundleResourceProxy();
369 if (bundleResourceProxy == nullptr) {
370 APP_LOGE("bundleResourceProxy is null");
371 return ERROR_BUNDLE_SERVICE_EXCEPTION;
372 }
373 ErrCode ret = bundleResourceProxy->GetAllLauncherAbilityResourceInfo(flags, launcherAbilityResourceInfos);
374 if (ret != ERR_OK) {
375 APP_LOGE("failed, errCode: %{public}d", ret);
376 }
377 return CommonFunc::ConvertErrCode(ret);
378 }
379
GetAllLauncherAbilityResourceInfoExec(napi_env env,void * data)380 void GetAllLauncherAbilityResourceInfoExec(napi_env env, void *data)
381 {
382 AllLauncherAbilityResourceInfoCallback *asyncCallbackInfo =
383 reinterpret_cast<AllLauncherAbilityResourceInfoCallback *>(data);
384 if (asyncCallbackInfo == nullptr) {
385 APP_LOGE("asyncCallbackInfo is null");
386 return;
387 }
388 asyncCallbackInfo->err = InnerGetAllLauncherAbilityResourceInfo(
389 asyncCallbackInfo->flags, asyncCallbackInfo->launcherAbilityResourceInfos);
390 }
391
GetAllLauncherAbilityResourceInfoComplete(napi_env env,napi_status status,void * data)392 void GetAllLauncherAbilityResourceInfoComplete(napi_env env, napi_status status, void *data)
393 {
394 AllLauncherAbilityResourceInfoCallback *asyncCallbackInfo =
395 reinterpret_cast<AllLauncherAbilityResourceInfoCallback *>(data);
396 if (asyncCallbackInfo == nullptr) {
397 APP_LOGE("asyncCallbackInfo is null");
398 return;
399 }
400 std::unique_ptr<AllLauncherAbilityResourceInfoCallback> callbackPtr {asyncCallbackInfo};
401 napi_value result[ARGS_SIZE_TWO] = {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_array(env, &result[1]));
405 ConvertLauncherAbilityResourceInfos(env, asyncCallbackInfo->launcherAbilityResourceInfos, result[1]);
406 } else {
407 result[0] = BusinessError::CreateCommonError(env, asyncCallbackInfo->err,
408 GET_ALL_LAUNCHER_ABILITY_RESOURCE_INFO, PERMISSION_GET_ALL_BUNDLE_RESOURCES);
409 }
410 CommonFunc::NapiReturnDeferred<AllLauncherAbilityResourceInfoCallback>(env, asyncCallbackInfo,
411 result, ARGS_SIZE_TWO);
412 }
413
GetAllLauncherAbilityResourceInfo(napi_env env,napi_callback_info info)414 napi_value GetAllLauncherAbilityResourceInfo(napi_env env, napi_callback_info info)
415 {
416 APP_LOGD("NAPI start");
417 NapiArg args(env, info);
418 if (!args.Init(ARGS_SIZE_ONE, ARGS_SIZE_TWO)) {
419 APP_LOGE("param count invalid");
420 BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
421 return nullptr;
422 }
423 AllLauncherAbilityResourceInfoCallback *asyncCallbackInfo =
424 new (std::nothrow) AllLauncherAbilityResourceInfoCallback(env);
425 if (asyncCallbackInfo == nullptr) {
426 APP_LOGE("asyncCallbackInfo is null");
427 return nullptr;
428 }
429 std::unique_ptr<AllLauncherAbilityResourceInfoCallback> callbackPtr {asyncCallbackInfo};
430 int32_t flags = 0;
431 if (!CommonFunc::ParseInt(env, args[ARGS_POS_ZERO], flags)) {
432 APP_LOGE("Flags %{public}d invalid!", flags);
433 BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, RESOURCE_FLAGS, TYPE_NUMBER);
434 return nullptr;
435 }
436 if (flags <= 0) {
437 flags = static_cast<int32_t>(ResourceFlag::GET_RESOURCE_INFO_ALL);
438 }
439 asyncCallbackInfo->flags = static_cast<uint32_t>(flags);
440 if (args.GetMaxArgc() >= ARGS_SIZE_TWO) {
441 napi_valuetype valueType = napi_undefined;
442 napi_typeof(env, args[ARGS_POS_ONE], &valueType);
443 if (valueType == napi_function) {
444 NAPI_CALL(env, napi_create_reference(env, args[ARGS_POS_ONE],
445 NAPI_RETURN_ONE, &asyncCallbackInfo->callback));
446 }
447 }
448 auto promise = CommonFunc::AsyncCallNativeMethod<AllLauncherAbilityResourceInfoCallback>(
449 env, asyncCallbackInfo, GET_ALL_LAUNCHER_ABILITY_RESOURCE_INFO, GetAllLauncherAbilityResourceInfoExec,
450 GetAllLauncherAbilityResourceInfoComplete);
451 callbackPtr.release();
452 APP_LOGD("NAPI end");
453 return promise;
454 }
455
CreateBundleResourceFlagObject(napi_env env,napi_value value)456 void CreateBundleResourceFlagObject(napi_env env, napi_value value)
457 {
458 napi_value nGetAll;
459 NAPI_CALL_RETURN_VOID(env, napi_create_int32(
460 env, static_cast<int32_t>(ResourceFlag::GET_RESOURCE_INFO_ALL), &nGetAll));
461 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, GET_RESOURCE_INFO_ALL, nGetAll));
462
463 napi_value nGetLabel;
464 NAPI_CALL_RETURN_VOID(env, napi_create_int32(
465 env, static_cast<int32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_LABEL), &nGetLabel));
466 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, GET_RESOURCE_INFO_WITH_LABEL, nGetLabel));
467
468 napi_value nGetIcon;
469 NAPI_CALL_RETURN_VOID(env, napi_create_int32(
470 env, static_cast<int32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_ICON), &nGetIcon));
471 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, GET_RESOURCE_INFO_WITH_ICON, nGetIcon));
472
473 napi_value nGetSortByLabel;
474 NAPI_CALL_RETURN_VOID(env, napi_create_int32(
475 env, static_cast<int32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_SORTED_BY_LABEL), &nGetSortByLabel));
476 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value,
477 GET_RESOURCE_INFO_WITH_SORTED_BY_LABEL, nGetSortByLabel));
478 }
479 } // AppExecFwk
480 } // OHOS
481