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

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

Issue 2640033006: Convert AutoBlocker static class to KeyedService. (Closed)
Patch Set: Nits and incognito browser context 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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 requesting_origin, embedding_origin, content_settings_type_); 118 requesting_origin, embedding_origin, content_settings_type_);
124 } 119 }
125 120
126 if (content_setting == CONTENT_SETTING_ALLOW || 121 if (content_setting == CONTENT_SETTING_ALLOW ||
127 content_setting == CONTENT_SETTING_BLOCK) { 122 content_setting == CONTENT_SETTING_BLOCK) {
128 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, 123 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
129 false /* persist */, content_setting); 124 false /* persist */, content_setting);
130 return; 125 return;
131 } 126 }
132 127
133 if (!db_manager_) {
134 safe_browsing::SafeBrowsingService* sb_service =
135 g_browser_process->safe_browsing_service();
136 if (sb_service)
137 db_manager_ = sb_service->database_manager();
138 }
139
140 // Asynchronously check whether the origin should be blocked from making this 128 // Asynchronously check whether the origin should be blocked from making this
141 // 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
142 // 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
143 // request, that request will be made to ContinueRequestPermission(). 131 // request, that request will be made to ContinueRequestPermission().
144 PermissionDecisionAutoBlocker::UpdateEmbargoedStatus( 132 PermissionDecisionAutoBlocker::GetForProfile(profile_)->UpdateEmbargoedStatus(
145 db_manager_, permission_type_, requesting_origin, web_contents, 133 permission_type_, requesting_origin, web_contents, base::Time::Now(),
146 safe_browsing_timeout_, profile_, base::Time::Now(),
147 base::Bind(&PermissionContextBase::ContinueRequestPermission, 134 base::Bind(&PermissionContextBase::ContinueRequestPermission,
148 weak_factory_.GetWeakPtr(), web_contents, id, 135 weak_factory_.GetWeakPtr(), web_contents, id,
149 requesting_origin, embedding_origin, user_gesture, callback)); 136 requesting_origin, embedding_origin, user_gesture, callback));
150 } 137 }
151 138
152 void PermissionContextBase::ContinueRequestPermission( 139 void PermissionContextBase::ContinueRequestPermission(
153 content::WebContents* web_contents, 140 content::WebContents* web_contents,
154 const PermissionRequestID& id, 141 const PermissionRequestID& id,
155 const GURL& requesting_origin, 142 const GURL& requesting_origin,
156 const GURL& embedding_origin, 143 const GURL& embedding_origin,
157 bool user_gesture, 144 bool user_gesture,
158 const BrowserPermissionCallback& callback, 145 const BrowserPermissionCallback& callback,
159 bool permission_blocked) { 146 bool permission_blocked) {
160 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 147 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
161 if (permission_blocked) { 148 if (permission_blocked) {
162 // TODO(meredithl): Add UMA metrics here. 149 // TODO(meredithl): Add UMA metrics here.
163 web_contents->GetMainFrame()->AddMessageToConsole( 150 web_contents->GetMainFrame()->AddMessageToConsole(
164 content::CONSOLE_MESSAGE_LEVEL_LOG, 151 content::CONSOLE_MESSAGE_LEVEL_LOG,
165 base::StringPrintf( 152 base::StringPrintf(
166 "%s permission has been auto-blocked.", 153 "%s permission has been auto-blocked.",
167 PermissionUtil::GetPermissionString(permission_type_).c_str())); 154 PermissionUtil::GetPermissionString(permission_type_).c_str()));
168 // Permission has been blacklisted, block the request. 155 // Permission has been automatically blocked.
169 // TODO(meredithl): Consider setting the content setting and persisting
170 // the decision to block.
171 callback.Run(CONTENT_SETTING_BLOCK); 156 callback.Run(CONTENT_SETTING_BLOCK);
172 return; 157 return;
173 } 158 }
174 159
175 // Site is not blacklisted by Safe Browsing for the requested permission.
176 PermissionUmaUtil::PermissionRequested(permission_type_, requesting_origin, 160 PermissionUmaUtil::PermissionRequested(permission_type_, requesting_origin,
177 embedding_origin, profile_); 161 embedding_origin, profile_);
178 162
179 DecidePermission(web_contents, id, requesting_origin, embedding_origin, 163 DecidePermission(web_contents, id, requesting_origin, embedding_origin,
180 user_gesture, callback); 164 user_gesture, callback);
181 } 165 }
182 166
183 ContentSetting PermissionContextBase::GetPermissionStatus( 167 ContentSetting PermissionContextBase::GetPermissionStatus(
184 const GURL& requesting_origin, 168 const GURL& requesting_origin,
185 const GURL& embedding_origin) const { 169 const GURL& embedding_origin) const {
186 // If the permission has been disabled through Finch, block all requests. 170 // If the permission has been disabled through Finch, block all requests.
187 if (IsPermissionKillSwitchOn()) 171 if (IsPermissionKillSwitchOn())
188 return CONTENT_SETTING_BLOCK; 172 return CONTENT_SETTING_BLOCK;
189 173
190 if (IsRestrictedToSecureOrigins() && 174 if (IsRestrictedToSecureOrigins() &&
191 !content::IsOriginSecure(requesting_origin)) { 175 !content::IsOriginSecure(requesting_origin)) {
192 return CONTENT_SETTING_BLOCK; 176 return CONTENT_SETTING_BLOCK;
193 } 177 }
194 178
195 ContentSetting content_setting = 179 ContentSetting content_setting =
196 GetPermissionStatusInternal(requesting_origin, embedding_origin); 180 GetPermissionStatusInternal(requesting_origin, embedding_origin);
197 if (content_setting == CONTENT_SETTING_ASK && 181 if (content_setting == CONTENT_SETTING_ASK &&
198 PermissionDecisionAutoBlocker::IsUnderEmbargo( 182 PermissionDecisionAutoBlockerFactory::GetForProfile(profile_)
dominickn 2017/01/20 04:34:24 Nit: remove factory
meredithl 2017/01/24 01:20:46 Done.
199 permission_type_, profile_, requesting_origin, base::Time::Now())) { 183 ->IsUnderEmbargo(permission_type_, requesting_origin,
184 base::Time::Now())) {
raymes 2017/01/23 06:14:23 Rather than pass the time in here I think Permissi
meredithl 2017/01/24 02:23:11 Done.
200 return CONTENT_SETTING_BLOCK; 185 return CONTENT_SETTING_BLOCK;
201 } 186 }
202 return content_setting; 187 return content_setting;
203 } 188 }
204 189
205 void PermissionContextBase::ResetPermission( 190 void PermissionContextBase::ResetPermission(
206 const GURL& requesting_origin, 191 const GURL& requesting_origin,
207 const GURL& embedding_origin) { 192 const GURL& embedding_origin) {
208 HostContentSettingsMapFactory::GetForProfile(profile_) 193 HostContentSettingsMapFactory::GetForProfile(profile_)
209 ->SetContentSettingDefaultScope(requesting_origin, embedding_origin, 194 ->SetContentSettingDefaultScope(requesting_origin, embedding_origin,
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
320 } else if (content_setting == CONTENT_SETTING_BLOCK) { 305 } else if (content_setting == CONTENT_SETTING_BLOCK) {
321 PermissionUmaUtil::PermissionDenied(permission_type_, gesture_type, 306 PermissionUmaUtil::PermissionDenied(permission_type_, gesture_type,
322 requesting_origin, profile_); 307 requesting_origin, profile_);
323 } else { 308 } else {
324 PermissionUmaUtil::PermissionDismissed(permission_type_, gesture_type, 309 PermissionUmaUtil::PermissionDismissed(permission_type_, gesture_type,
325 requesting_origin, profile_); 310 requesting_origin, profile_);
326 } 311 }
327 } 312 }
328 313
329 if (content_setting == CONTENT_SETTING_DEFAULT && 314 if (content_setting == CONTENT_SETTING_DEFAULT &&
330 PermissionDecisionAutoBlocker::RecordDismissAndEmbargo( 315 PermissionDecisionAutoBlocker::GetForProfile(profile_)
331 requesting_origin, permission_type_, profile_, base::Time::Now())) { 316 ->RecordDismissAndEmbargo(requesting_origin, permission_type_,
317 base::Time::Now())) {
332 // The permission has been embargoed, so it is blocked for this permission 318 // The permission has been embargoed, so it is blocked for this permission
333 // request, but not persisted. 319 // request, but not persisted.
334 content_setting = CONTENT_SETTING_BLOCK; 320 content_setting = CONTENT_SETTING_BLOCK;
335 } 321 }
336 322
337 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, 323 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
338 persist, content_setting); 324 persist, content_setting);
339 } 325 }
340 326
341 #if defined(OS_ANDROID) 327 #if defined(OS_ANDROID)
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 DCHECK(content_setting == CONTENT_SETTING_ALLOW || 369 DCHECK(content_setting == CONTENT_SETTING_ALLOW ||
384 content_setting == CONTENT_SETTING_BLOCK); 370 content_setting == CONTENT_SETTING_BLOCK);
385 DCHECK(!requesting_origin.SchemeIsFile()); 371 DCHECK(!requesting_origin.SchemeIsFile());
386 DCHECK(!embedding_origin.SchemeIsFile()); 372 DCHECK(!embedding_origin.SchemeIsFile());
387 373
388 HostContentSettingsMapFactory::GetForProfile(profile_) 374 HostContentSettingsMapFactory::GetForProfile(profile_)
389 ->SetContentSettingDefaultScope(requesting_origin, embedding_origin, 375 ->SetContentSettingDefaultScope(requesting_origin, embedding_origin,
390 content_settings_type_, std::string(), 376 content_settings_type_, std::string(),
391 content_setting); 377 content_setting);
392 } 378 }
393
394 void PermissionContextBase::SetSafeBrowsingDatabaseManagerAndTimeoutForTest(
395 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> db_manager,
396 int timeout) {
397 db_manager_ = db_manager;
398 safe_browsing_timeout_ = timeout;
399 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698