File: notification_permission_review_service.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (181 lines) | stat: -rw-r--r-- 7,368 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_UI_SAFETY_HUB_NOTIFICATION_PERMISSION_REVIEW_SERVICE_H_
#define CHROME_BROWSER_UI_SAFETY_HUB_NOTIFICATION_PERMISSION_REVIEW_SERVICE_H_

#include <utility>
#include <vector>

#include "base/scoped_observation.h"
#include "base/values.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/safety_hub/safety_hub_service.h"
#include "components/content_settings/core/browser/content_settings_observer.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings_pattern.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/site_engagement/content/site_engagement_service.h"

inline constexpr char kSafetyHubNotificationInfoString[] =
    "notificationInfoString";
inline constexpr char kSafetyHubNotificationPermissionsResultKey[] =
    "notificationPermissions";

struct NotificationPermissions {
  ContentSettingsPattern primary_pattern;
  ContentSettingsPattern secondary_pattern;
  int notification_count;

  NotificationPermissions(const ContentSettingsPattern& primary_pattern,
                          const ContentSettingsPattern& secondary_pattern,
                          int notification_count);
  ~NotificationPermissions();
};

// This class provides data for the "Review Notification Permissions" dialog.
// This module shows the domains that send a lot of notification, but have low
// engagement.
class NotificationPermissionsReviewService : public SafetyHubService,
                                             public content_settings::Observer {
 public:
  // The result of the periodic update contains the sites that sent a large
  // number of notifications, along with the number of notifications that they
  // sent. The sites that are added to the review blocklist should not be added
  // here.
  class NotificationPermissionsResult : public SafetyHubService::Result {
   public:
    NotificationPermissionsResult();

    NotificationPermissionsResult(const NotificationPermissionsResult&);
    NotificationPermissionsResult& operator=(
        const NotificationPermissionsResult&) = default;

    ~NotificationPermissionsResult() override;

    // TODO(crbug.com/40267370): Make methods private if they are not required
    // to be public.

    void AddNotificationPermission(const NotificationPermissions&);

    std::vector<NotificationPermissions> GetSortedNotificationPermissions();

    std::set<ContentSettingsPattern> GetOrigins() const;

    base::Value::List GetSortedListValueForUI();

    // SafetyHubService::Result implementation

    std::unique_ptr<SafetyHubService::Result> Clone() const override;

    base::Value::Dict ToDictValue() const override;

    bool IsTriggerForMenuNotification() const override;

    bool WarrantsNewMenuNotification(
        const base::Value::Dict& previous_result_dict) const override;

    std::u16string GetNotificationString() const override;

    int GetNotificationCommandId() const override;

   private:
    std::vector<NotificationPermissions> notification_permissions_;
  };

  explicit NotificationPermissionsReviewService(
      HostContentSettingsMap* hcsm,
      site_engagement::SiteEngagementService* engagement_service);

  NotificationPermissionsReviewService(
      const NotificationPermissionsReviewService&) = delete;
  NotificationPermissionsReviewService& operator=(
      const NotificationPermissionsReviewService&) = delete;

  ~NotificationPermissionsReviewService() override;

  // content_settings::Observer implementation.
  void OnContentSettingChanged(
      const ContentSettingsPattern& primary_pattern,
      const ContentSettingsPattern& secondary_pattern,
      ContentSettingsTypeSet content_type_set) override;

  // KeyedService implementation.
  void Shutdown() override;

  // SafetyHubService implementation.
  // Returns a weak pointer to the service.
  base::WeakPtr<SafetyHubService> GetAsWeakRef() override;

  // Add given pattern pair to the blocklist for the "Review notification
  // permission" feature. The patterns in blocklist will not be suggested to be
  // reviewed by the user again.
  void AddPatternToNotificationPermissionReviewBlocklist(
      const ContentSettingsPattern& primary_pattern,
      const ContentSettingsPattern& secondary_pattern);

  // Removes given origin from the blocklist for the "Review notification
  // permission" feature. The pattern may be suggested again for review.
  void RemovePatternFromNotificationPermissionReviewBlocklist(
      const ContentSettingsPattern& primary_pattern,
      const ContentSettingsPattern& secondary_pattern);

  // Returns a sorted list with the notification count for each domain to be
  // shown on the 'Review Notification Permissions' dialog. Those domains send a
  // lot of notifications, but have low site engagement.
  base::Value::List PopulateNotificationPermissionReviewData();

  // Returns the list of all notification permissions that should be reviewed.
  std::unique_ptr<Result> GetNotificationPermissions();

  // Sets the notification permission for the given origin.
  void SetNotificationPermissionsForOrigin(std::string origin,
                                           ContentSetting setting);

 private:
  // SafetyHubService implementation

  // Initializes the latest result to the notifications that should be reviewed.
  std::unique_ptr<SafetyHubService::Result> InitializeLatestResultImpl()
      override;

  // Returns the interval at which the repeated updates will be run.
  base::TimeDelta GetRepeatedUpdateInterval() override;

  // For the notification permission review service, there is not background
  // task. Instead, all operations happen on the UI thread.
  base::OnceCallback<std::unique_ptr<Result>()> GetBackgroundTask() override;

  // A boilerplate function that returns an empty result.
  static std::unique_ptr<SafetyHubService::Result> UpdateOnBackgroundThread();

  // Gathers all the sites that sent a lot of notifications, and that the user
  // should review.
  std::unique_ptr<Result> UpdateOnUIThread(
      std::unique_ptr<Result> result) override;

  // Returns |true| when the URL and notification count combination meets the
  // criteria for adding the origin to the review list.
  bool ShouldAddToNotificationPermissionReviewList(GURL url,
                                                   int notification_count);

  // Whether notifications from disruptive sites are revoked. In that case, the
  // notification permission module should be hidden.
  bool IsDisruptiveNotificationRevocationEnabled();

  // Used to determine how often the user engaged with websites.
  raw_ptr<site_engagement::SiteEngagementService> engagement_service_;

  // Used to update the notification permissions per URL.
  const scoped_refptr<HostContentSettingsMap> hcsm_;

  // Observer to watch for content settings changed.
  base::ScopedObservation<HostContentSettingsMap, content_settings::Observer>
      content_settings_observation_{this};

  base::WeakPtrFactory<NotificationPermissionsReviewService> weak_factory_{
      this};
};

#endif  // CHROME_BROWSER_UI_SAFETY_HUB_NOTIFICATION_PERMISSION_REVIEW_SERVICE_H_