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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/message_loop/message_loop.h" |
10 #include "base/metrics/field_trial.h" | 11 #include "base/metrics/field_trial.h" |
11 #include "base/test/mock_entropy_provider.h" | 12 #include "base/test/mock_entropy_provider.h" |
| 13 #include "base/test/test_mock_time_task_runner.h" |
| 14 #include "base/time/time.h" |
12 #include "build/build_config.h" | 15 #include "build/build_config.h" |
13 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 16 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
14 #include "chrome/browser/infobars/infobar_service.h" | 17 #include "chrome/browser/infobars/infobar_service.h" |
15 #include "chrome/browser/permissions/permission_queue_controller.h" | 18 #include "chrome/browser/permissions/permission_queue_controller.h" |
16 #include "chrome/browser/permissions/permission_request_id.h" | 19 #include "chrome/browser/permissions/permission_request_id.h" |
17 #include "chrome/browser/permissions/permission_util.h" | 20 #include "chrome/browser/permissions/permission_util.h" |
18 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" | 21 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" |
19 #include "chrome/common/chrome_switches.h" | 22 #include "chrome/common/chrome_switches.h" |
20 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 23 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
21 #include "chrome/test/base/testing_profile.h" | 24 #include "chrome/test/base/testing_profile.h" |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 | 84 |
82 void ResetFieldTrialList() { | 85 void ResetFieldTrialList() { |
83 // Destroy the existing FieldTrialList before creating a new one to avoid | 86 // Destroy the existing FieldTrialList before creating a new one to avoid |
84 // a DCHECK. | 87 // a DCHECK. |
85 field_trial_list_.reset(); | 88 field_trial_list_.reset(); |
86 field_trial_list_.reset(new base::FieldTrialList( | 89 field_trial_list_.reset(new base::FieldTrialList( |
87 new base::MockEntropyProvider)); | 90 new base::MockEntropyProvider)); |
88 variations::testing::ClearAllVariationParams(); | 91 variations::testing::ClearAllVariationParams(); |
89 } | 92 } |
90 | 93 |
| 94 ContentSetting GetContentSettingFromMap(GURL url_a, GURL url_b) { |
| 95 return HostContentSettingsMapFactory::GetForProfile(profile()) |
| 96 ->GetContentSetting(url_a.GetOrigin(), url_b.GetOrigin(), |
| 97 content_settings_type(), std::string()); |
| 98 } |
| 99 |
91 protected: | 100 protected: |
92 void UpdateTabContext(const PermissionRequestID& id, | 101 void UpdateTabContext(const PermissionRequestID& id, |
93 const GURL& requesting_origin, | 102 const GURL& requesting_origin, |
94 bool allowed) override { | 103 bool allowed) override { |
95 tab_context_updated_ = true; | 104 tab_context_updated_ = true; |
96 } | 105 } |
97 | 106 |
98 bool IsRestrictedToSecureOrigins() const override { | 107 bool IsRestrictedToSecureOrigins() const override { |
99 return false; | 108 return false; |
100 } | 109 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 permission_context.RequestPermission( | 151 permission_context.RequestPermission( |
143 web_contents(), | 152 web_contents(), |
144 id, url, true, | 153 id, url, true, |
145 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 154 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
146 base::Unretained(&permission_context))); | 155 base::Unretained(&permission_context))); |
147 | 156 |
148 RespondToPermission(&permission_context, id, url, true); | 157 RespondToPermission(&permission_context, id, url, true); |
149 EXPECT_TRUE(permission_context.permission_set()); | 158 EXPECT_TRUE(permission_context.permission_set()); |
150 EXPECT_TRUE(permission_context.permission_granted()); | 159 EXPECT_TRUE(permission_context.permission_granted()); |
151 EXPECT_TRUE(permission_context.tab_context_updated()); | 160 EXPECT_TRUE(permission_context.tab_context_updated()); |
152 | 161 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
153 ContentSetting setting = | 162 permission_context.GetContentSettingFromMap(url, url)); |
154 HostContentSettingsMapFactory::GetForProfile(profile()) | |
155 ->GetContentSetting(url.GetOrigin(), | |
156 url.GetOrigin(), | |
157 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
158 std::string()); | |
159 EXPECT_EQ(CONTENT_SETTING_ALLOW, setting); | |
160 } | 163 } |
161 | 164 |
162 void TestAskAndDismiss_TestContent() { | 165 void TestAskAndDismiss_TestContent() { |
163 TestPermissionContext permission_context( | 166 TestPermissionContext permission_context( |
164 profile(), content::PermissionType::MIDI_SYSEX, | 167 profile(), content::PermissionType::MIDI_SYSEX, |
165 CONTENT_SETTINGS_TYPE_MIDI_SYSEX); | 168 CONTENT_SETTINGS_TYPE_MIDI_SYSEX); |
166 GURL url("http://www.google.es"); | 169 GURL url("http://www.google.es"); |
167 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); | 170 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); |
168 | 171 |
169 const PermissionRequestID id( | 172 const PermissionRequestID id( |
170 web_contents()->GetRenderProcessHost()->GetID(), | 173 web_contents()->GetRenderProcessHost()->GetID(), |
171 web_contents()->GetMainFrame()->GetRoutingID(), | 174 web_contents()->GetMainFrame()->GetRoutingID(), |
172 -1); | 175 -1); |
173 permission_context.RequestPermission( | 176 permission_context.RequestPermission( |
174 web_contents(), | 177 web_contents(), |
175 id, url, true, | 178 id, url, true, |
176 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 179 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
177 base::Unretained(&permission_context))); | 180 base::Unretained(&permission_context))); |
178 | 181 |
179 RespondToPermission(&permission_context, id, url, false); | 182 RespondToPermission(&permission_context, id, url, false); |
180 EXPECT_TRUE(permission_context.permission_set()); | 183 EXPECT_TRUE(permission_context.permission_set()); |
181 EXPECT_FALSE(permission_context.permission_granted()); | 184 EXPECT_FALSE(permission_context.permission_granted()); |
182 EXPECT_TRUE(permission_context.tab_context_updated()); | 185 EXPECT_TRUE(permission_context.tab_context_updated()); |
183 | 186 EXPECT_EQ(CONTENT_SETTING_ASK, |
184 ContentSetting setting = | 187 permission_context.GetContentSettingFromMap(url, url)); |
185 HostContentSettingsMapFactory::GetForProfile(profile()) | |
186 ->GetContentSetting(url.GetOrigin(), | |
187 url.GetOrigin(), | |
188 CONTENT_SETTINGS_TYPE_MIDI_SYSEX, | |
189 std::string()); | |
190 EXPECT_EQ(CONTENT_SETTING_ASK, setting); | |
191 } | 188 } |
192 | 189 |
193 void TestRequestPermissionInvalidUrl( | 190 void TestRequestPermissionInvalidUrl( |
194 content::PermissionType permission_type, | 191 content::PermissionType permission_type, |
195 ContentSettingsType content_settings_type) { | 192 ContentSettingsType content_settings_type) { |
196 TestPermissionContext permission_context(profile(), permission_type, | 193 TestPermissionContext permission_context(profile(), permission_type, |
197 content_settings_type); | 194 content_settings_type); |
198 GURL url; | 195 GURL url; |
199 ASSERT_FALSE(url.is_valid()); | 196 ASSERT_FALSE(url.is_valid()); |
200 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); | 197 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); |
201 | 198 |
202 const PermissionRequestID id( | 199 const PermissionRequestID id( |
203 web_contents()->GetRenderProcessHost()->GetID(), | 200 web_contents()->GetRenderProcessHost()->GetID(), |
204 web_contents()->GetMainFrame()->GetRoutingID(), | 201 web_contents()->GetMainFrame()->GetRoutingID(), |
205 -1); | 202 -1); |
206 permission_context.RequestPermission( | 203 permission_context.RequestPermission( |
207 web_contents(), | 204 web_contents(), |
208 id, url, true, | 205 id, url, true, |
209 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 206 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
210 base::Unretained(&permission_context))); | 207 base::Unretained(&permission_context))); |
211 | 208 |
212 EXPECT_TRUE(permission_context.permission_set()); | 209 EXPECT_TRUE(permission_context.permission_set()); |
213 EXPECT_FALSE(permission_context.permission_granted()); | 210 EXPECT_FALSE(permission_context.permission_granted()); |
214 EXPECT_TRUE(permission_context.tab_context_updated()); | 211 EXPECT_TRUE(permission_context.tab_context_updated()); |
215 | 212 EXPECT_EQ(CONTENT_SETTING_ASK, |
216 ContentSetting setting = | 213 permission_context.GetContentSettingFromMap(url, url)); |
217 HostContentSettingsMapFactory::GetForProfile(profile()) | |
218 ->GetContentSetting(url.GetOrigin(), url.GetOrigin(), | |
219 content_settings_type, std::string()); | |
220 EXPECT_EQ(CONTENT_SETTING_ASK, setting); | |
221 } | 214 } |
222 | 215 |
223 void TestGrantAndRevoke_TestContent(content::PermissionType permission_type, | 216 void TestGrantAndRevoke_TestContent(content::PermissionType permission_type, |
224 ContentSettingsType content_settings_type, | 217 ContentSettingsType content_settings_type, |
225 ContentSetting expected_default) { | 218 ContentSetting expected_default) { |
226 TestPermissionContext permission_context(profile(), permission_type, | 219 TestPermissionContext permission_context(profile(), permission_type, |
227 content_settings_type); | 220 content_settings_type); |
228 GURL url("https://www.google.com"); | 221 GURL url("https://www.google.com"); |
229 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); | 222 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); |
230 | 223 |
231 const PermissionRequestID id( | 224 const PermissionRequestID id( |
232 web_contents()->GetRenderProcessHost()->GetID(), | 225 web_contents()->GetRenderProcessHost()->GetID(), |
233 web_contents()->GetMainFrame()->GetRoutingID(), | 226 web_contents()->GetMainFrame()->GetRoutingID(), |
234 -1); | 227 -1); |
235 permission_context.RequestPermission( | 228 permission_context.RequestPermission( |
236 web_contents(), | 229 web_contents(), |
237 id, url, true, | 230 id, url, true, |
238 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 231 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
239 base::Unretained(&permission_context))); | 232 base::Unretained(&permission_context))); |
240 | 233 |
241 RespondToPermission(&permission_context, id, url, true); | 234 RespondToPermission(&permission_context, id, url, true); |
242 EXPECT_TRUE(permission_context.permission_set()); | 235 EXPECT_TRUE(permission_context.permission_set()); |
243 EXPECT_TRUE(permission_context.permission_granted()); | 236 EXPECT_TRUE(permission_context.permission_granted()); |
244 EXPECT_TRUE(permission_context.tab_context_updated()); | 237 EXPECT_TRUE(permission_context.tab_context_updated()); |
245 | 238 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
246 ContentSetting setting = | 239 permission_context.GetContentSettingFromMap(url, url)); |
247 HostContentSettingsMapFactory::GetForProfile(profile()) | |
248 ->GetContentSetting(url.GetOrigin(), url.GetOrigin(), | |
249 content_settings_type, std::string()); | |
250 EXPECT_EQ(CONTENT_SETTING_ALLOW, setting); | |
251 | 240 |
252 // Try to reset permission. | 241 // Try to reset permission. |
253 permission_context.ResetPermission(url.GetOrigin(), url.GetOrigin()); | 242 permission_context.ResetPermission(url.GetOrigin(), url.GetOrigin()); |
254 ContentSetting setting_after_reset = | 243 ContentSetting setting_after_reset = |
255 HostContentSettingsMapFactory::GetForProfile(profile()) | 244 permission_context.GetContentSettingFromMap(url, url); |
256 ->GetContentSetting(url.GetOrigin(), url.GetOrigin(), | |
257 content_settings_type, std::string()); | |
258 ContentSetting default_setting = | 245 ContentSetting default_setting = |
259 HostContentSettingsMapFactory::GetForProfile(profile()) | 246 HostContentSettingsMapFactory::GetForProfile(profile()) |
260 ->GetDefaultContentSetting(content_settings_type, nullptr); | 247 ->GetDefaultContentSetting(content_settings_type, nullptr); |
261 EXPECT_EQ(default_setting, setting_after_reset); | 248 EXPECT_EQ(default_setting, setting_after_reset); |
262 } | 249 } |
263 | 250 |
264 void TestGlobalPermissionsKillSwitch( | 251 void TestGlobalPermissionsKillSwitch( |
265 content::PermissionType permission_type, | 252 content::PermissionType permission_type, |
266 ContentSettingsType content_settings_type) { | 253 ContentSettingsType content_settings_type) { |
267 TestPermissionContext permission_context(profile(), permission_type, | 254 TestPermissionContext permission_context(profile(), permission_type, |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
359 #endif | 346 #endif |
360 TestGrantAndRevoke_TestContent(content::PermissionType::MIDI_SYSEX, | 347 TestGrantAndRevoke_TestContent(content::PermissionType::MIDI_SYSEX, |
361 CONTENT_SETTINGS_TYPE_MIDI_SYSEX, | 348 CONTENT_SETTINGS_TYPE_MIDI_SYSEX, |
362 CONTENT_SETTING_ASK); | 349 CONTENT_SETTING_ASK); |
363 TestGrantAndRevoke_TestContent(content::PermissionType::PUSH_MESSAGING, | 350 TestGrantAndRevoke_TestContent(content::PermissionType::PUSH_MESSAGING, |
364 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | 351 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, |
365 CONTENT_SETTING_ASK); | 352 CONTENT_SETTING_ASK); |
366 } | 353 } |
367 #endif | 354 #endif |
368 | 355 |
| 356 // Tests content_settings::ContentSettingsInfo::DENY_IN_INCOGNITO_AFTER_DELAY. |
| 357 #if defined(ENABLE_NOTIFICATIONS) |
| 358 TEST_F(PermissionContextBaseTests, TestDenyInIncognitoAfterDelay) { |
| 359 TestPermissionContext permission_context( |
| 360 profile()->GetOffTheRecordProfile(), |
| 361 content::PermissionType::NOTIFICATIONS, |
| 362 CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 363 GURL url("http://www.google.com"); |
| 364 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); |
| 365 |
| 366 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), |
| 367 web_contents()->GetMainFrame()->GetRoutingID(), |
| 368 -1); |
| 369 |
| 370 scoped_refptr<base::SingleThreadTaskRunner> old_task_runner( |
| 371 base::MessageLoop::current()->task_runner()); |
| 372 scoped_refptr<base::TestMockTimeTaskRunner> task_runner( |
| 373 new base::TestMockTimeTaskRunner(base::Time::Now(), |
| 374 base::TimeTicks::Now())); |
| 375 base::MessageLoop::current()->SetTaskRunner(task_runner); |
| 376 |
| 377 permission_context.RequestPermission( |
| 378 web_contents(), id, url, true /* user_gesture */, |
| 379 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
| 380 base::Unretained(&permission_context))); |
| 381 |
| 382 // Should be blocked after 1-2 seconds, but the timer is reset whenever the |
| 383 // tab is not visible, so these 500ms never add up to >= 1 second. |
| 384 for (int n = 0; n < 10; n++) { |
| 385 web_contents()->WasShown(); |
| 386 task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(500)); |
| 387 web_contents()->WasHidden(); |
| 388 } |
| 389 |
| 390 EXPECT_FALSE(permission_context.permission_set()); |
| 391 EXPECT_FALSE(permission_context.tab_context_updated()); |
| 392 EXPECT_EQ(CONTENT_SETTING_ASK, |
| 393 permission_context.GetContentSettingFromMap(url, url)); |
| 394 |
| 395 // Time elapsed whilst hidden is not counted. |
| 396 // n.b. This line also clears out any old scheduled timer tasks. This is |
| 397 // important, because otherwise Timer::Reset (triggered by |
| 398 // VisibilityTimerTabHelper::WasShown) may choose to re-use an existing |
| 399 // scheduled task, and when it fires Timer::RunScheduledTask will call |
| 400 // TimeTicks::Now() (which unlike task_runner->NowTicks(), we can't fake), |
| 401 // and miscalculate the remaining delay at which to fire the timer. |
| 402 task_runner->FastForwardBy(base::TimeDelta::FromDays(1)); |
| 403 |
| 404 EXPECT_FALSE(permission_context.permission_set()); |
| 405 EXPECT_FALSE(permission_context.tab_context_updated()); |
| 406 EXPECT_EQ(CONTENT_SETTING_ASK, |
| 407 permission_context.GetContentSettingFromMap(url, url)); |
| 408 |
| 409 // Should be blocked after 1-2 seconds. So 500ms is not enough. |
| 410 web_contents()->WasShown(); |
| 411 task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(500)); |
| 412 |
| 413 EXPECT_FALSE(permission_context.permission_set()); |
| 414 EXPECT_FALSE(permission_context.tab_context_updated()); |
| 415 EXPECT_EQ(CONTENT_SETTING_ASK, |
| 416 permission_context.GetContentSettingFromMap(url, url)); |
| 417 |
| 418 // But 5*500ms > 2 seconds, so it should now be blocked. |
| 419 for (int n = 0; n < 4; n++) |
| 420 task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(500)); |
| 421 |
| 422 EXPECT_TRUE(permission_context.permission_set()); |
| 423 EXPECT_TRUE(permission_context.tab_context_updated()); |
| 424 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 425 permission_context.GetContentSettingFromMap(url, url)); |
| 426 |
| 427 base::MessageLoop::current()->SetTaskRunner(old_task_runner); |
| 428 } |
| 429 #endif |
| 430 |
369 // Tests the global kill switch by enabling/disabling the Field Trials. | 431 // Tests the global kill switch by enabling/disabling the Field Trials. |
370 TEST_F(PermissionContextBaseTests, TestGlobalKillSwitch) { | 432 TEST_F(PermissionContextBaseTests, TestGlobalKillSwitch) { |
371 TestGlobalPermissionsKillSwitch(content::PermissionType::GEOLOCATION, | 433 TestGlobalPermissionsKillSwitch(content::PermissionType::GEOLOCATION, |
372 CONTENT_SETTINGS_TYPE_GEOLOCATION); | 434 CONTENT_SETTINGS_TYPE_GEOLOCATION); |
373 TestGlobalPermissionsKillSwitch(content::PermissionType::NOTIFICATIONS, | 435 TestGlobalPermissionsKillSwitch(content::PermissionType::NOTIFICATIONS, |
374 CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | 436 CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
375 TestGlobalPermissionsKillSwitch(content::PermissionType::MIDI_SYSEX, | 437 TestGlobalPermissionsKillSwitch(content::PermissionType::MIDI_SYSEX, |
376 CONTENT_SETTINGS_TYPE_MIDI_SYSEX); | 438 CONTENT_SETTINGS_TYPE_MIDI_SYSEX); |
377 TestGlobalPermissionsKillSwitch(content::PermissionType::PUSH_MESSAGING, | 439 TestGlobalPermissionsKillSwitch(content::PermissionType::PUSH_MESSAGING, |
378 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING); | 440 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING); |
379 TestGlobalPermissionsKillSwitch(content::PermissionType::DURABLE_STORAGE, | 441 TestGlobalPermissionsKillSwitch(content::PermissionType::DURABLE_STORAGE, |
380 CONTENT_SETTINGS_TYPE_DURABLE_STORAGE); | 442 CONTENT_SETTINGS_TYPE_DURABLE_STORAGE); |
381 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) | 443 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) |
382 TestGlobalPermissionsKillSwitch( | 444 TestGlobalPermissionsKillSwitch( |
383 content::PermissionType::PROTECTED_MEDIA_IDENTIFIER, | 445 content::PermissionType::PROTECTED_MEDIA_IDENTIFIER, |
384 CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER); | 446 CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER); |
385 #endif | 447 #endif |
386 TestGlobalPermissionsKillSwitch(content::PermissionType::AUDIO_CAPTURE, | 448 TestGlobalPermissionsKillSwitch(content::PermissionType::AUDIO_CAPTURE, |
387 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC); | 449 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC); |
388 TestGlobalPermissionsKillSwitch(content::PermissionType::VIDEO_CAPTURE, | 450 TestGlobalPermissionsKillSwitch(content::PermissionType::VIDEO_CAPTURE, |
389 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA); | 451 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA); |
390 } | 452 } |
OLD | NEW |