diff --git a/frameworks/native/backup_ext/include/ext_backup.h b/frameworks/native/backup_ext/include/ext_backup.h index a7a015f3ab89ab8859ff7198977851ad4533b238..317175732a0e72690022e1f0c67eaeb8e504b05f 100644 --- a/frameworks/native/backup_ext/include/ext_backup.h +++ b/frameworks/native/backup_ext/include/ext_backup.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -132,6 +132,11 @@ public: */ virtual ErrCode OnRestore(std::function callback); + /** + * @brief Called do GetBackupInfo. + */ + virtual ErrCode GetBackupInfo(std::function callback); + /** * @brief 数据迁移判断 * diff --git a/frameworks/native/backup_ext/include/ext_backup_js.h b/frameworks/native/backup_ext/include/ext_backup_js.h index b1e9d89d42ac45a4fead0f33f91f66c0cde54d3e..5c76b2df4675b49439f13c107b925f62a2ba2d3f 100644 --- a/frameworks/native/backup_ext/include/ext_backup_js.h +++ b/frameworks/native/backup_ext/include/ext_backup_js.h @@ -55,8 +55,10 @@ struct CallJsParam { struct CallBackInfo { std::function callback; + std::function callbackParam; CallBackInfo(std::function callbackIn) : callback(callbackIn) {} + CallBackInfo(std::function callbackIn) : callbackParam(callbackIn) {} }; class ExtBackupJs : public ExtBackup { @@ -96,6 +98,7 @@ public: * @param callback The callback. */ ErrCode OnRestore(std::function callback) override; + ErrCode GetBackupInfo(std::function callback) override; public: explicit ExtBackupJs(AbilityRuntime::JsRuntime &jsRuntime) : jsRuntime_(jsRuntime) {} @@ -117,6 +120,7 @@ private: AbilityRuntime::JsRuntime &jsRuntime_; std::unique_ptr jsObj_; std::shared_ptr callbackInfo_; + std::shared_ptr callbackInfoEx_; }; } // 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 da4aa29cd01adc6c786ffee5a8dd6b409c750611..bf47f8744fb68b90135d09a57466d217e2e6bb88 100644 --- a/frameworks/native/backup_ext/include/ext_extension.h +++ b/frameworks/native/backup_ext/include/ext_extension.h @@ -43,6 +43,7 @@ public: ErrCode PublishIncrementalFile(const std::string &fileName) override; ErrCode HandleIncrementalBackup(UniqueFd incrementalFd, UniqueFd manifestFd) override; std::tuple GetIncrementalBackupFileHandle() override; + ErrCode GetBackupInfo(std::string &result) override; void AsyncTaskRestoreForUpgrade(void); void ExtClear(void); @@ -145,6 +146,9 @@ private: std::shared_mutex lock_; std::shared_ptr extension_; std::vector tars_; + std::mutex getExtInfoMtx_; + std::condition_variable getExtInfoCondition_; + std::string backupInfo_; OHOS::ThreadPool threadPool_; }; } // namespace OHOS::FileManagement::Backup diff --git a/frameworks/native/backup_ext/include/ext_extension_stub.h b/frameworks/native/backup_ext/include/ext_extension_stub.h index 97dabc07c6854d3ea5636703753181ba55ee8c86..2f5f015b600809af453d80f42441b59b7788ec33 100644 --- a/frameworks/native/backup_ext/include/ext_extension_stub.h +++ b/frameworks/native/backup_ext/include/ext_extension_stub.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -40,6 +40,7 @@ private: ErrCode CmdPublishIncrementalFile(MessageParcel &data, MessageParcel &reply); ErrCode CmdHandleIncrementalBackup(MessageParcel &data, MessageParcel &reply); ErrCode CmdGetIncrementalBackupFileHandle(MessageParcel &data, MessageParcel &reply); + ErrCode CmdGetBackupInfo(MessageParcel &data, MessageParcel &reply); private: using ExtensionInterface = int32_t (ExtExtensionStub::*)(MessageParcel &data, MessageParcel &reply); diff --git a/frameworks/native/backup_ext/src/ext_backup.cpp b/frameworks/native/backup_ext/src/ext_backup.cpp index 298df3f9cd0d69912e233bafb14ba85fe00844b2..0ffd1dd0041e465902be3119481df94f92a6bd4c 100644 --- a/frameworks/native/backup_ext/src/ext_backup.cpp +++ b/frameworks/native/backup_ext/src/ext_backup.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -256,4 +256,10 @@ ErrCode ExtBackup::OnRestore(function callback) return ERR_OK; } +ErrCode ExtBackup::GetBackupInfo(function callback) +{ + HILOGI("BackupExtensionAbility(base) GetBackupInfo."); + 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 edccf655b7eede93dc47b4b12fdd51cd5185be60..f0308a6e6ae297bcb06fc41f9a61468459fbf933 100644 --- a/frameworks/native/backup_ext/src/ext_backup_js.cpp +++ b/frameworks/native/backup_ext/src/ext_backup_js.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -291,6 +291,38 @@ ErrCode ExtBackupJs::OnRestore(function callback) return errCode; } +ErrCode ExtBackupJs::GetBackupInfo(std::function callback) +{ + 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, + napi_value result) -> bool { + if (!CheckPromise(env, result)) { + size_t strLen = 0; + napi_status status = napi_get_value_string_utf8(env, result, nullptr, -1, &strLen); + if (status != napi_ok) { + return false; + } + size_t bufLen = strLen + 1; + unique_ptr str = make_unique(bufLen); + status = napi_get_value_string_utf8(env, result, str.get(), bufLen, &strLen); + callBackInfo->callbackParam(str.get()); + return true; + } + HILOGI("BackupExtensionAbulity(JS) GetBackupInfo ok."); + return CallPromise(*jsRuntime, result, callBackInfo.get()); + }; + + auto errCode = CallJsMethod("getBackupInfo", jsRuntime_, jsObj_.get(), {}, retParser); + if (errCode != ERR_OK) { + HILOGE("CallJsMethod error, code:%{public}d.", errCode); + } + HILOGI("BackupExtensionAbulity(JS) GetBackupInfo end."); + return errCode; +} + static int DoCallJsMethod(CallJsParam *param) { AbilityRuntime::JsRuntime *jsRuntime = param->jsRuntime; diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index d44fc6e4165a0b47092d7c11cdbff7b177950306..98e54a135ae2cb3fbbe48d5992c071cd1a9d7ac6 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -69,6 +69,7 @@ using namespace std; namespace { const int64_t DEFAULT_SLICE_SIZE = 100 * 1024 * 1024; // 分片文件大小为100M const uint32_t MAX_FILE_COUNT = 6000; // 单个tar包最多包含6000个文件 +const int32_t CONNECT_WAIT_TIME_S = 15; } // namespace void BackupExtExtension::VerifyCaller() @@ -1578,4 +1579,29 @@ void BackupExtExtension::AppIncrementalDone(ErrCode errCode) HILOGE("Failed to notify the app done. err = %{public}d", ret); } } + +ErrCode BackupExtExtension::GetBackupInfo(std::string &result) +{ + auto obj = wptr(this); + auto ptr = obj.promote(); + auto callBackup = [ptr](std::string result) { + HILOGI("GetBackupInfo callBackup start. result = %{public}s", result.c_str()); + ptr->backupInfo_ = result; + ptr->getExtInfoCondition_.notify_one(); + }; + auto ret = ptr->extension_->GetBackupInfo(callBackup); + if (ret != ERR_OK) { + HILOGE("Failed to notify the app done. err = %{public}d", ret); + return BError(BError::Codes::EXT_INVAL_ARG, "extension getBackupInfo exception").GetCode(); + } + HILOGD("GetBackupInfo getExtInfoMtx_ lock."); + std::unique_lock lock(getExtInfoMtx_); + getExtInfoCondition_.wait_for(lock, std::chrono::seconds(CONNECT_WAIT_TIME_S)); + HILOGD("GetBackupInfo getExtInfoMtx_ unlock."); + + result = backupInfo_; + backupInfo_.clear(); + + return ERR_OK; +} } // namespace OHOS::FileManagement::Backup diff --git a/frameworks/native/backup_ext/src/ext_extension_stub.cpp b/frameworks/native/backup_ext/src/ext_extension_stub.cpp index 888099411b3d69b57fafa7b73e9be5179c5785eb..804013fe9d25956b2bd395ece19100e729e93082 100644 --- a/frameworks/native/backup_ext/src/ext_extension_stub.cpp +++ b/frameworks/native/backup_ext/src/ext_extension_stub.cpp @@ -45,6 +45,8 @@ ExtExtensionStub::ExtExtensionStub() &ExtExtensionStub::CmdHandleIncrementalBackup; opToInterfaceMap_[static_cast(IExtensionInterfaceCode::CMD_GET_INCREMENTAL_BACKUP_FILE_HANDLE)] = &ExtExtensionStub::CmdGetIncrementalBackupFileHandle; + opToInterfaceMap_[static_cast(IExtensionInterfaceCode::CMD_GET_BACKUP_INFO)] = + &ExtExtensionStub::CmdGetBackupInfo; } int32_t ExtExtensionStub::OnRemoteRequest(uint32_t code, @@ -195,4 +197,18 @@ ErrCode ExtExtensionStub::CmdGetIncrementalBackupFileHandle(MessageParcel &data, } return BError(BError::Codes::OK); } + +ErrCode ExtExtensionStub::CmdGetBackupInfo(MessageParcel &data, MessageParcel &reply) +{ + HILOGI("CmdGetBackupInfo Begin"); + std::string result; + int ret = GetBackupInfo(result); + if (!reply.WriteInt32(ret)) { + return BError(BError::Codes::EXT_BROKEN_IPC, "Failed to send out the ret").GetCode(); + } + if (!reply.WriteString(result)) { + return BError(BError::Codes::EXT_BROKEN_IPC, "Failed to send out the result").GetCode(); + } + return BError(BError::Codes::OK); +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/frameworks/native/backup_kit_inner/include/service_reverse.h b/frameworks/native/backup_kit_inner/include/service_reverse.h index 81707f8b2a6bd5cc1605405b787af7c099a51efc..4dbf43273831aead9ed82d9e628e95e6dfa8d739 100644 --- a/frameworks/native/backup_kit_inner/include/service_reverse.h +++ b/frameworks/native/backup_kit_inner/include/service_reverse.h @@ -34,6 +34,7 @@ public: void RestoreOnBundleFinished(int32_t errCode, std::string bundleName) override; void RestoreOnAllBundlesFinished(int32_t errCode) override; void RestoreOnFileReady(std::string bundleName, std::string fileName, int fd) override; + void RestoreOnResultReport(std::string result) override; void IncrementalBackupOnFileReady(std::string bundleName, std::string fileName, int fd, int manifestFd) override; void IncrementalBackupOnBundleStarted(int32_t errCode, std::string bundleName) override; 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 650a7e784da1d050b0cfb004e887cdc9fe78bf66..b58400ec9c6dcf63c00cd3b5929cd0d29d001e64 100644 --- a/frameworks/native/backup_kit_inner/include/service_reverse_stub.h +++ b/frameworks/native/backup_kit_inner/include/service_reverse_stub.h @@ -43,6 +43,7 @@ private: int32_t CmdRestoreOnBundleFinished(MessageParcel &data, MessageParcel &reply); int32_t CmdRestoreOnAllBundlesFinished(MessageParcel &data, MessageParcel &reply); int32_t CmdRestoreOnFileReady(MessageParcel &data, MessageParcel &reply); + int32_t CmdRestoreOnResultReport(MessageParcel &data, MessageParcel &reply); int32_t CmdIncrementalBackupOnFileReady(MessageParcel &data, MessageParcel &reply); int32_t CmdIncrementalBackupOnBundleStarted(MessageParcel &data, MessageParcel &reply); diff --git a/frameworks/native/backup_kit_inner/src/b_session_restore_async.cpp b/frameworks/native/backup_kit_inner/src/b_session_restore_async.cpp index 9336f3d95f948b52ca5a3b7efa57a9582e85ec58..5e1b8b18ae6384e6b1ecacec0ec50011ffcc1f79 100644 --- a/frameworks/native/backup_kit_inner/src/b_session_restore_async.cpp +++ b/frameworks/native/backup_kit_inner/src/b_session_restore_async.cpp @@ -57,6 +57,7 @@ shared_ptr BSessionRestoreAsync::Init(Callbacks callbacks) .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_proxy.cpp b/frameworks/native/backup_kit_inner/src/service_proxy.cpp index 0e03d48ef067b3554e6bc33b86bf65f47a03b4f1..f7a63ade93fd60243438672ec590945313b412f0 100644 --- a/frameworks/native/backup_kit_inner/src/service_proxy.cpp +++ b/frameworks/native/backup_kit_inner/src/service_proxy.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -406,4 +406,29 @@ void ServiceProxy::ServiceProxyLoadCallback::OnLoadSystemAbilityFail(int32_t sys isLoadSuccess_.store(false); proxyConVar_.notify_one(); } + +ErrCode ServiceProxy::GetBackupInfo(BundleName &bundleName, std::string &result) +{ + HILOGI("ServiceProxy GetBackupInfo Begin."); + BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor())) { + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode(); + } + if (!data.WriteString(bundleName)) { + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send bundleName").GetCode(); + } + MessageParcel reply; + MessageOption option; + option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME); + int32_t ret = Remote()->SendRequest(static_cast(IServiceInterfaceCode::SERVICE_CMD_GET_BACKUP_INFO), + data, reply, option); + if (ret != NO_ERROR) { + string str = "Failed to send out the request because of " + to_string(ret); + return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode(); + } + reply.ReadString(result); + HILOGI("ServiceProxy GetBackupInfo end. result = %s", result.c_str()); + return BError(BError::Codes::OK, "success"); +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/frameworks/native/backup_kit_inner/src/service_reverse.cpp b/frameworks/native/backup_kit_inner/src/service_reverse.cpp index 6564af51a70afd1142b9ca97e2cab607e638cb10..1fb6d5b41f42bdde1d9eab98a49070379b4156b1 100644 --- a/frameworks/native/backup_kit_inner/src/service_reverse.cpp +++ b/frameworks/native/backup_kit_inner/src/service_reverse.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -103,6 +103,12 @@ void ServiceReverse::RestoreOnFileReady(string bundleName, string fileName, int callbacksRestore_.onFileReady(bFileInfo, UniqueFd(fd)); } +void ServiceReverse::RestoreOnResultReport(string result) +{ + HILOGI("ServiceReverse RestoreOnResultReport begin with result: %s", result.c_str()); + callbacksRestore_.onResultReport(result); +} + ServiceReverse::ServiceReverse(BSessionBackup::Callbacks callbacks) : scenario_(Scenario::BACKUP), callbacksBackup_(callbacks) { 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 082a2a24c21f428253cdbfbb1f9535b33b867d10..942b91fa070311d490073321a362c91df0539031 100644 --- a/frameworks/native/backup_kit_inner/src/service_reverse_stub.cpp +++ b/frameworks/native/backup_kit_inner/src/service_reverse_stub.cpp @@ -63,6 +63,8 @@ ServiceReverseStub::ServiceReverseStub() &ServiceReverseStub::CmdRestoreOnAllBundlesFinished; opToInterfaceMap_[static_cast(IServiceReverseInterfaceCode::SERVICER_RESTORE_ON_FILE_READY)] = &ServiceReverseStub::CmdRestoreOnFileReady; + opToInterfaceMap_[static_cast(IServiceReverseInterfaceCode::SERVICER_RESTORE_ON_RESULT_REPORT)] = + &ServiceReverseStub::CmdRestoreOnResultReport; opToInterfaceMap_[static_cast(IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_BACKUP_ON_FILE_READY)] = &ServiceReverseStub::CmdIncrementalBackupOnFileReady; @@ -153,6 +155,16 @@ int32_t ServiceReverseStub::CmdRestoreOnFileReady(MessageParcel &data, MessagePa return BError(BError::Codes::OK); } +int32_t ServiceReverseStub::CmdRestoreOnResultReport(MessageParcel &data, MessageParcel &reply) +{ + std::string result; + if (!data.ReadString(result)) { + return BError(BError::Codes::EXT_INVAL_ARG, "Failed to read result").GetCode(); + } + RestoreOnResultReport(result); + return BError(BError::Codes::OK); +} + int32_t ServiceReverseStub::CmdIncrementalBackupOnFileReady(MessageParcel &data, MessageParcel &reply) { auto bundleName = data.ReadString(); 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 1e21a35d28357bc042ba033f707e9e5d34c7516d..6f73f63638199adb5a61d11a6768c77fff3be39e 100644 --- a/interfaces/api/js/napi/backup_ext/backup_extension_ability.js +++ b/interfaces/api/js/napi/backup_ext/backup_extension_ability.js @@ -21,6 +21,10 @@ class BackupExtensionAbility { onRestore(versionBackupedBundle) { console.log(versionBackupedBundle) } + + getBackupInfo() { + console.log() + } } export default BackupExtensionAbility \ No newline at end of file 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 59fe34360f272729a45417bdbda13cab9002910b..d97763fa0916bc700947a8159a6d7e66e94e5b32 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,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_async.h b/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore_async.h index 9b7853706ed8f7ba069a60719ab714a993904bdc..6e7a4df6f934ccb0aadeee894f8aa23991c902c0 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,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/i_extension.h b/interfaces/inner_api/native/backup_kit_inner/impl/i_extension.h index d5c0d7b358c688e1d9117f845bd4f8642e210402..3641d6a4a5e74b9cab116d06f846570d02629a80 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/i_extension.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/i_extension.h @@ -36,6 +36,7 @@ public: virtual ErrCode PublishIncrementalFile(const std::string &fileName) = 0; virtual ErrCode HandleIncrementalBackup(UniqueFd incrementalFd, UniqueFd manifestFd) = 0; virtual std::tuple GetIncrementalBackupFileHandle() = 0; + virtual ErrCode GetBackupInfo(std::string &result) = 0; }; } // namespace OHOS::FileManagement::Backup diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/i_extension_ipc_interface_code.h b/interfaces/inner_api/native/backup_kit_inner/impl/i_extension_ipc_interface_code.h index 81eaf29f576cb3d709ec338026ffc9288cbabbeb..5956908251ee0ac189116cec6e2f579f0975b886 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/i_extension_ipc_interface_code.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/i_extension_ipc_interface_code.h @@ -28,6 +28,7 @@ enum class IExtensionInterfaceCode { CMD_PUBLISH_INCREMENTAL_FILE, CMD_HANDLE_INCREMENTAL_BACKUP, CMD_GET_INCREMENTAL_BACKUP_FILE_HANDLE, + CMD_GET_BACKUP_INFO, }; } // namespace OHOS::FileManagement::Backup 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 4e58bbd5ec3d2b2b85ca3ad19d7eb213005d404d..ef588233b0cf483d20f6e4faacbde3ed334a34e9 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 @@ -60,6 +60,7 @@ public: virtual ErrCode AppIncrementalFileReady(const std::string &fileName, UniqueFd fd, UniqueFd manifestFd) = 0; virtual ErrCode AppIncrementalDone(ErrCode errCode) = 0; virtual ErrCode GetIncrementalFileHandle(const std::string &bundleName, const std::string &fileName) = 0; + virtual ErrCode GetBackupInfo(BundleName &bundleName, std::string &result) = 0; DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.Filemanagement.Backup.IService") }; 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 1de726cf2accd71d1f9bb6199c0c4e5bbf546dca..a1e4d4ba63eea2ce6a7326c1ac6654a2c456d733 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 @@ -38,6 +38,7 @@ enum class IServiceInterfaceCode { SERVICE_CMD_APP_INCREMENTAL_FILE_READY, SERVICE_CMD_APP_INCREMENTAL_DONE, SERVICE_CMD_GET_INCREMENTAL_FILE_NAME, + SERVICE_CMD_GET_BACKUP_INFO, }; } // namespace OHOS::FileManagement::Backup 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 fbdf2408b961261ecc3d2f4ef48242974f9d9fce..3fe6b51f9d687f014142db69a27abe5c7142661e 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 @@ -40,6 +40,7 @@ public: virtual void RestoreOnBundleFinished(int32_t errCode, std::string bundleName) = 0; virtual void RestoreOnAllBundlesFinished(int32_t errCode) = 0; virtual void RestoreOnFileReady(std::string bundleName, std::string fileName, int fd) = 0; + virtual void RestoreOnResultReport(std::string result) = 0; virtual void IncrementalBackupOnFileReady(std::string bundleName, std::string fileName, int fd, int manifestFd) = 0; virtual void IncrementalBackupOnBundleStarted(int32_t errCode, std::string bundleName) = 0; 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 b3300a21bb2033446d4e0682a4b4a1baf6797ed6..1c3bfecbd492472fb7e76d114333568623a93cbc 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 @@ -27,6 +27,7 @@ enum class IServiceReverseInterfaceCode { SERVICER_RESTORE_ON_SUB_TASK_FINISHED, SERVICER_RESTORE_ON_TASK_FINISHED, SERVICER_RESTORE_ON_FILE_READY, + SERVICER_RESTORE_ON_RESULT_REPORT, SERVICER_INCREMENTAL_BACKUP_ON_FILE_READY, SERVICER_INCREMENTAL_BACKUP_ON_SUB_TASK_STARTED, SERVICER_INCREMENTAL_BACKUP_ON_SUB_TASK_FINISHED, 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 6204832a8ddf2165d1c3cbbd1dab0a71e27f15ad..3fc047e7dabc28ec1a78f3805a109e384416398b 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 @@ -51,6 +51,7 @@ public: ErrCode AppIncrementalFileReady(const std::string &fileName, UniqueFd fd, UniqueFd manifestFd) override; ErrCode AppIncrementalDone(ErrCode errCode) override; ErrCode GetIncrementalFileHandle(const std::string &bundleName, const std::string &fileName) override; + ErrCode GetBackupInfo(BundleName &bundleName, std::string &result) override; public: explicit ServiceProxy(const sptr &impl) : IRemoteProxy(impl) {} diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 283085c8781acd80f1f9ce10be491f402bfba785..2573f0e060292729383302c22ec5cec4343ad124 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -116,9 +116,9 @@ ohos_shared_library("backup") { ] sources = [ - "${path_backup_js}/backup/local_capabilities.cpp", "${path_backup_js}/backup/module.cpp", "${path_backup_js}/backup/prop_n_exporter.cpp", + "${path_backup_js}/backup/prop_n_operation.cpp", "${path_backup_js}/backup/session_backup_n_exporter.cpp", "${path_backup_js}/backup/session_incremental_backup_n_exporter.cpp", "${path_backup_js}/backup/session_restore_n_exporter.cpp", diff --git a/interfaces/kits/js/backup/general_callbacks.h b/interfaces/kits/js/backup/general_callbacks.h index 6bde169b8d7f793ab38dd02dda5245dc9e060a82..d5d0e1a2e9c105004325697d0495f0c00c2e8f19 100644 --- a/interfaces/kits/js/backup/general_callbacks.h +++ b/interfaces/kits/js/backup/general_callbacks.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -29,7 +29,8 @@ public: onBundleBegin(env, thisPtr, jsCallbacks.GetProp("onBundleBegin")), onBundleEnd(env, thisPtr, jsCallbacks.GetProp("onBundleEnd")), onAllBundlesEnd(env, thisPtr, jsCallbacks.GetProp("onAllBundlesEnd")), - onBackupServiceDied(env, thisPtr, jsCallbacks.GetProp("onBackupServiceDied")) {}; + onBackupServiceDied(env, thisPtr, jsCallbacks.GetProp("onBackupServiceDied")), + onResultReport(env, thisPtr, jsCallbacks.GetProp("onResultReport")) {}; public: LibN::NAsyncWorkCallback onFileReady; @@ -37,6 +38,7 @@ public: LibN::NAsyncWorkCallback onBundleEnd; LibN::NAsyncWorkCallback onAllBundlesEnd; LibN::NAsyncWorkCallback onBackupServiceDied; + LibN::NAsyncWorkCallback onResultReport; }; } // namespace OHOS::FileManagement::Backup #endif // INTERFACES_KITS_JS_SRC_MOD_BACKUP_PROPERTIES_GENERAL_CALLBACKS_H \ No newline at end of file diff --git a/interfaces/kits/js/backup/prop_n_exporter.cpp b/interfaces/kits/js/backup/prop_n_exporter.cpp index 572a46db4bf77529bc9caedf7f2c073f983398d5..15d901d62297460f701c4a5a44472e1c7ad6d99c 100644 --- a/interfaces/kits/js/backup/prop_n_exporter.cpp +++ b/interfaces/kits/js/backup/prop_n_exporter.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -14,7 +14,7 @@ */ #include "prop_n_exporter.h" -#include "local_capabilities.h" +#include "prop_n_operation.h" namespace OHOS::FileManagement::Backup { using namespace std; @@ -23,7 +23,8 @@ using namespace LibN; bool PropNExporter::Export() { return exports_.AddProp({ - NVal::DeclareNapiFunction("getLocalCapabilities", LocalCapabilities::Async), + NVal::DeclareNapiFunction("getLocalCapabilities", PropNOperation::Async), + NVal::DeclareNapiFunction("getBackupInfo", PropNOperation::DoGetBackupInfo), }); } diff --git a/interfaces/kits/js/backup/prop_n_exporter.h b/interfaces/kits/js/backup/prop_n_exporter.h index defb4c78ac796430264493eef8384fb1a801b471..ca48be1e08b787250b746b96ffb270bbb1ef939a 100644 --- a/interfaces/kits/js/backup/prop_n_exporter.h +++ b/interfaces/kits/js/backup/prop_n_exporter.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -22,7 +22,7 @@ namespace OHOS::FileManagement::Backup { class PropNExporter final : public LibN::NExporter { public: - inline static const std::string className = "LocalCapabilities"; + inline static const std::string className = "propNOperation"; bool Export() override; std::string GetClassName() override; diff --git a/interfaces/kits/js/backup/local_capabilities.cpp b/interfaces/kits/js/backup/prop_n_operation.cpp similarity index 80% rename from interfaces/kits/js/backup/local_capabilities.cpp rename to interfaces/kits/js/backup/prop_n_operation.cpp index 10570a91e04578348fc4823287406b5c3d5557bc..3055a56a842558389f03741c7f152edfacc73bc2 100644 --- a/interfaces/kits/js/backup/local_capabilities.cpp +++ b/interfaces/kits/js/backup/prop_n_operation.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Copyright (c) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "local_capabilities.h" +#include "prop_n_operation.h" #include "b_error/b_error.h" #include "b_incremental_data.h" @@ -158,7 +158,7 @@ static napi_value AsyncDataList(napi_env env, const NFuncArg& funcArg) return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_LOCALCAPABILITIES_NAME, cbExec, cbCompl).val_; } -napi_value LocalCapabilities::Async(napi_env env, napi_callback_info info) +napi_value PropNOperation::Async(napi_env env, napi_callback_info info) { HILOGI("called LocalCapabilities::Async begin"); NFuncArg funcArg(env, info); @@ -178,4 +178,45 @@ napi_value LocalCapabilities::Async(napi_env env, napi_callback_info info) return AsyncCallback(env, funcArg); } + +napi_value PropNOperation::DoGetBackupInfo(napi_env env, napi_callback_info info) +{ + HILOGI("called DoGetBackupInfo begin"); + std::string result; + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + HILOGE("Number of arguments unmatched."); + NError(BError(BError::Codes::SDK_INVAL_ARG, "Number of arguments unmatched.").GetCode()).ThrowErr(env); + return nullptr; + } + NVal jsBundle(env, funcArg[NARG_POS::FIRST]); + auto [succ, bundle, size] = jsBundle.ToUTF8String(); + if (!succ) { + HILOGE("First argument is not string."); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + ServiceProxy::InvaildInstance(); + auto proxy = ServiceProxy::GetInstance(); + if (!proxy) { + HILOGE("called DoGetBackupInfo,failed to get proxy"); + return nullptr; + } + std::string bundleName = bundle.get(); + ErrCode errcode = proxy->GetBackupInfo(bundleName, result); + if (errcode != 0) { + HILOGE("proxy->GetBackupInfo faild."); + return nullptr; + } + + napi_value nResult; + napi_status status = napi_create_string_utf8(env, result.c_str(), result.size(), &nResult); + if (status != napi_ok) { + HILOGE("napi_create_string_utf8 faild."); + return nullptr; + } + HILOGI("DoGetBackupInfo success with result: %{public}s", result.c_str()); + return nResult; +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/interfaces/kits/js/backup/local_capabilities.h b/interfaces/kits/js/backup/prop_n_operation.h similarity index 87% rename from interfaces/kits/js/backup/local_capabilities.h rename to interfaces/kits/js/backup/prop_n_operation.h index 716b5d3dfb01a20ae323aa3e6e96604443b222ef..76ffb2ff5f43adec9bd5a756b2b9bb0f84c7723a 100644 --- a/interfaces/kits/js/backup/local_capabilities.h +++ b/interfaces/kits/js/backup/prop_n_operation.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -19,9 +19,10 @@ #include namespace OHOS::FileManagement::Backup { -class LocalCapabilities final { +class PropNOperation final { public: static napi_value Async(napi_env env, napi_callback_info info); + static napi_value DoGetBackupInfo(napi_env env, napi_callback_info info); }; const std::string PROCEDURE_LOCALCAPABILITIES_NAME = "getLocalCapalities"; diff --git a/interfaces/kits/js/backup/session_restore_n_exporter.cpp b/interfaces/kits/js/backup/session_restore_n_exporter.cpp index 34afc969781673d8961d8187bc6464aafcaddb14..a950ca2066d1799980ee35cc3206b9c3c0971b91 100644 --- a/interfaces/kits/js/backup/session_restore_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_restore_n_exporter.cpp @@ -247,6 +247,37 @@ static void OnBackupServiceDied(weak_ptr pCallbacks) callbacks->onBackupServiceDied.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 = [result {result}](napi_env env, NError err) -> NVal { + NVal resultStr = NVal::CreateUTF8String(env, result); + NVal res; + napi_status status = napi_set_named_property(env, res.val_, FILEIO_TAG_ERR_DATA.c_str(), resultStr.val_); + if (status != napi_ok) { + HILOGE("Failed to set data property, status %{public}d, bundleName %{public}s", status, result.c_str()); + } + + return res; + }; + + callbacks->onResultReport.ThreadSafeSchedule(cbCompl); +} + napi_value SessionRestoreNExporter::Constructor(napi_env env, napi_callback_info cbinfo) { HILOGI("called SessionRestore::Constructor begin"); @@ -284,6 +315,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)}); } if (!restoreEntity->sessionWhole && !restoreEntity->sessionSheet) { diff --git a/services/backup_sa/include/module_ipc/service.h b/services/backup_sa/include/module_ipc/service.h index 66dc61a548a628e2e0056232a1be444aa188d90e..6aaa3e7d15c9e2f2b8500b06655a62a1a0feacc0 100644 --- a/services/backup_sa/include/module_ipc/service.h +++ b/services/backup_sa/include/module_ipc/service.h @@ -56,6 +56,7 @@ public: ErrCode AppIncrementalFileReady(const std::string &fileName, UniqueFd fd, UniqueFd manifestFd) override; ErrCode AppIncrementalDone(ErrCode errCode) override; ErrCode GetIncrementalFileHandle(const std::string &bundleName, const std::string &fileName) override; + ErrCode GetBackupInfo(BundleName &bundleName, std::string &result) override; // 以下都是非IPC接口 public: @@ -113,6 +114,13 @@ public: * */ void SessionDeactive(); + /** + * @brief 构造拉起应用所需的want + * + * @param bundleName 应用名称 + * + */ + AAFwk::Want CreateConnectWant (BundleName &bundleName); public: explicit Service(int32_t saID, bool runOnCreate = false) : SystemAbility(saID, runOnCreate) @@ -197,6 +205,8 @@ private: private: static sptr instance_; static std::mutex instanceLock_; + std::mutex getBackupInfoMutx_; + std::condition_variable getBackupInfoCondition_; static inline std::atomic seed {1}; sptr session_; 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 401644b79c28c390ebdd8d2dd46d363a2f92545f..14923ee45085f69e3344aeb2299ca0cc2ba450bb 100644 --- a/services/backup_sa/include/module_ipc/service_reverse_proxy.h +++ b/services/backup_sa/include/module_ipc/service_reverse_proxy.h @@ -31,6 +31,7 @@ public: void RestoreOnBundleFinished(int32_t errCode, std::string bundleName) override; void RestoreOnAllBundlesFinished(int32_t errCode) override; void RestoreOnFileReady(std::string bundleName, std::string fileName, int fd) override; + void RestoreOnResultReport(std::string result) override; void IncrementalBackupOnFileReady(std::string bundleName, std::string fileName, int fd, int manifestFd) override; void IncrementalBackupOnBundleStarted(int32_t errCode, std::string bundleName) override; diff --git a/services/backup_sa/include/module_ipc/service_stub.h b/services/backup_sa/include/module_ipc/service_stub.h index 066c0f878bbc1e3a37e059ee6e7f6bf7c50cc2a4..2367f50162894e79b755a1184cb098ad6ff710b2 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 CmdAppIncrementalFileReady(MessageParcel &data, MessageParcel &reply); int32_t CmdAppIncrementalDone(MessageParcel &data, MessageParcel &reply); int32_t CmdGetIncrementalFileHandle(MessageParcel &data, MessageParcel &reply); + int32_t CmdGetBackupInfo(MessageParcel &data, MessageParcel &reply); public: template diff --git a/services/backup_sa/include/module_ipc/svc_extension_proxy.h b/services/backup_sa/include/module_ipc/svc_extension_proxy.h index f208cb2d487cbbc96fa067bf63d9094846e1343a..a63a781aa34b90cba12ff4140021eed5e589f638 100644 --- a/services/backup_sa/include/module_ipc/svc_extension_proxy.h +++ b/services/backup_sa/include/module_ipc/svc_extension_proxy.h @@ -32,6 +32,7 @@ public: ErrCode PublishIncrementalFile(const std::string &fileName) override; ErrCode HandleIncrementalBackup(UniqueFd incrementalFd, UniqueFd manifestFd) override; std::tuple GetIncrementalBackupFileHandle() override; + ErrCode GetBackupInfo(std::string &result) override; public: explicit SvcExtensionProxy(const sptr &remote) : IRemoteProxy(remote) {} diff --git a/services/backup_sa/include/module_ipc/svc_session_manager.h b/services/backup_sa/include/module_ipc/svc_session_manager.h index 777765d1c3118a3f6d993f73f36539caa620628c..12c647180475236369ef15aa6ad3773214468aaa 100644 --- a/services/backup_sa/include/module_ipc/svc_session_manager.h +++ b/services/backup_sa/include/module_ipc/svc_session_manager.h @@ -253,6 +253,13 @@ public: */ void AppendBundles(const std::vector &bundleNames); + /** + * @brief 添加指定应用 + * + * @param bundleName 应用名称 + */ + void CreateBackupConnection(BundleName &bundleName); + /** * @brief 开始备份 * diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index 7043f60e72e54f43d69a37bca060429445534b89..0844f9e8da124b177d1bc7723f4eb71992a300a2 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -64,6 +64,7 @@ REGISTER_SYSTEM_ABILITY_BY_ID(Service, FILEMANAGEMENT_BACKUP_SERVICE_SA_ID, fals namespace { constexpr int32_t DEBUG_ID = 100; +const int32_t CONNECT_WAIT_TIME_S = 15; } // namespace /* Shell/Xts user id equal to 0/1, we need set default 100 */ @@ -931,4 +932,69 @@ void Service::SessionDeactive() return; } } + +ErrCode Service::GetBackupInfo(BundleName &bundleName, std::string &result) +{ + try { + HILOGI("Service::GetBackupInfo begin."); + session_->IncreaseSessionCnt(); + session_->CreateBackupConnection(bundleName); + auto backupConnection = session_->GetExtConnection(bundleName); + auto callConnDone = [ptr {wptr(this)}](const string &&bundleName) { + HILOGI("callConnDone begin."); + auto thisPtr = ptr.promote(); + if (!thisPtr) { + HILOGW("this pointer is null."); + return; + } + thisPtr->getBackupInfoCondition_.notify_one(); + }; + backupConnection->SetCallback(callConnDone); + AAFwk::Want want = CreateConnectWant(bundleName); + backupConnection->ConnectBackupExtAbility(want, session_->GetSessionUserId()); + std::unique_lock lock(getBackupInfoMutx_); + getBackupInfoCondition_.wait_for(lock, std::chrono::seconds(CONNECT_WAIT_TIME_S)); + auto proxy = backupConnection->GetBackupExtProxy(); + if (!proxy) { + throw BError(BError::Codes::SA_INVAL_ARG, "Extension backup Proxy is empty"); + } + auto ret = proxy->GetBackupInfo(result); + backupConnection->DisconnectBackupExtAbility(); + if (ret != ERR_OK) { + HILOGE("Call Ext GetBackupInfo faild."); + return BError(BError::Codes::SA_INVAL_ARG); + } + HILOGI("Service::GetBackupInfo end. result: %s", result.c_str()); + session_->DecreaseSessionCnt(); + return BError(BError::Codes::OK); + } catch (...) { + session_->DecreaseSessionCnt(); + HILOGI("Unexpected exception"); + return UniqueFd(-EPERM); + } +} + +AAFwk::Want Service::CreateConnectWant (BundleName &bundleName) +{ + IServiceReverse::Scenario scenario = session_->GetScenario(); + BConstants::ExtensionAction action; + if (scenario == IServiceReverse::Scenario::BACKUP) { + action = BConstants::ExtensionAction::BACKUP; + } else if (scenario == IServiceReverse::Scenario::RESTORE) { + action = BConstants::ExtensionAction::RESTORE; + } else { + throw BError(BError::Codes::SA_INVAL_ARG, "Failed to scenario"); + } + AAFwk::Want want; + string backupExtName = session_->GetBackupExtName(bundleName); /* new device app ext name */ + 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 */ + 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); + return want; +} } // namespace OHOS::FileManagement::Backup 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 dc6d5fb894d5d1cf71e63fda5ac74de2435f5e44..a0f5a510cf5da76cd023a978d089395f20aa04df 100644 --- a/services/backup_sa/src/module_ipc/service_reverse_proxy.cpp +++ b/services/backup_sa/src/module_ipc/service_reverse_proxy.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -177,4 +177,23 @@ void ServiceReverseProxy::RestoreOnFileReady(string bundleName, string fileName, throw BError(BError::Codes::SA_BROKEN_IPC, to_string(err)); } } + +void ServiceReverseProxy::RestoreOnResultReport(string result) +{ + HILOGI("ServiceReverseProxy::RestoreOnResultReport Begin with result: %s", result.c_str()); + 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_RESTORE_ON_RESULT_REPORT), data, reply, + option); + err != ERR_OK) { + throw BError(BError::Codes::SA_BROKEN_IPC, to_string(err)); + } +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/services/backup_sa/src/module_ipc/service_stub.cpp b/services/backup_sa/src/module_ipc/service_stub.cpp index d55334d30a7575ab749044c3754f25f446eb347f..83aa61450b4e6d4c37160f2789c3b7b231fd260b 100644 --- a/services/backup_sa/src/module_ipc/service_stub.cpp +++ b/services/backup_sa/src/module_ipc/service_stub.cpp @@ -70,6 +70,8 @@ ServiceStub::ServiceStub() &ServiceStub::CmdAppIncrementalDone; opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_GET_INCREMENTAL_FILE_NAME)] = &ServiceStub::CmdGetIncrementalFileHandle; + opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_GET_BACKUP_INFO)] = + &ServiceStub::CmdGetBackupInfo; } int32_t ServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) @@ -276,6 +278,26 @@ int32_t ServiceStub::CmdFinish(MessageParcel &data, MessageParcel &reply) return BError(BError::Codes::OK); } +int32_t ServiceStub::CmdGetBackupInfo(MessageParcel &data, MessageParcel &reply) +{ + HILOGI("ServiceStub::CmdGetBackupInfo Begin."); + int ret = ERR_OK; + string bundleName; + if (!data.ReadString(bundleName)) { + return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to recive bundleName")); + } + string result; + ret = GetBackupInfo(bundleName, result); + if (ret != ERR_OK) { + return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to call GetBackupInfo")); + } + if (!reply.WriteString(result)) { + return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to write result")); + } + HILOGI("ServiceStub::CmdGetBackupInfo end."); + return BError(BError::Codes::OK); +} + int32_t ServiceStub::CmdRelease(MessageParcel &data, MessageParcel &reply) { HILOGI("Begin"); diff --git a/services/backup_sa/src/module_ipc/svc_extension_proxy.cpp b/services/backup_sa/src/module_ipc/svc_extension_proxy.cpp index 9479d43da0dcf3e855d451e55229e9c140466c53..18c7928215f38cd007a7b60694905e2089497c1f 100644 --- a/services/backup_sa/src/module_ipc/svc_extension_proxy.cpp +++ b/services/backup_sa/src/module_ipc/svc_extension_proxy.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -140,4 +140,35 @@ ErrCode SvcExtensionProxy::HandleRestore() HILOGI("Successful"); return reply.ReadInt32(); } + +ErrCode SvcExtensionProxy::GetBackupInfo(std::string &result) +{ + HILOGD("SvcExtensionProxy::GetBackupInfo begin."); + BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); + MessageParcel data; + data.WriteInterfaceToken(GetDescriptor()); + + MessageParcel reply; + MessageOption option; + int32_t ret = + Remote()->SendRequest(static_cast(IExtensionInterfaceCode::CMD_GET_BACKUP_INFO), data, reply, option); + if (ret != NO_ERROR) { + HILOGE("Received error %{public}d when doing IPC", ret); + return ErrCode(ret); + } + if (!reply.ReadInt32(ret)) { + HILOGE("fail to ReadInt32 ret"); + return ErrCode(ret); + } + if (ret != NO_ERROR) { + HILOGE("ret is not NO_ERROR. ret = %d", ret); + return ErrCode(ret); + } + if (!reply.ReadString(result)) { + HILOGE("fail to ReadInt32 ret"); + return ErrCode(ret); + } + HILOGI("SvcExtensionProxy::GetBackupInfo end. result: %s", result.c_str()); + return ret; +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file 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 0622b7040e3db4a407516dba0db8670b1f65bb2c..34c3b55b1ed62a5a8b6eef26bbed23824246a0f8 100644 --- a/services/backup_sa/src/module_ipc/svc_session_manager.cpp +++ b/services/backup_sa/src/module_ipc/svc_session_manager.cpp @@ -404,6 +404,20 @@ void SvcSessionManager::AppendBundles(const vector &bundleNames) impl_.isAppendFinish = true; } +void SvcSessionManager::CreateBackupConnection(BundleName &bundleName) +{ + HILOGI("SvcSessionManager::CreateBackupConnection begin."); + unique_lock lock(lock_); + if (!impl_.clientToken) { + throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + } + HILOGD("bundleName: %{public}s", bundleName.c_str()); + BackupExtInfo info {}; + info.backUpConnection = GetBackupExtAbility(bundleName); + info.backupExtName = bundleName; + impl_.backupExtNameMap.insert(make_pair(bundleName, info)); +} + void SvcSessionManager::Start() { HILOGI("Begin"); diff --git a/tests/mock/backup_kit_inner/b_session_restore_async_mock.cpp b/tests/mock/backup_kit_inner/b_session_restore_async_mock.cpp index a80632f9b8b3b3c6523b9ae731410f5ee9ab25f4..e76e4dbbb2d26c46c61bbbfbbad4473fbc1adbef 100644 --- a/tests/mock/backup_kit_inner/b_session_restore_async_mock.cpp +++ b/tests/mock/backup_kit_inner/b_session_restore_async_mock.cpp @@ -89,7 +89,7 @@ ErrCode BSessionRestoreAsync::AppendBundles(UniqueFd remoteCap, callbacks_.onBundleStarted(1, "com.example.app2backup"); callbacks_.onBundleFinished(1, "com.example.app2backup"); callbacks_.onAllBundlesFinished(1); - + callbacks_.onResultReport("com.example.app2backup"); callbacks_.onBackupServiceDied(); return BError(BError::Codes::OK); } diff --git a/tests/mock/backup_kit_inner/service_proxy_mock.cpp b/tests/mock/backup_kit_inner/service_proxy_mock.cpp index 1d9217548383690221f85bdc05a6399bdceb8b0e..25cbe64e75f1e85bcd0834d279f40c5a67462c89 100644 --- a/tests/mock/backup_kit_inner/service_proxy_mock.cpp +++ b/tests/mock/backup_kit_inner/service_proxy_mock.cpp @@ -138,6 +138,11 @@ ErrCode ServiceProxy::GetIncrementalFileHandle(const std::string &bundleName, co return BError(BError::Codes::OK); } +ErrCode ServiceProxy::GetBackupInfo(std::string &bundleName, std::string &result) +{ + return BError(BError::Codes::OK); +} + sptr ServiceProxy::GetInstance() { if (!GetMockGetInstance()) { diff --git a/tests/mock/module_ipc/service_mock.cpp b/tests/mock/module_ipc/service_mock.cpp index cb41e78f0e45c0d13670d9be5568897e23fb9377..38648a1ad7d2ab0118d8e456a27b87271cab6309 100644 --- a/tests/mock/module_ipc/service_mock.cpp +++ b/tests/mock/module_ipc/service_mock.cpp @@ -171,4 +171,9 @@ ErrCode Service::GetIncrementalFileHandle(const string &bundleName, const string { return BError(BError::Codes::OK); } + +ErrCode Service::GetBackupInfo(BundleName &bundleName, std::string &result) +{ + return BError(BError::Codes::OK); +} } // namespace OHOS::FileManagement::Backup diff --git a/tests/mock/module_ipc/service_reverse_proxy_mock.cpp b/tests/mock/module_ipc/service_reverse_proxy_mock.cpp index 3123ee3d74c4c5935222c695dc8bd34ff46f24e1..c52642c438c16c5289df37a7bce396cff527b39a 100644 --- a/tests/mock/module_ipc/service_reverse_proxy_mock.cpp +++ b/tests/mock/module_ipc/service_reverse_proxy_mock.cpp @@ -36,6 +36,8 @@ void ServiceReverseProxy::RestoreOnAllBundlesFinished(int32_t errCode) {} void ServiceReverseProxy::RestoreOnFileReady(string bundleName, string fileName, int fd) {} +void ServiceReverseProxy::RestoreOnResultReport(string result) {} + void ServiceReverseProxy::IncrementalBackupOnFileReady(string bundleName, string fileName, int fd, int manifestFd) {} void ServiceReverseProxy::IncrementalBackupOnBundleStarted(int32_t errCode, string bundleName) {} diff --git a/tests/mock/module_ipc/service_stub_mock.cpp b/tests/mock/module_ipc/service_stub_mock.cpp index 7ae9cced20181d08cb729f5ad7aa1c0e39848d1e..bcc3e4e40c2d4d7edb51a2ee97674a4545507c87 100644 --- a/tests/mock/module_ipc/service_stub_mock.cpp +++ b/tests/mock/module_ipc/service_stub_mock.cpp @@ -59,6 +59,8 @@ ServiceStub::ServiceStub() &ServiceStub::CmdAppIncrementalFileReady; opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_GET_INCREMENTAL_FILE_NAME)] = &ServiceStub::CmdGetIncrementalFileHandle; + opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_GET_BACKUP_INFO)] = + &ServiceStub::CmdGetBackupInfo; } int32_t ServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) @@ -175,6 +177,18 @@ int32_t ServiceStub::CmdFinish(MessageParcel &data, MessageParcel &reply) return BError(BError::Codes::OK); } +int32_t ServiceStub::CmdGetBackupInfo(MessageParcel &data, MessageParcel &reply) +{ + int ret = ERR_OK; + string bundleName; + if (!data.ReadString(bundleName)) { + return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to recive bundleName")); + } + std::string result; + ret = GetBackupInfo(bundleName, result); + return BError(BError::Codes::OK); +} + int32_t ServiceStub::CmdRelease(MessageParcel &data, MessageParcel &reply) { int res = Release(); diff --git a/tests/mock/module_ipc/svc_extension_proxy_mock.cpp b/tests/mock/module_ipc/svc_extension_proxy_mock.cpp index a973df624d911706bf5fb3860725619c00952e60..577db8a2118ffabd5123ee167971c3cdb8f863a7 100644 --- a/tests/mock/module_ipc/svc_extension_proxy_mock.cpp +++ b/tests/mock/module_ipc/svc_extension_proxy_mock.cpp @@ -43,6 +43,11 @@ ErrCode SvcExtensionProxy::HandleRestore() return 0; } +ErrCode SvcExtensionProxy::GetBackupInfo(std::string &result) +{ + return 0; +} + ErrCode SvcExtensionProxy::GetIncrementalFileHandle(const string &fileName) { return 0; diff --git a/tests/mock/module_ipc/svc_session_manager_mock.cpp b/tests/mock/module_ipc/svc_session_manager_mock.cpp index 386bc4a52db567a0ef5d614703fe1dcc054e85f5..69f45fd8d48593662c47251ea56a842d1fd8d85f 100644 --- a/tests/mock/module_ipc/svc_session_manager_mock.cpp +++ b/tests/mock/module_ipc/svc_session_manager_mock.cpp @@ -202,6 +202,15 @@ void SvcSessionManager::AppendBundles(const vector &bundleNames) impl_.backupExtNameMap.insert(make_pair("com.example.app2backup", info)); } +void SvcSessionManager::CreateBackupConnection(BundleName &bundleName) +{ + GTEST_LOG_(INFO) << "CreateBackupConnection"; + BackupExtInfo info {}; + info.backUpConnection = GetBackupExtAbility("com.example.app2backup"); + info.backupExtName = "com.example.app2backup"; + impl_.backupExtNameMap.insert(make_pair("com.example.app2backup", info)); +} + void SvcSessionManager::Start() {} void SvcSessionManager::Finish() {} diff --git a/tests/moduletests/backup_kit_inner/b_session_restore_async_test.cpp b/tests/moduletests/backup_kit_inner/b_session_restore_async_test.cpp index e9badac6b36cf72a9dafa31b8a0e2a9881eb4a37..02efd1058ba3703b9e6de3d3d5671f950c3c0e49 100644 --- a/tests/moduletests/backup_kit_inner/b_session_restore_async_test.cpp +++ b/tests/moduletests/backup_kit_inner/b_session_restore_async_test.cpp @@ -54,6 +54,11 @@ static void OnBackupServiceDied() GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest OnBackupServiceDied OK"; } +static void OnResultReport(std::string result) +{ + GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest OnResultReport OK"; +} + class BSessionRestoreAsyncTest : public testing::Test { public: static void SetUpTestCase(void) {}; @@ -87,6 +92,7 @@ void BSessionRestoreAsyncTest::Init() callbacks_.onBundleFinished = OnBundleFinished; callbacks_.onAllBundlesFinished = OnAllBundlesFinished; callbacks_.onBackupServiceDied = OnBackupServiceDied; + callbacks_.onResultReport = OnResultReport; } /** @@ -109,6 +115,7 @@ HWTEST_F(BSessionRestoreAsyncTest, SUB_backup_b_session_restore_async_0100, test callbacks_.onBundleFinished(ErrCode(BError::Codes::OK), ""); callbacks_.onAllBundlesFinished(ErrCode(BError::Codes::OK)); callbacks_.onBackupServiceDied(); + callbacks_.onResultReport(""); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest-an exception occurred by Callbacks."; diff --git a/tests/moduletests/backup_kit_inner/b_session_restore_test.cpp b/tests/moduletests/backup_kit_inner/b_session_restore_test.cpp index 77bded075e92f4ad1b095e24c4beb3db98652fc4..882708cf69adc5b5ecd83dc273190e33408d372d 100644 --- a/tests/moduletests/backup_kit_inner/b_session_restore_test.cpp +++ b/tests/moduletests/backup_kit_inner/b_session_restore_test.cpp @@ -55,6 +55,11 @@ static void OnBackupServiceDied() GTEST_LOG_(INFO) << "BSessionRestoreTest OnBackupServiceDied OK"; } +static void OnResultReport(const std::string result) +{ + GTEST_LOG_(INFO) << "BSessionRestoreAsyncTest OnResultReport OK"; +} + class BSessionRestoreTest : public testing::Test { public: static void SetUpTestCase(void) {}; @@ -88,6 +93,7 @@ void BSessionRestoreTest::Init() callbacks_.onBundleFinished = OnBundleFinished; callbacks_.onAllBundlesFinished = OnAllBundlesFinished; callbacks_.onBackupServiceDied = OnBackupServiceDied; + callbacks_.onResultReport = OnResultReport; } /** diff --git a/tests/unittests/backup_api/backup_impl/include/ext_extension_mock.h b/tests/unittests/backup_api/backup_impl/include/ext_extension_mock.h index b24f09103e477d879c262aa6472b98cb34bb9e92..963051d7872c6d14b85e2485c20358f32aba1a53 100644 --- a/tests/unittests/backup_api/backup_impl/include/ext_extension_mock.h +++ b/tests/unittests/backup_api/backup_impl/include/ext_extension_mock.h @@ -117,6 +117,11 @@ public: return {UniqueFd(-1), UniqueFd(-1)}; }; + ErrCode GetBackupInfo(std::string &result) override + { + return BError(BError::Codes::OK); + }; + private: int32_t nHandleBackupNum_ = 0; }; 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 9c4a84cc7097002b33989a29a75be29964d884eb..e3e79cab4cb40f1b4a948394814f02b4e0d5798e 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 @@ -111,6 +111,11 @@ public: return BError(BError::Codes::OK); } + ErrCode GetBackupInfo(BundleName &bundleName, std::string &result) override + { + return BError(BError::Codes::OK); + } + ErrCode Release() 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 bd08b8ce5c0c44e8af2b6b36624712010f1dea8f..a9a0539adeaf951b0e00fc5c5b00656f93370a6a 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 @@ -45,6 +45,7 @@ public: void RestoreOnBundleFinished(int32_t errCode, std::string bundleName) override {} void RestoreOnAllBundlesFinished(int32_t errCode) override {} void RestoreOnFileReady(std::string bundleName, std::string fileName, int fd) override {} + void RestoreOnResultReport(std::string result) override {} void IncrementalBackupOnFileReady(std::string bundleName, std::string fileName, int fd, int manifestFd) override {} void IncrementalBackupOnBundleStarted(int32_t errCode, std::string bundleName) override {} diff --git a/tests/unittests/backup_api/backup_impl/service_proxy_test.cpp b/tests/unittests/backup_api/backup_impl/service_proxy_test.cpp index 72f7167a731ed7fc1a7ea3a15cdcb1bfd7ba40d2..9ecb14e6604686087d90b3f32c8085588425cb1a 100644 --- a/tests/unittests/backup_api/backup_impl/service_proxy_test.cpp +++ b/tests/unittests/backup_api/backup_impl/service_proxy_test.cpp @@ -32,6 +32,7 @@ using namespace std; using namespace testing; namespace { +const string BUNDLE_NAME = "com.example.app2backup"; const string FILE_NAME = "1.tar"; constexpr int32_t SERVICE_ID = 5203; } // namespace @@ -686,4 +687,26 @@ HWTEST_F(ServiceProxyTest, SUB_Service_proxy_InvaildInstance_0100, testing::ext: ServiceProxy::InvaildInstance(); GTEST_LOG_(INFO) << "ServiceProxyTest-end SUB_Service_proxy_InvaildInstance_0100"; } + +/** + * @tc.number: SUB_Service_proxy_GetBackupInfo_0100 + * @tc.name: SUB_Service_proxy_GetBackupInfo_0100 + * @tc.desc: 测试 GetBackupInfo 获取应用信息接口调用成功和失败 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(ServiceProxyTest, SUB_Service_proxy_GetBackupInfo_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceProxyTest-begin SUB_Service_proxy_GetBackupInfo_0100"; + EXPECT_CALL(*mock_, SendRequest(_, _, _, _)) + .Times(1) + .WillOnce(Invoke(mock_.GetRefPtr(), &IServiceMock::InvokeSendRequest)); + std::string result; + std::string bundleName = "com.example.app2backup"; + int32_t ret = proxy_->GetBackupInfo(bundleName, result); + EXPECT_EQ(ret, BError(BError::Codes::OK)); + GTEST_LOG_(INFO) << "ServiceProxyTest-end SUB_Service_proxy_GetBackupInfo_0100"; +} } // namespace OHOS::FileManagement::Backup \ 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 4af64cdb4e664450b07f4101829bfef3ef362423..10cf595c8d500509d2a767c65f3211f3c20f79c3 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 @@ -48,6 +48,7 @@ public: MOCK_METHOD2(RestoreOnBundleFinished, void(int32_t errCode, string bundleName)); MOCK_METHOD1(RestoreOnAllBundlesFinished, void(int32_t errCode)); MOCK_METHOD3(RestoreOnFileReady, void(string bundleName, string fileName, int fd)); + 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_METHOD2(IncrementalBackupOnBundleFinished, void(int32_t errCode, string bundleName)); @@ -707,4 +708,42 @@ HWTEST_F(ServiceReverseStubTest, } GTEST_LOG_(INFO) << "ServiceReverseStubTest-end SUB_backup_ServiceReverseStub_IncrementalRestoreOnFileReady_0100"; } + +/** + * @tc.number: SUB_backup_ServiceReverseStub_RestoreOnResultReport_0100 + * @tc.name: SUB_backup_ServiceReverseStub_RestoreOnResultReport_0100 + * @tc.desc: Test function of RestoreOnResultReport interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I90ZZX + */ +HWTEST_F(ServiceReverseStubTest, + SUB_backup_ServiceReverseStub_RestoreOnResultReport_0100, + testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) + << "ServiceReverseStubTest-begin SUB_backup_ServiceReverseStub_RestoreOnResultReport_0100"; + try { + MockServiceReverse service; + EXPECT_CALL(service, RestoreOnResultReport(_)).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_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_RestoreOnResultReport_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 5c663ff2f861a54d40aa5002adac0b3954dc6c6b..e070407997b717922fe221327815835aaa633e06 100644 --- a/tests/unittests/backup_api/backup_impl/service_reverse_test.cpp +++ b/tests/unittests/backup_api/backup_impl/service_reverse_test.cpp @@ -77,6 +77,12 @@ static void OnAllBundlesFinishedTest(ErrCode err) static void OnBackupServiceDiedTest() {} +static void OnResultReportTest(std::string result) +{ + EXPECT_EQ(result, "result_report"); + GTEST_LOG_(INFO) << "ServiceReverseTest-OnResultReportTest SUCCESS"; +} + void ServiceReverseTest::TearDown() { service_ = nullptr; @@ -112,7 +118,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)}); + .onBackupServiceDied = bind(OnBackupServiceDiedTest), + .onResultReport = bind(OnResultReportTest, placeholders::_1)}); } } } @@ -1201,4 +1208,27 @@ HWTEST_F(ServiceReverseTest, } GTEST_LOG_(INFO) << "ServiceReverseTest-end SUB_backup_ServiceReverse_IncrementalRestoreOnAllBundlesFinished_0101"; } + +/** + * @tc.number: SUB_backup_ServiceReverse_RestoreOnResultReport_0100 + * @tc.name: SUB_backup_ServiceReverse_RestoreOnResultReport_0100 + * @tc.desc: 测试 RestoreOnResultReport 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(ServiceReverseTest, SUB_backup_ServiceReverse_RestoreOnResultReport_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceReverseTest-begin SUB_backup_ServiceReverse_RestoreOnResultReport_0100"; + try { + Init(IServiceReverse::Scenario::BACKUP); + std::string resultReport = "result_report"; + service_->RestoreOnResultReport(resultReport); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceReverseTest-an exception occurred by RestoreOnResultReport."; + } + GTEST_LOG_(INFO) << "ServiceReverseTest-end SUB_backup_ServiceReverse_RestoreOnResultReport_0100"; +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file 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 4b23b74fbb3fb109da506b21fff2ca1f07b3d26b..e41f1c44562ed2e371ebc0f172d7285c198bd254 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 @@ -35,6 +35,7 @@ namespace { const string BUNDLE_NAME = "com.example.app2backup"; const string FILE_NAME = "1.tar"; const string FILE_NAME_MANIFEST = "manifest.rp"; +const string RESULT_REPORT = "result_report"; } // namespace class ServiceReverseProxyTest : public testing::Test { @@ -488,4 +489,28 @@ HWTEST_F(ServiceReverseProxyTest, } GTEST_LOG_(INFO) << "ServiceReverseProxyTest-end SUB_ServiceReverse_proxy_IncrementalRestoreOnFileReady_0100"; } + +/** + * @tc.number: SUB_ServiceReverse_proxy_RestoreOnResultReport_0100 + * @tc.name: SUB_ServiceReverse_proxy_RestoreOnResultReport_0100 + * @tc.desc: Test function of RestoreOnResultReport interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(ServiceReverseProxyTest, SUB_ServiceReverse_proxy_RestoreOnResultReport_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceReverseProxyTest-begin SUB_ServiceReverse_proxy_RestoreOnResultReport_0100"; + try { + EXPECT_CALL(*mock_, SendRequest(_, _, _, _)) + .Times(1) + .WillOnce(Invoke(mock_.GetRefPtr(), &ServiceReverseMock::InvokeSendRequest)); + proxy_->RestoreOnResultReport(RESULT_REPORT); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceReverseProxyTest-an exception occurred by RestoreOnResultReport."; + } + GTEST_LOG_(INFO) << "ServiceReverseProxyTest-end SUB_ServiceReverse_proxy_RestoreOnResultReport_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 00723373e1d2af74985991d78237e7ace1c5399a..6b630e27d0c735ac0a30a828ce965fcd50f6345a 100644 --- a/tests/unittests/backup_sa/module_ipc/service_stub_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_stub_test.cpp @@ -62,6 +62,7 @@ public: MOCK_METHOD3(AppIncrementalFileReady, ErrCode(const std::string &fileName, UniqueFd fd, UniqueFd manifestFd)); MOCK_METHOD1(AppIncrementalDone, ErrCode(ErrCode errCode)); MOCK_METHOD2(GetIncrementalFileHandle, ErrCode(const std::string &bundleName, const std::string &fileName)); + MOCK_METHOD2(GetBackupInfo, ErrCode(string &bundleName, string &result)); UniqueFd InvokeGetLocalCapabilities() { if (bCapabilities_) { @@ -505,4 +506,36 @@ HWTEST_F(ServiceStubTest, SUB_backup_sa_ServiceStub_Release_0100, testing::ext:: } GTEST_LOG_(INFO) << "ServiceStubTest-end SUB_backup_sa_ServiceStub_Release_0100"; } + +/** + * @tc.number: SUB_backup_sa_ServiceStub_GetBackupInfo_0100 + * @tc.name: SUB_backup_sa_ServiceStub_GetBackupInfo_0100 + * @tc.desc: Test function of GetBackupInfo interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 0 + * @tc.require: I6URNZ + */ +HWTEST_F(ServiceStubTest, SUB_backup_sa_ServiceStub_GetBackupInfo_0100, testing::ext::TestSize.Level0) +{ + GTEST_LOG_(INFO) << "ServiceStubTest-begin SUB_backup_sa_ServiceStub_GetBackupInfo_0100"; + try { + MockService service; + EXPECT_CALL(service, GetBackupInfo(_, _)).WillOnce(Return(BError(BError::Codes::OK))); + MessageParcel data; + MessageParcel reply; + MessageOption option; + std::string bundleName = "com.example.app2backup"; + EXPECT_TRUE(data.WriteInterfaceToken(IService::GetDescriptor())); + EXPECT_TRUE(data.WriteString(bundleName)); + EXPECT_EQ(BError(BError::Codes::OK), + service.OnRemoteRequest( + static_cast(IServiceInterfaceCode::SERVICE_CMD_GET_BACKUP_INFO), data, + reply, option)); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceStubTest-an exception occurred by GetBackupInfo."; + } + GTEST_LOG_(INFO) << "ServiceStubTest-end SUB_backup_sa_ServiceStub_GetBackupInfo_0100"; +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/tests/unittests/backup_sa/module_ipc/service_test.cpp b/tests/unittests/backup_sa/module_ipc/service_test.cpp index 7189421ef030a7f09252cd9b2893cd3851680701..39be801bae44cc1d270e92739c29b0b911b7fd16 100644 --- a/tests/unittests/backup_sa/module_ipc/service_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_test.cpp @@ -852,4 +852,27 @@ HWTEST_F(ServiceTest, SUB_Service_SessionDeactive_0100, testing::ext::TestSize.L } GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_SessionDeactive_0100"; } + +/** + * @tc.number: SUB_Service_GetBackupInfo_0100 + * @tc.name: SUB_Service_GetBackupInfo_0100 + * @tc.desc: 测试 SessionDeactive 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I8ZIMJ + */ +HWTEST_F(ServiceTest, SUB_Service_GetBackupInfo_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_GetBackupInfo_0100"; + try { + std::string bundleName = "com.example.app2backup"; + std::string backupInfo = "backup info"; + servicePtr_->GetBackupInfo(bundleName, backupInfo); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by GetBackupInfo."; + } + GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_GetBackupInfo_0100"; +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/tests/unittests/backup_sa/module_ipc/svc_extension_proxy_test.cpp b/tests/unittests/backup_sa/module_ipc/svc_extension_proxy_test.cpp index 708699d79bee9f9476142a686fb65bda6b7b313b..ebf0fdd7cf61b3c8244d00393dd89be4bdbcee55 100644 --- a/tests/unittests/backup_sa/module_ipc/svc_extension_proxy_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/svc_extension_proxy_test.cpp @@ -144,4 +144,29 @@ HWTEST_F(SvcExtensionProxyTest, SUB_Ext_Extension_proxy_PublishFile_0100, testin EXPECT_NE(BError(BError::Codes::OK), ret); GTEST_LOG_(INFO) << "SvcExtensionProxyTest-end SUB_Ext_Extension_proxy_PublishFile_0100"; } + +/** + * @tc.number: SUB_Ext_Extension_proxy_GetBackupInfo_0100 + * @tc.name: SUB_Ext_Extension_proxy_GetBackupInfo_0100 + * @tc.desc: 测试 GetBackupInfo 接口调用成功和失败 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(SvcExtensionProxyTest, SUB_Ext_Extension_proxy_GetBackupInfo_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "SvcExtensionProxyTest-begin SUB_Ext_Extension_proxy_GetBackupInfo_0100"; + EXPECT_CALL(*mock_, SendRequest(_, _, _, _)) + .Times(2) + .WillOnce(Invoke(mock_.GetRefPtr(), &BackupExtExtensionMock::InvokeSendRequest)) + .WillOnce(Return(EPERM)); + string result = "result_report"; + ErrCode ret = proxy_->GetBackupInfo(result); + EXPECT_EQ(BError(BError::Codes::OK), ret); + + ret = proxy_->GetBackupInfo(result); + EXPECT_NE(BError(BError::Codes::OK), ret); + GTEST_LOG_(INFO) << "SvcExtensionProxyTest-end SUB_Ext_Extension_proxy_GetBackupInfo_0100"; +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file