Home | History | Annotate | Download | only in geolocation
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef CONTENT_BROWSER_GEOLOCATION_GEOLOCATION_PROVIDER_IMPL_H_
      6 #define CONTENT_BROWSER_GEOLOCATION_GEOLOCATION_PROVIDER_IMPL_H_
      7 
      8 #include <list>
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/callback_forward.h"
     13 #include "base/compiler_specific.h"
     14 #include "base/threading/thread.h"
     15 #include "content/common/content_export.h"
     16 #include "content/public/browser/geolocation_provider.h"
     17 #include "content/public/common/geoposition.h"
     18 
     19 template<typename Type> struct DefaultSingletonTraits;
     20 
     21 namespace content {
     22 class LocationArbitrator;
     23 
     24 // This is the main API to the geolocation subsystem. The application will hold
     25 // a single instance of this class and can register multiple clients to be
     26 // notified of location changes:
     27 // * Callbacks are registered by AddLocationUpdateCallback() and will keep
     28 //   receiving updates until unregistered by RemoveLocationUpdateCallback().
     29 // The application must instantiate the GeolocationProvider on the IO thread and
     30 // must communicate with it on the same thread.
     31 // The underlying location arbitrator will only be enabled whilst there is at
     32 // least one registered observer or pending callback. The arbitrator and the
     33 // location providers it uses run on a separate Geolocation thread.
     34 class CONTENT_EXPORT GeolocationProviderImpl
     35     : public NON_EXPORTED_BASE(GeolocationProvider),
     36       public base::Thread {
     37  public:
     38   // GeolocationProvider implementation:
     39   virtual void AddLocationUpdateCallback(const LocationUpdateCallback& callback,
     40                                          bool use_high_accuracy) OVERRIDE;
     41   virtual bool RemoveLocationUpdateCallback(
     42       const LocationUpdateCallback& callback) OVERRIDE;
     43   virtual void UserDidOptIntoLocationServices() OVERRIDE;
     44 
     45   bool LocationServicesOptedIn() const;
     46 
     47   // Overrides the location for automation/testing. Suppresses any further
     48   // updates from the actual providers and sends an update with the overridden
     49   // position to all registered clients.
     50   void OverrideLocationForTesting(const Geoposition& override_position);
     51 
     52   // Callback from the LocationArbitrator. Public for testing.
     53   void OnLocationUpdate(const Geoposition& position);
     54 
     55   // Gets a pointer to the singleton instance of the location relayer, which
     56   // is in turn bound to the browser's global context objects. This must only be
     57   // called on the IO thread so that the GeolocationProviderImpl is always
     58   // instantiated on the same thread. Ownership is NOT returned.
     59   static GeolocationProviderImpl* GetInstance();
     60 
     61  protected:
     62   friend struct DefaultSingletonTraits<GeolocationProviderImpl>;
     63   GeolocationProviderImpl();
     64   virtual ~GeolocationProviderImpl();
     65 
     66   // Useful for injecting mock geolocation arbitrator in tests.
     67   virtual LocationArbitrator* CreateArbitrator();
     68 
     69  private:
     70   typedef std::pair<LocationUpdateCallback, bool> LocationUpdateInfo;
     71   typedef std::list<LocationUpdateInfo> CallbackList;
     72 
     73   bool OnGeolocationThread() const;
     74 
     75   // Start and stop providers as needed when clients are added or removed.
     76   void OnClientsChanged();
     77 
     78   // Stops the providers when there are no more registered clients. Note that
     79   // once the Geolocation thread is started, it will stay alive (but sitting
     80   // idle without any pending messages).
     81   void StopProviders();
     82 
     83   // Starts the geolocation providers or updates their options (delegates to
     84   // arbitrator).
     85   void StartProviders(bool use_high_accuracy);
     86 
     87   // Updates the providers on the geolocation thread, which must be running.
     88   void InformProvidersPermissionGranted();
     89 
     90   // Notifies all registered clients that a position update is available.
     91   void NotifyClients(const Geoposition& position);
     92 
     93   // Thread
     94   virtual void Init() OVERRIDE;
     95   virtual void CleanUp() OVERRIDE;
     96 
     97   // Only used on the IO thread
     98   CallbackList callbacks_;
     99   bool user_did_opt_into_location_services_;
    100   Geoposition position_;
    101 
    102   // True only in testing, where we want to use a custom position.
    103   bool ignore_location_updates_;
    104 
    105   // Only to be used on the geolocation thread.
    106   LocationArbitrator* arbitrator_;
    107 
    108   DISALLOW_COPY_AND_ASSIGN(GeolocationProviderImpl);
    109 };
    110 
    111 }  // namespace content
    112 
    113 #endif  // CONTENT_BROWSER_GEOLOCATION_GEOLOCATION_PROVIDER_IMPL_H_
    114