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 "base/time/default_clock.h" | |
13 #include "components/keyed_service/content/browser_context_keyed_service_factory .h" | |
14 #include "components/keyed_service/core/keyed_service.h" | |
11 #include "content/public/browser/permission_type.h" | 15 #include "content/public/browser/permission_type.h" |
12 #include "url/gurl.h" | 16 #include "url/gurl.h" |
13 | 17 |
14 class GURL; | 18 class GURL; |
15 class Profile; | 19 class Profile; |
16 | 20 |
17 namespace content { | 21 namespace content { |
18 class WebContents; | 22 class WebContents; |
19 } | 23 } |
20 | 24 |
21 namespace safe_browsing { | 25 namespace safe_browsing { |
22 class SafeBrowsingDatabaseManager; | 26 class SafeBrowsingDatabaseManager; |
23 } | 27 } |
24 | 28 |
25 namespace base { | |
26 class Time; | |
27 } | |
28 | |
29 class HostContentSettingsMap; | |
30 | |
31 // The PermissionDecisionAutoBlocker decides whether or not a given origin | 29 // The PermissionDecisionAutoBlocker decides whether or not a given origin |
32 // should be automatically blocked from requesting a permission. When an origin | 30 // 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 | 31 // 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 | 32 // 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 | 33 // 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 | 34 // 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 | 35 // 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. | 36 // has a number of prior dismissals greater than a threshold. |
39 class PermissionDecisionAutoBlocker { | 37 class PermissionDecisionAutoBlocker : public KeyedService { |
40 public: | 38 public: |
41 // Removes any recorded counts for urls which match |filter| under |profile|. | 39 class Factory : public BrowserContextKeyedServiceFactory { |
42 static void RemoveCountsByUrl(Profile* profile, | 40 public: |
43 base::Callback<bool(const GURL& url)> filter); | 41 static PermissionDecisionAutoBlocker* GetForProfile(Profile* profile); |
42 static PermissionDecisionAutoBlocker::Factory* GetInstance(); | |
43 | |
44 private: | |
45 friend struct base::DefaultSingletonTraits<Factory>; | |
46 | |
47 Factory(); | |
48 ~Factory() override; | |
49 | |
50 // BrowserContextKeyedServiceFactory | |
51 KeyedService* BuildServiceInstanceFor( | |
52 content::BrowserContext* context) const override; | |
53 | |
54 content::BrowserContext* GetBrowserContextToUse( | |
55 content::BrowserContext* context) const override; | |
56 }; | |
57 | |
58 static PermissionDecisionAutoBlocker* GetForProfile(Profile* profile); | |
59 | |
60 // Removes any recorded counts for urls which match |filter|. | |
61 void RemoveCountsByUrl(base::Callback<bool(const GURL& url)> filter); | |
44 | 62 |
45 // Returns the current number of dismisses recorded for |permission| type at | 63 // Returns the current number of dismisses recorded for |permission| type at |
46 // |url|. | 64 // |url|. |
47 static int GetDismissCount(const GURL& url, | 65 int GetDismissCount(const GURL& url, content::PermissionType permission); |
48 content::PermissionType permission, | |
49 Profile* profile); | |
50 | 66 |
51 // Returns the current number of ignores recorded for |permission| | 67 // Returns the current number of ignores recorded for |permission| |
52 // type at |url|. | 68 // type at |url|. |
53 static int GetIgnoreCount(const GURL& url, | 69 int GetIgnoreCount(const GURL& url, content::PermissionType permission); |
54 content::PermissionType permission, | |
55 Profile* profile); | |
56 | 70 |
57 // Records that a dismissal of a prompt for |permission| was made. If the | 71 // Records that a dismissal of a prompt for |permission| was made. If the |
58 // total number of dismissals exceeds a threshhold and | 72 // total number of dismissals exceeds a threshhold and |
59 // features::kBlockPromptsIfDismissedOften is enabled it will place |url| | 73 // features::kBlockPromptsIfDismissedOften is enabled it will place |url| |
60 // under embargo for |permission|. | 74 // under embargo for |permission|. |
61 static bool RecordDismissAndEmbargo(const GURL& url, | 75 bool RecordDismissAndEmbargo(const GURL& url, |
62 content::PermissionType permission, | 76 content::PermissionType permission); |
63 Profile* profile, | |
64 base::Time current_time); | |
65 | 77 |
66 // Records that an ignore of a prompt for |permission| was made. | 78 // Records that an ignore of a prompt for |permission| was made. |
67 static int RecordIgnore(const GURL& url, | 79 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 | 80 |
78 // Updates the threshold to start blocking prompts from the field trial. | 81 // Updates the threshold to start blocking prompts from the field trial. |
79 static void UpdateFromVariations(); | 82 static void UpdateFromVariations(); |
80 | 83 |
81 // Checks if |request_origin| is under embargo for |permission|. Internally, | 84 // Checks if |request_origin| is under embargo for |permission|. Internally, |
82 // this will make a call to IsUnderEmbargo to check the content setting first, | 85 // 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 | 86 // but may also make a call to Safe Browsing to check the API blacklist, which |
84 // blacklisted for |permission|, which is performed asynchronously. | 87 // is performed asynchronously. |
85 static void UpdateEmbargoedStatus(Profile* profile, | 88 void UpdateEmbargoedStatus(content::PermissionType permission, |
86 base::Time current_time, | 89 const GURL& request_origin, |
87 base::Callback<void(bool)> callback); | 90 content::WebContents* web_contents, |
91 base::Callback<void(bool)> callback); | |
88 | 92 |
89 // Checks the status of the content setting to determine if |request_origin| | 93 // Checks the status of the content setting to determine if |request_origin| |
90 // is under embargo for |permission|. This checks both embargo for Permissions | 94 // is under embargo for |permission|. This checks both embargo for Permissions |
91 // Blacklisting and repeated dismissals. | 95 // Blacklisting and repeated dismissals. |
92 static bool IsUnderEmbargo(content::PermissionType permission, | 96 bool IsUnderEmbargo(content::PermissionType permission, |
93 Profile* profile, | 97 const GURL& request_origin); |
94 const GURL& request_origin, | |
95 base::Time current_time); | |
96 | 98 |
97 private: | 99 private: |
98 friend class PermissionContextBaseTests; | 100 friend class PermissionContextBaseTests; |
99 friend class PermissionDecisionAutoBlockerUnitTest; | 101 friend class PermissionDecisionAutoBlockerUnitTest; |
100 | 102 |
103 explicit PermissionDecisionAutoBlocker(Profile* profile); | |
104 ~PermissionDecisionAutoBlocker() override; | |
105 | |
101 // Get the result of the Safe Browsing check, if |should_be_embargoed| is true | 106 // 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|. | 107 // then |request_origin| will be placed under embargo for that |permission|. |
103 static void CheckSafeBrowsingResult(content::PermissionType permission, | 108 // The callback passed in will not be called if the |web_contents| passed in |
104 Profile* profile, | 109 // via UpdateEmbargoStatus is destroyed. Thus if the callback is run, the |
105 const GURL& request_origin, | 110 // profile associated with |web_contents| is guaranteed to be alive. |
raymes
2017/01/24 05:15:17
nit: sorry I meant for this comment to be in Permi
meredithl
2017/01/24 23:20:21
Done.
| |
106 base::Time current_time, | 111 void CheckSafeBrowsingResult(content::PermissionType permission, |
107 base::Callback<void(bool)> callback, | 112 const GURL& request_origin, |
108 bool should_be_embargoed); | 113 base::Callback<void(bool)> callback, |
114 bool should_be_embargoed); | |
109 | 115 |
110 static void PlaceUnderEmbargo(content::PermissionType permission, | 116 void PlaceUnderEmbargo(content::PermissionType permission, |
111 const GURL& request_origin, | 117 const GURL& request_origin, |
112 HostContentSettingsMap* map, | 118 const char* key); |
113 base::Time current_time, | |
114 const char* key); | |
115 | 119 |
116 // Updates the embargo status of a |permission| for a given |request_origin|. | 120 void SetSafeBrowsingDatabaseManagerAndTimeoutForTesting( |
117 static void UpdateEmbargoStatus(content::PermissionType permission, | 121 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> db_manager, |
118 Profile* profile, | 122 int timeout); |
119 const GURL& request_origin, | 123 |
120 base::Callback<void(bool)> callback, | 124 void SetClockForTesting(std::unique_ptr<base::Clock> clock); |
121 bool embargo); | |
122 | 125 |
123 // Keys used for storing count data in a website setting. | 126 // Keys used for storing count data in a website setting. |
124 static const char kPromptDismissCountKey[]; | 127 static const char kPromptDismissCountKey[]; |
125 static const char kPromptIgnoreCountKey[]; | 128 static const char kPromptIgnoreCountKey[]; |
126 static const char kPermissionDismissalEmbargoKey[]; | 129 static const char kPermissionDismissalEmbargoKey[]; |
127 static const char kPermissionBlacklistEmbargoKey[]; | 130 static const char kPermissionBlacklistEmbargoKey[]; |
128 | 131 |
132 Profile* profile_; | |
133 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> db_manager_; | |
134 | |
135 // Timeout in ms. | |
136 int safe_browsing_timeout_; | |
137 | |
138 std::unique_ptr<base::Clock> clock_; | |
139 | |
129 DISALLOW_IMPLICIT_CONSTRUCTORS(PermissionDecisionAutoBlocker); | 140 DISALLOW_IMPLICIT_CONSTRUCTORS(PermissionDecisionAutoBlocker); |
130 }; | 141 }; |
131 | |
132 #endif // CHROME_BROWSER_PERMISSIONS_PERMISSION_DECISION_AUTO_BLOCKER_H_ | 142 #endif // CHROME_BROWSER_PERMISSIONS_PERMISSION_DECISION_AUTO_BLOCKER_H_ |
OLD | NEW |