OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/geolocation/geolocation_permission_context.h" | 5 #include "chrome/browser/geolocation/geolocation_permission_context.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 void ClosedInfoBarTracker::Clear() { | 96 void ClosedInfoBarTracker::Clear() { |
97 removed_infobars_.clear(); | 97 removed_infobars_.clear(); |
98 } | 98 } |
99 | 99 |
100 | 100 |
101 // GeolocationPermissionContextTests ------------------------------------------ | 101 // GeolocationPermissionContextTests ------------------------------------------ |
102 | 102 |
103 class GeolocationPermissionContextTests | 103 class GeolocationPermissionContextTests |
104 : public ChromeRenderViewHostTestHarness { | 104 : public ChromeRenderViewHostTestHarness { |
105 protected: | 105 protected: |
| 106 |
106 // ChromeRenderViewHostTestHarness: | 107 // ChromeRenderViewHostTestHarness: |
107 virtual void SetUp() OVERRIDE; | 108 virtual void SetUp() OVERRIDE; |
108 virtual void TearDown() OVERRIDE; | 109 virtual void TearDown() OVERRIDE; |
109 | 110 |
110 PermissionRequestID RequestID(int bridge_id); | 111 PermissionRequestID RequestID(int bridge_id); |
111 PermissionRequestID RequestIDForTab(int tab, int bridge_id); | 112 PermissionRequestID RequestIDForTab(int tab, int bridge_id); |
112 InfoBarService* infobar_service() { | 113 InfoBarService* infobar_service() { |
113 return InfoBarService::FromWebContents(web_contents()); | 114 return InfoBarService::FromWebContents(web_contents()); |
114 } | 115 } |
115 InfoBarService* infobar_service_for_tab(int tab) { | 116 InfoBarService* infobar_service_for_tab(int tab) { |
116 return InfoBarService::FromWebContents(extra_tabs_[tab]); | 117 return InfoBarService::FromWebContents(extra_tabs_[tab]); |
117 } | 118 } |
118 | 119 |
119 void RequestGeolocationPermission(content::WebContents* web_contents, | 120 void RequestGeolocationPermission(content::WebContents* web_contents, |
120 const PermissionRequestID& id, | 121 const PermissionRequestID& id, |
121 const GURL& requesting_frame); | 122 const GURL& requesting_frame); |
122 void RequestGeolocationPermission(content::WebContents* web_contents, | 123 |
123 const PermissionRequestID& id, | |
124 const GURL& requesting_frame, | |
125 base::Closure* cancel_callback); | |
126 void PermissionResponse(const PermissionRequestID& id, | 124 void PermissionResponse(const PermissionRequestID& id, |
127 bool allowed); | 125 bool allowed); |
128 void CheckPermissionMessageSent(int bridge_id, bool allowed); | 126 void CheckPermissionMessageSent(int bridge_id, bool allowed); |
129 void CheckPermissionMessageSentForTab(int tab, int bridge_id, bool allowed); | 127 void CheckPermissionMessageSentForTab(int tab, int bridge_id, bool allowed); |
130 void CheckPermissionMessageSentInternal(MockRenderProcessHost* process, | 128 void CheckPermissionMessageSentInternal(MockRenderProcessHost* process, |
131 int bridge_id, | 129 int bridge_id, |
132 bool allowed); | 130 bool allowed); |
133 void AddNewTab(const GURL& url); | 131 void AddNewTab(const GURL& url); |
134 void CheckTabContentsState(const GURL& requesting_frame, | 132 void CheckTabContentsState(const GURL& requesting_frame, |
135 ContentSetting expected_content_setting); | 133 ContentSetting expected_content_setting); |
136 | 134 |
137 scoped_refptr<GeolocationPermissionContext> geolocation_permission_context_; | 135 // owned by the browser context |
| 136 GeolocationPermissionContext* geolocation_permission_context_; |
138 ClosedInfoBarTracker closed_infobar_tracker_; | 137 ClosedInfoBarTracker closed_infobar_tracker_; |
139 ScopedVector<content::WebContents> extra_tabs_; | 138 ScopedVector<content::WebContents> extra_tabs_; |
140 | 139 |
141 // A map between renderer child id and a pair represending the bridge id and | 140 // A map between renderer child id and a pair represending the bridge id and |
142 // whether the requested permission was allowed. | 141 // whether the requested permission was allowed. |
143 base::hash_map<int, std::pair<int, bool> > responses_; | 142 base::hash_map<int, std::pair<int, bool> > responses_; |
144 }; | 143 }; |
145 | 144 |
146 PermissionRequestID GeolocationPermissionContextTests::RequestID( | 145 PermissionRequestID GeolocationPermissionContextTests::RequestID( |
147 int bridge_id) { | 146 int bridge_id) { |
(...skipping 11 matching lines...) Expand all Loading... |
159 extra_tabs_[tab]->GetRenderProcessHost()->GetID(), | 158 extra_tabs_[tab]->GetRenderProcessHost()->GetID(), |
160 extra_tabs_[tab]->GetRenderViewHost()->GetRoutingID(), | 159 extra_tabs_[tab]->GetRenderViewHost()->GetRoutingID(), |
161 bridge_id, | 160 bridge_id, |
162 GURL()); | 161 GURL()); |
163 } | 162 } |
164 | 163 |
165 void GeolocationPermissionContextTests::RequestGeolocationPermission( | 164 void GeolocationPermissionContextTests::RequestGeolocationPermission( |
166 content::WebContents* web_contents, | 165 content::WebContents* web_contents, |
167 const PermissionRequestID& id, | 166 const PermissionRequestID& id, |
168 const GURL& requesting_frame) { | 167 const GURL& requesting_frame) { |
169 RequestGeolocationPermission(web_contents, id, requesting_frame, NULL); | 168 geolocation_permission_context_->RequestPermission( |
170 } | 169 web_contents, id, requesting_frame, false, |
171 | |
172 void GeolocationPermissionContextTests::RequestGeolocationPermission( | |
173 content::WebContents* web_contents, | |
174 const PermissionRequestID& id, | |
175 const GURL& requesting_frame, | |
176 base::Closure* cancel_callback) { | |
177 geolocation_permission_context_->RequestGeolocationPermission( | |
178 web_contents, id.bridge_id(), requesting_frame, false, | |
179 base::Bind(&GeolocationPermissionContextTests::PermissionResponse, | 170 base::Bind(&GeolocationPermissionContextTests::PermissionResponse, |
180 base::Unretained(this), id), | 171 base::Unretained(this), id)); |
181 cancel_callback); | |
182 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | 172 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
183 base::RunLoop().RunUntilIdle(); | 173 base::RunLoop().RunUntilIdle(); |
184 } | 174 } |
185 | 175 |
186 void GeolocationPermissionContextTests::PermissionResponse( | 176 void GeolocationPermissionContextTests::PermissionResponse( |
187 const PermissionRequestID& id, | 177 const PermissionRequestID& id, |
188 bool allowed) { | 178 bool allowed) { |
189 responses_[id.render_process_id()] = std::make_pair(id.bridge_id(), allowed); | 179 responses_[id.render_process_id()] = std::make_pair(id.bridge_id(), allowed); |
190 } | 180 } |
191 | 181 |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
460 requesting_frame_0, requesting_frame_0, | 450 requesting_frame_0, requesting_frame_0, |
461 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string())); | 451 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string())); |
462 | 452 |
463 EXPECT_EQ(CONTENT_SETTING_ASK, | 453 EXPECT_EQ(CONTENT_SETTING_ASK, |
464 profile()->GetHostContentSettingsMap()->GetContentSetting( | 454 profile()->GetHostContentSettingsMap()->GetContentSetting( |
465 requesting_frame_1, requesting_frame_0, | 455 requesting_frame_1, requesting_frame_0, |
466 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string())); | 456 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string())); |
467 | 457 |
468 NavigateAndCommit(requesting_frame_0); | 458 NavigateAndCommit(requesting_frame_0); |
469 EXPECT_EQ(0U, infobar_service()->infobar_count()); | 459 EXPECT_EQ(0U, infobar_service()->infobar_count()); |
| 460 |
470 // Request permission for two frames. | 461 // Request permission for two frames. |
471 base::Closure cancel_callback; | |
472 RequestGeolocationPermission( | 462 RequestGeolocationPermission( |
473 web_contents(), RequestID(0), requesting_frame_0, &cancel_callback); | 463 web_contents(), RequestID(0), requesting_frame_0); |
474 RequestGeolocationPermission( | 464 RequestGeolocationPermission( |
475 web_contents(), RequestID(1), requesting_frame_1); | 465 web_contents(), RequestID(1), requesting_frame_1); |
476 ASSERT_EQ(1U, infobar_service()->infobar_count()); | 466 ASSERT_EQ(1U, infobar_service()->infobar_count()); |
477 | 467 |
478 infobars::InfoBar* infobar_0 = infobar_service()->infobar_at(0); | 468 infobars::InfoBar* infobar_0 = infobar_service()->infobar_at(0); |
479 ConfirmInfoBarDelegate* infobar_delegate_0 = | 469 ConfirmInfoBarDelegate* infobar_delegate_0 = |
480 infobar_0->delegate()->AsConfirmInfoBarDelegate(); | 470 infobar_0->delegate()->AsConfirmInfoBarDelegate(); |
481 ASSERT_TRUE(infobar_delegate_0); | 471 ASSERT_TRUE(infobar_delegate_0); |
482 base::string16 text_0 = infobar_delegate_0->GetMessageText(); | 472 base::string16 text_0 = infobar_delegate_0->GetMessageText(); |
483 | 473 |
484 // Simulate the frame going away, ensure the infobar for this frame | 474 // Simulate the first frame going away, ensure the infobar for this frame |
485 // is removed and the next pending infobar is created. | 475 // is removed and the next pending infobar is created. |
486 cancel_callback.Run(); | 476 geolocation_permission_context_->CancelPermissionRequest(web_contents(), |
| 477 RequestID(0)); |
487 EXPECT_EQ(1U, closed_infobar_tracker_.size()); | 478 EXPECT_EQ(1U, closed_infobar_tracker_.size()); |
488 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_0)); | 479 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_0)); |
489 closed_infobar_tracker_.Clear(); | 480 closed_infobar_tracker_.Clear(); |
490 ASSERT_EQ(1U, infobar_service()->infobar_count()); | 481 ASSERT_EQ(1U, infobar_service()->infobar_count()); |
491 | 482 |
492 infobars::InfoBar* infobar_1 = infobar_service()->infobar_at(0); | 483 infobars::InfoBar* infobar_1 = infobar_service()->infobar_at(0); |
493 ConfirmInfoBarDelegate* infobar_delegate_1 = | 484 ConfirmInfoBarDelegate* infobar_delegate_1 = |
494 infobar_1->delegate()->AsConfirmInfoBarDelegate(); | 485 infobar_1->delegate()->AsConfirmInfoBarDelegate(); |
495 ASSERT_TRUE(infobar_delegate_1); | 486 ASSERT_TRUE(infobar_delegate_1); |
496 base::string16 text_1 = infobar_delegate_1->GetMessageText(); | 487 base::string16 text_1 = infobar_delegate_1->GetMessageText(); |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
649 | 640 |
650 // Delete the tab contents. | 641 // Delete the tab contents. |
651 DeleteContents(); | 642 DeleteContents(); |
652 | 643 |
653 // During contents destruction, the infobar will have been closed, and the | 644 // During contents destruction, the infobar will have been closed, and the |
654 // pending request should have been cleared without an infobar being created. | 645 // pending request should have been cleared without an infobar being created. |
655 ASSERT_EQ(1U, closed_infobar_tracker_.size()); | 646 ASSERT_EQ(1U, closed_infobar_tracker_.size()); |
656 ASSERT_TRUE(closed_infobar_tracker_.Contains(infobar)); | 647 ASSERT_TRUE(closed_infobar_tracker_.Contains(infobar)); |
657 } | 648 } |
658 | 649 |
659 TEST_F(GeolocationPermissionContextTests, InfoBarUsesCommittedEntry) { | |
660 GURL requesting_frame_0("http://www.example.com/geolocation"); | |
661 GURL requesting_frame_1("http://www.example-2.com/geolocation"); | |
662 NavigateAndCommit(requesting_frame_0); | |
663 NavigateAndCommit(requesting_frame_1); | |
664 EXPECT_EQ(0U, infobar_service()->infobar_count()); | |
665 // Go back: navigate to a pending entry before requesting geolocation | |
666 // permission. | |
667 web_contents()->GetController().GoBack(); | |
668 // Request permission for the committed frame (not the pending one). | |
669 RequestGeolocationPermission( | |
670 web_contents(), RequestID(0), requesting_frame_1); | |
671 // Ensure the infobar is created. | |
672 ASSERT_EQ(1U, infobar_service()->infobar_count()); | |
673 infobars::InfoBarDelegate* infobar_delegate = | |
674 infobar_service()->infobar_at(0)->delegate(); | |
675 ASSERT_TRUE(infobar_delegate); | |
676 // Ensure the infobar wouldn't expire for a navigation to the committed entry. | |
677 content::LoadCommittedDetails details; | |
678 details.entry = web_contents()->GetController().GetLastCommittedEntry(); | |
679 EXPECT_FALSE(infobar_delegate->ShouldExpire( | |
680 InfoBarService::NavigationDetailsFromLoadCommittedDetails(details))); | |
681 // Ensure the infobar will expire when we commit the pending navigation. | |
682 details.entry = web_contents()->GetController().GetActiveEntry(); | |
683 EXPECT_TRUE(infobar_delegate->ShouldExpire( | |
684 InfoBarService::NavigationDetailsFromLoadCommittedDetails(details))); | |
685 | |
686 // Delete the tab contents. | |
687 DeleteContents(); | |
688 } | |
689 | |
690 TEST_F(GeolocationPermissionContextTests, LastUsageAudited) { | 650 TEST_F(GeolocationPermissionContextTests, LastUsageAudited) { |
691 GURL requesting_frame("http://www.example.com/geolocation"); | 651 GURL requesting_frame("http://www.example.com/geolocation"); |
692 NavigateAndCommit(requesting_frame); | 652 NavigateAndCommit(requesting_frame); |
693 | 653 |
694 base::SimpleTestClock* test_clock = new base::SimpleTestClock; | 654 base::SimpleTestClock* test_clock = new base::SimpleTestClock; |
695 test_clock->SetNow(base::Time::UnixEpoch() + | 655 test_clock->SetNow(base::Time::UnixEpoch() + |
696 base::TimeDelta::FromSeconds(10)); | 656 base::TimeDelta::FromSeconds(10)); |
697 | 657 |
698 HostContentSettingsMap* map = profile()->GetHostContentSettingsMap(); | 658 HostContentSettingsMap* map = profile()->GetHostContentSettingsMap(); |
699 map->SetPrefClockForTesting(scoped_ptr<base::Clock>(test_clock)); | 659 map->SetPrefClockForTesting(scoped_ptr<base::Clock>(test_clock)); |
(...skipping 15 matching lines...) Expand all Loading... |
715 CheckTabContentsState(requesting_frame, CONTENT_SETTING_ALLOW); | 675 CheckTabContentsState(requesting_frame, CONTENT_SETTING_ALLOW); |
716 CheckPermissionMessageSent(0, true); | 676 CheckPermissionMessageSent(0, true); |
717 | 677 |
718 // Permission has been used at the starting time. | 678 // Permission has been used at the starting time. |
719 EXPECT_EQ(map->GetLastUsage(requesting_frame.GetOrigin(), | 679 EXPECT_EQ(map->GetLastUsage(requesting_frame.GetOrigin(), |
720 requesting_frame.GetOrigin(), | 680 requesting_frame.GetOrigin(), |
721 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(), | 681 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(), |
722 10); | 682 10); |
723 | 683 |
724 test_clock->Advance(base::TimeDelta::FromSeconds(3)); | 684 test_clock->Advance(base::TimeDelta::FromSeconds(3)); |
| 685 |
725 RequestGeolocationPermission(web_contents(), RequestID(0), requesting_frame); | 686 RequestGeolocationPermission(web_contents(), RequestID(0), requesting_frame); |
726 | |
727 // Permission has been used three seconds later. | 687 // Permission has been used three seconds later. |
728 EXPECT_EQ(map->GetLastUsage(requesting_frame.GetOrigin(), | 688 EXPECT_EQ(map->GetLastUsage(requesting_frame.GetOrigin(), |
729 requesting_frame.GetOrigin(), | 689 requesting_frame.GetOrigin(), |
730 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(), | 690 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(), |
731 13); | 691 13); |
732 } | 692 } |
733 | 693 |
734 TEST_F(GeolocationPermissionContextTests, LastUsageAuditedMultipleFrames) { | 694 TEST_F(GeolocationPermissionContextTests, LastUsageAuditedMultipleFrames) { |
735 base::SimpleTestClock* test_clock = new base::SimpleTestClock; | 695 base::SimpleTestClock* test_clock = new base::SimpleTestClock; |
736 test_clock->SetNow(base::Time::UnixEpoch() + | 696 test_clock->SetNow(base::Time::UnixEpoch() + |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
815 // it is the embedder. | 775 // it is the embedder. |
816 EXPECT_EQ(map->GetLastUsage(requesting_frame_0.GetOrigin(), | 776 EXPECT_EQ(map->GetLastUsage(requesting_frame_0.GetOrigin(), |
817 requesting_frame_0.GetOrigin(), | 777 requesting_frame_0.GetOrigin(), |
818 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(), | 778 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(), |
819 13); | 779 13); |
820 EXPECT_EQ(map->GetLastUsage(requesting_frame_1.GetOrigin(), | 780 EXPECT_EQ(map->GetLastUsage(requesting_frame_1.GetOrigin(), |
821 requesting_frame_0.GetOrigin(), | 781 requesting_frame_0.GetOrigin(), |
822 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(), | 782 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(), |
823 11); | 783 11); |
824 } | 784 } |
OLD | NEW |