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

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

Issue 2640033006: Convert AutoBlocker static class to KeyedService. (Closed)
Patch Set: Remove static methods, 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
127 if (content_setting == CONTENT_SETTING_ALLOW || 121 if (content_setting == CONTENT_SETTING_ALLOW ||
128 content_setting == CONTENT_SETTING_BLOCK) { 122 content_setting == CONTENT_SETTING_BLOCK) {
129 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, 123 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
130 false /* persist */, content_setting); 124 false /* persist */, content_setting);
131 return; 125 return;
132 } 126 }
127
133 // Asynchronously check whether the origin should be blocked from making this 128 // 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 129 // 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 130 // have been dismissed too many times in a row. If the origin is allowed to
136 // request, that request will be made to ContinueRequestPermission(). 131 // request, that request will be made to ContinueRequestPermission().
137 132 PermissionDecisionAutoBlocker::GetForProfile(profile_)->UpdateEmbargoedStatus(
138 PermissionDecisionAutoBlocker::UpdateEmbargoedStatus( 133 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, 134 base::Bind(&PermissionContextBase::ContinueRequestPermission,
142 weak_factory_.GetWeakPtr(), web_contents, id, 135 weak_factory_.GetWeakPtr(), web_contents, id,
143 requesting_origin, embedding_origin, user_gesture, callback)); 136 requesting_origin, embedding_origin, user_gesture, callback));
144 } 137 }
145 138
146 void PermissionContextBase::ContinueRequestPermission( 139 void PermissionContextBase::ContinueRequestPermission(
147 content::WebContents* web_contents, 140 content::WebContents* web_contents,
148 const PermissionRequestID& id, 141 const PermissionRequestID& id,
149 const GURL& requesting_origin, 142 const GURL& requesting_origin,
150 const GURL& embedding_origin, 143 const GURL& embedding_origin,
151 bool user_gesture, 144 bool user_gesture,
152 const BrowserPermissionCallback& callback, 145 const BrowserPermissionCallback& callback,
153 bool permission_blocked) { 146 bool permission_blocked) {
154 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 147 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
155 if (permission_blocked) { 148 if (permission_blocked) {
156 // TODO(meredithl): Add UMA metrics here. 149 // TODO(meredithl): Add UMA metrics here.
157 web_contents->GetMainFrame()->AddMessageToConsole( 150 web_contents->GetMainFrame()->AddMessageToConsole(
158 content::CONSOLE_MESSAGE_LEVEL_INFO, 151 content::CONSOLE_MESSAGE_LEVEL_INFO,
159 base::StringPrintf( 152 base::StringPrintf(
160 "%s permission has been auto-blocked.", 153 "%s permission has been auto-blocked.",
161 PermissionUtil::GetPermissionString(permission_type_).c_str())); 154 PermissionUtil::GetPermissionString(permission_type_).c_str()));
162 // Permission has been blacklisted, block the request. 155 // 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); 156 callback.Run(CONTENT_SETTING_BLOCK);
166 return; 157 return;
167 } 158 }
168 159
169 // Site is not blacklisted by Safe Browsing for the requested permission.
170 PermissionUmaUtil::PermissionRequested(permission_type_, requesting_origin, 160 PermissionUmaUtil::PermissionRequested(permission_type_, requesting_origin,
171 embedding_origin, profile_); 161 embedding_origin, profile_);
172 162
173 DecidePermission(web_contents, id, requesting_origin, embedding_origin, 163 DecidePermission(web_contents, id, requesting_origin, embedding_origin,
174 user_gesture, callback); 164 user_gesture, callback);
175 } 165 }
176 166
177 ContentSetting PermissionContextBase::GetPermissionStatus( 167 ContentSetting PermissionContextBase::GetPermissionStatus(
178 const GURL& requesting_origin, 168 const GURL& requesting_origin,
179 const GURL& embedding_origin) const { 169 const GURL& embedding_origin) const {
180 // If the permission has been disabled through Finch, block all requests. 170 // If the permission has been disabled through Finch, block all requests.
181 if (IsPermissionKillSwitchOn()) 171 if (IsPermissionKillSwitchOn())
182 return CONTENT_SETTING_BLOCK; 172 return CONTENT_SETTING_BLOCK;
183 173
184 if (IsRestrictedToSecureOrigins() && 174 if (IsRestrictedToSecureOrigins() &&
185 !content::IsOriginSecure(requesting_origin)) { 175 !content::IsOriginSecure(requesting_origin)) {
186 return CONTENT_SETTING_BLOCK; 176 return CONTENT_SETTING_BLOCK;
187 } 177 }
188 178
189 ContentSetting content_setting = 179 ContentSetting content_setting =
190 GetPermissionStatusInternal(requesting_origin, embedding_origin); 180 GetPermissionStatusInternal(requesting_origin, embedding_origin);
191 if (content_setting == CONTENT_SETTING_ASK && 181 if (content_setting == CONTENT_SETTING_ASK &&
192 PermissionDecisionAutoBlocker::IsUnderEmbargo( 182 PermissionDecisionAutoBlocker::GetForProfile(profile_)->IsUnderEmbargo(
193 permission_type_, profile_, requesting_origin, base::Time::Now())) { 183 permission_type_, requesting_origin)) {
194 return CONTENT_SETTING_BLOCK; 184 return CONTENT_SETTING_BLOCK;
195 } 185 }
196 return content_setting; 186 return content_setting;
197 } 187 }
198 188
199 void PermissionContextBase::ResetPermission( 189 void PermissionContextBase::ResetPermission(
200 const GURL& requesting_origin, 190 const GURL& requesting_origin,
201 const GURL& embedding_origin) { 191 const GURL& embedding_origin) {
202 HostContentSettingsMapFactory::GetForProfile(profile_) 192 HostContentSettingsMapFactory::GetForProfile(profile_)
203 ->SetContentSettingDefaultScope(requesting_origin, embedding_origin, 193 ->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) { 304 } else if (content_setting == CONTENT_SETTING_BLOCK) {
315 PermissionUmaUtil::PermissionDenied(permission_type_, gesture_type, 305 PermissionUmaUtil::PermissionDenied(permission_type_, gesture_type,
316 requesting_origin, profile_); 306 requesting_origin, profile_);
317 } else { 307 } else {
318 PermissionUmaUtil::PermissionDismissed(permission_type_, gesture_type, 308 PermissionUmaUtil::PermissionDismissed(permission_type_, gesture_type,
319 requesting_origin, profile_); 309 requesting_origin, profile_);
320 } 310 }
321 } 311 }
322 312
323 if (content_setting == CONTENT_SETTING_DEFAULT && 313 if (content_setting == CONTENT_SETTING_DEFAULT &&
324 PermissionDecisionAutoBlocker::RecordDismissAndEmbargo( 314 PermissionDecisionAutoBlocker::GetForProfile(profile_)
325 requesting_origin, permission_type_, profile_, base::Time::Now())) { 315 ->RecordDismissAndEmbargo(requesting_origin, permission_type_)) {
326 // The permission has been embargoed, so it is blocked for this permission 316 // The permission has been embargoed, so it is blocked for this permission
327 // request, but not persisted. 317 // request, but not persisted.
328 content_setting = CONTENT_SETTING_BLOCK; 318 content_setting = CONTENT_SETTING_BLOCK;
329 } 319 }
330 320
331 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, 321 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
332 persist, content_setting); 322 persist, content_setting);
333 } 323 }
334 324
335 #if defined(OS_ANDROID) 325 #if defined(OS_ANDROID)
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
377 DCHECK(content_setting == CONTENT_SETTING_ALLOW || 367 DCHECK(content_setting == CONTENT_SETTING_ALLOW ||
378 content_setting == CONTENT_SETTING_BLOCK); 368 content_setting == CONTENT_SETTING_BLOCK);
379 DCHECK(!requesting_origin.SchemeIsFile()); 369 DCHECK(!requesting_origin.SchemeIsFile());
380 DCHECK(!embedding_origin.SchemeIsFile()); 370 DCHECK(!embedding_origin.SchemeIsFile());
381 371
382 HostContentSettingsMapFactory::GetForProfile(profile_) 372 HostContentSettingsMapFactory::GetForProfile(profile_)
383 ->SetContentSettingDefaultScope(requesting_origin, embedding_origin, 373 ->SetContentSettingDefaultScope(requesting_origin, embedding_origin,
384 content_settings_type_, std::string(), 374 content_settings_type_, std::string(),
385 content_setting); 375 content_setting);
386 } 376 }
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