diff --git a/interfaces/kits/ani/BUILD.gn b/interfaces/kits/ani/BUILD.gn index 0bea65b3817a65ce87300cb944e2cbb6a9bcfafd..f46974cb112036efa0f79bf3e5891cf2e80f4e9f 100644 --- a/interfaces/kits/ani/BUILD.gn +++ b/interfaces/kits/ani/BUILD.gn @@ -32,6 +32,7 @@ group("ani_bms_packages") { "bundle_manager:extension_ability_info_etc", "bundle_manager:hap_module_info_etc", "bundle_manager:metadata_etc", + "bundle_manager:permission_def_etc", "bundle_manager:skill_etc", "bundle_monitor:ani_bundle_monitor", "bundle_monitor:bundle_monitor_etc", diff --git a/interfaces/kits/ani/bundle_manager/BUILD.gn b/interfaces/kits/ani/bundle_manager/BUILD.gn index 0de2879bffc51c22283f6f631c899ba4d443b745..368ab89ee37c70dc65d767d165e7c313564f9f34 100755 --- a/interfaces/kits/ani/bundle_manager/BUILD.gn +++ b/interfaces/kits/ani/bundle_manager/BUILD.gn @@ -16,6 +16,8 @@ import("//build/ohos.gni") import("//foundation/bundlemanager/bundle_framework/appexecfwk.gni") ohos_shared_library("ani_bundle_manager") { + branch_protector_ret = "pac_ret" + sanitize = { boundary_sanitize = true cfi = true @@ -32,7 +34,6 @@ ohos_shared_library("ani_bundle_manager") { "${kits_path}/js/common", ] sources = [ - "${kits_path}/js/bundle_manager/bundle_manager_helper.cpp", "ani_bundle_manager.cpp", ] @@ -46,9 +47,20 @@ ohos_shared_library("ani_bundle_manager") { "${common_path}:libappexecfwk_common", "${core_path}:appexecfwk_core", "${kits_path}/ani/common:bms_ani_common", + "${kits_path}/js/bundle_manager:bundle_manager_common", "${kits_path}/js/common:bundle_napi_common", ] + cflags = [ + "-Os", + "-fstack-protector-strong", + ] + + cflags_cc = [ + "-Os", + "-fstack-protector-strong", + ] + external_deps = [ "ability_base:want", "ability_runtime:ani_common", @@ -213,6 +225,24 @@ ohos_prebuilt_etc("skill_etc") { deps = [ ":skill" ] } +generate_static_abc("permission_def") { + base_url = "./ets" + files = [ + "./ets/bundleManager/PermissionDef.ets", + "./ets/bundleManager/PermissionDefInner.ets", + ] + is_boot_abc = "True" + device_dst_file = "/system/framework/permission_def.abc" +} + +ohos_prebuilt_etc("permission_def_etc") { + source = "$target_out_dir/permission_def.abc" + module_install_dir = "framework" + subsystem_name = "bundlemanager" + part_name = "bundle_framework" + deps = [ ":permission_def" ] +} + ohos_copy("copy_bundleManager_ets") { sources = [ "./ets/bundleManager/AbilityInfoInner.ets", diff --git a/interfaces/kits/ani/bundle_manager/ani_bundle_manager.cpp b/interfaces/kits/ani/bundle_manager/ani_bundle_manager.cpp index c20c3e652ba7d737708460e233c76246189b6c48..7914bbd59598f7ea089c5c120cad444acd754c35 100644 --- a/interfaces/kits/ani/bundle_manager/ani_bundle_manager.cpp +++ b/interfaces/kits/ani/bundle_manager/ani_bundle_manager.cpp @@ -33,11 +33,13 @@ #include "bundle_mgr_interface.h" #include "bundle_mgr_proxy.h" #include "business_error_ani.h" +#include "clean_cache_callback.h" #include "common_fun_ani.h" #include "common_func.h" #include "enum_util.h" #include "ipc_skeleton.h" #include "napi_constants.h" +#include "process_cache_callback_host.h" namespace OHOS { namespace AppExecFwk { @@ -1121,6 +1123,277 @@ static void EnableDynamicIconSync(ani_env* env, ani_string aniBundleName, ani_st } } +static ani_object GetBundleArchiveInfoSync(ani_env* env, ani_string aniHapFilePath, ani_double aniBundleFlags) +{ + std::string hapFilePath = CommonFunAni::AniStrToString(env, aniHapFilePath); + if (hapFilePath.empty()) { + APP_LOGE("HapFilePath is empty"); + BusinessErrorAni::ThrowCommonError(env, ERROR_PARAM_CHECK_ERROR, HAP_FILE_PATH, CommonFunAniNS::TYPE_STRING); + return nullptr; + } + int32_t bundleFlags = 0; + if (!CommonFunAni::TryCastDoubleTo(aniBundleFlags, &bundleFlags)) { + APP_LOGE("Cast aniBundleFlags failed"); + BusinessErrorAni::ThrowCommonError(env, ERROR_PARAM_CHECK_ERROR, BUNDLE_FLAGS, CommonFunAniNS::TYPE_NUMBER); + return nullptr; + } + + BundleInfo bundleInfo; + ErrCode ret = BundleManagerHelper::InnerGetBundleArchiveInfo(hapFilePath, bundleFlags, bundleInfo); + if (ret != ERR_OK) { + APP_LOGE("InnerGetBundleArchiveInfo failed ret: %{public}d", ret); + BusinessErrorAni::ThrowCommonError( + env, ret, GET_BUNDLE_ARCHIVE_INFO, Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED); + return nullptr; + } + + return CommonFunAni::ConvertBundleInfo(env, bundleInfo, bundleFlags); +} + +static ani_object GetLaunchWant(ani_env* env) +{ + auto iBundleMgr = CommonFunc::GetBundleMgr(); + if (iBundleMgr == nullptr) { + APP_LOGE("GetBundleMgr failed"); + BusinessErrorAni::ThrowError(env, ERROR_BUNDLE_SERVICE_EXCEPTION, ERR_MSG_BUNDLE_SERVICE_EXCEPTION); + return nullptr; + } + OHOS::AAFwk::Want want; + ErrCode ret = iBundleMgr->GetLaunchWant(want); + if (ret != ERR_OK) { + APP_LOGE("GetLaunchWant failed ret: %{public}d", ret); + BusinessErrorAni::ThrowError(env, ERROR_GET_LAUNCH_WANT_INVALID, ERR_MSG_LAUNCH_WANT_INVALID); + return nullptr; + } + ElementName elementName = want.GetElement(); + if (elementName.GetBundleName().empty() || elementName.GetAbilityName().empty()) { + APP_LOGE("bundleName or abilityName is empty"); + BusinessErrorAni::ThrowError(env, ERROR_GET_LAUNCH_WANT_INVALID, ERR_MSG_LAUNCH_WANT_INVALID); + return nullptr; + } + + return WrapWant(env, want); +} + +static ErrCode InnerGetProfile(const std::string& moduleName, const std::string& abilityName, + const std::string& metadataName, bool isExtensionProfile, std::vector& profileVec) +{ + auto iBundleMgr = CommonFunc::GetBundleMgr(); + if (iBundleMgr == nullptr) { + APP_LOGE("can not get iBundleMgr"); + return ERROR_BUNDLE_SERVICE_EXCEPTION; + } + + if (abilityName.empty()) { + APP_LOGE("InnerGetProfile failed due to empty abilityName"); + return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST; + } + + if (moduleName.empty()) { + APP_LOGE("InnerGetProfile failed due to empty moduleName"); + return ERR_BUNDLE_MANAGER_MODULE_NOT_EXIST; + } + auto baseFlag = static_cast(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE) + + static_cast(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA) + + static_cast(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_DISABLE); + ErrCode result; + BundleMgrClient client; + BundleInfo bundleInfo; + if (!isExtensionProfile) { + auto getAbilityFlag = baseFlag + static_cast(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_ABILITY); + result = iBundleMgr->GetBundleInfoForSelf(getAbilityFlag, bundleInfo); + if (result != ERR_OK) { + APP_LOGE("GetBundleInfoForSelf failed"); + return result; + } + AbilityInfo targetAbilityInfo; + result = BundleManagerHelper::GetAbilityFromBundleInfo(bundleInfo, abilityName, moduleName, targetAbilityInfo); + if (result != ERR_OK) { + return result; + } + if (!client.GetProfileFromAbility(targetAbilityInfo, metadataName, profileVec)) { + APP_LOGE("GetProfileFromAbility failed"); + return ERR_BUNDLE_MANAGER_PROFILE_NOT_EXIST; + } + return ERR_OK; + } else { + auto getExtensionFlag = + baseFlag + static_cast(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_EXTENSION_ABILITY); + result = iBundleMgr->GetBundleInfoForSelf(getExtensionFlag, bundleInfo); + if (result != ERR_OK) { + APP_LOGE("GetBundleInfoForSelf failed"); + return result; + } + + ExtensionAbilityInfo targetExtensionInfo; + result = + BundleManagerHelper::GetExtensionFromBundleInfo(bundleInfo, abilityName, moduleName, targetExtensionInfo); + if (result != ERR_OK) { + return result; + } + if (!client.GetProfileFromExtension(targetExtensionInfo, metadataName, profileVec)) { + APP_LOGE("GetProfileFromExtension failed"); + return ERR_BUNDLE_MANAGER_PROFILE_NOT_EXIST; + } + return ERR_OK; + } + + APP_LOGE("InnerGetProfile failed due to type is invalid"); + return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR; +} + +static ani_object GetProfileByAbilityInner(ani_env* env, ani_string aniModuleName, ani_string aniAbilityName, + ani_string aniMetadataName, ani_boolean aniIsExtensionProfile) +{ + std::string moduleName = CommonFunAni::AniStrToString(env, aniModuleName); + if (moduleName.empty()) { + APP_LOGE("ModuleName is empty"); + BusinessErrorAni::ThrowCommonError( + env, ERROR_PARAM_CHECK_ERROR, Constants::MODULE_NAME, CommonFunAniNS::TYPE_STRING); + return nullptr; + } + std::string abilityName = CommonFunAni::AniStrToString(env, aniAbilityName); + if (abilityName.empty()) { + APP_LOGE("AbilityName is empty"); + BusinessErrorAni::ThrowCommonError( + env, ERROR_PARAM_CHECK_ERROR, Constants::ABILITY_NAME, CommonFunAniNS::TYPE_STRING); + return nullptr; + } + std::string metadataName = CommonFunAni::AniStrToString(env, aniMetadataName); + if (metadataName.empty()) { + APP_LOGW("Parse metadataName failed, The default value is undefined"); + } + bool isExtensionProfile = CommonFunAni::AniBooleanToBool(aniIsExtensionProfile); + + std::vector profileVec; + ErrCode ret = InnerGetProfile(moduleName, abilityName, metadataName, isExtensionProfile, profileVec); + if (ret != ERR_OK) { + APP_LOGE("InnerGetProfile failed ret: %{public}d", ret); + BusinessErrorAni::ThrowCommonError(env, CommonFunc::ConvertErrCode(ret), + isExtensionProfile ? GET_PROFILE_BY_EXTENSION_ABILITY : GET_PROFILE_BY_ABILITY, ""); + return nullptr; + } + return CommonFunAni::ConvertAniArrayString(env, profileVec); +} + +static ani_object GetPermissionDefSync(ani_env* env, ani_string aniPermissionName) +{ + std::string permissionName = CommonFunAni::AniStrToString(env, aniPermissionName); + if (permissionName.empty()) { + APP_LOGE("permissionName is empty"); + BusinessErrorAni::ThrowCommonError(env, ERROR_PARAM_CHECK_ERROR, PERMISSION_NAME, CommonFunAniNS::TYPE_STRING); + return nullptr; + } + PermissionDef permissionDef; + ErrCode ret = BundleManagerHelper::InnerGetPermissionDef(permissionName, permissionDef); + if (ret != ERR_OK) { + APP_LOGE("InnerGetPermissionDef failed ret: %{public}d", ret); + BusinessErrorAni::ThrowCommonError( + env, ret, GET_PERMISSION_DEF, Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED); + return nullptr; + } + return CommonFunAni::ConvertPermissionDef(env, permissionDef); +} + +static void CleanBundleCacheFilesSync(ani_env* env, ani_string aniBundleNamee, ani_double aniAppIndex) +{ + std::string bundleName = CommonFunAni::AniStrToString(env, aniBundleNamee); + if (bundleName.empty()) { + APP_LOGE("BundleName is empty"); + BusinessErrorAni::ThrowCommonError( + env, ERROR_PARAM_CHECK_ERROR, Constants::BUNDLE_NAME, CommonFunAniNS::TYPE_STRING); + return; + } + + int32_t appIndex = 0; + if (!CommonFunAni::TryCastDoubleTo(aniAppIndex, &appIndex)) { + APP_LOGE("Cast aniAppIndex failed"); + BusinessErrorAni::ThrowCommonError( + env, ERROR_PARAM_CHECK_ERROR, Constants::APP_INDEX, CommonFunAniNS::TYPE_NUMBER); + return; + } + + sptr cleanCacheCallback = new (std::nothrow) CleanCacheCallback(); + ErrCode ret = BundleManagerHelper::InnerCleanBundleCacheCallback(bundleName, appIndex, cleanCacheCallback); + if ((ret == ERR_OK) && (cleanCacheCallback != nullptr)) { + // wait for OnCleanCacheFinished + APP_LOGI("clean exec wait"); + if (cleanCacheCallback->WaitForCompletion()) { + ret = cleanCacheCallback->GetErr() ? ERR_OK : ERROR_BUNDLE_SERVICE_EXCEPTION; + } else { + APP_LOGI("clean exec timeout"); + ret = ERROR_BUNDLE_SERVICE_EXCEPTION; + } + } + + if (ret != ERR_OK) { + APP_LOGE("CleanBundleCacheFiles failed ret: %{public}d", ret); + BusinessErrorAni::ThrowCommonError(env, ret, CLEAN_BUNDLE_CACHE_FILES, Constants::PERMISSION_REMOVECACHEFILE); + } +} + +static ani_double GetAllBundleCacheSizeSync(ani_env* env) +{ + sptr cacheCallback = new (std::nothrow) ProcessCacheCallbackHost(); + if (cacheCallback == nullptr) { + APP_LOGE("cacheCallback is null"); + return 0; + } + + auto iBundleMgr = CommonFunc::GetBundleMgr(); + if (iBundleMgr == nullptr) { + APP_LOGE("GetBundleMgr failed"); + BusinessErrorAni::ThrowError(env, ERROR_BUNDLE_SERVICE_EXCEPTION, ERR_MSG_BUNDLE_SERVICE_EXCEPTION); + return 0; + } + + ErrCode ret = CommonFunc::ConvertErrCode(iBundleMgr->GetAllBundleCacheStat(cacheCallback)); + if (ret != ERR_OK) { + APP_LOGE("GetAllBundleCacheSize failed ret: %{public}d", ret); + BusinessErrorAni::ThrowCommonError( + env, ret, GET_ALL_BUNDLE_CACHE_SIZE, Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED); + return 0; + } + + uint64_t cacheSize = 0; + cacheSize = cacheCallback->GetCacheStat(); + if (cacheSize > uint64_t(INT64_MAX)) { + APP_LOGW("value out of range for int64"); + cacheSize = uint64_t(INT64_MAX); + } + + return static_cast(cacheSize); +} + +static void CleanAllBundleCacheSync(ani_env* env) +{ + sptr cacheCallback = new (std::nothrow) ProcessCacheCallbackHost(); + if (cacheCallback == nullptr) { + APP_LOGE("cacheCallback is null"); + return; + } + + auto iBundleMgr = CommonFunc::GetBundleMgr(); + if (iBundleMgr == nullptr) { + APP_LOGE("GetBundleMgr failed"); + BusinessErrorAni::ThrowError(env, ERROR_BUNDLE_SERVICE_EXCEPTION, ERR_MSG_BUNDLE_SERVICE_EXCEPTION); + return; + } + + ErrCode ret = CommonFunc::ConvertErrCode(iBundleMgr->CleanAllBundleCache(cacheCallback)); + if (ret != ERR_OK) { + APP_LOGE("CleanAllBundleCache failed ret: %{public}d", ret); + BusinessErrorAni::ThrowCommonError( + env, ret, GET_ALL_BUNDLE_CACHE_SIZE, Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED); + return; + } + + auto result = cacheCallback->GetCleanRet(); + if (result != 0) { + APP_LOGE("CleanAllBundleCache failed, result %{public}d", result); + } +} + extern "C" { ANI_EXPORT ani_status ANI_Constructor(ani_vm* vm, uint32_t* result) { @@ -1174,6 +1447,15 @@ ANI_EXPORT ani_status ANI_Constructor(ani_vm* vm, uint32_t* result) ani_native_function { "queryAbilityInfoWithWants", nullptr, reinterpret_cast(QueryAbilityInfoWithWants) }, ani_native_function { "enableDynamicIconSync", nullptr, reinterpret_cast(EnableDynamicIconSync) }, + ani_native_function { "getBundleArchiveInfoSync", nullptr, reinterpret_cast(GetBundleArchiveInfoSync) }, + ani_native_function { "getLaunchWant", nullptr, reinterpret_cast(GetLaunchWant) }, + ani_native_function { "getProfileByAbilityInner", nullptr, reinterpret_cast(GetProfileByAbilityInner) }, + ani_native_function { "getPermissionDefSync", nullptr, reinterpret_cast(GetPermissionDefSync) }, + ani_native_function { "cleanBundleCacheFilesSync", nullptr, + reinterpret_cast(CleanBundleCacheFilesSync) }, + ani_native_function { "getAllBundleCacheSizeSync", nullptr, + reinterpret_cast(GetAllBundleCacheSizeSync) }, + ani_native_function { "cleanAllBundleCacheSync", nullptr, reinterpret_cast(CleanAllBundleCacheSync) }, }; res = env->Namespace_BindNativeFunctions(kitNs, methods.data(), methods.size()); @@ -1195,9 +1477,21 @@ void ANIClearCacheListener::DoClearCache() ani_env* env = nullptr; ani_option interopEnabled { "--interop=disable", nullptr }; ani_options aniArgs { 1, &interopEnabled }; - g_vm->AttachCurrentThread(&aniArgs, ANI_VERSION_1, &env); - for (auto& item : g_aniCache) { - env->GlobalReference_Delete(item.second); + if (g_vm == nullptr) { + APP_LOGE("g_vm is empty"); + return; + } + ani_status status = g_vm->AttachCurrentThread(&aniArgs, ANI_VERSION_1, &env); + if (status != ANI_OK) { + APP_LOGE("AttachCurrentThread fail %{public}d", status); + return; + } + if (env == nullptr) { + APP_LOGE("env is empty"); + } else { + for (auto& item : g_aniCache) { + env->GlobalReference_Delete(item.second); + } } g_vm->DetachCurrentThread(); g_aniCache.clear(); diff --git a/interfaces/kits/ani/bundle_manager/ani_bundle_manager.h b/interfaces/kits/ani/bundle_manager/ani_bundle_manager.h index ce3d64dbec8b81bb642470ecfd17bff498451047..ff06ccda1fb3eb5a7893f733b7730f410e47f35f 100644 --- a/interfaces/kits/ani/bundle_manager/ani_bundle_manager.h +++ b/interfaces/kits/ani/bundle_manager/ani_bundle_manager.h @@ -49,7 +49,7 @@ struct ANIQuery { bool operator==(const ANIQuery& query) const { return bundleName_ == query.bundleName_ && interfaceType_ == query.interfaceType_ && flags_ == query.flags_ && - userId_ == query.userId_; + userId_ == query.userId_; } }; @@ -57,7 +57,7 @@ struct ANIQueryHash { size_t operator()(const ANIQuery& query) const { return std::hash()(query.bundleName_) ^ std::hash()(query.interfaceType_) ^ - std::hash()(query.flags_) ^ std::hash()(query.userId_); + std::hash()(query.flags_) ^ std::hash()(query.userId_); } }; diff --git a/interfaces/kits/ani/bundle_manager/ets/@ohos.bundle.bundleManager.ets b/interfaces/kits/ani/bundle_manager/ets/@ohos.bundle.bundleManager.ets index f97b823e6b29f98042d0e4d82b21810c7ccc9393..b355cf5e6ad74b7a58df82bdc5e54ee3a057fec4 100644 --- a/interfaces/kits/ani/bundle_manager/ets/@ohos.bundle.bundleManager.ets +++ b/interfaces/kits/ani/bundle_manager/ets/@ohos.bundle.bundleManager.ets @@ -18,9 +18,11 @@ import { AsyncCallback, BusinessError } from '@ohos.base'; import { ApplicationInfo } from 'bundleManager.ApplicationInfo'; import { AbilityInfo } from 'bundleManager.AbilityInfo'; import { ExtensionAbilityInfo } from 'bundleManager.ExtensionAbilityInfo'; +import { PermissionDef } from 'bundleManager.PermissionDef'; import Want from '@ohos.app.ability.Want'; import { BundleInfoInner } from './bundleManager/BundleInfoInner'; import { ApplicationInfoInner } from './bundleManager/ApplicationInfoInner'; +import { PermissionDefInner } from './bundleManager/PermissionDefInner'; namespace bundleManager { @@ -224,6 +226,21 @@ namespace bundleManager { export native function enableDynamicIconSync(bundleName: string, moduleName: string): void; + export native function getBundleArchiveInfoSync(hapFilePath: string, bundleFlags: number): BundleInfo; + + export native function getLaunchWant(): Want; + + export native function getProfileByAbilityInner(moduleName: string, + abilityName: string, metadataName: string, isExtensionProfile: boolean): Array; + + export native function getPermissionDefSync(permissionName: string): PermissionDef; + + export native function cleanBundleCacheFilesSync(bundleName: string, appIndex: number): void; + + export native function getAllBundleCacheSizeSync(): number; + + export native function cleanAllBundleCacheSync(): void; + function isApplicationEnabledSync(bundleName: string): boolean { return bundleManager.isApplicationEnabledSyncInner(bundleName, 0); } @@ -270,6 +287,17 @@ namespace bundleManager { return bundleManager.queryExAbilityInfoSyncWithoutWant(extensionAbilityType, extensionAbilityFlags, userIdInfo); } + function getProfileByAbilitySync(moduleName: string, abilityName: string, metadataName?: string): Array { + let metadataNameInfo: string = metadataName ?? ""; + return bundleManager.getProfileByAbilityInner(moduleName, abilityName, metadataNameInfo, false); + } + + function getProfileByExtensionAbilitySync(moduleName: string, + extensionAbilityName: string, metadataName?: string): Array { + let metadataNameInfo: string = metadataName ?? ""; + return bundleManager.getProfileByAbilityInner(moduleName, extensionAbilityName, metadataNameInfo, true); + } + function getBundleInfoForSelf(bundleFlags: number, callback: AsyncCallback):void { let execFun = (): BundleInfo => { return bundleManager.getBundleInfoForSelfSync(bundleFlags); @@ -1002,6 +1030,188 @@ namespace bundleManager { ); return p; } + + function getBundleArchiveInfo(hapFilePath: string, bundleFlags: number, callback: AsyncCallback): void { + let execFun = (): BundleInfo => { + return bundleManager.getBundleArchiveInfoSync(hapFilePath, bundleFlags); + }; + let p1 = taskpool.execute(execFun); + p1.then((e: NullishType) => { + let resultBundleInfo: BundleInfo = e as BundleInfo; + let r = new BusinessError(); + callback(r, resultBundleInfo); + }, (err: BusinessError): void => { + let resultBundleInfo = new BundleInfoInner(); + callback(err, resultBundleInfo); + }); + } + + function getBundleArchiveInfo(hapFilePath: string, bundleFlags: number): Promise { + let p = new Promise((resolve: (bundleInfo: BundleInfo) => void, + reject: (error: BusinessError) => void) => { + let execFun = (): BundleInfo => { + return bundleManager.getBundleArchiveInfoSync(hapFilePath, bundleFlags); + }; + let p1 = taskpool.execute(execFun); + p1.then((e: NullishType) => { + let resultBundleInfo: BundleInfo = e as BundleInfo; + resolve(resultBundleInfo); + }, (err: BusinessError): void => { + reject(err); + }); + } + ); + return p; + } + + function getProfileByAbility(moduleName: string, abilityName: string, metadataName?: string): Promise> { + let p = new Promise>((resolve: (profile: Array) + => void, reject: (error: BusinessError) => void) => { + let metadataNameInfo: string = metadataName ?? ""; + let execFun = (): Array => { + return bundleManager.getProfileByAbilityInner(moduleName, abilityName, metadataNameInfo, false); + }; + let p1 = taskpool.execute(execFun); + p1.then((e: NullishType) => { + let profile: Array = e as Array; + resolve(profile); + }, (err: BusinessError): void => { + reject(err); + }); + } + ); + return p; + } + + function getProfileByAbility(moduleName: string, + abilityName: string, metadataName: string, callback: AsyncCallback>) { + let execFun = (): Array => { + return bundleManager.getProfileByAbilityInner(moduleName, abilityName, metadataName, false); + }; + let p1 = taskpool.execute(execFun); + p1.then((e: NullishType) => { + let profile: Array = e as Array; + let r = new BusinessError(); + callback(r, profile); + }, (err: BusinessError): void => { + let profile = new Array; + callback(err, profile); + }); + } + + function getProfileByExtensionAbility(moduleName: string, + extensionAbilityName: string, metadataName?: string): Promise> { + let p = new Promise>((resolve: (profile: Array) + => void, reject: (error: BusinessError) => void) => { + let metadataNameInfo: string = metadataName ?? ""; + let execFun = (): Array => { + return bundleManager.getProfileByAbilityInner(moduleName, extensionAbilityName, metadataNameInfo, true); + }; + let p1 = taskpool.execute(execFun); + p1.then((e: NullishType) => { + let profile: Array = e as Array; + resolve(profile); + }, (err: BusinessError): void => { + reject(err); + }); + } + ); + return p; + } + + function getProfileByExtensionAbility(moduleName: string, + extensionAbilityName: string, metadataName: string, callback: AsyncCallback>): void { + let execFun = (): Array => { + return bundleManager.getProfileByAbilityInner(moduleName, extensionAbilityName, metadataName, true); + }; + let p1 = taskpool.execute(execFun); + p1.then((e: NullishType) => { + let profile: Array = e as Array; + let r = new BusinessError(); + callback(r, profile); + }, (err: BusinessError): void => { + let profile = new Array; + callback(err, profile); + }); + } + + function getPermissionDef(permissionName: string): Promise { + let p = new Promise((resolve: (profile: PermissionDef) + => void, reject: (error: BusinessError) => void) => { + let execFun = (): PermissionDef => { + return bundleManager.getPermissionDefSync(permissionName); + }; + let p1 = taskpool.execute(execFun); + p1.then((e: NullishType) => { + let permissionDef: PermissionDef = e as PermissionDef; + resolve(permissionDef); + }, (err: BusinessError): void => { + reject(err); + }); + } + ); + return p; + } + + function getPermissionDef(permissionName: string, callback: AsyncCallback): void { + let execFun = (): PermissionDef => { + return bundleManager.getPermissionDefSync(permissionName); + }; + let p1 = taskpool.execute(execFun); + p1.then((e: NullishType) => { + let permissionDef: PermissionDef = e as PermissionDef; + let r = new BusinessError(); + callback(r, permissionDef); + }, (err: BusinessError): void => { + let permissionDef = new PermissionDefInner(); + callback(err, permissionDef); + }); + } + + function cleanBundleCacheFiles(bundleName: string, callback: AsyncCallback): void { + let execFun = (): void => { + return bundleManager.cleanBundleCacheFilesSync(bundleName, 0); + }; + let p1 = taskpool.execute(execFun); + p1.then(() => { + let r = new BusinessError(); + callback(r, undefined); + }, (err: BusinessError): void => { + callback(err, undefined); + }); + } + + function cleanBundleCacheFiles(bundleName: string): Promise { + let p = new Promise((resolve: (v:undefined) => void, reject: (error: BusinessError) => void) : void => { + let execFun = (): void => { + return bundleManager.cleanBundleCacheFilesSync(bundleName, 0); + }; + let p1 = taskpool.execute(execFun); + p1.then((): void => { + resolve(undefined); + }, (err: BusinessError): void => { + reject(err); + }); + } + ); + return p; + } + + function cleanBundleCacheFiles(bundleName: string, appIndex: number): Promise { + let p = new Promise((resolve: (v:undefined) => void, reject: (error: BusinessError) => void) : void => { + let execFun = (): void => { + return bundleManager.cleanBundleCacheFilesSync(bundleName, appIndex); + }; + let p1 = taskpool.execute(execFun); + p1.then((): void => { + resolve(undefined); + }, (err: BusinessError): void => { + reject(err); + }); + } + ); + return p; + } } export default bundleManager; diff --git a/interfaces/kits/ani/bundle_manager/ets/bundleManager/PermissionDef.ets b/interfaces/kits/ani/bundle_manager/ets/bundleManager/PermissionDef.ets new file mode 100644 index 0000000000000000000000000000000000000000..25d8fa427f232b43f761feea2a3b6fe1364bbad5 --- /dev/null +++ b/interfaces/kits/ani/bundle_manager/ets/bundleManager/PermissionDef.ets @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export interface PermissionDef { + readonly permissionName: string; + readonly grantMode: number; + readonly labelId: number; + readonly descriptionId: number; +} \ No newline at end of file diff --git a/interfaces/kits/ani/bundle_manager/ets/bundleManager/PermissionDefInner.ets b/interfaces/kits/ani/bundle_manager/ets/bundleManager/PermissionDefInner.ets new file mode 100644 index 0000000000000000000000000000000000000000..07420ff8c2e0d1e54682b992f8242d0724225274 --- /dev/null +++ b/interfaces/kits/ani/bundle_manager/ets/bundleManager/PermissionDefInner.ets @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { PermissionDef } from 'bundleManager.PermissionDef' + +export class PermissionDefInner implements PermissionDef { + public readonly permissionName: string = ''; + public readonly grantMode: number; + public readonly labelId: number; + public readonly descriptionId: number; +} \ No newline at end of file diff --git a/interfaces/kits/ani/common/common_fun_ani.cpp b/interfaces/kits/ani/common/common_fun_ani.cpp index 4bfcd1b9e57b5c3bacbb472b3ad6b2a04a766da1..b5627b490e71e49845738c451930c7119c423763 100644 --- a/interfaces/kits/ani/common/common_fun_ani.cpp +++ b/interfaces/kits/ani/common/common_fun_ani.cpp @@ -36,6 +36,7 @@ constexpr const char* CLASSNAME_PERMISSION = "LbundleManager/BundleInfoInner/Req constexpr const char* CLASSNAME_USEDSCENE = "LbundleManager/BundleInfoInner/UsedSceneInner;"; constexpr const char* CLASSNAME_SIGNATUREINFO = "LbundleManager/BundleInfoInner/SignatureInfoInner;"; constexpr const char* CLASSNAME_APPCLONEIDENTITY = "LbundleManager/BundleInfoInner/AppCloneIdentityInner;"; +constexpr const char* CLASSNAME_PERMISSIONDEF = "LbundleManager/PermissionDefInner/PermissionDefInner;"; constexpr const char* CLASSNAME_METADATA = "LbundleManager/MetadataInner/MetadataInner;"; constexpr const char* CLASSNAME_RESOURCE = "Lglobal/resourceInner/ResourceInner;"; constexpr const char* CLASSNAME_ROUTERITEM = "LbundleManager/HapModuleInfoInner/RouterItemInner;"; @@ -240,6 +241,8 @@ constexpr const char* PROPERTYNAME_DISPOSEDTYPE = "disposedType"; constexpr const char* PROPERTYNAME_CONTROLTYPE = "controlType"; constexpr const char* PROPERTYNAME_ELEMENTLIST = "elementList"; constexpr const char* PROPERTYNAME_UNINSTALLCOMPONENTTYPE = "uninstallComponentType"; +constexpr const char* PROPERTYNAME_PERMISSIONNAME = "permissionName"; +constexpr const char* PROPERTYNAME_GRANTMODE = "grantMode"; constexpr const char* PATH_PREFIX = "/data/app/el1/bundle/public"; constexpr const char* CODE_PATH_PREFIX = "/data/storage/el1/bundle/"; @@ -260,9 +263,9 @@ std::string CommonFunAni::AniStrToString(ani_env* env, ani_string aniStr) std::string buffer; buffer.resize(strSize + 1); - ani_size retSize; + ani_size retSize = 0; status = env->String_GetUTF8(aniStr, buffer.data(), buffer.size(), &retSize); - if (status != ANI_OK || retSize < 0) { + if (status != ANI_OK || retSize == 0) { APP_LOGE("String_GetUTF8SubString failed %{public}d", status); return ""; } @@ -339,8 +342,8 @@ ani_object CommonFunAni::ConvertBundleInfo(ani_env* env, const BundleInfo& bundl RETURN_NULL_IF_FALSE(CallSetter(env, cls, object, PROPERTYNAME_TARGETVERSION, bundleInfo.targetVersion)); // appInfo: ApplicationInfo - if ((static_cast(flags) & static_cast(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION)) == - static_cast(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION)) { + if ((static_cast(flags) & static_cast(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION)) == + static_cast(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION)) { ani_object aObject = ConvertApplicationInfo(env, bundleInfo.applicationInfo); RETURN_NULL_IF_NULL(aObject); RETURN_NULL_IF_FALSE(CallSetter(env, cls, object, PROPERTYNAME_APPINFO, aObject)); @@ -365,8 +368,9 @@ ani_object CommonFunAni::ConvertBundleInfo(ani_env* env, const BundleInfo& bundl RETURN_NULL_IF_FALSE(CallSetter(env, cls, object, PROPERTYNAME_PERMISSIONGRANTSTATES, aPermissionGrantStates)); // signatureInfo: SignatureInfo - if ((static_cast(flags) & static_cast(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_SIGNATURE_INFO)) == - static_cast(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_SIGNATURE_INFO)) { + if ((static_cast(flags) & + static_cast(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_SIGNATURE_INFO)) == + static_cast(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_SIGNATURE_INFO)) { ani_object aniSignatureInfoObj = ConvertSignatureInfo(env, bundleInfo.signatureInfo); RETURN_NULL_IF_NULL(aniSignatureInfoObj); RETURN_NULL_IF_FALSE(CallSetter(env, cls, object, PROPERTYNAME_SIGNATUREINFO, aniSignatureInfoObj)); @@ -1359,6 +1363,34 @@ ani_object CommonFunAni::ConvertAppCloneIdentity(ani_env* env, const std::string return object; } +ani_object CommonFunAni::ConvertPermissionDef(ani_env* env, const PermissionDef& permissionDef) +{ + RETURN_NULL_IF_NULL(env); + + ani_class cls = CreateClassByName(env, CLASSNAME_PERMISSIONDEF); + RETURN_NULL_IF_NULL(cls); + + ani_object object = CreateNewObjectByClass(env, cls); + RETURN_NULL_IF_NULL(object); + + ani_string string = nullptr; + + // permissionName: string + RETURN_NULL_IF_FALSE(StringToAniStr(env, permissionDef.permissionName, string)); + RETURN_NULL_IF_FALSE(CallSetter(env, cls, object, PROPERTYNAME_PERMISSIONNAME, string)); + + // grantMode: number + RETURN_NULL_IF_FALSE(CallSetter(env, cls, object, PROPERTYNAME_GRANTMODE, permissionDef.grantMode)); + + // labelId: number + RETURN_NULL_IF_FALSE(CallSetter(env, cls, object, PROPERTYNAME_LABELID, permissionDef.labelId)); + + // descriptionId: number + RETURN_NULL_IF_FALSE(CallSetter(env, cls, object, PROPERTYNAME_DESCRIPTIONID, permissionDef.descriptionId)); + + return object; +} + ani_object CommonFunAni::ConvertBundleResourceInfo(ani_env* env, const BundleResourceInfo& bundleResInfo) { RETURN_NULL_IF_NULL(env); diff --git a/interfaces/kits/ani/common/common_fun_ani.h b/interfaces/kits/ani/common/common_fun_ani.h index 5222d5a49fadea4149799e0658b41ab1288dcecb..ec4bd0de3ca3eef98c07856fb96c8d5b6430e595 100644 --- a/interfaces/kits/ani/common/common_fun_ani.h +++ b/interfaces/kits/ani/common/common_fun_ani.h @@ -158,6 +158,7 @@ public: static ani_object ConvertBundleInfo(ani_env* env, const BundleInfo& bundleInfo, int32_t flags); static ani_object ConvertAppCloneIdentity(ani_env* env, const std::string& bundleName, const int32_t appIndex); + static ani_object ConvertPermissionDef(ani_env* env, const PermissionDef& permissionDef); static ani_object ConvertBundleResourceInfo(ani_env* env, const BundleResourceInfo& bundleResInfo); static ani_object ConvertLauncherAbilityResourceInfo(ani_env* env, diff --git a/interfaces/kits/ani/launcher_bundle_manager/ani_launcher_bundle_manager.cpp b/interfaces/kits/ani/launcher_bundle_manager/ani_launcher_bundle_manager.cpp index e82f729ad3bb42a672d0a6e2a1a1713158a9f2d1..5a20b55edf2c5b4461f9dd4d08c1cb2dda9b97f7 100644 --- a/interfaces/kits/ani/launcher_bundle_manager/ani_launcher_bundle_manager.cpp +++ b/interfaces/kits/ani/launcher_bundle_manager/ani_launcher_bundle_manager.cpp @@ -23,12 +23,12 @@ #include "business_error_ani.h" #include "common_fun_ani.h" #include "common_func.h" +#include "ipc_skeleton.h" #include "js_launcher_service.h" #include "launcher_ability_info.h" +#include "napi_constants.h" #include "shortcut_info.h" #include "start_options.h" -#include "ipc_skeleton.h" -#include "napi_constants.h" namespace OHOS { namespace AppExecFwk { diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index e62a011e4850bf1bf86b0025c9efd720a85054c8..3c2d353e164271497fb2cedf340b0fe3b0f4be2c 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -23,6 +23,7 @@ group("napi_packages") { if (support_jsapi) { deps += [ "app_control:appcontrol", + "bundle_manager:bundle_manager_common", "bundle_manager:bundlemanager", "bundle_monitor:bundlemonitor", "bundle_resource:bundle_res_common", diff --git a/interfaces/kits/js/bundle_manager/BUILD.gn b/interfaces/kits/js/bundle_manager/BUILD.gn index e15a10822e516e8d93e326f2fd2c5bf7bc69824d..08487c17a6c49c46135c2cc1844d38cd171e22b1 100644 --- a/interfaces/kits/js/bundle_manager/BUILD.gn +++ b/interfaces/kits/js/bundle_manager/BUILD.gn @@ -14,6 +14,60 @@ import("//build/ohos.gni") import("../../../../appexecfwk.gni") +ohos_shared_library("bundle_manager_common") { + branch_protector_ret = "pac_ret" + + sanitize = { + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + integer_overflow = true + ubsan = true + } + + include_dirs = [ "${kits_path}/js/common" ] + sources = [ + "bundle_manager_helper.cpp", + "clean_cache_callback.cpp", + ] + + cflags = [ + "-Os", + "-fstack-protector-strong", + ] + + cflags_cc = [ + "-Os", + "-fstack-protector-strong", + ] + + deps = [ + "${base_path}:appexecfwk_base", + "${common_path}:libappexecfwk_common", + "${core_path}:appexecfwk_core", + "../common:bundle_napi_common", + ] + + defines = [ + "APP_LOG_TAG = \"BMS\"", + "LOG_DOMAIN = 0xD001120", + ] + + external_deps = [ + "ability_base:want", + "ability_runtime:napi_common", + "ability_runtime:runtime", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_single", + "napi:ace_napi", + ] + + subsystem_name = "bundlemanager" + part_name = "bundle_framework" +} + ohos_shared_library("bundlemanager") { branch_protector_ret = "pac_ret" @@ -33,13 +87,12 @@ ohos_shared_library("bundlemanager") { ] sources = [ "bundle_manager.cpp", - "bundle_manager_helper.cpp", "bundle_manager_sync.cpp", - "clean_cache_callback.cpp", "native_module.cpp", ] deps = [ + ":bundle_manager_common", "${base_path}:appexecfwk_base", "${common_path}:libappexecfwk_common", "${core_path}:appexecfwk_core", diff --git a/interfaces/kits/js/bundle_manager/bundle_manager.cpp b/interfaces/kits/js/bundle_manager/bundle_manager.cpp index d06f213e71661bccf7a575901a85ab62ba869a09..fe999ece9fcb699ad5114bc8f9bd374c91af0367 100644 --- a/interfaces/kits/js/bundle_manager/bundle_manager.cpp +++ b/interfaces/kits/js/bundle_manager/bundle_manager.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -51,7 +51,6 @@ constexpr const char* STRING_TYPE = "napi_string"; constexpr const char* GET_LAUNCH_WANT_FOR_BUNDLE = "GetLaunchWantForBundle"; constexpr const char* VERIFY_ABC = "VerifyAbc"; constexpr const char* DELETE_ABC = "DeleteAbc"; -constexpr const char* ERR_MSG_LAUNCH_WANT_INVALID = "The launch want is not found."; constexpr const char* ADDITIONAL_INFO = "additionalInfo"; constexpr const char* LINK = "link"; constexpr const char* DEVELOPER_ID = "developerId"; @@ -63,13 +62,8 @@ constexpr const char* STATE = "state"; constexpr const char* SOURCE_PATHS = "sourcePaths"; constexpr const char* DESTINATION_PATHS = "destinationPath"; constexpr const char* HOST_BUNDLE_NAME = "hostBundleName"; -const std::string GET_BUNDLE_ARCHIVE_INFO = "GetBundleArchiveInfo"; -const std::string GET_ALL_BUNDLE_CACHE_SIZE = "getAllBundleCacheSize"; -const std::string CLEAN_ALL_BUNDLE_CACHE = "cleanAllBundleCache"; const std::string QUERY_ABILITY_INFOS = "QueryAbilityInfos"; const std::string QUERY_EXTENSION_INFOS = "QueryExtensionInfos"; -const std::string GET_PERMISSION_DEF = "GetPermissionDef"; -const std::string PERMISSION_NAME = "permissionName"; const std::string PARAM_TYPE_CHECK_ERROR_WITH_POS = "param type check error, error position : "; const std::string GET_ALL_SHARED_BUNDLE_INFO = "GetAllSharedBundleInfo"; const std::string GET_SHARED_BUNDLE_INFO = "GetSharedBundleInfo"; @@ -152,18 +146,6 @@ void RegisterClearCacheListener() (void)EventFwk::CommonEventManager::SubscribeCommonEvent(g_clearCacheListener); } -static ErrCode InnerGetBundleArchiveInfo(std::string &hapFilePath, int32_t flags, BundleInfo &bundleInfo) -{ - auto iBundleMgr = CommonFunc::GetBundleMgr(); - if (iBundleMgr == nullptr) { - APP_LOGE("iBundleMgr is null"); - return ERROR_BUNDLE_SERVICE_EXCEPTION; - } - ErrCode ret = iBundleMgr->GetBundleArchiveInfoV9(hapFilePath, flags, bundleInfo); - APP_LOGD("GetBundleArchiveInfoV9 ErrCode : %{public}d", ret); - return CommonFunc::ConvertErrCode(ret); -} - void GetBundleArchiveInfoExec(napi_env env, void *data) { GetBundleArchiveInfoCallbackInfo *asyncCallbackInfo = reinterpret_cast(data); @@ -171,7 +153,7 @@ void GetBundleArchiveInfoExec(napi_env env, void *data) APP_LOGE("asyncCallbackInfo is null"); return; } - asyncCallbackInfo->err = InnerGetBundleArchiveInfo( + asyncCallbackInfo->err = BundleManagerHelper::InnerGetBundleArchiveInfo( asyncCallbackInfo->hapFilePath, asyncCallbackInfo->flags, asyncCallbackInfo->bundleInfo); } @@ -2225,27 +2207,6 @@ napi_value IsAbilityEnabled(napi_env env, napi_callback_info info) return promise; } -static ErrCode InnerCleanBundleCacheCallback( - const std::string &bundleName, int32_t appIndex, const OHOS::sptr cleanCacheCallback) -{ - if (cleanCacheCallback == nullptr) { - APP_LOGE("callback nullptr"); - return ERROR_BUNDLE_SERVICE_EXCEPTION; - } - auto iBundleMgr = CommonFunc::GetBundleMgr(); - if (iBundleMgr == nullptr) { - APP_LOGE("can not get iBundleMgr"); - return ERROR_BUNDLE_SERVICE_EXCEPTION; - } - int32_t userId = IPCSkeleton::GetCallingUid() / Constants::BASE_USER_RANGE; - ErrCode result = iBundleMgr->CleanBundleCacheFiles(bundleName, cleanCacheCallback, userId, appIndex); - if (result != ERR_OK) { - APP_LOGE("call error, bundleName is %{public}s, userId is %{public}d, appIndex is %{public}d", - bundleName.c_str(), userId, appIndex); - } - return CommonFunc::ConvertErrCode(result); -} - void CleanBundleCacheFilesExec(napi_env env, void *data) { CleanBundleCacheCallbackInfo* asyncCallbackInfo = reinterpret_cast(data); @@ -2256,7 +2217,7 @@ void CleanBundleCacheFilesExec(napi_env env, void *data) if (asyncCallbackInfo->cleanCacheCallback == nullptr) { asyncCallbackInfo->cleanCacheCallback = new (std::nothrow) CleanCacheCallback(); } - asyncCallbackInfo->err = InnerCleanBundleCacheCallback( + asyncCallbackInfo->err = BundleManagerHelper::InnerCleanBundleCacheCallback( asyncCallbackInfo->bundleName, asyncCallbackInfo->appIndex, asyncCallbackInfo->cleanCacheCallback); if ((asyncCallbackInfo->err == NO_ERROR) && (asyncCallbackInfo->cleanCacheCallback != nullptr)) { // wait for OnCleanCacheFinished @@ -2282,7 +2243,7 @@ void CleanBundleCacheFilesComplete(napi_env env, napi_status status, void *data) NAPI_CALL_RETURN_VOID(env, napi_get_null(env, &result[0])); } else { result[0] = BusinessError::CreateCommonError(env, asyncCallbackInfo->err, - "CleanBundleCacheFiles", Constants::PERMISSION_REMOVECACHEFILE); + CLEAN_BUNDLE_CACHE_FILES, Constants::PERMISSION_REMOVECACHEFILE); } CommonFunc::NapiReturnDeferred(env, asyncCallbackInfo, result, ARGS_SIZE_ONE); } @@ -2938,60 +2899,6 @@ napi_value GetLaunchWantForBundle(napi_env env, napi_callback_info info) return promise; } -ErrCode GetAbilityFromBundleInfo(const BundleInfo& bundleInfo, const std::string& abilityName, - const std::string& moduleName, AbilityInfo& targetAbilityInfo) -{ - bool ifExists = false; - for (const auto& hapModuleInfo : bundleInfo.hapModuleInfos) { - for (const auto& abilityInfo : hapModuleInfo.abilityInfos) { - if (abilityInfo.name == abilityName && abilityInfo.moduleName == moduleName) { - if (!abilityInfo.enabled) { - APP_LOGI("ability disabled"); - return ERR_BUNDLE_MANAGER_ABILITY_DISABLED; - } - ifExists = true; - targetAbilityInfo = abilityInfo; - break; - } - } - if (ifExists) { - break; - } - } - if (!ifExists) { - APP_LOGE("ability not exist"); - return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST; - } - return ERR_OK; -} - -ErrCode GetExtensionFromBundleInfo(const BundleInfo& bundleInfo, const std::string& abilityName, - const std::string& moduleName, ExtensionAbilityInfo& targetExtensionInfo) -{ - bool ifExists = false; - for (const auto& hapModuleInfo : bundleInfo.hapModuleInfos) { - for (const auto& extensionInfo : hapModuleInfo.extensionInfos) { - if (extensionInfo.name == abilityName && extensionInfo.moduleName == moduleName) { - ifExists = true; - targetExtensionInfo = extensionInfo; - break; - } - if (!extensionInfo.enabled) { - APP_LOGI("extension disabled"); - return ERR_BUNDLE_MANAGER_ABILITY_DISABLED; - } - } - if (ifExists) { - break; - } - } - if (!ifExists) { - APP_LOGE("ability not exist"); - return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST; - } - return ERR_OK; -} - static ErrCode InnerGetProfile(GetProfileCallbackInfo &info) { auto iBundleMgr = CommonFunc::GetBundleMgr(); @@ -3024,7 +2931,7 @@ static ErrCode InnerGetProfile(GetProfileCallbackInfo &info) return result; } AbilityInfo targetAbilityInfo; - result = GetAbilityFromBundleInfo( + result = BundleManagerHelper::GetAbilityFromBundleInfo( bundleInfo, info.abilityName, info.moduleName, targetAbilityInfo); if (result != ERR_OK) { return result; @@ -3046,7 +2953,7 @@ static ErrCode InnerGetProfile(GetProfileCallbackInfo &info) } ExtensionAbilityInfo targetExtensionInfo; - result = GetExtensionFromBundleInfo( + result = BundleManagerHelper::GetExtensionFromBundleInfo( bundleInfo, info.abilityName, info.moduleName, targetExtensionInfo); if (result != ERR_OK) { return result; @@ -3537,17 +3444,6 @@ void CreateAppDistributionTypeObject(napi_env env, napi_value value) NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "NONE", nNone)); } -static ErrCode InnerGetPermissionDef(const std::string &permissionName, PermissionDef &permissionDef) -{ - auto iBundleMgr = CommonFunc::GetBundleMgr(); - if (iBundleMgr == nullptr) { - APP_LOGE("can not get iBundleMgr"); - return ERROR_BUNDLE_SERVICE_EXCEPTION; - } - ErrCode ret = iBundleMgr->GetPermissionDef(permissionName, permissionDef); - return CommonFunc::ConvertErrCode(ret); -} - void GetPermissionDefExec(napi_env env, void *data) { AsyncPermissionDefineCallbackInfo *asyncCallbackInfo = reinterpret_cast(data); @@ -3556,7 +3452,7 @@ void GetPermissionDefExec(napi_env env, void *data) return; } if (asyncCallbackInfo->err == NO_ERROR) { - asyncCallbackInfo->err = InnerGetPermissionDef(asyncCallbackInfo->permissionName, + asyncCallbackInfo->err = BundleManagerHelper::InnerGetPermissionDef(asyncCallbackInfo->permissionName, asyncCallbackInfo->permissionDef); } } diff --git a/interfaces/kits/js/bundle_manager/bundle_manager_helper.cpp b/interfaces/kits/js/bundle_manager/bundle_manager_helper.cpp index 6b8583ca7c20b54e1890a2b6c5c7b7e8dad4f9ff..5baf61d21dbc9bbeca38bda8ada7299a0a1e6d56 100644 --- a/interfaces/kits/js/bundle_manager/bundle_manager_helper.cpp +++ b/interfaces/kits/js/bundle_manager/bundle_manager_helper.cpp @@ -21,6 +21,7 @@ #include "bundle_errors.h" #include "business_error.h" #include "common_func.h" +#include "ipc_skeleton.h" namespace OHOS { namespace AppExecFwk { @@ -119,5 +120,104 @@ ErrCode BundleManagerHelper::InnerEnableDynamicIcon( return CommonFunc::ConvertErrCode(ret); } + +ErrCode BundleManagerHelper::InnerGetBundleArchiveInfo(std::string& hapFilePath, int32_t flags, BundleInfo& bundleInfo) +{ + auto iBundleMgr = CommonFunc::GetBundleMgr(); + if (iBundleMgr == nullptr) { + APP_LOGE("iBundleMgr is null"); + return ERROR_BUNDLE_SERVICE_EXCEPTION; + } + ErrCode ret = iBundleMgr->GetBundleArchiveInfoV9(hapFilePath, flags, bundleInfo); + APP_LOGI("GetBundleArchiveInfoV9 ErrCode : %{public}d", ret); + return CommonFunc::ConvertErrCode(ret); +} + +ErrCode BundleManagerHelper::GetAbilityFromBundleInfo(const BundleInfo& bundleInfo, const std::string& abilityName, + const std::string& moduleName, AbilityInfo& targetAbilityInfo) +{ + bool ifExists = false; + for (const auto& hapModuleInfo : bundleInfo.hapModuleInfos) { + for (const auto& abilityInfo : hapModuleInfo.abilityInfos) { + if (abilityInfo.name == abilityName && abilityInfo.moduleName == moduleName) { + if (!abilityInfo.enabled) { + APP_LOGI("ability disabled"); + return ERR_BUNDLE_MANAGER_ABILITY_DISABLED; + } + ifExists = true; + targetAbilityInfo = abilityInfo; + break; + } + } + if (ifExists) { + break; + } + } + if (!ifExists) { + APP_LOGE("ability not exist"); + return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST; + } + return ERR_OK; +} + +ErrCode BundleManagerHelper::GetExtensionFromBundleInfo(const BundleInfo& bundleInfo, const std::string& abilityName, + const std::string& moduleName, ExtensionAbilityInfo& targetExtensionInfo) +{ + bool ifExists = false; + for (const auto& hapModuleInfo : bundleInfo.hapModuleInfos) { + for (const auto& extensionInfo : hapModuleInfo.extensionInfos) { + if (extensionInfo.name == abilityName && extensionInfo.moduleName == moduleName) { + ifExists = true; + targetExtensionInfo = extensionInfo; + break; + } + if (!extensionInfo.enabled) { + APP_LOGI("extension disabled"); + return ERR_BUNDLE_MANAGER_ABILITY_DISABLED; + } + } + if (ifExists) { + break; + } + } + if (!ifExists) { + APP_LOGE("ability not exist"); + return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST; + } + return ERR_OK; +} + +ErrCode BundleManagerHelper::InnerGetPermissionDef(const std::string& permissionName, PermissionDef& permissionDef) +{ + auto iBundleMgr = CommonFunc::GetBundleMgr(); + if (iBundleMgr == nullptr) { + APP_LOGE("can not get iBundleMgr"); + return ERROR_BUNDLE_SERVICE_EXCEPTION; + } + ErrCode ret = iBundleMgr->GetPermissionDef(permissionName, permissionDef); + APP_LOGI("GetPermissionDef ErrCode : %{public}d", ret); + return CommonFunc::ConvertErrCode(ret); +} + +ErrCode BundleManagerHelper::InnerCleanBundleCacheCallback( + const std::string &bundleName, int32_t appIndex, const OHOS::sptr cleanCacheCallback) +{ + if (cleanCacheCallback == nullptr) { + APP_LOGE("callback nullptr"); + return ERROR_BUNDLE_SERVICE_EXCEPTION; + } + auto iBundleMgr = CommonFunc::GetBundleMgr(); + if (iBundleMgr == nullptr) { + APP_LOGE("can not get iBundleMgr"); + return ERROR_BUNDLE_SERVICE_EXCEPTION; + } + int32_t userId = IPCSkeleton::GetCallingUid() / Constants::BASE_USER_RANGE; + ErrCode result = iBundleMgr->CleanBundleCacheFiles(bundleName, cleanCacheCallback, userId, appIndex); + if (result != ERR_OK) { + APP_LOGE("call error, bundleName is %{public}s, userId is %{public}d, appIndex is %{public}d", + bundleName.c_str(), userId, appIndex); + } + return CommonFunc::ConvertErrCode(result); +} } // AppExecFwk } // OHOS \ No newline at end of file diff --git a/interfaces/kits/js/bundle_manager/bundle_manager_helper.h b/interfaces/kits/js/bundle_manager/bundle_manager_helper.h index 64fa6c69c50bb5819e82c937e79fee9e5daa7ffa..d4e2e0524b6d245f22b02e90c6c95c4f8b741bbb 100644 --- a/interfaces/kits/js/bundle_manager/bundle_manager_helper.h +++ b/interfaces/kits/js/bundle_manager/bundle_manager_helper.h @@ -19,6 +19,7 @@ #include "bundle_errors.h" #include "bundle_mgr_interface.h" #include "bundle_mgr_proxy.h" +#include "clean_cache_callback.h" #include "common_func.h" namespace OHOS { @@ -32,6 +33,14 @@ public: static ErrCode InnerSetAbilityEnabled(const AbilityInfo &abilityInfo, bool &isEnable, int32_t appIndex); static ErrCode InnerSetApplicationEnabled(const std::string &bundleName, bool &isEnable, int32_t appIndex); static ErrCode InnerEnableDynamicIcon(const std::string &bundleName, const std::string &moduleName); + static ErrCode InnerGetBundleArchiveInfo(std::string& hapFilePath, int32_t flags, BundleInfo& bundleInfo); + static ErrCode GetAbilityFromBundleInfo(const BundleInfo& bundleInfo, const std::string& abilityName, + const std::string& moduleName, AbilityInfo& targetAbilityInfo); + static ErrCode GetExtensionFromBundleInfo(const BundleInfo& bundleInfo, const std::string& abilityName, + const std::string& moduleName, ExtensionAbilityInfo& targetExtensionInfo); + static ErrCode InnerGetPermissionDef(const std::string& permissionName, PermissionDef& permissionDef); + static ErrCode InnerCleanBundleCacheCallback( + const std::string &bundleName, int32_t appIndex, const OHOS::sptr cleanCacheCallback); }; } // AppExecFwk } // OHOS diff --git a/interfaces/kits/js/bundle_manager/bundle_manager_sync.cpp b/interfaces/kits/js/bundle_manager/bundle_manager_sync.cpp index 4b68844dc5fa1e7eafcec00a7cdc680787e01a02..77e8e6e1d22b5fce9fe0b88958e21530e125a211 100644 --- a/interfaces/kits/js/bundle_manager/bundle_manager_sync.cpp +++ b/interfaces/kits/js/bundle_manager/bundle_manager_sync.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -33,7 +33,6 @@ namespace OHOS { namespace AppExecFwk { constexpr const char* ABILITY_NAME = "abilityName"; constexpr const char* IS_ENABLE = "isEnable"; -constexpr const char* HAP_FILE_PATH = "hapFilePath"; constexpr const char* UID = "uid"; constexpr const char* EXTENSIONABILITY_TYPE = "extensionAbilityType"; const char* GET_ABILITY_LABEL_SYNC = "GetAbilityLabelSync"; @@ -45,7 +44,6 @@ const char* GET_PERMISSION_DEF_SYNC = "GetPermissionDefSync"; const char* GET_APP_PROVISION_INFO_SYNC = "GetAppProvisionInfoSync"; const char* GET_SIGNATURE_INFO_SYNC = "GetSignatureInfoSync"; const char* GET_SIGNATURE_INFO_PERMISSIONS = "ohos.permission.GET_SIGNATURE_INFO"; -const char* PERMISSION_NAME = "permissionName"; bool ParseWantWithParameter(napi_env env, napi_value args, Want &want) { napi_valuetype valueType; diff --git a/interfaces/kits/js/bundlemgr/bundle_mgr.cpp b/interfaces/kits/js/bundlemgr/bundle_mgr.cpp index 3a79aac6ea3982ba7315fd70772778af356176fa..ce3d5ae13defe55dae0aa0492c97b05ca4f96b53 100644 --- a/interfaces/kits/js/bundlemgr/bundle_mgr.cpp +++ b/interfaces/kits/js/bundlemgr/bundle_mgr.cpp @@ -102,11 +102,9 @@ enum class InstallErrorCode : uint8_t { const char* IS_SET_APPLICATION_ENABLED = "IsSetApplicationEnabled"; const char* IS_ABILITY_ENABLED = "IsAbilityEnabled"; const char* GET_LAUNCH_WANT_FOR_BUNDLE = "GetLaunchWantForBundle"; -const char* GET_BUNDLE_ARCHIVE_INFO = "GetBundleArchiveInfo"; const char* GET_ABILITY_ICON = "GetAbilityIcon"; constexpr const char* NAPI_GET_APPLICATION_INFO = "GetApplicationInfo"; const char* GET_ALL_BUNDLE_INFO = "GetAllBundleInfo"; -const char* GET_PERMISSION_DEF = "GetPermissionDef"; const char* QUERY_ABILITY_BY_WANT = "queryAbilityByWant"; const char* TYPE_MISMATCH = "type misMatch"; diff --git a/interfaces/kits/js/common/napi_constants.h b/interfaces/kits/js/common/napi_constants.h index bea89233ee7f6aa8ebd09761c3630223d81c7a94..b998e94fd7a0b09f230f8c4ddf4f578260cc30f5 100644 --- a/interfaces/kits/js/common/napi_constants.h +++ b/interfaces/kits/js/common/napi_constants.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -50,6 +50,8 @@ constexpr const char* ABILITY_INFO = "abilityInfo"; constexpr const char* LINK_FEATURE = "linkFeature"; constexpr const char* EXTENSION_TYPE_NAME = "extensionTypeName"; constexpr const char* EXTENSION_ABILITY_TYPE = "extensionAbilityType"; +constexpr const char* HAP_FILE_PATH = "hapFilePath"; +constexpr const char* PERMISSION_NAME = "permissionName"; constexpr const char* ERR_MSG_BUNDLE_SERVICE_EXCEPTION = "Bundle manager service is excepted."; constexpr const char* PARAM_EXTENSION_ABILITY_TYPE_EMPTY_ERROR = "BusinessError 401: Parameter error.Parameter extensionAbilityType is empty."; @@ -59,6 +61,7 @@ constexpr const char* INVALID_WANT_ERROR = "implicit query condition, at least one query param(action, entities, uri, type, or linkFeature) non-empty."; constexpr const char* PARAM_TYPE_CHECK_ERROR = "param type check error"; constexpr const char* APP_CLONE_IDENTITY_PERMISSIONS = "ohos.permission.GET_BUNDLE_INFO_PRIVILEGED"; +constexpr const char* ERR_MSG_LAUNCH_WANT_INVALID = "The launch want is not found."; constexpr const char* IS_APPLICATION_ENABLED_SYNC = "IsApplicationEnabledSync"; constexpr const char* GET_BUNDLE_INFO_FOR_SELF_SYNC = "GetBundleInfoForSelfSync"; constexpr const char* GET_BUNDLE_INFO_SYNC = "GetBundleInfoSync"; @@ -132,6 +135,13 @@ constexpr const char* UNINSTALL_DISPOSED_RULE_TYPE = "UninstallDisposedRule"; constexpr const char* SET_UNINSTALL_DISPOSED_RULE = "SetUninstallDisposedRule"; constexpr const char* DELETE_UNINSTALL_DISPOSED_RULE = "DeleteUninstallDisposedRule"; constexpr const char* GET_UNINSTALL_DISPOSED_RULE = "GetUninstallDisposedRule"; +constexpr const char* GET_BUNDLE_ARCHIVE_INFO = "GetBundleArchiveInfo"; +constexpr const char* GET_PROFILE_BY_EXTENSION_ABILITY = "GetProfileByExtensionAbility"; +constexpr const char* GET_PROFILE_BY_ABILITY = "GetProfileByAbility"; +constexpr const char* GET_PERMISSION_DEF = "GetPermissionDef"; +constexpr const char* CLEAN_BUNDLE_CACHE_FILES = "cleanBundleCacheFiles"; +constexpr const char* GET_ALL_BUNDLE_CACHE_SIZE = "getAllBundleCacheSize"; +constexpr const char* CLEAN_ALL_BUNDLE_CACHE = "cleanAllBundleCache"; } } }