| OLD | NEW |
| 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 Loading... |
| 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 Loading... |
| 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, |
| 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_INFO, | 151 content::CONSOLE_MESSAGE_LEVEL_INFO, |
| 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 PermissionDecisionAutoBlocker::GetForProfile(profile_)->IsUnderEmbargo( |
| 199 permission_type_, profile_, requesting_origin, base::Time::Now())) { | 183 permission_type_, requesting_origin)) { |
| 200 return CONTENT_SETTING_BLOCK; | 184 return CONTENT_SETTING_BLOCK; |
| 201 } | 185 } |
| 202 return content_setting; | 186 return content_setting; |
| 203 } | 187 } |
| 204 | 188 |
| 205 void PermissionContextBase::ResetPermission( | 189 void PermissionContextBase::ResetPermission( |
| 206 const GURL& requesting_origin, | 190 const GURL& requesting_origin, |
| 207 const GURL& embedding_origin) { | 191 const GURL& embedding_origin) { |
| 208 HostContentSettingsMapFactory::GetForProfile(profile_) | 192 HostContentSettingsMapFactory::GetForProfile(profile_) |
| 209 ->SetContentSettingDefaultScope(requesting_origin, embedding_origin, | 193 ->SetContentSettingDefaultScope(requesting_origin, embedding_origin, |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 } else if (content_setting == CONTENT_SETTING_BLOCK) { | 304 } else if (content_setting == CONTENT_SETTING_BLOCK) { |
| 321 PermissionUmaUtil::PermissionDenied(permission_type_, gesture_type, | 305 PermissionUmaUtil::PermissionDenied(permission_type_, gesture_type, |
| 322 requesting_origin, profile_); | 306 requesting_origin, profile_); |
| 323 } else { | 307 } else { |
| 324 PermissionUmaUtil::PermissionDismissed(permission_type_, gesture_type, | 308 PermissionUmaUtil::PermissionDismissed(permission_type_, gesture_type, |
| 325 requesting_origin, profile_); | 309 requesting_origin, profile_); |
| 326 } | 310 } |
| 327 } | 311 } |
| 328 | 312 |
| 329 if (content_setting == CONTENT_SETTING_DEFAULT && | 313 if (content_setting == CONTENT_SETTING_DEFAULT && |
| 330 PermissionDecisionAutoBlocker::RecordDismissAndEmbargo( | 314 PermissionDecisionAutoBlocker::GetForProfile(profile_) |
| 331 requesting_origin, permission_type_, profile_, base::Time::Now())) { | 315 ->RecordDismissAndEmbargo(requesting_origin, permission_type_)) { |
| 332 // 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 |
| 333 // request, but not persisted. | 317 // request, but not persisted. |
| 334 content_setting = CONTENT_SETTING_BLOCK; | 318 content_setting = CONTENT_SETTING_BLOCK; |
| 335 } | 319 } |
| 336 | 320 |
| 337 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, | 321 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, |
| 338 persist, content_setting); | 322 persist, content_setting); |
| 339 } | 323 } |
| 340 | 324 |
| 341 #if defined(OS_ANDROID) | 325 #if defined(OS_ANDROID) |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 DCHECK(content_setting == CONTENT_SETTING_ALLOW || | 367 DCHECK(content_setting == CONTENT_SETTING_ALLOW || |
| 384 content_setting == CONTENT_SETTING_BLOCK); | 368 content_setting == CONTENT_SETTING_BLOCK); |
| 385 DCHECK(!requesting_origin.SchemeIsFile()); | 369 DCHECK(!requesting_origin.SchemeIsFile()); |
| 386 DCHECK(!embedding_origin.SchemeIsFile()); | 370 DCHECK(!embedding_origin.SchemeIsFile()); |
| 387 | 371 |
| 388 HostContentSettingsMapFactory::GetForProfile(profile_) | 372 HostContentSettingsMapFactory::GetForProfile(profile_) |
| 389 ->SetContentSettingDefaultScope(requesting_origin, embedding_origin, | 373 ->SetContentSettingDefaultScope(requesting_origin, embedding_origin, |
| 390 content_settings_type_, std::string(), | 374 content_settings_type_, std::string(), |
| 391 content_setting); | 375 content_setting); |
| 392 } | 376 } |
| 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 } | |
| OLD | NEW |