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