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