| OLD | NEW |
| 1 // Copyright 2008, Google Inc. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // Redistribution and use in source and binary forms, with or without | 3 // found in the LICENSE file. |
| 4 // modification, are permitted provided that the following conditions are met: | 4 |
| 5 // | |
| 6 // 1. Redistributions of source code must retain the above copyright notice, | |
| 7 // this list of conditions and the following disclaimer. | |
| 8 // 2. Redistributions in binary form must reproduce the above copyright notice, | |
| 9 // this list of conditions and the following disclaimer in the documentation | |
| 10 // and/or other materials provided with the distribution. | |
| 11 // 3. Neither the name of Google Inc. nor the names of its contributors may be | |
| 12 // used to endorse or promote products derived from this software without | |
| 13 // specific prior written permission. | |
| 14 // | |
| 15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED | |
| 16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
| 17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | |
| 18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | |
| 21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
| 22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
| 23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
| 24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 25 // | |
| 26 // A location provider provides position information from a particular source | 5 // A location provider provides position information from a particular source |
| 27 // (GPS, network etc). The GearsGeolocation object uses a set of location | 6 // (GPS, network etc). The GearsGeolocation object uses a set of location |
| 28 // providers to obtain a position fix. | 7 // providers to obtain a position fix. |
| 29 // | 8 // |
| 30 // This file declares a base class to be used by all location providers. | 9 // This file declares a base class to be used by all location providers. |
| 31 // Primarily, this class declares interface methods to be implemented by derived | 10 // Primarily, this class declares interface methods to be implemented by derived |
| 32 // classes. | 11 // classes. |
| 33 | 12 |
| 34 #ifndef GEARS_GEOLOCATION_LOCATION_PROVIDER_H__ | 13 #ifndef CHROME_BROWSER_GEOLOCATION_LOCATION_PROVIDER_H_ |
| 35 #define GEARS_GEOLOCATION_LOCATION_PROVIDER_H__ | 14 #define CHROME_BROWSER_GEOLOCATION_LOCATION_PROVIDER_H_ |
| 36 | |
| 37 // TODO(joth): port to chromium | |
| 38 #if 0 | |
| 39 | 15 |
| 40 #include <map> | 16 #include <map> |
| 41 #include "gears/base/common/base_class.h" | 17 #include "base/lock.h" |
| 42 #include "gears/base/common/mutex.h" | 18 #include "base/message_loop.h" |
| 43 #include "gears/base/common/string16.h" | 19 #include "base/ref_counted.h" |
| 20 #include "base/string16.h" |
| 21 #include "base/task.h" |
| 44 | 22 |
| 23 class GURL; |
| 45 struct Position; | 24 struct Position; |
| 46 class RefCount; | 25 class URLRequestContextGetter; |
| 47 | 26 |
| 48 // The base class used by all location providers. | 27 // The base class used by all location providers. |
| 49 class LocationProviderBase { | 28 class LocationProviderBase |
| 29 : public base::RefCountedThreadSafe<LocationProviderBase> { |
| 50 public: | 30 public: |
| 31 // Provides storage for the access token used in the network request. |
| 32 // Normally the client (i.e. geolocation controller) implements this, but |
| 33 // also allows mocking for testing. |
| 34 class AccessTokenStore { |
| 35 public: |
| 36 virtual bool SetAccessToken(const GURL& url, |
| 37 const string16& access_token) = 0; |
| 38 virtual bool GetAccessToken(const GURL& url, string16* access_token) = 0; |
| 39 |
| 40 protected: |
| 41 virtual ~AccessTokenStore() {} |
| 42 }; |
| 43 |
| 44 // Clients of the location provider must implement this interface. All call- |
| 45 // backs to this interface will happen in the context of the thread on which |
| 46 // the location provider was created. |
| 51 class ListenerInterface { | 47 class ListenerInterface { |
| 52 public: | 48 public: |
| 53 // Used to inform listener that a new position fix is available or that a | 49 // Used to inform listener that a new position fix is available or that a |
| 54 // fatal error has occurred. Providers should call this for new listeners | 50 // fatal error has occurred. Providers should call this for new listeners |
| 55 // as soon as a position is available. | 51 // as soon as a position is available. |
| 56 virtual bool LocationUpdateAvailable(LocationProviderBase *provider) = 0; | 52 virtual bool LocationUpdateAvailable(LocationProviderBase* provider) = 0; |
| 57 // Used to inform listener that movement has been detected. If obtaining the | 53 // Used to inform listener that movement has been detected. If obtaining the |
| 58 // position succeeds, this will be followed by a call to | 54 // position succeeds, this will be followed by a call to |
| 59 // LocationUpdateAvailable. Some providers may not be able to detect | 55 // LocationUpdateAvailable. Some providers may not be able to detect |
| 60 // movement before a new fix is obtained, so will never call this method. | 56 // movement before a new fix is obtained, so will never call this method. |
| 61 // Note that this is not called in response to registration of a new | 57 // Note that this is not called in response to registration of a new |
| 62 // listener. | 58 // listener. |
| 63 virtual bool MovementDetected(LocationProviderBase *provider) = 0; | 59 virtual bool MovementDetected(LocationProviderBase* provider) = 0; |
| 60 |
| 61 protected: |
| 64 virtual ~ListenerInterface() {} | 62 virtual ~ListenerInterface() {} |
| 65 }; | 63 }; |
| 66 | 64 |
| 67 virtual ~LocationProviderBase() {} | 65 // TODO(joth): Make register / unregister non-virtual. |
| 68 | |
| 69 // Registers a listener, which will be called back on | 66 // Registers a listener, which will be called back on |
| 70 // ListenerInterface::LocationUpdateAvailable as soon as a position is | 67 // ListenerInterface::LocationUpdateAvailable as soon as a position is |
| 71 // available and again whenever a new position is available. Ref counts the | 68 // available and again whenever a new position is available. Ref counts the |
| 72 // listener to handle multiple calls to this method. | 69 // listener to handle multiple calls to this method. |
| 73 virtual void RegisterListener(ListenerInterface *listener, | 70 virtual void RegisterListener(ListenerInterface* listener); |
| 74 bool request_address); | |
| 75 // Unregisters a listener. Unrefs the listener to handle multiple calls to | 71 // Unregisters a listener. Unrefs the listener to handle multiple calls to |
| 76 // this method. Once the ref count reaches zero, the listener is removed and | 72 // this method. Once the ref count reaches zero, the listener is removed and |
| 77 // once this method returns, no further calls to | 73 // once this method returns, no further calls to |
| 78 // ListenerInterface::LocationUpdateAvailable will be made for this listener. | 74 // ListenerInterface::LocationUpdateAvailable will be made for this listener. |
| 79 // It may block if a callback is in progress. | 75 // It may block if a callback is in progress. |
| 80 virtual void UnregisterListener(ListenerInterface *listener); | 76 virtual void UnregisterListener(ListenerInterface* listener); |
| 81 | 77 |
| 82 // Interface methods | 78 // Interface methods |
| 79 // Returns false if the provider failed to start. |
| 80 virtual bool StartProvider() = 0; |
| 83 // Gets the current best position estimate. | 81 // Gets the current best position estimate. |
| 84 virtual void GetPosition(Position *position) = 0; | 82 virtual void GetPosition(Position* position) = 0; |
| 85 // Provides a hint to the provider that new location data is needed as soon | 83 // Provides a hint to the provider that new location data is needed as soon |
| 86 // as possible. Default implementation does nothing. | 84 // as possible. Default implementation does nothing. |
| 87 virtual void UpdatePosition() {} | 85 virtual void UpdatePosition() {} |
| 88 | 86 |
| 89 // Accessor methods. | 87 protected: |
| 90 typedef std::pair<bool, RefCount*> ListenerPair; | 88 // Instances should only be destroyed via the thread safe ref count; derived |
| 91 typedef std::map<ListenerInterface*, ListenerPair> ListenerMap; | 89 // classes should not have a public destructor. |
| 92 ListenerMap *GetListeners(); | 90 friend class base::RefCountedThreadSafe<LocationProviderBase>; |
| 93 Mutex *GetListenersMutex(); | 91 LocationProviderBase(); |
| 92 virtual ~LocationProviderBase(); |
| 94 | 93 |
| 95 protected: | |
| 96 // Inform listeners that a new position or error is available, using | 94 // Inform listeners that a new position or error is available, using |
| 97 // LocationUpdateAvailable. | 95 // LocationUpdateAvailable. |
| 98 virtual void UpdateListeners(); | 96 virtual void UpdateListeners(); |
| 99 // Inform listeners that movement has been detected, using MovementDetected. | 97 // Inform listeners that movement has been detected, using MovementDetected. |
| 100 virtual void InformListenersOfMovement(); | 98 virtual void InformListenersOfMovement(); |
| 101 | 99 |
| 100 MessageLoop* client_loop() { return client_loop_; } |
| 101 void CheckRunningInClientLoop(); |
| 102 |
| 102 private: | 103 private: |
| 104 // Utility methods to delegate method calls into the client thread |
| 105 template <class Method> |
| 106 bool RunInClientThread(const tracked_objects::Location& from_here, |
| 107 Method method) { |
| 108 if (MessageLoop::current() == client_loop_) { |
| 109 return false; |
| 110 } |
| 111 client_loop_->PostTask(from_here, NewRunnableMethod(this, method)); |
| 112 return true; |
| 113 } |
| 114 template <class Method, class A> |
| 115 bool RunInClientThread(const tracked_objects::Location& from_here, |
| 116 Method method, const A& a) { |
| 117 if (MessageLoop::current() == client_loop_) { |
| 118 return false; |
| 119 } |
| 120 client_loop_->PostTask(from_here, NewRunnableMethod(this, method, a)); |
| 121 return true; |
| 122 } |
| 123 |
| 103 // The listeners registered to this provider. For each listener, we store a | 124 // The listeners registered to this provider. For each listener, we store a |
| 104 // ref count and whether it requires an address. | 125 // ref count. |
| 126 typedef std::map<ListenerInterface*, int> ListenerMap; |
| 105 ListenerMap listeners_; | 127 ListenerMap listeners_; |
| 106 Mutex listeners_mutex_; | 128 |
| 129 // Reference to the client's message loop, all callbacks and access to |
| 130 // the listeners_ member should happen in this context. |
| 131 MessageLoop* client_loop_; |
| 107 }; | 132 }; |
| 108 | 133 |
| 109 // Factory functions for the various types of location provider to abstract over | 134 // Factory functions for the various types of location provider to abstract over |
| 110 // the platform-dependent implementations. | 135 // the platform-dependent implementations. |
| 111 LocationProviderBase *NewMockLocationProvider(); | 136 LocationProviderBase* NewMockLocationProvider(); |
| 112 LocationProviderBase *NewGpsLocationProvider( | 137 LocationProviderBase* NewGpsLocationProvider(); |
| 113 BrowsingContext *browsing_context, | 138 LocationProviderBase* NewNetworkLocationProvider( |
| 114 const std::string16 &reverse_geocode_url, | 139 LocationProviderBase::AccessTokenStore* access_token_store, |
| 115 const std::string16 &host_name, | 140 URLRequestContextGetter* context, |
| 116 const std::string16 &address_language); | 141 const GURL& url, |
| 117 LocationProviderBase *NewNetworkLocationProvider( | 142 const string16& host_name); |
| 118 BrowsingContext *browsing_context, | |
| 119 const std::string16 &url, | |
| 120 const std::string16 &host_name, | |
| 121 const std::string16 &language); | |
| 122 | 143 |
| 123 #endif // if 0 | 144 #endif // CHROME_BROWSER_GEOLOCATION_LOCATION_PROVIDER_H_ |
| 124 | |
| 125 #endif // GEARS_GEOLOCATION_LOCATION_PROVIDER_H__ | |
| OLD | NEW |