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