| 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/chrome_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 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/containers/hash_tables.h" | 12 #include "base/containers/hash_tables.h" |
| 13 #include "base/memory/scoped_vector.h" | 13 #include "base/memory/scoped_vector.h" |
| 14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "base/synchronization/waitable_event.h" | 15 #include "base/synchronization/waitable_event.h" |
| 16 #include "chrome/browser/chrome_notification_types.h" | 16 #include "chrome/browser/chrome_notification_types.h" |
| 17 #include "chrome/browser/content_settings/host_content_settings_map.h" | 17 #include "chrome/browser/content_settings/host_content_settings_map.h" |
| 18 #include "chrome/browser/content_settings/permission_request_id.h" | 18 #include "chrome/browser/content_settings/permission_request_id.h" |
| 19 #include "chrome/browser/content_settings/tab_specific_content_settings.h" | 19 #include "chrome/browser/content_settings/tab_specific_content_settings.h" |
| 20 #include "chrome/browser/geolocation/chrome_geolocation_permission_context_facto
ry.h" | 20 #include "chrome/browser/geolocation/geolocation_permission_context_factory.h" |
| 21 #include "chrome/browser/infobars/infobar_service.h" | 21 #include "chrome/browser/infobars/infobar_service.h" |
| 22 #include "chrome/browser/infobars/infobar_service.h" | 22 #include "chrome/browser/infobars/infobar_service.h" |
| 23 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 23 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 24 #include "chrome/test/base/testing_profile.h" | 24 #include "chrome/test/base/testing_profile.h" |
| 25 #include "components/infobars/core/confirm_infobar_delegate.h" | 25 #include "components/infobars/core/confirm_infobar_delegate.h" |
| 26 #include "components/infobars/core/infobar.h" | 26 #include "components/infobars/core/infobar.h" |
| 27 #include "content/public/browser/browser_thread.h" | 27 #include "content/public/browser/browser_thread.h" |
| 28 #include "content/public/browser/navigation_details.h" | 28 #include "content/public/browser/navigation_details.h" |
| 29 #include "content/public/browser/notification_registrar.h" | 29 #include "content/public/browser/notification_registrar.h" |
| 30 #include "content/public/browser/notification_service.h" | 30 #include "content/public/browser/notification_service.h" |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 InfoBarService* infobar_service() { | 108 InfoBarService* infobar_service() { |
| 109 return InfoBarService::FromWebContents(web_contents()); | 109 return InfoBarService::FromWebContents(web_contents()); |
| 110 } | 110 } |
| 111 InfoBarService* infobar_service_for_tab(int tab) { | 111 InfoBarService* infobar_service_for_tab(int tab) { |
| 112 return InfoBarService::FromWebContents(extra_tabs_[tab]); | 112 return InfoBarService::FromWebContents(extra_tabs_[tab]); |
| 113 } | 113 } |
| 114 | 114 |
| 115 void RequestGeolocationPermission(content::WebContents* web_contents, | 115 void RequestGeolocationPermission(content::WebContents* web_contents, |
| 116 const PermissionRequestID& id, | 116 const PermissionRequestID& id, |
| 117 const GURL& requesting_frame); | 117 const GURL& requesting_frame); |
| 118 void CancelGeolocationPermissionRequest(content::WebContents* web_contents, | 118 void RequestGeolocationPermission(content::WebContents* web_contents, |
| 119 const PermissionRequestID& id, | 119 const PermissionRequestID& id, |
| 120 const GURL& requesting_frame); | 120 const GURL& requesting_frame, |
| 121 base::Closure* cancel_callback); |
| 121 void PermissionResponse(const PermissionRequestID& id, | 122 void PermissionResponse(const PermissionRequestID& id, |
| 122 bool allowed); | 123 bool allowed); |
| 123 void CheckPermissionMessageSent(int bridge_id, bool allowed); | 124 void CheckPermissionMessageSent(int bridge_id, bool allowed); |
| 124 void CheckPermissionMessageSentForTab(int tab, int bridge_id, bool allowed); | 125 void CheckPermissionMessageSentForTab(int tab, int bridge_id, bool allowed); |
| 125 void CheckPermissionMessageSentInternal(MockRenderProcessHost* process, | 126 void CheckPermissionMessageSentInternal(MockRenderProcessHost* process, |
| 126 int bridge_id, | 127 int bridge_id, |
| 127 bool allowed); | 128 bool allowed); |
| 128 void AddNewTab(const GURL& url); | 129 void AddNewTab(const GURL& url); |
| 129 void CheckTabContentsState(const GURL& requesting_frame, | 130 void CheckTabContentsState(const GURL& requesting_frame, |
| 130 ContentSetting expected_content_setting); | 131 ContentSetting expected_content_setting); |
| 131 | 132 |
| 132 scoped_refptr<ChromeGeolocationPermissionContext> | 133 scoped_refptr<GeolocationPermissionContext> geolocation_permission_context_; |
| 133 geolocation_permission_context_; | |
| 134 ClosedInfoBarTracker closed_infobar_tracker_; | 134 ClosedInfoBarTracker closed_infobar_tracker_; |
| 135 ScopedVector<content::WebContents> extra_tabs_; | 135 ScopedVector<content::WebContents> extra_tabs_; |
| 136 | 136 |
| 137 // A map between renderer child id and a pair represending the bridge id and | 137 // A map between renderer child id and a pair represending the bridge id and |
| 138 // whether the requested permission was allowed. | 138 // whether the requested permission was allowed. |
| 139 base::hash_map<int, std::pair<int, bool> > responses_; | 139 base::hash_map<int, std::pair<int, bool> > responses_; |
| 140 }; | 140 }; |
| 141 | 141 |
| 142 PermissionRequestID GeolocationPermissionContextTests::RequestID( | 142 PermissionRequestID GeolocationPermissionContextTests::RequestID( |
| 143 int bridge_id) { | 143 int bridge_id) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 155 extra_tabs_[tab]->GetRenderProcessHost()->GetID(), | 155 extra_tabs_[tab]->GetRenderProcessHost()->GetID(), |
| 156 extra_tabs_[tab]->GetRenderViewHost()->GetRoutingID(), | 156 extra_tabs_[tab]->GetRenderViewHost()->GetRoutingID(), |
| 157 bridge_id, | 157 bridge_id, |
| 158 GURL()); | 158 GURL()); |
| 159 } | 159 } |
| 160 | 160 |
| 161 void GeolocationPermissionContextTests::RequestGeolocationPermission( | 161 void GeolocationPermissionContextTests::RequestGeolocationPermission( |
| 162 content::WebContents* web_contents, | 162 content::WebContents* web_contents, |
| 163 const PermissionRequestID& id, | 163 const PermissionRequestID& id, |
| 164 const GURL& requesting_frame) { | 164 const GURL& requesting_frame) { |
| 165 RequestGeolocationPermission(web_contents, id, requesting_frame, NULL); |
| 166 } |
| 167 |
| 168 void GeolocationPermissionContextTests::RequestGeolocationPermission( |
| 169 content::WebContents* web_contents, |
| 170 const PermissionRequestID& id, |
| 171 const GURL& requesting_frame, |
| 172 base::Closure* cancel_callback) { |
| 165 geolocation_permission_context_->RequestGeolocationPermission( | 173 geolocation_permission_context_->RequestGeolocationPermission( |
| 166 web_contents, id.bridge_id(), requesting_frame, false, | 174 web_contents, id.bridge_id(), requesting_frame, false, |
| 167 base::Bind(&GeolocationPermissionContextTests::PermissionResponse, | 175 base::Bind(&GeolocationPermissionContextTests::PermissionResponse, |
| 168 base::Unretained(this), id)); | 176 base::Unretained(this), id), |
| 177 cancel_callback); |
| 169 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | 178 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 170 base::RunLoop().RunUntilIdle(); | 179 base::RunLoop().RunUntilIdle(); |
| 171 } | 180 } |
| 172 | 181 |
| 173 void GeolocationPermissionContextTests::CancelGeolocationPermissionRequest( | |
| 174 content::WebContents* web_contents, | |
| 175 const PermissionRequestID& id, | |
| 176 const GURL& requesting_frame) { | |
| 177 geolocation_permission_context_->CancelGeolocationPermissionRequest( | |
| 178 web_contents, id.bridge_id(), requesting_frame); | |
| 179 } | |
| 180 | |
| 181 void GeolocationPermissionContextTests::PermissionResponse( | 182 void GeolocationPermissionContextTests::PermissionResponse( |
| 182 const PermissionRequestID& id, | 183 const PermissionRequestID& id, |
| 183 bool allowed) { | 184 bool allowed) { |
| 184 responses_[id.render_process_id()] = std::make_pair(id.bridge_id(), allowed); | 185 responses_[id.render_process_id()] = std::make_pair(id.bridge_id(), allowed); |
| 185 } | 186 } |
| 186 | 187 |
| 187 void GeolocationPermissionContextTests::CheckPermissionMessageSent( | 188 void GeolocationPermissionContextTests::CheckPermissionMessageSent( |
| 188 int bridge_id, | 189 int bridge_id, |
| 189 bool allowed) { | 190 bool allowed) { |
| 190 CheckPermissionMessageSentInternal(process(), bridge_id, allowed); | 191 CheckPermissionMessageSentInternal(process(), bridge_id, allowed); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 ChromeRenderViewHostTestHarness::SetUp(); | 245 ChromeRenderViewHostTestHarness::SetUp(); |
| 245 | 246 |
| 246 // Set up required helpers, and make this be as "tabby" as the code requires. | 247 // Set up required helpers, and make this be as "tabby" as the code requires. |
| 247 extensions::SetViewType(web_contents(), extensions::VIEW_TYPE_TAB_CONTENTS); | 248 extensions::SetViewType(web_contents(), extensions::VIEW_TYPE_TAB_CONTENTS); |
| 248 InfoBarService::CreateForWebContents(web_contents()); | 249 InfoBarService::CreateForWebContents(web_contents()); |
| 249 TabSpecificContentSettings::CreateForWebContents(web_contents()); | 250 TabSpecificContentSettings::CreateForWebContents(web_contents()); |
| 250 #if defined(OS_ANDROID) | 251 #if defined(OS_ANDROID) |
| 251 MockGoogleLocationSettingsHelper::SetLocationStatus(true, true); | 252 MockGoogleLocationSettingsHelper::SetLocationStatus(true, true); |
| 252 #endif | 253 #endif |
| 253 geolocation_permission_context_ = | 254 geolocation_permission_context_ = |
| 254 ChromeGeolocationPermissionContextFactory::GetForProfile(profile()); | 255 GeolocationPermissionContextFactory::GetForProfile(profile()); |
| 255 } | 256 } |
| 256 | 257 |
| 257 void GeolocationPermissionContextTests::TearDown() { | 258 void GeolocationPermissionContextTests::TearDown() { |
| 258 extra_tabs_.clear(); | 259 extra_tabs_.clear(); |
| 259 ChromeRenderViewHostTestHarness::TearDown(); | 260 ChromeRenderViewHostTestHarness::TearDown(); |
| 260 } | 261 } |
| 261 | 262 |
| 262 // Tests ---------------------------------------------------------------------- | 263 // Tests ---------------------------------------------------------------------- |
| 263 | 264 |
| 264 TEST_F(GeolocationPermissionContextTests, SinglePermission) { | 265 TEST_F(GeolocationPermissionContextTests, SinglePermission) { |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 470 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string())); | 471 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string())); |
| 471 | 472 |
| 472 EXPECT_EQ(CONTENT_SETTING_ASK, | 473 EXPECT_EQ(CONTENT_SETTING_ASK, |
| 473 profile()->GetHostContentSettingsMap()->GetContentSetting( | 474 profile()->GetHostContentSettingsMap()->GetContentSetting( |
| 474 requesting_frame_1, requesting_frame_0, | 475 requesting_frame_1, requesting_frame_0, |
| 475 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string())); | 476 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string())); |
| 476 | 477 |
| 477 NavigateAndCommit(requesting_frame_0); | 478 NavigateAndCommit(requesting_frame_0); |
| 478 EXPECT_EQ(0U, infobar_service()->infobar_count()); | 479 EXPECT_EQ(0U, infobar_service()->infobar_count()); |
| 479 // Request permission for two frames. | 480 // Request permission for two frames. |
| 481 base::Closure cancel_callback; |
| 480 RequestGeolocationPermission( | 482 RequestGeolocationPermission( |
| 481 web_contents(), RequestID(0), requesting_frame_0); | 483 web_contents(), RequestID(0), requesting_frame_0, &cancel_callback); |
| 482 RequestGeolocationPermission( | 484 RequestGeolocationPermission( |
| 483 web_contents(), RequestID(1), requesting_frame_1); | 485 web_contents(), RequestID(1), requesting_frame_1); |
| 484 ASSERT_EQ(1U, infobar_service()->infobar_count()); | 486 ASSERT_EQ(1U, infobar_service()->infobar_count()); |
| 485 | 487 |
| 486 infobars::InfoBar* infobar_0 = infobar_service()->infobar_at(0); | 488 infobars::InfoBar* infobar_0 = infobar_service()->infobar_at(0); |
| 487 ConfirmInfoBarDelegate* infobar_delegate_0 = | 489 ConfirmInfoBarDelegate* infobar_delegate_0 = |
| 488 infobar_0->delegate()->AsConfirmInfoBarDelegate(); | 490 infobar_0->delegate()->AsConfirmInfoBarDelegate(); |
| 489 ASSERT_TRUE(infobar_delegate_0); | 491 ASSERT_TRUE(infobar_delegate_0); |
| 490 base::string16 text_0 = infobar_delegate_0->GetMessageText(); | 492 base::string16 text_0 = infobar_delegate_0->GetMessageText(); |
| 491 | 493 |
| 492 // Simulate the frame going away, ensure the infobar for this frame | 494 // Simulate the frame going away, ensure the infobar for this frame |
| 493 // is removed and the next pending infobar is created. | 495 // is removed and the next pending infobar is created. |
| 494 CancelGeolocationPermissionRequest( | 496 cancel_callback.Run(); |
| 495 web_contents(), RequestID(0), requesting_frame_0); | |
| 496 EXPECT_EQ(1U, closed_infobar_tracker_.size()); | 497 EXPECT_EQ(1U, closed_infobar_tracker_.size()); |
| 497 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_0)); | 498 EXPECT_TRUE(closed_infobar_tracker_.Contains(infobar_0)); |
| 498 closed_infobar_tracker_.Clear(); | 499 closed_infobar_tracker_.Clear(); |
| 499 ASSERT_EQ(1U, infobar_service()->infobar_count()); | 500 ASSERT_EQ(1U, infobar_service()->infobar_count()); |
| 500 | 501 |
| 501 infobars::InfoBar* infobar_1 = infobar_service()->infobar_at(0); | 502 infobars::InfoBar* infobar_1 = infobar_service()->infobar_at(0); |
| 502 ConfirmInfoBarDelegate* infobar_delegate_1 = | 503 ConfirmInfoBarDelegate* infobar_delegate_1 = |
| 503 infobar_1->delegate()->AsConfirmInfoBarDelegate(); | 504 infobar_1->delegate()->AsConfirmInfoBarDelegate(); |
| 504 ASSERT_TRUE(infobar_delegate_1); | 505 ASSERT_TRUE(infobar_delegate_1); |
| 505 base::string16 text_1 = infobar_delegate_1->GetMessageText(); | 506 base::string16 text_1 = infobar_delegate_1->GetMessageText(); |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 688 EXPECT_FALSE(infobar_delegate->ShouldExpire( | 689 EXPECT_FALSE(infobar_delegate->ShouldExpire( |
| 689 InfoBarService::NavigationDetailsFromLoadCommittedDetails(details))); | 690 InfoBarService::NavigationDetailsFromLoadCommittedDetails(details))); |
| 690 // Ensure the infobar will expire when we commit the pending navigation. | 691 // Ensure the infobar will expire when we commit the pending navigation. |
| 691 details.entry = web_contents()->GetController().GetActiveEntry(); | 692 details.entry = web_contents()->GetController().GetActiveEntry(); |
| 692 EXPECT_TRUE(infobar_delegate->ShouldExpire( | 693 EXPECT_TRUE(infobar_delegate->ShouldExpire( |
| 693 InfoBarService::NavigationDetailsFromLoadCommittedDetails(details))); | 694 InfoBarService::NavigationDetailsFromLoadCommittedDetails(details))); |
| 694 | 695 |
| 695 // Delete the tab contents. | 696 // Delete the tab contents. |
| 696 DeleteContents(); | 697 DeleteContents(); |
| 697 } | 698 } |
| OLD | NEW |