diff --git a/frameworks/native/backup_ext/BUILD.gn b/frameworks/native/backup_ext/BUILD.gn index 1a0f4628b66f24eb028f77bed42031c87760f411..d2111460c0a91bcba91ce51de4591484a6e63845 100644 --- a/frameworks/native/backup_ext/BUILD.gn +++ b/frameworks/native/backup_ext/BUILD.gn @@ -29,6 +29,7 @@ ohos_shared_library("backup_extension_ability_native") { sources = [ "src/ext_backup.cpp", "src/ext_backup_ani.cpp", + "src/ext_backup_ani_error.cpp", "src/ext_backup_context.cpp", "src/ext_backup_context_js.cpp", "src/ext_backup_js.cpp", diff --git a/frameworks/native/backup_ext/ani/ets/@ohos.application.BackupExtensionAbility.ets b/frameworks/native/backup_ext/ani/ets/@ohos.application.BackupExtensionAbility.ets index b5aedba5e124a7bda30bef2560ace07d33542117..2941755b9a0310aa5b132925b55900e2b583c66c 100644 --- a/frameworks/native/backup_ext/ani/ets/@ohos.application.BackupExtensionAbility.ets +++ b/frameworks/native/backup_ext/ani/ets/@ohos.application.BackupExtensionAbility.ets @@ -21,7 +21,7 @@ export interface BundleVersion { export class BundleVersionInner implements BundleVersion { public code: number; - public name: string; + public name: string = ''; } export default abstract class BackupExtensionAbility { diff --git a/frameworks/native/backup_ext/include/ext_backup_ani.h b/frameworks/native/backup_ext/include/ext_backup_ani.h index 7e2aa923737c684400905654f24c5ab4a4b37a15..b6accd2bc3b1f9d5969dc4b0d9a1769bb7ad2c7c 100644 --- a/frameworks/native/backup_ext/include/ext_backup_ani.h +++ b/frameworks/native/backup_ext/include/ext_backup_ani.h @@ -16,8 +16,8 @@ #ifndef EXT_BACKUP_ANI_H #define EXT_BACKUP_ANI_H -#include "ext_backup_context.h" #include "ext_backup.h" +#include "ext_backup_context.h" #include "runtime.h" #include "sts_runtime.h" diff --git a/frameworks/native/backup_ext/include/ext_backup_ani_error.h b/frameworks/native/backup_ext/include/ext_backup_ani_error.h new file mode 100644 index 0000000000000000000000000000000000000000..d1a6a6905c88338e779232a6394f868bac064c08 --- /dev/null +++ b/frameworks/native/backup_ext/include/ext_backup_ani_error.h @@ -0,0 +1,43 @@ +/* + * 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. + */ + +#ifndef EXT_ANI_ERROR_H +#define EXT_ANI_ERROR_H + +#include +#include + +namespace OHOS { + +class AniError { +public: + enum errorCode : int { EXT_BROKEN_FRAMEWORK = 0x5001 }; + +public: + AniError() = default; + + static void ThrowBusinessError(ani_env *env, errorCode errCode, const std::string &errMsg); + + template + static void Assert(const T &t, ani_env *env, errorCode code, const std::string &errMsg) + { + if (!t) { + AniError::ThrowBusinessError(env, code, errMsg); + } + } +}; // AniError + +} // namespace OHOS +#endif // EXT_ANI_ERROR_H diff --git a/frameworks/native/backup_ext/src/ext_backup_ani.cpp b/frameworks/native/backup_ext/src/ext_backup_ani.cpp index 8597442651bc413bc7d44eb84f3cbf05eb2ea9b5..ad8d0530713de60c5da6a09ccfdb440e8295c4ba 100644 --- a/frameworks/native/backup_ext/src/ext_backup_ani.cpp +++ b/frameworks/native/backup_ext/src/ext_backup_ani.cpp @@ -14,10 +14,10 @@ */ #include "ext_backup_ani.h" - #include "ani_utils.h" #include "b_error/b_error.h" #include "b_error/b_excep_utils.h" +#include "ext_backup_ani_error.h" #include "filemgmt_libhilog.h" namespace OHOS::FileManagement::Backup { @@ -131,8 +131,9 @@ ErrCode ExtBackupAni::OnBackup(function callback, std::function callbackEx) { HILOGI("ExtBackupAni OnBackup"); - BExcepUltils::BAssert(etsObj_, BError::Codes::EXT_BROKEN_FRAMEWORK, - "The app does not provide the onBackup interface."); + auto env = stsRuntime_.GetAniEnv(); + AniError::Assert(etsObj_, env, AniError::errorCode::EXT_BROKEN_FRAMEWORK, + "The app does not provide the onBackup interface."); return CallEtsOnBackup(); } @@ -140,8 +141,9 @@ ErrCode ExtBackupAni::OnRestore(std::function callba std::function callbackEx) { HILOGI("ExtBackupAni OnRestore"); - BExcepUltils::BAssert(etsObj_, BError::Codes::EXT_BROKEN_FRAMEWORK, - "The app does not provide the OnRestore interface."); + auto env = stsRuntime_.GetAniEnv(); + AniError::Assert(etsObj_, env, AniError::errorCode::EXT_BROKEN_FRAMEWORK, + "The app does not provide the onRestore interface."); return CallEtsOnRestore(); } diff --git a/frameworks/native/backup_ext/src/ext_backup_ani_error.cpp b/frameworks/native/backup_ext/src/ext_backup_ani_error.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bd72c18a916cb9aab3eddfeee313a0403ebb0746 --- /dev/null +++ b/frameworks/native/backup_ext/src/ext_backup_ani_error.cpp @@ -0,0 +1,62 @@ +/* + * 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. + */ + +#include "ext_backup_ani_error.h" +#include "filemgmt_libhilog.h" + +namespace OHOS { + +void AniError::ThrowBusinessError(ani_env *env, errorCode errCode, const std::string &errMsg) +{ + static const char *errorClsName = "L@ohos/base/BusinessError;"; + HILOGD("Begin ThrowBusinessError."); + + ani_class cls {}; + if (ANI_OK != env->FindClass(errorClsName, &cls)) { + HILOGE("find class BusinessError %{public}s failed", errorClsName); + return; + } + + ani_method ctor; + if (ANI_OK != env->Class_FindMethod(cls, "", ":V", &ctor)) { + HILOGE("find method BusinessError.constructor failed"); + return; + } + + ani_object errorObject; + if (ANI_OK != env->Object_New(cls, ctor, &errorObject)) { + HILOGE("create BusinessError object failed"); + return; + } + + ani_double aniErrCode = static_cast(errCode); + ani_string errMsgStr; + if (ANI_OK != env->String_NewUTF8(errMsg.c_str(), errMsg.size(), &errMsgStr)) { + HILOGE("convert errMsg to ani_string failed"); + } + + HILOGD("ThrowBusinessError: errMsg: %{public}s.", errMsg.c_str()); + if (ANI_OK != env->Object_SetFieldByName_Double(errorObject, "code", aniErrCode)) { + HILOGE("set error code failed"); + } + + if (ANI_OK != env->Object_SetPropertyByName_Ref(errorObject, "message", errMsgStr)) { + HILOGE("set error message failed"); + } + env->ThrowError(static_cast(errorObject)); + return; +} + +} // namespace OHOS \ No newline at end of file diff --git a/test/fuzztest/backupext_fuzzer/BUILD.gn b/test/fuzztest/backupext_fuzzer/BUILD.gn index 4b68f3461e23eacdc09700e81760c9004a151e18..0f14511a82fa55c5388f6a0e655d6fa507816266 100644 --- a/test/fuzztest/backupext_fuzzer/BUILD.gn +++ b/test/fuzztest/backupext_fuzzer/BUILD.gn @@ -37,6 +37,7 @@ ohos_fuzztest("BackupExtFuzzTest") { sources = [ "${path_backup}/frameworks/native/backup_ext/src/ext_backup.cpp", "${path_backup}/frameworks/native/backup_ext/src/ext_backup_ani.cpp", + "${path_backup}/frameworks/native/backup_ext/src/ext_backup_ani_error.cpp", "${path_backup}/frameworks/native/backup_ext/src/ext_backup_context.cpp", "${path_backup}/frameworks/native/backup_ext/src/ext_backup_context_js.cpp", "${path_backup}/frameworks/native/backup_ext/src/ext_backup_js.cpp", diff --git a/tests/unittests/backup_ext/BUILD.gn b/tests/unittests/backup_ext/BUILD.gn index 5fd0ef93d38865fb9dfc9fcbdfbb608384d17e6c..604dc434cd467290e87b4f4ad0f8c7b961d5f915 100644 --- a/tests/unittests/backup_ext/BUILD.gn +++ b/tests/unittests/backup_ext/BUILD.gn @@ -135,6 +135,7 @@ ohos_unittest("tar_file_test") { sources = [ "${path_backup}/frameworks/native/backup_ext/src/ext_backup.cpp", "${path_backup}/frameworks/native/backup_ext/src/ext_backup_ani.cpp", + "${path_backup}/frameworks/native/backup_ext/src/ext_backup_ani_error.cpp", "${path_backup}/frameworks/native/backup_ext/src/ext_backup_context.cpp", "${path_backup}/frameworks/native/backup_ext/src/ext_backup_context_js.cpp", "${path_backup}/frameworks/native/backup_ext/src/ext_backup_js.cpp", @@ -212,6 +213,7 @@ ohos_unittest("untar_file_sup_test") { sources = [ "${path_backup}/frameworks/native/backup_ext/src/ext_backup.cpp", "${path_backup}/frameworks/native/backup_ext/src/ext_backup_ani.cpp", + "${path_backup}/frameworks/native/backup_ext/src/ext_backup_ani_error.cpp", "${path_backup}/frameworks/native/backup_ext/src/ext_backup_context.cpp", "${path_backup}/frameworks/native/backup_ext/src/ext_backup_context_js.cpp", "${path_backup}/frameworks/native/backup_ext/src/ext_backup_js.cpp", @@ -290,6 +292,7 @@ ohos_unittest("untar_file_test") { sources = [ "${path_backup}/frameworks/native/backup_ext/src/ext_backup.cpp", "${path_backup}/frameworks/native/backup_ext/src/ext_backup_ani.cpp", + "${path_backup}/frameworks/native/backup_ext/src/ext_backup_ani_error.cpp", "${path_backup}/frameworks/native/backup_ext/src/ext_backup_context.cpp", "${path_backup}/frameworks/native/backup_ext/src/ext_backup_context_js.cpp", "${path_backup}/frameworks/native/backup_ext/src/ext_backup_js.cpp",