diff options
Diffstat (limited to 'chrome')
13 files changed, 201 insertions, 67 deletions
diff --git a/chrome/browser/extensions/extension_geolocation_apitest.cc b/chrome/browser/extensions/extension_geolocation_apitest.cc new file mode 100644 index 0000000..68ce380 --- /dev/null +++ b/chrome/browser/extensions/extension_geolocation_apitest.cc @@ -0,0 +1,23 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/extensions/extension_apitest.h" +#include "chrome/browser/geolocation/location_arbitrator.h" +#include "chrome/browser/geolocation/mock_location_provider.h" +#include "chrome/common/chrome_switches.h" + +class GeolocationApiTest : public ExtensionApiTest { +public: + void SetUpCommandLine(CommandLine* command_line) { + ExtensionApiTest::SetUpCommandLine(command_line); + command_line->AppendSwitch(switches::kEnableGeolocation); + GeolocationArbitrator::SetProviderFactoryForTest( + &NewAutoSuccessMockLocationProvider); + } +}; + +IN_PROC_BROWSER_TEST_F(GeolocationApiTest, ExtensionGeolocationAccessFail) { + // Test that geolocation cannot be accessed from extension. + ASSERT_TRUE(RunExtensionTest("geolocation")) << message_; +} diff --git a/chrome/browser/geolocation/geolocation_browsertest.cc b/chrome/browser/geolocation/geolocation_browsertest.cc index 1957893..3694e22 100644 --- a/chrome/browser/geolocation/geolocation_browsertest.cc +++ b/chrome/browser/geolocation/geolocation_browsertest.cc @@ -7,6 +7,7 @@ #include "chrome/browser/browser.h" #include "chrome/browser/browser_list.h" #include "chrome/browser/geolocation/location_arbitrator.h" +#include "chrome/browser/geolocation/mock_location_provider.h" #include "chrome/browser/profile.h" #include "chrome/browser/renderer_host/render_view_host.h" #include "chrome/browser/tab_contents/tab_contents.h" @@ -70,7 +71,7 @@ class GeolocationBrowserTest : public InProcessBrowserTest { }; void Initialize(InitializationOptions options) { - GeolocationArbitrator::SetUseMockProvider(true); + GeolocationArbitrator::SetProviderFactoryForTest(&NewMockLocationProvider); if (!server_.get()) { server_ = StartHTTPServer(); } @@ -175,7 +176,7 @@ class GeolocationBrowserTest : public InProcessBrowserTest { command_line->AppendSwitch(switches::kEnableGeolocation); } virtual void TearDownInProcessBrowserTestFixture() { - GeolocationArbitrator::SetUseMockProvider(false); + GeolocationArbitrator::SetProviderFactoryForTest(NULL); } scoped_refptr<HTTPTestServer> server_; diff --git a/chrome/browser/geolocation/geolocation_permission_context.cc b/chrome/browser/geolocation/geolocation_permission_context.cc index be84486..f9fa9e2 100644 --- a/chrome/browser/geolocation/geolocation_permission_context.cc +++ b/chrome/browser/geolocation/geolocation_permission_context.cc @@ -214,7 +214,14 @@ void GeolocationPermissionContext::RequestPermissionFromUI( TabContents* tab_contents = tab_util::GetTabContentsByID(render_process_id, render_view_id); - DCHECK(tab_contents); + if (!tab_contents) { + // The tab may have gone away, or the request may not be from a tab at all. + LOG(WARNING) << "Attempt to use geolocaiton tabless renderer: " + << render_process_id << "," << render_view_id << "," << bridge_id + << " (geolocation is not supported in extensions)"; + NotifyPermissionSet(render_process_id, render_view_id, bridge_id, false); + return; + } tab_contents->AddInfoBar( new GeolocationConfirmInfoBarDelegate( tab_contents, this, render_process_id, render_view_id, diff --git a/chrome/browser/geolocation/location_arbitrator.cc b/chrome/browser/geolocation/location_arbitrator.cc index 7529dd2..4b42dd7 100644 --- a/chrome/browser/geolocation/location_arbitrator.cc +++ b/chrome/browser/geolocation/location_arbitrator.cc @@ -20,8 +20,9 @@ namespace { const char* kDefaultNetworkProviderUrl = "https://www.google.com/loc/json"; -bool g_use_mock_provider = false; -static GeolocationArbitrator* g_instance_ = NULL; +GeolocationArbitrator* g_instance_ = NULL; +GeolocationArbitrator::LocationProviderFactoryFunction + g_provider_factory_function_for_test = NULL; } // namespace class GeolocationArbitratorImpl @@ -132,8 +133,8 @@ void GeolocationArbitratorImpl::OnAccessTokenStoresLoaded( DCHECK(provider_ == NULL) << "OnAccessTokenStoresLoaded : has existing location " << "provider. Race condition caused repeat load of tokens?"; - if (g_use_mock_provider) { - provider_.reset(NewMockLocationProvider()); + if (g_provider_factory_function_for_test) { + provider_.reset(g_provider_factory_function_for_test()); } else { // TODO(joth): Once we have arbitration implementation, iterate the whole // set rather than cherry-pick our defaul url. @@ -195,8 +196,9 @@ GeolocationArbitrator* GeolocationArbitrator::GetInstance() { return g_instance_; } -void GeolocationArbitrator::SetUseMockProvider(bool use_mock) { - g_use_mock_provider = use_mock; +void GeolocationArbitrator::SetProviderFactoryForTest( + LocationProviderFactoryFunction factory_function) { + g_provider_factory_function_for_test = factory_function; } GeolocationArbitrator::GeolocationArbitrator() { diff --git a/chrome/browser/geolocation/location_arbitrator.h b/chrome/browser/geolocation/location_arbitrator.h index 96e1de2..98477c7 100644 --- a/chrome/browser/geolocation/location_arbitrator.h +++ b/chrome/browser/geolocation/location_arbitrator.h @@ -8,6 +8,7 @@ #include "base/ref_counted.h" class AccessTokenStore; +class LocationProviderBase; class URLRequestContextGetter; struct Geoposition; @@ -62,9 +63,11 @@ class GeolocationArbitrator : public base::RefCounted<GeolocationArbitrator> { // via AddObserver(). Returns true if the observer was removed. virtual bool RemoveObserver(Delegate* delegate) = 0; - // TODO(joth): This is a stop-gap for testing; once we have decoupled - // provider factory we should extract mock creation from the arbitrator. - static void SetUseMockProvider(bool use_mock); + // For testing, a factory functino can be set which will be used to create + // a specified test provider. Pass NULL to reset to the default behavior. + typedef LocationProviderBase* (*LocationProviderFactoryFunction)(void); + static void SetProviderFactoryForTest( + LocationProviderFactoryFunction factory_function); protected: friend class base::RefCounted<GeolocationArbitrator>; diff --git a/chrome/browser/geolocation/location_arbitrator_unittest.cc b/chrome/browser/geolocation/location_arbitrator_unittest.cc index dfa08c3..a385f13 100644 --- a/chrome/browser/geolocation/location_arbitrator_unittest.cc +++ b/chrome/browser/geolocation/location_arbitrator_unittest.cc @@ -7,6 +7,7 @@ #include "base/scoped_ptr.h" #include "chrome/browser/geolocation/fake_access_token_store.h" #include "chrome/browser/geolocation/location_provider.h" +#include "chrome/browser/geolocation/mock_location_provider.h" #include "chrome/common/geoposition.h" #include "testing/gtest/include/gtest/gtest.h" @@ -42,7 +43,7 @@ class GeolocationLocationArbitratorTest : public testing::Test { protected: virtual void SetUp() { access_token_store_ = new FakeAccessTokenStore; - GeolocationArbitrator::SetUseMockProvider(true); + GeolocationArbitrator::SetProviderFactoryForTest(&NewMockLocationProvider); arbitrator_ = GeolocationArbitrator::Create(access_token_store_.get(), NULL); } diff --git a/chrome/browser/geolocation/location_provider.cc b/chrome/browser/geolocation/location_provider.cc index 72eaa09..c8a9f67 100644 --- a/chrome/browser/geolocation/location_provider.cc +++ b/chrome/browser/geolocation/location_provider.cc @@ -2,12 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -// This file implements a mock location provider and the factory functions for -// creating various types of location provider. - #include "chrome/browser/geolocation/location_provider.h" -#include <assert.h> #include "base/logging.h" LocationProviderBase::LocationProviderBase() { @@ -73,29 +69,3 @@ void LocationProviderBase::InformListenersOfMovement() { LocationProviderBase* NewGpsLocationProvider() { return NULL; } - -MockLocationProvider::MockLocationProvider() : started_count_(0) { - CHECK(instance_ == NULL); - instance_ = this; -} - -MockLocationProvider::~MockLocationProvider() { - CHECK(instance_ == this); - instance_ = NULL; -} - -bool MockLocationProvider::StartProvider() { - ++started_count_; - return true; -} - -void MockLocationProvider::GetPosition(Geoposition *position) { - *position = position_; -} - -MockLocationProvider* MockLocationProvider::instance_ = NULL; - -LocationProviderBase* NewMockLocationProvider() { - return new MockLocationProvider; -} - diff --git a/chrome/browser/geolocation/location_provider.h b/chrome/browser/geolocation/location_provider.h index 4d0c9b1..8ac1c8f 100644 --- a/chrome/browser/geolocation/location_provider.h +++ b/chrome/browser/geolocation/location_provider.h @@ -87,32 +87,8 @@ class LocationProviderBase : public NonThreadSafe { ListenerMap listeners_; }; -// Mock implementation of a location provider for testing. -// TODO(joth): Move this and the implementation of this back in the unit_tests -// once the location_arbitrator mock/factory situation is resolved. -class MockLocationProvider : public LocationProviderBase { - public: - MockLocationProvider(); - virtual ~MockLocationProvider(); - - using LocationProviderBase::UpdateListeners; - using LocationProviderBase::InformListenersOfMovement; - - // LocationProviderBase implementation. - virtual bool StartProvider(); - virtual void GetPosition(Geoposition *position); - - Geoposition position_; - int started_count_; - - static MockLocationProvider* instance_; - - DISALLOW_COPY_AND_ASSIGN(MockLocationProvider); -}; - // Factory functions for the various types of location provider to abstract over // the platform-dependent implementations. -LocationProviderBase* NewMockLocationProvider(); LocationProviderBase* NewGpsLocationProvider(); LocationProviderBase* NewNetworkLocationProvider( AccessTokenStore* access_token_store, diff --git a/chrome/browser/geolocation/mock_location_provider.cc b/chrome/browser/geolocation/mock_location_provider.cc new file mode 100644 index 0000000..4c817cb --- /dev/null +++ b/chrome/browser/geolocation/mock_location_provider.cc @@ -0,0 +1,73 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// This file implements a mock location provider and the factory functions for +// various ways of creating it. + +#include "chrome/browser/geolocation/mock_location_provider.h" + +#include "base/compiler_specific.h" +#include "base/logging.h" +#include "base/message_loop.h" +#include "base/task.h" + +MockLocationProvider* MockLocationProvider::instance_ = NULL; + +MockLocationProvider::MockLocationProvider() + : started_count_(0) { + CHECK(instance_ == NULL); + instance_ = this; +} + +MockLocationProvider::~MockLocationProvider() { + CHECK(instance_ == this); + instance_ = NULL; +} + +bool MockLocationProvider::StartProvider() { + ++started_count_; + return true; +} + +void MockLocationProvider::GetPosition(Geoposition* position) { + *position = position_; +} + +// Mock location provider that automatically calls back it's client when +// StartProvider is called. +class AutoMockLocationProvider : public MockLocationProvider { + public: + explicit AutoMockLocationProvider(bool has_valid_location) + : ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)) { + if (has_valid_location) { + position_.accuracy = 3; + position_.latitude = 4.3; + position_.longitude = -7.8; + position_.timestamp = 4567; + } else { + position_.error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; + } + } + virtual bool StartProvider() { + MockLocationProvider::StartProvider(); + MessageLoop::current()->PostTask( + FROM_HERE, task_factory_.NewRunnableMethod( + &MockLocationProvider::UpdateListeners)); + return true; + } + + ScopedRunnableMethodFactory<MockLocationProvider> task_factory_; +}; + +LocationProviderBase* NewMockLocationProvider() { + return new MockLocationProvider; +} + +LocationProviderBase* NewAutoSuccessMockLocationProvider() { + return new AutoMockLocationProvider(true); +} + +LocationProviderBase* NewAutoFailMockLocationProvider() { + return new AutoMockLocationProvider(false); +} diff --git a/chrome/browser/geolocation/mock_location_provider.h b/chrome/browser/geolocation/mock_location_provider.h new file mode 100644 index 0000000..b690416 --- /dev/null +++ b/chrome/browser/geolocation/mock_location_provider.h @@ -0,0 +1,46 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_GEOLOCATION_MOCK_LOCATION_PROVIDER_H_ +#define CHROME_BROWSER_GEOLOCATION_MOCK_LOCATION_PROVIDER_H_ + +#include "chrome/browser/geolocation/location_provider.h" + +// Mock implementation of a location provider for testing. +class MockLocationProvider : public LocationProviderBase { + public: + MockLocationProvider(); + virtual ~MockLocationProvider(); + + using LocationProviderBase::UpdateListeners; + using LocationProviderBase::InformListenersOfMovement; + + // LocationProviderBase implementation. + virtual bool StartProvider(); + virtual void GetPosition(Geoposition *position); + + Geoposition position_; + int started_count_; + + // Set when an instance of the mock is created via a factory function. + static MockLocationProvider* instance_; + + DISALLOW_COPY_AND_ASSIGN(MockLocationProvider); +}; + +// Factory functions for the various sorts of mock location providers, +// for use with GeolocationArbitrator::SetProviderFactoryForTest (i.e. +// not intended for test code to use to get access to the mock, you can use +// MockLocationProvider::instance_ for this, or make a custom facotry method). + +// Creates a mock location provider with no default behavior. +LocationProviderBase* NewMockLocationProvider(); +// Creates a mock location provider that automatically notifies its +// listeners with a valid location when StartProvider is called. +LocationProviderBase* NewAutoSuccessMockLocationProvider(); +// Creates a mock location provider that automatically notifies its +// listeners with an error when StartProvider is called. +LocationProviderBase* NewAutoFailMockLocationProvider(); + +#endif // CHROME_BROWSER_GEOLOCATION_MOCK_LOCATION_PROVIDER_H_ diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index b2d84f5..537ab5c 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -57,6 +57,8 @@ # The only thing used from browser is Browser::Type. 'browser/browser.h', 'browser/cocoa/browser_test_helper.h', + 'browser/geolocation/mock_location_provider.cc', + 'browser/geolocation/mock_location_provider.h', 'browser/mock_browsing_data_appcache_helper.cc', 'browser/mock_browsing_data_appcache_helper.h', 'browser/mock_browsing_data_database_helper.cc', @@ -1169,6 +1171,7 @@ 'browser/extensions/extension_browsertest.h', 'browser/extensions/extension_browsertests_misc.cc', 'browser/extensions/extension_crash_recovery_browsertest.cc', + 'browser/extensions/extension_geolocation_apitest.cc', 'browser/extensions/extension_history_apitest.cc', 'browser/extensions/extension_i18n_apitest.cc', 'browser/extensions/extension_incognito_apitest.cc', diff --git a/chrome/test/data/extensions/api_test/geolocation/background.html b/chrome/test/data/extensions/api_test/geolocation/background.html new file mode 100644 index 0000000..553fec5 --- /dev/null +++ b/chrome/test/data/extensions/api_test/geolocation/background.html @@ -0,0 +1,22 @@ +<script> + // These API calls should fail since geolocation is not allowed from + // extensions. + chrome.test.runTests([ + function geolocation_getCurrentPosition() { + try { + navigator.geolocation.getCurrentPosition(chrome.test.fail, + chrome.test.succeed); + } catch (e) { + chrome.test.fail(); + } + }, + function geolocation_watchPosition() { + try { + navigator.geolocation.watchPosition(chrome.test.fail, + chrome.test.succeed); + } catch (e) { + chrome.test.fail(); + } + } +]); +</script>
\ No newline at end of file diff --git a/chrome/test/data/extensions/api_test/geolocation/manifest.json b/chrome/test/data/extensions/api_test/geolocation/manifest.json new file mode 100644 index 0000000..3c7ff3f --- /dev/null +++ b/chrome/test/data/extensions/api_test/geolocation/manifest.json @@ -0,0 +1,7 @@ +{ + "name": "geolocation access apitest", + "description": "tests geolocation is not accessible from extensions", + "version": "0.1", + "background_page": "background.html", + "permissions": [ ] +} |