OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_PERMISSIONS_PERMISSION_DECISION_AUTO_BLOCKER_H_ | 5 #ifndef CHROME_BROWSER_PERMISSIONS_PERMISSION_DECISION_AUTO_BLOCKER_H_ |
6 #define CHROME_BROWSER_PERMISSIONS_PERMISSION_DECISION_AUTO_BLOCKER_H_ | 6 #define CHROME_BROWSER_PERMISSIONS_PERMISSION_DECISION_AUTO_BLOCKER_H_ |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/memory/singleton.h" | |
12 #include "components/keyed_service/content/browser_context_keyed_service_factory .h" | |
13 #include "components/keyed_service/core/keyed_service.h" | |
11 #include "content/public/browser/permission_type.h" | 14 #include "content/public/browser/permission_type.h" |
12 #include "url/gurl.h" | 15 #include "url/gurl.h" |
13 | 16 |
14 class GURL; | 17 class GURL; |
15 class Profile; | 18 class Profile; |
16 | 19 |
17 namespace content { | 20 namespace content { |
18 class WebContents; | 21 class WebContents; |
19 } | 22 } |
20 | 23 |
21 namespace safe_browsing { | 24 namespace safe_browsing { |
22 class SafeBrowsingDatabaseManager; | 25 class SafeBrowsingDatabaseManager; |
23 } | 26 } |
24 | 27 |
25 namespace base { | 28 namespace base { |
26 class Time; | 29 class Clock; |
27 } | 30 } |
28 | 31 |
29 class HostContentSettingsMap; | 32 class HostContentSettingsMap; |
30 | 33 |
31 // The PermissionDecisionAutoBlocker decides whether or not a given origin | 34 // The PermissionDecisionAutoBlocker decides whether or not a given origin |
32 // should be automatically blocked from requesting a permission. When an origin | 35 // should be automatically blocked from requesting a permission. When an origin |
33 // is blocked, it is placed under an "embargo". Until the embargo expires, any | 36 // is blocked, it is placed under an "embargo". Until the embargo expires, any |
34 // requests made by the origin are automatically blocked. Once the embargo is | 37 // requests made by the origin are automatically blocked. Once the embargo is |
35 // lifted, the origin will be permitted to request a permission again, which may | 38 // lifted, the origin will be permitted to request a permission again, which may |
36 // result in it being placed under embargo again. Currently, an origin can be | 39 // result in it being placed under embargo again. Currently, an origin can be |
37 // placed under embargo if it appears on Safe Browsing's API blacklist, or if it | 40 // placed under embargo if it appears on Safe Browsing's API blacklist, or if it |
38 // has a number of prior dismissals greater than a threshold. | 41 // has a number of prior dismissals greater than a threshold. |
39 class PermissionDecisionAutoBlocker { | 42 class PermissionDecisionAutoBlocker : public KeyedService { |
40 public: | 43 public: |
41 // Removes any recorded counts for urls which match |filter| under |profile|. | 44 static PermissionDecisionAutoBlocker* GetForProfile(Profile* profile); |
42 static void RemoveCountsByUrl(Profile* profile, | 45 |
43 base::Callback<bool(const GURL& url)> filter); | 46 class Factory : public BrowserContextKeyedServiceFactory { |
47 public: | |
48 static PermissionDecisionAutoBlocker* GetForProfile(Profile* profile); | |
49 static PermissionDecisionAutoBlocker::Factory* GetInstance(); | |
50 | |
51 private: | |
52 friend struct base::DefaultSingletonTraits<Factory>; | |
53 | |
54 Factory(); | |
55 ~Factory() override; | |
56 | |
57 // BrowserContextKeyedServiceFactory | |
58 KeyedService* BuildServiceInstanceFor( | |
59 content::BrowserContext* context) const override; | |
60 | |
61 content::BrowserContext* GetBrowserContextToUse( | |
62 content::BrowserContext* context) const override; | |
63 }; | |
64 | |
65 // Removes any recorded counts for urls which match |filter|. | |
66 void RemoveCountsByUrl(base::Callback<bool(const GURL& url)> filter); | |
44 | 67 |
45 // Returns the current number of dismisses recorded for |permission| type at | 68 // Returns the current number of dismisses recorded for |permission| type at |
46 // |url|. | 69 // |url|. |
47 static int GetDismissCount(const GURL& url, | 70 int GetDismissCount(const GURL& url, content::PermissionType permission); |
48 content::PermissionType permission, | |
49 Profile* profile); | |
50 | 71 |
51 // Returns the current number of ignores recorded for |permission| | 72 // Returns the current number of ignores recorded for |permission| |
52 // type at |url|. | 73 // type at |url|. |
53 static int GetIgnoreCount(const GURL& url, | 74 int GetIgnoreCount(const GURL& url, content::PermissionType permission); |
54 content::PermissionType permission, | |
55 Profile* profile); | |
56 | 75 |
57 // Records that a dismissal of a prompt for |permission| was made. If the | 76 // Records that a dismissal of a prompt for |permission| was made. If the |
58 // total number of dismissals exceeds a threshhold and | 77 // total number of dismissals exceeds a threshhold and |
59 // features::kBlockPromptsIfDismissedOften is enabled it will place |url| | 78 // features::kBlockPromptsIfDismissedOften is enabled it will place |url| |
60 // under embargo for |permission|. | 79 // under embargo for |permission|. |
61 static bool RecordDismissAndEmbargo(const GURL& url, | 80 bool RecordDismissAndEmbargo(const GURL& url, |
62 content::PermissionType permission, | 81 content::PermissionType permission); |
63 Profile* profile, | |
64 base::Time current_time); | |
65 | 82 |
66 // Records that an ignore of a prompt for |permission| was made. | 83 // Records that an ignore of a prompt for |permission| was made. |
67 static int RecordIgnore(const GURL& url, | 84 int RecordIgnore(const GURL& url, content::PermissionType permission); |
68 content::PermissionType permission, | |
69 Profile* profile); | |
70 | |
71 // Records that a dismissal of a prompt for |permission| was made, and returns | |
72 // true if this dismissal should be considered a block. False otherwise. | |
73 // TODO(meredithl): Remove in favour of embargoing on repeated dismissals. | |
74 static bool ShouldChangeDismissalToBlock(const GURL& url, | |
75 content::PermissionType permission, | |
76 Profile* profile); | |
77 | 85 |
78 // Updates the threshold to start blocking prompts from the field trial. | 86 // Updates the threshold to start blocking prompts from the field trial. |
79 static void UpdateFromVariations(); | 87 static void UpdateFromVariations(); |
80 | 88 |
81 // Checks if |request_origin| is under embargo for |permission|. Internally, | 89 // Checks if |request_origin| is under embargo for |permission|. Internally, |
82 // this will make a call to IsUnderEmbargo to check the content setting first, | 90 // this will make a call to IsUnderEmbargo to check the content setting first, |
83 // but may also make a call to Safe Browsing to check if |request_origin| is | 91 // but may also make a call to Safe Browsing to check the API blacklist, which |
84 // blacklisted for |permission|, which is performed asynchronously. | 92 // is performed asynchronously. |
85 static void UpdateEmbargoedStatus(Profile* profile, | 93 void UpdateEmbargoedStatus(content::PermissionType permission, |
86 base::Time current_time, | 94 const GURL& request_origin, |
87 base::Callback<void(bool)> callback); | 95 content::WebContents* web_contents, |
96 base::Callback<void(bool)> callback); | |
88 | 97 |
89 // Checks the status of the content setting to determine if |request_origin| | 98 // Checks the status of the content setting to determine if |request_origin| |
90 // is under embargo for |permission|. This checks both embargo for Permissions | 99 // is under embargo for |permission|. This checks both embargo for Permissions |
91 // Blacklisting and repeated dismissals. | 100 // Blacklisting and repeated dismissals. |
92 static bool IsUnderEmbargo(content::PermissionType permission, | 101 bool IsUnderEmbargo(content::PermissionType permission, |
93 Profile* profile, | 102 const GURL& request_origin); |
94 const GURL& request_origin, | |
95 base::Time current_time); | |
96 | 103 |
97 private: | 104 private: |
98 friend class PermissionContextBaseTests; | 105 friend class PermissionContextBaseTests; |
99 friend class PermissionDecisionAutoBlockerUnitTest; | 106 friend class PermissionDecisionAutoBlockerUnitTest; |
100 | 107 |
108 explicit PermissionDecisionAutoBlocker(Profile* profile); | |
109 ~PermissionDecisionAutoBlocker() override; | |
110 | |
101 // Get the result of the Safe Browsing check, if |should_be_embargoed| is true | 111 // Get the result of the Safe Browsing check, if |should_be_embargoed| is true |
102 // then |request_origin| will be placed under embargo for that |permission|. | 112 // then |request_origin| will be placed under embargo for that |permission|. |
103 static void CheckSafeBrowsingResult(content::PermissionType permission, | 113 static void CheckSafeBrowsingResult(content::PermissionType permission, |
104 Profile* profile, | 114 Profile* profile, |
105 const GURL& request_origin, | 115 const GURL& request_origin, |
106 base::Time current_time, | 116 base::Time current_time, |
107 base::Callback<void(bool)> callback, | 117 base::Callback<void(bool)> callback, |
108 bool should_be_embargoed); | 118 bool should_be_embargoed); |
109 | 119 |
110 static void PlaceUnderEmbargo(content::PermissionType permission, | 120 static void PlaceUnderEmbargo(content::PermissionType permission, |
111 const GURL& request_origin, | 121 const GURL& request_origin, |
112 HostContentSettingsMap* map, | 122 HostContentSettingsMap* map, |
113 base::Time current_time, | 123 base::Time current_time, |
114 const char* key); | 124 const char* key); |
115 | 125 |
116 // Updates the embargo status of a |permission| for a given |request_origin|. | 126 // Updates the embargo status of a |permission| for a given |request_origin|. |
117 static void UpdateEmbargoStatus(content::PermissionType permission, | 127 static void UpdateEmbargoStatus(content::PermissionType permission, |
118 Profile* profile, | 128 Profile* profile, |
119 const GURL& request_origin, | 129 const GURL& request_origin, |
120 base::Callback<void(bool)> callback, | 130 base::Callback<void(bool)> callback, |
121 bool embargo); | 131 bool embargo); |
raymes
2017/01/24 03:31:08
Hmm, what is this for?
meredithl
2017/01/24 04:52:26
Oh goodness, just a result of a bad rebase I think
| |
122 | 132 |
133 void SetSafeBrowsingDatabaseManagerAndTimeoutForTesting( | |
134 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> db_manager, | |
135 int timeout); | |
136 | |
137 void SetClockForTesting(std::unique_ptr<base::Clock> clock); | |
138 | |
123 // Keys used for storing count data in a website setting. | 139 // Keys used for storing count data in a website setting. |
124 static const char kPromptDismissCountKey[]; | 140 static const char kPromptDismissCountKey[]; |
125 static const char kPromptIgnoreCountKey[]; | 141 static const char kPromptIgnoreCountKey[]; |
126 static const char kPermissionDismissalEmbargoKey[]; | 142 static const char kPermissionDismissalEmbargoKey[]; |
127 static const char kPermissionBlacklistEmbargoKey[]; | 143 static const char kPermissionBlacklistEmbargoKey[]; |
128 | 144 |
145 Profile* profile_; | |
146 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> db_manager_; | |
147 | |
148 // timeout in ms. | |
raymes
2017/01/24 03:31:08
nit: Timeout (with capital)
meredithl
2017/01/24 04:52:26
Done.
| |
149 int safe_browsing_timeout_; | |
150 | |
151 std::unique_ptr<base::Clock> clock_; | |
152 | |
129 DISALLOW_IMPLICIT_CONSTRUCTORS(PermissionDecisionAutoBlocker); | 153 DISALLOW_IMPLICIT_CONSTRUCTORS(PermissionDecisionAutoBlocker); |
130 }; | 154 }; |
131 | |
132 #endif // CHROME_BROWSER_PERMISSIONS_PERMISSION_DECISION_AUTO_BLOCKER_H_ | 155 #endif // CHROME_BROWSER_PERMISSIONS_PERMISSION_DECISION_AUTO_BLOCKER_H_ |
OLD | NEW |