From 0e37bba308c93ad3abf3e39b1fe7b4a72d822845 Mon Sep 17 00:00:00 2001 From: gaozhichao Date: Wed, 18 Dec 2024 10:25:00 +0800 Subject: [PATCH 1/2] =?UTF-8?q?[fix]=20=E4=BF=AE=E6=94=B9=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E7=AB=AF=E9=87=8D=E5=90=8D=E8=BF=94=E5=9B=9E=E5=80=BC?= =?UTF-8?q?=EF=BC=8C=E5=9C=A8=E5=AE=A2=E6=88=B7=E7=AB=AF=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E5=90=8C=E5=90=8D=E5=AE=9A=E6=97=B6=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gaozhichao --- .../inner_api/include/time_service_client.h | 1 - .../inner_api/src/time_service_client.cpp | 22 +++++-------------- services/timer/include/timer_manager.h | 3 +-- services/timer/src/timer_manager.cpp | 13 ++++++----- .../service_test/src/time_client_test.cpp | 7 ------ utils/native/include/time_common.h | 1 + 6 files changed, 15 insertions(+), 32 deletions(-) diff --git a/interfaces/inner_api/include/time_service_client.h b/interfaces/inner_api/include/time_service_client.h index 74cd7ce7..90e2a578 100644 --- a/interfaces/inner_api/include/time_service_client.h +++ b/interfaces/inner_api/include/time_service_client.h @@ -483,7 +483,6 @@ private: sptr listener_; static std::mutex instanceLock_; static sptr instance_; - TIME_API std::vector timerNameList_; TIME_API std::map> recoverTimerInfoMap_; TIME_API std::mutex recoverTimerInfoLock_; std::mutex proxyLock_; diff --git a/interfaces/inner_api/src/time_service_client.cpp b/interfaces/inner_api/src/time_service_client.cpp index f5bfd47c..1afab65c 100644 --- a/interfaces/inner_api/src/time_service_client.cpp +++ b/interfaces/inner_api/src/time_service_client.cpp @@ -266,11 +266,6 @@ uint64_t TimeServiceClient::CreateTimer(std::shared_ptr timerOptions // needs to acquire the lock `recoverTimerInfoLock_` before calling this method void TimeServiceClient::CheckNameLocked(std::string name) { - auto it = std::find(timerNameList_.begin(), timerNameList_.end(), name); - if (it == timerNameList_.end()) { - timerNameList_.push_back(name); - return; - } auto recoverIter = std::find_if(recoverTimerInfoMap_.begin(), recoverTimerInfoMap_.end(), [name](const auto& pair) { return pair.second->timerInfo->name == name; @@ -300,15 +295,16 @@ int32_t TimeServiceClient::CreateTimerV9(std::shared_ptr timerOption } auto errCode = proxy->CreateTimer(timerOptions, timerCallbackInfoObject, timerId); if (errCode != E_TIME_OK) { - TIME_HILOGE(TIME_MODULE_CLIENT, "create timer failed, errCode=%{public}d", errCode); - return errCode; + if (timerOptions->name != "" && errCode == E_TIME_TIMER_SAME_NAME_ERROR) { + CheckNameLocked(timerOptions->name); + } else { + TIME_HILOGE(TIME_MODULE_CLIENT, "create timer failed, errCode=%{public}d", errCode); + return errCode; + } } if (timerOptions->wantAgent == nullptr) { std::lock_guard lock(recoverTimerInfoLock_); - if (timerOptions->name != "") { - CheckNameLocked(timerOptions->name); - } auto info = recoverTimerInfoMap_.find(timerId); if (info != recoverTimerInfoMap_.end()) { TIME_HILOGE(TIME_MODULE_CLIENT, "recover timer info already insert."); @@ -420,12 +416,6 @@ int32_t TimeServiceClient::DestroyTimerV9(uint64_t timerId) std::lock_guard lock(recoverTimerInfoLock_); auto info = recoverTimerInfoMap_.find(timerId); if (info != recoverTimerInfoMap_.end()) { - if (info->second->timerInfo->name != "") { - auto it = std::find(timerNameList_.begin(), timerNameList_.end(), info->second->timerInfo->name); - if (it != timerNameList_.end()) { - timerNameList_.erase(it); - } - } recoverTimerInfoMap_.erase(timerId); } return errCode; diff --git a/services/timer/include/timer_manager.h b/services/timer/include/timer_manager.h index 4d473f59..0e856991 100644 --- a/services/timer/include/timer_manager.h +++ b/services/timer/include/timer_manager.h @@ -146,7 +146,7 @@ private: void DecreaseTimerCount(int uid); void CheckTimerCount(); void ShowTimerCountByUid(); - void AddTimerName(int uid, std::string name, uint64_t timerId); + int32_t AddTimerName(int uid, std::string name, uint64_t timerId); void DeleteTimerName(int uid, std::string name, uint64_t timerId); std::map> timerEntryMap_; @@ -161,7 +161,6 @@ private: std::vector> alarmBatches_; std::mutex mutex_; std::mutex entryMapMutex_; - std::mutex timerMapMutex_; std::chrono::system_clock::time_point lastTimeChangeClockTime_; std::chrono::steady_clock::time_point lastTimeChangeRealtime_; static std::mutex instanceLock_; diff --git a/services/timer/src/timer_manager.cpp b/services/timer/src/timer_manager.cpp index 3776026c..906974c0 100644 --- a/services/timer/src/timer_manager.cpp +++ b/services/timer/src/timer_manager.cpp @@ -151,12 +151,12 @@ OHOS::NativeRdb::ValuesBucket GetInsertValues(std::shared_ptr timerI } // needs to acquire the lock `entryMapMutex_` before calling this method -void TimerManager::AddTimerName(int uid, std::string name, uint64_t timerId) +int32_t TimerManager::AddTimerName(int uid, std::string name, uint64_t timerId) { if (timerNameMap_.find(uid) == timerNameMap_.end() || timerNameMap_[uid].find(name) == timerNameMap_[uid].end()) { timerNameMap_[uid][name] = timerId; TIME_HILOGD(TIME_MODULE_SERVICE, "record name: %{public}s id %{public}" PRId64 "", name.c_str(), timerId); - return; + return E_TIME_OK; } auto oldTimerId = timerNameMap_[uid][name]; timerNameMap_[uid][name] = timerId; @@ -165,7 +165,7 @@ void TimerManager::AddTimerName(int uid, std::string name, uint64_t timerId) UpdateOrDeleteDatabase(true, oldTimerId, needRecover); TIME_HILOGW(TIME_MODULE_SERVICE, "name: %{public}s in %{public}d already exist, destory timer %{public}" PRId64 "", name.c_str(), uid, oldTimerId); - return; + return E_TIME_TIMER_SAME_NAME_ERROR; } // needs to acquire the lock `entryMapMutex_` before calling this method @@ -205,6 +205,7 @@ int32_t TimerManager::CreateTimer(TimerPara ¶s, if (bundleName.empty()) { bundleName = TimeFileUtils::GetNameByPid(IPCSkeleton::GetCallingPid()); } + int32_t ret = E_TIME_OK; auto timerName = paras.name; std::shared_ptr timerInfo; { @@ -230,11 +231,11 @@ int32_t TimerManager::CreateTimer(TimerPara ¶s, timerEntryMap_.insert(std::make_pair(timerId, timerInfo)); IncreaseTimerCount(uid); if (timerName != "") { - AddTimerName(uid, timerName, timerId); + ret = AddTimerName(uid, timerName, timerId); } } if (type == NOT_STORE) { - return E_TIME_OK; + return ret; } else if (CheckNeedRecoverOnReboot(bundleName, paras.timerType, paras.autoRestore)) { TimeDatabase::GetInstance().Insert(std::string(HOLD_ON_REBOOT), GetInsertValues(timerInfo, paras)); @@ -242,7 +243,7 @@ int32_t TimerManager::CreateTimer(TimerPara ¶s, TimeDatabase::GetInstance().Insert(std::string(DROP_ON_REBOOT), GetInsertValues(timerInfo, paras)); } - return E_TIME_OK; + return ret; } void TimerManager::ReCreateTimer(uint64_t timerId, std::shared_ptr timerInfo) diff --git a/test/unittest/service_test/src/time_client_test.cpp b/test/unittest/service_test/src/time_client_test.cpp index ddfd9db2..05712a39 100644 --- a/test/unittest/service_test/src/time_client_test.cpp +++ b/test/unittest/service_test/src/time_client_test.cpp @@ -646,9 +646,6 @@ HWTEST_F(TimeClientTest, CreateTimer010, TestSize.Level1) auto errCode = TimeServiceClient::GetInstance()->CreateTimerV9(timerInfo, timerId1); EXPECT_EQ(errCode, TimeError::E_TIME_OK); EXPECT_NE(timerId1, 0); - auto nameList = TimeServiceClient::GetInstance()->timerNameList_; - auto name = std::find(nameList.begin(), nameList.end(), "testname"); - EXPECT_NE(name, nameList.end()); errCode = TimeServiceClient::GetInstance()->CreateTimerV9(timerInfo, timerId2); EXPECT_EQ(errCode, TimeError::E_TIME_OK); @@ -662,10 +659,6 @@ HWTEST_F(TimeClientTest, CreateTimer010, TestSize.Level1) errCode = TimeServiceClient::GetInstance()->DestroyTimerV9(timerId2); EXPECT_EQ(errCode, TimeError::E_TIME_OK); - - nameList = TimeServiceClient::GetInstance()->timerNameList_; - name = std::find(nameList.begin(), nameList.end(), "testname"); - EXPECT_EQ(name, nameList.end()); } /** diff --git a/utils/native/include/time_common.h b/utils/native/include/time_common.h index 3cd8a301..4d092838 100644 --- a/utils/native/include/time_common.h +++ b/utils/native/include/time_common.h @@ -62,6 +62,7 @@ enum TimeError { E_TIME_ACCOUNT_NOT_MATCH, E_TIME_ACCOUNT_ERROR, E_TIME_AUTO_RESTORE_ERROR, + E_TIME_TIMER_SAME_NAME_ERROR, }; enum DatabaseType { -- Gitee From 0aa2f3fac9be29591cd5978f5bf8767d40f4dc89 Mon Sep 17 00:00:00 2001 From: gaozhichao Date: Sat, 21 Dec 2024 16:22:44 +0800 Subject: [PATCH 2/2] =?UTF-8?q?[fix]=20=E5=AE=9A=E6=97=B6=E5=99=A8?= =?UTF-8?q?=E5=90=8D=E7=A7=B0=E9=9C=80=E6=B1=82=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gaozhichao --- interfaces/inner_api/include/itimer_info.h | 6 +-- .../inner_api/src/time_service_client.cpp | 6 +-- services/ipc/proxy/time_service_proxy.cpp | 2 +- services/ipc/stub/time_service_stub.cpp | 2 +- services/time_system_ability.cpp | 6 +-- services/timer/include/timer_info.h | 4 +- services/timer/include/timer_manager.h | 10 ++-- .../timer/include/timer_manager_interface.h | 2 +- services/timer/src/timer_info.cpp | 2 +- services/timer/src/timer_manager.cpp | 52 +++++++++++------- services/timer/src/timer_proxy.cpp | 2 +- .../service_test/src/time_proxy_test.cpp | 15 +++--- .../service_test/src/time_service_test.cpp | 54 +++++++++++-------- utils/native/include/time_common.h | 2 +- 14 files changed, 95 insertions(+), 70 deletions(-) diff --git a/interfaces/inner_api/include/itimer_info.h b/interfaces/inner_api/include/itimer_info.h index 084fde2b..802aac4a 100644 --- a/interfaces/inner_api/include/itimer_info.h +++ b/interfaces/inner_api/include/itimer_info.h @@ -32,7 +32,7 @@ public: bool disposable = false; bool autoRestore = false; uint64_t interval; - std::string name = ""; + std::shared_ptr name; std::shared_ptr wantAgent; /** @@ -109,9 +109,9 @@ public: * If set a timer with the same name as a previous one, * the previous timer will be destroyed. */ - void SetName(const std::string &_name) + void SetName(std::string _name) { - name = _name; + name = std::make_shared(_name); } virtual void SetWantAgent(std::shared_ptr wantAgent) = 0; virtual void OnTrigger() = 0; diff --git a/interfaces/inner_api/src/time_service_client.cpp b/interfaces/inner_api/src/time_service_client.cpp index 1afab65c..6b1c6846 100644 --- a/interfaces/inner_api/src/time_service_client.cpp +++ b/interfaces/inner_api/src/time_service_client.cpp @@ -268,7 +268,7 @@ void TimeServiceClient::CheckNameLocked(std::string name) { auto recoverIter = std::find_if(recoverTimerInfoMap_.begin(), recoverTimerInfoMap_.end(), [name](const auto& pair) { - return pair.second->timerInfo->name == name; + return *(pair.second->timerInfo->name) == name; }); if (recoverIter != recoverTimerInfoMap_.end()) { recoverIter = recoverTimerInfoMap_.erase(recoverIter); @@ -295,8 +295,8 @@ int32_t TimeServiceClient::CreateTimerV9(std::shared_ptr timerOption } auto errCode = proxy->CreateTimer(timerOptions, timerCallbackInfoObject, timerId); if (errCode != E_TIME_OK) { - if (timerOptions->name != "" && errCode == E_TIME_TIMER_SAME_NAME_ERROR) { - CheckNameLocked(timerOptions->name); + if (timerOptions->name != nullptr && errCode == E_TIME_TIMER_SAME_NAME_ERROR) { + CheckNameLocked(*timerOptions->name); } else { TIME_HILOGE(TIME_MODULE_CLIENT, "create timer failed, errCode=%{public}d", errCode); return errCode; diff --git a/services/ipc/proxy/time_service_proxy.cpp b/services/ipc/proxy/time_service_proxy.cpp index 2ea0b56a..c25240f3 100644 --- a/services/ipc/proxy/time_service_proxy.cpp +++ b/services/ipc/proxy/time_service_proxy.cpp @@ -62,7 +62,7 @@ int32_t TimeServiceProxy::CreateTimer(const std::shared_ptr &timerOp TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write descriptor"); return E_TIME_WRITE_PARCEL_ERROR; } - if (!data.WriteString(timerOptions->name)) { + if (!data.WriteString(*timerOptions->name)) { TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write name"); 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 9cc10ea8..c63d88e1 100644 --- a/services/ipc/stub/time_service_stub.cpp +++ b/services/ipc/stub/time_service_stub.cpp @@ -218,7 +218,7 @@ int32_t TimeServiceStub::OnCreateTimer(MessageParcel &data, MessageParcel &reply return E_TIME_NULLPTR; } auto timerOptions = std::make_shared(); - timerOptions->name = name; + timerOptions->name = std::make_shared(name); timerOptions->type = type; timerOptions->repeat = repeat; timerOptions->interval = interval; diff --git a/services/time_system_ability.cpp b/services/time_system_ability.cpp index 411ce06c..3e5bdc9b 100644 --- a/services/time_system_ability.cpp +++ b/services/time_system_ability.cpp @@ -367,7 +367,7 @@ void TimeSystemAbility::ParseTimerPara(const std::shared_ptr &timerO if (timerOptions->disposable) { paras.flag |= ITimerManager::TimerFlag::IS_DISPOSABLE; } - if (timerOptions->name != "") { + if (timerOptions->name != nullptr) { paras.name = timerOptions->name; } paras.interval = timerOptions->repeat ? timerOptions->interval : 0; @@ -379,7 +379,7 @@ int32_t TimeSystemAbility::CheckTimerPara(const DatabaseType type, const TimerPa paras.timerType == ITimerManager::TimerType::ELAPSED_REALTIME_WAKEUP || type == DatabaseType::NOT_STORE)) { return E_TIME_AUTO_RESTORE_ERROR; } - if (paras.name.size() > STR_MAX_LENGTH) { + if ((*paras.name).size() > STR_MAX_LENGTH) { return E_TIME_PARAMETERS_INVALID; } return E_TIME_OK; @@ -1029,7 +1029,7 @@ void TimeSystemAbility::RecoverTimerInner(std::shared_ptr(GetLong(resultSet, 0)); auto timerInfo = std::make_shared(TimerEntry { // line 11 is 'name' - GetString(resultSet, 11), + std::make_shared(GetString(resultSet, 11)), // Line 0 is 'timerId' timerId, // Line 1 is 'type' diff --git a/services/timer/include/timer_info.h b/services/timer/include/timer_info.h index 59b80412..c4cb9342 100644 --- a/services/timer/include/timer_info.h +++ b/services/timer/include/timer_info.h @@ -27,7 +27,7 @@ static const uint32_t HALF_SECEND = 2; class TimerInfo { public: - const std::string name; + const std::shared_ptr name; const uint64_t id; const int type; const std::chrono::milliseconds origWhen; @@ -49,7 +49,7 @@ public: std::chrono::milliseconds offset; std::string bundleName; - TimerInfo(std::string name, uint64_t id, int type, + TimerInfo(std::shared_ptr name, uint64_t id, int type, std::chrono::milliseconds when, std::chrono::steady_clock::time_point whenElapsed, std::chrono::milliseconds windowLength, diff --git a/services/timer/include/timer_manager.h b/services/timer/include/timer_manager.h index 0e856991..01965ec6 100644 --- a/services/timer/include/timer_manager.h +++ b/services/timer/include/timer_manager.h @@ -73,7 +73,7 @@ private: explicit TimerManager(std::shared_ptr impl); void TimerLooper(); - void SetHandler(std::string name, + void SetHandler(std::shared_ptr name, uint64_t id, int type, uint64_t triggerAtTime, @@ -86,7 +86,7 @@ private: int uid, int pid, const std::string &bundleName); - void SetHandlerLocked(std::string name, + void SetHandlerLocked(std::shared_ptr name, uint64_t id, int type, std::chrono::milliseconds when, @@ -146,14 +146,14 @@ private: void DecreaseTimerCount(int uid); void CheckTimerCount(); void ShowTimerCountByUid(); - int32_t AddTimerName(int uid, std::string name, uint64_t timerId); - void DeleteTimerName(int uid, std::string name, uint64_t timerId); + int32_t AddTimerName(int uid, std::shared_ptr, uint64_t timerId); + void DeleteTimerName(int uid, std::shared_ptr, uint64_t timerId); std::map> timerEntryMap_; // vector std::vector> timerCount_; // > - std::map> timerNameMap_; + std::map, uint64_t>> timerNameMap_; std::default_random_engine random_; std::atomic_bool runFlag_; std::shared_ptr handler_; diff --git a/services/timer/include/timer_manager_interface.h b/services/timer/include/timer_manager_interface.h index 512ef5f7..6751f807 100644 --- a/services/timer/include/timer_manager_interface.h +++ b/services/timer/include/timer_manager_interface.h @@ -24,7 +24,7 @@ namespace OHOS { namespace MiscServices { struct TimerEntry { - std::string name; + std::shared_ptr name; uint64_t id; int type; int64_t windowLength; diff --git a/services/timer/src/timer_info.cpp b/services/timer/src/timer_info.cpp index 03e47533..67c6d62f 100644 --- a/services/timer/src/timer_info.cpp +++ b/services/timer/src/timer_info.cpp @@ -31,7 +31,7 @@ bool TimerInfo::Matches(const std::string &packageName) const return false; } -TimerInfo::TimerInfo(std::string _name, uint64_t _id, int _type, +TimerInfo::TimerInfo(std::shared_ptr _name, uint64_t _id, int _type, std::chrono::milliseconds _when, std::chrono::steady_clock::time_point _whenElapsed, std::chrono::milliseconds _windowLength, diff --git a/services/timer/src/timer_manager.cpp b/services/timer/src/timer_manager.cpp index 906974c0..95f15b58 100644 --- a/services/timer/src/timer_manager.cpp +++ b/services/timer/src/timer_manager.cpp @@ -146,47 +146,61 @@ OHOS::NativeRdb::ValuesBucket GetInsertValues(std::shared_ptr timerI insertValues.PutInt("state", 0); insertValues.PutLong("triggerTime", 0); insertValues.PutInt("pid", timerInfo->pid); - insertValues.PutString("name", timerInfo->name); + insertValues.PutString("name", *timerInfo->name); return insertValues; } // needs to acquire the lock `entryMapMutex_` before calling this method -int32_t TimerManager::AddTimerName(int uid, std::string name, uint64_t timerId) +int32_t TimerManager::AddTimerName(int uid, std::shared_ptr name, uint64_t timerId) { - if (timerNameMap_.find(uid) == timerNameMap_.end() || timerNameMap_[uid].find(name) == timerNameMap_[uid].end()) { + if (timerNameMap_.find(uid) == timerNameMap_.end()) { timerNameMap_[uid][name] = timerId; - TIME_HILOGD(TIME_MODULE_SERVICE, "record name: %{public}s id %{public}" PRId64 "", name.c_str(), timerId); + TIME_HILOGD(TIME_MODULE_SERVICE, "record name: %{public}s id %{public}" PRId64 "", name->c_str(), timerId); return E_TIME_OK; } - auto oldTimerId = timerNameMap_[uid][name]; + auto it = std::find_if(timerNameMap_[uid].begin(), timerNameMap_[uid].begin(), + [name](const std::pair, uint64_t>& pair) { + return *(pair.first) == *name; + }); + if (it == timerNameMap_[uid].end()) { + timerNameMap_[uid][name] = timerId; + TIME_HILOGD(TIME_MODULE_SERVICE, "record name: %{public}s id %{public}" PRId64 "", name->c_str(), timerId); + return E_TIME_OK; + } + uint64_t oldTimerId = it->second; + timerNameMap_[uid].erase(it); timerNameMap_[uid][name] = timerId; bool needRecover; StopTimerInnerLocked(true, oldTimerId, needRecover); UpdateOrDeleteDatabase(true, oldTimerId, needRecover); - TIME_HILOGW(TIME_MODULE_SERVICE, "name: %{public}s in %{public}d already exist, destory timer %{public}" PRId64 "", - name.c_str(), uid, oldTimerId); + TIME_HILOGW(TIME_MODULE_SERVICE, "create:%{public}" PRId64 " name: %{public}s in %{public}d already exist," + "destory timer %{public}" PRId64 "",timerId, name->c_str(), uid, oldTimerId); return E_TIME_TIMER_SAME_NAME_ERROR; } // needs to acquire the lock `entryMapMutex_` before calling this method -void TimerManager::DeleteTimerName(int uid, std::string name, uint64_t timerId) +void TimerManager::DeleteTimerName(int uid, std::shared_ptr name, uint64_t timerId) { auto nameIter = timerNameMap_.find(uid); if (nameIter == timerNameMap_.end()) { TIME_HILOGE(TIME_MODULE_SERVICE, "NameMap has no uid %{public}d", uid); return; } - auto timerIter = nameIter->second.find(name); - if (timerIter == nameIter->second.end()) { - TIME_HILOGE(TIME_MODULE_SERVICE, "NameMap has no name:%{public}s uid: %{public}d", name.c_str(), uid); + auto timerIter = std::find_if(timerNameMap_[uid].begin(), timerNameMap_[uid].end(), + [name](const std::pair, uint64_t>& pair) { + return *(pair.first) == *name; + }); + if (timerIter == timerNameMap_[uid].end()) { + TIME_HILOGE(TIME_MODULE_SERVICE, "NameMap has no name:%{public}s uid: %{public}d", name->c_str(), uid); return; + } if (timerIter->second == timerId) { timerNameMap_[uid].erase(timerIter); return; } TIME_HILOGW(TIME_MODULE_SERVICE, - "timer %{public}" PRId64 " not exist in map, name:%{public}s uid%{public}d", timerId, name.c_str(), uid); + "timer %{public}" PRId64 " not exist in map, name:%{public}s uid%{public}d", timerId, name->c_str(), uid); } int32_t TimerManager::CreateTimer(TimerPara ¶s, @@ -230,7 +244,7 @@ int32_t TimerManager::CreateTimer(TimerPara ¶s, }); timerEntryMap_.insert(std::make_pair(timerId, timerInfo)); IncreaseTimerCount(uid); - if (timerName != "") { + if (timerName != nullptr) { ret = AddTimerName(uid, timerName, timerId); } } @@ -250,7 +264,7 @@ void TimerManager::ReCreateTimer(uint64_t timerId, std::shared_ptr t { std::lock_guard lock(entryMapMutex_); timerEntryMap_.insert(std::make_pair(timerId, timerInfo)); - if (timerInfo->name != "") { + if (timerInfo->name != nullptr) { AddTimerName(timerInfo->uid, timerInfo->name, timerId); } IncreaseTimerCount(timerInfo->uid); @@ -373,7 +387,6 @@ int32_t TimerManager::StopTimerInner(uint64_t timerNumber, bool needDestroy) TIME_HILOGI(TIME_MODULE_SERVICE, "id: %{public}" PRId64 ", needDestroy: %{public}d", timerNumber, needDestroy); int32_t ret; bool needRecover; - std::string name = ""; { std::lock_guard lock(entryMapMutex_); ret = StopTimerInnerLocked(needDestroy, timerNumber, needRecover); @@ -401,7 +414,7 @@ int32_t TimerManager::StopTimerInnerLocked(bool needDestroy, uint64_t timerNumbe auto name = it->second->name; timerEntryMap_.erase(it); DecreaseTimerCount(uid); - if (name != "") { + if (name != nullptr) { DeleteTimerName(uid, name, timerNumber); } } @@ -435,7 +448,7 @@ void TimerManager::UpdateOrDeleteDatabase(bool needDestroy, uint64_t timerNumber } } -void TimerManager::SetHandler(std::string name, +void TimerManager::SetHandler(std::shared_ptr name, uint64_t id, int type, uint64_t triggerAtTime, @@ -493,7 +506,8 @@ void TimerManager::SetHandler(std::string name, bundleName); } -void TimerManager::SetHandlerLocked(std::string name, uint64_t id, int type, +void TimerManager::SetHandlerLocked(std::shared_ptr name, + uint64_t id, int type, std::chrono::milliseconds when, std::chrono::steady_clock::time_point whenElapsed, std::chrono::milliseconds windowLength, @@ -1299,7 +1313,7 @@ bool TimerManager::ShowTimerEntryMap(int fd) auto iter = timerEntryMap_.begin(); for (; iter != timerEntryMap_.end(); iter++) { dprintf(fd, " - dump timer number = %lu\n", iter->first); - dprintf(fd, " * timer name = %s\n", iter->second->name.c_str()); + dprintf(fd, " * timer name = %s\n", iter->second->name->c_str()); dprintf(fd, " * timer id = %lu\n", iter->second->id); dprintf(fd, " * timer type = %d\n", iter->second->type); dprintf(fd, " * timer flag = %lu\n", iter->second->flag); diff --git a/services/timer/src/timer_proxy.cpp b/services/timer/src/timer_proxy.cpp index 4c607091..1b2c976d 100644 --- a/services/timer/src/timer_proxy.cpp +++ b/services/timer/src/timer_proxy.cpp @@ -265,7 +265,7 @@ bool TimerProxy::SetTimerExemption(const std::unordered_set &nameAr bool TimerProxy::IsTimerExemption(std::shared_ptr timer) { - auto key = timer->bundleName + "|" + timer->name; + auto key = timer->bundleName + "|" + *timer->name; TIME_HILOGD(TIME_MODULE_SERVICE, "key is: %{public}s", key.c_str()); if ((adjustExemptionList_.find(timer->bundleName) != adjustExemptionList_.end() || adjustExemptionList_.find(key) != adjustExemptionList_.end()) diff --git a/test/unittest/service_test/src/time_proxy_test.cpp b/test/unittest/service_test/src/time_proxy_test.cpp index 74f717cb..6a43e70e 100644 --- a/test/unittest/service_test/src/time_proxy_test.cpp +++ b/test/unittest/service_test/src/time_proxy_test.cpp @@ -785,7 +785,8 @@ HWTEST_F(TimeProxyTest, AdjustTimerExemption001, TestSize.Level0) TimerProxy::GetInstance().SetTimerExemption(exemptionSet, true); auto duration = std::chrono::milliseconds::zero(); auto timePoint = std::chrono::steady_clock::now(); - auto timerInfo = TimerInfo("name", 0, 0, duration, timePoint, duration, timePoint, duration, nullptr, + auto namePtr = std::make_shared("name"); + auto timerInfo = TimerInfo(namePtr, 0, 0, duration, timePoint, duration, timePoint, duration, nullptr, nullptr, 0, false, 0, 0, "bundleName"); auto timerInfoPtr = std::make_shared(timerInfo); auto ret = TimerProxy::GetInstance().IsTimerExemption(timerInfoPtr); @@ -823,11 +824,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, false, UID, 0, ""); + auto timerInfo1 = std::make_shared(nullptr, TIMER_ID, 0, duration, timePoint, duration, timePoint, + duration, 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, + auto timerInfo2 = std::make_shared(nullptr, TIMER_ID + 1, 0, duration, timePoint, duration, timePoint, duration, nullptr, nullptr, 0, false, UID, 0, ""); res = TimerProxy::GetInstance().CallbackAlarmIfNeed(timerInfo2); EXPECT_EQ(res, E_TIME_OK); @@ -879,11 +880,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, + auto timerInfo1 = std::make_shared(nullptr, TIMER_ID, 0, duration, timePoint, duration, timePoint, duration, 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, + auto timerInfo2 = std::make_shared(nullptr, TIMER_ID + 1, 0, duration, timePoint, duration, timePoint, duration, nullptr, nullptr, 0, false, 0, PID, ""); res = TimerProxy::GetInstance().CallbackAlarmIfNeed(timerInfo2); EXPECT_EQ(res, E_TIME_OK); @@ -925,7 +926,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, + auto timerInfo = std::make_shared(nullptr, TIMER_ID, 0, duration, timePoint, duration, timePoint, duration, nullptr, nullptr, 0, false, UID, PID, ""); TimerProxy::GetInstance().RecordUidTimerMap(timerInfo, false); { diff --git a/test/unittest/service_test/src/time_service_test.cpp b/test/unittest/service_test/src/time_service_test.cpp index 917293f1..d6cc5077 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, false, nullptr, nullptr, 0, 0, "bundleName"}); + TimerEntry{nullptr, timerId1, 0, 0, 0, 0, false, nullptr, nullptr, 0, 0, "bundleName"}); TimerManager::GetInstance()->ReCreateTimer(timerId1, entry); std::lock_guard lock(TimerManager::GetInstance()->entryMapMutex_); @@ -1125,7 +1125,7 @@ HWTEST_F(TimeServiceTest, TimerManager001, TestSize.Level0) HWTEST_F(TimeServiceTest, TimerManager002, TestSize.Level0) { uint64_t max = std::numeric_limits::max(); - TimerManager::GetInstance()->SetHandler("", + TimerManager::GetInstance()->SetHandler(nullptr, TIMER_ID, 0, max, @@ -1149,6 +1149,7 @@ HWTEST_F(TimeServiceTest, TimerManager002, TestSize.Level0) /** * @tc.name: TimerManager003. + * @tc.desc: test Set() with type > ALARM_TYPE_COUNT. * @tc.type: FUNC */ @@ -1169,7 +1170,7 @@ HWTEST_F(TimeServiceTest, TimerManager004, TestSize.Level0) { TimerManager::GetInstance()->DestroyTimer(TIMER_ID); auto entry = std::make_shared( - TimerEntry{"", TIMER_ID, 0, 0, 0, 0, false, nullptr, nullptr, UID, PID, "bundleName"}); + TimerEntry{nullptr, TIMER_ID, 0, 0, 0, 0, false, nullptr, nullptr, UID, PID, "bundleName"}); TimerManager::GetInstance()->ReCreateTimer(TIMER_ID, entry); { @@ -1220,7 +1221,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, + auto timerInfo = std::make_shared(nullptr, TIMER_ID, 0, duration, timePoint, duration, timePoint, duration, nullptr, nullptr, 0, false, 0, 0, ""); auto res = TimerManager::GetInstance()->NotifyWantAgent(timerInfo); EXPECT_FALSE(res); @@ -1288,7 +1289,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, + auto timerInfo1 = std::make_shared(nullptr, TIMER_ID, 0, duration, timePoint, duration, timePoint, duration, nullptr, nullptr, 0, false, 0, 0, ""); std::lock_guard lock(TimerManager::GetInstance()->mutex_); auto alarm = TimerManager::GetInstance()->mPendingIdleUntil_; @@ -1302,11 +1303,11 @@ HWTEST_F(TimeServiceTest, TimerManager007, TestSize.Level0) EXPECT_TRUE(res); 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, + auto timerInfo2 = std::make_shared(nullptr, TIMER_ID, 1, duration1, timePoint, duration, timePoint, duration, 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, + auto timerInfo3 = std::make_shared(nullptr, TIMER_ID, 2, duration, timePoint, duration, timePoint, duration, nullptr, nullptr, 0, false, 0, 0, ""); res = TimerManager::GetInstance()->AdjustDeliveryTimeBasedOnDeviceIdle(timerInfo3); EXPECT_TRUE(res); @@ -1335,7 +1336,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, false, nullptr, nullptr, 0, 0, "bundleName"}); + TimerEntry{nullptr, 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); @@ -1361,14 +1362,14 @@ 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, false, 0, 0, ""); + auto timerInfo = std::make_shared(nullptr, TIMER_ID, 0, duration, timePoint, duration, timePoint,\ + duration, 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, false, nullptr, nullptr, 0, 0, "bundleName"}); + TimerEntry{nullptr, 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); @@ -1408,7 +1409,7 @@ HWTEST_F(TimeServiceTest, TimerManager012, TestSize.Level0) } auto entry = std::make_shared( - TimerEntry{"", TIMER_ID, 0, 0, 0, 0, false, nullptr, nullptr, UID, 0, "bundleName"}); + TimerEntry{nullptr, TIMER_ID, 0, 0, 0, 0, false, nullptr, nullptr, UID, 0, "bundleName"}); timerManager->ReCreateTimer(TIMER_ID, entry); timerManager->OnPackageRemoved(UID); @@ -1471,13 +1472,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, false, nullptr, nullptr, 0, 0, "bundleName"}); + TimerEntry{nullptr, 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, false, nullptr, nullptr, 0, 0, "bundleName"}); + TimerEntry{nullptr, i, 0, 0, 0, 0, false, nullptr, nullptr, 0, 0, "bundleName"}); TimerManager::GetInstance()->ReCreateTimer(i, entry); } EXPECT_EQ(TimerManager::GetInstance()->timerOutOfRangeTimes_, 2); @@ -1492,21 +1493,30 @@ HWTEST_F(TimeServiceTest, TimerManager015, TestSize.Level0) { TIME_HILOGI(TIME_MODULE_CLIENT, "TimerManager015 start"); TimerManager::GetInstance()->timerNameMap_.clear(); + auto namePtr = std::make_shared("name"); auto entry = std::make_shared( - TimerEntry{"name", TIMER_ID, 0, 0, 0, 0, false, nullptr, nullptr, UID, 0, "bundleName"}); + TimerEntry{namePtr, TIMER_ID, 0, 0, 0, 0, false, nullptr, nullptr, UID, 0, "bundleName"}); TimerManager::GetInstance()->ReCreateTimer(TIMER_ID, entry); auto timerNameMap = TimerManager::GetInstance()->timerNameMap_; EXPECT_NE(timerNameMap.find(UID), timerNameMap.end()); - EXPECT_NE(timerNameMap[UID].find("name"), timerNameMap[UID].end()); - EXPECT_EQ(timerNameMap[UID]["name"], TIMER_ID); + auto it = std::find_if(timerNameMap[UID].begin(), timerNameMap[UID].begin(), + [namePtr](const std::pair, uint64_t>& pair) { + return *(pair.first) == *namePtr; + }); + EXPECT_NE(it, timerNameMap[UID].end()); + EXPECT_EQ(it->second, TIMER_ID); entry = std::make_shared( - TimerEntry{"name", TIMER_ID + 1, 0, 0, 0, 0, false, nullptr, nullptr, UID, 0, "bundleName"}); + TimerEntry{namePtr, TIMER_ID + 1, 0, 0, 0, 0, false, nullptr, nullptr, UID, 0, "bundleName"}); TimerManager::GetInstance()->ReCreateTimer(TIMER_ID + 1, entry); timerNameMap = TimerManager::GetInstance()->timerNameMap_; EXPECT_NE(timerNameMap.find(UID), timerNameMap.end()); - EXPECT_NE(timerNameMap[UID].find("name"), timerNameMap[UID].end()); - EXPECT_EQ(timerNameMap[UID]["name"], TIMER_ID + 1); + it = std::find_if(timerNameMap[UID].begin(), timerNameMap[UID].begin(), + [namePtr](const std::pair, uint64_t>& pair) { + return *(pair.first) == *namePtr; + }); + EXPECT_NE(it, timerNameMap[UID].end()); + EXPECT_EQ(it->second, TIMER_ID + 1); auto ret = TimerManager::GetInstance()->DestroyTimer(TIMER_ID); EXPECT_NE(ret, E_TIME_OK); } @@ -1681,7 +1691,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, + auto timerInfo = TimerInfo(nullptr, 0, 0, duration, timePoint, duration, timePoint, duration, nullptr, nullptr, 0, false, 0, 0, ""); auto res = timerInfo.UpdateWhenElapsedFromNow(timePoint, duration); EXPECT_FALSE(res); @@ -1696,7 +1706,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, + auto timerInfo = TimerInfo(nullptr, 0, 0, duration, timePoint, duration, timePoint, duration, nullptr, 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 4d092838..b4497685 100644 --- a/utils/native/include/time_common.h +++ b/utils/native/include/time_common.h @@ -26,7 +26,7 @@ namespace MiscServices { #define TIME_SERVICE_NAME "TimeService" struct TimerPara { - std::string name; + std::shared_ptr name; int timerType; int64_t windowLength; uint64_t interval; -- Gitee