From 2c5c1ff1eabdf97962e5ae67b4c41f0cfe6b8830 Mon Sep 17 00:00:00 2001 From: dingdong Date: Wed, 29 May 2024 09:34:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=A9=E5=B1=95backup=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: dingdong --- .../native/backup_ext/include/ext_backup.h | 12 ++ .../native/backup_ext/include/ext_backup_js.h | 21 ++++ .../native/backup_ext/src/ext_backup.cpp | 13 +++ .../native/backup_ext/src/ext_backup_js.cpp | 108 +++++++++++++++++- .../src/b_incremental_backup_session.cpp | 12 ++ .../src/service_incremental_proxy.cpp | 34 ++++++ .../backup_ext/backup_extension_ability.js | 4 + .../impl/b_incremental_backup_session.h | 9 ++ .../native/backup_kit_inner/impl/i_service.h | 3 +- .../impl/i_service_ipc_interface_code.h | 1 + .../backup_kit_inner/impl/service_proxy.h | 3 +- .../js/backup/session_backup_n_exporter.cpp | 2 +- .../session_incremental_backup_n_exporter.cpp | 38 +++++- .../backup_sa/include/module_ipc/service.h | 2 + .../include/module_ipc/service_stub.h | 1 + services/backup_sa/src/module_ipc/service.cpp | 2 + .../src/module_ipc/service_incremental.cpp | 51 +++++++++ .../backup_sa/src/module_ipc/service_stub.cpp | 20 ++++ .../backup_kit_inner/service_proxy_mock.cpp | 6 + tests/mock/module_ipc/service_mock.cpp | 6 + .../backup_impl/include/i_service_mock.h | 6 + .../module_ipc/service_stub_test.cpp | 2 + .../backup_sa/session/service_proxy_mock.cpp | 6 + utils/include/b_resources/b_constants.h | 1 + 24 files changed, 356 insertions(+), 7 deletions(-) diff --git a/frameworks/native/backup_ext/include/ext_backup.h b/frameworks/native/backup_ext/include/ext_backup.h index 2efa6c67f..264c6cdc6 100644 --- a/frameworks/native/backup_ext/include/ext_backup.h +++ b/frameworks/native/backup_ext/include/ext_backup.h @@ -122,6 +122,12 @@ public: */ virtual std::string GetUsrConfig() const; + /** + * @brief do backup. Subclasses can inherit to implement their own custom functionality. + */ + virtual ErrCode OnBackup(std::function callback, std::function callbackEx, + std::function callbackExAppDone); + /** * @brief do backup. Subclasses can inherit to implement their own custom functionality. */ @@ -167,6 +173,11 @@ public: */ bool RestoreDataReady(); + /** + * @brief Called Extention to notify + */ + virtual ErrCode CallExtBackup(std::string result); + /** * @brief Called Extention to notify */ @@ -180,6 +191,7 @@ public: protected: std::string appVersionStr_; + std::string backupRetInfo_; std::string restoreRetInfo_; int appVersionCode_; int restoreType_; diff --git a/frameworks/native/backup_ext/include/ext_backup_js.h b/frameworks/native/backup_ext/include/ext_backup_js.h index ec9097f8f..3355c00a4 100644 --- a/frameworks/native/backup_ext/include/ext_backup_js.h +++ b/frameworks/native/backup_ext/include/ext_backup_js.h @@ -99,6 +99,15 @@ public: */ static ExtBackupJs *Create(const std::unique_ptr &runtime); + /** + * @brief Call the app's OnBackup. + * + * @param callbackEx The callbackEx. + * @param callback The callback. + */ + ErrCode OnBackup(std::function callback, std::function callbackEx, + std::function callbackExAppDone) override; + /** * @brief Call the app's OnBackup. * @@ -128,6 +137,13 @@ public: */ ErrCode GetBackupInfo(std::function callback) override; + /** + * @brief Called Notification containing extended information + * + * @param result The result. + */ + ErrCode CallExtBackup(std::string result) override; + /** * @brief Called Notification containing extended information * @@ -151,9 +167,13 @@ private: std::tuple CallObjectMethod(std::string_view name, const std::vector &argv = {}); std::function &argv)> ParseRestoreExInfo(); std::function &argv)> ParseRestoreInfo(); + std::function &argv)> ParseBackupExInfo(); + std::function &argv)> ParseBackupInfo(); ErrCode CallJSRestoreEx(); ErrCode CallJSRestore(); + ErrCode CallJSBackupEx(); + ErrCode CallJsBackup(); void ExportJsContext(void); @@ -165,6 +185,7 @@ private: std::condition_variable callJsCon_; std::mutex callJsMutex_; std::atomic needCallOnRestore_; + std::atomic needCallOnBackup_; }; } // namespace OHOS::FileManagement::Backup diff --git a/frameworks/native/backup_ext/src/ext_backup.cpp b/frameworks/native/backup_ext/src/ext_backup.cpp index b9371bc91..d2b1d8108 100644 --- a/frameworks/native/backup_ext/src/ext_backup.cpp +++ b/frameworks/native/backup_ext/src/ext_backup.cpp @@ -244,6 +244,13 @@ bool ExtBackup::RestoreDataReady() return restoreType_ == RestoreTypeEnum::RESTORE_DATA_READDY; } +ErrCode ExtBackup::OnBackup(function callback, std::function callbackEx, + std::function callbackExAppDone) +{ + HILOGI("BackupExtensionAbility(base) OnBackup with Ex."); + return ERR_OK; +} + ErrCode ExtBackup::OnBackup(function callback) { HILOGI("BackupExtensionAbility(base) OnBackup."); @@ -269,6 +276,12 @@ ErrCode ExtBackup::GetBackupInfo(function callback) return ERR_OK; } +ErrCode ExtBackup::CallExtBackup(std::string result) +{ + HILOGI("BackupExtensionAbility(base) CallExtBackup."); + return ERR_OK; +} + ErrCode ExtBackup::CallExtRestore(std::string result) { HILOGI("BackupExtensionAbility(base) CallExtRestore."); diff --git a/frameworks/native/backup_ext/src/ext_backup_js.cpp b/frameworks/native/backup_ext/src/ext_backup_js.cpp index 6faf8aa99..f9126d35a 100644 --- a/frameworks/native/backup_ext/src/ext_backup_js.cpp +++ b/frameworks/native/backup_ext/src/ext_backup_js.cpp @@ -345,6 +345,15 @@ 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."); + 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 { @@ -355,12 +364,75 @@ ErrCode ExtBackupJs::OnBackup(function callback) HILOGI("CheckPromise(JS) OnBackup ok."); return CallPromise(*jsRuntime, result, callbackInfo.get()); }; + auto errCode = CallJsMethod("onBackup", jsRuntime_, jsObj_.get(), argParser, retParser); + if (errCode != ERR_OK) { + HILOGE("CallJsMethod error, code:%{public}d.", errCode); + return errCode; + } + return ERR_OK; +} + +ErrCode ExtBackupJs::OnBackup(function callback, std::function callbackEx, + std::function callbackExAppDone) +{ + HILOGI("BackupExtensionAbility(JS) OnBackup."); + BExcepUltils::BAssert(jsObj_, BError::Codes::EXT_BROKEN_FRAMEWORK, + "The app does not provide the onBackup interface."); + needCallOnBackup_.store(false); + callbackInfo_ = std::make_shared(callback); + callbackInfoEx_ = std::make_shared(callbackEx, callbackExAppDone); + return CallJSBackupEx(); +} + +ErrCode ExtBackupJs::CallJSBackupEx() +{ + HILOGI("Start call app js method onBackupEx"); + auto retParser = [jsRuntime {&jsRuntime_}, callbackInfoEx {callbackInfoEx_}](napi_env envir, napi_value result) -> + bool { + if (!CheckPromise(envir, result)) { + HILOGI("CheckPromise onBackupEx false"); + std::string str; + DealNapiStrValue(envir, result, str); + callbackInfoEx->callbackParam(str); + return true; + } + HILOGI("CheckPromise onBackupEx ok"); + return CallPromiseEx(*jsRuntime, result, callbackInfoEx.get()); + }; + auto errCode = CallJsMethod("onBackupEx", jsRuntime_, jsObj_.get(), ParseBackupExInfo(), retParser); + if (errCode != ERR_OK) { + HILOGE("Call onBackupEx error"); + return errCode; + } + if (!needCallOnBackup_.load()) { + if (callbackInfoEx_) { + HILOGI("Will call app done"); + callbackInfoEx_->callbackAppDone(); + } + HILOGI("Call Js method onBackupEx done"); + return ERR_OK; + } + return CallJsBackup(); +} - auto errCode = CallJsMethod("onBackup", jsRuntime_, jsObj_.get(), {}, retParser); +ErrCode ExtBackupJs::CallJsBackup() +{ + HILOGI("Start call app js method onBackup"); + auto retParser = [jsRuntime {&jsRuntime_}, callbackInfo {callbackInfo_}](napi_env env, napi_value result) -> bool { + if (!CheckPromise(env, result)) { + HILOGI("onBackup, CheckPromise false"); + callbackInfo->callback(); + return true; + } + HILOGI("CheckPromise Js Method onBackup ok."); + return CallPromise(*jsRuntime, result, callbackInfo.get()); + }; + auto errCode = CallJsMethod("onBackup", jsRuntime_, jsObj_.get(), ParseBackupInfo(), retParser); if (errCode != ERR_OK) { HILOGE("CallJsMethod error, code:%{public}d.", errCode); + return errCode; } - return errCode; + return ERR_OK; } ErrCode ExtBackupJs::OnRestore(function callback, std::function callbackEx, @@ -584,6 +656,26 @@ int ExtBackupJs::CallJsMethod(const std::string &funcName, return ERR_OK; } +std::function &argv)> ExtBackupJs::ParseBackupExInfo() +{ + auto onBackupExFun = [backupRetInfo(backupRetInfo_)](napi_env env, vector &argv) -> bool { + napi_value backupRetValue = nullptr; + napi_create_object(env, &backupRetValue); + napi_create_string_utf8(env, backupRetInfo.c_str(), backupRetInfo.size(), &backupRetValue); + argv.push_back(backupRetValue); + return true; + }; + return onBackupExFun; +} + +std::function &argv)> ExtBackupJs::ParseBackupInfo() +{ + auto onBackupFun = [](napi_env env, vector &argv) -> bool { + return true; + }; + return onBackupFun; +} + std::function &argv)> ExtBackupJs::ParseRestoreExInfo() { auto onRestoreExFun = [appVersionCode(appVersionCode_), appVersionStr(appVersionStr_), @@ -616,6 +708,18 @@ std::function &argv)> ExtBackupJs::Pa return onRestoreFun; } +ErrCode ExtBackupJs::CallExtBackup(std::string result) +{ + HILOGI("Start CallExtBackup, result is: %{public}s", result.c_str()); + if (result.size() == 0) { + needCallOnBackup_.store(true); + } else { + needCallOnBackup_.store(false); + } + HILOGI("End CallExtBackup"); + return ERR_OK; +} + ErrCode ExtBackupJs::CallExtRestore(std::string result) { HILOGI("Start CallExtRestore, result is: %{public}s", result.c_str()); diff --git a/frameworks/native/backup_kit_inner/src/b_incremental_backup_session.cpp b/frameworks/native/backup_kit_inner/src/b_incremental_backup_session.cpp index df26ec5cb..97dc526e1 100644 --- a/frameworks/native/backup_kit_inner/src/b_incremental_backup_session.cpp +++ b/frameworks/native/backup_kit_inner/src/b_incremental_backup_session.cpp @@ -95,6 +95,17 @@ ErrCode BIncrementalBackupSession::AppendBundles(vector bundle return proxy->AppendBundlesIncrementalBackupSession(bundlesToBackup); } +ErrCode BIncrementalBackupSession::AppendBundles(vector bundlesToBackup, + vector infos) +{ + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); + } + + return proxy->AppendBundlesIncrementalDetailsBackupSession(bundlesToBackup, infos); +} + ErrCode BIncrementalBackupSession::Release() { auto proxy = ServiceProxy::GetInstance(); @@ -104,4 +115,5 @@ ErrCode BIncrementalBackupSession::Release() return proxy->Release(); } + } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/frameworks/native/backup_kit_inner/src/service_incremental_proxy.cpp b/frameworks/native/backup_kit_inner/src/service_incremental_proxy.cpp index 9db74c402..3c2dfd903 100644 --- a/frameworks/native/backup_kit_inner/src/service_incremental_proxy.cpp +++ b/frameworks/native/backup_kit_inner/src/service_incremental_proxy.cpp @@ -160,6 +160,40 @@ ErrCode ServiceProxy::AppendBundlesIncrementalBackupSession(const vector &bundlesToBackup, + const std::vector &bundleInfos) +{ + HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); + BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "remote is nullptr"); + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor())) { + HILOGE("Failed to write descriptor"); + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode(); + } + + if (!WriteParcelableVector(bundlesToBackup, data)) { + HILOGE("Failed to send the bundleNames"); + return UniqueFd(-EPERM); + } + + if (!WriteParcelableVector(bundleInfos, data)) { + HILOGE("Failed to send the bundleInfos"); + return UniqueFd(-EPERM); + } + + MessageParcel reply; + MessageOption option; + + int32_t ret = Remote()->SendRequest( + static_cast(IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_INCREMENTAL_BACKUP_SESSION_DETAILS), + data, reply, option); + if (ret != NO_ERROR) { + HILOGE("Received error %{public}d when doing IPC", ret); + return BError(BError::Codes::SDK_INVAL_ARG, "Received error when doing IPC").GetCode(); + } + return reply.ReadInt32(); +} + ErrCode ServiceProxy::PublishIncrementalFile(const BFileInfo &fileInfo) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); 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 b1801058f..91378a7d3 100644 --- a/interfaces/api/js/napi/backup_ext/backup_extension_ability.js +++ b/interfaces/api/js/napi/backup_ext/backup_extension_ability.js @@ -18,6 +18,10 @@ class BackupExtensionAbility { console.log(); } + onBackupEx(backupInfo) { + console.log(); + } + onRestore(versionBackupedBundle) { console.log(versionBackupedBundle); } 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 7be54adb4..030bb402a 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 @@ -56,6 +56,15 @@ public: */ ErrCode AppendBundles(std::vector bundlesToBackup); + /** + * @brief 用于追加应用,现阶段仅支持在Start之前调用 + * + * @param bundlesToBackup 待备份的应用清单 + * @param infos 备份所需信息 + * @return ErrCode 规范错误码 + */ + ErrCode AppendBundles(std::vector bundlesToBackup, std::vector infos); + /** * @brief 用于结束服务 * 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 a3e3131a3..bb759e8ce 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 @@ -72,7 +72,8 @@ public: virtual ErrCode GetAppLocalListAndDoIncrementalBackup() = 0; virtual ErrCode InitIncrementalBackupSession(sptr remotse) = 0; virtual ErrCode AppendBundlesIncrementalBackupSession(const std::vector &bundlesToBackup) = 0; - + virtual ErrCode AppendBundlesIncrementalDetailsBackupSession(const std::vector &bundleNames, + const std::vector &bundleInfos) = 0; virtual ErrCode PublishIncrementalFile(const BFileInfo &fileInfo) = 0; virtual ErrCode PublishSAIncrementalFile(const BFileInfo &fileInfo, UniqueFd fd) = 0; virtual ErrCode AppIncrementalFileReady(const std::string &fileName, UniqueFd fd, UniqueFd manifestFd, diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/i_service_ipc_interface_code.h b/interfaces/inner_api/native/backup_kit_inner/impl/i_service_ipc_interface_code.h index 1520b414c..c18df5154 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/i_service_ipc_interface_code.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/i_service_ipc_interface_code.h @@ -37,6 +37,7 @@ enum class IServiceInterfaceCode { SERVICE_CMD_GET_LOCAL_CAPABILITIES_INCREMENTAL, SERVICE_CMD_INIT_INCREMENTAL_BACKUP_SESSION, SERVICE_CMD_APPEND_BUNDLES_INCREMENTAL_BACKUP_SESSION, + SERVICE_CMD_APPEND_BUNDLES_INCREMENTAL_BACKUP_SESSION_DETAILS, SERVICE_CMD_PUBLISH_INCREMENTAL_FILE, SERVICE_CMD_PUBLISH_SA_INCREMENTAL_FILE, SERVICE_CMD_APP_INCREMENTAL_FILE_READY, 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 4b5010aab..088b8d68a 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 @@ -55,7 +55,8 @@ public: ErrCode GetAppLocalListAndDoIncrementalBackup() override; ErrCode InitIncrementalBackupSession(sptr remote) override; ErrCode AppendBundlesIncrementalBackupSession(const std::vector &bundlesToBackup) override; - + ErrCode AppendBundlesIncrementalDetailsBackupSession(const std::vector &bundleNames, + const std::vector &bundleInfos) override; ErrCode PublishIncrementalFile(const BFileInfo &fileInfo) override; ErrCode PublishSAIncrementalFile(const BFileInfo &fileInfo, UniqueFd fd) override; ErrCode AppIncrementalFileReady(const std::string &fileName, UniqueFd fd, UniqueFd manifestFd, diff --git a/interfaces/kits/js/backup/session_backup_n_exporter.cpp b/interfaces/kits/js/backup/session_backup_n_exporter.cpp index e81151e53..77cf1a1fe 100644 --- a/interfaces/kits/js/backup/session_backup_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_backup_n_exporter.cpp @@ -275,7 +275,7 @@ napi_value SessionBackupNExporter::Constructor(napi_env env, napi_callback_info } static bool VerifyParamSuccess(NFuncArg &funcArg, std::vector &bundleNames, - std::vector &bundleInfos, napi_env env) + std::vector &bundleInfos, napi_env env) { if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { HILOGE("Number of arguments unmatched."); 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 65597dc0d..6ada96d08 100644 --- a/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp @@ -340,10 +340,36 @@ static std::tuple> ParseDataList(napi_env en return {true, backupData}; } +static bool VerifyParamSuccess(NFuncArg &funcArg, std::vector &bundleInfos, napi_env env) +{ + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + HILOGE("Number of arguments unmatched."); + NError(BError(BError::Codes::SDK_INVAL_ARG, "Number of arguments unmatched.").GetCode()).ThrowErr(env); + return false; + } + NVal jsInfos(env, funcArg[NARG_POS::SECOND]); + if (jsInfos.TypeIs(napi_undefined) || jsInfos.TypeIs(napi_null)) { + HILOGW("Second param is not exist"); + return true; + } + auto [deSuc, jsBundleInfos, deIgnore] = jsInfos.ToStringArray(); + if (deSuc) { + bundleInfos = jsBundleInfos; + return true; + } + HILOGI("First param is callback"); + return true; +} + napi_value SessionIncrementalBackupNExporter::AppendBundles(napi_env env, napi_callback_info cbinfo) { HILOGD("called SessionIncrementalBackup::AppendBundles begin"); + std::vector bundleInfos; NFuncArg funcArg(env, cbinfo); + if (!VerifyParamSuccess(funcArg, bundleInfos, env)) { + HILOGE("VerifyParamSuccess fail"); + return nullptr; + } if (!funcArg.InitArgs(NARG_CNT::ONE)) { HILOGE("Number of arguments unmatched."); NError(BError(BError::Codes::SDK_INVAL_ARG, "Number of arguments unmatched.").GetCode()).ThrowErr(env); @@ -364,10 +390,13 @@ napi_value SessionIncrementalBackupNExporter::AppendBundles(napi_env env, napi_c return nullptr; } - auto cbExec = [session {backupEntity->session.get()}, bundles { move(bundles) }]() -> NError { + auto cbExec = [session {backupEntity->session.get()}, bundles {move(bundles)}, infos {bundleInfos}]() -> NError { if (!session) { return NError(BError(BError::Codes::SDK_INVAL_ARG, "backup session is nullptr").GetCode()); } + if (!infos.empty()) { + return NError(session->AppendBundles(bundles, infos)); + } return NError(session->AppendBundles(bundles)); }; auto cbCompl = [](napi_env env, NError err) -> NVal { @@ -377,7 +406,12 @@ napi_value SessionIncrementalBackupNExporter::AppendBundles(napi_env env, napi_c HILOGD("Called SessionIncrementalBackup::AppendBundles end."); NVal thisVar(env, funcArg.GetThisVar()); - return NAsyncWorkPromise(env, thisVar).Schedule(className, cbExec, cbCompl).val_; + if (!bundleInfos.empty()) { + return NAsyncWorkPromise(env, thisVar).Schedule(className, cbExec, cbCompl).val_; + } else { + NVal cb(env, funcArg[NARG_POS::First]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(className, cbExec, cbCompl).val_; + } } napi_value SessionIncrementalBackupNExporter::Release(napi_env env, napi_callback_info cbinfo) diff --git a/services/backup_sa/include/module_ipc/service.h b/services/backup_sa/include/module_ipc/service.h index 0ab8e2a34..76f52c036 100644 --- a/services/backup_sa/include/module_ipc/service.h +++ b/services/backup_sa/include/module_ipc/service.h @@ -62,6 +62,8 @@ public: ErrCode GetAppLocalListAndDoIncrementalBackup() override; ErrCode InitIncrementalBackupSession(sptr remote) override; ErrCode AppendBundlesIncrementalBackupSession(const std::vector &bundlesToBackup) override; + ErrCode AppendBundlesIncrementalDetailsBackupSession(const std::vector &bundlesToBackup, + const std::vector &bundleInfos) override; ErrCode PublishIncrementalFile(const BFileInfo &fileInfo) override; ErrCode PublishSAIncrementalFile(const BFileInfo &fileInfo, UniqueFd fd) override; diff --git a/services/backup_sa/include/module_ipc/service_stub.h b/services/backup_sa/include/module_ipc/service_stub.h index 0e41c7958..0815b2311 100644 --- a/services/backup_sa/include/module_ipc/service_stub.h +++ b/services/backup_sa/include/module_ipc/service_stub.h @@ -53,6 +53,7 @@ private: int32_t CmdGetAppLocalListAndDoIncrementalBackup(MessageParcel &data, MessageParcel &reply); int32_t CmdInitIncrementalBackupSession(MessageParcel &data, MessageParcel &reply); int32_t CmdAppendBundlesIncrementalBackupSession(MessageParcel &data, MessageParcel &reply); + int32_t CmdAppendBundlesIncrementalDetailsBackupSession(MessageParcel &data, MessageParcel &reply); int32_t CmdPublishIncrementalFile(MessageParcel &data, MessageParcel &reply); int32_t CmdPublishSAIncrementalFile(MessageParcel &data, MessageParcel &reply); int32_t CmdAppIncrementalFileReady(MessageParcel &data, MessageParcel &reply); diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index 33938dd42..27d972f87 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -759,12 +759,14 @@ ErrCode Service::LaunchBackupExtension(const BundleName &bundleName) string versionName = session_->GetBundleVersionName(bundleName); /* old device app version name */ uint32_t versionCode = session_->GetBundleVersionCode(bundleName); /* old device app version code */ RestoreTypeEnum restoreType = session_->GetBundleRestoreType(bundleName); /* app restore type */ + string backupExtInfo = session_->GetBackupExtInfo(bundleName); want.SetElementName(bundleName, backupExtName); want.SetParam(BConstants::EXTENSION_ACTION_PARA, static_cast(action)); want.SetParam(BConstants::EXTENSION_VERSION_CODE_PARA, static_cast(versionCode)); want.SetParam(BConstants::EXTENSION_RESTORE_TYPE_PARA, static_cast(restoreType)); want.SetParam(BConstants::EXTENSION_VERSION_NAME_PARA, versionName); + want.SetParam(BConstants::EXTENSION_BAXKUP_EXT_INFO_PARA, backupExtInfo); auto backUpConnection = session_->GetExtConnection(bundleName); diff --git a/services/backup_sa/src/module_ipc/service_incremental.cpp b/services/backup_sa/src/module_ipc/service_incremental.cpp index fe9dd271c..bd0f19e7a 100644 --- a/services/backup_sa/src/module_ipc/service_incremental.cpp +++ b/services/backup_sa/src/module_ipc/service_incremental.cpp @@ -52,6 +52,7 @@ namespace OHOS::FileManagement::Backup { using namespace std; const std::string FILE_BACKUP_EVENTS = "FILE_BACKUP_EVENTS"; +const static string UNICAST_TYPE = "unicast"; namespace { constexpr int32_t DEBUG_ID = 100; @@ -232,6 +233,56 @@ ErrCode Service::AppendBundlesIncrementalBackupSession(const std::vector &bundlesToBackup, + const std::vector &bundleInfos) +{ + HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); + try { + session_->IncreaseSessionCnt(); // BundleMgrAdapter::GetBundleInfos可能耗时 + VerifyCaller(IServiceReverse::Scenario::BACKUP); + std::vectorbundleNamesOnly; + vector bundleNames {}; + for (auto &bundle : bundlesToBackup) { + bundleNames.emplace_back(bundle.bundleName); + } + std::map bundleNameDetailMap = + BJsonUtil::BuildBundleInfos(bundleNames, bundleInfos, bundleNamesOnly, session_->GetSessionUserId()); + auto backupInfos = BundleMgrAdapter::GetBundleInfos(bundleNames, session_->GetSessionUserId()); + session_->AppendBundles(bundleNames); + for (auto info : backupInfos) { + session_->SetBundleDataSize(info.name, info.spaceOccupied); + session_->SetBackupExtName(info.name, info.extensionName); + if (info.allToBackup == false) { + session_->GetServiceReverseProxy()->IncrementalBackupOnBundleStarted( + BError(BError::Codes::SA_FORBID_BACKUP_RESTORE), info.name); + session_->RemoveExtInfo(info.name); + } + auto iter = bundleNameDetailMap.find(info.name); + if (iter == bundleNameDetailMap.end()) { + continue; + } + BJsonUtil::BundleDetailInfo bundleDetailInfo = iter->second; + if (bundleDetailInfo.type == UNICAST_TYPE) { + session_->SetBackupExtInfo(info.name, bundleDetailInfo.detail); + } + } + for (auto &bundleInfo : bundlesToBackup) { + session_->SetIncrementalData(bundleInfo); + } + OnStartSched(); + session_->DecreaseSessionCnt(); + return BError(BError::Codes::OK); + } catch (const BError &e) { + session_->DecreaseSessionCnt(); + HILOGE("Failed, errCode = %{public}d", e.GetCode()); + return e.GetCode(); + } catch (...) { + session_->DecreaseSessionCnt(); + HILOGI("Unexpected exception"); + return EPERM; + } +} + ErrCode Service::PublishIncrementalFile(const BFileInfo &fileInfo) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); diff --git a/services/backup_sa/src/module_ipc/service_stub.cpp b/services/backup_sa/src/module_ipc/service_stub.cpp index b2c8b04aa..0276fcf27 100644 --- a/services/backup_sa/src/module_ipc/service_stub.cpp +++ b/services/backup_sa/src/module_ipc/service_stub.cpp @@ -79,6 +79,9 @@ ServiceStub::ServiceStub() opToInterfaceMap_[static_cast( IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_INCREMENTAL_BACKUP_SESSION)] = &ServiceStub::CmdAppendBundlesIncrementalBackupSession; + opToInterfaceMap_[static_cast( + IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_INCREMENTAL_BACKUP_SESSION_DETAILS)]= + &ServiceStub::CmdAppendBundlesIncrementalDetailsBackupSession; opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_PUBLISH_INCREMENTAL_FILE)] = &ServiceStub::CmdPublishIncrementalFile; opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_PUBLISH_SA_INCREMENTAL_FILE)] = @@ -476,6 +479,23 @@ int32_t ServiceStub::CmdAppendBundlesIncrementalBackupSession(MessageParcel &dat return BError(BError::Codes::OK); } +int32_t ServiceStub::CmdAppendBundlesIncrementalDetailsBackupSession(MessageParcel &data, MessageParcel &reply) +{ + vector bundlesToBackup; + if (!ReadParcelableVector(bundlesToBackup, data)) { + return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleNames"); + } + vector detailInfos; + if (!data.ReadStringVector(&detailInfos)) { + return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive detailInfos"); + } + int32_t res = AppendBundlesIncrementalDetailsBackupSession(bundlesToBackup, detailInfos); + if (!reply.WriteInt32(res)) { + return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to send the result ") + to_string(res)); + } + return BError(BError::Codes::OK); +} + int32_t ServiceStub::CmdPublishIncrementalFile(MessageParcel &data, MessageParcel &reply) { unique_ptr fileInfo(data.ReadParcelable()); diff --git a/tests/mock/backup_kit_inner/service_proxy_mock.cpp b/tests/mock/backup_kit_inner/service_proxy_mock.cpp index 7b1f6477d..73b5f89da 100644 --- a/tests/mock/backup_kit_inner/service_proxy_mock.cpp +++ b/tests/mock/backup_kit_inner/service_proxy_mock.cpp @@ -140,6 +140,12 @@ ErrCode ServiceProxy::AppendBundlesIncrementalBackupSession(const vector &bundlesToBackup, + const std::vector &detailInfos) +{ + return BError(BError::Codes::OK); +} + ErrCode ServiceProxy::PublishIncrementalFile(const BFileInfo &fileInfo) { return BError(BError::Codes::OK); diff --git a/tests/mock/module_ipc/service_mock.cpp b/tests/mock/module_ipc/service_mock.cpp index 6555b55b6..9f33235d8 100644 --- a/tests/mock/module_ipc/service_mock.cpp +++ b/tests/mock/module_ipc/service_mock.cpp @@ -179,6 +179,12 @@ ErrCode Service::AppendBundlesIncrementalBackupSession(const std::vector &bundlesToBackup, + const std::vector &bundleInfos) +{ + return BError(BError::Codes::OK); +} + ErrCode Service::PublishIncrementalFile(const BFileInfo &fileInfo) { 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 da4e564ea..e4d652ca5 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 @@ -167,6 +167,12 @@ public: return BError(BError::Codes::OK); } + ErrCode AppendBundlesIncrementalDetailsBackupSession(const std::vector &bundlesToBackup, + const std::vector &bundleInfos) override + { + return BError(BError::Codes::OK); + } + ErrCode PublishIncrementalFile(const BFileInfo &fileInfo) override { return BError(BError::Codes::OK); 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 03e474046..43c636078 100644 --- a/tests/unittests/backup_sa/module_ipc/service_stub_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_stub_test.cpp @@ -68,6 +68,8 @@ public: MOCK_METHOD0(GetAppLocalListAndDoIncrementalBackup, ErrCode()); MOCK_METHOD1(InitIncrementalBackupSession, ErrCode(sptr remote)); MOCK_METHOD1(AppendBundlesIncrementalBackupSession, ErrCode(const std::vector &bundlesToBackup)); + MOCK_METHOD2(AppendBundlesIncrementalDetailsBackupSession, + ErrCode(const std::vector &bundlesToBackup, const std::vector &bundleInfos)); MOCK_METHOD1(PublishIncrementalFile, ErrCode(const BFileInfo &fileInfo)); MOCK_METHOD2(PublishSAIncrementalFile, ErrCode(const BFileInfo &fileInfo, UniqueFd fd)); diff --git a/tests/unittests/backup_sa/session/service_proxy_mock.cpp b/tests/unittests/backup_sa/session/service_proxy_mock.cpp index fa5ec4096..954ed0b27 100644 --- a/tests/unittests/backup_sa/session/service_proxy_mock.cpp +++ b/tests/unittests/backup_sa/session/service_proxy_mock.cpp @@ -119,6 +119,12 @@ ErrCode ServiceProxy::AppendBundlesIncrementalBackupSession(const vector &bundleNames, + const vector &bundleInfos) +{ + return BError(BError::Codes::OK); +} + ErrCode ServiceProxy::PublishIncrementalFile(const BFileInfo &fileInfo) { return BError(BError::Codes::OK); diff --git a/utils/include/b_resources/b_constants.h b/utils/include/b_resources/b_constants.h index 393ca03bc..88c4a27ab 100644 --- a/utils/include/b_resources/b_constants.h +++ b/utils/include/b_resources/b_constants.h @@ -28,6 +28,7 @@ static inline const char *EXTENSION_ACTION_PARA = "extensionAction"; static inline const char *EXTENSION_RESTORE_TYPE_PARA = "restoreType"; static inline const char *EXTENSION_VERSION_CODE_PARA = "versionCode"; static inline const char *EXTENSION_VERSION_NAME_PARA = "versionName"; +static inline const char *EXTENSION_BAXKUP_EXT_INFO_PARA = "backupExtInfo"; enum class ExtensionAction { INVALID = 0, -- Gitee