File: permission_decision_auto_blocker.h

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (221 lines) | stat: -rw-r--r-- 9,892 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_PERMISSIONS_PERMISSION_DECISION_AUTO_BLOCKER_H_
#define COMPONENTS_PERMISSIONS_PERMISSION_DECISION_AUTO_BLOCKER_H_

#include <set>

#include "base/functional/callback.h"
#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/observer_list_types.h"
#include "base/time/default_clock.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "components/keyed_service/core/keyed_service.h"
#include "content/public/browser/permission_result.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "url/gurl.h"

class GURL;

namespace settings {
FORWARD_DECLARE_TEST(SiteSettingsHandlerTest, GetAllSites);
FORWARD_DECLARE_TEST(SiteSettingsHandlerTest, GetRecentSitePermissions);
FORWARD_DECLARE_TEST(SiteSettingsHandlerTest,
                     StorageAccessExceptions_Description_Embargoed);
FORWARD_DECLARE_TEST(SiteSettingsHandlerTest,
                     StorageAccessExceptions_Description_EmbargoedTwoProfiles);
}  // namespace settings

namespace site_settings {
FORWARD_DECLARE_TEST(RecentSiteSettingsHelperTest, CheckRecentSitePermissions);
}  // namespace site_settings

namespace permissions {

// Mockable interface for PermissionDecisionAutoBlocker (see below), for those
// few instances where this is used outside of permissions and needs separate
// unit tests.
class PermissionDecisionAutoBlockerBase {
 public:
  PermissionDecisionAutoBlockerBase() = default;
  virtual ~PermissionDecisionAutoBlockerBase() = default;

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

  // Returns whether |request_origin| is under embargo for |permission|.
  virtual bool IsEmbargoed(const GURL& request_origin,
                           ContentSettingsType permission) = 0;

  // Records that a dismissal of a prompt for |permission| was made. If the
  // total number of dismissals exceeds a threshhold and
  // features::kBlockPromptsIfDismissedOften is enabled, it will place |url|
  // under embargo for |permission|. |dismissed_prompt_was_quiet| will inform
  // the decision of which threshold to pick, depending on whether the prompt
  // that was presented to the user was quiet or not.
  virtual bool RecordDismissAndEmbargo(const GURL& url,
                                       ContentSettingsType permission,
                                       bool dismissed_prompt_was_quiet) = 0;

  // Records that an ignore of a prompt for |permission| was made. If the
  // total number of ignores exceeds a threshold and
  // features::kBlockPromptsIfIgnoredOften is enabled, it will place |url|
  // under embargo for |permission|. |ignored_prompt_was_quiet| will inform
  // the decision of which threshold to pick, depending on whether the prompt
  // that was presented to the user was quiet or not.
  virtual bool RecordIgnoreAndEmbargo(const GURL& url,
                                      ContentSettingsType permission,
                                      bool ignored_prompt_was_quiet) = 0;
};

// The PermissionDecisionAutoBlocker decides whether or not a given origin
// should be automatically blocked from requesting a permission. When an origin
// is blocked, it is placed under an "embargo". Until the embargo expires, any
// requests made by the origin are automatically blocked. Once the embargo is
// lifted, the origin will be permitted to request a permission again, which may
// result in it being placed under embargo again. Currently, an origin can be
// placed under embargo if it has a number of prior dismissals greater than a
// threshold.
class PermissionDecisionAutoBlocker : public PermissionDecisionAutoBlockerBase,
                                      public KeyedService {
 public:
  class Observer : public base::CheckedObserver {
   public:
    virtual void OnEmbargoStarted(const GURL& origin,
                                  ContentSettingsType content_setting) = 0;
  };

  PermissionDecisionAutoBlocker() = delete;

  explicit PermissionDecisionAutoBlocker(HostContentSettingsMap* settings_map);

  ~PermissionDecisionAutoBlocker() override;

  // PermissionDecisionAutoBlockerBase
  bool IsEmbargoed(const GURL& request_origin,
                   ContentSettingsType permission) override;
  bool RecordDismissAndEmbargo(const GURL& url,
                               ContentSettingsType permission,
                               bool dismissed_prompt_was_quiet) override;
  bool RecordIgnoreAndEmbargo(const GURL& url,
                              ContentSettingsType permission,
                              bool ignored_prompt_was_quiet) override;

  // Returns whether the permission auto blocker is enabled for the passed-in
  // content setting.
  static bool IsEnabledForContentSetting(ContentSettingsType content_setting);

  // Checks the status of the content setting to determine if |request_origin|
  // is under embargo for |permission|. This checks all types of embargo.
  // Prefer to use PermissionManager::GetPermissionStatus when possible. This
  // method is only exposed to facilitate permission checks from threads other
  // than the UI thread. See https://crbug.com/658020.
  static absl::optional<content::PermissionResult> GetEmbargoResult(
      HostContentSettingsMap* settings_map,
      const GURL& request_origin,
      ContentSettingsType permission,
      base::Time current_time);

  // Updates the threshold to start blocking prompts from the field trial.
  static void UpdateFromVariations();

  // Checks the status of the content setting to determine if |request_origin|
  // is under embargo for |permission|. This checks all types of embargo.
  absl::optional<content::PermissionResult> GetEmbargoResult(
      const GURL& request_origin,
      ContentSettingsType permission);

  // Returns the most recent recorded time either an ignore or dismiss embargo
  // was started. Records of embargo start times persist beyond the duration
  // of the embargo, but are removed along with embargoes when
  // RemoveEmbargoAndResetCounts is used. Returns base::Time() if no record is
  // found.
  base::Time GetEmbargoStartTime(const GURL& request_origin,
                                 ContentSettingsType permission);

  // Returns the current number of dismisses recorded for |permission| type at
  // |url|.
  int GetDismissCount(const GURL& url, ContentSettingsType permission);

  // Returns the current number of ignores recorded for |permission|
  // type at |url|.
  int GetIgnoreCount(const GURL& url, ContentSettingsType permission);

  // Returns a set of urls currently under embargo for |content_type|.
  std::set<GURL> GetEmbargoedOrigins(ContentSettingsType content_type);

  // Returns a set of urls currently under embargo for the provided
  // |content_type| types.
  std::set<GURL> GetEmbargoedOrigins(
      std::vector<ContentSettingsType> content_types);

  // Records that a prompt was displayed for |permission|. If
  // features::kBlockRepeatedAutoReauthnPrompts is enabled, it will place |url|
  // under embargo for |permission|.
  bool RecordDisplayAndEmbargo(const GURL& url, ContentSettingsType permission);

  // Clears any existing embargo status for |url|, |permission|. For
  // permissions embargoed under repeated dismissals, this means a prompt will
  // be shown to the user on next permission request. Clears dismiss and
  // ignore counts.
  void RemoveEmbargoAndResetCounts(const GURL& url,
                                   ContentSettingsType permission);

  // Same as above, but cleans the slate for all permissions and for all URLs
  // matching |filter|.
  void RemoveEmbargoAndResetCounts(
      base::RepeatingCallback<bool(const GURL& url)> filter);

  // Add and remove observers that want to receive embargo status updates.
  void AddObserver(Observer* obs);
  void RemoveObserver(Observer* obs);

  static const char* GetPromptDismissCountKeyForTesting();

 private:
  friend class PermissionDecisionAutoBlockerUnitTest;
  FRIEND_TEST_ALL_PREFIXES(site_settings::RecentSiteSettingsHelperTest,
                           CheckRecentSitePermissions);
  FRIEND_TEST_ALL_PREFIXES(settings::SiteSettingsHandlerTest, GetAllSites);
  FRIEND_TEST_ALL_PREFIXES(settings::SiteSettingsHandlerTest,
                           GetRecentSitePermissions);
  FRIEND_TEST_ALL_PREFIXES(settings::SiteSettingsHandlerTest,
                           StorageAccessExceptions_Description_Embargoed);
  FRIEND_TEST_ALL_PREFIXES(
      settings::SiteSettingsHandlerTest,
      StorageAccessExceptions_Description_EmbargoedTwoProfiles);

  void PlaceUnderEmbargo(const GURL& request_origin,
                         ContentSettingsType permission,
                         const char* key);

  void NotifyEmbargoStarted(const GURL& origin,
                            ContentSettingsType content_setting);

  void SetClockForTesting(base::Clock* clock);

  // Keys used for storing count data in a website setting.
  static const char kPromptDismissCountKey[];
  static const char kPromptIgnoreCountKey[];
  static const char kPromptDismissCountWithQuietUiKey[];
  static const char kPromptIgnoreCountWithQuietUiKey[];
  static const char kPermissionDismissalEmbargoKey[];
  static const char kPermissionIgnoreEmbargoKey[];
  static const char kPermissionDisplayEmbargoKey[];

  raw_ptr<HostContentSettingsMap> settings_map_;

  raw_ptr<base::Clock> clock_;

  base::ObserverList<Observer> observers_;
};

}  // namespace permissions

#endif  // COMPONENTS_PERMISSIONS_PERMISSION_DECISION_AUTO_BLOCKER_H_