1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/geolocation/geolocation_permission_context.h"
11 #include "base/bind.h"
12 #include "base/command_line.h"
13 #include "base/containers/hash_tables.h"
14 #include "base/id_map.h"
15 #include "base/memory/scoped_vector.h"
16 #include "base/synchronization/waitable_event.h"
17 #include "base/test/simple_test_clock.h"
18 #include "base/time/clock.h"
19 #include "chrome/browser/chrome_notification_types.h"
20 #include "chrome/browser/content_settings/tab_specific_content_settings.h"
21 #include "chrome/browser/geolocation/geolocation_permission_context_factory.h"
22 #include "chrome/browser/infobars/infobar_service.h"
23 #include "chrome/browser/permissions/permission_request_id.h"
24 #include "chrome/browser/ui/website_settings/mock_permission_bubble_view.h"
25 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
26 #include "chrome/browser/ui/website_settings/permission_bubble_request.h"
27 #include "chrome/common/chrome_switches.h"
28 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
29 #include "chrome/test/base/testing_profile.h"
30 #include "components/content_settings/core/browser/host_content_settings_map.h"
31 #include "components/infobars/core/confirm_infobar_delegate.h"
32 #include "components/infobars/core/infobar.h"
33 #include "content/public/browser/browser_thread.h"
34 #include "content/public/browser/navigation_details.h"
35 #include "content/public/browser/navigation_entry.h"
36 #include "content/public/browser/notification_observer.h"
37 #include "content/public/browser/notification_registrar.h"
38 #include "content/public/browser/notification_service.h"
39 #include "content/public/browser/render_frame_host.h"
40 #include "content/public/browser/web_contents.h"
41 #include "content/public/test/mock_render_process_host.h"
42 #include "content/public/test/test_renderer_host.h"
43 #include "content/public/test/test_utils.h"
44 #include "content/public/test/web_contents_tester.h"
45 #include "testing/gtest/include/gtest/gtest.h"
47 #if defined(OS_ANDROID)
48 #include "base/prefs/pref_service.h"
49 #include "chrome/browser/android/mock_location_settings.h"
50 #include "chrome/browser/geolocation/geolocation_permission_context_android.h"
53 #if defined(ENABLE_EXTENSIONS)
54 #include "extensions/browser/view_type_utils.h"
57 using content::MockRenderProcessHost
;
60 // ClosedInfoBarTracker -------------------------------------------------------
62 // We need to track which infobars were closed.
63 class ClosedInfoBarTracker
: public content::NotificationObserver
{
65 ClosedInfoBarTracker();
66 ~ClosedInfoBarTracker() override
;
68 // content::NotificationObserver:
69 void Observe(int type
,
70 const content::NotificationSource
& source
,
71 const content::NotificationDetails
& details
) override
;
73 size_t size() const { return removed_infobars_
.size(); }
75 bool Contains(infobars::InfoBar
* infobar
) const;
79 FRIEND_TEST_ALL_PREFIXES(GeolocationPermissionContextTests
, TabDestroyed
);
80 content::NotificationRegistrar registrar_
;
81 std::set
<infobars::InfoBar
*> removed_infobars_
;
84 ClosedInfoBarTracker::ClosedInfoBarTracker() {
85 registrar_
.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED
,
86 content::NotificationService::AllSources());
89 ClosedInfoBarTracker::~ClosedInfoBarTracker() {
92 void ClosedInfoBarTracker::Observe(
94 const content::NotificationSource
& source
,
95 const content::NotificationDetails
& details
) {
96 DCHECK(type
== chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED
);
97 removed_infobars_
.insert(
98 content::Details
<infobars::InfoBar::RemovedDetails
>(details
)->first
);
101 bool ClosedInfoBarTracker::Contains(infobars::InfoBar
* infobar
) const {
102 return removed_infobars_
.count(infobar
) != 0;
105 void ClosedInfoBarTracker::Clear() {
106 removed_infobars_
.clear();
110 // GeolocationPermissionContextTests ------------------------------------------
112 class GeolocationPermissionContextTests
113 : public ChromeRenderViewHostTestHarness
{
115 // ChromeRenderViewHostTestHarness:
116 void SetUp() override
;
117 void TearDown() override
;
119 PermissionRequestID
RequestID(int request_id
);
120 PermissionRequestID
RequestIDForTab(int tab
, int request_id
);
121 InfoBarService
* infobar_service() {
122 return InfoBarService::FromWebContents(web_contents());
124 InfoBarService
* infobar_service_for_tab(int tab
) {
125 return InfoBarService::FromWebContents(extra_tabs_
[tab
]);
128 void RequestGeolocationPermission(content::WebContents
* web_contents
,
129 const PermissionRequestID
& id
,
130 const GURL
& requesting_frame
,
133 void PermissionResponse(const PermissionRequestID
& id
,
134 ContentSetting content_setting
);
135 void CheckPermissionMessageSent(int request_id
, bool allowed
);
136 void CheckPermissionMessageSentForTab(int tab
, int request_id
, bool allowed
);
137 void CheckPermissionMessageSentInternal(MockRenderProcessHost
* process
,
140 void AddNewTab(const GURL
& url
);
141 void CheckTabContentsState(const GURL
& requesting_frame
,
142 ContentSetting expected_content_setting
);
143 size_t GetBubblesQueueSize(PermissionBubbleManager
* manager
);
144 void AcceptBubble(PermissionBubbleManager
* manager
);
145 void DenyBubble(PermissionBubbleManager
* manager
);
146 void CloseBubble(PermissionBubbleManager
* manager
);
147 void BubbleManagerDocumentLoadCompleted();
148 void BubbleManagerDocumentLoadCompleted(content::WebContents
* web_contents
);
149 ContentSetting
GetGeolocationContentSetting(GURL frame_0
, GURL frame_1
);
150 bool BubbleEnabled() const;
151 size_t GetNumberOfPrompts();
153 base::string16
GetPromptText();
155 // owned by the browser context
156 GeolocationPermissionContext
* geolocation_permission_context_
;
157 ClosedInfoBarTracker closed_infobar_tracker_
;
158 ScopedVector
<content::WebContents
> extra_tabs_
;
160 // A map between renderer child id and a pair represending the bridge id and
161 // whether the requested permission was allowed.
162 base::hash_map
<int, std::pair
<int, bool> > responses_
;
165 PermissionRequestID
GeolocationPermissionContextTests::RequestID(
167 return PermissionRequestID(
168 web_contents()->GetRenderProcessHost()->GetID(),
169 web_contents()->GetMainFrame()->GetRoutingID(),
174 PermissionRequestID
GeolocationPermissionContextTests::RequestIDForTab(
177 return PermissionRequestID(
178 extra_tabs_
[tab
]->GetRenderProcessHost()->GetID(),
179 extra_tabs_
[tab
]->GetMainFrame()->GetRoutingID(),
184 void GeolocationPermissionContextTests::RequestGeolocationPermission(
185 content::WebContents
* web_contents
,
186 const PermissionRequestID
& id
,
187 const GURL
& requesting_frame
,
189 geolocation_permission_context_
->RequestPermission(
190 web_contents
, id
, requesting_frame
, user_gesture
,
191 base::Bind(&GeolocationPermissionContextTests::PermissionResponse
,
192 base::Unretained(this), id
));
193 content::RunAllBlockingPoolTasksUntilIdle();
196 void GeolocationPermissionContextTests::PermissionResponse(
197 const PermissionRequestID
& id
,
198 ContentSetting content_setting
) {
199 responses_
[id
.render_process_id()] =
200 std::make_pair(id
.request_id(), content_setting
== CONTENT_SETTING_ALLOW
);
203 void GeolocationPermissionContextTests::CheckPermissionMessageSent(
206 CheckPermissionMessageSentInternal(process(), request_id
, allowed
);
209 void GeolocationPermissionContextTests::CheckPermissionMessageSentForTab(
213 CheckPermissionMessageSentInternal(static_cast<MockRenderProcessHost
*>(
214 extra_tabs_
[tab
]->GetRenderProcessHost()),
215 request_id
, allowed
);
218 void GeolocationPermissionContextTests::CheckPermissionMessageSentInternal(
219 MockRenderProcessHost
* process
,
222 ASSERT_EQ(responses_
.count(process
->GetID()), 1U);
223 EXPECT_EQ(request_id
, responses_
[process
->GetID()].first
);
224 EXPECT_EQ(allowed
, responses_
[process
->GetID()].second
);
225 responses_
.erase(process
->GetID());
228 void GeolocationPermissionContextTests::AddNewTab(const GURL
& url
) {
229 content::WebContents
* new_tab
= CreateTestWebContents();
230 new_tab
->GetController().LoadURL(
231 url
, content::Referrer(), ui::PAGE_TRANSITION_TYPED
, std::string());
232 content::NavigationEntry
* entry
= new_tab
->GetController().GetPendingEntry();
233 content::RenderFrameHostTester::For(new_tab
->GetMainFrame())
234 ->SendNavigate(extra_tabs_
.size() + 1, entry
->GetUniqueID(), true, url
);
236 // Set up required helpers, and make this be as "tabby" as the code requires.
237 #if defined(ENABLE_EXTENSIONS)
238 extensions::SetViewType(new_tab
, extensions::VIEW_TYPE_TAB_CONTENTS
);
240 InfoBarService::CreateForWebContents(new_tab
);
241 PermissionBubbleManager::CreateForWebContents(new_tab
);
242 PermissionBubbleManager
* permission_bubble_manager
=
243 PermissionBubbleManager::FromWebContents(new_tab
);
244 MockPermissionBubbleView::SetFactory(permission_bubble_manager
, false);
245 permission_bubble_manager
->DisplayPendingRequests(nullptr);
247 extra_tabs_
.push_back(new_tab
);
250 void GeolocationPermissionContextTests::CheckTabContentsState(
251 const GURL
& requesting_frame
,
252 ContentSetting expected_content_setting
) {
253 TabSpecificContentSettings
* content_settings
=
254 TabSpecificContentSettings::FromWebContents(web_contents());
255 const ContentSettingsUsagesState::StateMap
& state_map
=
256 content_settings
->geolocation_usages_state().state_map();
257 EXPECT_EQ(1U, state_map
.count(requesting_frame
.GetOrigin()));
258 EXPECT_EQ(0U, state_map
.count(requesting_frame
));
259 ContentSettingsUsagesState::StateMap::const_iterator settings
=
260 state_map
.find(requesting_frame
.GetOrigin());
261 ASSERT_FALSE(settings
== state_map
.end())
262 << "geolocation state not found " << requesting_frame
;
263 EXPECT_EQ(expected_content_setting
, settings
->second
);
266 void GeolocationPermissionContextTests::SetUp() {
267 ChromeRenderViewHostTestHarness::SetUp();
269 // Set up required helpers, and make this be as "tabby" as the code requires.
270 #if defined(ENABLE_EXTENSIONS)
271 extensions::SetViewType(web_contents(), extensions::VIEW_TYPE_TAB_CONTENTS
);
273 InfoBarService::CreateForWebContents(web_contents());
274 TabSpecificContentSettings::CreateForWebContents(web_contents());
275 geolocation_permission_context_
=
276 GeolocationPermissionContextFactory::GetForProfile(profile());
277 #if defined(OS_ANDROID)
278 static_cast<GeolocationPermissionContextAndroid
*>(
279 geolocation_permission_context_
)
280 ->SetLocationSettingsForTesting(
281 scoped_ptr
<LocationSettings
>(new MockLocationSettings()));
282 MockLocationSettings::SetLocationStatus(true, true);
284 PermissionBubbleManager::CreateForWebContents(web_contents());
285 PermissionBubbleManager
* permission_bubble_manager
=
286 PermissionBubbleManager::FromWebContents(web_contents());
287 MockPermissionBubbleView::SetFactory(permission_bubble_manager
, false);
288 permission_bubble_manager
->DisplayPendingRequests(nullptr);
291 void GeolocationPermissionContextTests::TearDown() {
293 ChromeRenderViewHostTestHarness::TearDown();
296 size_t GeolocationPermissionContextTests::GetBubblesQueueSize(
297 PermissionBubbleManager
* manager
) {
298 return manager
->requests_
.size();
301 void GeolocationPermissionContextTests::AcceptBubble(
302 PermissionBubbleManager
* manager
) {
306 void GeolocationPermissionContextTests::DenyBubble(
307 PermissionBubbleManager
* manager
) {
311 void GeolocationPermissionContextTests::CloseBubble(
312 PermissionBubbleManager
* manager
) {
316 void GeolocationPermissionContextTests::BubbleManagerDocumentLoadCompleted() {
317 GeolocationPermissionContextTests::BubbleManagerDocumentLoadCompleted(
321 void GeolocationPermissionContextTests::BubbleManagerDocumentLoadCompleted(
322 content::WebContents
* web_contents
) {
323 PermissionBubbleManager::FromWebContents(web_contents
)->
324 DocumentOnLoadCompletedInMainFrame();
327 ContentSetting
GeolocationPermissionContextTests::GetGeolocationContentSetting(
328 GURL frame_0
, GURL frame_1
) {
329 return profile()->GetHostContentSettingsMap()->GetContentSetting(
330 frame_0
, frame_1
, CONTENT_SETTINGS_TYPE_GEOLOCATION
, std::string());
333 bool GeolocationPermissionContextTests::BubbleEnabled() const {
334 #if defined (OS_ANDROID)
341 size_t GeolocationPermissionContextTests::GetNumberOfPrompts() {
342 if (BubbleEnabled()) {
343 PermissionBubbleManager
* manager
=
344 PermissionBubbleManager::FromWebContents(web_contents());
345 return GetBubblesQueueSize(manager
);
347 return infobar_service()->infobar_count();
351 void GeolocationPermissionContextTests::AcceptPrompt() {
352 if (BubbleEnabled()) {
353 PermissionBubbleManager
* manager
=
354 PermissionBubbleManager::FromWebContents(web_contents());
355 AcceptBubble(manager
);
357 infobars::InfoBar
* infobar
= infobar_service()->infobar_at(0);
358 ConfirmInfoBarDelegate
* infobar_delegate
=
359 infobar
->delegate()->AsConfirmInfoBarDelegate();
360 infobar_delegate
->Accept();
364 base::string16
GeolocationPermissionContextTests::GetPromptText() {
365 if (BubbleEnabled()) {
366 PermissionBubbleManager
* manager
=
367 PermissionBubbleManager::FromWebContents(web_contents());
368 return manager
->requests_
.front()->GetMessageText();
370 infobars::InfoBar
* infobar
= infobar_service()->infobar_at(0);
371 ConfirmInfoBarDelegate
* infobar_delegate
=
372 infobar
->delegate()->AsConfirmInfoBarDelegate();
373 return infobar_delegate
->GetMessageText();
376 // Tests ----------------------------------------------------------------------
378 TEST_F(GeolocationPermissionContextTests
, SinglePermissionInfobar
) {
379 if (BubbleEnabled()) return;
381 GURL
requesting_frame("http://www.example.com/geolocation");
382 NavigateAndCommit(requesting_frame
);
383 EXPECT_EQ(0U, infobar_service()->infobar_count());
384 RequestGeolocationPermission(
385 web_contents(), RequestID(0), requesting_frame
, true);
386 ASSERT_EQ(1U, infobar_service()->infobar_count());
387 infobars::InfoBar
* infobar
= infobar_service()->infobar_at(0);
388 ConfirmInfoBarDelegate
* infobar_delegate
=
389 infobar
->delegate()->AsConfirmInfoBarDelegate();
390 ASSERT_TRUE(infobar_delegate
);
391 infobar_delegate
->Cancel();
392 infobar_service()->RemoveInfoBar(infobar
);
393 EXPECT_EQ(1U, closed_infobar_tracker_
.size());
394 EXPECT_TRUE(closed_infobar_tracker_
.Contains(infobar
));
397 TEST_F(GeolocationPermissionContextTests
, SinglePermissionBubble
) {
398 if (!BubbleEnabled()) return;
400 GURL
requesting_frame("http://www.example.com/geolocation");
401 NavigateAndCommit(requesting_frame
);
402 BubbleManagerDocumentLoadCompleted();
404 EXPECT_EQ(0U, GetNumberOfPrompts());
405 RequestGeolocationPermission(
406 web_contents(), RequestID(0), requesting_frame
, true);
407 ASSERT_EQ(1U, GetNumberOfPrompts());
410 #if defined(OS_ANDROID)
411 // Infobar-only tests; Android doesn't support permission bubbles.
412 TEST_F(GeolocationPermissionContextTests
, GeolocationEnabledDisabled
) {
413 GURL
requesting_frame("http://www.example.com/geolocation");
414 NavigateAndCommit(requesting_frame
);
415 MockLocationSettings::SetLocationStatus(true, true);
416 EXPECT_EQ(0U, infobar_service()->infobar_count());
417 RequestGeolocationPermission(
418 web_contents(), RequestID(0), requesting_frame
, true);
419 EXPECT_EQ(1U, infobar_service()->infobar_count());
420 ConfirmInfoBarDelegate
* infobar_delegate_0
=
421 infobar_service()->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate();
422 ASSERT_TRUE(infobar_delegate_0
);
423 base::string16 text_0
= infobar_delegate_0
->GetButtonLabel(
424 ConfirmInfoBarDelegate::BUTTON_OK
);
427 MockLocationSettings::SetLocationStatus(true, false);
428 EXPECT_EQ(0U, infobar_service()->infobar_count());
429 RequestGeolocationPermission(
430 web_contents(), RequestID(0), requesting_frame
, true);
431 EXPECT_EQ(0U, infobar_service()->infobar_count());
434 TEST_F(GeolocationPermissionContextTests
, MasterEnabledGoogleAppsEnabled
) {
435 GURL
requesting_frame("http://www.example.com/geolocation");
436 NavigateAndCommit(requesting_frame
);
437 MockLocationSettings::SetLocationStatus(true, true);
438 EXPECT_EQ(0U, infobar_service()->infobar_count());
439 RequestGeolocationPermission(
440 web_contents(), RequestID(0), requesting_frame
, true);
441 EXPECT_EQ(1U, infobar_service()->infobar_count());
442 ConfirmInfoBarDelegate
* infobar_delegate
=
443 infobar_service()->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate();
444 ASSERT_TRUE(infobar_delegate
);
445 infobar_delegate
->Accept();
446 CheckTabContentsState(requesting_frame
, CONTENT_SETTING_ALLOW
);
447 CheckPermissionMessageSent(0, true);
450 TEST_F(GeolocationPermissionContextTests
, MasterEnabledGoogleAppsDisabled
) {
451 GURL
requesting_frame("http://www.example.com/geolocation");
452 NavigateAndCommit(requesting_frame
);
453 MockLocationSettings::SetLocationStatus(true, false);
454 EXPECT_EQ(0U, infobar_service()->infobar_count());
455 RequestGeolocationPermission(
456 web_contents(), RequestID(0), requesting_frame
, true);
457 EXPECT_EQ(0U, infobar_service()->infobar_count());
461 TEST_F(GeolocationPermissionContextTests
, QueuedPermission
) {
462 GURL
requesting_frame_0("http://www.example.com/geolocation");
463 GURL
requesting_frame_1("http://www.example-2.com/geolocation");
466 GetGeolocationContentSetting(requesting_frame_0
, requesting_frame_1
));
469 GetGeolocationContentSetting(requesting_frame_1
, requesting_frame_1
));
471 NavigateAndCommit(requesting_frame_0
);
472 if (BubbleEnabled()) BubbleManagerDocumentLoadCompleted();
474 // Check that no permission requests have happened yet.
475 EXPECT_EQ(0U, GetNumberOfPrompts());
477 // Request permission for two frames.
478 RequestGeolocationPermission(
479 web_contents(), RequestID(0), requesting_frame_0
, true);
480 RequestGeolocationPermission(
481 web_contents(), RequestID(1), requesting_frame_1
, true);
482 // Ensure only one infobar is created.
483 ASSERT_EQ(1U, GetNumberOfPrompts());
484 base::string16 text_0
= GetPromptText();
486 // Accept the first frame.
488 CheckTabContentsState(requesting_frame_0
, CONTENT_SETTING_ALLOW
);
489 CheckPermissionMessageSent(0, true);
491 if (!BubbleEnabled()) {
492 infobars::InfoBar
* infobar_0
= infobar_service()->infobar_at(0);
493 infobar_service()->RemoveInfoBar(infobar_0
);
494 EXPECT_EQ(1U, closed_infobar_tracker_
.size());
495 EXPECT_TRUE(closed_infobar_tracker_
.Contains(infobar_0
));
496 closed_infobar_tracker_
.Clear();
499 // Now we should have a new infobar for the second frame.
500 ASSERT_EQ(1U, GetNumberOfPrompts());
501 base::string16 text_1
= GetPromptText();
503 // Check that the messages differ.
504 EXPECT_NE(text_0
, text_1
);
506 // Cancel (block) this frame.
507 if (BubbleEnabled()) {
508 PermissionBubbleManager
* manager
=
509 PermissionBubbleManager::FromWebContents(web_contents());
512 infobars::InfoBar
* infobar_1
= infobar_service()->infobar_at(0);
513 infobar_1
->delegate()->AsConfirmInfoBarDelegate()->Cancel();
515 CheckTabContentsState(requesting_frame_1
, CONTENT_SETTING_BLOCK
);
516 CheckPermissionMessageSent(1, false);
518 // Ensure the persisted permissions are ok.
520 CONTENT_SETTING_ALLOW
,
521 GetGeolocationContentSetting(requesting_frame_0
, requesting_frame_0
));
523 CONTENT_SETTING_BLOCK
,
524 GetGeolocationContentSetting(requesting_frame_1
, requesting_frame_0
));
527 TEST_F(GeolocationPermissionContextTests
, HashIsIgnored
) {
528 GURL
url_a("http://www.example.com/geolocation#a");
529 GURL
url_b("http://www.example.com/geolocation#b");
531 // Navigate to the first url.
532 NavigateAndCommit(url_a
);
533 if (BubbleEnabled()) BubbleManagerDocumentLoadCompleted();
535 // Check permission is requested.
536 ASSERT_EQ(0U, GetNumberOfPrompts());
537 RequestGeolocationPermission(
538 web_contents(), RequestID(0), url_a
, BubbleEnabled());
539 ASSERT_EQ(1U, GetNumberOfPrompts());
541 // Change the hash, we'll still be on the same page.
542 NavigateAndCommit(url_b
);
543 if (BubbleEnabled()) BubbleManagerDocumentLoadCompleted();
547 CheckTabContentsState(url_a
, CONTENT_SETTING_ALLOW
);
548 CheckTabContentsState(url_b
, CONTENT_SETTING_ALLOW
);
549 CheckPermissionMessageSent(0, true);
552 if (!BubbleEnabled()) {
553 infobars::InfoBar
* infobar
= infobar_service()->infobar_at(0);
554 infobar_service()->RemoveInfoBar(infobar
);
555 EXPECT_EQ(1U, closed_infobar_tracker_
.size());
556 EXPECT_TRUE(closed_infobar_tracker_
.Contains(infobar
));
560 TEST_F(GeolocationPermissionContextTests
, PermissionForFileScheme
) {
561 // TODO(felt): The bubble is rejecting file:// permission requests.
562 // Fix and enable this test. crbug.com/444047
563 if (BubbleEnabled()) return;
565 GURL
requesting_frame("file://example/geolocation.html");
566 NavigateAndCommit(requesting_frame
);
567 if (BubbleEnabled()) BubbleManagerDocumentLoadCompleted();
569 // Check permission is requested.
570 ASSERT_EQ(0U, GetNumberOfPrompts());
571 RequestGeolocationPermission(
572 web_contents(), RequestID(0), requesting_frame
, true);
573 EXPECT_EQ(1U, GetNumberOfPrompts());
577 CheckTabContentsState(requesting_frame
, CONTENT_SETTING_ALLOW
);
578 CheckPermissionMessageSent(0, true);
580 // Make sure the setting is not stored.
583 GetGeolocationContentSetting(requesting_frame
, requesting_frame
));
586 TEST_F(GeolocationPermissionContextTests
, CancelGeolocationPermissionRequest
) {
587 GURL
frame_0("http://www.example.com/geolocation");
588 GURL
frame_1("http://www.example-2.com/geolocation");
590 CONTENT_SETTING_ASK
, GetGeolocationContentSetting(frame_0
, frame_0
));
592 CONTENT_SETTING_ASK
, GetGeolocationContentSetting(frame_1
, frame_0
));
594 NavigateAndCommit(frame_0
);
595 if (BubbleEnabled()) BubbleManagerDocumentLoadCompleted();
597 ASSERT_EQ(0U, GetNumberOfPrompts());
599 // Request permission for two frames.
600 RequestGeolocationPermission(
601 web_contents(), RequestID(0), frame_0
, true);
602 RequestGeolocationPermission(
603 web_contents(), RequestID(1), frame_1
, true);
605 // Get the first permission request text.
606 ASSERT_EQ(1U, GetNumberOfPrompts());
607 base::string16 text_0
= GetPromptText();
608 ASSERT_FALSE(text_0
.empty());
610 // Simulate the frame going away; the request should be removed.
611 if (BubbleEnabled()) {
612 PermissionBubbleManager
* manager
=
613 PermissionBubbleManager::FromWebContents(web_contents());
614 CloseBubble(manager
);
616 geolocation_permission_context_
->CancelPermissionRequest(web_contents(),
620 // Check that the next pending request is created correctly.
621 base::string16 text_1
= GetPromptText();
622 EXPECT_NE(text_0
, text_1
);
624 // Allow this frame and check that it worked.
626 CheckTabContentsState(frame_1
, CONTENT_SETTING_ALLOW
);
627 CheckPermissionMessageSent(1, true);
629 // Ensure the persisted permissions are ok.
631 CONTENT_SETTING_ASK
, GetGeolocationContentSetting(frame_0
, frame_0
));
633 CONTENT_SETTING_ALLOW
, GetGeolocationContentSetting(frame_1
, frame_0
));
636 TEST_F(GeolocationPermissionContextTests
, InvalidURL
) {
637 // Navigate to the first url.
638 GURL
invalid_embedder("about:blank");
639 GURL requesting_frame
;
640 NavigateAndCommit(invalid_embedder
);
641 if (BubbleEnabled()) BubbleManagerDocumentLoadCompleted();
643 // Nothing should be displayed.
644 EXPECT_EQ(0U, GetNumberOfPrompts());
645 RequestGeolocationPermission(
646 web_contents(), RequestID(0), requesting_frame
, true);
647 EXPECT_EQ(0U, GetNumberOfPrompts());
648 CheckPermissionMessageSent(0, false);
651 TEST_F(GeolocationPermissionContextTests
, SameOriginMultipleTabs
) {
652 GURL
url_a("http://www.example.com/geolocation");
653 GURL
url_b("http://www.example-2.com/geolocation");
654 NavigateAndCommit(url_a
); // Tab A0
655 AddNewTab(url_b
); // Tab B (extra_tabs_[0])
656 AddNewTab(url_a
); // Tab A1 (extra_tabs_[1])
657 if (BubbleEnabled()) {
658 BubbleManagerDocumentLoadCompleted();
659 BubbleManagerDocumentLoadCompleted(extra_tabs_
[0]);
660 BubbleManagerDocumentLoadCompleted(extra_tabs_
[1]);
662 PermissionBubbleManager
* manager_a0
=
663 PermissionBubbleManager::FromWebContents(web_contents());
664 PermissionBubbleManager
* manager_b
=
665 PermissionBubbleManager::FromWebContents(extra_tabs_
[0]);
666 PermissionBubbleManager
* manager_a1
=
667 PermissionBubbleManager::FromWebContents(extra_tabs_
[1]);
669 // Request permission in all three tabs.
670 RequestGeolocationPermission(
671 web_contents(), RequestID(0), url_a
, true);
672 RequestGeolocationPermission(
673 extra_tabs_
[0], RequestIDForTab(0, 0), url_b
, true);
674 RequestGeolocationPermission(
675 extra_tabs_
[1], RequestIDForTab(1, 0), url_a
, true);
676 ASSERT_EQ(1U, GetNumberOfPrompts()); // For A0.
677 if (BubbleEnabled()) {
678 ASSERT_EQ(1U, GetBubblesQueueSize(manager_b
));
679 ASSERT_EQ(1U, GetBubblesQueueSize(manager_a1
));
681 ASSERT_EQ(1U, infobar_service_for_tab(0)->infobar_count());
682 ASSERT_EQ(1U, infobar_service_for_tab(1)->infobar_count());
685 // Accept the permission in tab A0.
686 if (BubbleEnabled()) {
687 AcceptBubble(manager_a0
);
689 infobars::InfoBar
* infobar_a0
= infobar_service()->infobar_at(0);
690 ConfirmInfoBarDelegate
* infobar_delegate_a0
=
691 infobar_a0
->delegate()->AsConfirmInfoBarDelegate();
692 ASSERT_TRUE(infobar_delegate_a0
);
693 infobar_delegate_a0
->Accept();
694 infobar_service()->RemoveInfoBar(infobar_a0
);
695 EXPECT_EQ(2U, closed_infobar_tracker_
.size());
696 EXPECT_TRUE(closed_infobar_tracker_
.Contains(infobar_a0
));
698 CheckPermissionMessageSent(0, true);
699 // Because they're the same origin, this will cause tab A1's infobar to
700 // disappear. It does not cause the bubble to disappear: crbug.com/443013.
701 // TODO(felt): Update this test when the bubble's behavior is changed.
703 ASSERT_EQ(1U, GetBubblesQueueSize(manager_a1
));
705 CheckPermissionMessageSentForTab(1, 0, true);
707 // Either way, tab B should still have a pending permission request.
709 ASSERT_EQ(1U, GetBubblesQueueSize(manager_b
));
711 ASSERT_EQ(1U, infobar_service_for_tab(0)->infobar_count());
714 TEST_F(GeolocationPermissionContextTests
, QueuedOriginMultipleTabs
) {
715 GURL
url_a("http://www.example.com/geolocation");
716 GURL
url_b("http://www.example-2.com/geolocation");
717 NavigateAndCommit(url_a
); // Tab A0.
718 AddNewTab(url_a
); // Tab A1.
719 if (BubbleEnabled()) {
720 BubbleManagerDocumentLoadCompleted();
721 BubbleManagerDocumentLoadCompleted(extra_tabs_
[0]);
723 PermissionBubbleManager
* manager_a0
=
724 PermissionBubbleManager::FromWebContents(web_contents());
725 PermissionBubbleManager
* manager_a1
=
726 PermissionBubbleManager::FromWebContents(extra_tabs_
[0]);
728 // Request permission in both tabs; the extra tab will have two permission
729 // requests from two origins.
730 RequestGeolocationPermission(
731 web_contents(), RequestID(0), url_a
, true);
732 RequestGeolocationPermission(
733 extra_tabs_
[0], RequestIDForTab(0, 0), url_a
, true);
734 RequestGeolocationPermission(
735 extra_tabs_
[0], RequestIDForTab(0, 1), url_b
, true);
736 if (BubbleEnabled()) {
737 ASSERT_EQ(1U, GetBubblesQueueSize(manager_a0
));
738 ASSERT_EQ(1U, GetBubblesQueueSize(manager_a1
));
740 ASSERT_EQ(1U, infobar_service()->infobar_count());
741 ASSERT_EQ(1U, infobar_service_for_tab(0)->infobar_count());
744 // Accept the first request in tab A1.
745 if (BubbleEnabled()) {
746 AcceptBubble(manager_a1
);
748 infobars::InfoBar
* infobar_a1
= infobar_service_for_tab(0)->infobar_at(0);
749 ConfirmInfoBarDelegate
* infobar_delegate_a1
=
750 infobar_a1
->delegate()->AsConfirmInfoBarDelegate();
751 ASSERT_TRUE(infobar_delegate_a1
);
752 infobar_delegate_a1
->Accept();
753 infobar_service_for_tab(0)->RemoveInfoBar(infobar_a1
);
754 EXPECT_EQ(2U, closed_infobar_tracker_
.size());
755 EXPECT_TRUE(closed_infobar_tracker_
.Contains(infobar_a1
));
757 CheckPermissionMessageSentForTab(0, 0, true);
759 // Because they're the same origin, this will cause tab A0's infobar to
760 // disappear. It does not cause the bubble to disappear: crbug.com/443013.
761 // TODO(felt): Update this test when the bubble's behavior is changed.
762 if (BubbleEnabled()) {
763 EXPECT_EQ(1U, GetBubblesQueueSize(manager_a0
));
765 EXPECT_EQ(0U, infobar_service()->infobar_count());
766 CheckPermissionMessageSent(0, true);
769 // The second request should now be visible in tab A1.
771 ASSERT_EQ(1U, GetBubblesQueueSize(manager_a1
));
773 ASSERT_EQ(1U, infobar_service_for_tab(0)->infobar_count());
775 // Accept the second request and check that it's gone.
776 if (BubbleEnabled()) {
777 AcceptBubble(manager_a1
);
778 EXPECT_EQ(0U, GetBubblesQueueSize(manager_a1
));
780 infobars::InfoBar
* infobar_1
= infobar_service_for_tab(0)->infobar_at(0);
781 ConfirmInfoBarDelegate
* infobar_delegate_1
=
782 infobar_1
->delegate()->AsConfirmInfoBarDelegate();
783 ASSERT_TRUE(infobar_delegate_1
);
784 infobar_delegate_1
->Accept();
788 TEST_F(GeolocationPermissionContextTests
, TabDestroyed
) {
789 GURL
requesting_frame_0("http://www.example.com/geolocation");
790 GURL
requesting_frame_1("http://www.example-2.com/geolocation");
793 GetGeolocationContentSetting(requesting_frame_0
, requesting_frame_0
));
796 GetGeolocationContentSetting(requesting_frame_1
, requesting_frame_0
));
798 NavigateAndCommit(requesting_frame_0
);
799 if (BubbleEnabled()) BubbleManagerDocumentLoadCompleted();
801 // Request permission for two frames.
802 RequestGeolocationPermission(
803 web_contents(), RequestID(0), requesting_frame_0
, false);
804 RequestGeolocationPermission(
805 web_contents(), RequestID(1), requesting_frame_1
, false);
807 // Ensure only one prompt is created.
808 ASSERT_EQ(1U, GetNumberOfPrompts());
810 // Delete the tab contents.
811 if (!BubbleEnabled()) {
812 infobars::InfoBar
* infobar
= infobar_service()->infobar_at(0);
814 ASSERT_EQ(1U, closed_infobar_tracker_
.size());
815 ASSERT_TRUE(closed_infobar_tracker_
.Contains(infobar
));
818 // The content settings should not have changed.
821 GetGeolocationContentSetting(requesting_frame_0
, requesting_frame_0
));
824 GetGeolocationContentSetting(requesting_frame_1
, requesting_frame_0
));
827 TEST_F(GeolocationPermissionContextTests
, LastUsageAudited
) {
828 GURL
requesting_frame("http://www.example.com/geolocation");
829 NavigateAndCommit(requesting_frame
);
830 if (BubbleEnabled()) BubbleManagerDocumentLoadCompleted();
832 base::SimpleTestClock
* test_clock
= new base::SimpleTestClock
;
833 test_clock
->SetNow(base::Time::UnixEpoch() +
834 base::TimeDelta::FromSeconds(10));
836 HostContentSettingsMap
* map
= profile()->GetHostContentSettingsMap();
837 map
->SetPrefClockForTesting(scoped_ptr
<base::Clock
>(test_clock
));
839 // The permission shouldn't have been used yet.
840 EXPECT_EQ(map
->GetLastUsage(requesting_frame
.GetOrigin(),
841 requesting_frame
.GetOrigin(),
842 CONTENT_SETTINGS_TYPE_GEOLOCATION
).ToDoubleT(),
844 ASSERT_EQ(0U, GetNumberOfPrompts());
845 RequestGeolocationPermission(
846 web_contents(), RequestID(0), requesting_frame
, false);
847 ASSERT_EQ(1U, GetNumberOfPrompts());
850 CheckTabContentsState(requesting_frame
, CONTENT_SETTING_ALLOW
);
851 CheckPermissionMessageSent(0, true);
853 // Permission has been used at the starting time.
854 EXPECT_EQ(map
->GetLastUsage(requesting_frame
.GetOrigin(),
855 requesting_frame
.GetOrigin(),
856 CONTENT_SETTINGS_TYPE_GEOLOCATION
).ToDoubleT(),
859 test_clock
->Advance(base::TimeDelta::FromSeconds(3));
860 RequestGeolocationPermission(
861 web_contents(), RequestID(0), requesting_frame
, false);
863 // Permission has been used three seconds later.
864 EXPECT_EQ(map
->GetLastUsage(requesting_frame
.GetOrigin(),
865 requesting_frame
.GetOrigin(),
866 CONTENT_SETTINGS_TYPE_GEOLOCATION
).ToDoubleT(),
870 TEST_F(GeolocationPermissionContextTests
, LastUsageAuditedMultipleFrames
) {
871 base::SimpleTestClock
* test_clock
= new base::SimpleTestClock
;
872 test_clock
->SetNow(base::Time::UnixEpoch() +
873 base::TimeDelta::FromSeconds(10));
875 HostContentSettingsMap
* map
= profile()->GetHostContentSettingsMap();
876 map
->SetPrefClockForTesting(scoped_ptr
<base::Clock
>(test_clock
));
878 GURL
requesting_frame_0("http://www.example.com/geolocation");
879 GURL
requesting_frame_1("http://www.example-2.com/geolocation");
881 // The permission shouldn't have been used yet.
882 EXPECT_EQ(map
->GetLastUsage(requesting_frame_0
.GetOrigin(),
883 requesting_frame_0
.GetOrigin(),
884 CONTENT_SETTINGS_TYPE_GEOLOCATION
).ToDoubleT(),
886 EXPECT_EQ(map
->GetLastUsage(requesting_frame_1
.GetOrigin(),
887 requesting_frame_0
.GetOrigin(),
888 CONTENT_SETTINGS_TYPE_GEOLOCATION
).ToDoubleT(),
891 NavigateAndCommit(requesting_frame_0
);
892 if (BubbleEnabled()) BubbleManagerDocumentLoadCompleted();
894 EXPECT_EQ(0U, GetNumberOfPrompts());
896 // Request permission for two frames.
897 RequestGeolocationPermission(
898 web_contents(), RequestID(0), requesting_frame_0
, false);
899 RequestGeolocationPermission(
900 web_contents(), RequestID(1), requesting_frame_1
, false);
902 // Ensure only one infobar is created.
903 ASSERT_EQ(1U, GetNumberOfPrompts());
905 // Accept the first frame.
907 if (!BubbleEnabled())
908 infobar_service()->RemoveInfoBar(infobar_service()->infobar_at(0));
909 CheckTabContentsState(requesting_frame_0
, CONTENT_SETTING_ALLOW
);
910 CheckPermissionMessageSent(0, true);
912 // Verify that accepting the first didn't accept because it's embedded
914 EXPECT_EQ(map
->GetLastUsage(requesting_frame_0
.GetOrigin(),
915 requesting_frame_0
.GetOrigin(),
916 CONTENT_SETTINGS_TYPE_GEOLOCATION
).ToDoubleT(),
918 EXPECT_EQ(map
->GetLastUsage(requesting_frame_1
.GetOrigin(),
919 requesting_frame_0
.GetOrigin(),
920 CONTENT_SETTINGS_TYPE_GEOLOCATION
).ToDoubleT(),
923 ASSERT_EQ(1U, GetNumberOfPrompts());
925 test_clock
->Advance(base::TimeDelta::FromSeconds(1));
927 // Allow the second frame.
929 CheckTabContentsState(requesting_frame_1
, CONTENT_SETTING_ALLOW
);
930 CheckPermissionMessageSent(1, true);
931 if (!BubbleEnabled())
932 infobar_service()->RemoveInfoBar(infobar_service()->infobar_at(0));
934 // Verify that the times are different.
935 EXPECT_EQ(map
->GetLastUsage(requesting_frame_0
.GetOrigin(),
936 requesting_frame_0
.GetOrigin(),
937 CONTENT_SETTINGS_TYPE_GEOLOCATION
).ToDoubleT(),
939 EXPECT_EQ(map
->GetLastUsage(requesting_frame_1
.GetOrigin(),
940 requesting_frame_0
.GetOrigin(),
941 CONTENT_SETTINGS_TYPE_GEOLOCATION
).ToDoubleT(),
944 test_clock
->Advance(base::TimeDelta::FromSeconds(2));
945 RequestGeolocationPermission(
946 web_contents(), RequestID(0), requesting_frame_0
, false);
948 // Verify that requesting permission in one frame doesn't update other where
949 // it is the embedder.
950 EXPECT_EQ(map
->GetLastUsage(requesting_frame_0
.GetOrigin(),
951 requesting_frame_0
.GetOrigin(),
952 CONTENT_SETTINGS_TYPE_GEOLOCATION
).ToDoubleT(),
954 EXPECT_EQ(map
->GetLastUsage(requesting_frame_1
.GetOrigin(),
955 requesting_frame_0
.GetOrigin(),
956 CONTENT_SETTINGS_TYPE_GEOLOCATION
).ToDoubleT(),