diff --git a/bundle.json b/bundle.json index 7ff8c77064742446ea91581f9423bc76b41d70bf..6643cf64641eb63162b742a0759742054dee697d 100644 --- a/bundle.json +++ b/bundle.json @@ -51,7 +51,10 @@ "bounds_checking_function" ] }, - "adapted_system_type": [ "small", "standard" ], + "adapted_system_type": [ + "small", + "standard" + ], "rom": "1024KB", "ram": "1024KB", "build": { diff --git a/frameworks/native/backup_ext/include/ext_backup.h b/frameworks/native/backup_ext/include/ext_backup.h index d9dd12aefd8ac75e4868deb7bf9ed5b2ea508440..bec5dc8efed215a91b121d2b3138b87cc80a918d 100644 --- a/frameworks/native/backup_ext/include/ext_backup.h +++ b/frameworks/native/backup_ext/include/ext_backup.h @@ -133,6 +133,11 @@ public: virtual ErrCode OnRestore(std::function callbackEx, std::function callback); + /** + * @brief Called do restore. + */ + virtual ErrCode OnRestore(std::function callback); + /** * @brief Called do GetBackupInfo. */ @@ -144,7 +149,7 @@ public: * @return true * @return false */ - bool WasFromSpeicalVersion(void); + bool WasFromSpecialVersion(void); /** * @brief Version for clone and cloud @@ -162,6 +167,11 @@ public: */ bool RestoreDataReady(); + /** + * @brief Called Notification containing extended information + */ + virtual ErrCode CallExtNotify(std::string result); + public: ExtBackup() = default; ~ExtBackup() override = default; diff --git a/frameworks/native/backup_ext/include/ext_backup_js.h b/frameworks/native/backup_ext/include/ext_backup_js.h index c159507fa218edb4ad2b73bddc14983d7086f409..8cec9c2a62d731ba171a0065756465535de412ef 100644 --- a/frameworks/native/backup_ext/include/ext_backup_js.h +++ b/frameworks/native/backup_ext/include/ext_backup_js.h @@ -53,15 +53,17 @@ struct CallJsParam { } }; -struct CallBackInfo { +struct CallbackInfo { std::function callback; - std::function callbackParam; + CallbackInfo(std::function callbackIn) : callback(callbackIn) {} +}; - CallBackInfo(std::function callbackIn) : callback(callbackIn) {} - CallBackInfo(std::function callbackIn) : callbackParam(callbackIn) {} +struct CallbackInfoEx { + std::function callbackParam; + CallbackInfoEx(std::function callbackIn) : callbackParam(callbackIn) {} }; -class ExtBackupJs : public ExtBackup, public virtual RefBase { +class ExtBackupJs : public ExtBackup { public: /** * @brief Init the extension. @@ -100,6 +102,13 @@ public: */ ErrCode OnRestore(std::function callbackEx, std::function callback) override; + + /** + * @brief Call the app's OnRestore. + * + * @param callback The callBack. + */ + ErrCode OnRestore(std::function callback) override; /** * @brief get app backup detail * @@ -107,6 +116,13 @@ public: */ ErrCode GetBackupInfo(std::function callback) override; + /** + * @brief Called Notification containing extended information + * + * @param result The result. + */ + ErrCode CallExtNotify(std::string result) override; + public: explicit ExtBackupJs(AbilityRuntime::JsRuntime &jsRuntime) : jsRuntime_(jsRuntime) {} ~ExtBackupJs() @@ -123,17 +139,19 @@ private: std::tuple CallObjectMethod(std::string_view name, const std::vector &argv = {}); std::function &argv)> ParseRestoreExInfo(); std::function &argv)> ParseRestoreInfo(); - std::function ParseOnRestoreExRet(); + + ErrCode CallJSRestoreEx(napi_env env, napi_value val); + ErrCode CallJSRestore(napi_env env, napi_value val); void ExportJsContext(void); AbilityRuntime::JsRuntime &jsRuntime_; std::unique_ptr jsObj_; - std::shared_ptr callbackInfoEx_; - std::shared_ptr callbackInfo_; - std::condition_variable extJsRetCon_; - std::mutex extJsRetMutex_; - std::atomic atoRet_; + std::shared_ptr callbackInfoEx_; + std::shared_ptr callbackInfo_; + std::condition_variable callJsCon_; + std::mutex callJsMutex_; + std::atomic needCallOnRestore_; }; } // namespace OHOS::FileManagement::Backup diff --git a/frameworks/native/backup_ext/include/ext_extension.h b/frameworks/native/backup_ext/include/ext_extension.h index 82061b3f280a5782d0d54fde0309a2705e130aa5..dbd944cdb7b9e00ddf7e0d993c511d477673d4bc 100644 --- a/frameworks/native/backup_ext/include/ext_extension.h +++ b/frameworks/native/backup_ext/include/ext_extension.h @@ -25,9 +25,9 @@ #include "b_json/b_json_entity_extension_config.h" #include "b_json/b_report_entity.h" -#include "b_resources/b_constants.h" #include "ext_backup_js.h" #include "ext_extension_stub.h" +#include "i_service.h" #include "thread_pool.h" #include "unique_fd.h" @@ -102,7 +102,7 @@ private: * * @param restoreRetInfo app restore reportInfo */ - void AppResultReport(const std::string &restoreRetInfo); + void AppResultReport(const std::string restoreRetInfo, BackupRestoreScenario scenario); /** * @brief Executing Backup Tasks Asynchronously @@ -149,6 +149,13 @@ private: * @param errCode */ void AppIncrementalDone(ErrCode errCode); + + /** + * @brief get callbackEx for execute onRestore + * + * @param errCode + */ + std::function GetCallbackExFun(wptr obj); private: std::shared_mutex lock_; std::shared_ptr extension_; diff --git a/frameworks/native/backup_ext/src/ext_backup.cpp b/frameworks/native/backup_ext/src/ext_backup.cpp index f49be75fe5999aad627b1c5269dd2adfcb156bb7..d5cd2a130157df9df6f5317141d2b2265f15206c 100644 --- a/frameworks/native/backup_ext/src/ext_backup.cpp +++ b/frameworks/native/backup_ext/src/ext_backup.cpp @@ -108,7 +108,7 @@ bool ExtBackup::AllowToBackupRestore() string usrConfig = GetUsrConfig(); BJsonCachedEntity cachedEntity(usrConfig); auto cache = cachedEntity.Structuralize(); - if (cache.GetAllowToBackupRestore() || WasFromSpeicalVersion() || SpeicalVersionForCloneAndCloud()) { + if (cache.GetAllowToBackupRestore() || WasFromSpecialVersion() || SpeicalVersionForCloneAndCloud()) { return true; } return false; @@ -186,7 +186,7 @@ sptr ExtBackup::OnConnect(const AAFwk::Want &want) sptr(new BackupExtExtension(std::static_pointer_cast(shared_from_this()))); // 排除特殊场景 - if (!WasFromSpeicalVersion()) { + if (!WasFromSpecialVersion()) { remoteObject->ExtClear(); } @@ -220,7 +220,7 @@ void ExtBackup::OnDisconnect(const AAFwk::Want &want) } } -bool ExtBackup::WasFromSpeicalVersion(void) +bool ExtBackup::WasFromSpecialVersion(void) { if (appVersionCode_ == BConstants::DEFAULT_VERSION_CODE && appVersionStr_ == BConstants::DEFAULT_VERSION_NAME) { return true; @@ -252,6 +252,12 @@ ErrCode ExtBackup::OnBackup(function callback) ErrCode ExtBackup::OnRestore(std::function callbackEx, function callback) +{ + HILOGI("BackupExtensionAbility(base) OnRestore with Ex."); + return ERR_OK; +} + +ErrCode ExtBackup::OnRestore(function callback) { HILOGI("BackupExtensionAbility(base) OnRestore."); return ERR_OK; @@ -263,4 +269,10 @@ ErrCode ExtBackup::GetBackupInfo(function callback) return ERR_OK; } +ErrCode ExtBackup::CallExtNotify(std::string result) +{ + HILOGI("BackupExtensionAbility(base) CallExtNotify."); + return ERR_OK; +} + } // namespace OHOS::FileManagement::Backup diff --git a/frameworks/native/backup_ext/src/ext_backup_js.cpp b/frameworks/native/backup_ext/src/ext_backup_js.cpp index 5437943f6338c9026f1aa5a50d5e2fd4c11958e6..7cfbc554250ec598070b802ef4dd1038d90f88c3 100644 --- a/frameworks/native/backup_ext/src/ext_backup_js.cpp +++ b/frameworks/native/backup_ext/src/ext_backup_js.cpp @@ -62,6 +62,32 @@ static string GetSrcPath(const AppExecFwk::AbilityInfo &info) return ""; } +static napi_status DealNapiStrValue(napi_env env, const napi_value napi_StrValue, std::string &result) +{ + HILOGI("Start DealNapiStrValue"); + std::string buffer = ""; + size_t bufferSize = 0; + napi_status status = napi_ok; + status = napi_get_value_string_utf8(env, napi_StrValue, nullptr, -1, &bufferSize); + if (status != napi_ok) { + HILOGE("Can not get buffer size"); + return status; + } + buffer.reserve(bufferSize + 1); + buffer.resize(bufferSize); + if (bufferSize > 0) { + status = napi_get_value_string_utf8(env, napi_StrValue, buffer.data(), bufferSize + 1, &bufferSize); + if (status != napi_ok) { + HILOGE("Can not get buffer value"); + return status; + } + } + if (buffer.data() != nullptr) { + result = buffer; + } + return status; +} + static napi_value PromiseCallback(napi_env env, napi_callback_info info) { HILOGI("Promise callback."); @@ -70,12 +96,35 @@ static napi_value PromiseCallback(napi_env env, napi_callback_info info) HILOGE("Failed to get callback info."); return nullptr; } - auto *callbackInfo = static_cast(data); + auto *callbackInfo = static_cast(data); + if (callbackInfo == nullptr) { + HILOGE("CallbackInfo is nullptr"); + return nullptr; + } callbackInfo->callback(); data = nullptr; return nullptr; } +static napi_value PromiseCallbackEx(napi_env env, napi_callback_info info) +{ + HILOGI("PromiseEx callback."); + void *data = nullptr; + std::string str; + size_t argc = 1; + napi_value argv = {nullptr}; + NAPI_CALL_NO_THROW(napi_get_cb_info(env, info, &argc, &argv, nullptr, &data), nullptr); + auto *callbackInfoEx = static_cast(data); + if (callbackInfoEx == nullptr) { + HILOGE("CallbackInfo is nullPtr"); + return nullptr; + } + DealNapiStrValue(env, argv, str); + callbackInfoEx->callbackParam(str); + data = nullptr; + return nullptr; +} + static bool CheckPromise(napi_env env, napi_value value) { if (value == nullptr) { @@ -90,7 +139,7 @@ static bool CheckPromise(napi_env env, napi_value value) return isPromise; } -static bool CallPromise(AbilityRuntime::JsRuntime &jsRuntime, napi_value result, CallBackInfo *callbackInfo) +static bool CallPromise(AbilityRuntime::JsRuntime &jsRuntime, napi_value result, CallbackInfo *callbackInfo) { AbilityRuntime::HandleScope handleScope(jsRuntime); auto env = jsRuntime.GetNapiEnv(); @@ -115,6 +164,32 @@ static bool CallPromise(AbilityRuntime::JsRuntime &jsRuntime, napi_value result, return true; } +static bool CallPromiseEx(AbilityRuntime::JsRuntime &jsRuntime, napi_value result, CallbackInfoEx *callbackInfoEx) +{ + AbilityRuntime::HandleScope handleScope(jsRuntime); + auto env = jsRuntime.GetNapiEnv(); + napi_value method = nullptr; + if (napi_get_named_property(env, result, "then", &method) != napi_ok) { + HILOGI("CallPromise, Failed to get method then"); + return false; + } + bool isCallable = false; + if (napi_is_callable(env, method, &isCallable) != napi_ok) { + HILOGI("CallPromise, Failed to check method then is callable"); + return false; + } + if (!isCallable) { + HILOGI("CallPromise, property then is not callable."); + return false; + } + napi_value ret; + napi_create_function(env, "promiseCallbackEx", strlen("promiseCallbackEx"), PromiseCallbackEx, callbackInfoEx, + &ret); + napi_value argv[1] = {ret}; + napi_call_function(env, result, method, 1, argv, nullptr); + return true; +} + void ExtBackupJs::Init(const shared_ptr &record, const shared_ptr &application, shared_ptr &handler, @@ -243,7 +318,7 @@ ErrCode ExtBackupJs::OnBackup(function callback) HILOGI("BackupExtensionAbility(JS) OnBackup."); BExcepUltils::BAssert(jsObj_, BError::Codes::EXT_BROKEN_FRAMEWORK, "The app does not provide the onBackup interface."); - callbackInfo_ = std::make_shared(callback); + callbackInfo_ = std::make_shared(callback); auto retParser = [jsRuntime {&jsRuntime_}, callbackInfo {callbackInfo_}](napi_env env, napi_value result) -> bool { if (!CheckPromise(env, result)) { @@ -267,30 +342,105 @@ ErrCode ExtBackupJs::OnRestore(std::function callbackEx HILOGI("BackupExtensionAbility(JS) OnRestore."); BExcepUltils::BAssert(jsObj_, BError::Codes::EXT_BROKEN_FRAMEWORK, "The app does not provide the onRestore interface."); - atoRet_.store(false); - callbackInfoEx_ = std::make_shared(callbackEx); - callbackInfo_ = std::make_shared(callback); - auto retParser = ParseOnRestoreExRet(); - HILOGI("Start execute call Js onRestoreEx method"); - std::unique_lock lock(extJsRetMutex_); - int32_t errCode = CallJsMethod("onRestoreEx", jsRuntime_, jsObj_.get(), ParseRestoreExInfo(), retParser); - extJsRetCon_.wait_for(lock, std::chrono::seconds(WAIT_ONRESTORE_EX_TIMEOUT)); - - auto retParserBase = [jsRuntime {&jsRuntime_}, callbackInfoEx {callbackInfoEx_}, callbackInfo {callbackInfo_}] - (napi_env env, napi_value result) -> bool { + needCallOnRestore_.store(false); + callbackInfo_ = std::make_shared(callback); + callbackInfoEx_ = std::make_shared(callbackEx); + auto envir = jsRuntime_.GetNapiEnv(); + napi_value value = jsObj_.get()->GetNapiValue(); + if (value == nullptr) { + HILOGE("failed to get napi value object"); + return EINVAL; + } + return CallJSRestoreEx(envir, value); +} + +ErrCode ExtBackupJs::OnRestore(function callback) +{ + HILOGI("Start execute BackupExtensionAbility(JS) OnRestore."); + BExcepUltils::BAssert(jsObj_, BError::Codes::EXT_BROKEN_FRAMEWORK, + "The app does not provide the onRestore interface."); + auto argParser = [appVersionCode(appVersionCode_), + appVersionStr(appVersionStr_)](napi_env env, vector &argv) -> bool { + napi_value objValue = nullptr; + napi_create_object(env, &objValue); + napi_set_named_property(env, objValue, "code", AbilityRuntime::CreateJsValue(env, appVersionCode)); + napi_set_named_property(env, objValue, "name", AbilityRuntime::CreateJsValue(env, appVersionStr.c_str())); + argv.push_back(objValue); + return true; + }; + callbackInfo_ = std::make_shared(callback); + auto retParser = [jsRuntime {&jsRuntime_}, callbackInfo {callbackInfo_}](napi_env env, napi_value result) -> bool { if (!CheckPromise(env, result)) { - HILOGI("Will callBack onRestore"); + HILOGI("CheckPromise false"); callbackInfo->callback(); return true; } - HILOGI("CheckPromise(JS) OnRestore ok."); + HILOGI("CheckPromise Js Method onRestore ok."); return CallPromise(*jsRuntime, result, callbackInfo.get()); }; - if (atoRet_.load()) { - errCode = CallJsMethod("onRestore", jsRuntime_, jsObj_.get(), ParseRestoreInfo(), retParserBase); + auto errCode = CallJsMethod("onRestore", jsRuntime_, jsObj_.get(), argParser, retParser); + if (errCode != ERR_OK) { + HILOGE("CallJsMethod error, code:%{public}d.", errCode); + return errCode; } + return ERR_OK; +} + +ErrCode ExtBackupJs::CallJSRestoreEx(napi_env env, napi_value val) +{ + bool isExist; + napi_has_named_property(env, val, "onRestoreEx", &isExist); + if (!isExist) { + HILOGI("Js method onRestoreEx is not exist"); + return CallJSRestore(env, val); + } + HILOGI("Js method onRestoreEx is exist"); + auto retParser = [jsRuntime {&jsRuntime_}, callbackInfoEx {callbackInfoEx_}](napi_env envir, napi_value result) -> + bool { + if (!CheckPromise(envir, result)) { + HILOGI("CheckPromise onRestoreEx false"); + std::string str; + DealNapiStrValue(envir, result, str); + callbackInfoEx->callbackParam(str); + return true; + } + HILOGI("CheckPromise onRestoreEx ok"); + return CallPromiseEx(*jsRuntime, result, callbackInfoEx.get()); + }; + auto errCode = CallJsMethod("onRestoreEx", jsRuntime_, jsObj_.get(), ParseRestoreExInfo(), retParser); if (errCode != ERR_OK) { - HILOGE("exe js method onRestore error,errCode: %{public}d", errCode); + HILOGE("Call onRestoreEx error"); + return errCode; + } + HILOGI("Call Js method lock"); + if (!needCallOnRestore_.load()) { + HILOGI("Call Js method onRestoreEx done"); + return ERR_OK; + } + return CallJSRestore(env, val); +} + +ErrCode ExtBackupJs::CallJSRestore(napi_env env, napi_value val) +{ + bool isExist; + napi_has_named_property(env, val, "onRestore", &isExist); + if (!isExist) { + HILOGI("Js method onRestore is not exist"); + return ErrCode(isExist); + } + HILOGI("Js method onRestore is exist"); + auto retParser = [jsRuntime {&jsRuntime_}, callbackInfo {callbackInfo_}](napi_env env, napi_value result) -> bool { + if (!CheckPromise(env, result)) { + HILOGI("onRestore, CheckPromise false"); + callbackInfo->callback(); + return true; + } + HILOGI("CheckPromise Js Method onRestore ok."); + return CallPromise(*jsRuntime, result, callbackInfo.get()); + }; + auto errCode = CallJsMethod("onRestore", jsRuntime_, jsObj_.get(), ParseRestoreInfo(), retParser); + if (errCode != ERR_OK) { + HILOGE("CallJsMethod error, code:%{public}d.", errCode); return errCode; } return ERR_OK; @@ -301,8 +451,8 @@ ErrCode ExtBackupJs::GetBackupInfo(std::function callba HILOGI("BackupExtensionAbility(JS) GetBackupInfo begin."); BExcepUltils::BAssert(jsObj_, BError::Codes::EXT_BROKEN_FRAMEWORK, "The app does not provide the GetBackupInfo interface."); - callbackInfo_ = std::make_shared(callback); - auto retParser = [jsRuntime {&jsRuntime_}, callBackInfo {callbackInfo_}](napi_env env, + callbackInfoEx_ = std::make_shared(callback); + auto retParser = [jsRuntime {&jsRuntime_}, callBackInfo {callbackInfoEx_}](napi_env env, napi_value result) -> bool { if (!CheckPromise(env, result)) { size_t strLen = 0; @@ -317,7 +467,7 @@ ErrCode ExtBackupJs::GetBackupInfo(std::function callba return true; } HILOGI("BackupExtensionAbulity(JS) GetBackupInfo ok."); - return CallPromise(*jsRuntime, result, callBackInfo.get()); + return CallPromiseEx(*jsRuntime, result, callBackInfo.get()); }; auto errCode = CallJsMethod("getBackupInfo", jsRuntime_, jsObj_.get(), {}, retParser); @@ -357,9 +507,10 @@ static int DoCallJsMethod(CallJsParam *param) napi_close_handle_scope(env, scope); return EINVAL; } + napi_status status; napi_value method; - napi_get_named_property(env, value, param->funcName.c_str(), &method); - if (param->retParser == nullptr) { + status = napi_get_named_property(env, value, param->funcName.c_str(), &method); + if (status != napi_ok || param->retParser == nullptr) { HILOGE("ResultValueParser must not null."); napi_close_handle_scope(env, scope); return EINVAL; @@ -407,8 +558,6 @@ int ExtBackupJs::CallJsMethod(const std::string &funcName, HILOGE("failed to call DoCallJsMethod."); } } while (false); - std::unique_lock lock(param->backupOperateMutex); - param->isReady = true; param->backupOperateCondition.notify_one(); }); if (ret != 0) { @@ -416,8 +565,7 @@ int ExtBackupJs::CallJsMethod(const std::string &funcName, return EINVAL; } std::unique_lock lock(param->backupOperateMutex); - param->backupOperateCondition.wait(lock, [param]() { return param->isReady; }); - + param->backupOperateCondition.wait_for(lock, std::chrono::seconds(WAIT_ONRESTORE_EX_TIMEOUT)); return ERR_OK; } @@ -453,45 +601,16 @@ std::function &argv)> ExtBackupJs::Pa return onRestoreFun; } -std::function ExtBackupJs::ParseOnRestoreExRet() +ErrCode ExtBackupJs::CallExtNotify(std::string result) { - auto retParser = [jsRuntime {&jsRuntime_}, callbackInfoEx {callbackInfoEx_}, - callbackInfo {callbackInfo_}, ptr {wptr(this)}](napi_env env, napi_value result) -> bool { - auto thisPtr = ptr.promote(); - if (!thisPtr) { - HILOGE("this pointer is null"); - return false; - } - if (!CheckPromise(env, result)) { - napi_valuetype valueType = napi_undefined; - napi_typeof(env, result, &valueType); - if (valueType == napi_string) { - size_t strLen = 0; - napi_status status = napi_get_value_string_utf8(env, result, nullptr, -1, &strLen); - if (status != napi_ok) { - HILOGE("Args result length is empty"); - return false; - } - size_t bufLen = strLen + 1; - unique_ptr restoreRetStr = make_unique(bufLen); - HILOGI("Will callBack onRestoreEx, restoreRetStr is: %{public}s", restoreRetStr.get()); - if (strlen(restoreRetStr.get()) == 0) { - HILOGI("app restore ret is empty,will call on restore"); - thisPtr->atoRet_.store(true); - thisPtr->extJsRetCon_.notify_one(); - return false; - } - callbackInfoEx->callbackParam(restoreRetStr.get()); - return true; - } - thisPtr->atoRet_.store(true); - thisPtr->extJsRetCon_.notify_one(); - return false; - } - HILOGI("CheckPromise(JS) OnRestore ok."); - return CallPromise(*jsRuntime, result, callbackInfo.get()); - }; - return retParser; + HILOGI("Start CallExtNotify, result is: %{public}s", result.c_str()); + if (result.size() == 0) { + needCallOnRestore_.store(true); + } else { + needCallOnRestore_.store(false); + } + callJsCon_.notify_one(); + HILOGI("End CallExtNotify"); + return ERR_OK; } - } // namespace OHOS::FileManagement::Backup diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index b9e53947846f6aa125a9b10bd39bbccc43feb3d3..c8f14c737430abb76436e28966ec1080ac6e02ec 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -46,10 +46,10 @@ #include "b_json/b_json_cached_entity.h" #include "b_json/b_json_entity_ext_manage.h" #include "b_json/b_report_entity.h" -#include "b_resources/b_constants.h" #include "b_tarball/b_tarball_factory.h" #include "filemgmt_libhilog.h" #include "hitrace_meter.h" +#include "i_service.h" #include "service_proxy.h" #include "tar_file.h" #include "untar_file.h" @@ -1027,24 +1027,12 @@ void BackupExtExtension::AsyncTaskRestoreForUpgrade() HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); auto task = [obj {wptr(this)}]() { auto ptr = obj.promote(); - auto callBackupEx = [obj](const std::string &restoreRetInfo) { - HILOGI("begin call restoreEx"); - auto extensionPtr = obj.promote(); - BExcepUltils::BAssert(extensionPtr, BError::Codes::EXT_BROKEN_FRAMEWORK, - "Ext extension handle have been already released"); - if (restoreRetInfo.size()) { - extensionPtr->AppResultReport(restoreRetInfo); - } - extensionPtr->AppDone(BError(BError::Codes::OK)); - extensionPtr->DoClear(); - }; + auto callBackupEx = ptr->GetCallbackExFun(obj); auto callBackup = [obj]() { - HILOGI("begin call restore"); auto extensionPtr = obj.promote(); BExcepUltils::BAssert(extensionPtr, BError::Codes::EXT_BROKEN_FRAMEWORK, "Ext extension handle have been already released"); extensionPtr->AppDone(BError(BError::Codes::OK)); - // 清空恢复目录 extensionPtr->DoClear(); }; try { @@ -1063,10 +1051,6 @@ void BackupExtExtension::AsyncTaskRestoreForUpgrade() ptr->AppDone(BError(BError::Codes::EXT_INVAL_ARG).GetCode()); } }; - - // REM: 这里异步化了,需要做并发控制 - // 在往线程池中投入任务之前将需要的数据拷贝副本到参数中,保证不发生读写竞争, - // 由于拷贝参数时尚运行在主线程中,故在参数拷贝过程中是线程安全的。 threadPool_.AddTask([task]() { try { task(); @@ -1086,15 +1070,16 @@ void BackupExtExtension::AsyncTaskIncrementalRestoreForUpgrade() { auto task = [obj {wptr(this)}]() { auto ptr = obj.promote(); - auto callBackupEx = [obj](const std::string &restoreRetInfo) { - HILOGI("begin call restore"); + auto callBackupEx = [obj](const std::string restoreRetInfo) { + HILOGI("begin call restoreEx"); auto extensionPtr = obj.promote(); BExcepUltils::BAssert(extensionPtr, BError::Codes::EXT_BROKEN_FRAMEWORK, "Ext extension handle have been already released"); + extensionPtr->extension_->CallExtNotify(restoreRetInfo); if (restoreRetInfo.size()) { - extensionPtr->AppResultReport(restoreRetInfo); + extensionPtr->AppResultReport(restoreRetInfo, BackupRestoreScenario::INCREMENTAL_RESTORE); } - extensionPtr->AppIncrementalDone(BError(BError::Codes::OK)); + extensionPtr->AppDone(BError(BError::Codes::OK)); extensionPtr->DoClear(); }; auto callBackup = [obj]() { @@ -1172,12 +1157,12 @@ void BackupExtExtension::AppDone(ErrCode errCode) } } -void BackupExtExtension::AppResultReport(const std::string &restoreRetInfo) +void BackupExtExtension::AppResultReport(const std::string restoreRetInfo, BackupRestoreScenario scenario) { HILOGI("Begin"); auto proxy = ServiceProxy::GetInstance(); BExcepUltils::BAssert(proxy, BError::Codes::EXT_BROKEN_IPC, "Failed to obtain the ServiceProxy handle"); - auto ret = proxy->ServiceResultReport(restoreRetInfo); + auto ret = proxy->ServiceResultReport(restoreRetInfo, scenario); if (ret != ERR_OK) { HILOGE("Failed notify app restoreResultReport, errCode: %{public}d", ret); } @@ -1241,7 +1226,7 @@ ErrCode BackupExtExtension::HandleRestore() } // async do restore. - if (extension_->WasFromSpeicalVersion() && extension_->RestoreDataReady()) { + if (extension_->WasFromSpecialVersion() && extension_->RestoreDataReady()) { HILOGI("Restore directly when upgrading."); AsyncTaskRestoreForUpgrade(); } @@ -1629,4 +1614,25 @@ ErrCode BackupExtExtension::GetBackupInfo(std::string &result) return ERR_OK; } + +std::function BackupExtExtension::GetCallbackExFun(wptr obj) +{ + HILOGI("Begin get callbackEx"); + return [obj](const std::string restoreRetInfo) { + auto extensionPtr = obj.promote(); + BExcepUltils::BAssert(extensionPtr, BError::Codes::EXT_BROKEN_FRAMEWORK, + "Ext extension handle have been already released"); + extensionPtr->extension_->CallExtNotify(restoreRetInfo); + if (restoreRetInfo.size()) { + if (extensionPtr->extension_->WasFromSpecialVersion() && extensionPtr->extension_->RestoreDataReady()) { + extensionPtr->AppResultReport(restoreRetInfo, + BackupRestoreScenario::INCREMENTAL_RESTORE); + } else { + extensionPtr->AppResultReport(restoreRetInfo, BackupRestoreScenario::FULL_RESTORE); + } + } + extensionPtr->AppDone(BError(BError::Codes::OK)); + extensionPtr->DoClear(); + }; +} } // namespace OHOS::FileManagement::Backup diff --git a/frameworks/native/backup_kit_inner/include/service_reverse.h b/frameworks/native/backup_kit_inner/include/service_reverse.h index 4dbf43273831aead9ed82d9e628e95e6dfa8d739..98b7e59e06607aab47f2a626878ed37150890819 100644 --- a/frameworks/native/backup_kit_inner/include/service_reverse.h +++ b/frameworks/native/backup_kit_inner/include/service_reverse.h @@ -27,6 +27,7 @@ class ServiceReverse final : public ServiceReverseStub { public: void BackupOnFileReady(std::string bundleName, std::string fileName, int fd) override; void BackupOnBundleStarted(int32_t errCode, std::string bundleName) override; + void BackupOnResultReport(std::string result) override; void BackupOnBundleFinished(int32_t errCode, std::string bundleName) override; void BackupOnAllBundlesFinished(int32_t errCode) override; @@ -38,6 +39,7 @@ public: void IncrementalBackupOnFileReady(std::string bundleName, std::string fileName, int fd, int manifestFd) override; void IncrementalBackupOnBundleStarted(int32_t errCode, std::string bundleName) override; + void IncrementalBackupOnResultReport(std::string result) override; void IncrementalBackupOnBundleFinished(int32_t errCode, std::string bundleName) override; void IncrementalBackupOnAllBundlesFinished(int32_t errCode) override; @@ -45,6 +47,7 @@ public: void IncrementalRestoreOnBundleFinished(int32_t errCode, std::string bundleName) override; void IncrementalRestoreOnAllBundlesFinished(int32_t errCode) override; void IncrementalRestoreOnFileReady(std::string bundleName, std::string fileName, int fd, int manifestFd) override; + void IncrementalRestoreOnResultReport(std::string result) override; public: ServiceReverse() = delete; diff --git a/frameworks/native/backup_kit_inner/include/service_reverse_stub.h b/frameworks/native/backup_kit_inner/include/service_reverse_stub.h index b58400ec9c6dcf63c00cd3b5929cd0d29d001e64..879eccf1bca385f72eeae8a53ca1174671b5ab9b 100644 --- a/frameworks/native/backup_kit_inner/include/service_reverse_stub.h +++ b/frameworks/native/backup_kit_inner/include/service_reverse_stub.h @@ -36,6 +36,7 @@ private: int32_t CmdBackupOnFileReady(MessageParcel &data, MessageParcel &reply); int32_t CmdBackupOnBundleStarted(MessageParcel &data, MessageParcel &reply); + int32_t CmdBackupOnResultReport(MessageParcel &data, MessageParcel &reply); int32_t CmdBackupOnBundleFinished(MessageParcel &data, MessageParcel &reply); int32_t CmdBackupOnAllBundlesFinished(MessageParcel &data, MessageParcel &reply); @@ -47,6 +48,7 @@ private: int32_t CmdIncrementalBackupOnFileReady(MessageParcel &data, MessageParcel &reply); int32_t CmdIncrementalBackupOnBundleStarted(MessageParcel &data, MessageParcel &reply); + int32_t CmdIncrementalBackupOnResultReport(MessageParcel &data, MessageParcel &reply); int32_t CmdIncrementalBackupOnBundleFinished(MessageParcel &data, MessageParcel &reply); int32_t CmdIncrementalBackupOnAllBundlesFinished(MessageParcel &data, MessageParcel &reply); @@ -54,6 +56,7 @@ private: int32_t CmdIncrementalRestoreOnBundleFinished(MessageParcel &data, MessageParcel &reply); int32_t CmdIncrementalRestoreOnAllBundlesFinished(MessageParcel &data, MessageParcel &reply); int32_t CmdIncrementalRestoreOnFileReady(MessageParcel &data, MessageParcel &reply); + int32_t CmdIncrementalRestoreOnResultReport(MessageParcel &data, MessageParcel &reply); }; } // namespace OHOS::FileManagement::Backup diff --git a/frameworks/native/backup_kit_inner/src/b_incremental_session_restore_async.cpp b/frameworks/native/backup_kit_inner/src/b_incremental_session_restore_async.cpp index 9a1ca08e97473ccad29de95c2131a972d074c467..825ca41161acf723a4cad5f579b8baf64c340078 100644 --- a/frameworks/native/backup_kit_inner/src/b_incremental_session_restore_async.cpp +++ b/frameworks/native/backup_kit_inner/src/b_incremental_session_restore_async.cpp @@ -52,10 +52,12 @@ shared_ptr BIncrementalSessionRestoreAsync::Ini HILOGE("Failed to get backup service"); return nullptr; } - BIncrementalRestoreSession::Callbacks callbacksTmp {.onFileReady = callbacks.onFileReady, + BIncrementalRestoreSession::Callbacks callbacksTmp { + .onFileReady = callbacks.onFileReady, .onBundleStarted = callbacks.onBundleStarted, .onBundleFinished = callbacks.onBundleFinished, .onAllBundlesFinished = callbacks.onAllBundlesFinished, + .onResultReport = callbacks.onResultReport, .onBackupServiceDied = callbacks.onBackupServiceDied}; int32_t res = proxy->InitRestoreSession(new ServiceReverse(callbacksTmp)); if (res != 0) { diff --git a/frameworks/native/backup_kit_inner/src/service_incremental_reverse.cpp b/frameworks/native/backup_kit_inner/src/service_incremental_reverse.cpp index 61651a0ce9e89a6c00d15f013e42a86aaac5a13a..a846150011b918c8dcdd582b24bcc9b23fd5c861 100644 --- a/frameworks/native/backup_kit_inner/src/service_incremental_reverse.cpp +++ b/frameworks/native/backup_kit_inner/src/service_incremental_reverse.cpp @@ -42,6 +42,16 @@ void ServiceReverse::IncrementalBackupOnBundleStarted(int32_t errCode, string bu callbacksIncrementalBackup_.onBundleStarted(errCode, bundleName); } +void ServiceReverse::IncrementalBackupOnResultReport(std::string result) +{ + HILOGI("begin"); + if (scenario_ != Scenario::BACKUP || !callbacksIncrementalBackup_.onResultReport) { + HILOGI("Error scenario or callback is nullptr"); + return; + } + callbacksIncrementalBackup_.onResultReport(result); +} + void ServiceReverse::IncrementalBackupOnBundleFinished(int32_t errCode, string bundleName) { HILOGI("begin"); @@ -103,6 +113,16 @@ void ServiceReverse::IncrementalRestoreOnFileReady(string bundleName, string fil callbacksIncrementalRestore_.onFileReady(bFileInfo, UniqueFd(fd), UniqueFd(manifestFd)); } +void ServiceReverse::IncrementalRestoreOnResultReport(std::string result) +{ + HILOGI("begin"); + if (scenario_ != Scenario::RESTORE || !callbacksIncrementalRestore_.onResultReport) { + HILOGI("Error scenario or callback is nullptr"); + return; + } + callbacksIncrementalRestore_.onResultReport(result); +} + ServiceReverse::ServiceReverse(BIncrementalBackupSession::Callbacks callbacks) : scenario_(Scenario::BACKUP), callbacksIncrementalBackup_(callbacks) { diff --git a/frameworks/native/backup_kit_inner/src/service_proxy.cpp b/frameworks/native/backup_kit_inner/src/service_proxy.cpp index a890b92a9c81ecb116a05f99814062c159d7e728..811ecb99daf6c27cf6b18db6314d9164208adcd3 100644 --- a/frameworks/native/backup_kit_inner/src/service_proxy.cpp +++ b/frameworks/native/backup_kit_inner/src/service_proxy.cpp @@ -208,7 +208,7 @@ ErrCode ServiceProxy::AppDone(ErrCode errCode) return reply.ReadInt32(); } -ErrCode ServiceProxy::ServiceResultReport(const std::string &restoreRetInfo) +ErrCode ServiceProxy::ServiceResultReport(const std::string restoreRetInfo, BackupRestoreScenario scenario) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); HILOGI("Begin"); @@ -220,6 +220,9 @@ ErrCode ServiceProxy::ServiceResultReport(const std::string &restoreRetInfo) if (!data.WriteString(restoreRetInfo)) { return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the restoreRetInfo").GetCode(); } + if (!data.WriteInt32(static_cast(scenario))) { + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the scenario").GetCode(); + } MessageParcel reply; MessageOption option; int32_t ret = diff --git a/frameworks/native/backup_kit_inner/src/service_reverse.cpp b/frameworks/native/backup_kit_inner/src/service_reverse.cpp index 1fb6d5b41f42bdde1d9eab98a49070379b4156b1..4377cb02543bb478ade36c628bbf17b1cd4036a2 100644 --- a/frameworks/native/backup_kit_inner/src/service_reverse.cpp +++ b/frameworks/native/backup_kit_inner/src/service_reverse.cpp @@ -42,6 +42,16 @@ void ServiceReverse::BackupOnBundleStarted(int32_t errCode, string bundleName) callbacksBackup_.onBundleStarted(errCode, bundleName); } +void ServiceReverse::BackupOnResultReport(std::string result) +{ + HILOGI("begin"); + if (scenario_ != Scenario::BACKUP || !callbacksBackup_.onResultReport) { + HILOGI("Error scenario or callback is nullptr"); + return; + } + callbacksBackup_.onResultReport(result); +} + void ServiceReverse::BackupOnBundleFinished(int32_t errCode, string bundleName) { HILOGI("begin"); @@ -105,7 +115,11 @@ void ServiceReverse::RestoreOnFileReady(string bundleName, string fileName, int void ServiceReverse::RestoreOnResultReport(string result) { - HILOGI("ServiceReverse RestoreOnResultReport begin with result: %s", result.c_str()); + HILOGI("ServiceReverse RestoreOnResultReport begin with result: %{public}s", result.c_str()); + if (scenario_ != Scenario::RESTORE || !callbacksRestore_.onResultReport) { + HILOGI("Error scenario or callback is nullptr"); + return; + } callbacksRestore_.onResultReport(result); } diff --git a/frameworks/native/backup_kit_inner/src/service_reverse_stub.cpp b/frameworks/native/backup_kit_inner/src/service_reverse_stub.cpp index 942b91fa070311d490073321a362c91df0539031..f1a81f834d0b9d7814884539e96d5793985d35f0 100644 --- a/frameworks/native/backup_kit_inner/src/service_reverse_stub.cpp +++ b/frameworks/native/backup_kit_inner/src/service_reverse_stub.cpp @@ -50,6 +50,8 @@ ServiceReverseStub::ServiceReverseStub() &ServiceReverseStub::CmdBackupOnFileReady; opToInterfaceMap_[static_cast(IServiceReverseInterfaceCode::SERVICER_BACKUP_ON_SUB_TASK_STARTED)] = &ServiceReverseStub::CmdBackupOnBundleStarted; + opToInterfaceMap_[static_cast(IServiceReverseInterfaceCode::SERVICER_BACKUP_ON_RESULT_REPORT)] = + &ServiceReverseStub::CmdBackupOnResultReport; opToInterfaceMap_[static_cast(IServiceReverseInterfaceCode::SERVICER_BACKUP_ON_SUB_TASK_FINISHED)] = &ServiceReverseStub::CmdBackupOnBundleFinished; opToInterfaceMap_[static_cast(IServiceReverseInterfaceCode::SERVICER_BACKUP_ON_TASK_FINISHED)] = @@ -57,6 +59,8 @@ ServiceReverseStub::ServiceReverseStub() opToInterfaceMap_[static_cast(IServiceReverseInterfaceCode::SERVICER_RESTORE_ON_SUB_TASK_STARTED)] = &ServiceReverseStub::CmdRestoreOnBundleStarted; + opToInterfaceMap_[static_cast(IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_BACKUP_ON_RESULT_REPORT)] + = &ServiceReverseStub::CmdIncrementalBackupOnResultReport; opToInterfaceMap_[static_cast(IServiceReverseInterfaceCode::SERVICER_RESTORE_ON_SUB_TASK_FINISHED)] = &ServiceReverseStub::CmdRestoreOnBundleFinished; opToInterfaceMap_[static_cast(IServiceReverseInterfaceCode::SERVICER_RESTORE_ON_TASK_FINISHED)] = @@ -89,6 +93,9 @@ ServiceReverseStub::ServiceReverseStub() &ServiceReverseStub::CmdIncrementalRestoreOnAllBundlesFinished; opToInterfaceMap_[static_cast(IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_RESTORE_ON_FILE_READY)] = &ServiceReverseStub::CmdIncrementalRestoreOnFileReady; + opToInterfaceMap_[static_cast( + IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_RESTORE_ON_RESULT_REPORT)] = + &ServiceReverseStub::CmdIncrementalRestoreOnResultReport; } int32_t ServiceReverseStub::CmdBackupOnFileReady(MessageParcel &data, MessageParcel &reply) @@ -100,6 +107,13 @@ int32_t ServiceReverseStub::CmdBackupOnFileReady(MessageParcel &data, MessagePar return BError(BError::Codes::OK); } +int32_t ServiceReverseStub::CmdBackupOnResultReport(MessageParcel &data, MessageParcel &reply) +{ + std::string result = data.ReadString(); + BackupOnResultReport(result); + return BError(BError::Codes::OK); +} + int32_t ServiceReverseStub::CmdBackupOnBundleStarted(MessageParcel &data, MessageParcel &reply) { int32_t errCode = data.ReadInt32(); @@ -183,6 +197,13 @@ int32_t ServiceReverseStub::CmdIncrementalBackupOnBundleStarted(MessageParcel &d return BError(BError::Codes::OK); } +int32_t ServiceReverseStub::CmdIncrementalBackupOnResultReport(MessageParcel &data, MessageParcel &reply) +{ + std::string result = data.ReadString(); + IncrementalBackupOnResultReport(result); + return BError(BError::Codes::OK); +} + int32_t ServiceReverseStub::CmdIncrementalBackupOnBundleFinished(MessageParcel &data, MessageParcel &reply) { int32_t errCode = data.ReadInt32(); @@ -230,4 +251,11 @@ int32_t ServiceReverseStub::CmdIncrementalRestoreOnFileReady(MessageParcel &data IncrementalRestoreOnFileReady(bundleName, fileName, fd, manifestFd); return BError(BError::Codes::OK); } + +int32_t ServiceReverseStub::CmdIncrementalRestoreOnResultReport(MessageParcel &data, MessageParcel &reply) +{ + auto result = data.ReadString(); + IncrementalRestoreOnResultReport(result); + return BError(BError::Codes::OK); +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/interfaces/api/js/napi/backup_ext/backup_extension_ability.js b/interfaces/api/js/napi/backup_ext/backup_extension_ability.js index 1b64f900bec981ad8962ca5396eacf4fbb9b1eea..b1801058f4390d545007974492a97ac627a44b1c 100644 --- a/interfaces/api/js/napi/backup_ext/backup_extension_ability.js +++ b/interfaces/api/js/napi/backup_ext/backup_extension_ability.js @@ -15,19 +15,19 @@ class BackupExtensionAbility { onBackup() { - console.log() + console.log(); } onRestore(versionBackupedBundle) { - console.log(versionBackupedBundle) + console.log(versionBackupedBundle); } getBackupInfo() { - console.log() + console.log(); } onRestoreEx(versionBackupedBundle, restoreInfo) { - console.log() + console.log(); } } diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_backup_session.h b/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_backup_session.h index f6dda7ba46c194a39388250b4a7518979bcd7de4..957296fe3b80403d9fc500bdd90faa9a83d8e7aa 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_backup_session.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_backup_session.h @@ -35,6 +35,7 @@ public: std::function onBundleFinished; // 当某个应用的备份流程结束或意外中止时执行的回调函数 std::function onAllBundlesFinished; // 当整个备份流程结束或意外中止时执行的回调函数 + std::function onResultReport; // 某个应用备份流程中自定义错误信息的上报的回调函数 std::function onBackupServiceDied; // 当备份服务意外死亡时执行的回调函数 }; diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_restore_session.h b/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_restore_session.h index 7dd4fe84719564551b5ffdedb115f83f5a1196e2..dbed91c2f8525b9d4d87608aef6d3a74ec9c40b4 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_restore_session.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_restore_session.h @@ -35,6 +35,7 @@ public: std::function onBundleFinished; // 当某个应用的恢复流程结束或意外中止时执行的回调函数 std::function onAllBundlesFinished; // 当整个恢复流程结束或意外中止时执行的回调函数 + std::function onResultReport; // 某个应用恢复流程中自定义错误信息的上报的回调函数 std::function onBackupServiceDied; // 当备份服务意外死亡时执行的回调函数 }; diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_session_restore_async.h b/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_session_restore_async.h index ef198582a2e29a348920ff2bec2fcf64d6532372..cb893e41d3ccb8954c3d8b941ba40bfa02058717 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_session_restore_async.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_session_restore_async.h @@ -37,6 +37,7 @@ public: std::function onBundleFinished; // 当某个应用的恢复流程结束或意外中止时执行的回调函数 std::function onAllBundlesFinished; // 当整个恢复流程结束或意外中止时执行的回调函数 + std::function onResultReport; // 某个应用恢复流程中自定义错误信息的上报的回调函数 std::function onBackupServiceDied; // 当备份服务意外死亡时执行的回调函数 }; diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/b_session_backup.h b/interfaces/inner_api/native/backup_kit_inner/impl/b_session_backup.h index c662a5844912465670375f87b689276be3a19e2a..24a2f246a396b9dca1be684540ced9d2b882d757 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/b_session_backup.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/b_session_backup.h @@ -33,6 +33,7 @@ public: std::function onBundleStarted; // 当启动某个应用的备份流程结束时执行的回调函数 std::function onBundleFinished; // 当某个应用的备份流程结束或意外中止时执行的回调函数 std::function onAllBundlesFinished; // 当整个备份流程结束或意外中止时执行的回调函数 + std::function onResultReport; // 某个应用备份流程中自定义错误信息的上报的回调函数 std::function onBackupServiceDied; // 当备份服务意外死亡时执行的回调函数 }; diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore.h b/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore.h index 3cbc4efae1885db77abe96739d532bc05c03c8df..dfe89ba349425461d07ae3058d0126210b2895bc 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore.h @@ -33,7 +33,7 @@ public: std::function onBundleStarted; // 当启动某个应用的恢复流程结束时执行的回调函数 std::function onBundleFinished; // 当某个应用的恢复流程结束或意外中止时执行的回调函数 std::function onAllBundlesFinished; // 当整个恢复流程结束或意外中止时执行的回调函数 - std::function onResultReport; + std::function onResultReport; // 某个应用恢复流程中自定义错误信息的上报的回调函数 std::function onBackupServiceDied; // 当备份服务意外死亡时执行的回调函数 }; diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore_async.h b/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore_async.h index 6474810ef8c50bf5be6eb0feedbbaaae792b2afa..5140e0e081884ba473f6d320099ce7676d15cac5 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore_async.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore_async.h @@ -37,7 +37,7 @@ public: std::function onBundleFinished; // 当某个应用的恢复流程结束或意外中止时执行的回调函数 std::function onAllBundlesFinished; // 当整个恢复流程结束或意外中止时执行的回调函数 - std::function onResultReport; + std::function onResultReport; // 某个应用恢复流程中自定义错误信息的上报的回调函数 std::function onBackupServiceDied; // 当备份服务意外死亡时执行的回调函数 }; diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/i_service.h b/interfaces/inner_api/native/backup_kit_inner/impl/i_service.h index 8c8a7138aa85fdd85f19b9de2aa4b4da7707b038..3ab134964d4551394942915f1b3c58aa39e19082 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/i_service.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/i_service.h @@ -35,6 +35,13 @@ typedef enum TypeRestoreTypeEnum { RESTORE_DATA_READDY = 1, } RestoreTypeEnum; +enum BackupRestoreScenario { + FULL_BACKUP = 0, + INCREMENTAL_BACKUP = 1, + FULL_RESTORE = 2, + INCREMENTAL_RESTORE = 3, +}; + class IService : public IRemoteBroker { public: virtual ErrCode InitRestoreSession(sptr remote) = 0; @@ -44,7 +51,8 @@ public: virtual ErrCode PublishFile(const BFileInfo &fileInfo) = 0; virtual ErrCode AppFileReady(const std::string &fileName, UniqueFd fd) = 0; virtual ErrCode AppDone(ErrCode errCode) = 0; - virtual ErrCode ServiceResultReport(const std::string &restoreRetInfo) = 0; + virtual ErrCode ServiceResultReport(const std::string restoreRetInfo, + BackupRestoreScenario scenario) = 0; virtual ErrCode GetFileHandle(const std::string &bundleName, const std::string &fileName) = 0; virtual ErrCode AppendBundlesRestoreSession(UniqueFd fd, const std::vector &bundleNames, diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/i_service_reverse.h b/interfaces/inner_api/native/backup_kit_inner/impl/i_service_reverse.h index 3fe6b51f9d687f014142db69a27abe5c7142661e..a4bfcad900f46259ac2bc7331057b7903dbfa09f 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/i_service_reverse.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/i_service_reverse.h @@ -33,6 +33,7 @@ public: public: virtual void BackupOnFileReady(std::string bundleName, std::string fileName, int fd) = 0; virtual void BackupOnBundleStarted(int32_t errCode, std::string bundleName) = 0; + virtual void BackupOnResultReport(std::string result) = 0; virtual void BackupOnBundleFinished(int32_t errCode, std::string bundleName) = 0; virtual void BackupOnAllBundlesFinished(int32_t errCode) = 0; @@ -44,6 +45,7 @@ public: virtual void IncrementalBackupOnFileReady(std::string bundleName, std::string fileName, int fd, int manifestFd) = 0; virtual void IncrementalBackupOnBundleStarted(int32_t errCode, std::string bundleName) = 0; + virtual void IncrementalBackupOnResultReport(std::string result) = 0; virtual void IncrementalBackupOnBundleFinished(int32_t errCode, std::string bundleName) = 0; virtual void IncrementalBackupOnAllBundlesFinished(int32_t errCode) = 0; @@ -54,6 +56,7 @@ public: std::string fileName, int fd, int manifestFd) = 0; + virtual void IncrementalRestoreOnResultReport(std::string result) = 0; DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.FileManagement.Backup.IServiceReverse") }; diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/i_service_reverse_ipc_interface_code.h b/interfaces/inner_api/native/backup_kit_inner/impl/i_service_reverse_ipc_interface_code.h index 1c3bfecbd492472fb7e76d114333568623a93cbc..a41ec673f0ede4d5870070b3873176113e109b5a 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/i_service_reverse_ipc_interface_code.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/i_service_reverse_ipc_interface_code.h @@ -23,6 +23,7 @@ enum class IServiceReverseInterfaceCode { SERVICER_BACKUP_ON_SUB_TASK_STARTED, SERVICER_BACKUP_ON_SUB_TASK_FINISHED, SERVICER_BACKUP_ON_TASK_FINISHED, + SERVICER_BACKUP_ON_RESULT_REPORT, SERVICER_RESTORE_ON_SUB_TASK_STARTED, SERVICER_RESTORE_ON_SUB_TASK_FINISHED, SERVICER_RESTORE_ON_TASK_FINISHED, @@ -32,10 +33,12 @@ enum class IServiceReverseInterfaceCode { SERVICER_INCREMENTAL_BACKUP_ON_SUB_TASK_STARTED, SERVICER_INCREMENTAL_BACKUP_ON_SUB_TASK_FINISHED, SERVICER_INCREMENTAL_BACKUP_ON_TASK_FINISHED, + SERVICER_INCREMENTAL_BACKUP_ON_RESULT_REPORT, SERVICER_INCREMENTAL_RESTORE_ON_SUB_TASK_STARTED, SERVICER_INCREMENTAL_RESTORE_ON_SUB_TASK_FINISHED, SERVICER_INCREMENTAL_RESTORE_ON_TASK_FINISHED, SERVICER_INCREMENTAL_RESTORE_ON_FILE_READY, + SERVICER_INCREMENTAL_RESTORE_ON_RESULT_REPORT, }; } // namespace OHOS::FileManagement::Backup diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/service_proxy.h b/interfaces/inner_api/native/backup_kit_inner/impl/service_proxy.h index d93b68e969f7087b3bf2199c25be0651f60bcdfc..f16380208df172d33741d9171df4678b83b7d878 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/service_proxy.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/service_proxy.h @@ -35,7 +35,7 @@ public: ErrCode PublishFile(const BFileInfo &fileInfo) override; ErrCode AppFileReady(const std::string &fileName, UniqueFd fd) override; ErrCode AppDone(ErrCode errCode) override; - ErrCode ServiceResultReport(const std::string &restoreRetInfo) override; + ErrCode ServiceResultReport(const std::string restoreRetInfo, BackupRestoreScenario scenario) override; ErrCode GetFileHandle(const std::string &bundleName, const std::string &fileName) override; ErrCode AppendBundlesRestoreSession(UniqueFd fd, const std::vector &bundleNames, diff --git a/interfaces/kits/js/backup/session_backup_n_exporter.cpp b/interfaces/kits/js/backup/session_backup_n_exporter.cpp index d3252402ca00a5a3a1e7b1e49dc75f4020317fd0..ecad353e8fc5097fc82e08793dfa479e80ebc5f8 100644 --- a/interfaces/kits/js/backup/session_backup_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_backup_n_exporter.cpp @@ -181,6 +181,29 @@ static void onAllBundlesEnd(weak_ptr pCallbacks, ErrCode err) callbacks->onAllBundlesEnd.ThreadSafeSchedule(cbCompl); } +static void OnResultReport(weak_ptr pCallbacks, const std::string result) +{ + HILOGI("callback function onResultReport begin"); + if (pCallbacks.expired()) { + HILOGI("callbacks is unbound"); + return; + } + auto callbacks = pCallbacks.lock(); + if (!callbacks) { + HILOGI("callback function onResultReport has already been released"); + return; + } + if (!bool(callbacks->onResultReport)) { + HILOGI("callback function onResultReport is undefined"); + return; + } + auto cbCompl = [res {result}](napi_env env, NError err) -> NVal { + NVal str = NVal::CreateUTF8String(env, res); + return str; + }; + callbacks->onResultReport.ThreadSafeSchedule(cbCompl); +} + static void OnBackupServiceDied(weak_ptr pCallbacks) { if (pCallbacks.expired()) { @@ -229,6 +252,7 @@ napi_value SessionBackupNExporter::Constructor(napi_env env, napi_callback_info .onBundleStarted = bind(onBundleBegin, backupEntity->callbacks, placeholders::_1, placeholders::_2), .onBundleFinished = bind(onBundleEnd, backupEntity->callbacks, placeholders::_1, placeholders::_2), .onAllBundlesFinished = bind(onAllBundlesEnd, backupEntity->callbacks, placeholders::_1), + .onResultReport = bind(OnResultReport, backupEntity->callbacks, placeholders::_1), .onBackupServiceDied = bind(OnBackupServiceDied, backupEntity->callbacks)}); if (!backupEntity->session) { NError(BError(BError::Codes::SDK_INVAL_ARG, "Failed to init backup").GetCode()).ThrowErr(env); diff --git a/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp b/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp index 6925a336ab7b4cd2304c44fb89405f155bb6e7b3..f371827281aae50a96bafa39f49b58cec7e66737 100644 --- a/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp @@ -187,6 +187,29 @@ static void onAllBundlesEnd(weak_ptr pCallbacks, ErrCode err) callbacks->onAllBundlesEnd.ThreadSafeSchedule(cbCompl); } +static void OnResultReport(weak_ptr pCallbacks, const std::string result) +{ + HILOGI("callback function onResultReport begin"); + if (pCallbacks.expired()) { + HILOGI("callbacks is unbound"); + return; + } + auto callbacks = pCallbacks.lock(); + if (!callbacks) { + HILOGI("callback function onResultReport has already been released"); + return; + } + if (!bool(callbacks->onResultReport)) { + HILOGI("callback function onResultReport is undefined"); + return; + } + auto cbCompl = [res {result}](napi_env env, NError err) -> NVal { + NVal str = NVal::CreateUTF8String(env, res); + return str; + }; + callbacks->onResultReport.ThreadSafeSchedule(cbCompl); +} + static void OnBackupServiceDied(weak_ptr pCallbacks) { if (pCallbacks.expired()) { @@ -235,6 +258,7 @@ napi_value SessionIncrementalBackupNExporter::Constructor(napi_env env, napi_cal .onBundleStarted = bind(onBundleBegin, backupEntity->callbacks, placeholders::_1, placeholders::_2), .onBundleFinished = bind(onBundleEnd, backupEntity->callbacks, placeholders::_1, placeholders::_2), .onAllBundlesFinished = bind(onAllBundlesEnd, backupEntity->callbacks, placeholders::_1), + .onResultReport = bind(OnResultReport, backupEntity->callbacks, placeholders::_1), .onBackupServiceDied = bind(OnBackupServiceDied, backupEntity->callbacks)}); if (!backupEntity->session) { NError(BError(BError::Codes::SDK_INVAL_ARG, "Failed to init backup").GetCode()).ThrowErr(env); diff --git a/interfaces/kits/js/backup/session_restore_n_exporter.cpp b/interfaces/kits/js/backup/session_restore_n_exporter.cpp index ba8b66b3082cf040005df22b110963a4ae0918fb..98db2f4ccb9f73a43ce6aa056bcecb7f6f1208e0 100644 --- a/interfaces/kits/js/backup/session_restore_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_restore_n_exporter.cpp @@ -247,7 +247,7 @@ static void OnBackupServiceDied(weak_ptr pCallbacks) callbacks->onBackupServiceDied.ThreadSafeSchedule(cbCompl); } -static void onResultReport(weak_ptr pCallbacks, const std::string result) +static void OnResultReport(weak_ptr pCallbacks, const std::string result) { HILOGI("callback function onResultReport begin."); if (pCallbacks.expired()) { @@ -263,16 +263,15 @@ static void onResultReport(weak_ptr pCallbacks, const std::str HILOGI("callback function onResultReport is undefined"); return; } - - auto cbCompl = [result {result}](napi_env env, NError err) -> NVal { - return NVal::CreateUTF8String(env, result); + auto cbCompl = [res {result}](napi_env env, NError err) -> NVal { + NVal str = NVal::CreateUTF8String(env, res); + return str; }; - callbacks->onResultReport.ThreadSafeSchedule(cbCompl); } -static bool DealArgs(NFuncArg &funcArg, int32_t fd, std::vector &bundles, - std::vector &bundleDetails, napi_env env) +static bool VerifyParamSuccess(NFuncArg &funcArg, int32_t &fd, std::vector &bundleNames, + std::vector &bundleInfos, napi_env env) { if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) { HILOGE("Number of arguments unmatched."); @@ -281,34 +280,36 @@ static bool DealArgs(NFuncArg &funcArg, int32_t fd, std::vector &bu } NVal remoteCap(env, funcArg[NARG_POS::FIRST]); auto [err, jsFd] = remoteCap.ToInt32(); - fd = jsFd; if (!err) { HILOGE("First argument is not remote capabilitily file number."); NError(BError(BError::Codes::SDK_INVAL_ARG, "First argument is not remote capabilitily file number.").GetCode()) .ThrowErr(env); return false; } + fd = jsFd; NVal jsBundles(env, funcArg[NARG_POS::SECOND]); - auto [succ, jsBundlesParam, ignore] = jsBundles.ToStringArray(); + auto [succ, jsBundleNames, ignore] = jsBundles.ToStringArray(); if (!succ) { HILOGE("First argument is not bundles array."); NError(BError(BError::Codes::SDK_INVAL_ARG, "First argument is not bundles array.").GetCode()).ThrowErr(env); return false; } - bundles = jsBundlesParam; - NVal jsDetails(env, funcArg[NARG_POS::THIRD]); - if (jsDetails.TypeIs(napi_undefined) || jsDetails.TypeIs(napi_null)) { + bundleNames = jsBundleNames; + NVal jsInfos(env, funcArg[NARG_POS::THIRD]); + if (jsInfos.TypeIs(napi_undefined) || jsInfos.TypeIs(napi_null)) { HILOGW("Third param is not exist"); - } else { - auto [deSuc, jsBundleDetails, deIgnore] = jsDetails.ToStringArray(); - if (!deSuc) { - HILOGE("Third argument is not bundles array."); - NError(BError(BError::Codes::SDK_INVAL_ARG, "Third argument is not bundles array.").GetCode()) - .ThrowErr(env); + return true; + } + auto [deSuc, jsBundleInfos, deIgnore] = jsInfos.ToStringArray(); + if (deSuc) { + bundleInfos = jsBundleInfos; + if (bundleNames.size() != bundleInfos.size()) { + HILOGE("bundleNames count is not equals bundleInfos count"); return false; } - bundleDetails = jsBundleDetails; + return true; } + HILOGI("Third param is callback"); return true; } @@ -341,6 +342,7 @@ napi_value SessionRestoreNExporter::Constructor(napi_env env, napi_callback_info .onBundleStarted = bind(onBundleBegin, restoreEntity->callbacks, placeholders::_1, placeholders::_2), .onBundleFinished = bind(onBundleEnd, restoreEntity->callbacks, placeholders::_1, placeholders::_2), .onAllBundlesFinished = bind(onAllBundlesEnd, restoreEntity->callbacks, placeholders::_1), + .onResultReport = bind(OnResultReport, restoreEntity->callbacks, placeholders::_1), .onBackupServiceDied = bind(OnBackupServiceDied, restoreEntity->callbacks)}); } else { restoreEntity->sessionSheet = nullptr; @@ -349,7 +351,7 @@ napi_value SessionRestoreNExporter::Constructor(napi_env env, napi_callback_info .onBundleStarted = bind(onBundleBegin, restoreEntity->callbacks, placeholders::_1, placeholders::_2), .onBundleFinished = bind(onBundleEnd, restoreEntity->callbacks, placeholders::_1, placeholders::_2), .onAllBundlesFinished = bind(onAllBundlesEnd, restoreEntity->callbacks, placeholders::_1), - .onResultReport = bind(onResultReport, restoreEntity->callbacks, placeholders::_1), + .onResultReport = bind(OnResultReport, restoreEntity->callbacks, placeholders::_1), .onBackupServiceDied = bind(OnBackupServiceDied, restoreEntity->callbacks)}); } if (!restoreEntity->sessionWhole && !restoreEntity->sessionSheet) { @@ -369,11 +371,11 @@ napi_value SessionRestoreNExporter::Constructor(napi_env env, napi_callback_info napi_value SessionRestoreNExporter::AppendBundles(napi_env env, napi_callback_info cbinfo) { HILOGI("called SessionRestore::AppendBundles begin"); - int32_t fd = -1; - std::vector bundles; - std::vector bundleDetails; + int32_t fd = BConstants::INVALID_FD_NUM; + std::vector bundleNames; + std::vector bundleInfos; NFuncArg funcArg(env, cbinfo); - if (!DealArgs(funcArg, fd, bundles, bundleDetails, env)) { + if (!VerifyParamSuccess(funcArg, fd, bundleNames, bundleInfos, env)) { return nullptr; } auto restoreEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); @@ -382,18 +384,18 @@ napi_value SessionRestoreNExporter::AppendBundles(napi_env env, napi_callback_in NError(BError(BError::Codes::SDK_INVAL_ARG, "Failed to get RestoreSession entity.").GetCode()).ThrowErr(env); return nullptr; } - auto cbExec = [entity {restoreEntity}, fd {fd}, bundles {bundles}, bundleDetails {bundleDetails}]() -> NError { + auto cbExec = [entity {restoreEntity}, fd {fd}, bundles {bundleNames}, infos {bundleInfos}]() -> NError { if (!(entity && (entity->sessionWhole || entity->sessionSheet))) { return NError(BError(BError::Codes::SDK_INVAL_ARG, "restore session is nullptr").GetCode()); } if (entity->sessionWhole) { - if (!bundleDetails.empty()) { - return NError(entity->sessionWhole->AppendBundles(UniqueFd(fd), bundles, bundleDetails)); + if (!infos.empty()) { + return NError(entity->sessionWhole->AppendBundles(UniqueFd(fd), bundles, infos)); } return NError(entity->sessionWhole->AppendBundles(UniqueFd(fd), bundles)); } - if (!bundleDetails.empty()) { - return NError(entity->sessionSheet->AppendBundles(UniqueFd(fd), bundles, bundleDetails)); + if (!infos.empty()) { + return NError(entity->sessionSheet->AppendBundles(UniqueFd(fd), bundles, infos)); } return NError(entity->sessionSheet->AppendBundles(UniqueFd(fd), bundles)); }; @@ -404,7 +406,11 @@ napi_value SessionRestoreNExporter::AppendBundles(napi_env env, napi_callback_in NVal thisVar(env, funcArg.GetThisVar()); if (funcArg.GetArgc() == NARG_CNT::TWO) { return NAsyncWorkPromise(env, thisVar).Schedule(className, cbExec, cbCompl).val_; + } else if (!bundleInfos.empty()) { + HILOGI("The third param is string array"); + return NAsyncWorkPromise(env, thisVar).Schedule(className, cbExec, cbCompl).val_; } else { + HILOGI("The third param is call back"); NVal cb(env, funcArg[NARG_POS::THIRD]); return NAsyncWorkCallback(env, thisVar, cb).Schedule(className, cbExec, cbCompl).val_; } diff --git a/services/backup_sa/include/module_ipc/service.h b/services/backup_sa/include/module_ipc/service.h index 21b65a93272703abc7773ced32ef0f1799ee8010..0ffea74ba9180ea3478ebe6d65e898a2a58c4c03 100644 --- a/services/backup_sa/include/module_ipc/service.h +++ b/services/backup_sa/include/module_ipc/service.h @@ -21,6 +21,7 @@ #include "b_jsonutil/b_jsonutil.h" #include "b_json/b_json_entity_caps.h" +#include "b_resources/b_constants.h" #include "i_service_reverse.h" #include "iremote_stub.h" #include "module_sched/sched_scheduler.h" @@ -41,11 +42,11 @@ public: ErrCode PublishFile(const BFileInfo &fileInfo) override; ErrCode AppFileReady(const std::string &fileName, UniqueFd fd) override; ErrCode AppDone(ErrCode errCode) override; - ErrCode ServiceResultReport(const std::string &restoreRetInfo) override; + ErrCode ServiceResultReport(const std::string restoreRetInfo, BackupRestoreScenario sennario) override; ErrCode GetFileHandle(const std::string &bundleName, const std::string &fileName) override; ErrCode AppendBundlesRestoreSession(UniqueFd fd, const std::vector &bundleNames, - const std::vector &detailInfos, + const std::vector &bundleInfos, RestoreTypeEnum restoreType = RestoreTypeEnum::RESTORE_DATA_WAIT_SEND, int32_t userId = DEFAULT_INVAL_VALUE) override; ErrCode AppendBundlesRestoreSession(UniqueFd fd, diff --git a/services/backup_sa/include/module_ipc/service_reverse_proxy.h b/services/backup_sa/include/module_ipc/service_reverse_proxy.h index 14923ee45085f69e3344aeb2299ca0cc2ba450bb..a56c89f6acfb9f67b9a2bcc60e335bf9dc2dab63 100644 --- a/services/backup_sa/include/module_ipc/service_reverse_proxy.h +++ b/services/backup_sa/include/module_ipc/service_reverse_proxy.h @@ -24,6 +24,7 @@ class ServiceReverseProxy final : public IRemoteProxy, protecte public: void BackupOnFileReady(std::string bundleName, std::string fileName, int fd) override; void BackupOnBundleStarted(int32_t errCode, std::string bundleName) override; + void BackupOnResultReport(std::string result) override; void BackupOnBundleFinished(int32_t errCode, std::string bundleName) override; void BackupOnAllBundlesFinished(int32_t errCode) override; @@ -35,6 +36,7 @@ public: void IncrementalBackupOnFileReady(std::string bundleName, std::string fileName, int fd, int manifestFd) override; void IncrementalBackupOnBundleStarted(int32_t errCode, std::string bundleName) override; + void IncrementalBackupOnResultReport(std::string result) override; void IncrementalBackupOnBundleFinished(int32_t errCode, std::string bundleName) override; void IncrementalBackupOnAllBundlesFinished(int32_t errCode) override; @@ -42,6 +44,7 @@ public: void IncrementalRestoreOnBundleFinished(int32_t errCode, std::string bundleName) override; void IncrementalRestoreOnAllBundlesFinished(int32_t errCode) override; void IncrementalRestoreOnFileReady(std::string bundleName, std::string fileName, int fd, int manifestFd) override; + void IncrementalRestoreOnResultReport(std::string result) override; public: explicit ServiceReverseProxy(const sptr &impl) : IRemoteProxy(impl) {} diff --git a/services/backup_sa/src/module_external/bms_adapter.cpp b/services/backup_sa/src/module_external/bms_adapter.cpp index 1ae0c15dbcd1ff2bc6efe611bf615b9f0e2667f5..1e5cf4ec3cf9c45724ac243825b2ac3758d12f9f 100644 --- a/services/backup_sa/src/module_external/bms_adapter.cpp +++ b/services/backup_sa/src/module_external/bms_adapter.cpp @@ -102,8 +102,8 @@ static int64_t GetBundleStats(const string &bundleName, int32_t userId) bundleStats[i] = 0; } } - int64_t dataSize_ = bundleStats[LOCAL] + bundleStats[DISTRIBUTED] + bundleStats[DATABASE]; - return dataSize_; + int64_t dataSize = bundleStats[LOCAL] + bundleStats[DISTRIBUTED] + bundleStats[DATABASE]; + return dataSize; } vector BundleMgrAdapter::GetBundleInfos(const vector &bundleNames, int32_t userId) @@ -114,7 +114,7 @@ vector BundleMgrAdapter::GetBundleInfos(const vecto HILOGI("Begin Get bundleName:%{public}s", bundleName.c_str()); AppExecFwk::BundleInfo installedBundle; if (!bms->GetBundleInfo(bundleName, AppExecFwk::GET_BUNDLE_WITH_EXTENSION_INFO, installedBundle, userId)) { - throw BError(BError::Codes::SA_BROKEN_IPC, "Failed to get bundle info"); + throw BError(BError::Codes::SA_BUNDLE_INFO_EMPTY, "Failed to get bundle info"); } if (installedBundle.applicationInfo.codePath == HMOS_HAP_CODE_PATH || installedBundle.applicationInfo.codePath == LINUX_HAP_CODE_PATH) { diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index 8442b27592f1bd5b0796fb26638b5f31f0cfc2e9..b7f4bb953def0bec6bd4e87bab4d74e3f4d634bd 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -312,7 +312,7 @@ static vector GetRestoreBundleNames(UniqueFd fd, } ErrCode Service::AppendBundlesRestoreSession(UniqueFd fd, const vector &bundleNames, - const std::vector &detailInfos, RestoreTypeEnum restoreType, int32_t userId) + const std::vector &bundleInfos, RestoreTypeEnum restoreType, int32_t userId) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); try { @@ -322,12 +322,10 @@ ErrCode Service::AppendBundlesRestoreSession(UniqueFd fd, const vectorSetSessionUserId(userId); } VerifyCaller(IServiceReverse::Scenario::RESTORE); - std::vector hosBundleNames; - std::vector bundleDetails = - BJsonUtil::ConvertBundleDetailInfos(bundleNames, detailInfos, ":", hosBundleNames, userId); - std::map bundleNameDetailMap; - BJsonUtil::RecordBundleDetailRelation(bundleNameDetailMap, bundleDetails); - auto restoreInfos = GetRestoreBundleNames(move(fd), session_, hosBundleNames); + std::vector bundleNamesOnly; + std::map bundleNameDetailMap = + BJsonUtil::BuildBundleInfos(bundleNames, bundleInfos, bundleNamesOnly, userId); + auto restoreInfos = GetRestoreBundleNames(move(fd), session_, bundleNamesOnly); auto restoreBundleNames = SvcRestoreDepsManager::GetInstance().GetRestoreBundleNames(restoreInfos, restoreType); if (restoreBundleNames.empty()) { session_->DecreaseSessionCnt(); @@ -368,8 +366,27 @@ ErrCode Service::AppendBundlesRestoreSession(UniqueFd fd, return BError(BError::Codes::OK); } session_->AppendBundles(restoreBundleNames); - std::map bundleNameDetailMap; - SetCurrentSessProperties(restoreInfos, restoreBundleNames, bundleNameDetailMap, restoreType); + for (auto restoreInfo : restoreInfos) { + auto it = find_if(restoreBundleNames.begin(), restoreBundleNames.end(), + [&restoreInfo](const auto &bundleName) { return bundleName == restoreInfo.name; }); + if (it == restoreBundleNames.end()) { + throw BError(BError::Codes::SA_BUNDLE_INFO_EMPTY, "Can't find bundle name"); + } + HILOGD("bundleName: %{public}s, extensionName: %{public}s", restoreInfo.name.c_str(), + restoreInfo.extensionName.c_str()); + if ((restoreInfo.allToBackup == false && + !SpeicalVersion(restoreInfo.versionName, restoreInfo.versionCode)) || + restoreInfo.extensionName.empty()) { + OnBundleStarted(BError(BError::Codes::SA_FORBID_BACKUP_RESTORE), session_, restoreInfo.name); + session_->RemoveExtInfo(restoreInfo.name); + continue; + } + session_->SetBundleRestoreType(restoreInfo.name, restoreType); + session_->SetBundleVersionCode(restoreInfo.name, restoreInfo.versionCode); + session_->SetBundleVersionName(restoreInfo.name, restoreInfo.versionName); + session_->SetBundleDataSize(restoreInfo.name, restoreInfo.spaceOccupied); + session_->SetBackupExtName(restoreInfo.name, restoreInfo.extensionName); + } OnStartSched(); session_->DecreaseSessionCnt(); return BError(BError::Codes::OK); @@ -513,6 +530,7 @@ ErrCode Service::AppFileReady(const string &fileName, UniqueFd fd) try { HILOGI("Begin"); string callerName = VerifyCallerAndGetCallerName(); + HILOGI("Caller name is:%{public}s", callerName.c_str()); if (fileName.find('/') != string::npos) { throw BError(BError::Codes::SA_INVAL_ARG, "Filename is not valid"); } @@ -588,12 +606,21 @@ ErrCode Service::AppDone(ErrCode errCode) } } -ErrCode Service::ServiceResultReport(const std::string &restoreRetInfo) +ErrCode Service::ServiceResultReport(const std::string restoreRetInfo, BackupRestoreScenario sennario) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); try { HILOGI("Begin"); - session_->GetServiceReverseProxy()->RestoreOnResultReport(restoreRetInfo); + if (sennario == BackupRestoreScenario::FULL_RESTORE) { + session_->GetServiceReverseProxy()->RestoreOnResultReport(restoreRetInfo); + } else if (sennario == BackupRestoreScenario::INCREMENTAL_RESTORE) { + session_->GetServiceReverseProxy()->IncrementalRestoreOnResultReport(restoreRetInfo); + } else if (sennario == BackupRestoreScenario::FULL_BACKUP) { + session_->GetServiceReverseProxy()->BackupOnResultReport(restoreRetInfo); + } else if (sennario == BackupRestoreScenario::INCREMENTAL_BACKUP) { + session_->GetServiceReverseProxy()->IncrementalBackupOnResultReport(restoreRetInfo); + } + return BError(BError::Codes::OK); } catch (const BError &e) { return e.GetCode(); // 任意异常产生,终止监听该任务 diff --git a/services/backup_sa/src/module_ipc/service_incremental.cpp b/services/backup_sa/src/module_ipc/service_incremental.cpp index cec2eeec1eaf6103dca4842f91ac479681b2d93f..2a536f8c6ca6d520d65f0b4d26b3dc14ceca87b0 100644 --- a/services/backup_sa/src/module_ipc/service_incremental.cpp +++ b/services/backup_sa/src/module_ipc/service_incremental.cpp @@ -184,7 +184,7 @@ ErrCode Service::PublishIncrementalFile(const BFileInfo &fileInfo) try { HILOGI("Begin"); VerifyCaller(IServiceReverse::Scenario::RESTORE); - + HILOGI("Start get ExtConnection, bundleName:%{public}s", fileInfo.owner.c_str()); auto backUpConnection = session_->GetExtConnection(fileInfo.owner); auto proxy = backUpConnection->GetBackupExtProxy(); if (!proxy) { @@ -194,7 +194,6 @@ ErrCode Service::PublishIncrementalFile(const BFileInfo &fileInfo) if (res) { HILOGE("Failed to publish file for backup extension"); } - return res; } catch (const BError &e) { return e.GetCode(); diff --git a/services/backup_sa/src/module_ipc/service_incremental_reverse_proxy.cpp b/services/backup_sa/src/module_ipc/service_incremental_reverse_proxy.cpp index b190a820ca5241d177fa8940d505431bc5f1af5d..ce5df96e7d79f0d73b882fad2839aa2a0865b7b8 100644 --- a/services/backup_sa/src/module_ipc/service_incremental_reverse_proxy.cpp +++ b/services/backup_sa/src/module_ipc/service_incremental_reverse_proxy.cpp @@ -62,6 +62,25 @@ void ServiceReverseProxy::IncrementalBackupOnBundleStarted(int32_t errCode, stri } } +void ServiceReverseProxy::IncrementalBackupOnResultReport(std::string result) +{ + HILOGI("Begin"); + BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteString(result)) { + throw BError(BError::Codes::SA_BROKEN_IPC); + }; + + MessageParcel reply; + MessageOption option; + if (int err = Remote()->SendRequest( + static_cast(IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_BACKUP_ON_RESULT_REPORT), data, + reply, option); + err != ERR_OK) { + throw BError(BError::Codes::SA_BROKEN_IPC, to_string(err)); + } +} + void ServiceReverseProxy::IncrementalBackupOnBundleFinished(int32_t errCode, string bundleName) { HILOGI("Begin"); @@ -161,6 +180,25 @@ void ServiceReverseProxy::IncrementalRestoreOnAllBundlesFinished(int32_t errCode } } +void ServiceReverseProxy::IncrementalRestoreOnResultReport(std::string result) +{ + HILOGI("Begin"); + BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteString(result)) { + throw BError(BError::Codes::SA_BROKEN_IPC); + } + + MessageParcel reply; + MessageOption option; + if (int err = Remote()->SendRequest( + static_cast(IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_RESTORE_ON_RESULT_REPORT), data, + reply, option); + err != ERR_OK) { + throw BError(BError::Codes::SA_BROKEN_IPC, to_string(err)); + } +} + void ServiceReverseProxy::IncrementalRestoreOnFileReady(string bundleName, string fileName, int fd, int manifestFd) { HILOGI("Begin"); diff --git a/services/backup_sa/src/module_ipc/service_reverse_proxy.cpp b/services/backup_sa/src/module_ipc/service_reverse_proxy.cpp index a0f5a510cf5da76cd023a978d089395f20aa04df..0e3011c410c3c0271263ec76fedd35a7577b90b3 100644 --- a/services/backup_sa/src/module_ipc/service_reverse_proxy.cpp +++ b/services/backup_sa/src/module_ipc/service_reverse_proxy.cpp @@ -61,6 +61,25 @@ void ServiceReverseProxy::BackupOnBundleStarted(int32_t errCode, string bundleNa } } +void ServiceReverseProxy::BackupOnResultReport(std::string result) +{ + HILOGI("Begin"); + BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteString(result)) { + throw BError(BError::Codes::SA_BROKEN_IPC); + }; + + MessageParcel reply; + MessageOption option; + if (int err = Remote()->SendRequest( + static_cast(IServiceReverseInterfaceCode::SERVICER_BACKUP_ON_RESULT_REPORT), data, reply, + option); + err != ERR_OK) { + throw BError(BError::Codes::SA_BROKEN_IPC, to_string(err)); + } +} + void ServiceReverseProxy::BackupOnBundleFinished(int32_t errCode, string bundleName) { HILOGI("Begin"); diff --git a/services/backup_sa/src/module_ipc/service_stub.cpp b/services/backup_sa/src/module_ipc/service_stub.cpp index 1810b62f108ab973c663f7be8276d620bc84b4bd..6da1b23e55d7015b13f0ba251afaf3397fb4e705 100644 --- a/services/backup_sa/src/module_ipc/service_stub.cpp +++ b/services/backup_sa/src/module_ipc/service_stub.cpp @@ -217,10 +217,15 @@ int32_t ServiceStub::CmdResultReport(MessageParcel &data, MessageParcel &reply) { HILOGI("Begin"); std::string restoreRetInfo; + int32_t scenario; if (!data.ReadString(restoreRetInfo)) { return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive restoreRetInfo"); } - int res = ServiceResultReport(restoreRetInfo); + if (!data.ReadInt32(scenario)) { + return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive scenario"); + } + BackupRestoreScenario secenrioInfo = static_cast(scenario); + int res = ServiceResultReport(restoreRetInfo, secenrioInfo); if (!reply.WriteInt32(res)) { stringstream ss; ss << "Failed to send the result " << res; diff --git a/services/backup_sa/src/module_ipc/svc_session_manager.cpp b/services/backup_sa/src/module_ipc/svc_session_manager.cpp index 34c3b55b1ed62a5a8b6eef26bbed23824246a0f8..34fa0c96b99d12f0d80db32e0f5d3886ba651618 100644 --- a/services/backup_sa/src/module_ipc/svc_session_manager.cpp +++ b/services/backup_sa/src/module_ipc/svc_session_manager.cpp @@ -136,7 +136,7 @@ bool SvcSessionManager::OnBunleFileReady(const string &bundleName, const string if (!impl_.clientToken) { throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); } - + HILOGI("Begin, bundleName name is:%{public}s", bundleName.c_str()); auto it = impl_.backupExtNameMap.find(bundleName); if (it == impl_.backupExtNameMap.end()) { stringstream ss; @@ -162,6 +162,7 @@ bool SvcSessionManager::OnBunleFileReady(const string &bundleName, const string return true; } } + HILOGI("End, bundleName name is:%{public}s", bundleName.c_str()); return false; } diff --git a/tests/mock/backup_kit_inner/service_proxy_mock.cpp b/tests/mock/backup_kit_inner/service_proxy_mock.cpp index 76a6a9897150e0ee5e94d2ec7c6b09c61edc6562..c27e3007fb6efb58e26960b64ec5b67087ebd467 100644 --- a/tests/mock/backup_kit_inner/service_proxy_mock.cpp +++ b/tests/mock/backup_kit_inner/service_proxy_mock.cpp @@ -75,7 +75,7 @@ ErrCode ServiceProxy::AppDone(ErrCode errCode) return BError(BError::Codes::OK); } -ErrCode ServiceProxy::ServiceResultReport(const std::string &restoreRetInfo) +ErrCode ServiceProxy::ServiceResultReport(const std::string restoreRetInfo, BackupRestoreScenario scenario) { return BError(BError::Codes::OK); } diff --git a/tests/mock/module_ipc/service_mock.cpp b/tests/mock/module_ipc/service_mock.cpp index 50511c6023828ba61b87ee4e0fbd4e4ba9efdf55..8eea6f764408c68f0ec9ad9176da02792f093901 100644 --- a/tests/mock/module_ipc/service_mock.cpp +++ b/tests/mock/module_ipc/service_mock.cpp @@ -72,7 +72,7 @@ ErrCode Service::AppDone(ErrCode errCode) return BError(BError::Codes::OK); } -ErrCode Service::ServiceResultReport(const std::string &restoreRetInfo) +ErrCode Service::ServiceResultReport(const std::string restoreRetInfo, BackupRestoreScenario sennario) { return BError(BError::Codes::OK); } diff --git a/tests/mock/module_ipc/service_reverse_proxy_mock.cpp b/tests/mock/module_ipc/service_reverse_proxy_mock.cpp index c52642c438c16c5289df37a7bce396cff527b39a..58ab5d82dc06788ab5faaa01996250fab1294156 100644 --- a/tests/mock/module_ipc/service_reverse_proxy_mock.cpp +++ b/tests/mock/module_ipc/service_reverse_proxy_mock.cpp @@ -24,6 +24,8 @@ void ServiceReverseProxy::BackupOnFileReady(string bundleName, string fileName, void ServiceReverseProxy::BackupOnBundleStarted(int32_t errCode, string bundleName) {} +void ServiceReverseProxy::BackupOnResultReport(string result) {} + void ServiceReverseProxy::BackupOnBundleFinished(int32_t errCode, string bundleName) {} void ServiceReverseProxy::BackupOnAllBundlesFinished(int32_t errCode) {} @@ -42,6 +44,8 @@ void ServiceReverseProxy::IncrementalBackupOnFileReady(string bundleName, string void ServiceReverseProxy::IncrementalBackupOnBundleStarted(int32_t errCode, string bundleName) {} +void ServiceReverseProxy::IncrementalBackupOnResultReport(string result) {} + void ServiceReverseProxy::IncrementalBackupOnBundleFinished(int32_t errCode, string bundleName) {} void ServiceReverseProxy::IncrementalBackupOnAllBundlesFinished(int32_t errCode) {} @@ -53,4 +57,6 @@ void ServiceReverseProxy::IncrementalRestoreOnBundleFinished(int32_t errCode, st void ServiceReverseProxy::IncrementalRestoreOnAllBundlesFinished(int32_t errCode) {} void ServiceReverseProxy::IncrementalRestoreOnFileReady(string bundleName, string fileName, int fd, int manifestFd) {} + +void ServiceReverseProxy::IncrementalRestoreOnResultReport(string result) {} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/tests/mock/module_ipc/service_stub_mock.cpp b/tests/mock/module_ipc/service_stub_mock.cpp index a703f0c08637dc15d24f5e4bdb465bac7892c98e..24002a3508608efcf651e697fb3780d54dd823d6 100644 --- a/tests/mock/module_ipc/service_stub_mock.cpp +++ b/tests/mock/module_ipc/service_stub_mock.cpp @@ -18,6 +18,7 @@ #include #include "b_error/b_error.h" +#include "b_resources/b_constants.h" #include "module_ipc/service_reverse_proxy.h" namespace OHOS::FileManagement::Backup { @@ -147,7 +148,10 @@ int32_t ServiceStub::CmdResultReport(MessageParcel &data, MessageParcel &reply) { std::string restoreRetInfo; data.ReadString(restoreRetInfo); - int res = ServiceResultReport(restoreRetInfo); + int32_t scenario; + data.ReadInt32(scenario); + BackupRestoreScenario type = static_cast(scenario); + int res = ServiceResultReport(restoreRetInfo, type); reply.WriteInt32(res); return BError(BError::Codes::OK); } diff --git a/tests/unittests/backup_api/backup_impl/include/i_service_mock.h b/tests/unittests/backup_api/backup_impl/include/i_service_mock.h index 5dae931be363d439416bb2522c0575868b7c7f4f..227ff21ac765b8b2ad09d28b21c2093435356a7b 100644 --- a/tests/unittests/backup_api/backup_impl/include/i_service_mock.h +++ b/tests/unittests/backup_api/backup_impl/include/i_service_mock.h @@ -23,6 +23,7 @@ #include "b_error/b_error.h" #include "i_service.h" +#include "b_resources/b_constants.h" #include "iremote_stub.h" #include "test_manager.h" @@ -88,7 +89,7 @@ public: return BError(BError::Codes::OK); } - ErrCode ServiceResultReport(const std::string &restoreRetInfo) override + ErrCode ServiceResultReport(const std::string restoreRetInfo, BackupRestoreScenario scenario) override { return BError(BError::Codes::OK); } diff --git a/tests/unittests/backup_api/backup_impl/include/service_reverse_mock.h b/tests/unittests/backup_api/backup_impl/include/service_reverse_mock.h index a9a0539adeaf951b0e00fc5c5b00656f93370a6a..3205e512e2a9606213ef13fa48f0f210a1f6808a 100644 --- a/tests/unittests/backup_api/backup_impl/include/service_reverse_mock.h +++ b/tests/unittests/backup_api/backup_impl/include/service_reverse_mock.h @@ -38,6 +38,7 @@ public: } void BackupOnFileReady(std::string bundleName, std::string fileName, int fd) override {} void BackupOnBundleStarted(int32_t errCode, std::string bundleName) override {} + void BackupOnResultReport(std::string result) override {}; void BackupOnBundleFinished(int32_t errCode, std::string bundleName) override {} void BackupOnAllBundlesFinished(int32_t errCode) override {} @@ -49,6 +50,7 @@ public: void IncrementalBackupOnFileReady(std::string bundleName, std::string fileName, int fd, int manifestFd) override {} void IncrementalBackupOnBundleStarted(int32_t errCode, std::string bundleName) override {} + void IncrementalBackupOnResultReport(std::string result) override {} void IncrementalBackupOnBundleFinished(int32_t errCode, std::string bundleName) override {} void IncrementalBackupOnAllBundlesFinished(int32_t errCode) override {} @@ -56,6 +58,7 @@ public: void IncrementalRestoreOnBundleFinished(int32_t errCode, std::string bundleName) override {} void IncrementalRestoreOnAllBundlesFinished(int32_t errCode) override {} void IncrementalRestoreOnFileReady(std::string bundleName, std::string fileName, int fd, int manifestFd) override {} + void IncrementalRestoreOnResultReport(std::string result) override {}; }; } // namespace OHOS::FileManagement::Backup #endif // MOCK_SERVICE_REVERSE_MOCK_H \ No newline at end of file diff --git a/tests/unittests/backup_api/backup_impl/service_reverse_stub_test.cpp b/tests/unittests/backup_api/backup_impl/service_reverse_stub_test.cpp index 10cf595c8d500509d2a767c65f3211f3c20f79c3..8d1b8ff08835cfb4d88bc05077612c2dec8924a5 100644 --- a/tests/unittests/backup_api/backup_impl/service_reverse_stub_test.cpp +++ b/tests/unittests/backup_api/backup_impl/service_reverse_stub_test.cpp @@ -42,6 +42,7 @@ class MockServiceReverse final : public ServiceReverseStub { public: MOCK_METHOD3(BackupOnFileReady, void(string bundleName, string fileName, int fd)); MOCK_METHOD2(BackupOnBundleStarted, void(int32_t errCode, string bundleName)); + MOCK_METHOD1(BackupOnResultReport, void(string result)); MOCK_METHOD2(BackupOnBundleFinished, void(int32_t errCode, string bundleName)); MOCK_METHOD1(BackupOnAllBundlesFinished, void(int32_t errCode)); MOCK_METHOD2(RestoreOnBundleStarted, void(int32_t errCode, std::string bundleName)); @@ -51,12 +52,14 @@ public: MOCK_METHOD1(RestoreOnResultReport, void(string result)); MOCK_METHOD4(IncrementalBackupOnFileReady, void(string bundleName, string fileName, int fd, int manifestFd)); MOCK_METHOD2(IncrementalBackupOnBundleStarted, void(int32_t errCode, string bundleName)); + MOCK_METHOD1(IncrementalBackupOnResultReport, void(string result)); MOCK_METHOD2(IncrementalBackupOnBundleFinished, void(int32_t errCode, string bundleName)); MOCK_METHOD1(IncrementalBackupOnAllBundlesFinished, void(int32_t errCode)); MOCK_METHOD2(IncrementalRestoreOnBundleStarted, void(int32_t errCode, std::string bundleName)); MOCK_METHOD2(IncrementalRestoreOnBundleFinished, void(int32_t errCode, string bundleName)); MOCK_METHOD1(IncrementalRestoreOnAllBundlesFinished, void(int32_t errCode)); MOCK_METHOD4(IncrementalRestoreOnFileReady, void(string bundleName, string fileName, int fd, int manifestFd)); + MOCK_METHOD1(IncrementalRestoreOnResultReport, void(string result)); }; class ServiceReverseStubTest : public testing::Test { @@ -141,6 +144,41 @@ HWTEST_F(ServiceReverseStubTest, GTEST_LOG_(INFO) << "ServiceReverseStubTest-end SUB_backup_ServiceReverseStub_BackupOnBundleStarted_0100"; } +/** + * @tc.number: SUB_backup_ServiceReverseStub_BackupOnResultReport_0100 + * @tc.name: SUB_backup_ServiceReverseStub_BackupOnResultReport_0100 + * @tc.desc: Test function of BackupOnResultReport interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(ServiceReverseStubTest, + SUB_backup_ServiceReverseStub_BackupOnResultReport_0100, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceReverseStubTest-begin SUB_backup_ServiceReverseStub_BackupOnResultReport_0100"; + try { + MockServiceReverse service; + EXPECT_CALL(service, BackupOnResultReport(_)).WillOnce(Return()); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + EXPECT_TRUE(data.WriteInterfaceToken(IServiceReverse::GetDescriptor())); + EXPECT_TRUE(data.WriteString(BUNDLE_NAME)); + + EXPECT_EQ(BError(BError::Codes::OK), + service.OnRemoteRequest( + static_cast(IServiceReverseInterfaceCode::SERVICER_BACKUP_ON_RESULT_REPORT), data, + reply, option)); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceReverseStubTest-an exception occurred by BackupOnResultReport."; + } + GTEST_LOG_(INFO) << "ServiceReverseStubTest-end SUB_backup_ServiceReverseStub_BackupOnResultReport_0100"; +} + /** * @tc.number: SUB_backup_ServiceReverseStub_BackupOnBundleFinished_0100 * @tc.name: SUB_backup_ServiceReverseStub_BackupOnBundleFinished_0100 @@ -472,6 +510,44 @@ HWTEST_F(ServiceReverseStubTest, << "ServiceReverseStubTest-end SUB_backup_ServiceReverseStub_IncrementalBackupOnBundleStarted_0100"; } +/** + * @tc.number: SUB_backup_ServiceReverseStub_IncrementalBackupOnResultReport_0100 + * @tc.name: SUB_backup_ServiceReverseStub_IncrementalBackupOnResultReport_0100 + * @tc.desc: Test function of IncrementalBackupOnResultReport interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I90ZZX + */ +HWTEST_F(ServiceReverseStubTest, + SUB_backup_ServiceReverseStub_IncrementalBackupOnResultReport_0100, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) + << "ServiceReverseStubTest-begin SUB_backup_ServiceReverseStub_IncrementalBackupOnResultReport_0100"; + try { + MockServiceReverse service; + EXPECT_CALL(service, IncrementalBackupOnResultReport(_)).WillOnce(Return()); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + EXPECT_TRUE(data.WriteInterfaceToken(IServiceReverse::GetDescriptor())); + EXPECT_TRUE(data.WriteString(BUNDLE_NAME)); + + EXPECT_EQ( + BError(BError::Codes::OK), + service.OnRemoteRequest( + static_cast(IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_BACKUP_ON_RESULT_REPORT), + data, reply, option)); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceReverseStubTest-an exception occurred by IncrementalBackupOnResultReport."; + } + GTEST_LOG_(INFO) + << "ServiceReverseStubTest-end SUB_backup_ServiceReverseStub_IncrementalBackupOnResultReport_0100"; +} + /** * @tc.number: SUB_backup_ServiceReverseStub_IncrementalBackupOnBundleFinished_0100 * @tc.name: SUB_backup_ServiceReverseStub_IncrementalBackupOnBundleFinished_0100 @@ -746,4 +822,42 @@ HWTEST_F(ServiceReverseStubTest, GTEST_LOG_(INFO) << "ServiceReverseStubTest-end SUB_backup_ServiceReverseStub_RestoreOnResultReport_0100"; } + +/** + * @tc.number: SUB_backup_ServiceReverseStub_IncrementalRestoreOnResultReport_0100 + * @tc.name: SUB_backup_ServiceReverseStub_IncrementalRestoreOnResultReport_0100 + * @tc.desc: Test function of IncrementalRestoreOnResultReport interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I90ZZX + */ +HWTEST_F(ServiceReverseStubTest, + SUB_backup_ServiceReverseStub_IncrementalRestoreOnResultReport_0100, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) + << "ServiceReverseStubTest-begin SUB_backup_ServiceReverseStub_RestoreOnResultReport_0100"; + try { + MockServiceReverse service; + EXPECT_CALL(service, IncrementalRestoreOnResultReport(_)).WillOnce(Return()); + MessageParcel data; + MessageParcel reply; + MessageOption option; + std::string resultReport = "result_report"; + EXPECT_TRUE(data.WriteInterfaceToken(IServiceReverse::GetDescriptor())); + EXPECT_TRUE(data.WriteString(resultReport)); + + EXPECT_EQ( + BError(BError::Codes::OK), + service.OnRemoteRequest( + static_cast(IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_RESTORE_ON_RESULT_REPORT), + data, reply, option)); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceReverseStubTest-an exception occurred by RestoreOnResultReport."; + } + GTEST_LOG_(INFO) + << "ServiceReverseStubTest-end SUB_backup_ServiceReverseStub_IncrementalRestoreOnResultReport_0100"; +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/tests/unittests/backup_api/backup_impl/service_reverse_test.cpp b/tests/unittests/backup_api/backup_impl/service_reverse_test.cpp index e070407997b717922fe221327815835aaa633e06..372cadcde398e27601c767c0cf6400cbb4f2b308 100644 --- a/tests/unittests/backup_api/backup_impl/service_reverse_test.cpp +++ b/tests/unittests/backup_api/backup_impl/service_reverse_test.cpp @@ -96,6 +96,7 @@ void ServiceReverseTest::Init(IServiceReverse::Scenario scenario, int nType) .onBundleStarted = nullptr, .onBundleFinished = nullptr, .onAllBundlesFinished = nullptr, + .onResultReport = nullptr, .onBackupServiceDied = nullptr}); } else { service_ = new ServiceReverse(BSessionBackup::Callbacks { @@ -103,6 +104,7 @@ void ServiceReverseTest::Init(IServiceReverse::Scenario scenario, int nType) .onBundleStarted = bind(OnBundleStartedTest, placeholders::_1, placeholders::_2), .onBundleFinished = bind(OnBundleFinishedTest, placeholders::_1, placeholders::_2), .onAllBundlesFinished = bind(OnAllBundlesFinishedTest, placeholders::_1), + .onResultReport = bind(OnResultReportTest, placeholders::_1), .onBackupServiceDied = bind(OnBackupServiceDiedTest)}); } } else { @@ -111,6 +113,7 @@ void ServiceReverseTest::Init(IServiceReverse::Scenario scenario, int nType) .onBundleStarted = nullptr, .onBundleFinished = nullptr, .onAllBundlesFinished = nullptr, + .onResultReport = nullptr, .onBackupServiceDied = nullptr}); } else { service_ = new ServiceReverse(BSessionRestore::Callbacks { @@ -118,8 +121,8 @@ void ServiceReverseTest::Init(IServiceReverse::Scenario scenario, int nType) .onBundleStarted = bind(OnBundleStartedTest, placeholders::_1, placeholders::_2), .onBundleFinished = bind(OnBundleFinishedTest, placeholders::_1, placeholders::_2), .onAllBundlesFinished = bind(OnAllBundlesFinishedTest, placeholders::_1), - .onBackupServiceDied = bind(OnBackupServiceDiedTest), - .onResultReport = bind(OnResultReportTest, placeholders::_1)}); + .onResultReport = bind(OnResultReportTest, placeholders::_1), + .onBackupServiceDied = bind(OnBackupServiceDiedTest)}); } } } @@ -132,6 +135,7 @@ void ServiceReverseTest::IncrementalInit(IServiceReverse::Scenario scenario, int .onBundleStarted = nullptr, .onBundleFinished = nullptr, .onAllBundlesFinished = nullptr, + .onResultReport = nullptr, .onBackupServiceDied = nullptr}); } else { service_ = new ServiceReverse(BIncrementalBackupSession::Callbacks { @@ -139,6 +143,7 @@ void ServiceReverseTest::IncrementalInit(IServiceReverse::Scenario scenario, int .onBundleStarted = bind(OnBundleStartedTest, placeholders::_1, placeholders::_2), .onBundleFinished = bind(OnBundleFinishedTest, placeholders::_1, placeholders::_2), .onAllBundlesFinished = bind(OnAllBundlesFinishedTest, placeholders::_1), + .onResultReport = bind(OnResultReportTest, placeholders::_1), .onBackupServiceDied = bind(OnBackupServiceDiedTest)}); } } else { @@ -154,6 +159,7 @@ void ServiceReverseTest::IncrementalInit(IServiceReverse::Scenario scenario, int .onBundleStarted = bind(OnBundleStartedTest, placeholders::_1, placeholders::_2), .onBundleFinished = bind(OnBundleFinishedTest, placeholders::_1, placeholders::_2), .onAllBundlesFinished = bind(OnAllBundlesFinishedTest, placeholders::_1), + .onResultReport = bind(OnResultReportTest, placeholders::_1), .onBackupServiceDied = bind(OnBackupServiceDiedTest)}); } } diff --git a/tests/unittests/backup_sa/module_ipc/service_reverse_proxy_test.cpp b/tests/unittests/backup_sa/module_ipc/service_reverse_proxy_test.cpp index ecbe474dcfdbf68d56ef79bac63bf8e02ca28257..da53aace96a988b363e124acf44a4378cec94403 100644 --- a/tests/unittests/backup_sa/module_ipc/service_reverse_proxy_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_reverse_proxy_test.cpp @@ -513,4 +513,29 @@ HWTEST_F(ServiceReverseProxyTest, SUB_ServiceReverse_proxy_RestoreOnResultReport } GTEST_LOG_(INFO) << "ServiceReverseProxyTest-end SUB_ServiceReverse_proxy_RestoreOnResultReport_0100"; } + +/** + * @tc.number: SUB_ServiceReverse_proxy_IncrementalRestoreOnResultReport_0100 + * @tc.name: SUB_ServiceReverse_proxy_IncrementalRestoreOnResultReport_0100 + * @tc.desc: Test function of IncrementalRestoreOnResultReport interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(ServiceReverseProxyTest, SUB_ServiceReverse_proxy_IncrementalRestoreOnResultReport_0100, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceReverseProxyTest-begin SUB_ServiceReverse_proxy_IncrementalRestoreOnResultReport_0100"; + try { + EXPECT_CALL(*mock_, SendRequest(_, _, _, _)) + .Times(1) + .WillOnce(Invoke(mock_.GetRefPtr(), &ServiceReverseMock::InvokeSendRequest)); + proxy_->IncrementalRestoreOnResultReport(RESULT_REPORT); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceReverseProxyTest-an exception occurred by RestoreOnResultReport."; + } + GTEST_LOG_(INFO) << "ServiceReverseProxyTest-end SUB_ServiceReverse_proxy_IncrementalRestoreOnResultReport_0100"; +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/tests/unittests/backup_sa/module_ipc/service_stub_test.cpp b/tests/unittests/backup_sa/module_ipc/service_stub_test.cpp index 78300691abe89c7f7cd7a35b730466641b5623b1..e726bbeb11cf22a687f21e4b51982250ed1b8a83 100644 --- a/tests/unittests/backup_sa/module_ipc/service_stub_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_stub_test.cpp @@ -23,6 +23,7 @@ #include #include "b_error/b_error.h" +#include "b_resources/b_constants.h" #include "i_service.h" #include "module_ipc/service_stub.h" #include "service_reverse_mock.h" @@ -47,7 +48,7 @@ public: MOCK_METHOD1(PublishFile, ErrCode(const BFileInfo &fileInfo)); MOCK_METHOD2(AppFileReady, ErrCode(const string &fileName, UniqueFd fd)); MOCK_METHOD1(AppDone, ErrCode(ErrCode errCode)); - MOCK_METHOD1(ServiceResultReport, ErrCode(const string &restoreRetInfo)); + MOCK_METHOD2(ServiceResultReport, ErrCode(const string restoreRetInfo, BackupRestoreScenario scenario)); MOCK_METHOD2(GetFileHandle, ErrCode(const string &bundleName, const string &fileName)); MOCK_METHOD5( AppendBundlesRestoreSession, diff --git a/tests/unittests/backup_utils/b_jsonutil/b_jsonutil_test.cpp b/tests/unittests/backup_utils/b_jsonutil/b_jsonutil_test.cpp index 77ef3170bbc0e3a9495acb3ae54239490db41662..6881b31de5b758093ea0047c3e3be5827493331b 100644 --- a/tests/unittests/backup_utils/b_jsonutil/b_jsonutil_test.cpp +++ b/tests/unittests/backup_utils/b_jsonutil/b_jsonutil_test.cpp @@ -63,17 +63,17 @@ HWTEST_F(BJsonUtilTest, b_jsonutil_ParseBundleNameIndexStr_0100, testing::ext::T } /** - * @tc.number: b_jsonutil_ConvertBundleDetailInfos_0100 - * @tc.name: b_jsonutil_ConvertBundleDetailInfos_0100 - * @tc.desc: Test function of ConvertBundleDetailInfos interface for SUCCESS. + * @tc.number: b_jsonutil_BuildBundleInfos_0100 + * @tc.name: b_jsonutil_BuildBundleInfos_0100 + * @tc.desc: Test function of BuildBundleInfos interface for SUCCESS. * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 0 * @tc.require: I6F3GV */ -HWTEST_F(BJsonUtilTest, b_jsonutil_ConvertBundleDetailInfos_0100, testing::ext::TestSize.Level0) +HWTEST_F(BJsonUtilTest, b_jsonutil_BuildBundleInfos_0100, testing::ext::TestSize.Level0) { - GTEST_LOG_(INFO) << "BJsonUtilTest-begin ConvertBundleDetailInfoss_0100"; + GTEST_LOG_(INFO) << "BJsonUtilTest-begin BuildBundleInfos_0100"; try { std::vector bundleNames; std::string bundleName = "com.hos.app01:1"; @@ -92,14 +92,14 @@ HWTEST_F(BJsonUtilTest, b_jsonutil_ConvertBundleDetailInfos_0100, testing::ext:: detailInfos.push_back(detail01); int32_t userId = 100; std::vector realBundleNames; - std::vector detailInfos = - BJsonUtil::ConvertBundleDetailInfos(bundleNames, detailInfos, pattern, - realBundleNames, userId); - EXPECT_EQ("com.hos.app01", detailInfos.at(0)); + std::map bundleNameDetailMap = + BJsonUtil::BuildBundleInfos(bundleNames, detailInfos, realBundleNames, userId); + std::string key = "com.hos.app01"; + EXPECT_EQ("com.hos.app01", bundleNameDetailMap[key].bundleName); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "BJsonUtilTest-an exception occurred."; } - GTEST_LOG_(INFO) << "BJsonUtilTest-end b_dir_ConvertBundleDetailInfos_0100"; + GTEST_LOG_(INFO) << "BJsonUtilTest-end b_dir_BuildBundleInfos_0100"; } } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/tools/backup_tool/src/tools_op_backup.cpp b/tools/backup_tool/src/tools_op_backup.cpp index 30b625e2b7e6b561f370d6099b111da2c11a2436..50af13e25a518c484bda67ef795611fda9fbe7ce 100644 --- a/tools/backup_tool/src/tools_op_backup.cpp +++ b/tools/backup_tool/src/tools_op_backup.cpp @@ -158,6 +158,12 @@ static void OnBundleStarted(shared_ptr ctx, ErrCode err, const BundleNa } } +static void OnResultReport(shared_ptr ctx, const std::string result) +{ + printf("OnResultReport result is = %s\n", result.c_str()); + ctx->TryNotify(true); +} + static void OnBundleFinished(shared_ptr ctx, ErrCode err, const BundleName name) { printf("BundleFinished errCode = %d, BundleName = %s\n", err, name.c_str()); @@ -243,6 +249,7 @@ static int32_t InitPathCapFile(const string &pathCapFile, vector bundleN .onBundleStarted = bind(OnBundleStarted, ctx, placeholders::_1, placeholders::_2), .onBundleFinished = bind(OnBundleFinished, ctx, placeholders::_1, placeholders::_2), .onAllBundlesFinished = bind(OnAllBundlesFinished, ctx, placeholders::_1), + .onResultReport = bind(OnResultReport, ctx, placeholders::_1), .onBackupServiceDied = bind(OnBackupServiceDied, ctx)}); if (ctx->session_ == nullptr) { printf("Failed to init backup\n"); diff --git a/tools/backup_tool/src/tools_op_incremental_backup.cpp b/tools/backup_tool/src/tools_op_incremental_backup.cpp index 8971eced5faf1f9240d89afc87aef70d6553a12a..86ee25a0af61fd5e7f9295230caf3dbe00eef3e2 100644 --- a/tools/backup_tool/src/tools_op_incremental_backup.cpp +++ b/tools/backup_tool/src/tools_op_incremental_backup.cpp @@ -209,6 +209,12 @@ static void OnAllBundlesFinished(shared_ptr ctx, ErrCode err) ctx->TryNotify(); } +static void OnResultReport(shared_ptr ctx, const std::string resultInfo) +{ + printf("OnResultReport, resultInfo = %s\n", resultInfo.c_str()); + ctx->TryNotify(true); +} + static void OnBackupServiceDied(shared_ptr ctx) { printf("backupServiceDied\n"); @@ -298,6 +304,7 @@ static int32_t Init(const string &pathCapFile, const vector& bundleNames .onBundleStarted = bind(OnBundleStarted, ctx, placeholders::_1, placeholders::_2), .onBundleFinished = bind(OnBundleFinished, ctx, placeholders::_1, placeholders::_2), .onAllBundlesFinished = bind(OnAllBundlesFinished, ctx, placeholders::_1), + .onResultReport = bind(OnResultReport, ctx, placeholders::_1), .onBackupServiceDied = bind(OnBackupServiceDied, ctx)}); if (ctx->session_ == nullptr) { printf("Failed to init backup\n"); diff --git a/tools/backup_tool/src/tools_op_incremental_restore.cpp b/tools/backup_tool/src/tools_op_incremental_restore.cpp index 703a922ed9533df8164ebf3fde12275128e5ed44..fa143196cb7e2de011a9bf755447ecb211556836 100644 --- a/tools/backup_tool/src/tools_op_incremental_restore.cpp +++ b/tools/backup_tool/src/tools_op_incremental_restore.cpp @@ -199,6 +199,12 @@ static void OnBackupServiceDied(shared_ptr ctx) ctx->TryNotify(true); } +static void OnResultReport(shared_ptr ctx, const std::string resultInfo) +{ + printf("OnResultReport, detailInfo = %s\n", resultInfo.c_str()); + ctx->TryNotify(true); +} + static void RestoreApp(shared_ptr restore) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "RestoreApp"); @@ -251,6 +257,7 @@ static int32_t InitRestoreSession(shared_ptr ctx, .onBundleStarted = bind(OnBundleStarted, ctx, placeholders::_1, placeholders::_2), .onBundleFinished = bind(OnBundleFinished, ctx, placeholders::_1, placeholders::_2), .onAllBundlesFinished = bind(OnAllBundlesFinished, ctx, placeholders::_1), + .onResultReport = bind(OnResultReport, ctx, placeholders::_1), .onBackupServiceDied = bind(OnBackupServiceDied, ctx)}); if (ctx->session_ == nullptr) { printf("Failed to init restore\n"); diff --git a/tools/backup_tool/src/tools_op_incremental_restore_async.cpp b/tools/backup_tool/src/tools_op_incremental_restore_async.cpp index 8b52144b8c86c6ee7f85fd7da860050c07238da0..29bfab07376a3304802c90c4a80b38f2b66ced0d 100644 --- a/tools/backup_tool/src/tools_op_incremental_restore_async.cpp +++ b/tools/backup_tool/src/tools_op_incremental_restore_async.cpp @@ -210,6 +210,12 @@ static void AdapteCloneOptimize(const string &path) close(cachedEntity.GetFd().Release()); } +static void OnResultReport(shared_ptr ctx, const std::string resultInfo) +{ + printf("OnResultReport, detailInfo = %s\n", resultInfo.c_str()); + ctx->TryNotify(true); +} + static void RestoreApp(shared_ptr restore, vector &bundleNames) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "RestoreApp"); @@ -354,6 +360,7 @@ static int32_t InitArg(const string &pathCapFile, .onBundleStarted = bind(OnBundleStarted, ctx, placeholders::_1, placeholders::_2), .onBundleFinished = bind(OnBundleFinished, ctx, placeholders::_1, placeholders::_2), .onAllBundlesFinished = bind(OnAllBundlesFinished, ctx, placeholders::_1), + .onResultReport = bind(OnResultReport, ctx, placeholders::_1), .onBackupServiceDied = bind(OnBackupServiceDied, ctx)}); if (ctx->session_ == nullptr) { printf("Failed to init restore\n"); diff --git a/tools/backup_tool/src/tools_op_restore_async.cpp b/tools/backup_tool/src/tools_op_restore_async.cpp index 0be93a066383b655c31726264baec291e3063fcc..1ced7f9a3bf2814410c97c5abaf22b204ddb44a2 100644 --- a/tools/backup_tool/src/tools_op_restore_async.cpp +++ b/tools/backup_tool/src/tools_op_restore_async.cpp @@ -144,6 +144,12 @@ static void OnAllBundlesFinished(shared_ptr ctx, ErrCode err) ctx->TryNotify(true); } +static void OnResultReport(shared_ptr ctx, const std::string resultInfo) +{ + printf("OnResultReport, detailInfo = %s\n", resultInfo.c_str()); + ctx->TryNotify(true); +} + static void OnBackupServiceDied(shared_ptr ctx) { printf("backupServiceDied\n"); @@ -338,6 +344,7 @@ static int32_t InitArg(const string &pathCapFile, .onBundleStarted = bind(OnBundleStarted, ctx, placeholders::_1, placeholders::_2), .onBundleFinished = bind(OnBundleFinished, ctx, placeholders::_1, placeholders::_2), .onAllBundlesFinished = bind(OnAllBundlesFinished, ctx, placeholders::_1), + .onResultReport = bind(OnResultReport, ctx, placeholders::_1), .onBackupServiceDied = bind(OnBackupServiceDied, ctx)}); if (ctx->session_ == nullptr) { printf("Failed to init restore\n"); diff --git a/utils/include/b_jsonutil/b_jsonutil.h b/utils/include/b_jsonutil/b_jsonutil.h index 66ff58e9e75df1d8780ff74c2e15e00fd470ce5d..083aad791668d956de444be9e2b56af411ec323e 100644 --- a/utils/include/b_jsonutil/b_jsonutil.h +++ b/utils/include/b_jsonutil/b_jsonutil.h @@ -52,9 +52,8 @@ public: * @return 结构体集合 * */ - static std::vector ConvertBundleDetailInfos(const std::vector &bundleNames, - const std::vector &details, const std::string &patternInfo, - std::vector &realBundleNames, int32_t userId); + static std::map BuildBundleInfos(const std::vector &bundleNames, + const std::vector &details, std::vector &realBundleNames, int32_t userId); /** * @brief 解析单个bundle对应的json串 @@ -63,17 +62,7 @@ public: * @param bundleDetail 结构体对象 * */ - static void ParseBundleDetailInfo(const std::string &bundleDetailInfo, BundleDetailInfo &bundleDetail); - - /** - * @brief 根据bundleName获取对应的detailInfo - * - * @param bundleNameDetailMap bundleName和detail的对应关系 - * @param bundleDetailInfo 结构体对象 - * - */ - static void RecordBundleDetailRelation(std::map &bundleNameDetailMap, - std::vector &bundleDetailInfo); + static void ParseBundleInfoJson(const std::string &bundleInfo, BundleDetailInfo &bundleDetail); }; } // namespace OHOS::FileManagement::Backup diff --git a/utils/include/b_resources/b_constants.h b/utils/include/b_resources/b_constants.h index 131bf801644dcb1bc3e22df0ff0f927f13070ee2..448c9cea446d753c6e81fac2a68d0f523fc3b0d0 100644 --- a/utils/include/b_resources/b_constants.h +++ b/utils/include/b_resources/b_constants.h @@ -68,6 +68,8 @@ constexpr int MAX_PARCELABLE_VECTOR_NUM = 10000; constexpr int DEFAULT_VFS_CACHE_PRESSURE = 100; // 默认内存回收参数 constexpr int BACKUP_VFS_CACHE_PRESSURE = 10000; // 备份过程修改参数 +constexpr int32_t INVALID_FD_NUM = -1; + // backup.para内配置项的名称,该配置项值为true时可在不更新hap包的情况下,可以读取包管理元数据配置文件的内容 static inline std::string BACKUP_DEBUG_OVERRIDE_EXTENSION_CONFIG_KEY = "backup.debug.overrideExtensionConfig"; diff --git a/utils/src/b_jsonutil/b_jsonutil.cpp b/utils/src/b_jsonutil/b_jsonutil.cpp index bb631ff5e0585a1805fc24f6c8aa0c3326e7b870..b92d2a9c0516e94618e0db19080165d875834607 100644 --- a/utils/src/b_jsonutil/b_jsonutil.cpp +++ b/utils/src/b_jsonutil/b_jsonutil.cpp @@ -25,6 +25,7 @@ namespace OHOS::FileManagement::Backup { using namespace std; namespace { const static int BUNDLE_INDEX_DEFAULT_VAL = 0; + const static std::string BUNDLE_INDEX_SPLICE = ":"; const static string COMMON_EVENT_TYPE = "broadcast"; } @@ -48,46 +49,52 @@ BJsonUtil::BundleDetailInfo BJsonUtil::ParseBundleNameIndexStr(const std::string return bundleDetailInfo; } -std::vector BJsonUtil::ConvertBundleDetailInfos( - const std::vector &bundleNameIndexStrs, const std::vector &details, - const std::string &patternInfo, std::vector &realBundleNames, int32_t userId) +std::map BJsonUtil::BuildBundleInfos( + const std::vector &bundleNames, const std::vector &bundleInfos, + std::vector &bundleNamesOnly, int32_t userId) { - std::vector bundleDetailInfoList; - for (size_t pos = 0; pos < bundleNameIndexStrs.size(); pos++) { - std::string bundleNameIndexStr = bundleNameIndexStrs[pos]; - BundleDetailInfo bundleDetailInfo; + std::vector bundleDetailInfos; + std::map bundleNameDetailMap; + for (size_t i = 0; i < bundleNames.size(); i++) { + std::string bundleName = bundleNames[i]; + BJsonUtil::BundleDetailInfo bundleDetailInfo; bundleDetailInfo.userId = userId; - size_t hasPos = bundleNameIndexStr.find(patternInfo); - if (hasPos == std::string::npos) { - bundleDetailInfo.bundleName = bundleNameIndexStr; + size_t pos = bundleName.find(BUNDLE_INDEX_SPLICE); + if (pos == 0 || pos == (bundleName.size() - 1)) { + HILOGE("Current bundle name is wrong"); + continue; + } + if (pos == std::string::npos) { + bundleDetailInfo.bundleName = bundleName; bundleDetailInfo.bundleIndex = BUNDLE_INDEX_DEFAULT_VAL; - realBundleNames.emplace_back(bundleNameIndexStr); + bundleNamesOnly.emplace_back(bundleName); } else { - std::string bundleName = bundleNameIndexStr.substr(0, hasPos); - std::string indexStr = bundleNameIndexStr.substr(hasPos + 1); - int index = std::stoi(indexStr); - bundleDetailInfo.bundleName = bundleName; + std::string bundleNameSplit = bundleName.substr(0, pos); + std::string indexSplit = bundleName.substr(pos, bundleName.size() - 1); + int index = std::stoi(indexSplit); + bundleDetailInfo.bundleName = bundleNameSplit; bundleDetailInfo.bundleIndex = index; - realBundleNames.emplace_back(bundleName); + bundleNamesOnly.emplace_back(bundleNameSplit); } - if (pos < details.size()) { - std::string bundleDetailStr = details[pos]; - ParseBundleDetailInfo(bundleDetailStr, bundleDetailInfo); + if (i < bundleInfos.size()) { + std::string bundleInfo = bundleInfos[i]; + ParseBundleInfoJson(bundleInfo, bundleDetailInfo); } - bundleDetailInfoList.emplace_back(bundleDetailInfo); + bundleDetailInfos.emplace_back(bundleDetailInfo); + bundleNameDetailMap[bundleDetailInfo.bundleName] = bundleDetailInfo; } - return bundleDetailInfoList; + return bundleNameDetailMap; } -void BJsonUtil::ParseBundleDetailInfo(const std::string &bundleDetailInfo, BundleDetailInfo &bundleDetail) +void BJsonUtil::ParseBundleInfoJson(const std::string &bundleInfo, BundleDetailInfo &bundleDetail) { - cJSON *root = cJSON_Parse(bundleDetailInfo.c_str()); + cJSON *root = cJSON_Parse(bundleInfo.c_str()); if (root == nullptr) { HILOGE("Parse json error,root is null"); return; } cJSON *infos = cJSON_GetObjectItem(root, "infos"); - if (!cJSON_IsArray(infos)) { + if (infos == nullptr || !cJSON_IsArray(infos) || cJSON_GetArraySize(infos) == 0) { HILOGE("Parse json error, infos is not array"); cJSON_Delete(root); return; @@ -115,13 +122,4 @@ void BJsonUtil::ParseBundleDetailInfo(const std::string &bundleDetailInfo, Bundl } cJSON_Delete(root); } - -void BJsonUtil::RecordBundleDetailRelation(std::map &bundleNameDetailMap, - std::vector &bundleDetailInfos) -{ - for (auto &bundleDetail : bundleDetailInfos) { - std::string bundleName = bundleDetail.bundleName; - bundleNameDetailMap[bundleName] = bundleDetail; - } -} } \ No newline at end of file