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