From f182d7f70b2ad9dd71b5684a6bfa03712f5dac1b Mon Sep 17 00:00:00 2001 From: cheerful_ricky Date: Fri, 30 May 2025 17:17:31 +0800 Subject: [PATCH] fix timer cancel don't trigger removeWantAgent and fix timer leak Signed-off-by: cheerful_ricky --- ...dvanced_notification_live_view_service.cpp | 3 +++ .../advanced_notification_publish.cpp | 8 +++----- .../advanced_notification_publish_service.cpp | 10 ++++++---- .../ans/src/advanced_notification_service.cpp | 11 +++++++---- ...anced_notification_service_branch_test.cpp | 19 +++++++++++++++++++ ...dvanced_notification_service_unit_test.cpp | 18 ++++++++++++++++++ 6 files changed, 56 insertions(+), 13 deletions(-) diff --git a/services/ans/src/advanced_notification_live_view_service.cpp b/services/ans/src/advanced_notification_live_view_service.cpp index 83c712360..b9345c323 100644 --- a/services/ans/src/advanced_notification_live_view_service.cpp +++ b/services/ans/src/advanced_notification_live_view_service.cpp @@ -116,6 +116,9 @@ ErrCode AdvancedNotificationService::UpdateNotificationTimerInfo(const std::shar ErrCode result = ERR_OK; if (!record->request->IsCommonLiveView()) { + if ((record->request->GetAutoDeletedTime() > GetCurrentTime())) { + StartAutoDeletedTimer(record); + } return ERR_OK; } diff --git a/services/ans/src/advanced_notification_manager/advanced_notification_publish.cpp b/services/ans/src/advanced_notification_manager/advanced_notification_publish.cpp index 70974cdd7..f1bc5ce08 100644 --- a/services/ans/src/advanced_notification_manager/advanced_notification_publish.cpp +++ b/services/ans/src/advanced_notification_manager/advanced_notification_publish.cpp @@ -241,17 +241,15 @@ ErrCode AdvancedNotificationService::PublishNotificationForIndirectProxy(const s sptr sortingMap = GenerateSortingMap(); NotificationSubscriberManager::GetInstance()->NotifyConsumed(record->notification, sortingMap); + if ((record->request->GetAutoDeletedTime() > GetCurrentTime()) && !record->request->IsCommonLiveView()) { + StartAutoDeletedTimer(record); + } }); notificationSvrQueue_->wait(handler); if (result != ERR_OK) { NotificationAnalyticsUtil::ReportPublishFailedEvent(request, message); return result; } - - if ((record->request->GetAutoDeletedTime() > GetCurrentTime()) && !record->request->IsCommonLiveView()) { - StartAutoDelete(record, - record->request->GetAutoDeletedTime(), NotificationConstant::TRIGGER_AUTO_DELETE_REASON_DELETE); - } return ERR_OK; } diff --git a/services/ans/src/advanced_notification_publish_service.cpp b/services/ans/src/advanced_notification_publish_service.cpp index 918387de0..5669f34e5 100644 --- a/services/ans/src/advanced_notification_publish_service.cpp +++ b/services/ans/src/advanced_notification_publish_service.cpp @@ -112,6 +112,9 @@ void AdvancedNotificationService::SetCollaborateReminderFlag(const sptr &record) { if (!record->request->IsCommonLiveView()) { + if ((record->request->GetAutoDeletedTime() > GetCurrentTime())) { + StartAutoDeletedTimer(record); + } return; } @@ -760,15 +763,14 @@ ErrCode AdvancedNotificationService::PublishNotificationBySa(const sptrnotification, false, 0); sptr sortingMap = GenerateSortingMap(); NotificationSubscriberManager::GetInstance()->NotifyConsumed(record->notification, sortingMap); + if ((record->request->GetAutoDeletedTime() > GetCurrentTime()) && !record->request->IsCommonLiveView()) { + StartAutoDeletedTimer(record); + } }); notificationSvrQueue_->wait(handler); if (result != ERR_OK) { return result; } - - if ((record->request->GetAutoDeletedTime() > GetCurrentTime()) && !record->request->IsCommonLiveView()) { - StartAutoDeletedTimer(record); - } return ERR_OK; } diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 3b3c07d44..51da96466 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -608,6 +608,10 @@ ErrCode AdvancedNotificationService::StartAutoDeletedTimer(const std::shared_ptr ANS_LOGE("%{public}s", message.c_str()); return ERR_ANS_TASK_ERR; } + uint64_t originTimerId = record->notification->GetAutoDeletedTimer(); + if (originTimerId != NotificationConstant::INVALID_TIMER_ID) { + CancelTimer(originTimerId); + } record->notification->SetAutoDeletedTimer(timerId); return ERR_OK; } @@ -755,10 +759,6 @@ ErrCode AdvancedNotificationService::PublishPreparedNotification(const sptrwait(handler); - // live view handled in UpdateNotificationTimerInfo, ignore here. - if ((record->request->GetAutoDeletedTime() > GetCurrentTime()) && !record->request->IsCommonLiveView()) { - StartAutoDeletedTimer(record); - } return result; } @@ -1128,6 +1128,8 @@ ErrCode AdvancedNotificationService::UpdateInNotificationList(const std::shared_ LIVEVIEW_ALL_SCENARIOS_EXTENTION_WRAPPER->UpdateLiveviewVoiceContent(record->request); } FillLockScreenPicture(record->request, (*iter)->request); + record->notification->SetAutoDeletedTimer((*iter)->notification->GetAutoDeletedTimer()); + record->notification->SetArchiveTimer((*iter)->notification->GetArchiveTimer()); record->notification->SetUpdateTimer((*iter)->notification->GetUpdateTimer()); if (!record->request->IsSystemLiveView()) { record->notification->SetFinishTimer((*iter)->notification->GetFinishTimer()); @@ -1834,6 +1836,7 @@ void AdvancedNotificationService::TriggerAutoDelete(const std::string &hashCode, if (record->notification->GetKey() == hashCode) { UpdateRecentNotification(record->notification, true, reason); + TriggerRemoveWantAgent(record->request, reason, record->isThirdparty); CancelTimer(record->notification->GetAutoDeletedTimer()); NotificationSubscriberManager::GetInstance()->NotifyCanceled(record->notification, nullptr, reason); ProcForDeleteLiveView(record); diff --git a/services/ans/test/unittest/advanced_notification_service_branch_test.cpp b/services/ans/test/unittest/advanced_notification_service_branch_test.cpp index f203eef34..d48872dd6 100644 --- a/services/ans/test/unittest/advanced_notification_service_branch_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_branch_test.cpp @@ -1256,6 +1256,25 @@ HWTEST_F(AnsBranchTest, AnsBranchTest_279002, Function | SmallTest | Level1) ASSERT_EQ(record->notification->GetFinishTimer(), NotificationConstant::INVALID_TIMER_ID); } +HWTEST_F(AnsBranchTest, AnsBranchTest_279003, Function | SmallTest | Level1) +{ + auto record = std::make_shared(); + NotificationRequest notificationRequest; + notificationRequest.SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION); + auto basicContent = std::make_shared(); + auto content = std::make_shared(basicContent); + notificationRequest.SetContent(content); + + record->request = sptr::MakeSptr(notificationRequest); + record->notification = new (std::nothrow) Notification(record->request); + + auto result = advancedNotificationService_->UpdateNotificationTimerInfo(record); + + ASSERT_EQ(result, ERR_OK); + /* finish timer not change, but update timer changed */ + ASSERT_EQ(record->notification->GetAutoDeletedTimer(), NotificationConstant::INVALID_TIMER_ID); +} + /** * @tc.number : GetDeviceRemindType_3000 * @tc.name : GetDeviceRemindType_3000 diff --git a/services/ans/test/unittest/advanced_notification_service_test/advanced_notification_service_unit_test.cpp b/services/ans/test/unittest/advanced_notification_service_test/advanced_notification_service_unit_test.cpp index a457a8b7a..dd5e4a78f 100644 --- a/services/ans/test/unittest/advanced_notification_service_test/advanced_notification_service_unit_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_test/advanced_notification_service_unit_test.cpp @@ -692,6 +692,24 @@ HWTEST_F(AdvancedNotificationServiceUnitTest, StartAutoDeletedTimer_200, Functio ASSERT_EQ(ret, (int)ERR_OK); } +/** + * @tc.name: StartAutoDeletedTimer_300 + * @tc.desc: Test StartAutoDeletedTimer when timer succeeded to create and cancel origin timer. + * @tc.type: FUNC + */ +HWTEST_F(AdvancedNotificationServiceUnitTest, StartAutoDeletedTimer_300, Function | SmallTest | Level1) +{ + sptr request = new (std::nothrow) NotificationRequest(); + auto bundle = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID); + auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle); + record->notification->SetAutoDeletedTimer(1); + + auto ret = advancedNotificationService_->StartAutoDeletedTimer(record); + advancedNotificationService_->TriggerAutoDelete("hashCode", 27); + + ASSERT_EQ(ret, (int)ERR_OK); +} + /** * @tc.name: ReportDoNotDisturbModeChanged_100 * @tc.desc: Test ReportDoNotDisturbModeChanged. -- Gitee