notifications_engagement_service.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_PERMISSIONS_NOTIFICATIONS_ENGAGEMENT_SERVICE_H_
  5. #define COMPONENTS_PERMISSIONS_NOTIFICATIONS_ENGAGEMENT_SERVICE_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/time/time.h"
  8. #include "components/content_settings/core/browser/host_content_settings_map.h"
  9. #include "components/keyed_service/core/keyed_service.h"
  10. class GURL;
  11. class PrefService;
  12. namespace content {
  13. class BrowserContext;
  14. } // namespace content
  15. namespace permissions {
  16. // This class records and stores notification engagement per origin for the
  17. // past 90 days. Engagements per origin are bucketed by week: A notification
  18. // engagement or display is assigned to the last Monday midnight in local time.
  19. class NotificationsEngagementService : public KeyedService {
  20. public:
  21. explicit NotificationsEngagementService(content::BrowserContext* context,
  22. PrefService* pref_service);
  23. NotificationsEngagementService(const NotificationsEngagementService&) =
  24. delete;
  25. NotificationsEngagementService& operator=(
  26. const NotificationsEngagementService&) = delete;
  27. ~NotificationsEngagementService() override = default;
  28. // KeyedService implementation.
  29. void Shutdown() override;
  30. void RecordNotificationDisplayed(const GURL& url);
  31. void RecordNotificationInteraction(const GURL& url);
  32. // ISO8601 defines Monday as the first day of the week. Additionally, in most
  33. // of the world the workweek starts with Monday, so Monday is used
  34. // specifically here, as notification usage is a function of work/personal
  35. // settings.
  36. static std::string GetBucketLabelForLastMonday(base::Time time);
  37. static absl::optional<base::Time> ParsePeriodBeginFromBucketLabel(
  38. const std::string& label);
  39. private:
  40. void IncrementCounts(const GURL& url,
  41. const int display_count_delta,
  42. const int click_count_delta);
  43. // Used to update the notification engagement per URL.
  44. raw_ptr<HostContentSettingsMap> settings_map_;
  45. raw_ptr<PrefService> pref_service_;
  46. };
  47. } // namespace permissions
  48. #endif // COMPONENTS_PERMISSIONS_NOTIFICATIONS_ENGAGEMENT_SERVICE_H_