1 // Copyright 2022 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_PERMISSIONS_NOTIFICATIONS_ENGAGEMENT_SERVICE_H_
6 #define COMPONENTS_PERMISSIONS_NOTIFICATIONS_ENGAGEMENT_SERVICE_H_
8 #include "base/memory/raw_ptr.h"
9 #include "base/time/time.h"
10 #include "components/content_settings/core/browser/host_content_settings_map.h"
11 #include "components/keyed_service/core/keyed_service.h"
18 } // namespace content
20 namespace permissions {
22 // This class records and stores notification engagement per origin for the
23 // past 30 days. Engagements per origin are bucketed daily: A notification
24 // engagement or display is assigned to the midnight in local time.
25 class NotificationsEngagementService : public KeyedService {
27 explicit NotificationsEngagementService(content::BrowserContext* context,
28 PrefService* pref_service);
30 NotificationsEngagementService(const NotificationsEngagementService&) =
32 NotificationsEngagementService& operator=(
33 const NotificationsEngagementService&) = delete;
35 ~NotificationsEngagementService() override = default;
37 // KeyedService implementation.
38 void Shutdown() override;
40 void RecordNotificationDisplayed(const GURL& url);
41 void RecordNotificationDisplayed(const GURL& url, int display_count);
42 void RecordNotificationInteraction(const GURL& url);
44 static std::string GetBucketLabel(base::Time time);
45 static absl::optional<base::Time> ParsePeriodBeginFromBucketLabel(
46 const std::string& label);
49 void IncrementCounts(const GURL& url,
50 const int display_count_delta,
51 const int click_count_delta);
53 // Used to update the notification engagement per URL.
54 raw_ptr<HostContentSettingsMap> settings_map_;
56 raw_ptr<PrefService> pref_service_;
59 } // namespace permissions
61 #endif // COMPONENTS_PERMISSIONS_NOTIFICATIONS_ENGAGEMENT_SERVICE_H_