From d3938100433473153320bca003254bd9a87fc54b Mon Sep 17 00:00:00 2001 From: Clone_Zhang Date: Fri, 16 Aug 2024 20:05:57 +0800 Subject: [PATCH] fix:timeout strategy Signed-off-by: Clone_Zhang --- .../native/backup_ext/include/ext_extension.h | 30 +- .../native/backup_ext/src/ext_extension.cpp | 285 ++++-------------- .../backup_ext/src/sub_ext_extension.cpp | 236 +++++++++++++++ .../backup_kit_inner/src/service_proxy.cpp | 44 +++ .../native/backup_kit_inner/impl/i_service.h | 2 + .../impl/i_service_ipc_interface_code.h | 2 + .../backup_kit_inner/impl/service_proxy.h | 2 + .../kits/js/backup/prop_n_operation.cpp | 4 +- .../backup_sa/include/module_ipc/service.h | 10 + .../include/module_ipc/service_stub.h | 2 + .../include/module_ipc/svc_session_manager.h | 54 +++- services/backup_sa/src/module_ipc/service.cpp | 148 ++++++--- .../src/module_ipc/service_incremental.cpp | 6 +- .../backup_sa/src/module_ipc/service_stub.cpp | 36 +++ .../src/module_ipc/svc_session_manager.cpp | 135 +++++++-- .../backup_kit_inner/service_proxy_mock.cpp | 9 + tests/mock/module_ipc/service_mock.cpp | 10 + .../module_ipc/svc_session_manager_mock.cpp | 20 +- .../svc_session_manager_throw_mock.cpp | 24 +- .../svc_session_manager_throw_mock.h | 12 +- .../backup_impl/include/i_service_mock.h | 10 + .../module_ipc/service_stub_test.cpp | 2 + .../module_ipc/service_throw_test.cpp | 71 +---- .../module_ipc/svc_session_manager_test.cpp | 120 ++++---- .../backup_sa/session/service_proxy_mock.cpp | 10 + utils/BUILD.gn | 1 + utils/include/b_resources/b_constants.h | 1 + utils/include/b_sa/b_sa_utils.h | 2 +- utils/include/b_utils/b_time.h | 28 ++ utils/src/b_utils/b_time.cpp | 40 +++ 30 files changed, 889 insertions(+), 467 deletions(-) create mode 100644 utils/include/b_utils/b_time.h create mode 100644 utils/src/b_utils/b_time.cpp diff --git a/frameworks/native/backup_ext/include/ext_extension.h b/frameworks/native/backup_ext/include/ext_extension.h index f74b25bb0..1c8ed889a 100644 --- a/frameworks/native/backup_ext/include/ext_extension.h +++ b/frameworks/native/backup_ext/include/ext_extension.h @@ -170,6 +170,20 @@ private: */ void AppIncrementalDone(ErrCode errCode); + /** + * @brief start extension timer by ipc + * + * @param result + */ + void StartExtTimer(bool &isExtStart); + + /** + * @brief start fwk timer by ipc + * + * @param errCode + */ + void StartFwkTimer(bool &isFwkStart); + /** * @brief get callbackEx for execute onRestore * @@ -178,12 +192,26 @@ private: std::function RestoreResultCallbackEx(wptr obj); /** - * @brief get callbackEx for execute onRestore with string param + * @brief get increCallbackEx for execute onRestore with string param * * @param errCode */ std::function IncRestoreResultCallbackEx(wptr obj); + /** + * @brief get increCallback for execute onRestore with string param + * + * @param errCode + */ + std::function IncRestoreResultCallback(wptr obj); + + /** + * @brief get callback for execute onRestore with string param + * + * @param errCode + */ + std::function RestoreResultCallback(wptr obj); + /** * @brief get callbackEx for execute onRestore * diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index 3ab7acc64..a86396d42 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -785,6 +785,14 @@ void BackupExtExtension::AsyncTaskBackup(const string config) auto ptr = obj.promote(); BExcepUltils::BAssert(ptr, BError::Codes::EXT_BROKEN_FRAMEWORK, "Ext extension handle have been released"); try { + HILOGI("Do backup, start fwk timer begin."); + bool isFwkStart; + ptr->StartFwkTimer(isFwkStart); + if (!isFwkStart) { + HILOGE("Do backup, start fwk timer fail."); + return; + } + HILOGI("Do backup, start fwk timer end."); BJsonCachedEntity cachedEntity(config); auto cache = cachedEntity.Structuralize(); auto ret = ptr->DoBackup(cache); @@ -1260,26 +1268,19 @@ void BackupExtExtension::AsyncTaskRestoreForUpgrade() { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); auto task = [obj {wptr(this)}]() { - auto callBackup = [obj](ErrCode errCode, std::string errMsg) { - auto extensionPtr = obj.promote(); - if (extensionPtr == nullptr) { - HILOGE("Ext extension handle have been released"); - return; - } - HILOGI("Current bundle will execute app done"); - if (errMsg.empty()) { - extensionPtr->AppDone(errCode); - } else { - std::string errInfo; - BJsonUtil::BuildRestoreErrInfo(errInfo, errCode, errMsg); - extensionPtr->AppResultReport(errInfo, BackupRestoreScenario::FULL_RESTORE, errCode); - } - extensionPtr->DoClear(); - }; auto ptr = obj.promote(); BExcepUltils::BAssert(ptr, BError::Codes::EXT_BROKEN_FRAMEWORK, "Ext extension handle have been released"); BExcepUltils::BAssert(ptr->extension_, BError::Codes::EXT_INVAL_ARG, "Extension handle have been released"); try { + HILOGI("On restore, start ext timer begin."); + bool isExtStart; + ptr->StartExtTimer(isExtStart); + if (!isExtStart) { + HILOGE("On restore, start ext timer fail."); + return; + } + HILOGI("On restore, start ext timer end."); + auto callBackup = ptr->RestoreResultCallback(obj); auto callBackupEx = ptr->RestoreResultCallbackEx(obj); ErrCode err = ptr->extension_->OnRestore(callBackup, callBackupEx); if (err != ERR_OK) { @@ -1317,26 +1318,19 @@ void BackupExtExtension::ExtClear() void BackupExtExtension::AsyncTaskIncrementalRestoreForUpgrade() { auto task = [obj {wptr(this)}]() { - auto callBackup = [obj](ErrCode errCode, std::string errMsg) { - auto extensionPtr = obj.promote(); - if (extensionPtr == nullptr) { - HILOGE("Ext extension handle have been released"); - return; - } - HILOGI("Current bundle will execute app done"); - if (errMsg.empty()) { - extensionPtr->AppIncrementalDone(errCode); - } else { - std::string errInfo; - BJsonUtil::BuildRestoreErrInfo(errInfo, errCode, errMsg); - extensionPtr->AppResultReport(errInfo, BackupRestoreScenario::INCREMENTAL_RESTORE, errCode); - } - extensionPtr->DoClear(); - }; auto ptr = obj.promote(); BExcepUltils::BAssert(ptr, BError::Codes::EXT_BROKEN_FRAMEWORK, "Ext extension handle have been released"); BExcepUltils::BAssert(ptr->extension_, BError::Codes::EXT_INVAL_ARG, "Extension handle have been released"); try { + HILOGI("On incrementalRestore, start ext timer begin."); + bool isExtStart; + ptr->StartExtTimer(isExtStart); + if (!isExtStart) { + HILOGE("On incrementalRestore, start ext timer fail."); + return; + } + HILOGI("On incrementalRestore, start ext timer end."); + auto callBackup = ptr->IncRestoreResultCallback(obj); auto callBackupEx = ptr->IncRestoreResultCallbackEx(obj); ErrCode err = ptr->extension_->OnRestore(callBackup, callBackupEx); if (err != ERR_OK) { @@ -1427,6 +1421,28 @@ void BackupExtExtension::AppResultReport(const std::string restoreRetInfo, } } +void BackupExtExtension::StartExtTimer(bool &isExtStart) +{ + auto proxy = ServiceProxy::GetInstance(); + BExcepUltils::BAssert(proxy, BError::Codes::EXT_BROKEN_IPC, "Failed to obtain the ServiceProxy handle"); + HILOGI("Start ext timer by ipc."); + auto ret = proxy->StartExtTimer(isExtStart); + if (ret != ERR_OK) { + HILOGE("Start ext timer failed, errCode: %{public}d", ret); + } +} + +void BackupExtExtension::StartFwkTimer(bool &isFwkStart) +{ + auto proxy = ServiceProxy::GetInstance(); + BExcepUltils::BAssert(proxy, BError::Codes::EXT_BROKEN_IPC, "Failed to obtain the ServiceProxy handle"); + HILOGI("Start fwk timer by ipc."); + auto ret = proxy->StartFwkTimer(isFwkStart); + if (ret != ERR_OK) { + HILOGE("Start fwk timer failed, errCode: %{public}d", ret); + } +} + void BackupExtExtension::AsyncTaskOnBackup() { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); @@ -1773,7 +1789,14 @@ ErrCode BackupExtExtension::IncrementalBigFileReady(const TarMap &pkgInfo, void BackupExtExtension::AsyncTaskDoIncrementalBackup(UniqueFd incrementalFd, UniqueFd manifestFd) { - HILOGI("Start AsyncTaskDoIncrementalBackup"); + HILOGI("Do IncrementalBackup, start fwk timer begin."); + bool isFwkStart; + StartFwkTimer(isFwkStart); + if (!isFwkStart) { + HILOGE("Do IncrementalBackup, start fwk timer fail."); + return; + } + HILOGI("Do IncrementalBackup, start fwk timer end."); int incrementalFdDup = dup(incrementalFd); int manifestFdDup = dup(manifestFd); if (incrementalFdDup < 0) { @@ -1989,200 +2012,4 @@ int BackupExtExtension::DoIncrementalBackup(const vector smallFiles.size(), allFiles.size()); return err; } - -void BackupExtExtension::AppIncrementalDone(ErrCode errCode) -{ - HILOGI("Begin"); - auto proxy = ServiceProxy::GetInstance(); - if (proxy == nullptr) { - HILOGE("Failed to obtain the ServiceProxy handle"); - DoClear(); - return; - } - auto ret = proxy->AppIncrementalDone(errCode); - if (ret != ERR_OK) { - 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(); - if (ptr == nullptr) { - HILOGE("Failed to get ext extension."); - return BError(BError::Codes::EXT_INVAL_ARG, "extension getBackupInfo exception").GetCode(); - } - if (ptr->extension_ == nullptr) { - HILOGE("Failed to get extension."); - return BError(BError::Codes::EXT_INVAL_ARG, "extension getBackupInfo exception").GetCode(); - } - auto callBackup = [ptr](ErrCode errCode, const std::string result) { - if (ptr == nullptr) { - HILOGE("Failed to get ext extension."); - return; - } - HILOGI("GetBackupInfo callBackup start. result = %{public}s", result.c_str()); - ptr->backupInfo_ = result; - }; - auto ret = ptr->extension_->GetBackupInfo(callBackup); - if (ret != ERR_OK) { - HILOGE("Failed to get backupInfo. err = %{public}d", ret); - return BError(BError::Codes::EXT_INVAL_ARG, "extension getBackupInfo exception").GetCode(); - } - HILOGD("backupInfo = %s", backupInfo_.c_str()); - result = backupInfo_; - backupInfo_.clear(); - - return ERR_OK; -} - -ErrCode BackupExtExtension::UpdateFdSendRate(std::string &bundleName, int32_t sendRate) -{ - try { - std::lock_guard lock(updateSendRateLock_); - HILOGI("Update SendRate, bundleName:%{public}s, sendRate:%{public}d", bundleName.c_str(), sendRate); - VerifyCaller(); - bundleName_ = bundleName; - sendRate_ = sendRate; - if (sendRate > 0) { - startSendFdRateCon_.notify_one(); - } - return ERR_OK; - } catch (...) { - HILOGE("Failed to UpdateFdSendRate"); - return BError(BError::Codes::EXT_BROKEN_IPC).GetCode(); - } -} - -std::function BackupExtExtension::RestoreResultCallbackEx(wptr obj) -{ - HILOGI("Begin get callbackEx"); - return [obj](ErrCode errCode, const std::string restoreRetInfo) { - auto extensionPtr = obj.promote(); - if (extensionPtr == nullptr) { - HILOGE("Ext extension handle have been released"); - return; - } - if (extensionPtr->extension_ == nullptr) { - HILOGE("Extension handle have been released"); - return; - } - extensionPtr->extension_->InvokeAppExtMethod(errCode, restoreRetInfo); - if (errCode == ERR_OK) { - if (restoreRetInfo.size()) { - HILOGI("Will notify restore result report"); - extensionPtr->AppResultReport(restoreRetInfo, BackupRestoreScenario::FULL_RESTORE); - } - return; - } - if (restoreRetInfo.empty()) { - extensionPtr->AppDone(errCode); - extensionPtr->DoClear(); - } else { - std::string errInfo; - BJsonUtil::BuildRestoreErrInfo(errInfo, errCode, restoreRetInfo); - extensionPtr->AppResultReport(errInfo, BackupRestoreScenario::FULL_RESTORE, errCode); - extensionPtr->DoClear(); - } - }; -} - -std::function BackupExtExtension::AppDoneCallbackEx(wptr obj) -{ - HILOGI("Begin get callback for appDone"); - return [obj](ErrCode errCode, std::string errMsg) { - HILOGI("begin call callBackupExAppDone"); - auto extensionPtr = obj.promote(); - if (extensionPtr == nullptr) { - HILOGE("Ext extension handle have been released"); - return; - } - extensionPtr->AppDone(errCode); - extensionPtr->DoClear(); - }; -} - -std::function BackupExtExtension::IncRestoreResultCallbackEx(wptr obj) -{ - HILOGI("Begin get callback for onRestore"); - return [obj](ErrCode errCode, const std::string restoreRetInfo) { - HILOGI("begin call restoreEx"); - auto extensionPtr = obj.promote(); - if (extensionPtr == nullptr) { - HILOGE("Ext extension handle have been released"); - return; - } - if (extensionPtr->extension_ == nullptr) { - HILOGE("Extension handle have been released"); - return; - } - extensionPtr->extension_->InvokeAppExtMethod(errCode, restoreRetInfo); - if (errCode == ERR_OK) { - if (restoreRetInfo.size()) { - extensionPtr->AppResultReport(restoreRetInfo, BackupRestoreScenario::INCREMENTAL_RESTORE); - } - return; - } - if (restoreRetInfo.empty()) { - extensionPtr->AppIncrementalDone(errCode); - extensionPtr->DoClear(); - } else { - std::string errInfo; - BJsonUtil::BuildRestoreErrInfo(errInfo, errCode, restoreRetInfo); - extensionPtr->AppResultReport(errInfo, BackupRestoreScenario::INCREMENTAL_RESTORE, errCode); - extensionPtr->DoClear(); - } - }; -} - -std::function BackupExtExtension::HandleBackupEx(wptr obj) -{ - HILOGI("Begin get HandleBackupEx"); - return [obj](ErrCode errCode, const std::string backupExRetInfo) { - auto proxy = ServiceProxy::GetInstance(); - if (proxy == nullptr) { - throw BError(BError::Codes::EXT_BROKEN_BACKUP_SA, std::generic_category().message(errno)); - } - auto extensionPtr = obj.promote(); - if (extensionPtr == nullptr) { - HILOGE("Ext extension handle have been released"); - return; - } - if (extensionPtr->extension_ == nullptr) { - HILOGE("Extension handle have been released"); - return; - } - extensionPtr->extension_->InvokeAppExtMethod(errCode, backupExRetInfo); - if (backupExRetInfo.size()) { - HILOGI("Start GetAppLocalListAndDoIncrementalBackup"); - proxy->GetAppLocalListAndDoIncrementalBackup(); - HILOGI("Will notify backup result report"); - extensionPtr->AppResultReport(backupExRetInfo, BackupRestoreScenario::INCREMENTAL_BACKUP); - } - }; -} - -std::function BackupExtExtension::HandleTaskBackupEx(wptr obj) -{ - HILOGI("Begin get HandleTaskBackupEx"); - return [obj](ErrCode errCode, const std::string backupExRetInfo) { - HILOGI("begin call backup"); - auto extensionPtr = obj.promote(); - if (extensionPtr == nullptr) { - HILOGE("Ext extension handle have been released"); - return; - } - if (extensionPtr->extension_ == nullptr) { - HILOGE("Extension handle have been released"); - return; - } - extensionPtr->extension_->InvokeAppExtMethod(errCode, backupExRetInfo); - if (backupExRetInfo.size()) { - extensionPtr->AsyncTaskBackup(extensionPtr->extension_->GetUsrConfig()); - HILOGI("Will notify backup result report"); - extensionPtr->AppResultReport(backupExRetInfo, BackupRestoreScenario::FULL_BACKUP); - } - }; -} } // namespace OHOS::FileManagement::Backup diff --git a/frameworks/native/backup_ext/src/sub_ext_extension.cpp b/frameworks/native/backup_ext/src/sub_ext_extension.cpp index 888d44c6e..29786a9dd 100644 --- a/frameworks/native/backup_ext/src/sub_ext_extension.cpp +++ b/frameworks/native/backup_ext/src/sub_ext_extension.cpp @@ -172,4 +172,240 @@ tuple> BackupExtExtension::CheckRestoreFileInfos() } return {true, errFiles}; } + +void BackupExtExtension::AppIncrementalDone(ErrCode errCode) +{ + HILOGI("Begin"); + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + HILOGE("Failed to obtain the ServiceProxy handle"); + DoClear(); + return; + } + auto ret = proxy->AppIncrementalDone(errCode); + if (ret != ERR_OK) { + 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(); + if (ptr == nullptr) { + HILOGE("Failed to get ext extension."); + return BError(BError::Codes::EXT_INVAL_ARG, "extension getBackupInfo exception").GetCode(); + } + if (ptr->extension_ == nullptr) { + HILOGE("Failed to get extension."); + return BError(BError::Codes::EXT_INVAL_ARG, "extension getBackupInfo exception").GetCode(); + } + auto callBackup = [ptr](ErrCode errCode, const std::string result) { + if (ptr == nullptr) { + HILOGE("Failed to get ext extension."); + return; + } + HILOGI("GetBackupInfo callBackup start. result = %{public}s", result.c_str()); + ptr->backupInfo_ = result; + }; + auto ret = ptr->extension_->GetBackupInfo(callBackup); + if (ret != ERR_OK) { + HILOGE("Failed to get backupInfo. err = %{public}d", ret); + return BError(BError::Codes::EXT_INVAL_ARG, "extension getBackupInfo exception").GetCode(); + } + HILOGD("backupInfo = %s", backupInfo_.c_str()); + result = backupInfo_; + backupInfo_.clear(); + + return ERR_OK; +} + +ErrCode BackupExtExtension::UpdateFdSendRate(std::string &bundleName, int32_t sendRate) +{ + try { + std::lock_guard lock(updateSendRateLock_); + HILOGI("Update SendRate, bundleName:%{public}s, sendRate:%{public}d", bundleName.c_str(), sendRate); + VerifyCaller(); + bundleName_ = bundleName; + sendRate_ = sendRate; + if (sendRate > 0) { + startSendFdRateCon_.notify_one(); + } + return ERR_OK; + } catch (...) { + HILOGE("Failed to UpdateFdSendRate"); + return BError(BError::Codes::EXT_BROKEN_IPC).GetCode(); + } +} + +std::function BackupExtExtension::RestoreResultCallbackEx(wptr obj) +{ + HILOGI("Begin get callbackEx"); + return [obj](ErrCode errCode, const std::string restoreRetInfo) { + auto extensionPtr = obj.promote(); + if (extensionPtr == nullptr) { + HILOGE("Ext extension handle have been released"); + return; + } + if (extensionPtr->extension_ == nullptr) { + HILOGE("Extension handle have been released"); + return; + } + extensionPtr->extension_->InvokeAppExtMethod(errCode, restoreRetInfo); + if (errCode == ERR_OK) { + if (restoreRetInfo.size()) { + HILOGI("Will notify restore result report"); + extensionPtr->AppResultReport(restoreRetInfo, BackupRestoreScenario::FULL_RESTORE); + } + return; + } + if (restoreRetInfo.empty()) { + extensionPtr->AppDone(errCode); + extensionPtr->DoClear(); + } else { + std::string errInfo; + BJsonUtil::BuildRestoreErrInfo(errInfo, errCode, restoreRetInfo); + extensionPtr->AppResultReport(errInfo, BackupRestoreScenario::FULL_RESTORE, errCode); + extensionPtr->DoClear(); + } + }; +} + +std::function BackupExtExtension::AppDoneCallbackEx(wptr obj) +{ + HILOGI("Begin get callback for appDone"); + return [obj](ErrCode errCode, std::string errMsg) { + HILOGI("begin call callBackupExAppDone"); + auto extensionPtr = obj.promote(); + if (extensionPtr == nullptr) { + HILOGE("Ext extension handle have been released"); + return; + } + extensionPtr->AppDone(errCode); + extensionPtr->DoClear(); + }; +} + +std::function BackupExtExtension::IncRestoreResultCallbackEx(wptr obj) +{ + HILOGI("Begin get callback for onRestore"); + return [obj](ErrCode errCode, const std::string restoreRetInfo) { + HILOGI("begin call restoreEx"); + auto extensionPtr = obj.promote(); + if (extensionPtr == nullptr) { + HILOGE("Ext extension handle have been released"); + return; + } + if (extensionPtr->extension_ == nullptr) { + HILOGE("Extension handle have been released"); + return; + } + extensionPtr->extension_->InvokeAppExtMethod(errCode, restoreRetInfo); + if (errCode == ERR_OK) { + if (restoreRetInfo.size()) { + extensionPtr->AppResultReport(restoreRetInfo, BackupRestoreScenario::INCREMENTAL_RESTORE); + } + return; + } + if (restoreRetInfo.empty()) { + extensionPtr->AppIncrementalDone(errCode); + extensionPtr->DoClear(); + } else { + std::string errInfo; + BJsonUtil::BuildRestoreErrInfo(errInfo, errCode, restoreRetInfo); + extensionPtr->AppResultReport(errInfo, BackupRestoreScenario::INCREMENTAL_RESTORE, errCode); + extensionPtr->DoClear(); + } + }; +} + +std::function BackupExtExtension::IncRestoreResultCallback(wptr obj) +{ + return [obj](ErrCode errCode, std::string errMsg) { + auto extensionPtr = obj.promote(); + if (extensionPtr == nullptr) { + HILOGE("Ext extension handle have been released"); + return; + } + HILOGI("Current bundle will execute app done"); + if (errMsg.empty()) { + extensionPtr->AppIncrementalDone(errCode); + } else { + std::string errInfo; + BJsonUtil::BuildRestoreErrInfo(errInfo, errCode, errMsg); + extensionPtr->AppResultReport(errInfo, BackupRestoreScenario::INCREMENTAL_RESTORE, errCode); + } + extensionPtr->DoClear(); + }; +} + +std::function BackupExtExtension::RestoreResultCallback(wptr obj) +{ + return [obj](ErrCode errCode, std::string errMsg) { + auto extensionPtr = obj.promote(); + if (extensionPtr == nullptr) { + HILOGE("Ext extension handle have been released"); + return; + } + HILOGI("Current bundle will execute app done"); + if (errMsg.empty()) { + extensionPtr->AppDone(errCode); + } else { + std::string errInfo; + BJsonUtil::BuildRestoreErrInfo(errInfo, errCode, errMsg); + extensionPtr->AppResultReport(errInfo, BackupRestoreScenario::FULL_RESTORE, errCode); + } + extensionPtr->DoClear(); + }; +} + +std::function BackupExtExtension::HandleBackupEx(wptr obj) +{ + HILOGI("Begin get HandleBackupEx"); + return [obj](ErrCode errCode, const std::string backupExRetInfo) { + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + throw BError(BError::Codes::EXT_BROKEN_BACKUP_SA, std::generic_category().message(errno)); + } + auto extensionPtr = obj.promote(); + if (extensionPtr == nullptr) { + HILOGE("Ext extension handle have been released"); + return; + } + if (extensionPtr->extension_ == nullptr) { + HILOGE("Extension handle have been released"); + return; + } + extensionPtr->extension_->InvokeAppExtMethod(errCode, backupExRetInfo); + if (backupExRetInfo.size()) { + HILOGI("Start GetAppLocalListAndDoIncrementalBackup"); + proxy->GetAppLocalListAndDoIncrementalBackup(); + HILOGI("Will notify backup result report"); + extensionPtr->AppResultReport(backupExRetInfo, BackupRestoreScenario::INCREMENTAL_BACKUP); + } + }; +} + +std::function BackupExtExtension::HandleTaskBackupEx(wptr obj) +{ + HILOGI("Begin get HandleTaskBackupEx"); + return [obj](ErrCode errCode, const std::string backupExRetInfo) { + HILOGI("begin call backup"); + auto extensionPtr = obj.promote(); + if (extensionPtr == nullptr) { + HILOGE("Ext extension handle have been released"); + return; + } + if (extensionPtr->extension_ == nullptr) { + HILOGE("Extension handle have been released"); + return; + } + extensionPtr->extension_->InvokeAppExtMethod(errCode, backupExRetInfo); + if (backupExRetInfo.size()) { + extensionPtr->AsyncTaskBackup(extensionPtr->extension_->GetUsrConfig()); + HILOGI("Will notify backup result report"); + extensionPtr->AppResultReport(backupExRetInfo, BackupRestoreScenario::FULL_BACKUP); + } + }; +} } // namespace OHOS::FileManagement::Backup diff --git a/frameworks/native/backup_kit_inner/src/service_proxy.cpp b/frameworks/native/backup_kit_inner/src/service_proxy.cpp index 1d004e4f1..0187729c3 100644 --- a/frameworks/native/backup_kit_inner/src/service_proxy.cpp +++ b/frameworks/native/backup_kit_inner/src/service_proxy.cpp @@ -585,4 +585,48 @@ ErrCode ServiceProxy::UpdateSendRate(std::string &bundleName, int32_t sendRate, HILOGI("ServiceProxy UpdateSendRate end. ret = %{public}d", ret); return BError(BError::Codes::OK, "success"); } + +ErrCode ServiceProxy::StartExtTimer(bool &isExtStart) +{ + HILOGI("ServiceProxy StartExtTimer 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(); + } + MessageParcel reply; + MessageOption option; + option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME); + int32_t ret = Remote()->SendRequest(static_cast(IServiceInterfaceCode::SERVICE_CMD_START_EXT_TIMER), + 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.ReadBool(isExtStart); + HILOGI("ServiceProxy StartExtTimer end. isExtStart = %d", isExtStart); + return BError(BError::Codes::OK, "success"); +} + +ErrCode ServiceProxy::StartFwkTimer(bool &isFwkStart) +{ + HILOGI("ServiceProxy StartFwkTimer 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(); + } + MessageParcel reply; + MessageOption option; + option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME); + int32_t ret = Remote()->SendRequest(static_cast(IServiceInterfaceCode::SERVICE_CMD_START_FWK_TIMER), + 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.ReadBool(isFwkStart); + HILOGI("ServiceProxy StartFwkTimer end. isFwkStart = %d", isFwkStart); + return BError(BError::Codes::OK, "success"); +} } // 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 49348fb58..6e7b7d4c7 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 @@ -85,6 +85,8 @@ public: virtual ErrCode GetBackupInfo(BundleName &bundleName, std::string &result) = 0; virtual ErrCode UpdateTimer(BundleName &bundleName, uint32_t timeOut, bool &result) = 0; virtual ErrCode UpdateSendRate(std::string &bundleName, int32_t sendRate, bool &result) = 0; + virtual ErrCode StartExtTimer(bool &isExtStart) = 0; + virtual ErrCode StartFwkTimer(bool &isFwkStart) = 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 1de4b9c45..f8dfcc679 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 @@ -45,6 +45,8 @@ enum class IServiceInterfaceCode { SERVICE_CMD_GET_INCREMENTAL_FILE_NAME, SERVICE_CMD_GET_BACKUP_INFO, SERVICE_CMD_UPDATE_TIMER, + SERVICE_CMD_START_EXT_TIMER, + SERVICE_CMD_START_FWK_TIMER, SERVICE_CMD_UPDATE_SENDRATE, SERVICE_CMD_GET_APP_LOCAL_LIST_AND_DO_INCREMENTAL_BACKUP, }; diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/service_proxy.h b/interfaces/inner_api/native/backup_kit_inner/impl/service_proxy.h index d0845087e..9eab1671e 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 @@ -68,6 +68,8 @@ public: ErrCode GetBackupInfo(BundleName &bundleName, std::string &result) override; ErrCode UpdateTimer(BundleName &bundleName, uint32_t timeOut, bool &result) override; ErrCode UpdateSendRate(std::string &bundleName, int32_t sendRate, bool &result) override; + ErrCode StartExtTimer(bool &isExtStart) override; + ErrCode StartFwkTimer(bool &isFwkStart) override; public: explicit ServiceProxy(const sptr &impl) : IRemoteProxy(impl) {} diff --git a/interfaces/kits/js/backup/prop_n_operation.cpp b/interfaces/kits/js/backup/prop_n_operation.cpp index 593c67a59..6e27a0700 100644 --- a/interfaces/kits/js/backup/prop_n_operation.cpp +++ b/interfaces/kits/js/backup/prop_n_operation.cpp @@ -31,8 +31,6 @@ namespace OHOS::FileManagement::Backup { using namespace std; using namespace LibN; -const int32_t H_TO_MS = 3600 * 1000; - static napi_value AsyncCallback(napi_env env, const NFuncArg& funcArg) { HILOGD("called LocalCapabilities::AsyncCallback begin"); @@ -311,7 +309,7 @@ napi_value PropNOperation::DoUpdateTimer(napi_env env, napi_callback_info info) } NVal jsBundleInt(env, funcArg[NARG_POS::SECOND]); auto [succInt, time] = jsBundleInt.ToInt32(); - if (!succInt || time <= 0 || time > H_TO_MS) { + if (!succInt || time <= 0 || time > static_cast(BConstants::H2MS)) { HILOGE("Second argument is not number."); NError(E_PARAMS).ThrowErr(env); return nullptr; diff --git a/services/backup_sa/include/module_ipc/service.h b/services/backup_sa/include/module_ipc/service.h index 45a9393e5..2eba65653 100644 --- a/services/backup_sa/include/module_ipc/service.h +++ b/services/backup_sa/include/module_ipc/service.h @@ -77,6 +77,8 @@ public: ErrCode GetBackupInfo(BundleName &bundleName, std::string &result) override; ErrCode UpdateTimer(BundleName &bundleName, uint32_t timeOut, bool &result) override; ErrCode UpdateSendRate(std::string &bundleName, int32_t sendRate, bool &result) override; + ErrCode StartExtTimer(bool &isExtStart) override; + ErrCode StartFwkTimer(bool &isFwkStart) override; ErrCode SAResultReport(const std::string bundleName, const std::string resultInfo, const ErrCode errCode, const BackupRestoreScenario sennario); @@ -227,6 +229,14 @@ public: */ std::function GetBackupInfoConnectDied(wptr obj, std::string &bundleName); + /** + * @brief timeout callback + * + * @param ptr 当前对象 + * @param bundleName 应用名称 + */ + std::function TimeOutCallback(wptr ptr, std::string bundleName); + public: explicit Service(int32_t saID, bool runOnCreate = false) : SystemAbility(saID, runOnCreate) { diff --git a/services/backup_sa/include/module_ipc/service_stub.h b/services/backup_sa/include/module_ipc/service_stub.h index bd40fdd1e..564b4539c 100644 --- a/services/backup_sa/include/module_ipc/service_stub.h +++ b/services/backup_sa/include/module_ipc/service_stub.h @@ -62,6 +62,8 @@ private: int32_t CmdGetBackupInfo(MessageParcel &data, MessageParcel &reply); int32_t CmdUpdateTimer(MessageParcel &data, MessageParcel &reply); int32_t CmdUpdateSendRate(MessageParcel &data, MessageParcel &reply); + int32_t CmdStartExtTimer(MessageParcel &data, MessageParcel &reply); + int32_t CmdStartFwkTimer(MessageParcel &data, MessageParcel &reply); void ServiceStubSupplement(); void ServiceStubSuppAppendBundles(); 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 7baf1cabb..676d2d205 100644 --- a/services/backup_sa/include/module_ipc/svc_session_manager.h +++ b/services/backup_sa/include/module_ipc/svc_session_manager.h @@ -56,9 +56,12 @@ struct BackupExtInfo { /* Clone App: old device app versionCode */ std::string versionName; /* Ext Ability APP process time */ - uint32_t extTimerId; + uint32_t timerId; /* Timer Status: true is start & false is stop */ - bool timerStatus {false}; + bool extTimerStatus {false}; + bool fwkTimerStatus {false}; + uint32_t timeCount; + uint32_t startTime; int64_t dataSize; int64_t lastIncrementalTime; int32_t manifestFd; @@ -384,12 +387,39 @@ public: void SetBundleDataSize(const std::string &bundleName, int64_t dataSize); /** - * @brief 启动应用扩展能力定时器 + * @brief 启动框架定时器 * * @param bundleName 应用名称 - * @return + * @param callback 超时回调 + * @return bool */ - void BundleExtTimerStart(const std::string &bundleName, const Utils::Timer::TimerCallback &callback); + bool StartFwkTimer(const std::string &bundleName, const Utils::Timer::TimerCallback &callback); + + /** + * @brief 停止框架定时器 + * + * @param bundleName 应用名称 + * @param callback 超时回调 + * @return bool + */ + bool StopFwkTimer(const std::string &bundleName); + + /** + * @brief 启动extension定时器 + * + * @param bundleName 应用名称 + * @param callback 超时回调 + * @return bool + */ + bool StartExtTimer(const std::string &bundleName, const Utils::Timer::TimerCallback &callback); + + /** + * @brief 停止extension定时器 + * + * @param bundleName 应用名称 + * @return bool + */ + bool StopExtTimer(const std::string &bundleName); /** * @brief 重新设置定时器 @@ -402,14 +432,6 @@ public: bool UpdateTimer(const std::string &bundleName, uint32_t timeOut, const Utils::Timer::TimerCallback &callback); - /** - * @brief 取消/暂停应用扩展能力定时器 - * - * @param bundleName 应用名称 - * @return - */ - void BundleExtTimerStop(const std::string &bundleName); - /** * @brief sessionCnt加计数 * @@ -532,11 +554,11 @@ public: */ explicit SvcSessionManager(wptr reversePtr) : reversePtr_(reversePtr) { - extBundleTimer.Setup(); + timer_.Setup(); } ~SvcSessionManager() override { - extBundleTimer.Shutdown(); + timer_.Shutdown(); } private: @@ -545,7 +567,7 @@ private: sptr deathRecipient_; Impl impl_; uint32_t extConnectNum_ {0}; - Utils::Timer extBundleTimer {"backupBundleExtTimer"}; + Utils::Timer timer_ {"backupTimer"}; std::atomic sessionCnt_ {0}; bool unloadSAFlag_ {false}; int32_t memoryParaCurSize_ {BConstants::DEFAULT_VFS_CACHE_PRESSURE}; diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index 8f65b2178..2cdd68162 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -745,7 +745,8 @@ ErrCode Service::AppFileReady(const string &fileName, UniqueFd fd, int32_t errCo // 通知extension清空缓存 proxy->HandleClear(); // 清除Timer - session_->BundleExtTimerStop(callerName); + session_->StopFwkTimer(callerName); + session_->StopExtTimer(callerName); // 通知TOOL 备份完成 session_->GetServiceReverseProxy()->BackupOnBundleFinished(BError(BError::Codes::OK), callerName); // 断开extension @@ -786,7 +787,8 @@ ErrCode Service::AppDone(ErrCode errCode) throw BError(BError::Codes::SA_INVAL_ARG, "Extension backup Proxy is empty"); } proxy->HandleClear(); - session_->BundleExtTimerStop(callerName); + session_->StopFwkTimer(callerName); + session_->StopExtTimer(callerName); NotifyCallerCurAppDone(errCode, callerName); backUpConnection->DisconnectBackupExtAbility(); ClearSessionAndSchedInfo(callerName); @@ -863,7 +865,8 @@ void Service::NotifyCloneBundleFinish(std::string bundleName) throw BError(BError::Codes::SA_INVAL_ARG, "Extension backup Proxy is empty"); } proxy->HandleClear(); - session_->BundleExtTimerStop(bundleName); + session_->StopFwkTimer(bundleName); + session_->StopExtTimer(bundleName); backUpConnection->DisconnectBackupExtAbility(); ClearSessionAndSchedInfo(bundleName); } @@ -1048,7 +1051,8 @@ void Service::ExtConnectDied(const string &callName) try { HILOGI("Begin, bundleName: %{public}s", callName.c_str()); /* Clear Timer */ - session_->BundleExtTimerStop(callName); + session_->StopFwkTimer(callName); + session_->StopExtTimer(callName); auto backUpConnection = session_->GetExtConnection(callName); if (backUpConnection != nullptr && backUpConnection->IsExtAbilityConnected()) { backUpConnection->DisconnectBackupExtAbility(); @@ -1193,44 +1197,15 @@ void Service::NoticeClientFinish(const string &bundleName, ErrCode errCode) void Service::ExtConnectDone(string bundleName) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); - auto timeoutCallback = [ptr {wptr(this)}, bundleName]() { - HILOGI("begin timeoutCallback bundleName = %{public}s", bundleName.c_str()); - auto thisPtr = ptr.promote(); - if (!thisPtr) { - HILOGE("ServicePtr is nullptr."); - return; - } - auto sessionPtr = ptr->session_; - if (sessionPtr == nullptr) { - HILOGE("SessionPtr is nullptr."); - return; - } - try { - if (SAUtils::IsSABundleName(bundleName)) { - auto sessionConnection = sessionPtr->GetSAExtConnection(bundleName); - shared_ptr saConnection = sessionConnection.lock(); - if (saConnection == nullptr) { - HILOGE("lock sa connection ptr is nullptr"); - return; - } - sessionPtr->BundleExtTimerStop(bundleName); - saConnection->DisconnectBackupSAExt(); - } else { - auto sessionConnection = sessionPtr->GetExtConnection(bundleName); - sessionPtr->BundleExtTimerStop(bundleName); - sessionConnection->DisconnectBackupExtAbility(); - } - thisPtr->ClearSessionAndSchedInfo(bundleName); - thisPtr->NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_TIMEOUT)); - } catch (...) { - HILOGE("Unexpected exception, bundleName: %{public}s", bundleName.c_str()); - thisPtr->ClearSessionAndSchedInfo(bundleName); - thisPtr->NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_TIMEOUT)); - } - }; try { HILOGE("begin %{public}s", bundleName.data()); - session_->BundleExtTimerStart(bundleName, timeoutCallback); + auto timeoutCallback = TimeOutCallback(wptr(this), bundleName); + auto scenario = session_->GetScenario(); + if (scenario == IServiceReverse::Scenario::BACKUP) { + session_->StartExtTimer(bundleName, timeoutCallback); + } else if (scenario == IServiceReverse::Scenario::RESTORE) { + session_->StartFwkTimer(bundleName, timeoutCallback); + } session_->SetServiceSchedAction(bundleName, BConstants::ServiceSchedAction::RUNNING); sched_->Sched(bundleName); } catch (...) { @@ -1574,6 +1549,54 @@ ErrCode Service::GetBackupInfo(BundleName &bundleName, std::string &result) } } +ErrCode Service::StartExtTimer(bool &isExtStart) +{ + try { + HILOGI("Service::StartExtTimer begin."); + string bundleName = VerifyCallerAndGetCallerName(); + if (session_ == nullptr) { + HILOGE("StartExtTimer error, session_ is nullptr."); + isExtStart = false; + return BError(BError::Codes::SA_INVAL_ARG); + } + session_->IncreaseSessionCnt(); + auto timeoutCallback = TimeOutCallback(wptr(this), bundleName); + session_->StopFwkTimer(bundleName); + isExtStart = session_->StartExtTimer(bundleName, timeoutCallback); + session_->DecreaseSessionCnt(); + return BError(BError::Codes::OK); + } catch (...) { + isExtStart = false; + session_->DecreaseSessionCnt(); + HILOGI("Unexpected exception"); + return EPERM; + } +} + +ErrCode Service::StartFwkTimer(bool &isFwkStart) +{ + try { + HILOGI("Service::StartFwkTimer begin."); + string bundleName = VerifyCallerAndGetCallerName(); + if (session_ == nullptr) { + HILOGE("StartFwkTimer error, session_ is nullptr."); + isFwkStart = false; + return BError(BError::Codes::SA_INVAL_ARG); + } + session_->IncreaseSessionCnt(); + auto timeoutCallback = TimeOutCallback(wptr(this), bundleName); + session_->StopExtTimer(bundleName); + isFwkStart = session_->StartFwkTimer(bundleName, timeoutCallback); + session_->DecreaseSessionCnt(); + return BError(BError::Codes::OK); + } catch (...) { + isFwkStart = false; + session_->DecreaseSessionCnt(); + HILOGI("Unexpected exception"); + return EPERM; + } +} + ErrCode Service::UpdateTimer(BundleName &bundleName, uint32_t timeOut, bool &result) { auto timeoutCallback = [ptr {wptr(this)}, bundleName]() { @@ -1590,7 +1613,8 @@ ErrCode Service::UpdateTimer(BundleName &bundleName, uint32_t timeOut, bool &res } try { auto sessionConnection = sessionPtr->GetExtConnection(bundleName); - sessionPtr->BundleExtTimerStop(bundleName); + sessionPtr->StopFwkTimer(bundleName); + sessionPtr->StopExtTimer(bundleName); sessionConnection->DisconnectBackupExtAbility(); thisPtr->ClearSessionAndSchedInfo(bundleName); thisPtr->NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_TIMEOUT)); @@ -1722,7 +1746,8 @@ ErrCode Service::SADone(ErrCode errCode, std::string bundleName) HILOGE("lock sa connection ptr is nullptr"); return BError(BError::Codes::SA_INVAL_ARG); } - session_->BundleExtTimerStop(bundleName); + session_->StopFwkTimer(bundleName); + session_->StopExtTimer(bundleName); saConnection->DisconnectBackupSAExt(); ClearSessionAndSchedInfo(bundleName); } @@ -1765,4 +1790,43 @@ void Service::NotifyCallerCurAppDone(ErrCode errCode, const std::string &callerN session_->GetServiceReverseProxy()->RestoreOnBundleFinished(errCode, callerName); } } + +std::function Service::TimeOutCallback(wptr ptr, std::string bundleName) +{ + return [ptr, bundleName]() { + HILOGI("begin timeoutCallback bundleName = %{public}s", bundleName.c_str()); + auto thisPtr = ptr.promote(); + if (!thisPtr) { + HILOGE("ServicePtr is nullptr."); + return; + } + auto sessionPtr = thisPtr->session_; + if (sessionPtr == nullptr) { + HILOGE("SessionPtr is nullptr."); + return; + } + try { + if (SAUtils::IsSABundleName(bundleName)) { + auto sessionConnection = sessionPtr->GetSAExtConnection(bundleName); + shared_ptr saConnection = sessionConnection.lock(); + if (saConnection == nullptr) { + HILOGE("lock sa connection ptr is nullptr"); + return; + } + saConnection->DisconnectBackupSAExt(); + } else { + auto sessionConnection = sessionPtr->GetExtConnection(bundleName); + sessionConnection->DisconnectBackupExtAbility(); + } + sessionPtr->StopFwkTimer(bundleName); + sessionPtr->StopExtTimer(bundleName); + thisPtr->ClearSessionAndSchedInfo(bundleName); + thisPtr->NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_TIMEOUT)); + } catch (...) { + HILOGE("Unexpected exception, bundleName: %{public}s", bundleName.c_str()); + thisPtr->ClearSessionAndSchedInfo(bundleName); + thisPtr->NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_TIMEOUT)); + } + }; +} } // namespace OHOS::FileManagement::Backup diff --git a/services/backup_sa/src/module_ipc/service_incremental.cpp b/services/backup_sa/src/module_ipc/service_incremental.cpp index 7df256035..deb63ff3c 100644 --- a/services/backup_sa/src/module_ipc/service_incremental.cpp +++ b/services/backup_sa/src/module_ipc/service_incremental.cpp @@ -413,7 +413,8 @@ ErrCode Service::AppIncrementalFileReady(const std::string &fileName, UniqueFd f // 通知extension清空缓存 proxy->HandleClear(); // 清除Timer - session_->BundleExtTimerStop(callerName); + session_->StopFwkTimer(callerName); + session_->StopExtTimer(callerName); // 通知TOOL 备份完成 HILOGI("reverse: Will notify IncrementalBackupOnBundleFinished"); session_->GetServiceReverseProxy()->IncrementalBackupOnBundleFinished(BError(BError::Codes::OK), @@ -457,7 +458,8 @@ ErrCode Service::AppIncrementalDone(ErrCode errCode) return BError(BError::Codes::SA_INVAL_ARG, "Extension backup Proxy is empty"); } proxy->HandleClear(); - session_->BundleExtTimerStop(callerName); + session_->StopFwkTimer(callerName); + session_->StopExtTimer(callerName); NotifyCallerCurAppIncrementDone(errCode, callerName); backUpConnection->DisconnectBackupExtAbility(); ClearSessionAndSchedInfo(callerName); diff --git a/services/backup_sa/src/module_ipc/service_stub.cpp b/services/backup_sa/src/module_ipc/service_stub.cpp index bd6d1be91..0fa7782c6 100644 --- a/services/backup_sa/src/module_ipc/service_stub.cpp +++ b/services/backup_sa/src/module_ipc/service_stub.cpp @@ -43,6 +43,10 @@ void ServiceStub::ServiceStubSupplement() opToInterfaceMap_[static_cast( IServiceInterfaceCode::SERVICE_CMD_GET_APP_LOCAL_LIST_AND_DO_INCREMENTAL_BACKUP)] = &ServiceStub::CmdGetAppLocalListAndDoIncrementalBackup; + opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_START_EXT_TIMER)] = + &ServiceStub::CmdStartExtTimer; + opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_START_FWK_TIMER)] = + &ServiceStub::CmdStartFwkTimer; } void ServiceStub::ServiceStubSuppAppendBundles() @@ -447,6 +451,38 @@ int32_t ServiceStub::CmdUpdateSendRate(MessageParcel &data, MessageParcel &reply return BError(BError::Codes::OK); } +int32_t ServiceStub::CmdStartExtTimer(MessageParcel &data, MessageParcel &reply) +{ + HILOGI("ServiceStub::CmdStartExtTimer Begin."); + int ret = ERR_OK; + bool isExtStart; + ret = StartExtTimer(isExtStart); + if (ret != ERR_OK) { + return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to call UpdateTimer")); + } + if (!reply.WriteBool(isExtStart)) { + return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to write result")); + } + HILOGI("ServiceStub::CmdStartExtTimer end."); + return BError(BError::Codes::OK); +} + +int32_t ServiceStub::CmdStartFwkTimer(MessageParcel &data, MessageParcel &reply) +{ + HILOGI("ServiceStub::CmdStartFwkTimer Begin."); + int ret = ERR_OK; + bool isFwkStart; + ret = StartFwkTimer(isFwkStart); + if (ret != ERR_OK) { + return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to call UpdateTimer")); + } + if (!reply.WriteBool(isFwkStart)) { + return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to write result")); + } + HILOGI("ServiceStub::CmdStartFwkTimer end."); + return BError(BError::Codes::OK); +} + int32_t ServiceStub::CmdRelease(MessageParcel &data, MessageParcel &reply) { int res = Release(); 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 20bb48b24..9e1fa9833 100644 --- a/services/backup_sa/src/module_ipc/svc_session_manager.cpp +++ b/services/backup_sa/src/module_ipc/svc_session_manager.cpp @@ -28,6 +28,7 @@ #include "b_json/b_json_entity_ext_manage.h" #include "b_resources/b_constants.h" #include "b_sa/b_sa_utils.h" +#include "b_utils/b_time.h" #include "filemgmt_libhilog.h" #include "module_ipc/service.h" #include "module_ipc/svc_restore_deps_manager.h" @@ -683,60 +684,131 @@ uint32_t SvcSessionManager::CalAppProcessTime(const std::string &bundleName) } timeout = timeout < minTimeout ? minTimeout : timeout; resTimeoutMs = (uint32_t)(timeout * invertMillisecond % UINT_MAX); /* conver second to millisecond */ - HILOGI("Calculate App extension process run timeout=%{public}u(us), bundleName=%{public}s ", resTimeoutMs, + HILOGI("Calculate App extension process run timeout=%{public}u(ms), bundleName=%{public}s ", resTimeoutMs, bundleName.c_str()); return resTimeoutMs; } -void SvcSessionManager::BundleExtTimerStart(const std::string &bundleName, const Utils::Timer::TimerCallback &callback) +bool SvcSessionManager::StartFwkTimer(const std::string &bundleName, const Utils::Timer::TimerCallback &callback) { unique_lock lock(lock_); + HILOGI("StartFwkTimer begin bundleName %{public}s", bundleName.c_str()); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + HILOGE("No caller token was specified"); + return false; + } + auto it = GetBackupExtNameMap(bundleName); + if (it->second.fwkTimerStatus == true) { + HILOGE("FwkTimer is registered, unregister first."); + return false; } uint32_t timeout = CalAppProcessTime(bundleName); - auto it = GetBackupExtNameMap(bundleName); - if (it->second.timerStatus == false) { - it->second.timerStatus = true; - it->second.extTimerId = extBundleTimer.Register(callback, timeout, true); + it->second.fwkTimerStatus = true; + it->second.timerId = timer_.Register(callback, timeout, true); + HILOGI("StartFwkTimer end bundleName %{public}s", bundleName.c_str()); + return true; +} + +bool SvcSessionManager::StopFwkTimer(const std::string &bundleName) +{ + unique_lock lock(lock_); + HILOGI("StopFwkTimer begin bundleName %{public}s", bundleName.c_str()); + if (!impl_.clientToken) { + HILOGE("No caller token was specified"); + return false; } + auto it = GetBackupExtNameMap(bundleName); + if (it->second.fwkTimerStatus == false) { + HILOGE("FwkTimer is unregistered, register first."); + return true; + } + + it->second.fwkTimerStatus = false; + timer_.Unregister(it->second.timerId); + HILOGI("StopFwkTimer end bundleName %{public}s", bundleName.c_str()); + return true; +} + +bool SvcSessionManager::StartExtTimer(const std::string &bundleName, const Utils::Timer::TimerCallback &callback) +{ + unique_lock lock(lock_); + HILOGI("StartExtTimer begin bundleName %{public}s", bundleName.c_str()); + if (!impl_.clientToken) { + HILOGE("No caller token was specified"); + return false; + } + auto it = GetBackupExtNameMap(bundleName); + if (it->second.extTimerStatus == true) { + HILOGE("ExtTimer is registered, unregister first."); + return false; + } + uint32_t timeout = it->second.timeCount; + timeout = (timeout != 0) ? timeout : CalAppProcessTime(bundleName); + it->second.extTimerStatus = true; + it->second.startTime = static_cast(TimeUtils::GetTimeMS()); + it->second.timeCount = timeout; + it->second.timerId = timer_.Register(callback, timeout, true); + HILOGI("StartExtTimer end, timeout %{public}u(ms), bundleName %{public}s", timeout, bundleName.c_str()); + return true; +} + +bool SvcSessionManager::StopExtTimer(const std::string &bundleName) +{ + unique_lock lock(lock_); + HILOGI("StopExtTimer begin bundleName %{public}s", bundleName.c_str()); + if (!impl_.clientToken) { + HILOGE("No caller token was specified"); + return false; + } + auto it = GetBackupExtNameMap(bundleName); + if (it->second.extTimerStatus == false) { + HILOGE("ExtTimer is unregistered, register first."); + return true; + } + + it->second.extTimerStatus = false; + it->second.startTime = 0; + it->second.timeCount = 0; + timer_.Unregister(it->second.timerId); + HILOGI("StopExtTimer end bundleName %{public}s", bundleName.c_str()); + return true; } bool SvcSessionManager::UpdateTimer(const std::string &bundleName, uint32_t timeOut, const Utils::Timer::TimerCallback &callback) { unique_lock lock(lock_); + HILOGI("UpdateTimer begin bundleName %{public}s", bundleName.c_str()); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + HILOGE("No caller token was specified"); + return false; } auto it = GetBackupExtNameMap(bundleName); - if (it->second.timerStatus == true) { - // 定时器已存在,则先销毁,再重新注册 - it->second.timerStatus = false; - extBundleTimer.Unregister(it->second.extTimerId); - HILOGI("UpdateTimer timeout=%{public}u(ms), bundleName=%{public}s ", - timeOut, bundleName.c_str()); - it->second.extTimerId = extBundleTimer.Register(callback, timeOut, true); - it->second.timerStatus = true; + if (it->second.extTimerStatus == false) { + HILOGI("ExtTimer is unregistered, just count. timeout %{public}u(ms), timeCount %{public}u(ms)", + timeOut, it->second.timeCount); return true; } - return false; -} -void SvcSessionManager::BundleExtTimerStop(const std::string &bundleName) -{ - unique_lock lock(lock_); - if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + if (it->second.startTime == 0) { + HILOGE("ExtTimer is registered, but start time is zero."); + return false; } - auto it = GetBackupExtNameMap(bundleName); - if (it->second.timerStatus == true) { - it->second.timerStatus = false; - extBundleTimer.Unregister(it->second.extTimerId); - } + it->second.timeCount += timeOut; + uint32_t updateTime = static_cast(TimeUtils::GetTimeMS()); + uint32_t elapseTime = updateTime - it->second.startTime; + uint32_t realTimeout = it->second.timeCount - elapseTime; + timer_.Unregister(it->second.timerId); + HILOGI("UpdateTimer timeout %{public}u(ms), timeCount %{public}u(ms), elapseTime %{public}u(ms)," + "realTimeout %{public}u(ms), bundleName %{public}s ", + timeOut, it->second.timeCount, elapseTime, realTimeout, bundleName.c_str()); + it->second.timerId = timer_.Register(callback, realTimeout, true); + it->second.extTimerStatus = true; + HILOGI("UpdateTimer end bundleName %{public}s", bundleName.c_str()); + return true; } void SvcSessionManager::IncreaseSessionCnt() @@ -768,9 +840,10 @@ void SvcSessionManager::ClearSessionData() unique_lock lock(lock_); for (auto &&it : impl_.backupExtNameMap) { // clear timer - if (it.second.timerStatus == true) { - it.second.timerStatus = false; - extBundleTimer.Unregister(it.second.extTimerId); + if (it.second.fwkTimerStatus == true || it.second.extTimerStatus == true) { + it.second.fwkTimerStatus = false; + it.second.extTimerStatus = false; + timer_.Unregister(it.second.timerId); } // disconnect extension if (it.second.schedAction == BConstants::ServiceSchedAction::RUNNING) { diff --git a/tests/mock/backup_kit_inner/service_proxy_mock.cpp b/tests/mock/backup_kit_inner/service_proxy_mock.cpp index 95af50b71..8066f1222 100644 --- a/tests/mock/backup_kit_inner/service_proxy_mock.cpp +++ b/tests/mock/backup_kit_inner/service_proxy_mock.cpp @@ -182,6 +182,15 @@ ErrCode ServiceProxy::UpdateTimer(BundleName &bundleName, uint32_t timeOut, bool return BError(BError::Codes::OK); } +ErrCode ServiceProxy::StartExtTimer(bool &isExtStart) +{ + return BError(BError::Codes::OK); +} +ErrCode ServiceProxy::StartFwkTimer(bool &isFwkStart) +{ + return BError(BError::Codes::OK); +} + ErrCode ServiceProxy::UpdateSendRate(std::string &bundleName, int32_t sendRate, bool &result) { return BError(BError::Codes::OK); diff --git a/tests/mock/module_ipc/service_mock.cpp b/tests/mock/module_ipc/service_mock.cpp index c058c5e83..a1cea2b9a 100644 --- a/tests/mock/module_ipc/service_mock.cpp +++ b/tests/mock/module_ipc/service_mock.cpp @@ -216,6 +216,16 @@ ErrCode Service::GetBackupInfo(BundleName &bundleName, std::string &result) return BError(BError::Codes::OK); } +ErrCode Service::StartExtTimer(bool &isExtStart) +{ + return BError(BError::Codes::OK); +} + +ErrCode Service::StartFwkTimer(bool &isFwkStart) +{ + return BError(BError::Codes::OK); +} + ErrCode Service::UpdateTimer(BundleName &bundleName, uint32_t timeOut, bool &result) { return BError(BError::Codes::OK); diff --git a/tests/mock/module_ipc/svc_session_manager_mock.cpp b/tests/mock/module_ipc/svc_session_manager_mock.cpp index 53b97b777..dcaf5e5ec 100644 --- a/tests/mock/module_ipc/svc_session_manager_mock.cpp +++ b/tests/mock/module_ipc/svc_session_manager_mock.cpp @@ -354,8 +354,24 @@ void SvcSessionManager::SetBundleDataSize(const std::string &bundleName, int64_t it->second.dataSize = dataSize; } -void SvcSessionManager::BundleExtTimerStart(const std::string &bundleName, const Utils::Timer::TimerCallback &callback) +bool SvcSessionManager::StartFwkTimer(const std::string &bundleName, const Utils::Timer::TimerCallback &callback) { + return true; +} + +bool SvcSessionManager::StopFwkTimer(const std::string &bundleName) +{ + return true; +} + +bool SvcSessionManager::StartExtTimer(const std::string &bundleName, const Utils::Timer::TimerCallback &callback) +{ + return true; +} + +bool SvcSessionManager::StopExtTimer(const std::string &bundleName) +{ + return true; } bool SvcSessionManager::UpdateTimer(const std::string &bundleName, uint32_t timeOut, @@ -364,8 +380,6 @@ bool SvcSessionManager::UpdateTimer(const std::string &bundleName, uint32_t time return true; } -void SvcSessionManager::BundleExtTimerStop(const std::string &bundleName) {} - void SvcSessionManager::IncreaseSessionCnt() {} void SvcSessionManager::DecreaseSessionCnt() {} diff --git a/tests/mock/module_ipc/svc_session_manager_throw_mock.cpp b/tests/mock/module_ipc/svc_session_manager_throw_mock.cpp index 80acd08fc..6176f43dd 100644 --- a/tests/mock/module_ipc/svc_session_manager_throw_mock.cpp +++ b/tests/mock/module_ipc/svc_session_manager_throw_mock.cpp @@ -219,9 +219,24 @@ void SvcSessionManager::SetBundleDataSize(const std::string &bundleName, int64_t BackupSvcSessionManager::session->SetBundleDataSize(bundleName, dataSize); } -void SvcSessionManager::BundleExtTimerStart(const std::string &bundleName, const Utils::Timer::TimerCallback &callback) +bool SvcSessionManager::StartFwkTimer(const std::string &bundleName, const Utils::Timer::TimerCallback &callback) { - BackupSvcSessionManager::session->BundleExtTimerStart(bundleName, callback); + return BackupSvcSessionManager::session->StartFwkTimer(bundleName, callback); +} + +bool SvcSessionManager::StopFwkTimer(const std::string &bundleName) +{ + return BackupSvcSessionManager::session->StopFwkTimer(bundleName); +} + +bool SvcSessionManager::StartExtTimer(const std::string &bundleName, const Utils::Timer::TimerCallback &callback) +{ + return BackupSvcSessionManager::session->StartExtTimer(bundleName, callback); +} + +bool SvcSessionManager::StopExtTimer(const std::string &bundleName) +{ + return BackupSvcSessionManager::session->StopExtTimer(bundleName); } bool SvcSessionManager::UpdateTimer(const std::string &bundleName, uint32_t timeOut, @@ -230,11 +245,6 @@ bool SvcSessionManager::UpdateTimer(const std::string &bundleName, uint32_t time return BackupSvcSessionManager::session->UpdateTimer(bundleName, timeOut, callback); } -void SvcSessionManager::BundleExtTimerStop(const std::string &bundleName) -{ - BackupSvcSessionManager::session->BundleExtTimerStop(bundleName); -} - void SvcSessionManager::IncreaseSessionCnt() { BackupSvcSessionManager::session->IncreaseSessionCnt(); diff --git a/tests/mock/module_ipc/svc_session_manager_throw_mock.h b/tests/mock/module_ipc/svc_session_manager_throw_mock.h index f00d6e93d..a916d7c6a 100644 --- a/tests/mock/module_ipc/svc_session_manager_throw_mock.h +++ b/tests/mock/module_ipc/svc_session_manager_throw_mock.h @@ -67,9 +67,11 @@ public: virtual void SetBundleVersionName(const std::string &, std::string) = 0; virtual std::string GetBundleVersionName(const std::string &) = 0; virtual void SetBundleDataSize(const std::string &, int64_t) = 0; - virtual void BundleExtTimerStart(const std::string &, const Utils::Timer::TimerCallback &) = 0; + virtual bool StartFwkTimer(const std::string &, const Utils::Timer::TimerCallback &) = 0; + virtual bool StopFwkTimer(const std::string &) = 0; + virtual bool StartExtTimer(const std::string &, const Utils::Timer::TimerCallback &) = 0; + virtual bool StopExtTimer(const std::string &) = 0; virtual bool UpdateTimer(const std::string &, uint32_t, const Utils::Timer::TimerCallback &) = 0; - virtual void BundleExtTimerStop(const std::string &) = 0; virtual void IncreaseSessionCnt() = 0; virtual void DecreaseSessionCnt() = 0; virtual int32_t GetMemParaCurSize() = 0; @@ -130,9 +132,11 @@ public: MOCK_METHOD(void, SetBundleVersionName, (const std::string &, std::string)); MOCK_METHOD(std::string, GetBundleVersionName, (const std::string &)); MOCK_METHOD(void, SetBundleDataSize, (const std::string &, int64_t)); - MOCK_METHOD(void, BundleExtTimerStart, (const std::string &, const Utils::Timer::TimerCallback &)); + MOCK_METHOD(bool, StartFwkTimer, (const std::string &, const Utils::Timer::TimerCallback &)); + MOCK_METHOD(bool, StopFwkTimer, (const std::string &)); + MOCK_METHOD(bool, StartExtTimer, (const std::string &, const Utils::Timer::TimerCallback &)); + MOCK_METHOD(bool, StopExtTimer, (const std::string &)); MOCK_METHOD(bool, UpdateTimer, (const std::string &, uint32_t, const Utils::Timer::TimerCallback &)); - MOCK_METHOD(void, BundleExtTimerStop, (const std::string &)); MOCK_METHOD(void, IncreaseSessionCnt, ()); MOCK_METHOD(void, DecreaseSessionCnt, ()); MOCK_METHOD(int32_t, GetMemParaCurSize, ()); 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 0a0ffa7e8..7b6f60248 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 @@ -143,6 +143,16 @@ public: return BError(BError::Codes::OK); } + ErrCode StartExtTimer(bool &isExtStart) override + { + return BError(BError::Codes::OK); + } + + ErrCode StartFwkTimer(bool &isFwkStart) override + { + return BError(BError::Codes::OK); + } + ErrCode UpdateSendRate(std::string &bundleName, int32_t sendRate, bool &result) 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 307c3c7e0..d241322b0 100644 --- a/tests/unittests/backup_sa/module_ipc/service_stub_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_stub_test.cpp @@ -80,6 +80,8 @@ public: MOCK_METHOD2(GetIncrementalFileHandle, ErrCode(const std::string &bundleName, const std::string &fileName)); MOCK_METHOD2(GetBackupInfo, ErrCode(string &bundleName, string &result)); MOCK_METHOD3(UpdateTimer, ErrCode(BundleName &bundleName, uint32_t timeOut, bool &result)); + MOCK_METHOD1(StartExtTimer, ErrCode(bool &isExtStart)); + MOCK_METHOD1(StartFwkTimer, ErrCode(bool &isFwkStart)); MOCK_METHOD3(UpdateSendRate, ErrCode(std::string &bundleName, int32_t sendRate, bool &result)); }; diff --git a/tests/unittests/backup_sa/module_ipc/service_throw_test.cpp b/tests/unittests/backup_sa/module_ipc/service_throw_test.cpp index 06dc18af3..1b83632f0 100644 --- a/tests/unittests/backup_sa/module_ipc/service_throw_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_throw_test.cpp @@ -529,8 +529,9 @@ HWTEST_F(ServiceThrowTest, SUB_Service_throw_OnBackupExtensionDied_0100, testing EXPECT_CALL(*sessionMock, VerifyBundleName(_)).WillOnce(Invoke([]() { throw BError(BError::Codes::EXT_THROW_EXCEPTION); })); - EXPECT_CALL(*sessionMock, BundleExtTimerStop(_)).WillOnce(Invoke([]() { + EXPECT_CALL(*sessionMock, StopFwkTimer(_)).WillOnce(Invoke([]() { throw BError(BError::Codes::EXT_THROW_EXCEPTION); + return true; })); EXPECT_CALL(*sessionMock, RemoveExtInfo(_)).WillOnce(Invoke([]() { throw BError(BError::Codes::EXT_THROW_EXCEPTION); @@ -548,40 +549,6 @@ HWTEST_F(ServiceThrowTest, SUB_Service_throw_OnBackupExtensionDied_0100, testing GTEST_LOG_(INFO) << "ServiceThrowTest-end SUB_Service_throw_OnBackupExtensionDied_0100"; } -/** - * @tc.number: SUB_Service_throw_ExtConnectDied_0100 - * @tc.name: SUB_Service_throw_ExtConnectDied_0100 - * @tc.desc: 测试 ExtConnectDied 接口的 catch 分支 - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: issuesIAC04T - */ -HWTEST_F(ServiceThrowTest, SUB_Service_throw_ExtConnectDied_0100, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "ServiceThrowTest-begin SUB_Service_throw_ExtConnectDied_0100"; - try { - EXPECT_NE(service, nullptr); - string callName; - EXPECT_CALL(*sessionMock, BundleExtTimerStop(_)).WillOnce(Invoke([]() { - throw BError(BError::Codes::EXT_THROW_EXCEPTION); - })); - EXPECT_CALL(*sessionMock, RemoveExtInfo(_)).WillOnce(Invoke([]() { - throw BError(BError::Codes::EXT_THROW_EXCEPTION); - })); - EXPECT_CALL(*sessionMock, GetScenario()).WillOnce(Invoke([]() { - throw BError(BError::Codes::EXT_THROW_EXCEPTION); - return IServiceReverse::Scenario::UNDEFINED; - })); - service->ExtConnectDied(callName); - EXPECT_TRUE(true); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "ServiceThrowTest-an exception occurred by ExtConnectDied."; - } - GTEST_LOG_(INFO) << "ServiceThrowTest-end SUB_Service_throw_ExtConnectDied_0100"; -} - /** * @tc.number: SUB_Service_throw_ExtStart_0100 * @tc.name: SUB_Service_throw_ExtStart_0100 @@ -687,40 +654,6 @@ HWTEST_F(ServiceThrowTest, SUB_Service_throw_NoticeClientFinish_0100, testing::e GTEST_LOG_(INFO) << "ServiceThrowTest-end SUB_Service_throw_NoticeClientFinish_0100"; } -/** - * @tc.number: SUB_Service_throw_ExtConnectDone_0100 - * @tc.name: SUB_Service_throw_ExtConnectDone_0100 - * @tc.desc: 测试 ExtConnectDone 接口的 catch 分支 - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: issuesIAC04T - */ -HWTEST_F(ServiceThrowTest, SUB_Service_throw_ExtConnectDone_0100, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "ServiceThrowTest-begin SUB_Service_throw_ExtConnectDone_0100"; - try { - EXPECT_NE(service, nullptr); - string bundleName; - EXPECT_CALL(*sessionMock, BundleExtTimerStart(_, _)).WillOnce(Invoke([]() { - throw BError(BError::Codes::EXT_THROW_EXCEPTION); - })); - EXPECT_CALL(*sessionMock, RemoveExtInfo(_)).WillOnce(Invoke([]() { - throw BError(BError::Codes::EXT_THROW_EXCEPTION); - })); - EXPECT_CALL(*sessionMock, GetScenario()).WillOnce(Invoke([]() { - throw BError(BError::Codes::EXT_THROW_EXCEPTION); - return IServiceReverse::Scenario::UNDEFINED; - })); - service->ExtConnectDone(bundleName); - EXPECT_TRUE(true); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "ServiceThrowTest-an exception occurred by ExtConnectDone."; - } - GTEST_LOG_(INFO) << "ServiceThrowTest-end SUB_Service_throw_ExtConnectDone_0100"; -} - /** * @tc.number: SUB_Service_throw_ClearSessionAndSchedInfo_0100 * @tc.name: SUB_Service_throw_ClearSessionAndSchedInfo_0100 diff --git a/tests/unittests/backup_sa/module_ipc/svc_session_manager_test.cpp b/tests/unittests/backup_sa/module_ipc/svc_session_manager_test.cpp index 411ade2de..0f6bb9407 100644 --- a/tests/unittests/backup_sa/module_ipc/svc_session_manager_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/svc_session_manager_test.cpp @@ -1704,47 +1704,47 @@ HWTEST_F(SvcSessionManagerTest, SUB_backup_sa_session_SetBundleDataSize_0100, te } /** - * @tc.number: SUB_backup_sa_session_BundleExtTimerStart_0100 - * @tc.name: SUB_backup_sa_session_BundleExtTimerStart_0100 - * @tc.desc: 测试 BundleExtTimerStart + * @tc.number: SUB_backup_sa_session_StartFwkTimer_0100 + * @tc.name: SUB_backup_sa_session_StartFwkTimer_0100 + * @tc.desc: 测试 StartFwkTimer * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 * @tc.require: I6F3GV */ -HWTEST_F(SvcSessionManagerTest, SUB_backup_sa_session_BundleExtTimerStart_0100, testing::ext::TestSize.Level1) +HWTEST_F(SvcSessionManagerTest, SUB_backup_sa_session_StartFwkTimer_0100, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "SvcSessionManagerTest-begin SUB_backup_sa_session_BundleExtTimerStart_0100"; + GTEST_LOG_(INFO) << "SvcSessionManagerTest-begin SUB_backup_sa_session_StartFwkTimer_0100"; try { auto callback = []() -> void {}; - try { - EXPECT_TRUE(sessionManagerPtr_ != nullptr); - sessionManagerPtr_->impl_.clientToken = 0; - sessionManagerPtr_->BundleExtTimerStart(BUNDLE_NAME, callback); - EXPECT_TRUE(false); - } catch (BError &err) { - EXPECT_EQ(err.GetRawCode(), BError::Codes::SA_INVAL_ARG); - } + EXPECT_TRUE(sessionManagerPtr_ != nullptr); + sessionManagerPtr_->impl_.clientToken = 0; + auto ret = sessionManagerPtr_->StartFwkTimer(BUNDLE_NAME, callback); + EXPECT_FALSE(ret); BackupExtInfo info; - info.timerStatus = false; + info.fwkTimerStatus = false; sessionManagerPtr_->impl_.clientToken = CLIENT_TOKEN_ID; sessionManagerPtr_->impl_.backupExtNameMap.clear(); sessionManagerPtr_->impl_.backupExtNameMap[BUNDLE_NAME] = info; - sessionManagerPtr_->BundleExtTimerStart(BUNDLE_NAME, callback); - EXPECT_TRUE(true); + ret = sessionManagerPtr_->StartFwkTimer(BUNDLE_NAME, callback); + EXPECT_TRUE(ret); + ret = sessionManagerPtr_->StopFwkTimer(BUNDLE_NAME); + EXPECT_TRUE(ret); - info.timerStatus = true; + info.fwkTimerStatus = true; sessionManagerPtr_->impl_.clientToken = CLIENT_TOKEN_ID; sessionManagerPtr_->impl_.backupExtNameMap.clear(); sessionManagerPtr_->impl_.backupExtNameMap[BUNDLE_NAME] = info; - sessionManagerPtr_->BundleExtTimerStart(BUNDLE_NAME, callback); - EXPECT_TRUE(true); + ret = sessionManagerPtr_->StartFwkTimer(BUNDLE_NAME, callback); + EXPECT_FALSE(ret); + ret = sessionManagerPtr_->StopFwkTimer(BUNDLE_NAME); + EXPECT_TRUE(ret); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "SvcSessionManagerTest-an exception occurred by BundleExtTimerStart."; + GTEST_LOG_(INFO) << "SvcSessionManagerTest-an exception occurred by StartFwkTimer."; } - GTEST_LOG_(INFO) << "SvcSessionManagerTest-end SUB_backup_sa_session_BundleExtTimerStart_0100"; + GTEST_LOG_(INFO) << "SvcSessionManagerTest-end SUB_backup_sa_session_StartFwkTimer_0100"; } /** @@ -1761,29 +1761,35 @@ HWTEST_F(SvcSessionManagerTest, SUB_backup_sa_session_UpdateTimer_0100, testing: GTEST_LOG_(INFO) << "SvcSessionManagerTest-begin SUB_backup_sa_session_UpdateTimer_0100"; try { auto callback = []() -> void {}; - try { - EXPECT_TRUE(sessionManagerPtr_ != nullptr); - sessionManagerPtr_->impl_.clientToken = 0; - sessionManagerPtr_->UpdateTimer(BUNDLE_NAME, 30, callback); - EXPECT_TRUE(false); - } catch (BError &err) { - EXPECT_EQ(err.GetRawCode(), BError::Codes::SA_INVAL_ARG); - } - - BackupExtInfo info; - info.timerStatus = false; - sessionManagerPtr_->impl_.clientToken = CLIENT_TOKEN_ID; - sessionManagerPtr_->impl_.backupExtNameMap.clear(); - sessionManagerPtr_->impl_.backupExtNameMap[BUNDLE_NAME] = info; + EXPECT_TRUE(sessionManagerPtr_ != nullptr); + sessionManagerPtr_->impl_.clientToken = 0; auto ret = sessionManagerPtr_->UpdateTimer(BUNDLE_NAME, 30, callback); EXPECT_FALSE(ret); - info.timerStatus = true; + BackupExtInfo info; + info.extTimerStatus = false; sessionManagerPtr_->impl_.clientToken = CLIENT_TOKEN_ID; sessionManagerPtr_->impl_.backupExtNameMap.clear(); sessionManagerPtr_->impl_.backupExtNameMap[BUNDLE_NAME] = info; ret = sessionManagerPtr_->UpdateTimer(BUNDLE_NAME, 30, callback); EXPECT_TRUE(ret); + + sessionManagerPtr_->impl_.clientToken = CLIENT_TOKEN_ID; + sessionManagerPtr_->impl_.backupExtNameMap.clear(); + sessionManagerPtr_->impl_.backupExtNameMap[BUNDLE_NAME] = info; + ret = sessionManagerPtr_->StartExtTimer(BUNDLE_NAME, callback); + EXPECT_TRUE(ret); + ret = sessionManagerPtr_->UpdateTimer(BUNDLE_NAME, 30, callback); + EXPECT_TRUE(ret); + ret = sessionManagerPtr_->StopExtTimer(BUNDLE_NAME); + EXPECT_TRUE(ret); + + info.extTimerStatus = true; + sessionManagerPtr_->impl_.clientToken = CLIENT_TOKEN_ID; + sessionManagerPtr_->impl_.backupExtNameMap.clear(); + sessionManagerPtr_->impl_.backupExtNameMap[BUNDLE_NAME] = info; + ret = sessionManagerPtr_->UpdateTimer(BUNDLE_NAME, 30, callback); + EXPECT_FALSE(ret); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "SvcSessionManagerTest-an exception occurred by UpdateTimer."; @@ -1792,46 +1798,42 @@ HWTEST_F(SvcSessionManagerTest, SUB_backup_sa_session_UpdateTimer_0100, testing: } /** - * @tc.number: SUB_backup_sa_session_BundleExtTimerStop_0100 - * @tc.name: SUB_backup_sa_session_BundleExtTimerStop_0100 - * @tc.desc: 测试 BundleExtTimerStop + * @tc.number: SUB_backup_sa_session_StopFwkTimer_0100 + * @tc.name: SUB_backup_sa_session_StopFwkTimer_0100 + * @tc.desc: 测试 StopFwkTimer * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 * @tc.require: I6F3GV */ -HWTEST_F(SvcSessionManagerTest, SUB_backup_sa_session_BundleExtTimerStop_0100, testing::ext::TestSize.Level1) +HWTEST_F(SvcSessionManagerTest, SUB_backup_sa_session_StopFwkTimer_0100, testing::ext::TestSize.Level1) { - GTEST_LOG_(INFO) << "SvcSessionManagerTest-begin SUB_backup_sa_session_BundleExtTimerStop_0100"; + GTEST_LOG_(INFO) << "SvcSessionManagerTest-begin SUB_backup_sa_session_StopFwkTimer_0100"; try { - try { - EXPECT_TRUE(sessionManagerPtr_ != nullptr); - sessionManagerPtr_->impl_.clientToken = 0; - sessionManagerPtr_->BundleExtTimerStop(BUNDLE_NAME); - EXPECT_TRUE(false); - } catch (BError &err) { - EXPECT_EQ(err.GetRawCode(), BError::Codes::SA_INVAL_ARG); - } + EXPECT_TRUE(sessionManagerPtr_ != nullptr); + sessionManagerPtr_->impl_.clientToken = 0; + auto ret = sessionManagerPtr_->StopFwkTimer(BUNDLE_NAME); + EXPECT_FALSE(ret); BackupExtInfo info; - info.timerStatus = false; + info.fwkTimerStatus = false; sessionManagerPtr_->impl_.clientToken = CLIENT_TOKEN_ID; sessionManagerPtr_->impl_.backupExtNameMap.clear(); sessionManagerPtr_->impl_.backupExtNameMap[BUNDLE_NAME] = info; - sessionManagerPtr_->BundleExtTimerStop(BUNDLE_NAME); - EXPECT_TRUE(true); + ret = sessionManagerPtr_->StopFwkTimer(BUNDLE_NAME); + EXPECT_TRUE(ret); - info.timerStatus = true; + info.fwkTimerStatus = true; sessionManagerPtr_->impl_.clientToken = CLIENT_TOKEN_ID; sessionManagerPtr_->impl_.backupExtNameMap.clear(); sessionManagerPtr_->impl_.backupExtNameMap[BUNDLE_NAME] = info; - sessionManagerPtr_->BundleExtTimerStop(BUNDLE_NAME); - EXPECT_TRUE(true); + ret = sessionManagerPtr_->StopFwkTimer(BUNDLE_NAME); + EXPECT_TRUE(ret); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "SvcSessionManagerTest-an exception occurred by BundleExtTimerStop."; + GTEST_LOG_(INFO) << "SvcSessionManagerTest-an exception occurred by StopFwkTimer."; } - GTEST_LOG_(INFO) << "SvcSessionManagerTest-end SUB_backup_sa_session_BundleExtTimerStop_0100"; + GTEST_LOG_(INFO) << "SvcSessionManagerTest-end SUB_backup_sa_session_StopFwkTimer_0100"; } /** @@ -1848,7 +1850,7 @@ HWTEST_F(SvcSessionManagerTest, SUB_backup_sa_session_ClearSessionData_0100, tes GTEST_LOG_(INFO) << "SvcSessionManagerTest-begin SUB_backup_sa_session_ClearSessionData_0100"; try { BackupExtInfo info; - info.timerStatus = true; + info.fwkTimerStatus = true; info.schedAction = BConstants::ServiceSchedAction::RUNNING; info.backUpConnection = sptr(new SvcBackupConnection(nullptr, nullptr, BUNDLE_NAME)); EXPECT_TRUE(sessionManagerPtr_ != nullptr); @@ -1857,7 +1859,7 @@ HWTEST_F(SvcSessionManagerTest, SUB_backup_sa_session_ClearSessionData_0100, tes sessionManagerPtr_->ClearSessionData(); EXPECT_TRUE(true); - info.timerStatus = false; + info.fwkTimerStatus = false; info.schedAction = BConstants::ServiceSchedAction::WAIT; sessionManagerPtr_->impl_.backupExtNameMap.clear(); sessionManagerPtr_->impl_.backupExtNameMap[BUNDLE_NAME] = info; diff --git a/tests/unittests/backup_sa/session/service_proxy_mock.cpp b/tests/unittests/backup_sa/session/service_proxy_mock.cpp index 722084256..917725a54 100644 --- a/tests/unittests/backup_sa/session/service_proxy_mock.cpp +++ b/tests/unittests/backup_sa/session/service_proxy_mock.cpp @@ -162,6 +162,16 @@ ErrCode ServiceProxy::UpdateTimer(BundleName &bundleName, uint32_t timeOut, bool return BError(BError::Codes::OK); } +ErrCode ServiceProxy::StartExtTimer(bool &isExtStart) +{ + return BError(BError::Codes::OK); +} + +ErrCode ServiceProxy::StartFwkTimer(bool &isFwkStart) +{ + return BError(BError::Codes::OK); +} + ErrCode ServiceProxy::UpdateSendRate(std::string &bundleName, int32_t sendRate, bool &result) { return BError(BError::Codes::OK); diff --git a/utils/BUILD.gn b/utils/BUILD.gn index 151ab2480..1ff282364 100644 --- a/utils/BUILD.gn +++ b/utils/BUILD.gn @@ -77,6 +77,7 @@ ohos_shared_library("backup_utils") { "src/b_sa/b_sa_utils.cpp", "src/b_tarball/b_tarball_cmdline.cpp", "src/b_tarball/b_tarball_factory.cpp", + "src/b_utils/b_time.cpp", ] configs = [ ":utils_private_config" ] diff --git a/utils/include/b_resources/b_constants.h b/utils/include/b_resources/b_constants.h index 7d85e5f90..aeb11cd77 100644 --- a/utils/include/b_resources/b_constants.h +++ b/utils/include/b_resources/b_constants.h @@ -77,6 +77,7 @@ constexpr int MAX_FD_SEND_RATE = 800; // 允许应用申请的最大FD数量 constexpr int MIN_FD_SEND_RATE = 0; // 允许应用申请的最小FD数量 constexpr int DEFAULT_FD_SEND_RATE = 60; // 框架默认的FD数量 constexpr int32_t PARAM_STARING_MAX_MEMORY = 2 * 1024 * 1024; +constexpr uint32_t H2MS = 60 * 60 * 1000; // backup.para内配置项的名称,该配置项值为true时可在不更新hap包的情况下,可以读取包管理元数据配置文件的内容 static inline std::string BACKUP_DEBUG_OVERRIDE_EXTENSION_CONFIG_KEY = "backup.debug.overrideExtensionConfig"; diff --git a/utils/include/b_sa/b_sa_utils.h b/utils/include/b_sa/b_sa_utils.h index 86d1b8aa9..b8d3edf4c 100644 --- a/utils/include/b_sa/b_sa_utils.h +++ b/utils/include/b_sa/b_sa_utils.h @@ -25,5 +25,5 @@ public: static bool CheckPermission(const std::string &permission); static bool IsSystemApp(); }; -} // namespace OHOS::FileManagement::Backup::BEncryption +} // namespace OHOS::FileManagement::Backup::SAUtils #endif // OHOS_FILEMGMT_BACKUP_B_SA_H \ No newline at end of file diff --git a/utils/include/b_utils/b_time.h b/utils/include/b_utils/b_time.h new file mode 100644 index 000000000..77560a7b4 --- /dev/null +++ b/utils/include/b_utils/b_time.h @@ -0,0 +1,28 @@ +/* + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_FILEMGMT_BACKUP_B_TIME_H +#define OHOS_FILEMGMT_BACKUP_B_TIME_H +#include + +namespace OHOS::FileManagement::Backup { +class TimeUtils { +public: + static int64_t GetTimeS(); + static int64_t GetTimeMS(); + static int64_t GetTimeUS(); +}; +} // namespace OHOS::FileManagement::TimeUtils +#endif // OHOS_FILEMGMT_BACKUP_B_TIME_H \ No newline at end of file diff --git a/utils/src/b_utils/b_time.cpp b/utils/src/b_utils/b_time.cpp new file mode 100644 index 000000000..6656cf582 --- /dev/null +++ b/utils/src/b_utils/b_time.cpp @@ -0,0 +1,40 @@ +/* + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "b_utils/b_time.h" +#include + +namespace OHOS::FileManagement::Backup { +int64_t TimeUtils::GetTimeS() +{ + std::chrono::seconds nowS = std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()); + return nowS.count(); +} + +int64_t TimeUtils::GetTimeMS() +{ + std::chrono::milliseconds nowMs = std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()); + return nowMs.count(); +} + +int64_t TimeUtils::GetTimeUS() +{ + std::chrono::microseconds nowUs = std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()); + return nowUs.count(); +} +} // namespace OHOS::FileManagement::Backup \ No newline at end of file