diff --git a/interfaces/innerkits/include/time_service_client.h b/interfaces/innerkits/include/time_service_client.h index 802609b..eca2372 100644 --- a/interfaces/innerkits/include/time_service_client.h +++ b/interfaces/innerkits/include/time_service_client.h @@ -168,7 +168,7 @@ public: * @param isProxy true if set proxy, false if remove proxy. * @return bool true on success, false on failure. */ - bool ProxyTimer(int32_t uid, bool isProxy); + bool ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger); /** * ResetAllProxy diff --git a/services/time_manager/include/time_service.h b/services/time_manager/include/time_service.h index b952f30..8d6ba68 100644 --- a/services/time_manager/include/time_service.h +++ b/services/time_manager/include/time_service.h @@ -63,7 +63,7 @@ public: bool DestroyTimer(uint64_t timerId) override; void NetworkTimeStatusOff() override; void NetworkTimeStatusOn() override; - bool ProxyTimer(int32_t uid, bool isProxy) override; + bool ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger) override; bool ResetAllProxy() override; protected: diff --git a/services/time_manager/include/time_service_interface.h b/services/time_manager/include/time_service_interface.h index 35b9dd2..d6b190c 100644 --- a/services/time_manager/include/time_service_interface.h +++ b/services/time_manager/include/time_service_interface.h @@ -186,7 +186,7 @@ public: * @param isProxy true if proxy, false if not proxy * @return bool true on success, false on failure. */ - virtual bool ProxyTimer(int32_t uid, bool isProxy) = 0; + virtual bool ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger) = 0; /** * ResetAllProxy diff --git a/services/time_manager/include/time_service_proxy.h b/services/time_manager/include/time_service_proxy.h index fa0a9be..2ebbb75 100644 --- a/services/time_manager/include/time_service_proxy.h +++ b/services/time_manager/include/time_service_proxy.h @@ -44,7 +44,7 @@ public: bool DestroyTimer(uint64_t timerId) override; void NetworkTimeStatusOff() override; void NetworkTimeStatusOn() override; - bool ProxyTimer(int32_t uid, bool isProxy) override; + bool ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger) override; bool ResetAllProxy() override; private: static inline BrokerDelegator delegator_; diff --git a/services/time_manager/src/time_service.cpp b/services/time_manager/src/time_service.cpp index 34818c8..60167d8 100644 --- a/services/time_manager/src/time_service.cpp +++ b/services/time_manager/src/time_service.cpp @@ -580,7 +580,7 @@ void TimeService::NetworkTimeStatusOn() DelayedSingleton::GetInstance()->UpdateStatusOn(); } -bool TimeService::ProxyTimer(int32_t uid, bool isProxy) +bool TimeService::ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger) { auto hasPerm = DelayedSingleton::GetInstance()->CheckProxyCallingPermission(); if (!hasPerm) { @@ -597,7 +597,7 @@ bool TimeService::ProxyTimer(int32_t uid, bool isProxy) return false; } } - return timerManagerHandler_->ProxyTimer(uid, isProxy); + return timerManagerHandler_->ProxyTimer(uid, isProxy, needRetrigger); } bool TimeService::ResetAllProxy() diff --git a/services/time_manager/src/time_service_client.cpp b/services/time_manager/src/time_service_client.cpp index b5a3956..8dc1d8c 100644 --- a/services/time_manager/src/time_service_client.cpp +++ b/services/time_manager/src/time_service_client.cpp @@ -435,7 +435,7 @@ void TimeSaDeathRecipient::OnRemoteDied(const wptr &object) TimeServiceClient::GetInstance()->OnRemoteSaDied(object); } -bool TimeServiceClient::ProxyTimer(int32_t uid, bool isProxy) +bool TimeServiceClient::ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger) { TIME_HILOGD(TIME_MODULE_CLIENT, "ProxyTimer start uid: %{public}d, isProxy: %{public}d", uid, isProxy); @@ -447,7 +447,7 @@ bool TimeServiceClient::ProxyTimer(int32_t uid, bool isProxy) TIME_HILOGE(TIME_MODULE_CLIENT, "ProxyTimer ConnectService failed."); return false; } - return timeServiceProxy_->ProxyTimer(uid, isProxy); + return timeServiceProxy_->ProxyTimer(uid, isProxy, needRetrigger); } bool TimeServiceClient::ResetAllProxy() diff --git a/services/time_manager/src/time_service_proxy.cpp b/services/time_manager/src/time_service_proxy.cpp index a9c448e..3924b18 100644 --- a/services/time_manager/src/time_service_proxy.cpp +++ b/services/time_manager/src/time_service_proxy.cpp @@ -390,7 +390,7 @@ void TimeServiceProxy::NetworkTimeStatusOff() return; } -bool TimeServiceProxy::ProxyTimer(int32_t uid, bool isProxy) +bool TimeServiceProxy::ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger) { MessageParcel data, reply; MessageOption option; @@ -407,6 +407,10 @@ bool TimeServiceProxy::ProxyTimer(int32_t uid, bool isProxy) TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write parcelable"); return false; } + if (!data.WriteBool(needRetrigger)) { + TIME_HILOGE(TIME_MODULE_CLIENT, "Failed to write parcelable"); + return false; + } int32_t result = Remote()->SendRequest(PROXY_TIMER, data, reply, option); if (result != ERR_NONE) { diff --git a/services/time_manager/src/time_service_stub.cpp b/services/time_manager/src/time_service_stub.cpp index 4612501..053ce92 100644 --- a/services/time_manager/src/time_service_stub.cpp +++ b/services/time_manager/src/time_service_stub.cpp @@ -317,7 +317,8 @@ int32_t TimeServiceStub::OnTimerProxy(MessageParcel &data, MessageParcel &reply) return E_TIME_READ_PARCEL_ERROR; } auto isProxy = data.ReadBool(); - if (!ProxyTimer(uid, isProxy)) { + auto needRetrigger = data.ReadBool(); + if (!ProxyTimer(uid, isProxy, needRetrigger)) { return E_TIME_DEAL_FAILED; } TIME_HILOGI(TIME_MODULE_SERVICE, "end."); diff --git a/services/time_manager/test/unittest/src/time_service_test.cpp b/services/time_manager/test/unittest/src/time_service_test.cpp index 30fc86a..7bd9712 100644 --- a/services/time_manager/test/unittest/src/time_service_test.cpp +++ b/services/time_manager/test/unittest/src/time_service_test.cpp @@ -317,9 +317,9 @@ HWTEST_F(TimeServiceTest, CreateTimer006, TestSize.Level0) HWTEST_F(TimeServiceTest, ProxyTimer001, TestSize.Level0) { int32_t uid = 99999; - auto ret = TimeServiceClient::GetInstance()->ProxyTimer(uid, true); + auto ret = TimeServiceClient::GetInstance()->ProxyTimer(uid, true, true); EXPECT_TRUE(ret); - ret = TimeServiceClient::GetInstance()->ProxyTimer(uid, false); + ret = TimeServiceClient::GetInstance()->ProxyTimer(uid, false, true); EXPECT_TRUE(ret); } @@ -332,7 +332,7 @@ HWTEST_F(TimeServiceTest, ProxyTimer001, TestSize.Level0) HWTEST_F(TimeServiceTest, ProxyTimer002, TestSize.Level0) { int32_t uid = 99999; - auto ret = TimeServiceClient::GetInstance()->ProxyTimer(uid, true); + auto ret = TimeServiceClient::GetInstance()->ProxyTimer(uid, true, true); EXPECT_TRUE(ret); ret = TimeServiceClient::GetInstance()->ResetAllProxy(); EXPECT_TRUE(ret); @@ -347,6 +347,21 @@ HWTEST_F(TimeServiceTest, ProxyTimer002, TestSize.Level0) HWTEST_F(TimeServiceTest, ProxyTimer003, TestSize.Level0) { int32_t uid = 99999; - auto ret = TimeServiceClient::GetInstance()->ProxyTimer(uid, false); + auto ret = TimeServiceClient::GetInstance()->ProxyTimer(uid, false, true); + EXPECT_FALSE(ret); +} + +/** +* @tc.name: ProxyTimer004. +* @tc.desc: proxy timer. +* @tc.type: FUNC +* @tc.require: SR000H0GQ6 AR000H2VTQ +*/ +HWTEST_F(TimeServiceTest, ProxyTimer004, TestSize.Level0) +{ + int32_t uid = 99999; + auto ret = TimeServiceClient::GetInstance()->ProxyTimer(uid, true, false); + EXPECT_FALSE(ret); + auto ret = TimeServiceClient::GetInstance()->ProxyTimer(uid, false, false); EXPECT_FALSE(ret); } \ No newline at end of file diff --git a/services/timer/include/timer_manager.h b/services/timer/include/timer_manager.h index b5fcf5d..2725e76 100644 --- a/services/timer/include/timer_manager.h +++ b/services/timer/include/timer_manager.h @@ -41,7 +41,7 @@ public: bool StartTimer(uint64_t timerNumber, uint64_t triggerTime) override; bool StopTimer(uint64_t timerNumber) override; bool DestroyTimer(uint64_t timerNumber) override; - bool ProxyTimer(int32_t uid, bool isProxy) override; + bool ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger) override; bool ResetAllProxy() override; ~TimerManager() override; diff --git a/services/timer/include/timer_manager_interface.h b/services/timer/include/timer_manager_interface.h index bf7dd2a..ffebabd 100644 --- a/services/timer/include/timer_manager_interface.h +++ b/services/timer/include/timer_manager_interface.h @@ -56,7 +56,7 @@ public: virtual bool StopTimer(uint64_t timerNumber) = 0; virtual bool DestroyTimer(uint64_t timerNumber) = 0; virtual ~ITimerManager() = default; - virtual bool ProxyTimer(int32_t uid, bool isProxy) = 0; + virtual bool ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger) = 0; virtual bool ResetAllProxy() = 0; }; // ITimerManager } // MiscService diff --git a/services/timer/src/timer_manager.cpp b/services/timer/src/timer_manager.cpp index b20d5a4..9cec273 100644 --- a/services/timer/src/timer_manager.cpp +++ b/services/timer/src/timer_manager.cpp @@ -134,6 +134,21 @@ bool TimerManager::StopTimer(uint64_t timerNumber) return false; } RemoveHandler(timerNumber); + int32_t uid = it->second->uid; + auto itMap = proxyMap_.find(uid); + if (itMap != proxyMap_.end()) { + auto alarms = itMap->second; + for (auto itAlarm = alarms.begin(); itAlarm != alarms.end();) { + if ((*itAlarm)->id == timerNumber) { + alarms.erase(itAlarm); + } else { + itAlarm++; + } + } + if (alarms.empty()) { + proxyMap_.erase(uid); + } + } TIME_HILOGD(TIME_MODULE_SERVICE, "end."); return true; } @@ -148,6 +163,21 @@ bool TimerManager::DestroyTimer(uint64_t timerNumber) return false; } RemoveHandler(timerNumber); + int32_t uid = it->second->uid; + auto itMap = proxyMap_.find(uid); + if (itMap != proxyMap_.end()) { + auto alarms = itMap->second; + for (auto itAlarm = alarms.begin(); itAlarm != alarms.end();) { + if ((*itAlarm)->id == timerNumber) { + alarms.erase(itAlarm); + } else { + itAlarm++; + } + } + if (alarms.empty()) { + proxyMap_.erase(uid); + } + } timerEntryMap_.erase(it); TIME_HILOGD(TIME_MODULE_SERVICE, "end."); return true; @@ -581,7 +611,7 @@ void TimerManager::CallbackAlarmIfNeed(std::shared_ptr alarm) } } -bool TimerManager::ProxyTimer(int32_t uid, bool isProxy) +bool TimerManager::ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger) { std::lock_guard lock(proxyMutex_); TIME_HILOGD(TIME_MODULE_SERVICE, "start"); @@ -596,15 +626,21 @@ bool TimerManager::ProxyTimer(int32_t uid, bool isProxy) TIME_HILOGE(TIME_MODULE_SERVICE, "Uid: %{public}d doesn't exist in the proxy list." PRId64 "", uid); return false; } + if (!needRetrigger) { + TIME_HILOGI(TIME_MODULE_SERVICE, "ProxyTimer doesn't need retrigger, clear all callbacks!"); + proxyMap_.erase(uid); + return true; + } auto itMap = proxyMap_.find(uid); if (itMap != proxyMap_.end()) { auto timeInfoVec = itMap->second; for (auto alarm : timeInfoVec) { if (!alarm->callback) { - TIME_HILOGE(TIME_MODULE_SERVICE, "Callback is nullptr!"); + TIME_HILOGE(TIME_MODULE_SERVICE, "ProxyTimer Callback is nullptr!"); continue; } alarm->callback(alarm->id); + timeInfoVec.clear(); TIME_HILOGD(TIME_MODULE_SERVICE, "Shut down proxy, proxyUid: %{public}d, alarmId: %{public}" PRId64 "", uid, alarm->id); }