diff --git a/interfaces/kits/js/bundle_manager/bundle_manager_sync.cpp b/interfaces/kits/js/bundle_manager/bundle_manager_sync.cpp index 28d44e699108f33882b15653522bae9525e5f4bf..159c4aa5b921266119fda7dcbcf6446bd6f03fbe 100644 --- a/interfaces/kits/js/bundle_manager/bundle_manager_sync.cpp +++ b/interfaces/kits/js/bundle_manager/bundle_manager_sync.cpp @@ -33,12 +33,19 @@ namespace AppExecFwk { constexpr const char* MODULE_NAME = "moduleName"; constexpr const char* ABILITY_NAME = "abilityName"; constexpr const char* BUNDLE_NAME = "bundleName"; +constexpr const char* BUNDLE_FLAGS = "bundleFlags"; +constexpr const char* HAP_FILE_PATH = "hapFilePath"; +constexpr const char* UID = "uid"; constexpr const char* USER_ID = "userId"; constexpr const char* EXTENSIONABILITY_TYPE = "extensionAbilityType"; constexpr const char* FLAGS = "flags"; constexpr const char* ERR_MSG_BUNDLE_SERVICE_EXCEPTION = "Bundle manager service is excepted."; const std::string GET_ABILITY_LABEL_SYNC = "GetAbilityLabelSync"; const std::string GET_LAUNCH_WANT_FOR_BUNDLE_SYNC = "GetLaunchWantForBundleSync"; +const std::string GET_BUNDLE_ARCHIVE_INFO_SYNC = "GetBundleArchiveInfoSync"; +const std::string GET_BUNDLE_NAME_BY_UID_SYNC = "GetBundleNameByUidSync"; +const std::string GET_PROFILE_BY_EXTENSION_ABILITY_SYNC = "GetProfileByExtensionAbilitySync"; +const std::string GET_PROFILE_BY_ABILITY_SYNC = "GetProfileByAbilitySync"; const std::string QUERY_EXTENSION_INFOS_SYNC = "QueryExtensionInfosSync"; const std::string GET_PERMISSION_DEF_SYNC = "GetPermissionDefSync"; const std::string BUNDLE_PERMISSIONS = "ohos.permission.GET_BUNDLE_INFO or ohos.permission.GET_BUNDLE_INFO_PRIVILEGED"; @@ -317,5 +324,238 @@ napi_value GetLaunchWantForBundleSync(napi_env env, napi_callback_info info) APP_LOGD("call GetLaunchWantForBundleSync done"); return nWant; } + +napi_value GetBundleArchiveInfoSync(napi_env env, napi_callback_info info) +{ + APP_LOGD("NAPI getBundleArchiveInfoSync called"); + NapiArg args(env, info); + if (!args.Init(ARGS_SIZE_TWO, ARGS_SIZE_TWO)) { + APP_LOGE("param count invalid."); + BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR); + return nullptr; + } + std::string hapFilePath; + if (!CommonFunc::ParseString(env, args[ARGS_POS_ZERO], hapFilePath)) { + BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, HAP_FILE_PATH, TYPE_STRING); + return nullptr; + } + int32_t bundleFlags; + if (!CommonFunc::ParseInt(env, args[ARGS_POS_ONE], bundleFlags)) { + BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, BUNDLE_FLAGS, TYPE_NUMBER); + return nullptr; + } + auto iBundleMgr = CommonFunc::GetBundleMgr(); + if (iBundleMgr == nullptr) { + napi_value error = BusinessError::CreateCommonError(env, ERROR_BUNDLE_SERVICE_EXCEPTION, + GET_BUNDLE_ARCHIVE_INFO_SYNC, BUNDLE_PERMISSIONS); + napi_throw(env, error); + return nullptr; + } + BundleInfo bundleInfo; + ErrCode ret = CommonFunc::ConvertErrCode( + iBundleMgr->GetBundleArchiveInfoV9(hapFilePath, bundleFlags, bundleInfo)); + if (ret != ERR_OK) { + APP_LOGE("getBundleArchiveInfoSync failed"); + napi_value businessError = BusinessError::CreateCommonError( + env, ret, GET_BUNDLE_ARCHIVE_INFO_SYNC, BUNDLE_PERMISSIONS); + napi_throw(env, businessError); + return nullptr; + } + napi_value nBundleInfo = nullptr; + NAPI_CALL(env, napi_create_object(env, &nBundleInfo)); + CommonFunc::ConvertBundleInfo(env, bundleInfo, nBundleInfo, bundleFlags); + APP_LOGD("call getBundleArchiveInfoSync done."); + return nBundleInfo; +} + +napi_value GetBundleNameByUidSync(napi_env env, napi_callback_info info) +{ + APP_LOGD("NAPI GetBundleNameByUidSync called"); + NapiArg args(env, info); + if (!args.Init(ARGS_SIZE_ONE, ARGS_SIZE_ONE)) { + APP_LOGE("param count invalid."); + BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR); + return nullptr; + } + int32_t uid = -1; + if (!CommonFunc::ParseInt(env, args[ARGS_POS_ZERO], uid)) { + BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, UID, TYPE_NUMBER); + return nullptr; + } + auto iBundleMgr = CommonFunc::GetBundleMgr(); + if (iBundleMgr == nullptr) { + napi_value error = BusinessError::CreateCommonError(env, ERROR_BUNDLE_SERVICE_EXCEPTION, + GET_BUNDLE_NAME_BY_UID_SYNC, BUNDLE_PERMISSIONS); + napi_throw(env, error); + return nullptr; + } + std::string bundleName; + ErrCode ret = CommonFunc::ConvertErrCode(iBundleMgr->GetNameForUid(uid, bundleName)); + if (ret != ERR_OK) { + APP_LOGE("GetBundleNameByUidSync failed"); + napi_value businessError = BusinessError::CreateCommonError( + env, ret, GET_BUNDLE_NAME_BY_UID_SYNC, BUNDLE_PERMISSIONS); + napi_throw(env, businessError); + return nullptr; + } + napi_value nBundleName = nullptr; + napi_create_string_utf8(env, bundleName.c_str(), NAPI_AUTO_LENGTH, &nBundleName); + APP_LOGD("call GetBundleNameByUidSync done."); + return nBundleName; +} + +ErrCode ParamsProcessGetProfileByAbilitySync(napi_env env, napi_callback_info info, + std::string& moduleName, std::string& abilityName, std::string& metadataName) +{ + NapiArg args(env, info); + if (!args.Init(ARGS_SIZE_TWO, ARGS_SIZE_THREE)) { + APP_LOGE("param count invalid."); + BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR); + return ERROR_PARAM_CHECK_ERROR; + } + if (!CommonFunc::ParseString(env, args[ARGS_POS_ZERO], moduleName)) { + BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, MODULE_NAME, TYPE_STRING); + return ERROR_PARAM_CHECK_ERROR; + } + if (!CommonFunc::ParseString(env, args[ARGS_POS_ONE], abilityName)) { + BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, ABILITY_NAME, TYPE_STRING); + return ERROR_PARAM_CHECK_ERROR; + } + if (args.GetMaxArgc() == ARGS_SIZE_THREE) { + if (!CommonFunc::ParseString(env, args[ARGS_POS_TWO], metadataName)) { + APP_LOGW("Parse metadataName param failed"); + } + } + return ERR_OK; +} + +ErrCode CheckAbilityFromBundleInfo(const BundleInfo& bundleInfo, const std::string& abilityName, + const std::string& moduleName, AbilityInfo& targetAbilityInfo) +{ + for (const auto& hapModuleInfo : bundleInfo.hapModuleInfos) { + for (const auto& abilityInfo : hapModuleInfo.abilityInfos) { + if (abilityInfo.name == abilityName && abilityInfo.moduleName == moduleName) { + targetAbilityInfo = abilityInfo; + return ERR_OK; + } + } + } + return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST; +} + +napi_value GetProfileByAbilitySync(napi_env env, napi_callback_info info) +{ + APP_LOGD("NAPI GetProfileByAbilitySync called"); + std::string moduleName; + std::string abilityName; + std::string metadataName; + if (ParamsProcessGetProfileByAbilitySync(env, info, moduleName, abilityName, metadataName) != ERR_OK) { + return nullptr; + } + auto iBundleMgr = CommonFunc::GetBundleMgr(); + if (iBundleMgr == nullptr) { + napi_value error = BusinessError::CreateCommonError(env, ERROR_BUNDLE_SERVICE_EXCEPTION, + GET_PROFILE_BY_ABILITY_SYNC); + napi_throw(env, error); + return nullptr; + } + auto baseFlag = static_cast(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE) + + static_cast(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA); + auto getAbilityFlag = baseFlag + static_cast(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_ABILITY); + BundleInfo bundleInfo; + ErrCode ret = CommonFunc::ConvertErrCode(iBundleMgr->GetBundleInfoForSelf(getAbilityFlag, bundleInfo)); + if (ret != ERR_OK) { + APP_LOGE("GetProfileByAbilitySync failed"); + napi_value businessError = BusinessError::CreateCommonError(env, ret, GET_PROFILE_BY_ABILITY_SYNC); + napi_throw(env, businessError); + return nullptr; + } + AbilityInfo targetAbilityInfo; + ret = CheckAbilityFromBundleInfo(bundleInfo, abilityName, moduleName, targetAbilityInfo); + if (ret != ERR_OK) { + APP_LOGE("GetProfileByAbilitySync failed by CheckAbilityFromBundleInfo"); + napi_value businessError = BusinessError::CreateCommonError(env, ret, GET_PROFILE_BY_ABILITY_SYNC); + napi_throw(env, businessError); + return nullptr; + } + BundleMgrClient client; + std::vector profileVec; + if (!client.GetProfileFromAbility(targetAbilityInfo, metadataName, profileVec)) { + APP_LOGE("GetProfileByAbilitySync failed by GetProfileFromAbility"); + napi_value businessError = BusinessError::CreateCommonError( + env, ERR_BUNDLE_MANAGER_PROFILE_NOT_EXIST, GET_PROFILE_BY_ABILITY_SYNC); + napi_throw(env, businessError); + return nullptr; + } + napi_value nProfileInfos = nullptr; + NAPI_CALL(env, napi_create_array(env, &nProfileInfos)); + CommonFunc::ConvertStringArrays(env, profileVec, nProfileInfos); + return nProfileInfos; +} + +ErrCode CheckExtensionFromBundleInfo(const BundleInfo& bundleInfo, const std::string& abilityName, + const std::string& moduleName, ExtensionAbilityInfo& targetExtensionInfo) +{ + for (const auto& hapModuleInfo : bundleInfo.hapModuleInfos) { + for (const auto& extensionInfo : hapModuleInfo.extensionInfos) { + if (extensionInfo.name == abilityName && extensionInfo.moduleName == moduleName) { + targetExtensionInfo = extensionInfo; + return ERR_OK; + } + } + } + return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST; +} + +napi_value GetProfileByExAbilitySync(napi_env env, napi_callback_info info) +{ + APP_LOGD("NAPI GetProfileByExAbilitySync called"); + std::string moduleName; + std::string extensionAbilityName; + std::string metadataName; + if (ParamsProcessGetProfileByAbilitySync(env, info, moduleName, extensionAbilityName, metadataName) != ERR_OK) { + return nullptr; + } + auto iBundleMgr = CommonFunc::GetBundleMgr(); + if (iBundleMgr == nullptr) { + napi_value error = BusinessError::CreateCommonError(env, ERROR_BUNDLE_SERVICE_EXCEPTION, + GET_PROFILE_BY_EXTENSION_ABILITY_SYNC); + napi_throw(env, error); + return nullptr; + } + auto baseFlag = static_cast(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE) + + static_cast(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA); + auto getExtensionFlag = baseFlag + + static_cast(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_EXTENSION_ABILITY); + BundleInfo bundleInfo; + ErrCode ret = CommonFunc::ConvertErrCode(iBundleMgr->GetBundleInfoForSelf(getExtensionFlag, bundleInfo)); + if (ret != ERR_OK) { + APP_LOGE("GetProfileByExAbilitySync failed"); + napi_value businessError = BusinessError::CreateCommonError(env, ret, GET_PROFILE_BY_EXTENSION_ABILITY_SYNC); + napi_throw(env, businessError); + return nullptr; + } + ExtensionAbilityInfo targetExtensionInfo; + ret = CheckExtensionFromBundleInfo(bundleInfo, extensionAbilityName, moduleName, targetExtensionInfo); + if (ret != ERR_OK) { + APP_LOGE("GetProfileByExAbilitySync failed by CheckExtensionFromBundleInfo"); + napi_value businessError = BusinessError::CreateCommonError(env, ret, GET_PROFILE_BY_EXTENSION_ABILITY_SYNC); + napi_throw(env, businessError); + return nullptr; + } + BundleMgrClient client; + std::vector profileVec; + if (!client.GetProfileFromExtension(targetExtensionInfo, metadataName, profileVec)) { + APP_LOGE("GetProfileByExAbilitySync failed by GetProfileFromExtension"); + napi_value businessError = BusinessError::CreateCommonError( + env, ERR_BUNDLE_MANAGER_PROFILE_NOT_EXIST, GET_PROFILE_BY_ABILITY_SYNC); + napi_throw(env, businessError); + return nullptr; + } + napi_value nProfileInfos = nullptr; + NAPI_CALL(env, napi_create_array(env, &nProfileInfos)); + CommonFunc::ConvertStringArrays(env, profileVec, nProfileInfos); + return nProfileInfos; +} } // namespace AppExecFwk } // namespace OHOS diff --git a/interfaces/kits/js/bundle_manager/bundle_manager_sync.h b/interfaces/kits/js/bundle_manager/bundle_manager_sync.h index 13438cbb23c8962b4fb5696b4ee2e8fd5c83c79b..82c25ff7c0017121fcdcb3a3d5cd04b4d18d5e17 100644 --- a/interfaces/kits/js/bundle_manager/bundle_manager_sync.h +++ b/interfaces/kits/js/bundle_manager/bundle_manager_sync.h @@ -35,6 +35,10 @@ napi_value QueryExtensionInfosSync(napi_env env, napi_callback_info info); napi_value GetPermissionDefSync(napi_env env, napi_callback_info info); napi_value GetAbilityLabelSync(napi_env env, napi_callback_info info); napi_value GetLaunchWantForBundleSync(napi_env env, napi_callback_info info); +napi_value GetBundleArchiveInfoSync(napi_env env, napi_callback_info info); +napi_value GetBundleNameByUidSync(napi_env env, napi_callback_info info); +napi_value GetProfileByAbilitySync(napi_env env, napi_callback_info info); +napi_value GetProfileByExAbilitySync(napi_env env, napi_callback_info info); } // namespace AppExecFwk } // namespace OHOS diff --git a/interfaces/kits/js/bundle_manager/native_module.cpp b/interfaces/kits/js/bundle_manager/native_module.cpp index 5e41fa72ee0f67bf3401b03ac32552bc25b8cbe1..6b7589c75d4689674e8554e3d784a7fc4ad00f2e 100644 --- a/interfaces/kits/js/bundle_manager/native_module.cpp +++ b/interfaces/kits/js/bundle_manager/native_module.cpp @@ -80,7 +80,9 @@ static napi_value BundleManagerExport(napi_env env, napi_value exports) napi_property_descriptor desc[] = { DECLARE_NAPI_FUNCTION("getBundleArchiveInfo", GetBundleArchiveInfo), + DECLARE_NAPI_FUNCTION("getBundleArchiveInfoSync", GetBundleArchiveInfoSync), DECLARE_NAPI_FUNCTION("getBundleNameByUid", GetBundleNameByUid), + DECLARE_NAPI_FUNCTION("getBundleNameByUidSync", GetBundleNameByUidSync), DECLARE_NAPI_FUNCTION("queryAbilityInfo", QueryAbilityInfos), DECLARE_NAPI_FUNCTION("queryAbilityInfoSync", QueryAbilityInfosSync), DECLARE_NAPI_FUNCTION("queryExtensionAbilityInfo", QueryExtensionInfos), @@ -96,7 +98,9 @@ static napi_value BundleManagerExport(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("getLaunchWantForBundle", GetLaunchWantForBundle), DECLARE_NAPI_FUNCTION("getLaunchWantForBundleSync", GetLaunchWantForBundleSync), DECLARE_NAPI_FUNCTION("getProfileByAbility", GetProfileByAbility), + DECLARE_NAPI_FUNCTION("getProfileByAbilitySync", GetProfileByAbilitySync), DECLARE_NAPI_FUNCTION("getProfileByExtensionAbility", GetProfileByExAbility), + DECLARE_NAPI_FUNCTION("getProfileByExtensionAbilitySync", GetProfileByExAbilitySync), DECLARE_NAPI_FUNCTION("getPermissionDefSync", GetPermissionDefSync), DECLARE_NAPI_FUNCTION("getPermissionDef", GetPermissionDef), DECLARE_NAPI_FUNCTION("getAllBundleInfo", GetBundleInfos),