diff --git a/services/ans/src/common/notification_analytics_util.cpp b/services/ans/src/common/notification_analytics_util.cpp index b8a0e8db4095f762466037f480bee03ebf41342c..dfb3090e166b695edbf4108c66a741d5a9a59e66 100644 --- a/services/ans/src/common/notification_analytics_util.cpp +++ b/services/ans/src/common/notification_analytics_util.cpp @@ -93,6 +93,7 @@ static uint64_t reportAggregateTimeId = 0; static std::list successReportCacheList; static bool g_successReportFlag = false; static std::shared_ptr reportAggregateTimeInfo = std::make_shared(); +static ffrt::mutex reportAggListMutex_; static std::list reportAggList; static int32_t SLOT_REPORT_INTERVAL = 7 * 24 * NotificationConstant::HOUR_TO_MS; @@ -592,23 +593,26 @@ void NotificationAnalyticsUtil::CreateLiveViewTimerExecute() void NotificationAnalyticsUtil::ExecuteLiveViewReport() { - std::lock_guard lock(ReportLiveViewMessageMutex_); - if (liveViewMessages.empty()) { - ANS_LOGI("report end"); - g_reportLiveViewFlag = false; - return; - } - if (reportAggregateTimeId == 0) { - sptr aggregateTimer = MiscServices::TimeServiceClient::GetInstance(); - if (aggregateTimer == nullptr) { - ANS_LOGE("null aggregateTimer"); + { + std::lock_guard lockReportLiveView(ReportLiveViewMessageMutex_); + if (liveViewMessages.empty()) { + ANS_LOGI("report end"); g_reportLiveViewFlag = false; return; } - reportAggregateTimeId = aggregateTimer->CreateTimer(reportAggregateTimeInfo); + if (reportAggregateTimeId == 0) { + sptr aggregateTimer = MiscServices::TimeServiceClient::GetInstance(); + if (aggregateTimer == nullptr) { + ANS_LOGE("null aggregateTimer"); + g_reportLiveViewFlag = false; + return; + } + reportAggregateTimeId = aggregateTimer->CreateTimer(reportAggregateTimeInfo); + } + ReportCache reportCache = AggregateLiveView(); + std::lock_guard lockReportAggList(reportAggListMutex_); + reportAggList.emplace_back(reportCache); } - ReportCache reportCache = AggregateLiveView(); - reportAggList.emplace_back(reportCache); if (!g_successReportFlag) { ExecuteSuccessCacheList(); } @@ -1251,16 +1255,18 @@ void NotificationAnalyticsUtil::AggregateBadgeChange() reportAggregateTimeId = aggregateTimer->CreateTimer(reportAggregateTimeInfo); } + for (auto bundle : removeBundles) { + badgeInfos.erase(bundle); + } + } + { + std::lock_guard lock(reportAggListMutex_); ReportCache reportInfo; reportInfo.want = want; reportInfo.eventCode = ANS_CUSTOMIZE_CODE ; reportAggList.emplace_back(reportInfo); - - for (auto bundle : removeBundles) { - badgeInfos.erase(bundle); - } } - + if (!g_successReportFlag) { ExecuteSuccessCacheList(); } @@ -1268,26 +1274,28 @@ void NotificationAnalyticsUtil::AggregateBadgeChange() void NotificationAnalyticsUtil::AddSuccessListCache(EventFwk::Want& want, int32_t eventCode) { - std::lock_guard lock(reportSuccessCacheMutex_); - int32_t size = static_cast(successReportCacheList.size()); - if (size >= SUCCESS_REPORT_CACHE_MAX_SIZE) { - ANS_LOGW("Success list size is max."); - return; - } - - if (reportAggregateTimeId == 0) { - sptr aggregateTimer = MiscServices::TimeServiceClient::GetInstance(); - if (aggregateTimer == nullptr) { - ANS_LOGE("null aggregateTimer"); + { + std::lock_guard lock(reportSuccessCacheMutex_); + int32_t size = static_cast(successReportCacheList.size()); + if (size >= SUCCESS_REPORT_CACHE_MAX_SIZE) { + ANS_LOGW("Success list size is max."); return; } - reportAggregateTimeId = aggregateTimer->CreateTimer(reportAggregateTimeInfo); - } - ReportCache reportCache; - reportCache.want = want; - reportCache.eventCode = eventCode; - successReportCacheList.push_back(reportCache); + if (reportAggregateTimeId == 0) { + sptr aggregateTimer = MiscServices::TimeServiceClient::GetInstance(); + if (aggregateTimer == nullptr) { + ANS_LOGE("null aggregateTimer"); + return; + } + reportAggregateTimeId = aggregateTimer->CreateTimer(reportAggregateTimeInfo); + } + + ReportCache reportCache; + reportCache.want = want; + reportCache.eventCode = eventCode; + successReportCacheList.push_back(reportCache); + } if (!g_successReportFlag) { ExecuteSuccessCacheList(); } @@ -1332,30 +1340,39 @@ ReportCache NotificationAnalyticsUtil::Aggregate() void NotificationAnalyticsUtil::ExecuteSuccessCacheList() { - if (successReportCacheList.empty()) { - ANS_LOGI("successReportCacheList is empty"); - if (reportAggList.empty()) { - g_successReportFlag = false; - ANS_LOGI("No aggregate data need report"); - return; - } - } else { - while (!successReportCacheList.empty()) { + { + std::lock_guard lockReportSuccessCache(reportSuccessCacheMutex_); + if (successReportCacheList.empty()) { + ANS_LOGI("successReportCacheList is empty"); + std::lock_guard lockReportAggList(reportAggListMutex_); + if (reportAggList.empty()) { + g_successReportFlag = false; + ANS_LOGI("No aggregate data need report"); + return; + } + auto reportCount = MAX_REPORT_COUNT; + while (reportCount > 0 && !reportAggList.empty()) { + auto reportCache = reportAggList.front(); + ReportCommonEvent(reportCache); + reportAggList.pop_front(); + reportCount--; + } + } else { auto reportCache = Aggregate(); + std::lock_guard lockReportAggList(reportAggListMutex_); reportAggList.emplace_back(reportCache); + auto reportCount = MAX_REPORT_COUNT; + while (reportCount > 0 && !reportAggList.empty()) { + auto reportCache = reportAggList.front(); + ReportCommonEvent(reportCache); + reportAggList.pop_front(); + reportCount--; + } } } - auto reportCount = MAX_REPORT_COUNT; - while (reportCount > 0 && !reportAggList.empty()) { - auto reportCache = reportAggList.front(); - ReportCommonEvent(reportCache); - reportAggList.pop_front(); - reportCount--; - } CheckBadgeReport(); auto triggerFunc = [] { - std::lock_guard lock(reportSuccessCacheMutex_); NotificationAnalyticsUtil::ExecuteSuccessCacheList(); }; reportAggregateTimeInfo->SetCallbackInfo(triggerFunc); diff --git a/services/ans/test/unittest/notification_subscriber_manager_test.cpp b/services/ans/test/unittest/notification_subscriber_manager_test.cpp index e392378167214ae2b0314af07c29d21bb55498ff..addf96dfdb2fe72c8d2b0411f3504aaa794085b2 100644 --- a/services/ans/test/unittest/notification_subscriber_manager_test.cpp +++ b/services/ans/test/unittest/notification_subscriber_manager_test.cpp @@ -970,6 +970,11 @@ HWTEST_F(NotificationSubscriberManagerTest, NotifyConsumedInner_002, Function | sptr request = new NotificationRequest(); request->SetCreatorUid(DEFAULT_UID); request->SetOwnerBundleName("test1"); + request->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION); + request->SetNotificationId(1); + auto normalContent = std::make_shared(); + auto content = std::make_shared(normalContent); + request->SetContent(content); sptr notification = new Notification(request); sptr notificationMap = new (std::nothrow) NotificationSortingMap(); @@ -1022,6 +1027,11 @@ HWTEST_F(NotificationSubscriberManagerTest, NotifyConsumedInner_004, Function | sptr request = new NotificationRequest(); request->SetCreatorUid(DEFAULT_UID); request->SetOwnerBundleName("test1"); + request->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION); + request->SetNotificationId(1); + auto normalContent = std::make_shared(); + auto content = std::make_shared(normalContent); + request->SetContent(content); sptr notification = new Notification(request); sptr notificationMap = nullptr;