diff --git a/framework/js/napi/common/src/napi_utils.cpp b/framework/js/napi/common/src/napi_utils.cpp index a05f680cb27111bd0a50d6a3ac9fd8dbe1614d20..382b8bc9f57a55f642c54308d9fbd8dcb6c40332 100644 --- a/framework/js/napi/common/src/napi_utils.cpp +++ b/framework/js/napi/common/src/napi_utils.cpp @@ -32,6 +32,7 @@ int32_t NapiUtils::ConvertErrorCode(int32_t timeErrorCode) return JsErrorCode::SYSTEM_APP_ERROR; case MiscServices::E_TIME_NO_PERMISSION: return JsErrorCode::PERMISSION_ERROR; + case MiscServices::E_TIME_AUTO_RESTORE_ERROR: case MiscServices::E_TIME_PARAMETERS_INVALID: return JsErrorCode::PARAMETER_ERROR; case MiscServices::E_TIME_NTP_UPDATE_FAILED: diff --git a/framework/js/napi/system_timer/include/napi_system_timer.h b/framework/js/napi/system_timer/include/napi_system_timer.h index 5f06c31e485d42c9597cc89325c090432c81d9eb..38f8b819efe3c25a06439e132aac38767e22b81c 100644 --- a/framework/js/napi/system_timer/include/napi_system_timer.h +++ b/framework/js/napi/system_timer/include/napi_system_timer.h @@ -37,6 +37,7 @@ public: virtual void SetRepeat(bool repeat) override; virtual void SetInterval(const uint64_t &interval) override; virtual void SetWantAgent(std::shared_ptr wantAgent) override; + void SetAutoRestore(bool _autoRestore); void SetCallbackInfo(const napi_env &env, const napi_ref &ref); private: diff --git a/framework/js/napi/system_timer/src/napi_system_timer.cpp b/framework/js/napi/system_timer/src/napi_system_timer.cpp index e8a0017f2d757ed49f9c78295a6ac7666af0f718..d94fc314cb83c27c7fc3edda67fcd7b6cf572abe 100644 --- a/framework/js/napi/system_timer/src/napi_system_timer.cpp +++ b/framework/js/napi/system_timer/src/napi_system_timer.cpp @@ -94,6 +94,10 @@ void ITimerInfoInstance::SetInterval(const uint64_t &_interval) { interval = _interval; } +void ITimerInfoInstance::SetAutoRestore(bool _autoRestore) +{ + autoRestore = _autoRestore; +} void ITimerInfoInstance::SetWantAgent(std::shared_ptr _wantAgent) { wantAgent = _wantAgent; @@ -124,6 +128,7 @@ napi_value NapiSystemTimer::SystemTimerInit(napi_env env, napi_value exports) std::map PARA_NAPI_TYPE_MAP = { { "type", napi_number }, { "repeat", napi_boolean }, + { "autoRestore", napi_boolean }, { "interval", napi_number }, { "wantAgent", napi_object }, { "callback", napi_function }, @@ -132,6 +137,7 @@ std::map PARA_NAPI_TYPE_MAP = { std::map NAPI_TYPE_STRING_MAP = { { "type", "number" }, { "repeat", "boolean" }, + { "autoRestore", "boolean" }, { "interval", "number" }, { "wantAgent", "object" }, { "callback", "function" }, @@ -156,6 +162,10 @@ void ParseTimerOptions(napi_env env, ContextBase *context, std::string paraType, bool repeat = false; napi_get_value_bool(env, result, &repeat); iTimerInfoInstance->SetRepeat(repeat); + } else if (paraType == "autoRestore") { + bool autoRestore = false; + napi_get_value_bool(env, result, &autoRestore); + iTimerInfoInstance->SetAutoRestore(autoRestore); } else if (paraType == "interval") { int64_t interval = 0; napi_get_value_int64(env, result, &interval); @@ -195,6 +205,12 @@ void NapiSystemTimer::GetTimerOptions(const napi_env &env, ContextBase *context, ParseTimerOptions(env, context, "repeat", value, iTimerInfoInstance); CHECK_STATUS_RETURN_VOID(TIME_MODULE_JS_NAPI, context, context->errMessage, JsErrorCode::PARAMETER_ERROR); + // autoRestore?: boolean + napi_has_named_property(env, value, "autoRestore", &hasProperty); + if (hasProperty) { + ParseTimerOptions(env, context, "autoRestore", value, iTimerInfoInstance); + CHECK_STATUS_RETURN_VOID(TIME_MODULE_JS_NAPI, context, context->errMessage, JsErrorCode::PARAMETER_ERROR); + } // interval?: number napi_has_named_property(env, value, "interval", &hasProperty); if (hasProperty) { diff --git a/interfaces/inner_api/include/itimer_info.h b/interfaces/inner_api/include/itimer_info.h index 8e2ae6007bc9f8862603953f404e16a3a8ac5e11..16cde63fbf83bc9335fa4dca3ff03f82c03755b7 100644 --- a/interfaces/inner_api/include/itimer_info.h +++ b/interfaces/inner_api/include/itimer_info.h @@ -30,6 +30,7 @@ public: int type; bool repeat; bool disposable = false; + bool autoRestore = false; uint64_t interval; std::shared_ptr wantAgent; @@ -83,12 +84,23 @@ public: * @para: _disposable bool * true: the timer will be destoryed automaticly when it is triggered. * But do not take effect for repeat timer. - * fasle: the timer need to be destroyed by client + * false: the timer need to be destroyed by client */ void SetDisposable(const bool &_disposable) { disposable = _disposable; } + + /** + * SetAutoRestore set timer restored upon reboot + * @para: _autoRestore bool + * true: the timer will be restored when the device reboots. + * false: the timer won't be restored when the device reboots. + */ + void SetAutoRestore(bool _autoRestore) + { + autoRestore = _autoRestore; + } virtual void SetWantAgent(std::shared_ptr wantAgent) = 0; virtual void OnTrigger() = 0; }; diff --git a/services/ipc/proxy/time_service_proxy.cpp b/services/ipc/proxy/time_service_proxy.cpp index dc7cb3359ad9eeec6961e3b839142bc444e12214..cfd01ec20ed0ee38bdbdf200729ae6761ec65e1b 100644 --- a/services/ipc/proxy/time_service_proxy.cpp +++ b/services/ipc/proxy/time_service_proxy.cpp @@ -74,6 +74,10 @@ int32_t TimeServiceProxy::CreateTimer(const std::shared_ptr &timerOp TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write disposable"); return E_TIME_WRITE_PARCEL_ERROR; } + if (!data.WriteBool(timerOptions->autoRestore)) { + TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write autoRestore"); + return E_TIME_WRITE_PARCEL_ERROR; + } if (!data.WriteUint64(timerOptions->interval)) { TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write interval"); return E_TIME_WRITE_PARCEL_ERROR; diff --git a/services/ipc/stub/time_service_stub.cpp b/services/ipc/stub/time_service_stub.cpp index f645d897b91f8a4ce2e67fa7d6381d8c5064c98e..d7d8cab8a0020c64770649c10ea73c82d1272866 100644 --- a/services/ipc/stub/time_service_stub.cpp +++ b/services/ipc/stub/time_service_stub.cpp @@ -201,6 +201,7 @@ int32_t TimeServiceStub::OnCreateTimer(MessageParcel &data, MessageParcel &reply auto type = data.ReadInt32(); auto repeat = data.ReadBool(); auto disposable = data.ReadBool(); + auto autoRestore = data.ReadBool(); auto interval = data.ReadUint64(); if (data.ReadBool()) { wantAgent = std::shared_ptr( @@ -220,6 +221,7 @@ int32_t TimeServiceStub::OnCreateTimer(MessageParcel &data, MessageParcel &reply timerOptions->repeat = repeat; timerOptions->interval = interval; timerOptions->disposable = disposable; + timerOptions->autoRestore = autoRestore; timerOptions->wantAgent = wantAgent; uint64_t timerId = data.ReadUint64(); auto errCode = CreateTimer(timerOptions, obj, timerId); diff --git a/services/time_system_ability.cpp b/services/time_system_ability.cpp index a437422eb970cc375dd5af4355475ded1c4056ba..43c47338f291110a15fd0760c22b11a3a1f035ee 100644 --- a/services/time_system_ability.cpp +++ b/services/time_system_ability.cpp @@ -343,11 +343,11 @@ void TimeSystemAbility::OnStop() void TimeSystemAbility::ParseTimerPara(const std::shared_ptr &timerOptions, TimerPara ¶s) { auto uIntType = static_cast(timerOptions->type); - auto disposable = timerOptions->disposable; bool isRealtime = (uIntType & TIMER_TYPE_REALTIME_MASK) > 0; bool isWakeup = (uIntType & TIMER_TYPE_REALTIME_WAKEUP_MASK) > 0; paras.windowLength = (uIntType & TIMER_TYPE_EXACT_MASK) > 0 ? 0 : -1; paras.flag = (uIntType & TIMER_TYPE_EXACT_MASK) > 0 ? 1 : 0; + paras.autoRestore = timerOptions->autoRestore; if (isRealtime && isWakeup) { paras.timerType = ITimerManager::TimerType::ELAPSED_REALTIME_WAKEUP; } else if (isRealtime) { @@ -358,17 +358,26 @@ void TimeSystemAbility::ParseTimerPara(const std::shared_ptr &timerO paras.timerType = ITimerManager::TimerType::RTC; } if ((uIntType & TIMER_TYPE_IDLE_MASK) > 0) { - paras.flag += ITimerManager::TimerFlag::IDLE_UNTIL; + paras.flag |= ITimerManager::TimerFlag::IDLE_UNTIL; } if ((uIntType & TIMER_TYPE_INEXACT_REMINDER_MASK) > 0) { - paras.flag += ITimerManager::TimerFlag::INEXACT_REMINDER; + paras.flag |= ITimerManager::TimerFlag::INEXACT_REMINDER; } - if (disposable) { - paras.flag += ITimerManager::TimerFlag::IS_DISPOSABLE; + if (timerOptions->disposable) { + paras.flag |= ITimerManager::TimerFlag::IS_DISPOSABLE; } paras.interval = timerOptions->repeat ? timerOptions->interval : 0; } +int32_t TimeSystemAbility::CheckTimerPara(const DatabaseType type, const TimerPara ¶s) +{ + if (paras.autoRestore && (paras.timerType == ITimerManager::TimerType::ELAPSED_REALTIME || + paras.timerType == ITimerManager::TimerType::ELAPSED_REALTIME_WAKEUP || type == DatabaseType::NOT_STORE)) { + return E_TIME_AUTO_RESTORE_ERROR; + } + return E_TIME_OK; +} + int32_t TimeSystemAbility::CreateTimer(const std::shared_ptr &timerOptions, sptr &obj, uint64_t &timerId) { @@ -381,8 +390,19 @@ int32_t TimeSystemAbility::CreateTimer(const std::shared_ptr &timerO TIME_HILOGE(TIME_MODULE_SERVICE, "ITimerCallback nullptr."); return E_TIME_NULLPTR; } + auto type = DatabaseType::NOT_STORE; + if (timerOptions->wantAgent != nullptr) { + type = DatabaseType::STORE; + } + int uid = IPCSkeleton::GetCallingUid(); + int pid = IPCSkeleton::GetCallingPid(); struct TimerPara paras {}; ParseTimerPara(timerOptions, paras); + int32_t res = CheckTimerPara(type, paras); + if (res != E_TIME_OK) { + TIME_HILOGE(TIME_MODULE_SERVICE, "check para err:%{public}d,uid:%{public}d", res, uid); + return res; + } auto timerManager = TimerManager::GetInstance(); if (timerManager == nullptr) { return E_TIME_NULLPTR; @@ -403,14 +423,8 @@ int32_t TimeSystemAbility::CreateTimer(const std::shared_ptr &timerO if ((static_cast(paras.flag) & static_cast(ITimerManager::TimerFlag::IDLE_UNTIL)) > 0 && !TimePermission::CheckProxyCallingPermission()) { TIME_HILOGW(TIME_MODULE_SERVICE, "App not support create idle timer."); - paras.flag = 0; + paras.flag &= ~ITimerManager::TimerFlag::IDLE_UNTIL; } - auto type = DatabaseType::NOT_STORE; - if (timerOptions->wantAgent != nullptr) { - type = DatabaseType::STORE; - } - int uid = IPCSkeleton::GetCallingUid(); - int pid = IPCSkeleton::GetCallingPid(); return timerManager->CreateTimer(paras, callbackFunc, timerOptions->wantAgent, uid, pid, timerId, type); } @@ -976,7 +990,7 @@ bool TimeSystemAbility::RecoverTimer() int count; holdResultSet->GetRowCount(count); TIME_HILOGI(TIME_MODULE_SERVICE, "hold result rows count: %{public}d", count); - RecoverTimerInner(holdResultSet); + RecoverTimerInner(holdResultSet, true); } if (holdResultSet != nullptr) { holdResultSet->Close(); @@ -990,7 +1004,7 @@ bool TimeSystemAbility::RecoverTimer() int count; dropResultSet->GetRowCount(count); TIME_HILOGI(TIME_MODULE_SERVICE, "drop result rows count: %{public}d", count); - RecoverTimerInner(dropResultSet); + RecoverTimerInner(dropResultSet, false); } if (dropResultSet != nullptr) { dropResultSet->Close(); @@ -998,7 +1012,7 @@ bool TimeSystemAbility::RecoverTimer() return true; } -void TimeSystemAbility::RecoverTimerInner(std::shared_ptr resultSet) +void TimeSystemAbility::RecoverTimerInner(std::shared_ptr resultSet, bool autoRestore) { auto timerManager = TimerManager::GetInstance(); if (timerManager == nullptr) { @@ -1017,6 +1031,8 @@ void TimeSystemAbility::RecoverTimerInner(std::shared_ptr(GetLong(resultSet, 4)), // Line 2 is 'flag' GetInt(resultSet, 2), + // autoRestore depends on the table type + autoRestore, // Callback can't recover. nullptr, // Line 7 is 'wantAgent' diff --git a/services/time_system_ability.h b/services/time_system_ability.h index f09f446a9f4c891a89ae7d77749a7f0b90790512..2cd8b2934f95ff5670a4ae6a9b92ee2a2f3ad445 100644 --- a/services/time_system_ability.h +++ b/services/time_system_ability.h @@ -102,6 +102,7 @@ private: int32_t Init(); void ParseTimerPara(const std::shared_ptr &timerOptions, TimerPara ¶s); + int32_t CheckTimerPara(const DatabaseType type, const TimerPara ¶s); bool GetTimeByClockId(clockid_t clockId, struct timespec &tv); int SetRtcTime(time_t sec); bool CheckRtc(const std::string &rtcPath, uint64_t rtcId); @@ -113,7 +114,7 @@ private: void RegisterPackageRemovedSubscriber(); void RegisterOsAccountSubscriber(); bool IsValidTime(int64_t time); - void RecoverTimerInner(std::shared_ptr resultSet); + void RecoverTimerInner(std::shared_ptr resultSet, bool autoRestore); void SetAutoReboot(); ServiceRunningState state_; diff --git a/services/timer/include/timer_info.h b/services/timer/include/timer_info.h index f87cf6f8460673172f2879b6e7d878606207e584..5541d2e5ce8aaecaee09556f1958d739393dad9e 100644 --- a/services/timer/include/timer_info.h +++ b/services/timer/include/timer_info.h @@ -31,6 +31,7 @@ public: const int type; const std::chrono::milliseconds origWhen; const bool wakeup; + const bool autoRestore; const std::function callback; const std::shared_ptr wantAgent; const uint32_t flags; @@ -56,6 +57,7 @@ public: std::function callback, std::shared_ptr wantAgent, uint32_t flags, + bool autoRestore, int uid, int pid, const std::string &bundleName); diff --git a/services/timer/include/timer_manager.h b/services/timer/include/timer_manager.h index 85a0fb15de2dd6366c026addec0a7b57a988a98b..458a8e6c9a1ed1c8276f19085e2923a4e0124d49 100644 --- a/services/timer/include/timer_manager.h +++ b/services/timer/include/timer_manager.h @@ -79,6 +79,7 @@ private: int64_t windowLength, uint64_t interval, int flag, + bool autoRestore, std::function callback, std::shared_ptr wantAgent, int uid, @@ -94,6 +95,7 @@ private: std::function callback, const std::shared_ptr &wantAgent, uint32_t flags, + bool autoRestore, uint64_t callingUid, uint64_t callingPid, const std::string &bundleName); @@ -127,7 +129,6 @@ private: bool AdjustDeliveryTimeBasedOnDeviceIdle(const std::shared_ptr &alarm); bool AdjustTimersBasedOnDeviceIdle(); void HandleRepeatTimer(const std::shared_ptr &timer, std::chrono::steady_clock::time_point nowElapsed); - inline bool CheckNeedRecoverOnReboot(std::string bundleName, int type); #ifdef POWER_MANAGER_ENABLE void HandleRunningLock(const std::shared_ptr &firstWakeup); void AddRunningLock(long long holdLockTime); diff --git a/services/timer/include/timer_manager_interface.h b/services/timer/include/timer_manager_interface.h index 6c3dd44cd13114215b9817796c97e69910e44f3b..5e0e067b7b65bb7de4e601c2ad44dac0aee816a5 100644 --- a/services/timer/include/timer_manager_interface.h +++ b/services/timer/include/timer_manager_interface.h @@ -29,6 +29,7 @@ struct TimerEntry { int64_t windowLength; uint64_t interval; int flag; + bool autoRestore; std::function callback; std::shared_ptr wantAgent; int uid; diff --git a/services/timer/src/timer_info.cpp b/services/timer/src/timer_info.cpp index 9ad408a5ea8034b0f892f72044c2590d0dd977ae..2555c1c1d1214cd1c39dee0aaaa02d8f2ca75809 100644 --- a/services/timer/src/timer_info.cpp +++ b/services/timer/src/timer_info.cpp @@ -40,6 +40,7 @@ TimerInfo::TimerInfo(uint64_t _id, int _type, std::function _callback, std::shared_ptr _wantAgent, uint32_t _flags, + bool _autoRestore, int _uid, int _pid, const std::string &_bundleName) @@ -47,6 +48,7 @@ TimerInfo::TimerInfo(uint64_t _id, int _type, type {_type}, origWhen {_when}, wakeup {_type == ITimerManager::ELAPSED_REALTIME_WAKEUP || _type == ITimerManager::RTC_WAKEUP}, + autoRestore {_autoRestore}, callback {std::move(_callback)}, wantAgent {_wantAgent}, flags {_flags}, diff --git a/services/timer/src/timer_manager.cpp b/services/timer/src/timer_manager.cpp index 63b864360a0a7bc7690439ae9ec08554c10a307b..791a3642f744feae2f6fa43f010493d80fbb4559 100644 --- a/services/timer/src/timer_manager.cpp +++ b/services/timer/src/timer_manager.cpp @@ -178,6 +178,7 @@ int32_t TimerManager::CreateTimer(TimerPara ¶s, paras.windowLength, paras.interval, paras.flag, + paras.autoRestore, std::move(callback), wantAgent, uid, @@ -189,7 +190,7 @@ int32_t TimerManager::CreateTimer(TimerPara ¶s, } if (type == NOT_STORE) { return E_TIME_OK; - } else if (CheckNeedRecoverOnReboot(bundleName, paras.timerType)) { + } else if (paras.autoRestore) { TimeDatabase::GetInstance().Insert(std::string(HOLD_ON_REBOOT), GetInsertValues(timerInfo, paras)); } else { @@ -229,12 +230,10 @@ int32_t TimerManager::StartTimer(uint64_t timerId, uint64_t triggerTime) RemoveLocked(timerId, false); } SetHandler(timerInfo->id, timerInfo->type, triggerTime, timerInfo->windowLength, timerInfo->interval, - timerInfo->flag, timerInfo->callback, timerInfo->wantAgent, timerInfo->uid, timerInfo->pid, - timerInfo->bundleName); + timerInfo->flag, timerInfo->autoRestore, timerInfo->callback, timerInfo->wantAgent, timerInfo->uid, + timerInfo->pid, timerInfo->bundleName); } - auto tableName = (CheckNeedRecoverOnReboot(timerInfo->bundleName, timerInfo->type) - ? HOLD_ON_REBOOT - : DROP_ON_REBOOT); + auto tableName = (timerInfo->autoRestore ? HOLD_ON_REBOOT : DROP_ON_REBOOT); OHOS::NativeRdb::ValuesBucket values; values.PutInt("state", 1); values.PutLong("triggerTime", static_cast(triggerTime)); @@ -334,7 +333,7 @@ int32_t TimerManager::StopTimerInner(uint64_t timerNumber, bool needDestroy) TimerProxy::GetInstance().RemovePidProxy(timerNumber, it->second->pid); TimerProxy::GetInstance().EraseTimerFromProxyUidMap(timerNumber, it->second->uid); TimerProxy::GetInstance().EraseTimerFromProxyPidMap(timerNumber, it->second->pid); - needRecoverOnReboot = CheckNeedRecoverOnReboot(it->second->bundleName, it->second->type); + needRecoverOnReboot = it->second->autoRestore; if (needDestroy) { int uid = it->second->uid; timerEntryMap_.erase(it); @@ -373,6 +372,7 @@ void TimerManager::SetHandler(uint64_t id, int64_t windowLength, uint64_t interval, int flag, + bool autoRestore, std::function callback, std::shared_ptr wantAgent, int uid, @@ -416,6 +416,7 @@ void TimerManager::SetHandler(uint64_t id, std::move(callback), wantAgent, static_cast(flag), + autoRestore, uid, pid, bundleName); @@ -430,13 +431,14 @@ void TimerManager::SetHandlerLocked(uint64_t id, int type, std::function callback, const std::shared_ptr &wantAgent, uint32_t flags, + bool autoRestore, uint64_t callingUid, uint64_t callingPid, const std::string &bundleName) { TIME_HILOGD(TIME_MODULE_SERVICE, "start id: %{public}" PRId64 "", id); auto alarm = std::make_shared(id, type, when, whenElapsed, windowLength, maxWhen, - interval, std::move(callback), wantAgent, flags, callingUid, + interval, std::move(callback), wantAgent, flags, autoRestore, callingUid, callingPid, bundleName); if (TimerProxy::GetInstance().IsUidProxy(alarm->uid)) { TIME_HILOGI(TIME_MODULE_SERVICE, "Timer already proxy, uid=%{public}" PRIu64 " id=%{public}" PRId64 "", @@ -914,10 +916,10 @@ void TimerManager::DeliverTimersLocked(const std::vectorwantAgent) { - if (!NotifyWantAgent(timer) && CheckNeedRecoverOnReboot(timer->bundleName, timer->type)) { + if (!NotifyWantAgent(timer) && timer->autoRestore) { NotifyWantAgentRetry(timer); } - if (CheckNeedRecoverOnReboot(timer->bundleName, timer->type)) { + if (timer->autoRestore) { OHOS::NativeRdb::ValuesBucket values; values.PutInt("state", 0); OHOS::NativeRdb::RdbPredicates rdbPredicates(HOLD_ON_REBOOT); @@ -1360,19 +1362,13 @@ void TimerManager::HandleRepeatTimer( auto nextElapsed = timer->whenElapsed + delta; SetHandlerLocked(timer->id, timer->type, timer->when + delta, nextElapsed, timer->windowLength, MaxTriggerTime(nowElapsed, nextElapsed, timer->repeatInterval), timer->repeatInterval, timer->callback, - timer->wantAgent, timer->flags, timer->uid, timer->pid, timer->bundleName); + timer->wantAgent, timer->flags, timer->autoRestore, timer->uid, timer->pid, timer->bundleName); } else { TimerProxy::GetInstance().RemoveUidTimerMap(timer); TimerProxy::GetInstance().RemovePidTimerMap(timer); } } -inline bool TimerManager::CheckNeedRecoverOnReboot(std::string bundleName, int type) -{ - return (std::find(NEED_RECOVER_ON_REBOOT.begin(), NEED_RECOVER_ON_REBOOT.end(), bundleName) != - NEED_RECOVER_ON_REBOOT.end() && (type == RTC || type == RTC_WAKEUP)); -} - #ifdef POWER_MANAGER_ENABLE // needs to acquire the lock `mutex_` before calling this method void TimerManager::HandleRunningLock(const std::shared_ptr &firstWakeup) diff --git a/test/fuzztest/timemanager_fuzzer/timetesttimer_fuzzer/timer_info.h b/test/fuzztest/timemanager_fuzzer/timetesttimer_fuzzer/timer_info.h index c7f3d61c6b66afce1baf35dd4291588f1eb9b9a7..9b16463d4ba50a4443286fc3f286822a1443a3bb 100644 --- a/test/fuzztest/timemanager_fuzzer/timetesttimer_fuzzer/timer_info.h +++ b/test/fuzztest/timemanager_fuzzer/timetesttimer_fuzzer/timer_info.h @@ -29,6 +29,7 @@ public: void SetRepeat(bool repeat) override; void SetInterval(const uint64_t &interval) override; void SetWantAgent(std::shared_ptr wantAgent) override; + void SetAutoRestore(bool autoRestore); }; TimerInfo::TimerInfo() = default; @@ -52,6 +53,10 @@ void TimerInfo::SetWantAgent(std::shared_ptrwantAgent = wantAgent; } +void TimerInfo::SetAutoRestore(bool autoRestore) +{ + this->autoRestore = autoRestore; +} void TimerInfo::OnTrigger() { } diff --git a/test/fuzztest/timemanager_fuzzer/timetesttimer_fuzzer/timetesttimer_fuzzer.cpp b/test/fuzztest/timemanager_fuzzer/timetesttimer_fuzzer/timetesttimer_fuzzer.cpp index 35c9a395c968c9ef45d71698cc845761e65fc0bb..8130041c50680e2bf5b63b7937b741e593564094 100644 --- a/test/fuzztest/timemanager_fuzzer/timetesttimer_fuzzer/timetesttimer_fuzzer.cpp +++ b/test/fuzztest/timemanager_fuzzer/timetesttimer_fuzzer/timetesttimer_fuzzer.cpp @@ -62,6 +62,7 @@ bool FuzzTimeCreateTimerV9(int64_t data, size_t size) timerInfo->SetType(static_cast(data)); timerInfo->SetInterval(data); timerInfo->SetRepeat(data); + timerInfo->SetAutoRestore(false); uint64_t timerId = 0; TimeServiceClient::GetInstance()->CreateTimerV9(timerInfo, timerId); TimeServiceClient::GetInstance()->DestroyTimer(timerId); diff --git a/test/unittest/service_test/include/timer_info_test.h b/test/unittest/service_test/include/timer_info_test.h index e3104c15599cfd6c5ce52509d0e55c95a1c41cb1..4368916fd0026397a7356bf50fd6640027133a2b 100644 --- a/test/unittest/service_test/include/timer_info_test.h +++ b/test/unittest/service_test/include/timer_info_test.h @@ -41,6 +41,7 @@ public: virtual void OnTrigger() override; virtual void SetType(const int &type) override; virtual void SetRepeat(bool repeat) override; + void SetAutoRestore(bool restore); virtual void SetInterval(const uint64_t &interval) override; void SetDisposable(const bool &disposable); virtual void SetWantAgent(std::shared_ptr wantAgent) override; @@ -98,6 +99,11 @@ void TimerInfoTest::SetDisposable(const bool &_disposable) disposable = _disposable; } +void TimerInfoTest::SetAutoRestore(bool _autoRestore) +{ + autoRestore = _autoRestore; +} + } // namespace MiscServices } // namespace OHOS #endif \ No newline at end of file diff --git a/test/unittest/service_test/src/time_client_test.cpp b/test/unittest/service_test/src/time_client_test.cpp index e701cc63be4d8df80642766724790e3cccaef7e3..b5d0339cad3e66f39a8e8efcb037f6872baf4378 100644 --- a/test/unittest/service_test/src/time_client_test.cpp +++ b/test/unittest/service_test/src/time_client_test.cpp @@ -957,6 +957,7 @@ HWTEST_F(TimeClientTest, StartTimer013, TestSize.Level1) timerInfo->SetType(timerInfo->TIMER_TYPE_EXACT); timerInfo->SetRepeat(false); timerInfo->SetDisposable(true); + timerInfo->SetAutoRestore(false); timerInfo->SetCallbackInfo(TimeOutCallback1); auto errCode = TimeServiceClient::GetInstance()->CreateTimerV9(timerInfo, timerId); EXPECT_EQ(errCode, TimeError::E_TIME_OK); @@ -985,6 +986,7 @@ HWTEST_F(TimeClientTest, StartTimer014, TestSize.Level1) timerInfo->SetRepeat(true); timerInfo->SetInterval(1000); timerInfo->SetDisposable(true); + timerInfo->SetAutoRestore(false); timerInfo->SetCallbackInfo(TimeOutCallback1); auto errCode = TimeServiceClient::GetInstance()->CreateTimerV9(timerInfo, timerId); EXPECT_EQ(errCode, TimeError::E_TIME_OK); diff --git a/test/unittest/service_test/src/time_proxy_test.cpp b/test/unittest/service_test/src/time_proxy_test.cpp index 32e0823396c0c62baf88ff0d5deeb74e6093f150..f7057f744617a17a027438427d1804b626cff3e5 100644 --- a/test/unittest/service_test/src/time_proxy_test.cpp +++ b/test/unittest/service_test/src/time_proxy_test.cpp @@ -805,11 +805,11 @@ HWTEST_F(TimeProxyTest, ProxyTimerCover002, TestSize.Level1) auto duration = std::chrono::milliseconds::zero(); auto timePoint = std::chrono::steady_clock::now(); auto timerInfo1 = std::make_shared(TIMER_ID, 0, duration, timePoint, duration, timePoint, duration, - nullptr, nullptr, 0, UID, 0, ""); + nullptr, nullptr, 0, false, UID, 0, ""); auto res = TimerProxy::GetInstance().CallbackAlarmIfNeed(timerInfo1); EXPECT_EQ(res, E_TIME_OK); auto timerInfo2 = std::make_shared(TIMER_ID + 1, 0, duration, timePoint, duration, timePoint, duration, - nullptr, nullptr, 0, UID, 0, ""); + nullptr, nullptr, 0, false, UID, 0, ""); res = TimerProxy::GetInstance().CallbackAlarmIfNeed(timerInfo2); EXPECT_EQ(res, E_TIME_OK); @@ -861,11 +861,11 @@ HWTEST_F(TimeProxyTest, ProxyTimerCover003, TestSize.Level1) auto duration = std::chrono::milliseconds::zero(); auto timePoint = std::chrono::steady_clock::now(); auto timerInfo1 = std::make_shared(TIMER_ID, 0, duration, timePoint, duration, timePoint, duration, - nullptr, nullptr, 0, 0, PID, ""); + nullptr, nullptr, 0, false, 0, PID, ""); auto res = TimerProxy::GetInstance().CallbackAlarmIfNeed(timerInfo1); EXPECT_EQ(res, E_TIME_OK); auto timerInfo2 = std::make_shared(TIMER_ID + 1, 0, duration, timePoint, duration, timePoint, duration, - nullptr, nullptr, 0, 0, PID, ""); + nullptr, nullptr, 0, false, 0, PID, ""); res = TimerProxy::GetInstance().CallbackAlarmIfNeed(timerInfo2); EXPECT_EQ(res, E_TIME_OK); @@ -907,7 +907,7 @@ HWTEST_F(TimeProxyTest, ProxyTimerCover004, TestSize.Level1) auto duration = std::chrono::milliseconds::zero(); auto timePoint = std::chrono::steady_clock::now(); auto timerInfo = std::make_shared(TIMER_ID, 0, duration, timePoint, duration, timePoint, duration, - nullptr, nullptr, 0, UID, PID, ""); + nullptr, nullptr, 0, false, UID, PID, ""); TimerProxy::GetInstance().RecordUidTimerMap(timerInfo, false); { std::lock_guard lock(TimerProxy::GetInstance().uidTimersMutex_); diff --git a/test/unittest/service_test/src/time_service_test.cpp b/test/unittest/service_test/src/time_service_test.cpp index ffdb25bf6874f636c2b075b99de31a2351534555..46b517ab703aaeeb85fef020d0739ace075ac386 100644 --- a/test/unittest/service_test/src/time_service_test.cpp +++ b/test/unittest/service_test/src/time_service_test.cpp @@ -1105,7 +1105,7 @@ HWTEST_F(TimeServiceTest, TimerManager001, TestSize.Level0) { auto timerId1 = TIMER_ID; auto entry = std::make_shared( - TimerEntry{timerId1, 0, 0, 0, 0, nullptr, nullptr, 0, 0, "bundleName"}); + TimerEntry{timerId1, 0, 0, 0, 0, false, nullptr, nullptr, 0, 0, "bundleName"}); TimerManager::GetInstance()->ReCreateTimer(timerId1, entry); std::lock_guard lock(TimerManager::GetInstance()->entryMapMutex_); @@ -1131,6 +1131,7 @@ HWTEST_F(TimeServiceTest, TimerManager002, TestSize.Level0) 10, 0, 1, + false, nullptr, nullptr, 0, @@ -1167,7 +1168,7 @@ HWTEST_F(TimeServiceTest, TimerManager004, TestSize.Level0) { TimerManager::GetInstance()->DestroyTimer(TIMER_ID); auto entry = std::make_shared( - TimerEntry{TIMER_ID, 0, 0, 0, 0, nullptr, nullptr, UID, PID, "bundleName"}); + TimerEntry{TIMER_ID, 0, 0, 0, 0, false, nullptr, nullptr, UID, PID, "bundleName"}); TimerManager::GetInstance()->ReCreateTimer(TIMER_ID, entry); { @@ -1219,7 +1220,7 @@ HWTEST_F(TimeServiceTest, TimerManager005, TestSize.Level0) auto duration = std::chrono::milliseconds::zero(); auto timePoint = std::chrono::steady_clock::now(); auto timerInfo = std::make_shared(TIMER_ID, 0, duration, timePoint, duration, timePoint, duration, - nullptr, nullptr, 0, 0, 0, ""); + nullptr, nullptr, 0, false, 0, 0, ""); auto res = TimerManager::GetInstance()->NotifyWantAgent(timerInfo); EXPECT_FALSE(res); @@ -1287,7 +1288,7 @@ HWTEST_F(TimeServiceTest, TimerManager007, TestSize.Level0) auto duration = std::chrono::milliseconds::zero(); auto timePoint = std::chrono::steady_clock::now(); auto timerInfo1 = std::make_shared(TIMER_ID, 0, duration, timePoint, duration, timePoint, duration, - nullptr, nullptr, 0, 0, 0, ""); + nullptr, nullptr, 0, false, 0, 0, ""); std::lock_guard lock(TimerManager::GetInstance()->mutex_); auto alarm = TimerManager::GetInstance()->mPendingIdleUntil_; TimerManager::GetInstance()->mPendingIdleUntil_ = timerInfo1; @@ -1301,11 +1302,11 @@ HWTEST_F(TimeServiceTest, TimerManager007, TestSize.Level0) auto duration1 = std::chrono::duration_cast( (timePoint + std::chrono::hours(1)).time_since_epoch()); auto timerInfo2 = std::make_shared(TIMER_ID, 1, duration1, timePoint, duration, timePoint, duration, - nullptr, nullptr, 0, 0, 0, ""); + nullptr, nullptr, 0, false, 0, 0, ""); res = TimerManager::GetInstance()->AdjustDeliveryTimeBasedOnDeviceIdle(timerInfo2); EXPECT_TRUE(res); auto timerInfo3 = std::make_shared(TIMER_ID, 2, duration, timePoint, duration, timePoint, duration, - nullptr, nullptr, 0, 0, 0, ""); + nullptr, nullptr, 0, false, 0, 0, ""); res = TimerManager::GetInstance()->AdjustDeliveryTimeBasedOnDeviceIdle(timerInfo3); EXPECT_TRUE(res); @@ -1333,7 +1334,7 @@ HWTEST_F(TimeServiceTest, TimerManager008, TestSize.Level0) HWTEST_F(TimeServiceTest, TimerManager009, TestSize.Level0) { auto entry = std::make_shared( - TimerEntry{TIMER_ID, 0, 0, 0, 0, nullptr, nullptr, 0, 0, "bundleName"}); + TimerEntry{TIMER_ID, 0, 0, 0, 0, false, nullptr, nullptr, 0, 0, "bundleName"}); TimerManager::GetInstance()->ReCreateTimer(TIMER_ID, entry); uint64_t triggerTime = std::numeric_limits::max(); TimerManager::GetInstance()->StartTimer(TIMER_ID, triggerTime); @@ -1360,13 +1361,13 @@ HWTEST_F(TimeServiceTest, TimerManager010, TestSize.Level0) auto duration = std::chrono::milliseconds::zero(); auto timePoint = std::chrono::steady_clock::now(); auto timerInfo = std::make_shared(TIMER_ID, 0, duration, timePoint, duration, timePoint, duration, - nullptr, nullptr, 0, 0, 0, ""); + nullptr, nullptr, 0, false, 0, 0, ""); { std::lock_guard lock(TimerManager::GetInstance()->mutex_); TimerManager::GetInstance()->mPendingIdleUntil_ = timerInfo; } auto entry = std::make_shared( - TimerEntry{TIMER_ID, 0, 0, 0, 0, nullptr, nullptr, 0, 0, "bundleName"}); + TimerEntry{TIMER_ID, 0, 0, 0, 0, false, nullptr, nullptr, 0, 0, "bundleName"}); TimerManager::GetInstance()->ReCreateTimer(TIMER_ID, entry); TimerManager::GetInstance()->HandleRSSDeath(); auto res = TimerManager::GetInstance()->DestroyTimer(TIMER_ID); @@ -1406,7 +1407,7 @@ HWTEST_F(TimeServiceTest, TimerManager012, TestSize.Level0) } auto entry = std::make_shared( - TimerEntry{TIMER_ID, 0, 0, 0, 0, nullptr, nullptr, UID, 0, "bundleName"}); + TimerEntry{TIMER_ID, 0, 0, 0, 0, false, nullptr, nullptr, UID, 0, "bundleName"}); timerManager->ReCreateTimer(TIMER_ID, entry); timerManager->OnPackageRemoved(UID); @@ -1469,13 +1470,13 @@ HWTEST_F(TimeServiceTest, TimerManager014, TestSize.Level0) uint64_t i = 0; for (; i <= TIMER_ALARM_COUNT; ++i) { auto entry = std::make_shared( - TimerEntry{i, 0, 0, 0, 0, nullptr, nullptr, 0, 0, "bundleName"}); + TimerEntry{i, 0, 0, 0, 0, false, nullptr, nullptr, 0, 0, "bundleName"}); TimerManager::GetInstance()->ReCreateTimer(i, entry); } EXPECT_EQ(TimerManager::GetInstance()->timerOutOfRangeTimes_, 1); for (; i <= TIMER_ALARM_COUNT * 2; ++i) { auto entry = std::make_shared( - TimerEntry{i, 0, 0, 0, 0, nullptr, nullptr, 0, 0, "bundleName"}); + TimerEntry{i, 0, 0, 0, 0, false, nullptr, nullptr, 0, 0, "bundleName"}); TimerManager::GetInstance()->ReCreateTimer(i, entry); } EXPECT_EQ(TimerManager::GetInstance()->timerOutOfRangeTimes_, 2); @@ -1669,7 +1670,7 @@ HWTEST_F(TimeServiceTest, TimerInfo001, TestSize.Level0) auto duration = std::chrono::milliseconds::zero(); auto timePoint = std::chrono::steady_clock::now(); auto timerInfo = TimerInfo(0, 0, duration, timePoint, duration, timePoint, duration, nullptr, - nullptr, 0, 0, 0, ""); + nullptr, 0, false, 0, 0, ""); auto res = timerInfo.UpdateWhenElapsedFromNow(timePoint, duration); EXPECT_FALSE(res); } @@ -1684,7 +1685,7 @@ HWTEST_F(TimeServiceTest, TimerInfo002, TestSize.Level0) auto duration = std::chrono::milliseconds(0); auto timePoint = std::chrono::steady_clock::now(); auto timerInfo = TimerInfo(0, 0, duration, timePoint, duration, timePoint, duration, nullptr, - nullptr, 0, 0, 0, ""); + nullptr, 0, false, 0, 0, ""); auto res = timerInfo.AdjustTimer(timePoint, 1); EXPECT_TRUE(res); } diff --git a/utils/native/include/time_common.h b/utils/native/include/time_common.h index 6ae9176e502c7db90e48b8a718875d88fb235eac..bdc2c0c0de550209a783c5ddfb69b2959d54bf72 100644 --- a/utils/native/include/time_common.h +++ b/utils/native/include/time_common.h @@ -30,6 +30,7 @@ struct TimerPara { int64_t windowLength; uint64_t interval; bool disposable; + bool autoRestore; int flag; }; @@ -58,6 +59,7 @@ enum TimeError { E_TIME_NTP_NOT_UPDATE, E_TIME_ACCOUNT_NOT_MATCH, E_TIME_ACCOUNT_ERROR, + E_TIME_AUTO_RESTORE_ERROR, }; enum DatabaseType {