| 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 #ifndef CONTENT_BROWSER_GEOLOCATION_GEOLOCATION_PROVIDER_H_ |    5 #ifndef CONTENT_BROWSER_GEOLOCATION_GEOLOCATION_PROVIDER_H_ | 
|    6 #define CONTENT_BROWSER_GEOLOCATION_GEOLOCATION_PROVIDER_H_ |    6 #define CONTENT_BROWSER_GEOLOCATION_GEOLOCATION_PROVIDER_H_ | 
|    7 #pragma once |    7 #pragma once | 
|    8  |    8  | 
|    9 #include <map> |    9 #include <map> | 
 |   10 #include <vector> | 
|   10  |   11  | 
 |   12 #include "base/basictypes.h" | 
 |   13 #include "base/compiler_specific.h" | 
|   11 #include "base/threading/thread.h" |   14 #include "base/threading/thread.h" | 
|   12 #include "content/browser/geolocation/geolocation_observer.h" |   15 #include "content/browser/geolocation/geolocation_observer.h" | 
|   13 #include "content/common/content_export.h" |   16 #include "content/common/content_export.h" | 
 |   17 #include "content/public/browser/geolocation.h" | 
|   14 #include "content/public/common/geoposition.h" |   18 #include "content/public/common/geoposition.h" | 
|   15  |   19  | 
|   16 class GeolocationArbitrator; |   20 class GeolocationArbitrator; | 
 |   21 class GeolocationProviderTest; | 
 |   22 template<typename Type> struct DefaultSingletonTraits; | 
|   17  |   23  | 
|   18 template<typename Type> |   24 // This is the main API to the geolocation subsystem. The application will hold | 
|   19 struct DefaultSingletonTraits; |   25 // a single instance of this class and can register multiple clients to be | 
|   20  |   26 // notified of location changes: | 
|   21 // This is the main API to the geolocation subsystem. The application |   27 // * Observers are registered by AddObserver() and will keep receiving updates | 
|   22 // will hold a single instance of this class, and can register multiple |   28 //   until unregistered by RemoveObserver(). | 
|   23 // observers which will be notified of location updates. Underlying location |   29 // * Callbacks are registered by RequestCallback() and will be called exactly | 
|   24 // arbitrator will only be enabled whilst there is at least one observer |   30 //   once when the next update becomes available. | 
|   25 // registered. |   31 // The application must instantiate the GeolocationProvider on the IO thread and | 
 |   32 // must communicate with it on the same thread. | 
 |   33 // The underlying location arbitrator will only be enabled whilst there is at | 
 |   34 // least one registered observer or pending callback. The arbitrator and the | 
 |   35 // location providers it uses run on a separate Geolocation thread. | 
|   26 class CONTENT_EXPORT GeolocationProvider |   36 class CONTENT_EXPORT GeolocationProvider | 
|   27     : public base::Thread, public GeolocationObserver { |   37     : public base::Thread, public GeolocationObserver { | 
|   28  public: |   38  public: | 
|   29   GeolocationProvider(); |   39   // The GeolocationObserverOptions passed are used as a 'hint' for the provider | 
|   30  |   40   // preferences for this particular observer, however the observer could | 
|   31   // Must be called from the same thread as the GeolocationProvider was created |   41   // receive updates for best available locations from any active provider | 
|   32   // on. The GeolocationObserverOptions passed are used as a 'hint' for the |   42   // whilst it is registered. | 
|   33   // provider preferences for this particular observer, however the observer |   43   // If an existing observer is added a second time, its options are updated | 
|   34   // could receive callbacks for best available locations from any active |  | 
|   35   // provider whilst it is registered. |  | 
|   36   // If an existing observer is added a second time it's options are updated |  | 
|   37   // but only a single call to RemoveObserver() is required to remove it. |   44   // but only a single call to RemoveObserver() is required to remove it. | 
|   38   void AddObserver(GeolocationObserver* delegate, |   45   void AddObserver(GeolocationObserver* delegate, | 
|   39                    const GeolocationObserverOptions& update_options); |   46                    const GeolocationObserverOptions& update_options); | 
|   40  |   47  | 
|   41   // Remove a previously registered observer. No-op if not previously registered |   48   // Remove a previously registered observer. No-op if not previously registered | 
|   42   // via AddObserver(). Returns true if the observer was removed. |   49   // via AddObserver(). Returns true if the observer was removed. | 
|   43   bool RemoveObserver(GeolocationObserver* delegate); |   50   bool RemoveObserver(GeolocationObserver* delegate); | 
|   44  |   51  | 
 |   52   // Request a single callback when the next location update becomes available. | 
 |   53   // Callbacks must only be requested by code that is allowed to access the | 
 |   54   // location. No further permission checks will be made. | 
 |   55   void RequestCallback(const content::GeolocationUpdateCallback& callback); | 
 |   56  | 
|   45   void OnPermissionGranted(); |   57   void OnPermissionGranted(); | 
|   46   bool HasPermissionBeenGranted() const; |   58   bool HasPermissionBeenGranted() const; | 
|   47  |   59  | 
|   48   // GeolocationObserver |   60   // GeolocationObserver implementation. | 
|   49   virtual void OnLocationUpdate(const content::Geoposition& position) OVERRIDE; |   61   virtual void OnLocationUpdate(const content::Geoposition& position) OVERRIDE; | 
|   50  |   62  | 
|   51   // Overrides the location for automation/testing. Updates any current |   63   // Overrides the location for automation/testing. Suppresses any further | 
|   52   // observers with the overriden position. Any further updates from location |   64   // updates from the actual providers and sends an update with the overridden | 
|   53   // providers will be ignored. |   65   // position to all registered clients. | 
|   54   void OverrideLocationForTesting( |   66   void OverrideLocationForTesting( | 
|   55       const content::Geoposition& override_position); |   67       const content::Geoposition& override_position); | 
|   56  |   68  | 
|   57   // Gets a pointer to the singleton instance of the location relayer, which |   69   // Gets a pointer to the singleton instance of the location relayer, which | 
|   58   // is in turn bound to the browser's global context objects. Ownership is NOT |   70   // is in turn bound to the browser's global context objects. This must only be | 
|   59   // returned. |   71   // called on the IO thread so that the GeolocationProvider is always | 
 |   72   // instantiated on the same thread. Ownership is NOT returned. | 
|   60   static GeolocationProvider* GetInstance(); |   73   static GeolocationProvider* GetInstance(); | 
|   61  |   74  | 
 |   75  protected: | 
 |   76   friend struct DefaultSingletonTraits<GeolocationProvider>; | 
 |   77   GeolocationProvider(); | 
 |   78   virtual ~GeolocationProvider(); | 
 |   79  | 
 |   80  private: | 
|   62   typedef std::map<GeolocationObserver*, GeolocationObserverOptions> |   81   typedef std::map<GeolocationObserver*, GeolocationObserverOptions> | 
|   63       ObserverMap; |   82       ObserverMap; | 
|   64  |   83  | 
|   65  private: |   84   typedef std::vector<content::GeolocationUpdateCallback> CallbackList; | 
|   66   friend struct DefaultSingletonTraits<GeolocationProvider>; |  | 
|   67   virtual ~GeolocationProvider(); |  | 
|   68  |   85  | 
|   69   bool OnClientThread() const; |  | 
|   70   bool OnGeolocationThread() const; |   86   bool OnGeolocationThread() const; | 
|   71  |   87  | 
|   72   // When the observer list changes, we may start the thread and the required |   88   // Start and stop providers as needed when clients are added or removed. | 
|   73   // providers, or stop them. |   89   void OnClientsChanged(); | 
|   74   void OnObserversChanged(); |  | 
|   75  |   90  | 
|   76   // Stop the providers when there are no more observers. Note that once our |   91   // Stops the providers when there are no more registered clients. Note that | 
|   77   // thread is started, we'll keep it alive (but with no pending messages). |   92   // once the Geolocation thread is started, it will stay alive (but sitting | 
 |   93   // idle without any pending messages). | 
|   78   void StopProviders(); |   94   void StopProviders(); | 
|   79  |   95  | 
|   80   // Starts or updates the observers' geolocation options |   96   // Starts the geolocation providers or updates their options (delegates to | 
|   81   // (delegates to arbitrator). |   97   // arbitrator). | 
|   82   void StartProviders(const GeolocationObserverOptions& options); |   98   void StartProviders(const GeolocationObserverOptions& options); | 
|   83  |   99  | 
|   84   // Update the providers on the geolocation thread, which must be running. |  100   // Updates the providers on the geolocation thread, which must be running. | 
|   85   void InformProvidersPermissionGranted(); |  101   void InformProvidersPermissionGranted(); | 
|   86  |  102  | 
|   87   // Notifies observers when a new position fix is available. |  103   // Notifies all registered clients that a position update is available. | 
|   88   void NotifyObservers(const content::Geoposition& position); |  104   void NotifyClients(const content::Geoposition& position); | 
|   89  |  105  | 
|   90   // Thread |  106   // Thread | 
|   91   virtual void Init() OVERRIDE; |  107   virtual void Init() OVERRIDE; | 
|   92   virtual void CleanUp() OVERRIDE; |  108   virtual void CleanUp() OVERRIDE; | 
|   93  |  109  | 
|   94   scoped_refptr<base::MessageLoopProxy> client_loop_; |  110   // Only used on the IO thread | 
|   95  |  | 
|   96   // Only used on client thread |  | 
|   97   ObserverMap observers_; |  111   ObserverMap observers_; | 
 |  112   CallbackList callbacks_; | 
|   98   bool is_permission_granted_; |  113   bool is_permission_granted_; | 
|   99   content::Geoposition position_; |  114   content::Geoposition position_; | 
 |  115  | 
|  100   // True only in testing, where we want to use a custom position. |  116   // True only in testing, where we want to use a custom position. | 
|  101   bool ignore_location_updates_; |  117   bool ignore_location_updates_; | 
|  102  |  118  | 
|  103   // Only to be used on the geolocation thread. |  119   // Only to be used on the geolocation thread. | 
|  104   GeolocationArbitrator* arbitrator_; |  120   GeolocationArbitrator* arbitrator_; | 
|  105  |  121  | 
|  106   DISALLOW_COPY_AND_ASSIGN(GeolocationProvider); |  122   DISALLOW_COPY_AND_ASSIGN(GeolocationProvider); | 
|  107 }; |  123 }; | 
|  108  |  124  | 
|  109 #endif  // CONTENT_BROWSER_GEOLOCATION_GEOLOCATION_PROVIDER_H_ |  125 #endif  // CONTENT_BROWSER_GEOLOCATION_GEOLOCATION_PROVIDER_H_ | 
| OLD | NEW |