• 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 #include <string>
16 
17 #include "app_log_wrapper.h"
18 #include "bundle_errors.h"
19 #include "bundle_mgr_client.h"
20 #include "bundle_mgr_interface.h"
21 #include "bundle_mgr_proxy.h"
22 #include "business_error.h"
23 #include "bundle_constants.h"
24 #include "common_func.h"
25 #include "hap_module_info.h"
26 #include "ipc_skeleton.h"
27 #include "napi_arg.h"
28 #include "napi_constants.h"
29 #include "bundle_manager_sync.h"
30 
31 namespace OHOS {
32 namespace AppExecFwk {
33 constexpr const char* MODULE_NAME = "moduleName";
34 constexpr const char* ABILITY_NAME = "abilityName";
35 constexpr const char* BUNDLE_NAME = "bundleName";
36 constexpr const char* ABILITY_INFO = "abilityInfo";
37 constexpr const char* IS_ENABLE = "isEnable";
38 constexpr const char* BUNDLE_FLAGS = "bundleFlags";
39 constexpr const char* HAP_FILE_PATH = "hapFilePath";
40 constexpr const char* UID = "uid";
41 constexpr const char* EXTENSIONABILITY_TYPE = "extensionAbilityType";
42 constexpr const char* FLAGS = "flags";
43 constexpr const char* ERR_MSG_BUNDLE_SERVICE_EXCEPTION = "Bundle manager service is excepted.";
44 const std::string SET_APPLICATION_ENABLED_SYNC = "SetApplicationEnabledSync";
45 const std::string SET_ABILITY_ENABLED_SYNC = "SetAbilityEnabledSync";
46 const std::string IS_APPLICATION_ENABLED_SYNC = "IsApplicationEnabledSync";
47 const std::string IS_ABILITY_ENABLED_SYNC = "IsAbilityEnabledSync";
48 const std::string GET_ABILITY_LABEL_SYNC = "GetAbilityLabelSync";
49 const std::string GET_LAUNCH_WANT_FOR_BUNDLE_SYNC = "GetLaunchWantForBundleSync";
50 const std::string GET_BUNDLE_ARCHIVE_INFO_SYNC = "GetBundleArchiveInfoSync";
51 const std::string GET_BUNDLE_NAME_BY_UID_SYNC = "GetBundleNameByUidSync";
52 const std::string GET_PROFILE_BY_EXTENSION_ABILITY_SYNC = "GetProfileByExtensionAbilitySync";
53 const std::string GET_PROFILE_BY_ABILITY_SYNC = "GetProfileByAbilitySync";
54 const std::string QUERY_EXTENSION_INFOS_SYNC = "QueryExtensionInfosSync";
55 const std::string GET_PERMISSION_DEF_SYNC = "GetPermissionDefSync";
56 const std::string GET_APP_PROVISION_INFO_SYNC = "GetAppProvisionInfoSync";
57 const std::string BUNDLE_PERMISSIONS = "ohos.permission.GET_BUNDLE_INFO or ohos.permission.GET_BUNDLE_INFO_PRIVILEGED";
58 const std::string PERMISSION_NAME = "permissionName";
59 const std::string INVALID_WANT_ERROR =
60     "implicit query condition, at least one query param(action entities uri type) non-empty.";
61 const std::string PARAM_TYPE_CHECK_ERROR = "param type check error";
62 
SetApplicationEnabledSync(napi_env env,napi_callback_info info)63 napi_value SetApplicationEnabledSync(napi_env env, napi_callback_info info)
64 {
65     APP_LOGD("NAPI SetApplicationEnabledSync called");
66     NapiArg args(env, info);
67     if (!args.Init(ARGS_SIZE_TWO, ARGS_SIZE_TWO)) {
68         APP_LOGE("param count invalid");
69         BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
70         return nullptr;
71     }
72     std::string bundleName;
73     bool isEnable = false;
74     if (!CommonFunc::ParseString(env, args[ARGS_POS_ZERO], bundleName)) {
75         APP_LOGE("parse bundleName failed");
76         BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, BUNDLE_NAME, TYPE_STRING);
77         return nullptr;
78     }
79     if (!CommonFunc::ParseBool(env, args[ARGS_POS_ONE], isEnable)) {
80         APP_LOGE("parse isEnable failed");
81         BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, IS_ENABLE, TYPE_BOOLEAN);
82         return nullptr;
83     }
84     auto iBundleMgr = CommonFunc::GetBundleMgr();
85     if (iBundleMgr == nullptr) {
86         APP_LOGE("can not get iBundleMgr");
87         BusinessError::ThrowError(env, ERROR_BUNDLE_SERVICE_EXCEPTION, ERR_MSG_BUNDLE_SERVICE_EXCEPTION);
88         return nullptr;
89     }
90     ErrCode ret = CommonFunc::ConvertErrCode(iBundleMgr->SetApplicationEnabled(bundleName, isEnable));
91     if (ret != NO_ERROR) {
92         APP_LOGE("SetApplicationEnabledSync failed");
93         napi_value businessError = BusinessError::CreateCommonError(
94             env, ret, SET_APPLICATION_ENABLED_SYNC, Constants::PERMISSION_CHANGE_ABILITY_ENABLED_STATE);
95         napi_throw(env, businessError);
96         return nullptr;
97     }
98     napi_value nRet = nullptr;
99     NAPI_CALL(env, napi_get_undefined(env, &nRet));
100     APP_LOGD("call SetApplicationEnabledSync done");
101     return nRet;
102 }
103 
SetAbilityEnabledSync(napi_env env,napi_callback_info info)104 napi_value SetAbilityEnabledSync(napi_env env, napi_callback_info info)
105 {
106     APP_LOGD("NAPI SetAbilityEnabledSync called");
107     NapiArg args(env, info);
108     if (!args.Init(ARGS_SIZE_TWO, ARGS_SIZE_TWO)) {
109         APP_LOGE("param count invalid");
110         BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
111         return nullptr;
112     }
113     AbilityInfo abilityInfo;
114     bool isEnable = false;
115     if (!CommonFunc::ParseAbilityInfo(env, args[ARGS_POS_ZERO], abilityInfo)) {
116         APP_LOGE("parse abilityInfo failed");
117         BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, ABILITY_INFO, TYPE_OBJECT);
118         return nullptr;
119     }
120     if (!CommonFunc::ParseBool(env, args[ARGS_POS_ONE], isEnable)) {
121         APP_LOGE("parse isEnable failed");
122         BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, IS_ENABLE, TYPE_BOOLEAN);
123         return nullptr;
124     }
125     auto iBundleMgr = CommonFunc::GetBundleMgr();
126     if (iBundleMgr == nullptr) {
127         APP_LOGE("can not get iBundleMgr");
128         BusinessError::ThrowError(env, ERROR_BUNDLE_SERVICE_EXCEPTION, ERR_MSG_BUNDLE_SERVICE_EXCEPTION);
129         return nullptr;
130     }
131     ErrCode ret = CommonFunc::ConvertErrCode(iBundleMgr->SetAbilityEnabled(abilityInfo, isEnable));
132     if (ret != NO_ERROR) {
133         APP_LOGE("SetAbilityEnabledSync failed");
134         napi_value businessError = BusinessError::CreateCommonError(
135             env, ret, SET_ABILITY_ENABLED_SYNC, Constants::PERMISSION_CHANGE_ABILITY_ENABLED_STATE);
136         napi_throw(env, businessError);
137         return nullptr;
138     }
139     napi_value nRet = nullptr;
140     NAPI_CALL(env, napi_get_undefined(env, &nRet));
141     APP_LOGD("call SetAbilityEnabledSync done");
142     return nRet;
143 }
144 
IsApplicationEnabledSync(napi_env env,napi_callback_info info)145 napi_value IsApplicationEnabledSync(napi_env env, napi_callback_info info)
146 {
147     APP_LOGD("NAPI IsApplicationEnabledSync called");
148     NapiArg args(env, info);
149     if (!args.Init(ARGS_SIZE_ONE, ARGS_SIZE_ONE)) {
150         APP_LOGE("param count invalid");
151         BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
152         return nullptr;
153     }
154     std::string bundleName;
155     if (!CommonFunc::ParseString(env, args[ARGS_POS_ZERO], bundleName)) {
156         APP_LOGE("parse bundleName failed");
157         BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, BUNDLE_NAME, TYPE_STRING);
158         return nullptr;
159     }
160     auto iBundleMgr = CommonFunc::GetBundleMgr();
161     if (iBundleMgr == nullptr) {
162         APP_LOGE("can not get iBundleMgr");
163         BusinessError::ThrowError(env, ERROR_BUNDLE_SERVICE_EXCEPTION, ERR_MSG_BUNDLE_SERVICE_EXCEPTION);
164         return nullptr;
165     }
166     bool isEnable = false;
167     ErrCode ret = CommonFunc::ConvertErrCode(iBundleMgr->IsApplicationEnabled(bundleName, isEnable));
168     if (ret != NO_ERROR) {
169         APP_LOGE("IsApplicationEnabledSync failed");
170         napi_value businessError = BusinessError::CreateCommonError(env, ret, IS_APPLICATION_ENABLED_SYNC);
171         napi_throw(env, businessError);
172         return nullptr;
173     }
174     napi_value nIsEnabled = nullptr;
175     NAPI_CALL(env, napi_get_boolean(env, isEnable, &nIsEnabled));
176     APP_LOGD("call IsApplicationEnabledSync done.");
177     return nIsEnabled;
178 }
179 
IsAbilityEnabledSync(napi_env env,napi_callback_info info)180 napi_value IsAbilityEnabledSync(napi_env env, napi_callback_info info)
181 {
182     APP_LOGD("NAPI IsAbilityEnabledSync called");
183     NapiArg args(env, info);
184     if (!args.Init(ARGS_SIZE_ONE, ARGS_SIZE_ONE)) {
185         APP_LOGE("param count invalid");
186         BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
187         return nullptr;
188     }
189     AbilityInfo abilityInfo;
190     if (!CommonFunc::ParseAbilityInfo(env, args[ARGS_POS_ZERO], abilityInfo)) {
191         APP_LOGE("parse abilityInfo failed");
192         BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, ABILITY_INFO, TYPE_OBJECT);
193         return nullptr;
194     }
195     auto iBundleMgr = CommonFunc::GetBundleMgr();
196     if (iBundleMgr == nullptr) {
197         APP_LOGE("can not get iBundleMgr");
198         BusinessError::ThrowError(env, ERROR_BUNDLE_SERVICE_EXCEPTION, ERR_MSG_BUNDLE_SERVICE_EXCEPTION);
199         return nullptr;
200     }
201     bool isEnable = false;
202     ErrCode ret = CommonFunc::ConvertErrCode(iBundleMgr->IsAbilityEnabled(abilityInfo, isEnable));
203     if (ret != NO_ERROR) {
204         APP_LOGE("IsAbilityEnabledSync failed");
205         napi_value businessError = BusinessError::CreateCommonError(env, ret, IS_ABILITY_ENABLED_SYNC);
206         napi_throw(env, businessError);
207         return nullptr;
208     }
209     napi_value nIsEnabled = nullptr;
210     NAPI_CALL(env, napi_get_boolean(env, isEnable, &nIsEnabled));
211     APP_LOGD("call IsAbilityEnabledSync done.");
212     return nIsEnabled;
213 }
214 
ParamsProcessQueryExtensionInfosSync(napi_env env,napi_callback_info info,ExtensionParamInfo & extensionParamInfo)215 ErrCode ParamsProcessQueryExtensionInfosSync(napi_env env, napi_callback_info info,
216     ExtensionParamInfo& extensionParamInfo)
217 {
218     NapiArg args(env, info);
219     if (!args.Init(ARGS_SIZE_THREE, ARGS_SIZE_FOUR)) {
220         APP_LOGE("param count invalid");
221         BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
222         return ERROR_PARAM_CHECK_ERROR;
223     }
224     for (size_t i = 0; i < args.GetMaxArgc(); ++i) {
225         napi_valuetype valueType = napi_undefined;
226         napi_typeof(env, args[i], &valueType);
227         if (i == ARGS_POS_ZERO) {
228             if (!CommonFunc::ParseWantPerformance(env, args[i], extensionParamInfo.want)) {
229                 APP_LOGE("invalid want");
230                 BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, INVALID_WANT_ERROR);
231                 return ERROR_PARAM_CHECK_ERROR;
232             }
233         } else if (i == ARGS_POS_ONE) {
234             if (!CommonFunc::ParseInt(env, args[i], extensionParamInfo.extensionAbilityType)) {
235                 APP_LOGE("invalid extensionAbilityType");
236                 BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR,
237                     EXTENSIONABILITY_TYPE, TYPE_NUMBER);
238                 return ERROR_PARAM_CHECK_ERROR;
239             }
240         } else if (i == ARGS_POS_TWO) {
241             if (!CommonFunc::ParseInt(env, args[i], extensionParamInfo.flags)) {
242                 APP_LOGE("invalid flags");
243                 BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, FLAGS, TYPE_NUMBER);
244                 return ERROR_PARAM_CHECK_ERROR;
245             }
246         } else if (i == ARGS_POS_THREE) {
247             if (!CommonFunc::ParseInt(env, args[i], extensionParamInfo.userId)) {
248                 APP_LOGW("Parse userId failed, set this parameter to the caller userId!");
249             }
250         } else {
251             APP_LOGE("parameter is invalid");
252             BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_TYPE_CHECK_ERROR);
253             return ERROR_PARAM_CHECK_ERROR;
254         }
255     }
256     if (extensionParamInfo.userId == Constants::UNSPECIFIED_USERID) {
257         extensionParamInfo.userId = IPCSkeleton::GetCallingUid() / Constants::BASE_USER_RANGE;
258     }
259     return ERR_OK;
260 }
261 
QueryExtensionInfosSync(napi_env env,napi_callback_info info)262 napi_value QueryExtensionInfosSync(napi_env env, napi_callback_info info)
263 {
264     APP_LOGD("NAPI QueryExtensionInfosSync call");
265     ExtensionParamInfo extensionParamInfo;
266     if (ParamsProcessQueryExtensionInfosSync(env, info, extensionParamInfo) != ERR_OK) {
267         APP_LOGE("paramsProcess is invalid");
268         BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_TYPE_CHECK_ERROR);
269         return nullptr;
270     }
271     std::vector<ExtensionAbilityInfo> extensionInfos;
272     ErrCode ret;
273     auto iBundleMgr = CommonFunc::GetBundleMgr();
274     if (iBundleMgr == nullptr) {
275         APP_LOGE("can not get iBundleMgr");
276         BusinessError::ThrowError(env, ERROR_BUNDLE_SERVICE_EXCEPTION, ERR_MSG_BUNDLE_SERVICE_EXCEPTION);
277         return nullptr;
278     }
279     if (extensionParamInfo.extensionAbilityType == static_cast<int32_t>(ExtensionAbilityType::UNSPECIFIED)) {
280         APP_LOGD("query extensionAbilityInfo sync without type");
281         ret = CommonFunc::ConvertErrCode(
282             iBundleMgr->QueryExtensionAbilityInfosV9(extensionParamInfo.want, extensionParamInfo.flags,
283             extensionParamInfo.userId, extensionInfos));
284     } else {
285         ExtensionAbilityType type = static_cast<ExtensionAbilityType>(extensionParamInfo.extensionAbilityType);
286         APP_LOGD("query extensionAbilityInfo sync with type %{public}d", extensionParamInfo.extensionAbilityType);
287         ret = CommonFunc::ConvertErrCode(iBundleMgr->QueryExtensionAbilityInfosV9(
288             extensionParamInfo.want, type, extensionParamInfo.flags,
289             extensionParamInfo.userId, extensionInfos));
290     }
291     if (ret != NO_ERROR) {
292         APP_LOGE("QueryExtensionAbilityInfosV9 failed");
293         napi_value businessError = BusinessError::CreateCommonError(
294             env, ret, QUERY_EXTENSION_INFOS_SYNC, BUNDLE_PERMISSIONS);
295         napi_throw(env, businessError);
296         return nullptr;
297     }
298     napi_value nExtensionInfos = nullptr;
299     NAPI_CALL(env, napi_create_array(env, &nExtensionInfos));
300     CommonFunc::ConvertExtensionInfos(env, extensionInfos, nExtensionInfos);
301     APP_LOGD("call QueryExtensionInfosSync done");
302     return nExtensionInfos;
303 }
304 
GetPermissionDefSync(napi_env env,napi_callback_info info)305 napi_value GetPermissionDefSync(napi_env env, napi_callback_info info)
306 {
307     APP_LOGD("GetPermissionDefSync called");
308     NapiArg args(env, info);
309     if (!args.Init(ARGS_SIZE_ONE, ARGS_SIZE_ONE)) {
310         APP_LOGE("param count invalid");
311         BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
312         return nullptr;
313     }
314     std::string permissionName;
315     if (!CommonFunc::ParseString(env, args[ARGS_POS_ZERO], permissionName)) {
316         BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, PERMISSION_NAME, TYPE_STRING);
317         return nullptr;
318     }
319     auto iBundleMgr = CommonFunc::GetBundleMgr();
320     if (iBundleMgr == nullptr) {
321         APP_LOGE("can not get iBundleMgr");
322         BusinessError::ThrowError(env, ERROR_BUNDLE_SERVICE_EXCEPTION, ERR_MSG_BUNDLE_SERVICE_EXCEPTION);
323         return nullptr;
324     }
325     OHOS::AppExecFwk::PermissionDef permissionDef;
326     ErrCode ret = CommonFunc::ConvertErrCode(
327         iBundleMgr->GetPermissionDef(permissionName, permissionDef));
328     if (ret != NO_ERROR) {
329         APP_LOGE("GetPermissionDef failed");
330         napi_value businessError = BusinessError::CreateCommonError(
331             env, ret, GET_PERMISSION_DEF_SYNC, Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED);
332         napi_throw(env, businessError);
333         return nullptr;
334     }
335     napi_value nPermissionDef = nullptr;
336     NAPI_CALL(env, napi_create_object(env, &nPermissionDef));
337     CommonFunc::ConvertPermissionDef(env, nPermissionDef, permissionDef);
338     APP_LOGD("call GetPermissionDefSync done");
339     return nPermissionDef;
340 }
341 
ParamsProcessGetAbilityLabelSync(napi_env env,napi_callback_info info,std::string & bundleName,std::string & moduleName,std::string & abilityName)342 ErrCode ParamsProcessGetAbilityLabelSync(napi_env env, napi_callback_info info,
343     std::string& bundleName, std::string& moduleName, std::string& abilityName)
344 {
345     NapiArg args(env, info);
346     if (!args.Init(ARGS_SIZE_THREE, ARGS_SIZE_THREE)) {
347         APP_LOGE("param count invalid");
348         BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
349         return ERROR_PARAM_CHECK_ERROR;
350     }
351     if (args.GetMaxArgc() >= ARGS_SIZE_THREE) {
352         if (!CommonFunc::ParseString(env, args[ARGS_POS_ZERO], bundleName)) {
353             BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, BUNDLE_NAME, TYPE_STRING);
354             return ERROR_PARAM_CHECK_ERROR;
355         }
356         if (!CommonFunc::ParseString(env, args[ARGS_POS_ONE], moduleName)) {
357             BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, MODULE_NAME, TYPE_STRING);
358             return ERROR_PARAM_CHECK_ERROR;
359         }
360         if (!CommonFunc::ParseString(env, args[ARGS_POS_TWO], abilityName)) {
361             BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, ABILITY_NAME, TYPE_STRING);
362             return ERROR_PARAM_CHECK_ERROR;
363         }
364     } else {
365         BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
366         return ERROR_PARAM_CHECK_ERROR;
367     }
368     return ERR_OK;
369 }
370 
GetAbilityLabelSync(napi_env env,napi_callback_info info)371 napi_value GetAbilityLabelSync(napi_env env, napi_callback_info info)
372 {
373     APP_LOGD("begin to GetAbilityLabelSync");
374 #ifdef GLOBAL_RESMGR_ENABLE
375     std::string bundleName;
376     std::string moduleName;
377     std::string abilityName;
378     if (ParamsProcessGetAbilityLabelSync(env, info, bundleName, moduleName, abilityName) != ERR_OK) {
379         APP_LOGE("paramsProcess is invalid");
380         BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_TYPE_CHECK_ERROR);
381         return nullptr;
382     }
383     auto iBundleMgr = CommonFunc::GetBundleMgr();
384     if (iBundleMgr == nullptr) {
385         APP_LOGE("can not get iBundleMgr");
386         BusinessError::ThrowError(env, ERROR_BUNDLE_SERVICE_EXCEPTION, ERR_MSG_BUNDLE_SERVICE_EXCEPTION);
387         return nullptr;
388     }
389     std::string abilityLabel;
390     ErrCode ret = CommonFunc::ConvertErrCode(
391         iBundleMgr->GetAbilityLabel(bundleName, moduleName, abilityName, abilityLabel));
392     if (ret != NO_ERROR) {
393         APP_LOGE("GetAbilityLabel failed");
394         napi_value businessError = BusinessError::CreateCommonError(
395             env, ret, GET_ABILITY_LABEL_SYNC, BUNDLE_PERMISSIONS);
396         napi_throw(env, businessError);
397         return nullptr;
398     }
399     napi_value nAbilityLabel = nullptr;
400     napi_create_string_utf8(env, abilityLabel.c_str(), NAPI_AUTO_LENGTH, &nAbilityLabel);
401     APP_LOGD("call GetAbilityLabelSync done");
402     return nAbilityLabel;
403 #else
404     APP_LOGE("SystemCapability.BundleManager.BundleFramework.Resource not supported.");
405     napi_value error = BusinessError::CreateCommonError(env, ERROR_SYSTEM_ABILITY_NOT_FOUND, "getAbilityLabel");
406     napi_throw(env, error);
407     return nullptr;
408 #endif
409 }
410 
ParamsProcessGetLaunchWantForBundleSync(napi_env env,napi_callback_info info,std::string & bundleName,int32_t & userId)411 ErrCode ParamsProcessGetLaunchWantForBundleSync(napi_env env, napi_callback_info info,
412     std::string& bundleName, int32_t& userId)
413 {
414     NapiArg args(env, info);
415     if (!args.Init(ARGS_SIZE_ONE, ARGS_SIZE_TWO)) {
416         APP_LOGE("param count invalid");
417         BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
418         return ERROR_PARAM_CHECK_ERROR;
419     }
420     for (size_t i = 0; i < args.GetMaxArgc(); ++i) {
421         napi_valuetype valueType = napi_undefined;
422         napi_typeof(env, args[i], &valueType);
423         if (i == ARGS_POS_ZERO) {
424             if (!CommonFunc::ParseString(env, args[i], bundleName)) {
425                 APP_LOGE("bundleName %{public}s invalid", bundleName.c_str());
426                 BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, BUNDLE_NAME, TYPE_STRING);
427                 return ERROR_PARAM_CHECK_ERROR;
428             }
429         } else if (i == ARGS_POS_ONE) {
430             if (!CommonFunc::ParseInt(env, args[i], userId)) {
431                 APP_LOGW("userId parseInt failed");
432             }
433         } else {
434             APP_LOGE("parameter is invalid");
435             BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_TYPE_CHECK_ERROR);
436             return ERROR_PARAM_CHECK_ERROR;
437         }
438     }
439     if (bundleName.size() == 0) {
440         return ERROR_PARAM_CHECK_ERROR;
441     }
442     if (userId == Constants::UNSPECIFIED_USERID) {
443         userId = IPCSkeleton::GetCallingUid() / Constants::BASE_USER_RANGE;
444     }
445     return ERR_OK;
446 }
447 
GetLaunchWantForBundleSync(napi_env env,napi_callback_info info)448 napi_value GetLaunchWantForBundleSync(napi_env env, napi_callback_info info)
449 {
450     APP_LOGD("NAPI GetLaunchWantForBundleSync call");
451     std::string bundleName;
452     int32_t userId = Constants::UNSPECIFIED_USERID;
453     if (ParamsProcessGetLaunchWantForBundleSync(env, info, bundleName, userId) != ERR_OK) {
454         APP_LOGE("paramsProcess is invalid");
455         BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_TYPE_CHECK_ERROR);
456         return nullptr;
457     }
458     auto iBundleMgr = CommonFunc::GetBundleMgr();
459     if (iBundleMgr == nullptr) {
460         APP_LOGE("can not get iBundleMgr");
461         BusinessError::ThrowError(env, ERROR_BUNDLE_SERVICE_EXCEPTION, ERR_MSG_BUNDLE_SERVICE_EXCEPTION);
462         return nullptr;
463     }
464     OHOS::AAFwk::Want want;
465     ErrCode ret = CommonFunc::ConvertErrCode(
466         iBundleMgr->GetLaunchWantForBundle(bundleName, want, userId));
467     if (ret != NO_ERROR) {
468         APP_LOGE("GetLaunchWantForBundle failed");
469         napi_value businessError = BusinessError::CreateCommonError(
470             env, ret, GET_LAUNCH_WANT_FOR_BUNDLE_SYNC, Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED);
471         napi_throw(env, businessError);
472         return nullptr;
473     }
474     napi_value nWant = nullptr;
475     NAPI_CALL(env, napi_create_object(env, &nWant));
476     CommonFunc::ConvertWantInfo(env, nWant, want);
477     APP_LOGD("call GetLaunchWantForBundleSync done");
478     return nWant;
479 }
480 
GetBundleArchiveInfoSync(napi_env env,napi_callback_info info)481 napi_value GetBundleArchiveInfoSync(napi_env env, napi_callback_info info)
482 {
483     APP_LOGD("NAPI getBundleArchiveInfoSync called");
484     NapiArg args(env, info);
485     if (!args.Init(ARGS_SIZE_TWO, ARGS_SIZE_TWO)) {
486         APP_LOGE("param count invalid.");
487         BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
488         return nullptr;
489     }
490     std::string hapFilePath;
491     if (!CommonFunc::ParseString(env, args[ARGS_POS_ZERO], hapFilePath)) {
492         BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, HAP_FILE_PATH, TYPE_STRING);
493         return nullptr;
494     }
495     int32_t bundleFlags;
496     if (!CommonFunc::ParseInt(env, args[ARGS_POS_ONE], bundleFlags)) {
497         BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, BUNDLE_FLAGS, TYPE_NUMBER);
498         return nullptr;
499     }
500     auto iBundleMgr = CommonFunc::GetBundleMgr();
501     if (iBundleMgr == nullptr) {
502         napi_value error = BusinessError::CreateCommonError(env, ERROR_BUNDLE_SERVICE_EXCEPTION,
503             GET_BUNDLE_ARCHIVE_INFO_SYNC, BUNDLE_PERMISSIONS);
504         napi_throw(env, error);
505         return nullptr;
506     }
507     BundleInfo bundleInfo;
508     ErrCode ret = CommonFunc::ConvertErrCode(
509         iBundleMgr->GetBundleArchiveInfoV9(hapFilePath, bundleFlags, bundleInfo));
510     if (ret != ERR_OK) {
511         APP_LOGE("getBundleArchiveInfoSync failed");
512         napi_value businessError = BusinessError::CreateCommonError(
513             env, ret, GET_BUNDLE_ARCHIVE_INFO_SYNC, BUNDLE_PERMISSIONS);
514         napi_throw(env, businessError);
515         return nullptr;
516     }
517     napi_value nBundleInfo = nullptr;
518     NAPI_CALL(env, napi_create_object(env, &nBundleInfo));
519     CommonFunc::ConvertBundleInfo(env, bundleInfo, nBundleInfo, bundleFlags);
520     APP_LOGD("call getBundleArchiveInfoSync done.");
521     return nBundleInfo;
522 }
523 
GetBundleNameByUidSync(napi_env env,napi_callback_info info)524 napi_value GetBundleNameByUidSync(napi_env env, napi_callback_info info)
525 {
526     APP_LOGD("NAPI GetBundleNameByUidSync called");
527     NapiArg args(env, info);
528     if (!args.Init(ARGS_SIZE_ONE, ARGS_SIZE_ONE)) {
529         APP_LOGE("param count invalid.");
530         BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
531         return nullptr;
532     }
533     int32_t uid = -1;
534     if (!CommonFunc::ParseInt(env, args[ARGS_POS_ZERO], uid)) {
535         BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, UID, TYPE_NUMBER);
536         return nullptr;
537     }
538     auto iBundleMgr = CommonFunc::GetBundleMgr();
539     if (iBundleMgr == nullptr) {
540         napi_value error = BusinessError::CreateCommonError(env, ERROR_BUNDLE_SERVICE_EXCEPTION,
541             GET_BUNDLE_NAME_BY_UID_SYNC, BUNDLE_PERMISSIONS);
542         napi_throw(env, error);
543         return nullptr;
544     }
545     std::string bundleName;
546     ErrCode ret = CommonFunc::ConvertErrCode(iBundleMgr->GetNameForUid(uid, bundleName));
547     if (ret != ERR_OK) {
548         APP_LOGE("GetBundleNameByUidSync failed");
549         napi_value businessError = BusinessError::CreateCommonError(
550             env, ret, GET_BUNDLE_NAME_BY_UID_SYNC, BUNDLE_PERMISSIONS);
551         napi_throw(env, businessError);
552         return nullptr;
553     }
554     napi_value nBundleName = nullptr;
555     napi_create_string_utf8(env, bundleName.c_str(), NAPI_AUTO_LENGTH, &nBundleName);
556     APP_LOGD("call GetBundleNameByUidSync done.");
557     return nBundleName;
558 }
559 
ParamsProcessGetProfileByAbilitySync(napi_env env,napi_callback_info info,std::string & moduleName,std::string & abilityName,std::string & metadataName)560 ErrCode ParamsProcessGetProfileByAbilitySync(napi_env env, napi_callback_info info,
561     std::string& moduleName, std::string& abilityName, std::string& metadataName)
562 {
563     NapiArg args(env, info);
564     if (!args.Init(ARGS_SIZE_TWO, ARGS_SIZE_THREE)) {
565         APP_LOGE("param count invalid.");
566         BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
567         return ERROR_PARAM_CHECK_ERROR;
568     }
569     if (!CommonFunc::ParseString(env, args[ARGS_POS_ZERO], moduleName)) {
570         BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, MODULE_NAME, TYPE_STRING);
571         return ERROR_PARAM_CHECK_ERROR;
572     }
573     if (moduleName.empty()) {
574         APP_LOGE("param failed due to empty moduleName");
575         napi_value businessError = BusinessError::CreateCommonError(env, ERROR_MODULE_NOT_EXIST);
576         napi_throw(env, businessError);
577         return ERROR_MODULE_NOT_EXIST;
578     }
579     if (!CommonFunc::ParseString(env, args[ARGS_POS_ONE], abilityName)) {
580         BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, ABILITY_NAME, TYPE_STRING);
581         return ERROR_PARAM_CHECK_ERROR;
582     }
583     if (abilityName.empty()) {
584         APP_LOGE("param failed due to empty abilityName");
585         napi_value businessError = BusinessError::CreateCommonError(env, ERROR_ABILITY_NOT_EXIST);
586         napi_throw(env, businessError);
587         return ERROR_ABILITY_NOT_EXIST;
588     }
589     if (args.GetMaxArgc() == ARGS_SIZE_THREE) {
590         if (!CommonFunc::ParseString(env, args[ARGS_POS_TWO], metadataName)) {
591             APP_LOGW("Parse metadataName param failed");
592         }
593     }
594     return ERR_OK;
595 }
596 
CheckAbilityFromBundleInfo(const BundleInfo & bundleInfo,const std::string & abilityName,const std::string & moduleName,AbilityInfo & targetAbilityInfo)597 ErrCode CheckAbilityFromBundleInfo(const BundleInfo& bundleInfo, const std::string& abilityName,
598     const std::string& moduleName, AbilityInfo& targetAbilityInfo)
599 {
600     for (const auto& hapModuleInfo : bundleInfo.hapModuleInfos) {
601         for (const auto& abilityInfo : hapModuleInfo.abilityInfos) {
602             if (abilityInfo.name == abilityName && abilityInfo.moduleName == moduleName) {
603                 if (!abilityInfo.enabled) {
604                     APP_LOGI("ability disabled");
605                     return ERROR_ABILITY_IS_DISABLED;
606                 }
607                 targetAbilityInfo = abilityInfo;
608                 return ERR_OK;
609             }
610         }
611     }
612     return ERROR_ABILITY_NOT_EXIST;
613 }
614 
GetProfileByAbilitySync(napi_env env,napi_callback_info info)615 napi_value GetProfileByAbilitySync(napi_env env, napi_callback_info info)
616 {
617     APP_LOGD("NAPI GetProfileByAbilitySync called");
618     std::string moduleName;
619     std::string abilityName;
620     std::string metadataName;
621     if (ParamsProcessGetProfileByAbilitySync(env, info, moduleName, abilityName, metadataName) != ERR_OK) {
622         return nullptr;
623     }
624     auto iBundleMgr = CommonFunc::GetBundleMgr();
625     if (iBundleMgr == nullptr) {
626         napi_value error = BusinessError::CreateCommonError(env, ERROR_BUNDLE_SERVICE_EXCEPTION,
627             GET_PROFILE_BY_ABILITY_SYNC);
628         napi_throw(env, error);
629         return nullptr;
630     }
631     auto baseFlag = static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE) +
632            static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA) +
633            static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_DISABLE);
634     auto getAbilityFlag = baseFlag + static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_ABILITY);
635     BundleInfo bundleInfo;
636     ErrCode ret = CommonFunc::ConvertErrCode(iBundleMgr->GetBundleInfoForSelf(getAbilityFlag, bundleInfo));
637     if (ret != ERR_OK) {
638         APP_LOGE("GetProfileByAbilitySync failed");
639         napi_value businessError = BusinessError::CreateCommonError(env, ret, GET_PROFILE_BY_ABILITY_SYNC);
640         napi_throw(env, businessError);
641         return nullptr;
642     }
643     AbilityInfo targetAbilityInfo;
644     ret = CheckAbilityFromBundleInfo(bundleInfo, abilityName, moduleName, targetAbilityInfo);
645     if (ret != ERR_OK) {
646         APP_LOGE("GetProfileByAbilitySync failed by CheckAbilityFromBundleInfo");
647         napi_value businessError = BusinessError::CreateCommonError(env, ret, GET_PROFILE_BY_ABILITY_SYNC);
648         napi_throw(env, businessError);
649         return nullptr;
650     }
651     BundleMgrClient client;
652     std::vector<std::string> profileVec;
653     if (!client.GetProfileFromAbility(targetAbilityInfo, metadataName, profileVec)) {
654         APP_LOGE("GetProfileByAbilitySync failed by GetProfileFromAbility");
655         napi_value businessError = BusinessError::CreateCommonError(
656             env, ERROR_PROFILE_NOT_EXIST, GET_PROFILE_BY_ABILITY_SYNC);
657         napi_throw(env, businessError);
658         return nullptr;
659     }
660     napi_value nProfileInfos = nullptr;
661     NAPI_CALL(env, napi_create_array(env, &nProfileInfos));
662     CommonFunc::ConvertStringArrays(env, profileVec, nProfileInfos);
663     return nProfileInfos;
664 }
665 
CheckExtensionFromBundleInfo(const BundleInfo & bundleInfo,const std::string & abilityName,const std::string & moduleName,ExtensionAbilityInfo & targetExtensionInfo)666 ErrCode CheckExtensionFromBundleInfo(const BundleInfo& bundleInfo, const std::string& abilityName,
667     const std::string& moduleName, ExtensionAbilityInfo& targetExtensionInfo)
668 {
669     for (const auto& hapModuleInfo : bundleInfo.hapModuleInfos) {
670         for (const auto& extensionInfo : hapModuleInfo.extensionInfos) {
671             if (extensionInfo.name == abilityName && extensionInfo.moduleName == moduleName) {
672                 if (!extensionInfo.enabled) {
673                     APP_LOGI("extension disabled");
674                     return ERROR_ABILITY_IS_DISABLED;
675                 }
676                 targetExtensionInfo = extensionInfo;
677                 return ERR_OK;
678             }
679         }
680     }
681     return ERROR_ABILITY_NOT_EXIST;
682 }
683 
GetProfileByExAbilitySync(napi_env env,napi_callback_info info)684 napi_value GetProfileByExAbilitySync(napi_env env, napi_callback_info info)
685 {
686     APP_LOGD("NAPI GetProfileByExAbilitySync called");
687     std::string moduleName;
688     std::string extensionAbilityName;
689     std::string metadataName;
690     if (ParamsProcessGetProfileByAbilitySync(env, info, moduleName, extensionAbilityName, metadataName) != ERR_OK) {
691         return nullptr;
692     }
693     auto iBundleMgr = CommonFunc::GetBundleMgr();
694     if (iBundleMgr == nullptr) {
695         napi_value error = BusinessError::CreateCommonError(env, ERROR_BUNDLE_SERVICE_EXCEPTION,
696             GET_PROFILE_BY_EXTENSION_ABILITY_SYNC);
697         napi_throw(env, error);
698         return nullptr;
699     }
700     auto baseFlag = static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE) +
701            static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA) +
702            static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_DISABLE);
703     auto getExtensionFlag = baseFlag +
704         static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_EXTENSION_ABILITY);
705     BundleInfo bundleInfo;
706     ErrCode ret = CommonFunc::ConvertErrCode(iBundleMgr->GetBundleInfoForSelf(getExtensionFlag, bundleInfo));
707     if (ret != ERR_OK) {
708         APP_LOGE("GetProfileByExAbilitySync failed");
709         napi_value businessError = BusinessError::CreateCommonError(env, ret, GET_PROFILE_BY_EXTENSION_ABILITY_SYNC);
710         napi_throw(env, businessError);
711         return nullptr;
712     }
713     ExtensionAbilityInfo targetExtensionInfo;
714     ret = CheckExtensionFromBundleInfo(bundleInfo, extensionAbilityName, moduleName, targetExtensionInfo);
715     if (ret != ERR_OK) {
716         APP_LOGE("GetProfileByExAbilitySync failed by CheckExtensionFromBundleInfo");
717         napi_value businessError = BusinessError::CreateCommonError(env, ret, GET_PROFILE_BY_EXTENSION_ABILITY_SYNC);
718         napi_throw(env, businessError);
719         return nullptr;
720     }
721     BundleMgrClient client;
722     std::vector<std::string> profileVec;
723     if (!client.GetProfileFromExtension(targetExtensionInfo, metadataName, profileVec)) {
724         APP_LOGE("GetProfileByExAbilitySync failed by GetProfileFromExtension");
725         napi_value businessError = BusinessError::CreateCommonError(
726             env, ERROR_PROFILE_NOT_EXIST, GET_PROFILE_BY_EXTENSION_ABILITY_SYNC);
727         napi_throw(env, businessError);
728         return nullptr;
729     }
730     napi_value nProfileInfos = nullptr;
731     NAPI_CALL(env, napi_create_array(env, &nProfileInfos));
732     CommonFunc::ConvertStringArrays(env, profileVec, nProfileInfos);
733     return nProfileInfos;
734 }
735 
GetAppProvisionInfoSync(napi_env env,napi_callback_info info)736 napi_value GetAppProvisionInfoSync(napi_env env, napi_callback_info info)
737 {
738     APP_LOGD("NAPI GetAppProvisionInfoSync called");
739     NapiArg args(env, info);
740     if (!args.Init(ARGS_SIZE_ONE, ARGS_SIZE_TWO)) {
741         APP_LOGE("param count invalid.");
742         BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR);
743         return nullptr;
744     }
745     std::string bundleName;
746     if (!CommonFunc::ParseString(env, args[ARGS_POS_ZERO], bundleName)) {
747         BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, BUNDLE_NAME, TYPE_STRING);
748         return nullptr;
749     }
750     int32_t userId = IPCSkeleton::GetCallingUid() / Constants::BASE_USER_RANGE;
751     if (args.GetMaxArgc() >= ARGS_SIZE_TWO) {
752         if (!CommonFunc::ParseInt(env, args[ARGS_POS_ONE], userId)) {
753             APP_LOGW("Parse userId failed, set this parameter to the caller userId!");
754         }
755     }
756     auto iBundleMgr = CommonFunc::GetBundleMgr();
757     if (iBundleMgr == nullptr) {
758         BusinessError::ThrowError(env, ERROR_BUNDLE_SERVICE_EXCEPTION, ERR_MSG_BUNDLE_SERVICE_EXCEPTION);
759         return nullptr;
760     }
761     AppProvisionInfo appProvisionInfo;
762     ErrCode ret = CommonFunc::ConvertErrCode(
763         iBundleMgr->GetAppProvisionInfo(bundleName, userId, appProvisionInfo));
764     if (ret != ERR_OK) {
765         APP_LOGE("GetAppProvisionInfoSync failed");
766         napi_value businessError = BusinessError::CreateCommonError(
767             env, ret, GET_APP_PROVISION_INFO_SYNC, Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED);
768         napi_throw(env, businessError);
769         return nullptr;
770     }
771     napi_value nAppProvisionInfo = nullptr;
772     NAPI_CALL(env, napi_create_object(env, &nAppProvisionInfo));
773     CommonFunc::ConvertAppProvisionInfo(env, appProvisionInfo, nAppProvisionInfo);
774     APP_LOGD("call GetAppProvisionInfoSync done.");
775     return nAppProvisionInfo;
776 }
777 }  // namespace AppExecFwk
778 }  // namespace OHOS
779