Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Side by Side Diff: chrome/browser/permissions/permission_context_base.cc

Issue 2640033006: Convert AutoBlocker static class to KeyedService. (Closed)
Patch Set: Add clock, move browsing data tests, nits Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 #include "chrome/browser/permissions/permission_context_base.h" 5 #include "chrome/browser/permissions/permission_context_base.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
11 11
12 #include "base/callback.h" 12 #include "base/callback.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
15 #include "base/strings/stringprintf.h" 15 #include "base/strings/stringprintf.h"
16 #include "base/time/time.h"
16 #include "build/build_config.h" 17 #include "build/build_config.h"
17 #include "chrome/browser/browser_process.h" 18 #include "chrome/browser/browser_process.h"
18 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" 19 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
19 #include "chrome/browser/permissions/permission_decision_auto_blocker.h" 20 #include "chrome/browser/permissions/permission_decision_auto_blocker.h"
20 #include "chrome/browser/permissions/permission_request.h" 21 #include "chrome/browser/permissions/permission_request.h"
21 #include "chrome/browser/permissions/permission_request_id.h" 22 #include "chrome/browser/permissions/permission_request_id.h"
22 #include "chrome/browser/permissions/permission_request_impl.h" 23 #include "chrome/browser/permissions/permission_request_impl.h"
23 #include "chrome/browser/permissions/permission_request_manager.h" 24 #include "chrome/browser/permissions/permission_request_manager.h"
24 #include "chrome/browser/permissions/permission_uma_util.h" 25 #include "chrome/browser/permissions/permission_uma_util.h"
25 #include "chrome/browser/permissions/permission_util.h" 26 #include "chrome/browser/permissions/permission_util.h"
(...skipping 15 matching lines...) Expand all
41 #if defined(OS_ANDROID) 42 #if defined(OS_ANDROID)
42 #include "chrome/browser/permissions/permission_queue_controller.h" 43 #include "chrome/browser/permissions/permission_queue_controller.h"
43 #endif 44 #endif
44 45
45 // static 46 // static
46 const char PermissionContextBase::kPermissionsKillSwitchFieldStudy[] = 47 const char PermissionContextBase::kPermissionsKillSwitchFieldStudy[] =
47 "PermissionsKillSwitch"; 48 "PermissionsKillSwitch";
48 // static 49 // static
49 const char PermissionContextBase::kPermissionsKillSwitchBlockedValue[] = 50 const char PermissionContextBase::kPermissionsKillSwitchBlockedValue[] =
50 "blocked"; 51 "blocked";
51 // Maximum time in milliseconds to wait for safe browsing service to check a
52 // url for blacklisting. After this amount of time, the check will be aborted
53 // and the url will be treated as not blacklisted.
54 // TODO(meredithl): Revisit this once UMA metrics have data about request time.
55 const int kCheckUrlTimeoutMs = 2000;
56 52
57 PermissionContextBase::PermissionContextBase( 53 PermissionContextBase::PermissionContextBase(
58 Profile* profile, 54 Profile* profile,
59 const content::PermissionType permission_type, 55 const content::PermissionType permission_type,
60 const ContentSettingsType content_settings_type) 56 const ContentSettingsType content_settings_type)
61 : profile_(profile), 57 : profile_(profile),
62 permission_type_(permission_type), 58 permission_type_(permission_type),
63 content_settings_type_(content_settings_type), 59 content_settings_type_(content_settings_type),
64 safe_browsing_timeout_(kCheckUrlTimeoutMs),
65 weak_factory_(this) { 60 weak_factory_(this) {
66 #if defined(OS_ANDROID) 61 #if defined(OS_ANDROID)
67 permission_queue_controller_.reset(new PermissionQueueController( 62 permission_queue_controller_.reset(new PermissionQueueController(
68 profile_, permission_type_, content_settings_type_)); 63 profile_, permission_type_, content_settings_type_));
69 #endif 64 #endif
70 PermissionDecisionAutoBlocker::UpdateFromVariations(); 65 PermissionDecisionAutoBlocker::UpdateFromVariations();
71 } 66 }
72 67
73 PermissionContextBase::~PermissionContextBase() { 68 PermissionContextBase::~PermissionContextBase() {
74 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 69 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 // Synchronously check the content setting to see if the user has already made 111 // Synchronously check the content setting to see if the user has already made
117 // a decision, or if the origin is under embargo. If so, respect that 112 // a decision, or if the origin is under embargo. If so, respect that
118 // decision. 113 // decision.
119 ContentSetting content_setting = 114 ContentSetting content_setting =
120 GetPermissionStatus(requesting_origin, embedding_origin); 115 GetPermissionStatus(requesting_origin, embedding_origin);
121 if (content_setting == CONTENT_SETTING_ALLOW) { 116 if (content_setting == CONTENT_SETTING_ALLOW) {
122 HostContentSettingsMapFactory::GetForProfile(profile_)->UpdateLastUsage( 117 HostContentSettingsMapFactory::GetForProfile(profile_)->UpdateLastUsage(
123 requesting_origin, embedding_origin, content_settings_type_); 118 requesting_origin, embedding_origin, content_settings_type_);
124 } 119 }
125 120
126 121
raymes 2017/01/24 03:31:08 nit: unnecessary newline
meredithl 2017/01/24 04:52:25 Done.
127 if (content_setting == CONTENT_SETTING_ALLOW || 122 if (content_setting == CONTENT_SETTING_ALLOW ||
128 content_setting == CONTENT_SETTING_BLOCK) { 123 content_setting == CONTENT_SETTING_BLOCK) {
129 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, 124 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
130 false /* persist */, content_setting); 125 false /* persist */, content_setting);
131 return; 126 return;
132 } 127 }
128
133 // Asynchronously check whether the origin should be blocked from making this 129 // Asynchronously check whether the origin should be blocked from making this
134 // permission request. It may be on the Safe Browsing API blacklist, or it may 130 // permission request. It may be on the Safe Browsing API blacklist, or it may
135 // have been dismissed too many times in a row. If the origin is allowed to 131 // have been dismissed too many times in a row. If the origin is allowed to
136 // request, that request will be made to ContinueRequestPermission(). 132 // request, that request will be made to ContinueRequestPermission().
137 133 PermissionDecisionAutoBlocker::GetForProfile(profile_)->UpdateEmbargoedStatus(
138 PermissionDecisionAutoBlocker::UpdateEmbargoedStatus( 134 permission_type_, requesting_origin, web_contents,
139 db_manager_, permission_type_, requesting_origin, web_contents,
140 safe_browsing_timeout_, profile_, base::Time::Now(),
141 base::Bind(&PermissionContextBase::ContinueRequestPermission, 135 base::Bind(&PermissionContextBase::ContinueRequestPermission,
142 weak_factory_.GetWeakPtr(), web_contents, id, 136 weak_factory_.GetWeakPtr(), web_contents, id,
143 requesting_origin, embedding_origin, user_gesture, callback)); 137 requesting_origin, embedding_origin, user_gesture, callback));
144 } 138 }
145 139
146 void PermissionContextBase::ContinueRequestPermission( 140 void PermissionContextBase::ContinueRequestPermission(
147 content::WebContents* web_contents, 141 content::WebContents* web_contents,
148 const PermissionRequestID& id, 142 const PermissionRequestID& id,
149 const GURL& requesting_origin, 143 const GURL& requesting_origin,
150 const GURL& embedding_origin, 144 const GURL& embedding_origin,
151 bool user_gesture, 145 bool user_gesture,
152 const BrowserPermissionCallback& callback, 146 const BrowserPermissionCallback& callback,
153 bool permission_blocked) { 147 bool permission_blocked) {
154 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 148 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
155 if (permission_blocked) { 149 if (permission_blocked) {
156 // TODO(meredithl): Add UMA metrics here. 150 // TODO(meredithl): Add UMA metrics here.
157 web_contents->GetMainFrame()->AddMessageToConsole( 151 web_contents->GetMainFrame()->AddMessageToConsole(
158 content::CONSOLE_MESSAGE_LEVEL_INFO, 152 content::CONSOLE_MESSAGE_LEVEL_INFO,
159 base::StringPrintf( 153 base::StringPrintf(
160 "%s permission has been auto-blocked.", 154 "%s permission has been auto-blocked.",
161 PermissionUtil::GetPermissionString(permission_type_).c_str())); 155 PermissionUtil::GetPermissionString(permission_type_).c_str()));
162 // Permission has been blacklisted, block the request. 156 // Permission has been automatically blocked.
163 // TODO(meredithl): Consider setting the content setting and persisting
164 // the decision to block.
165 callback.Run(CONTENT_SETTING_BLOCK); 157 callback.Run(CONTENT_SETTING_BLOCK);
166 return; 158 return;
167 } 159 }
168 160
169 // Site is not blacklisted by Safe Browsing for the requested permission.
170 PermissionUmaUtil::PermissionRequested(permission_type_, requesting_origin, 161 PermissionUmaUtil::PermissionRequested(permission_type_, requesting_origin,
171 embedding_origin, profile_); 162 embedding_origin, profile_);
172 163
173 DecidePermission(web_contents, id, requesting_origin, embedding_origin, 164 DecidePermission(web_contents, id, requesting_origin, embedding_origin,
174 user_gesture, callback); 165 user_gesture, callback);
175 } 166 }
176 167
177 ContentSetting PermissionContextBase::GetPermissionStatus( 168 ContentSetting PermissionContextBase::GetPermissionStatus(
178 const GURL& requesting_origin, 169 const GURL& requesting_origin,
179 const GURL& embedding_origin) const { 170 const GURL& embedding_origin) const {
180 // If the permission has been disabled through Finch, block all requests. 171 // If the permission has been disabled through Finch, block all requests.
181 if (IsPermissionKillSwitchOn()) 172 if (IsPermissionKillSwitchOn())
182 return CONTENT_SETTING_BLOCK; 173 return CONTENT_SETTING_BLOCK;
183 174
184 if (IsRestrictedToSecureOrigins() && 175 if (IsRestrictedToSecureOrigins() &&
185 !content::IsOriginSecure(requesting_origin)) { 176 !content::IsOriginSecure(requesting_origin)) {
186 return CONTENT_SETTING_BLOCK; 177 return CONTENT_SETTING_BLOCK;
187 } 178 }
188 179
189 ContentSetting content_setting = 180 ContentSetting content_setting =
190 GetPermissionStatusInternal(requesting_origin, embedding_origin); 181 GetPermissionStatusInternal(requesting_origin, embedding_origin);
191 if (content_setting == CONTENT_SETTING_ASK && 182 if (content_setting == CONTENT_SETTING_ASK &&
192 PermissionDecisionAutoBlocker::IsUnderEmbargo( 183 PermissionDecisionAutoBlocker::GetForProfile(profile_)->IsUnderEmbargo(
193 permission_type_, profile_, requesting_origin, base::Time::Now())) { 184 permission_type_, requesting_origin)) {
194 return CONTENT_SETTING_BLOCK; 185 return CONTENT_SETTING_BLOCK;
195 } 186 }
196 return content_setting; 187 return content_setting;
197 } 188 }
198 189
199 void PermissionContextBase::ResetPermission( 190 void PermissionContextBase::ResetPermission(
200 const GURL& requesting_origin, 191 const GURL& requesting_origin,
201 const GURL& embedding_origin) { 192 const GURL& embedding_origin) {
202 HostContentSettingsMapFactory::GetForProfile(profile_) 193 HostContentSettingsMapFactory::GetForProfile(profile_)
203 ->SetContentSettingDefaultScope(requesting_origin, embedding_origin, 194 ->SetContentSettingDefaultScope(requesting_origin, embedding_origin,
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 } else if (content_setting == CONTENT_SETTING_BLOCK) { 305 } else if (content_setting == CONTENT_SETTING_BLOCK) {
315 PermissionUmaUtil::PermissionDenied(permission_type_, gesture_type, 306 PermissionUmaUtil::PermissionDenied(permission_type_, gesture_type,
316 requesting_origin, profile_); 307 requesting_origin, profile_);
317 } else { 308 } else {
318 PermissionUmaUtil::PermissionDismissed(permission_type_, gesture_type, 309 PermissionUmaUtil::PermissionDismissed(permission_type_, gesture_type,
319 requesting_origin, profile_); 310 requesting_origin, profile_);
320 } 311 }
321 } 312 }
322 313
323 if (content_setting == CONTENT_SETTING_DEFAULT && 314 if (content_setting == CONTENT_SETTING_DEFAULT &&
324 PermissionDecisionAutoBlocker::RecordDismissAndEmbargo( 315 PermissionDecisionAutoBlocker::GetForProfile(profile_)
325 requesting_origin, permission_type_, profile_, base::Time::Now())) { 316 ->RecordDismissAndEmbargo(requesting_origin, permission_type_)) {
326 // The permission has been embargoed, so it is blocked for this permission 317 // The permission has been embargoed, so it is blocked for this permission
327 // request, but not persisted. 318 // request, but not persisted.
328 content_setting = CONTENT_SETTING_BLOCK; 319 content_setting = CONTENT_SETTING_BLOCK;
329 } 320 }
330 321
331 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, 322 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
332 persist, content_setting); 323 persist, content_setting);
333 } 324 }
334 325
335 #if defined(OS_ANDROID) 326 #if defined(OS_ANDROID)
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
377 DCHECK(content_setting == CONTENT_SETTING_ALLOW || 368 DCHECK(content_setting == CONTENT_SETTING_ALLOW ||
378 content_setting == CONTENT_SETTING_BLOCK); 369 content_setting == CONTENT_SETTING_BLOCK);
379 DCHECK(!requesting_origin.SchemeIsFile()); 370 DCHECK(!requesting_origin.SchemeIsFile());
380 DCHECK(!embedding_origin.SchemeIsFile()); 371 DCHECK(!embedding_origin.SchemeIsFile());
381 372
382 HostContentSettingsMapFactory::GetForProfile(profile_) 373 HostContentSettingsMapFactory::GetForProfile(profile_)
383 ->SetContentSettingDefaultScope(requesting_origin, embedding_origin, 374 ->SetContentSettingDefaultScope(requesting_origin, embedding_origin,
384 content_settings_type_, std::string(), 375 content_settings_type_, std::string(),
385 content_setting); 376 content_setting);
386 } 377 }
387
388 void PermissionContextBase::SetSafeBrowsingDatabaseManagerAndTimeoutForTest(
389 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> db_manager,
390 int timeout) {
391 db_manager_ = db_manager;
392 safe_browsing_timeout_ = timeout;
393 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698