diff options
author | joth@chromium.org <joth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-02-02 18:28:08 +0000 |
---|---|---|
committer | joth@chromium.org <joth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-02-02 18:28:08 +0000 |
commit | 9732c032ed9b6664149f03472475d31844a1c8ba (patch) | |
tree | a19ee04741c6fe7f5fd0d2f84bd7bd335db9009b /chrome/browser/geolocation/location_provider.h | |
parent | 393b7525cea33eb4030758181a01d6f9c9b7ae82 (diff) | |
download | chromium_src-9732c032ed9b6664149f03472475d31844a1c8ba.zip chromium_src-9732c032ed9b6664149f03472475d31844a1c8ba.tar.gz chromium_src-9732c032ed9b6664149f03472475d31844a1c8ba.tar.bz2 |
Port the gears geolocation network provider to Chromium
BUG=http://crbug.com/11246
TEST=See http://codereview.chromium.org/556106
Review URL: http://codereview.chromium.org/552250
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@37850 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/geolocation/location_provider.h')
-rw-r--r-- | chrome/browser/geolocation/location_provider.h | 151 |
1 files changed, 85 insertions, 66 deletions
diff --git a/chrome/browser/geolocation/location_provider.h b/chrome/browser/geolocation/location_provider.h index 5383dd8..762ccb1 100644 --- a/chrome/browser/geolocation/location_provider.h +++ b/chrome/browser/geolocation/location_provider.h @@ -1,28 +1,7 @@ -// Copyright 2008, Google Inc. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. -// 2. Redistributions in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// 3. Neither the name of Google Inc. nor the names of its contributors may be -// used to endorse or promote products derived from this software without -// specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED -// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO -// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; -// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR -// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF -// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// +// 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. + // A location provider provides position information from a particular source // (GPS, network etc). The GearsGeolocation object uses a set of location // providers to obtain a position fix. @@ -31,95 +10,135 @@ // Primarily, this class declares interface methods to be implemented by derived // classes. -#ifndef GEARS_GEOLOCATION_LOCATION_PROVIDER_H__ -#define GEARS_GEOLOCATION_LOCATION_PROVIDER_H__ - -// TODO(joth): port to chromium -#if 0 +#ifndef CHROME_BROWSER_GEOLOCATION_LOCATION_PROVIDER_H_ +#define CHROME_BROWSER_GEOLOCATION_LOCATION_PROVIDER_H_ #include <map> -#include "gears/base/common/base_class.h" -#include "gears/base/common/mutex.h" -#include "gears/base/common/string16.h" +#include "base/lock.h" +#include "base/message_loop.h" +#include "base/ref_counted.h" +#include "base/string16.h" +#include "base/task.h" +class GURL; struct Position; -class RefCount; +class URLRequestContextGetter; // The base class used by all location providers. -class LocationProviderBase { +class LocationProviderBase + : public base::RefCountedThreadSafe<LocationProviderBase> { public: + // Provides storage for the access token used in the network request. + // Normally the client (i.e. geolocation controller) implements this, but + // also allows mocking for testing. + class AccessTokenStore { + public: + virtual bool SetAccessToken(const GURL& url, + const string16& access_token) = 0; + virtual bool GetAccessToken(const GURL& url, string16* access_token) = 0; + + protected: + virtual ~AccessTokenStore() {} + }; + + // Clients of the location provider must implement this interface. All call- + // backs to this interface will happen in the context of the thread on which + // the location provider was created. class ListenerInterface { public: // Used to inform listener that a new position fix is available or that a // fatal error has occurred. Providers should call this for new listeners // as soon as a position is available. - virtual bool LocationUpdateAvailable(LocationProviderBase *provider) = 0; + virtual bool LocationUpdateAvailable(LocationProviderBase* provider) = 0; // Used to inform listener that movement has been detected. If obtaining the // position succeeds, this will be followed by a call to // LocationUpdateAvailable. Some providers may not be able to detect // movement before a new fix is obtained, so will never call this method. // Note that this is not called in response to registration of a new // listener. - virtual bool MovementDetected(LocationProviderBase *provider) = 0; + virtual bool MovementDetected(LocationProviderBase* provider) = 0; + + protected: virtual ~ListenerInterface() {} }; - virtual ~LocationProviderBase() {} - + // TODO(joth): Make register / unregister non-virtual. // Registers a listener, which will be called back on // ListenerInterface::LocationUpdateAvailable as soon as a position is // available and again whenever a new position is available. Ref counts the // listener to handle multiple calls to this method. - virtual void RegisterListener(ListenerInterface *listener, - bool request_address); + virtual void RegisterListener(ListenerInterface* listener); // Unregisters a listener. Unrefs the listener to handle multiple calls to // this method. Once the ref count reaches zero, the listener is removed and // once this method returns, no further calls to // ListenerInterface::LocationUpdateAvailable will be made for this listener. // It may block if a callback is in progress. - virtual void UnregisterListener(ListenerInterface *listener); + virtual void UnregisterListener(ListenerInterface* listener); // Interface methods + // Returns false if the provider failed to start. + virtual bool StartProvider() = 0; // Gets the current best position estimate. - virtual void GetPosition(Position *position) = 0; + virtual void GetPosition(Position* position) = 0; // Provides a hint to the provider that new location data is needed as soon // as possible. Default implementation does nothing. virtual void UpdatePosition() {} - // Accessor methods. - typedef std::pair<bool, RefCount*> ListenerPair; - typedef std::map<ListenerInterface*, ListenerPair> ListenerMap; - ListenerMap *GetListeners(); - Mutex *GetListenersMutex(); - protected: + // Instances should only be destroyed via the thread safe ref count; derived + // classes should not have a public destructor. + friend class base::RefCountedThreadSafe<LocationProviderBase>; + LocationProviderBase(); + virtual ~LocationProviderBase(); + // Inform listeners that a new position or error is available, using // LocationUpdateAvailable. virtual void UpdateListeners(); // Inform listeners that movement has been detected, using MovementDetected. virtual void InformListenersOfMovement(); + MessageLoop* client_loop() { return client_loop_; } + void CheckRunningInClientLoop(); + private: + // Utility methods to delegate method calls into the client thread + template <class Method> + bool RunInClientThread(const tracked_objects::Location& from_here, + Method method) { + if (MessageLoop::current() == client_loop_) { + return false; + } + client_loop_->PostTask(from_here, NewRunnableMethod(this, method)); + return true; + } + template <class Method, class A> + bool RunInClientThread(const tracked_objects::Location& from_here, + Method method, const A& a) { + if (MessageLoop::current() == client_loop_) { + return false; + } + client_loop_->PostTask(from_here, NewRunnableMethod(this, method, a)); + return true; + } + // The listeners registered to this provider. For each listener, we store a - // ref count and whether it requires an address. + // ref count. + typedef std::map<ListenerInterface*, int> ListenerMap; ListenerMap listeners_; - Mutex listeners_mutex_; + + // Reference to the client's message loop, all callbacks and access to + // the listeners_ member should happen in this context. + MessageLoop* client_loop_; }; // Factory functions for the various types of location provider to abstract over // the platform-dependent implementations. -LocationProviderBase *NewMockLocationProvider(); -LocationProviderBase *NewGpsLocationProvider( - BrowsingContext *browsing_context, - const std::string16 &reverse_geocode_url, - const std::string16 &host_name, - const std::string16 &address_language); -LocationProviderBase *NewNetworkLocationProvider( - BrowsingContext *browsing_context, - const std::string16 &url, - const std::string16 &host_name, - const std::string16 &language); - -#endif // if 0 - -#endif // GEARS_GEOLOCATION_LOCATION_PROVIDER_H__ +LocationProviderBase* NewMockLocationProvider(); +LocationProviderBase* NewGpsLocationProvider(); +LocationProviderBase* NewNetworkLocationProvider( + LocationProviderBase::AccessTokenStore* access_token_store, + URLRequestContextGetter* context, + const GURL& url, + const string16& host_name); + +#endif // CHROME_BROWSER_GEOLOCATION_LOCATION_PROVIDER_H_ |