diff --git a/frameworks/native/backup_ext/include/ext_extension.h b/frameworks/native/backup_ext/include/ext_extension.h index 68184723ea589a1ec62e7e8eed9e53b6813f6448..f6a0578734a4ae8fd221dad1ce973b693663a162 100644 --- a/frameworks/native/backup_ext/include/ext_extension.h +++ b/frameworks/native/backup_ext/include/ext_extension.h @@ -168,6 +168,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 * diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index c75e71b562e1e90369c7c02673d407b9beb748ed..14fb5beeb872f3060e8e46b2e3b2f8f17f320951 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -752,6 +752,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); @@ -1195,6 +1203,14 @@ void BackupExtExtension::AsyncTaskRestoreForUpgrade() 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 callBackupEx = ptr->RestoreResultCallbackEx(obj); ErrCode err = ptr->extension_->OnRestore(callBackup, callBackupEx); if (err != ERR_OK) { @@ -1252,6 +1268,14 @@ void BackupExtExtension::AsyncTaskIncrementalRestoreForUpgrade() 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 callBackupEx = ptr->IncRestoreResultCallbackEx(obj); ErrCode err = ptr->extension_->OnRestore(callBackup, callBackupEx); if (err != ERR_OK) { @@ -1342,6 +1366,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__); @@ -1689,6 +1735,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) { diff --git a/frameworks/native/backup_kit_inner/src/service_proxy.cpp b/frameworks/native/backup_kit_inner/src/service_proxy.cpp index 1d004e4f16e346181588c601b4cbfce81328748d..0187729c399c6828118ef0048759237505a60dff 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 49348fb58591984d27a328c01487941ac9d8e4ea..6e7b7d4c70da7a09c06dc7a90d4a90fa747745e2 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 1de4b9c45934275547880b510fcdbaed86101a9b..f8dfcc67960f91d12e724d45ed4192e8d2bdcf56 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 d0845087ee63d5148626174e238c488aafeb1f2b..9eab1671e6e4a8bd23e6a274155c8508be2fc103 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 593c67a597cf7d0d1da81c8f7fcbeab8950c75ae..6e27a070019529bb28fad8bb0f91d44f9f777f6c 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 45a9393e5a3bd987ec3eda79f67aa9377b924302..654c1f59e53665308fc9fc505b678c5f3d8ee5f2 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); diff --git a/services/backup_sa/include/module_ipc/service_stub.h b/services/backup_sa/include/module_ipc/service_stub.h index bd40fdd1ea400b1baa92db978454e426720eb89b..564b4539c8fe7e39d4aa590543fafc60aae7493c 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 7baf1cabb497f8fae39e39ea406e3adae9220071..676d2d2052bee9b883ddc8dd1d362afbb31d6699 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 + */ + 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 */ - void BundleExtTimerStart(const std::string &bundleName, const Utils::Timer::TimerCallback &callback); + 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 8f65b2178ea21c37dd667fcfed57bb84392eeb38..fe1f59ae81573b5d4f4028b9b5986c162b1d88a9 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(); @@ -1213,13 +1217,13 @@ void Service::ExtConnectDone(string bundleName) HILOGE("lock sa connection ptr is nullptr"); return; } - sessionPtr->BundleExtTimerStop(bundleName); saConnection->DisconnectBackupSAExt(); } else { auto sessionConnection = sessionPtr->GetExtConnection(bundleName); - sessionPtr->BundleExtTimerStop(bundleName); sessionConnection->DisconnectBackupExtAbility(); } + sessionPtr->StopFwkTimer(bundleName); + sessionPtr->StopExtTimer(bundleName); thisPtr->ClearSessionAndSchedInfo(bundleName); thisPtr->NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_TIMEOUT)); } catch (...) { @@ -1230,7 +1234,12 @@ void Service::ExtConnectDone(string bundleName) }; try { HILOGE("begin %{public}s", bundleName.data()); - session_->BundleExtTimerStart(bundleName, timeoutCallback); + 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 +1583,106 @@ ErrCode Service::GetBackupInfo(BundleName &bundleName, std::string &result) } } +ErrCode Service::StartExtTimer(bool &isExtStart) +{ + string bundleName = VerifyCallerAndGetCallerName(); + auto timeoutCallback = [ptr {wptr(this)}, bundleName]() { + HILOGE("Backup <%{public}s> Extension Process Timeout", bundleName.c_str()); + auto thisPtr = ptr.promote(); + if (!thisPtr) { + HILOGW("this pointer is null."); + return; + } + auto sessionPtr = ptr->session_; + if (sessionPtr == nullptr) { + HILOGW("SessionPtr is null."); + return; + } + try { + auto sessionConnection = sessionPtr->GetExtConnection(bundleName); + sessionPtr->StopFwkTimer(bundleName); + sessionPtr->StopExtTimer(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()); + sessionPtr->StopFwkTimer(bundleName); + sessionPtr->StopExtTimer(bundleName); + thisPtr->ClearSessionAndSchedInfo(bundleName); + thisPtr->NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_TIMEOUT)); + } + }; + try { + HILOGI("Service::StartExtTimer begin."); + if (session_ == nullptr) { + HILOGE("StartExtTimer error, session_ is nullptr."); + isExtStart = false; + return BError(BError::Codes::SA_INVAL_ARG); + } + session_->IncreaseSessionCnt(); + 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) +{ + string bundleName = VerifyCallerAndGetCallerName(); + auto timeoutCallback = [ptr {wptr(this)}, bundleName]() { + HILOGE("Backup <%{public}s> Extension Process Timeout", bundleName.c_str()); + auto thisPtr = ptr.promote(); + if (!thisPtr) { + HILOGW("this pointer is null."); + return; + } + auto sessionPtr = ptr->session_; + if (sessionPtr == nullptr) { + HILOGW("SessionPtr is null."); + return; + } + try { + auto sessionConnection = sessionPtr->GetExtConnection(bundleName); + sessionPtr->StopFwkTimer(bundleName); + sessionPtr->StopExtTimer(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()); + sessionPtr->StopFwkTimer(bundleName); + sessionPtr->StopExtTimer(bundleName); + thisPtr->ClearSessionAndSchedInfo(bundleName); + thisPtr->NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_TIMEOUT)); + } + }; + try { + HILOGI("Service::StartFwkTimer begin."); + if (session_ == nullptr) { + HILOGE("StartFwkTimer error, session_ is nullptr."); + isFwkStart = false; + return BError(BError::Codes::SA_INVAL_ARG); + } + session_->IncreaseSessionCnt(); + 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]() { @@ -1588,9 +1697,10 @@ ErrCode Service::UpdateTimer(BundleName &bundleName, uint32_t timeOut, bool &res HILOGW("SessionPtr is null."); return; } + sessionPtr->StopFwkTimer(bundleName); + sessionPtr->StopExtTimer(bundleName); try { auto sessionConnection = sessionPtr->GetExtConnection(bundleName); - sessionPtr->BundleExtTimerStop(bundleName); sessionConnection->DisconnectBackupExtAbility(); thisPtr->ClearSessionAndSchedInfo(bundleName); thisPtr->NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_TIMEOUT)); @@ -1722,7 +1832,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); } diff --git a/services/backup_sa/src/module_ipc/service_incremental.cpp b/services/backup_sa/src/module_ipc/service_incremental.cpp index 7df256035fad13833d418ac4cea476cc2edeba3b..deb63ff3cfd57d693b892709645f62cc732a952f 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 bd6d1be91a849d22d0c7c3cb718783bdaeb34bbf..0fa7782c65649ddf054813a8ce1f20fc544830ac 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 20bb48b24bbc56b798a58b4766ac2780dd907fc2..d2f515e613ec8fa09e05befd04d9bd205dd83ea3 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); + 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.timerStatus == false) { - it->second.timerStatus = true; - it->second.extTimerId = extBundleTimer.Register(callback, timeout, true); + 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::UpdateTimer(const std::string &bundleName, uint32_t timeOut, - const Utils::Timer::TimerCallback &callback) +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) { - 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.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.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) { + HILOGE("ExtTimer is unregistered, register first."); return true; } - return false; + + 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; } -void SvcSessionManager::BundleExtTimerStop(const std::string &bundleName) +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); + it->second.timeCount += timeOut; + if (it->second.timeCount > BConstants::EXT_MAX_TIMEOUT) { + HILOGE("Over extTimer limit %{public}u(ms), timeout %{public}u(ms),timeCount %{public}u(ms), bundleName " + "%{public}s", BConstants::EXT_MAX_TIMEOUT, timeOut, it->second.timeCount, bundleName.c_str()); + it->second.timeCount -= timeOut; + return false; + } + 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; } + + 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), elapseTime %{public}u(ms), realTimeout %{public}u(ms), " + "bundleName %{public}s ", timeOut, 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 95af50b71d06825b69bb51869b7c205e10128787..8066f122231336fa0a0dd88d84f78960e58f3dcf 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 c058c5e8365192df93ca1661178718dc278033a4..a1cea2b9ac4dd49b3b9c27a42b0bb83f0e19294a 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 53b97b777bb8e73a97fb4ebefb3839d0b1e679d6..dcaf5e5ec6b56ea6193181b37dd3f325c1ec68d3 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 80acd08fcab61190243b8bcf8b9ca84c17845f1a..6176f43dde67d0e1213bc7d3ec67ed4336d0cc2f 100644 --- a/tests/mock/module_ipc/svc_session_manager_throw_mock.cpp +++ b/tests/mock/module_ipc/svc_session_manager_throw_mock.cpp @@ -219,20 +219,30 @@ 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::UpdateTimer(const std::string &bundleName, uint32_t timeOut, - const Utils::Timer::TimerCallback &callback) +bool SvcSessionManager::StopFwkTimer(const std::string &bundleName) { - return BackupSvcSessionManager::session->UpdateTimer(bundleName, timeOut, callback); + return BackupSvcSessionManager::session->StopFwkTimer(bundleName); +} + +bool SvcSessionManager::StartExtTimer(const std::string &bundleName, const Utils::Timer::TimerCallback &callback) +{ + return BackupSvcSessionManager::session->StartExtTimer(bundleName, callback); } -void SvcSessionManager::BundleExtTimerStop(const std::string &bundleName) +bool SvcSessionManager::StopExtTimer(const std::string &bundleName) { - BackupSvcSessionManager::session->BundleExtTimerStop(bundleName); + return BackupSvcSessionManager::session->StopExtTimer(bundleName); +} + +bool SvcSessionManager::UpdateTimer(const std::string &bundleName, uint32_t timeOut, + const Utils::Timer::TimerCallback &callback) +{ + return BackupSvcSessionManager::session->UpdateTimer(bundleName, timeOut, callback); } void SvcSessionManager::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 f00d6e93d4d97c69f78c75855a6aa1151799b0f8..a916d7c6a2bed20792fd5b3dffca59991d29f91f 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 0a0ffa7e8217f72fd5116723bb041dc0093140a3..7b6f60248726096bf0ee4c7b9c40288d4e88af4b 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 307c3c7e0eec0cb3a872148766bcde9da890d7f7..d241322b0650369b8fb36927adf0c3a3d6f990bb 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 06dc18af33722d59bdebe3c3a1c0a98a8bd7613b..a2137563413daeecd47b0d201a48a4f3a1ea0ebb 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 @@ -702,8 +669,9 @@ HWTEST_F(ServiceThrowTest, SUB_Service_throw_ExtConnectDone_0100, testing::ext:: try { EXPECT_NE(service, nullptr); string bundleName; - EXPECT_CALL(*sessionMock, BundleExtTimerStart(_, _)).WillOnce(Invoke([]() { + EXPECT_CALL(*sessionMock, StartFwkTimer(_, _)).WillOnce(Invoke([]() { throw BError(BError::Codes::EXT_THROW_EXCEPTION); + return true; })); EXPECT_CALL(*sessionMock, RemoveExtInfo(_)).WillOnce(Invoke([]() { throw BError(BError::Codes::EXT_THROW_EXCEPTION); 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 411ade2de2282c038bfedaa4abb03516d5ab90a7..db47f82813442d4b7df46559c93037493f51196f 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,43 @@ 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); + sessionManagerPtr_->StartFwkTimer(BUNDLE_NAME, callback); EXPECT_TRUE(true); - 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); + sessionManagerPtr_->StartFwkTimer(BUNDLE_NAME, callback); EXPECT_TRUE(true); } 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,24 +1757,20 @@ 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); - } + EXPECT_TRUE(sessionManagerPtr_ != nullptr); + sessionManagerPtr_->impl_.clientToken = 0; + auto ret = sessionManagerPtr_->UpdateTimer(BUNDLE_NAME, 30, callback); + EXPECT_FALSE(ret); BackupExtInfo info; - info.timerStatus = false; + info.extTimerStatus = false; sessionManagerPtr_->impl_.clientToken = CLIENT_TOKEN_ID; sessionManagerPtr_->impl_.backupExtNameMap.clear(); sessionManagerPtr_->impl_.backupExtNameMap[BUNDLE_NAME] = info; - auto ret = sessionManagerPtr_->UpdateTimer(BUNDLE_NAME, 30, callback); - EXPECT_FALSE(ret); + ret = sessionManagerPtr_->UpdateTimer(BUNDLE_NAME, 30, callback); + EXPECT_TRUE(ret); - info.timerStatus = true; + info.extTimerStatus = true; sessionManagerPtr_->impl_.clientToken = CLIENT_TOKEN_ID; sessionManagerPtr_->impl_.backupExtNameMap.clear(); sessionManagerPtr_->impl_.backupExtNameMap[BUNDLE_NAME] = info; @@ -1792,46 +1784,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 +1836,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 +1845,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 7220842562c3aafab49350bf5e33da678c37ed05..917725a54f4b0ddd87bc4776f356b6f442dd284d 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 151ab2480952d44f0d996e643939aa1ccf607bb8..1ff282364003bb5634aeb68643db99d54c8dcc05 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 7d85e5f907475e13c556b03a6878db74ed8ff9fe..4f994e7cf5e2cc217486901914feb8d86764aaf2 100644 --- a/utils/include/b_resources/b_constants.h +++ b/utils/include/b_resources/b_constants.h @@ -77,6 +77,8 @@ 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; +constexpr uint32_t EXT_MAX_TIMEOUT = 4 * H2MS; // 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 86d1b8aa995db8ee53eb0b40000ac358607531d1..b8d3edf4cb048d937f4a5b73fd27ad6ce7fcea33 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 0000000000000000000000000000000000000000..77560a7b4037fd5fac7fef4af3b3b3fd2b9700c9 --- /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 0000000000000000000000000000000000000000..1cc77172118f3caf0f0f16cd31742156d3b26df2 --- /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::microseconds nowS = std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()); + return nowS.count(); +} + +int64_t TimeUtils::GetTimeMS() +{ + std::chrono::microseconds 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