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> |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
45 | 45 |
46 // static | 46 // static |
47 const char PermissionContextBase::kPermissionsKillSwitchFieldStudy[] = | 47 const char PermissionContextBase::kPermissionsKillSwitchFieldStudy[] = |
48 "PermissionsKillSwitch"; | 48 "PermissionsKillSwitch"; |
49 // static | 49 // static |
50 const char PermissionContextBase::kPermissionsKillSwitchBlockedValue[] = | 50 const char PermissionContextBase::kPermissionsKillSwitchBlockedValue[] = |
51 "blocked"; | 51 "blocked"; |
52 | 52 |
53 PermissionContextBase::PermissionContextBase( | 53 PermissionContextBase::PermissionContextBase( |
54 Profile* profile, | 54 Profile* profile, |
55 const content::PermissionType permission_type, | |
56 const ContentSettingsType content_settings_type) | 55 const ContentSettingsType content_settings_type) |
57 : profile_(profile), | 56 : profile_(profile), |
58 permission_type_(permission_type), | |
59 content_settings_type_(content_settings_type), | 57 content_settings_type_(content_settings_type), |
60 weak_factory_(this) { | 58 weak_factory_(this) { |
61 #if defined(OS_ANDROID) | 59 #if defined(OS_ANDROID) |
62 permission_queue_controller_.reset(new PermissionQueueController( | 60 permission_queue_controller_.reset( |
63 profile_, permission_type_, content_settings_type_)); | 61 new PermissionQueueController(profile_, content_settings_type_)); |
64 #endif | 62 #endif |
65 PermissionDecisionAutoBlocker::UpdateFromVariations(); | 63 PermissionDecisionAutoBlocker::UpdateFromVariations(); |
66 } | 64 } |
67 | 65 |
68 PermissionContextBase::~PermissionContextBase() { | 66 PermissionContextBase::~PermissionContextBase() { |
69 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 67 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
70 } | 68 } |
71 | 69 |
72 void PermissionContextBase::RequestPermission( | 70 void PermissionContextBase::RequestPermission( |
73 content::WebContents* web_contents, | 71 content::WebContents* web_contents, |
74 const PermissionRequestID& id, | 72 const PermissionRequestID& id, |
75 const GURL& requesting_frame, | 73 const GURL& requesting_frame, |
76 bool user_gesture, | 74 bool user_gesture, |
77 const BrowserPermissionCallback& callback) { | 75 const BrowserPermissionCallback& callback) { |
78 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 76 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
79 | 77 |
80 // First check if this permission has been disabled. | 78 // First check if this permission has been disabled. |
81 if (IsPermissionKillSwitchOn()) { | 79 if (IsPermissionKillSwitchOn()) { |
82 // Log to the developer console. | 80 // Log to the developer console. |
83 web_contents->GetMainFrame()->AddMessageToConsole( | 81 web_contents->GetMainFrame()->AddMessageToConsole( |
84 content::CONSOLE_MESSAGE_LEVEL_INFO, | 82 content::CONSOLE_MESSAGE_LEVEL_INFO, |
85 base::StringPrintf( | 83 base::StringPrintf( |
86 "%s permission has been blocked.", | 84 "%s permission has been blocked.", |
87 PermissionUtil::GetPermissionString(permission_type_).c_str())); | 85 PermissionUtil::GetPermissionString(content_settings_type_) |
86 .c_str())); | |
88 // The kill switch is enabled for this permission; Block all requests. | 87 // The kill switch is enabled for this permission; Block all requests. |
89 callback.Run(CONTENT_SETTING_BLOCK); | 88 callback.Run(CONTENT_SETTING_BLOCK); |
90 return; | 89 return; |
91 } | 90 } |
92 | 91 |
93 GURL requesting_origin = requesting_frame.GetOrigin(); | 92 GURL requesting_origin = requesting_frame.GetOrigin(); |
94 GURL embedding_origin = web_contents->GetLastCommittedURL().GetOrigin(); | 93 GURL embedding_origin = web_contents->GetLastCommittedURL().GetOrigin(); |
95 | 94 |
96 if (!requesting_origin.is_valid() || !embedding_origin.is_valid()) { | 95 if (!requesting_origin.is_valid() || !embedding_origin.is_valid()) { |
97 std::string type_name = | 96 std::string type_name = |
98 content_settings::WebsiteSettingsRegistry::GetInstance() | 97 content_settings::WebsiteSettingsRegistry::GetInstance() |
99 ->Get(content_settings_type_) | 98 ->Get(content_settings_type_) |
100 ->name(); | 99 ->name(); |
101 | 100 |
102 DVLOG(1) << "Attempt to use " << type_name | 101 DVLOG(1) << "Attempt to use " << type_name |
103 << " from an invalid URL: " << requesting_origin << "," | 102 << " from an invalid URL: " << requesting_origin << "," |
104 << embedding_origin << " (" << type_name | 103 << embedding_origin << " (" << type_name |
105 << " is not supported in popups)"; | 104 << " is not supported in popups)"; |
106 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, | 105 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, |
107 false /* persist */, CONTENT_SETTING_BLOCK); | 106 false /* persist */, CONTENT_SETTING_BLOCK); |
108 return; | 107 return; |
109 } | 108 } |
110 | 109 |
111 // Synchronously check the content setting to see if the user has already made | 110 // Synchronously check the content setting to see if the user has already made |
112 // a decision, or if the origin is under embargo. If so, respect that | 111 // a decision, or if the origin is under embargo. If so, respect that |
113 // decision. | 112 // decision. |
114 ContentSetting content_setting = | 113 ContentSetting content_setting = |
115 GetPermissionStatus(requesting_origin, embedding_origin); | 114 GetPermissionStatus(requesting_origin, embedding_origin); |
116 if (content_setting == CONTENT_SETTING_ALLOW) { | 115 if (content_setting == CONTENT_SETTING_ALLOW) { |
116 ContentSettingsType type_for_map = content_settings_type_; | |
117 if (type_for_map == CONTENT_SETTINGS_TYPE_PUSH_MESSAGING) | |
118 type_for_map = CONTENT_SETTINGS_TYPE_NOTIFICATIONS; | |
117 HostContentSettingsMapFactory::GetForProfile(profile_)->UpdateLastUsage( | 119 HostContentSettingsMapFactory::GetForProfile(profile_)->UpdateLastUsage( |
118 requesting_origin, embedding_origin, content_settings_type_); | 120 requesting_origin, embedding_origin, type_for_map); |
raymes
2017/02/07 04:46:15
I think we should just delete this since it's not
Timothy Loh
2017/02/08 04:01:06
Sure, I'll do this later :)
raymes
2017/02/09 00:39:56
In that case, please add a note about why we need
Timothy Loh
2017/02/10 07:25:38
Done.
| |
119 } | 121 } |
120 | 122 |
121 if (content_setting == CONTENT_SETTING_ALLOW || | 123 if (content_setting == CONTENT_SETTING_ALLOW || |
122 content_setting == CONTENT_SETTING_BLOCK) { | 124 content_setting == CONTENT_SETTING_BLOCK) { |
123 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, | 125 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, |
124 false /* persist */, content_setting); | 126 false /* persist */, content_setting); |
125 return; | 127 return; |
126 } | 128 } |
127 | 129 |
128 // Asynchronously check whether the origin should be blocked from making this | 130 // Asynchronously check whether the origin should be blocked from making this |
129 // permission request. It may be on the Safe Browsing API blacklist, or it may | 131 // permission request. It may be on the Safe Browsing API blacklist, or it may |
130 // have been dismissed too many times in a row. If the origin is allowed to | 132 // have been dismissed too many times in a row. If the origin is allowed to |
131 // request, that request will be made to ContinueRequestPermission(). | 133 // request, that request will be made to ContinueRequestPermission(). |
132 PermissionDecisionAutoBlocker::GetForProfile(profile_)->UpdateEmbargoedStatus( | 134 PermissionDecisionAutoBlocker::GetForProfile(profile_)->UpdateEmbargoedStatus( |
133 permission_type_, requesting_origin, web_contents, | 135 content_settings_type_, requesting_origin, web_contents, |
134 base::Bind(&PermissionContextBase::ContinueRequestPermission, | 136 base::Bind(&PermissionContextBase::ContinueRequestPermission, |
135 weak_factory_.GetWeakPtr(), web_contents, id, | 137 weak_factory_.GetWeakPtr(), web_contents, id, |
136 requesting_origin, embedding_origin, user_gesture, callback)); | 138 requesting_origin, embedding_origin, user_gesture, callback)); |
137 } | 139 } |
138 | 140 |
139 void PermissionContextBase::ContinueRequestPermission( | 141 void PermissionContextBase::ContinueRequestPermission( |
140 content::WebContents* web_contents, | 142 content::WebContents* web_contents, |
141 const PermissionRequestID& id, | 143 const PermissionRequestID& id, |
142 const GURL& requesting_origin, | 144 const GURL& requesting_origin, |
143 const GURL& embedding_origin, | 145 const GURL& embedding_origin, |
144 bool user_gesture, | 146 bool user_gesture, |
145 const BrowserPermissionCallback& callback, | 147 const BrowserPermissionCallback& callback, |
146 bool permission_blocked) { | 148 bool permission_blocked) { |
147 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 149 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
148 if (permission_blocked) { | 150 if (permission_blocked) { |
149 // TODO(meredithl): Add UMA metrics here. | 151 // TODO(meredithl): Add UMA metrics here. |
150 web_contents->GetMainFrame()->AddMessageToConsole( | 152 web_contents->GetMainFrame()->AddMessageToConsole( |
151 content::CONSOLE_MESSAGE_LEVEL_INFO, | 153 content::CONSOLE_MESSAGE_LEVEL_INFO, |
152 base::StringPrintf( | 154 base::StringPrintf( |
153 "%s permission has been auto-blocked.", | 155 "%s permission has been auto-blocked.", |
154 PermissionUtil::GetPermissionString(permission_type_).c_str())); | 156 PermissionUtil::GetPermissionString(content_settings_type_) |
157 .c_str())); | |
155 // Permission has been automatically blocked. | 158 // Permission has been automatically blocked. |
156 callback.Run(CONTENT_SETTING_BLOCK); | 159 callback.Run(CONTENT_SETTING_BLOCK); |
157 return; | 160 return; |
158 } | 161 } |
159 | 162 |
160 PermissionUmaUtil::PermissionRequested(permission_type_, requesting_origin, | 163 PermissionUmaUtil::PermissionRequested( |
161 embedding_origin, profile_); | 164 content_settings_type_, requesting_origin, embedding_origin, profile_); |
162 | 165 |
163 DecidePermission(web_contents, id, requesting_origin, embedding_origin, | 166 DecidePermission(web_contents, id, requesting_origin, embedding_origin, |
164 user_gesture, callback); | 167 user_gesture, callback); |
165 } | 168 } |
166 | 169 |
167 ContentSetting PermissionContextBase::GetPermissionStatus( | 170 ContentSetting PermissionContextBase::GetPermissionStatus( |
168 const GURL& requesting_origin, | 171 const GURL& requesting_origin, |
169 const GURL& embedding_origin) const { | 172 const GURL& embedding_origin) const { |
170 // If the permission has been disabled through Finch, block all requests. | 173 // If the permission has been disabled through Finch, block all requests. |
171 if (IsPermissionKillSwitchOn()) | 174 if (IsPermissionKillSwitchOn()) |
172 return CONTENT_SETTING_BLOCK; | 175 return CONTENT_SETTING_BLOCK; |
173 | 176 |
174 if (IsRestrictedToSecureOrigins() && | 177 if (IsRestrictedToSecureOrigins() && |
175 !content::IsOriginSecure(requesting_origin)) { | 178 !content::IsOriginSecure(requesting_origin)) { |
176 return CONTENT_SETTING_BLOCK; | 179 return CONTENT_SETTING_BLOCK; |
177 } | 180 } |
178 | 181 |
179 ContentSetting content_setting = | 182 ContentSetting content_setting = |
180 GetPermissionStatusInternal(requesting_origin, embedding_origin); | 183 GetPermissionStatusInternal(requesting_origin, embedding_origin); |
181 if (content_setting == CONTENT_SETTING_ASK && | 184 if (content_setting == CONTENT_SETTING_ASK && |
182 PermissionDecisionAutoBlocker::GetForProfile(profile_)->IsUnderEmbargo( | 185 PermissionDecisionAutoBlocker::GetForProfile(profile_)->IsUnderEmbargo( |
183 permission_type_, requesting_origin)) { | 186 content_settings_type_, requesting_origin)) { |
184 return CONTENT_SETTING_BLOCK; | 187 return CONTENT_SETTING_BLOCK; |
185 } | 188 } |
186 return content_setting; | 189 return content_setting; |
187 } | 190 } |
188 | 191 |
189 void PermissionContextBase::ResetPermission( | 192 void PermissionContextBase::ResetPermission( |
190 const GURL& requesting_origin, | 193 const GURL& requesting_origin, |
191 const GURL& embedding_origin) { | 194 const GURL& embedding_origin) { |
192 HostContentSettingsMapFactory::GetForProfile(profile_) | 195 HostContentSettingsMapFactory::GetForProfile(profile_) |
193 ->SetContentSettingDefaultScope(requesting_origin, embedding_origin, | 196 ->SetContentSettingDefaultScope(requesting_origin, embedding_origin, |
(...skipping 18 matching lines...) Expand all Loading... | |
212 GetQueueController()->CancelInfoBarRequest(id); | 215 GetQueueController()->CancelInfoBarRequest(id); |
213 #else | 216 #else |
214 NOTREACHED(); | 217 NOTREACHED(); |
215 #endif | 218 #endif |
216 } | 219 } |
217 } | 220 } |
218 | 221 |
219 bool PermissionContextBase::IsPermissionKillSwitchOn() const { | 222 bool PermissionContextBase::IsPermissionKillSwitchOn() const { |
220 const std::string param = variations::GetVariationParamValue( | 223 const std::string param = variations::GetVariationParamValue( |
221 kPermissionsKillSwitchFieldStudy, | 224 kPermissionsKillSwitchFieldStudy, |
222 PermissionUtil::GetPermissionString(permission_type_)); | 225 PermissionUtil::GetPermissionString(content_settings_type_)); |
223 | 226 |
224 return param == kPermissionsKillSwitchBlockedValue; | 227 return param == kPermissionsKillSwitchBlockedValue; |
225 } | 228 } |
226 | 229 |
227 ContentSetting PermissionContextBase::GetPermissionStatusInternal( | 230 ContentSetting PermissionContextBase::GetPermissionStatusInternal( |
228 const GURL& requesting_origin, | 231 const GURL& requesting_origin, |
229 const GURL& embedding_origin) const { | 232 const GURL& embedding_origin) const { |
230 return HostContentSettingsMapFactory::GetForProfile(profile_) | 233 return HostContentSettingsMapFactory::GetForProfile(profile_) |
231 ->GetContentSetting(requesting_origin, embedding_origin, | 234 ->GetContentSetting(requesting_origin, embedding_origin, |
232 content_settings_type_, std::string()); | 235 content_settings_type_, std::string()); |
(...skipping 11 matching lines...) Expand all Loading... | |
244 if (PermissionRequestManager::IsEnabled()) { | 247 if (PermissionRequestManager::IsEnabled()) { |
245 PermissionRequestManager* permission_request_manager = | 248 PermissionRequestManager* permission_request_manager = |
246 PermissionRequestManager::FromWebContents(web_contents); | 249 PermissionRequestManager::FromWebContents(web_contents); |
247 // TODO(felt): sometimes |permission_request_manager| is null. This check is | 250 // TODO(felt): sometimes |permission_request_manager| is null. This check is |
248 // meant to prevent crashes. See crbug.com/457091. | 251 // meant to prevent crashes. See crbug.com/457091. |
249 if (!permission_request_manager) | 252 if (!permission_request_manager) |
250 return; | 253 return; |
251 | 254 |
252 std::unique_ptr<PermissionRequest> request_ptr = | 255 std::unique_ptr<PermissionRequest> request_ptr = |
253 base::MakeUnique<PermissionRequestImpl>( | 256 base::MakeUnique<PermissionRequestImpl>( |
254 requesting_origin, permission_type_, profile_, user_gesture, | 257 requesting_origin, content_settings_type_, profile_, user_gesture, |
255 base::Bind(&PermissionContextBase::PermissionDecided, | 258 base::Bind(&PermissionContextBase::PermissionDecided, |
256 weak_factory_.GetWeakPtr(), id, requesting_origin, | 259 weak_factory_.GetWeakPtr(), id, requesting_origin, |
257 embedding_origin, user_gesture, callback), | 260 embedding_origin, user_gesture, callback), |
258 base::Bind(&PermissionContextBase::CleanUpRequest, | 261 base::Bind(&PermissionContextBase::CleanUpRequest, |
259 weak_factory_.GetWeakPtr(), id)); | 262 weak_factory_.GetWeakPtr(), id)); |
260 PermissionRequest* request = request_ptr.get(); | 263 PermissionRequest* request = request_ptr.get(); |
261 | 264 |
262 bool inserted = | 265 bool inserted = |
263 pending_requests_ | 266 pending_requests_ |
264 .insert(std::make_pair(id.ToString(), std::move(request_ptr))) | 267 .insert(std::make_pair(id.ToString(), std::move(request_ptr))) |
(...skipping 27 matching lines...) Expand all Loading... | |
292 if (PermissionRequestManager::IsEnabled()) { | 295 if (PermissionRequestManager::IsEnabled()) { |
293 // Infobar persistence and its related UMA is tracked on the infobar | 296 // Infobar persistence and its related UMA is tracked on the infobar |
294 // controller directly. | 297 // controller directly. |
295 PermissionRequestGestureType gesture_type = | 298 PermissionRequestGestureType gesture_type = |
296 user_gesture ? PermissionRequestGestureType::GESTURE | 299 user_gesture ? PermissionRequestGestureType::GESTURE |
297 : PermissionRequestGestureType::NO_GESTURE; | 300 : PermissionRequestGestureType::NO_GESTURE; |
298 DCHECK(content_setting == CONTENT_SETTING_ALLOW || | 301 DCHECK(content_setting == CONTENT_SETTING_ALLOW || |
299 content_setting == CONTENT_SETTING_BLOCK || | 302 content_setting == CONTENT_SETTING_BLOCK || |
300 content_setting == CONTENT_SETTING_DEFAULT); | 303 content_setting == CONTENT_SETTING_DEFAULT); |
301 if (content_setting == CONTENT_SETTING_ALLOW) { | 304 if (content_setting == CONTENT_SETTING_ALLOW) { |
302 PermissionUmaUtil::PermissionGranted(permission_type_, gesture_type, | 305 PermissionUmaUtil::PermissionGranted(content_settings_type_, gesture_type, |
303 requesting_origin, profile_); | 306 requesting_origin, profile_); |
304 } else if (content_setting == CONTENT_SETTING_BLOCK) { | 307 } else if (content_setting == CONTENT_SETTING_BLOCK) { |
305 PermissionUmaUtil::PermissionDenied(permission_type_, gesture_type, | 308 PermissionUmaUtil::PermissionDenied(content_settings_type_, gesture_type, |
306 requesting_origin, profile_); | 309 requesting_origin, profile_); |
307 } else { | 310 } else { |
308 PermissionUmaUtil::PermissionDismissed(permission_type_, gesture_type, | 311 PermissionUmaUtil::PermissionDismissed( |
309 requesting_origin, profile_); | 312 content_settings_type_, gesture_type, requesting_origin, profile_); |
310 } | 313 } |
311 } | 314 } |
312 | 315 |
313 if (content_setting == CONTENT_SETTING_DEFAULT && | 316 if (content_setting == CONTENT_SETTING_DEFAULT && |
314 PermissionDecisionAutoBlocker::GetForProfile(profile_) | 317 PermissionDecisionAutoBlocker::GetForProfile(profile_) |
315 ->RecordDismissAndEmbargo(requesting_origin, permission_type_)) { | 318 ->RecordDismissAndEmbargo(requesting_origin, |
319 content_settings_type_)) { | |
316 // The permission has been embargoed, so it is blocked for this permission | 320 // The permission has been embargoed, so it is blocked for this permission |
317 // request, but not persisted. | 321 // request, but not persisted. |
318 content_setting = CONTENT_SETTING_BLOCK; | 322 content_setting = CONTENT_SETTING_BLOCK; |
319 } | 323 } |
320 | 324 |
321 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, | 325 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, |
322 persist, content_setting); | 326 persist, content_setting); |
323 } | 327 } |
324 | 328 |
325 #if defined(OS_ANDROID) | 329 #if defined(OS_ANDROID) |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
367 DCHECK(content_setting == CONTENT_SETTING_ALLOW || | 371 DCHECK(content_setting == CONTENT_SETTING_ALLOW || |
368 content_setting == CONTENT_SETTING_BLOCK); | 372 content_setting == CONTENT_SETTING_BLOCK); |
369 DCHECK(!requesting_origin.SchemeIsFile()); | 373 DCHECK(!requesting_origin.SchemeIsFile()); |
370 DCHECK(!embedding_origin.SchemeIsFile()); | 374 DCHECK(!embedding_origin.SchemeIsFile()); |
371 | 375 |
372 HostContentSettingsMapFactory::GetForProfile(profile_) | 376 HostContentSettingsMapFactory::GetForProfile(profile_) |
373 ->SetContentSettingDefaultScope(requesting_origin, embedding_origin, | 377 ->SetContentSettingDefaultScope(requesting_origin, embedding_origin, |
374 content_settings_type_, std::string(), | 378 content_settings_type_, std::string(), |
375 content_setting); | 379 content_setting); |
376 } | 380 } |
OLD | NEW |