From ddb3e7305b9822c4d3d01190554e088ff95a412c Mon Sep 17 00:00:00 2001 From: lanhaoyu Date: Thu, 28 Aug 2025 12:07:16 +0800 Subject: [PATCH] GetLaunchWantForBundle add empty check Signed-off-by: lanhaoyu --- .../bundle_installer/ani_bundle_installer.cpp | 6 ++--- .../ets/@ohos.bundle.bundleManager.ets | 23 +++++++++++++++++++ .../kits/js/bundle_manager/bundle_manager.cpp | 4 ++-- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/interfaces/kits/ani/bundle_installer/ani_bundle_installer.cpp b/interfaces/kits/ani/bundle_installer/ani_bundle_installer.cpp index 14a331bcd4..5fbe484511 100644 --- a/interfaces/kits/ani/bundle_installer/ani_bundle_installer.cpp +++ b/interfaces/kits/ani/bundle_installer/ani_bundle_installer.cpp @@ -98,6 +98,9 @@ static bool CheckInstallParam(ani_env* env, InstallParam& installParam) static void ExecuteInstall(const std::vector& hapFiles, InstallParam& installParam, InstallResult& installResult) { + if (installParam.installFlag == InstallFlag::NORMAL) { + installParam.installFlag = InstallFlag::REPLACE_EXISTING; + } if (hapFiles.empty() && installParam.sharedBundleDirPaths.empty()) { installResult.resultCode = static_cast(IStatusReceiver::ERR_INSTALL_FILE_PATH_INVALID); return; @@ -219,9 +222,6 @@ static void AniInstall(ani_env* env, [[maybe_unused]] ani_object installerObj, if (!GetInstallParamForInstall(env, arrayObj, aniInstParam, hapFiles, installParam)) { return; } - if (installParam.installFlag == InstallFlag::NORMAL) { - installParam.installFlag = InstallFlag::REPLACE_EXISTING; - } InstallResult result; ExecuteInstall(hapFiles, installParam, result); ProcessResult(env, result, InstallOption::INSTALL); 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 7da70be20b..5f247d147f 100644 --- a/interfaces/kits/ani/bundle_manager/ets/@ohos.bundle.bundleManager.ets +++ b/interfaces/kits/ani/bundle_manager/ets/@ohos.bundle.bundleManager.ets @@ -25,6 +25,17 @@ namespace bundleManager { loadLibrary("ani_bundle_manager.z"); const EMPTY_USER_ID: number = -500; + const ERROR_PARAM_CHECK_ERROR: int = 401; + const PARAM_BUNDLENAME_TYPE_ERROR: string = "BusinessError 401: Parameter error. The type of bundleName must be string."; + const PARAM_TYPE_CHECK_ERROR: string = "param type check error"; + + function createBusinessError(code: int, message: string) { + let err = new BusinessError(); + err.code = code; + err.name = 'Error'; + err.message = message; + return err; + } enum BundleFlag { GET_BUNDLE_INFO_DEFAULT = 0x00000000, @@ -250,6 +261,9 @@ namespace bundleManager { } function getLaunchWantForBundleSync(bundleName: string, userId?: number): Want { + if (bundleName === "") { + throw createBusinessError(ERROR_PARAM_CHECK_ERROR, PARAM_TYPE_CHECK_ERROR); + } let userIdInfo: number = userId ?? EMPTY_USER_ID; return bundleManager.getLaunchWantForBundleNative(bundleName, userIdInfo, true); } @@ -657,6 +671,9 @@ namespace bundleManager { } function getLaunchWantForBundle(bundleName: string, userId?: number): Promise { + if (bundleName === "") { + throw createBusinessError(ERROR_PARAM_CHECK_ERROR, PARAM_BUNDLENAME_TYPE_ERROR); + } let p = new Promise(( resolve: (want: Want) => void, reject: (error: BusinessError) => void) => { let userIdInfo: number = userId ?? EMPTY_USER_ID; @@ -676,6 +693,9 @@ namespace bundleManager { } function getLaunchWantForBundle(bundleName: string, userId: number, callback: AsyncCallback): void { + if (bundleName === "") { + throw createBusinessError(ERROR_PARAM_CHECK_ERROR, PARAM_BUNDLENAME_TYPE_ERROR); + } let execFun = (): Want => { return bundleManager.getLaunchWantForBundleNative(bundleName, userId, false); }; @@ -689,6 +709,9 @@ namespace bundleManager { } function getLaunchWantForBundle(bundleName: string, callback: AsyncCallback): void { + if (bundleName === "") { + throw createBusinessError(ERROR_PARAM_CHECK_ERROR, PARAM_BUNDLENAME_TYPE_ERROR); + } let execFun = (): Want => { return bundleManager.getLaunchWantForBundleNative(bundleName, EMPTY_USER_ID, false); }; diff --git a/interfaces/kits/js/bundle_manager/bundle_manager.cpp b/interfaces/kits/js/bundle_manager/bundle_manager.cpp index d590f91bde..cc88918010 100644 --- a/interfaces/kits/js/bundle_manager/bundle_manager.cpp +++ b/interfaces/kits/js/bundle_manager/bundle_manager.cpp @@ -2866,12 +2866,12 @@ napi_value GetLaunchWantForBundle(napi_env env, napi_callback_info info) if (i == ARGS_POS_ZERO) { if (valueType != napi_string) { APP_LOGE("GetLaunchWantForBundle bundleName is not a string"); - BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, BUNDLE_NAME, STRING_TYPE); + BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, BUNDLE_NAME, TYPE_STRING); return nullptr; } CommonFunc::ParseString(env, args[i], asyncCallbackInfo->bundleName); if (asyncCallbackInfo->bundleName.size() == 0) { - BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, BUNDLE_NAME, STRING_TYPE); + BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, BUNDLE_NAME, TYPE_STRING); return nullptr; } } else if (i == ARGS_POS_ONE) { -- Gitee