diff --git a/frameworks/native/backup_ext/include/ext_backup_js.h b/frameworks/native/backup_ext/include/ext_backup_js.h index 78b17af5873d10e15939071ba8e4c376944951b5..2d41756ce25f6bfc985006080f3916a92d2b8f29 100644 --- a/frameworks/native/backup_ext/include/ext_backup_js.h +++ b/frameworks/native/backup_ext/include/ext_backup_js.h @@ -29,6 +29,7 @@ #include "want.h" namespace OHOS::FileManagement::Backup { +using InputArgsParser = std::function &)>; using ResultValueParser = std::function; struct CallJsParam { @@ -38,13 +39,17 @@ struct CallJsParam { std::string funcName; AbilityRuntime::JsRuntime *jsRuntime; NativeReference *jsObj; - const std::vector &argv; + InputArgsParser argParser; ResultValueParser retParser; - CallJsParam(const std::string &funcNameIn, AbilityRuntime::JsRuntime *jsRuntimeIn, NativeReference *jsObjIn, - const std::vector &argvIn, ResultValueParser &retParserIn) - : funcName(funcNameIn), jsRuntime(jsRuntimeIn), jsObj(jsObjIn), argv(argvIn), retParser(retParserIn) - {} + CallJsParam(const std::string &funcNameIn, + AbilityRuntime::JsRuntime *jsRuntimeIn, + NativeReference *jsObjIn, + InputArgsParser &argParserIn, + ResultValueParser &retParserIn) + : funcName(funcNameIn), jsRuntime(jsRuntimeIn), jsObj(jsObjIn), argParser(argParserIn), retParser(retParserIn) + { + } }; class ExtBackupJs : public ExtBackup { @@ -92,7 +97,7 @@ private: int CallJsMethod(const std::string &funcName, AbilityRuntime::JsRuntime &jsRuntime, NativeReference *jsObj, - const std::vector &argv, + InputArgsParser argParser, ResultValueParser retParser); std::tuple CallObjectMethod(std::string_view name, const std::vector &argv = {}); diff --git a/frameworks/native/backup_ext/src/ext_backup_js.cpp b/frameworks/native/backup_ext/src/ext_backup_js.cpp index f2e602382e2631488946ed9e1adc5a0205b66716..cf34fa574bac9894566aa81f87ea27e614e545b8 100644 --- a/frameworks/native/backup_ext/src/ext_backup_js.cpp +++ b/frameworks/native/backup_ext/src/ext_backup_js.cpp @@ -130,17 +130,11 @@ ExtBackupJs *ExtBackupJs::Create(const unique_ptr &runt ErrCode ExtBackupJs::OnBackup(void) { HILOGI("BackupExtensionAbility(JS) OnBackup."); - if (!jsObj_) { - HILOGI("The app does not provide the onBackup interface."); - return ERR_OK; - } - auto ret = std::make_shared(); - auto retParser = [ret](NativeEngine &engine, NativeValue *result) -> bool { - bool res = AbilityRuntime::ConvertFromJsValue(engine, result, *ret); - if (!res) { - HILOG_ERROR("Convert js value fail."); - } - return res; + BExcepUltils::BAssert(jsObj_, BError::Codes::EXT_BROKEN_FRAMEWORK, + "The app does not provide the onRestore interface."); + + auto retParser = [](NativeEngine &engine, NativeValue *result) -> bool { + return true; }; auto errCode = CallJsMethod("onBackup", jsRuntime_, jsObj_.get(), {}, retParser); @@ -148,50 +142,36 @@ ErrCode ExtBackupJs::OnBackup(void) HILOGE("CallJsMethod error, code:%{public}d.", errCode); return errCode; } - - if (*ret != ERR_OK) { - HILOGE("backup fail."); - return *ret; - } return ERR_OK; } ErrCode ExtBackupJs::OnRestore(void) { HILOGI("BackupExtensionAbility(JS) OnRestore."); - if (!jsObj_) { - HILOGI("The app does not provide the onRestore interface."); - return ERR_OK; - } - auto ret = std::make_shared(); - vector argv; - NativeValue *verCode = jsRuntime_.GetNativeEngine().CreateNumber(appVersionCode_); - NativeValue *verStr = jsRuntime_.GetNativeEngine().CreateString(appVersionStr_.c_str(), appVersionStr_.length()); + BExcepUltils::BAssert(jsObj_, BError::Codes::EXT_BROKEN_FRAMEWORK, + "The app does not provide the onRestore interface."); - NativeValue *param = jsRuntime_.GetNativeEngine().CreateObject(); - auto paramObj = reinterpret_cast(param->GetInterface(NativeObject::INTERFACE_ID)); - paramObj->SetProperty("code", verCode); - paramObj->SetProperty("name", verStr); - argv.push_back(param); + auto argParser = [appVersionCode(appVersionCode_), + appVersionStr(appVersionStr_)](NativeEngine &engine, vector &argv) -> bool { + NativeValue *verCode = engine.CreateNumber(appVersionCode); + NativeValue *verStr = engine.CreateString(appVersionStr.c_str(), appVersionStr.length()); + NativeValue *param = engine.CreateObject(); + auto paramObj = reinterpret_cast(param->GetInterface(NativeObject::INTERFACE_ID)); + paramObj->SetProperty("code", verCode); + paramObj->SetProperty("name", verStr); + argv.push_back(param); + return true; + }; - auto retParser = [ret](NativeEngine &engine, NativeValue *result) -> bool { - bool res = AbilityRuntime::ConvertFromJsValue(engine, result, *ret); - if (!res) { - HILOG_ERROR("Convert js value fail."); - } - return res; + auto retParser = [](NativeEngine &engine, NativeValue *result) -> bool { + return true; }; - auto errCode = CallJsMethod("onRestore", jsRuntime_, jsObj_.get(), argv, retParser); + auto errCode = CallJsMethod("onRestore", jsRuntime_, jsObj_.get(), argParser, retParser); if (errCode != ERR_OK) { HILOGE("CallJsMethod error, code:%{public}d.", errCode); return errCode; } - - if (*ret != ERR_OK) { - HILOGE("backup fail."); - return *ret; - } return ERR_OK; } @@ -204,6 +184,13 @@ static int DoCallJsMethod(CallJsParam *param) } AbilityRuntime::HandleEscape handleEscape(*jsRuntime); auto &nativeEngine = jsRuntime->GetNativeEngine(); + vector argv = {}; + if (param->argParser != nullptr) { + if (!param->argParser(nativeEngine, argv)) { + HILOGE("failed to get params."); + return EINVAL; + } + } NativeValue *value = param->jsObj->Get(); if (value == nullptr) { HILOGE("failed to get native value object."); @@ -223,8 +210,8 @@ static int DoCallJsMethod(CallJsParam *param) HILOGE("ResultValueParser must not null."); return EINVAL; } - if (!param->retParser(nativeEngine, handleEscape.Escape(nativeEngine.CallFunction(value, method, param->argv.data(), - param->argv.size())))) { + if (!param->retParser(nativeEngine, + handleEscape.Escape(nativeEngine.CallFunction(value, method, argv.data(), argv.size())))) { HILOGI("Parser js result fail."); return EINVAL; } @@ -234,7 +221,7 @@ static int DoCallJsMethod(CallJsParam *param) int ExtBackupJs::CallJsMethod(const std::string &funcName, AbilityRuntime::JsRuntime &jsRuntime, NativeReference *jsObj, - const std::vector &argv, + InputArgsParser argParser, ResultValueParser retParser) { uv_loop_s *loop = nullptr; @@ -243,16 +230,12 @@ int ExtBackupJs::CallJsMethod(const std::string &funcName, HILOGE("failed to get uv event loop."); return EINVAL; } - auto param = std::make_shared(funcName, &jsRuntime, jsObj, argv, retParser); - if (param == nullptr) { - HILOGE("failed to new param."); - return EINVAL; - } + auto param = std::make_shared(funcName, &jsRuntime, jsObj, argParser, retParser); + BExcepUltils::BAssert(param, BError::Codes::EXT_BROKEN_FRAMEWORK, "failed to new param."); + auto work = std::make_shared(); - if (work == nullptr) { - HILOGE("failed to new uv_work_t."); - return EINVAL; - } + BExcepUltils::BAssert(work, BError::Codes::EXT_BROKEN_FRAMEWORK, "failed to new uv_work_t."); + work->data = reinterpret_cast(param.get()); int ret = uv_queue_work( loop, work.get(), [](uv_work_t *work) {},