Chromium Code Reviews| 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 DEVICE_GEOLOCATION_GEOLOCATION_PROVIDER_IMPL_H_ | 5 #ifndef DEVICE_GEOLOCATION_GEOLOCATION_PROVIDER_IMPL_H_ |
| 6 #define DEVICE_GEOLOCATION_GEOLOCATION_PROVIDER_IMPL_H_ | 6 #define DEVICE_GEOLOCATION_GEOLOCATION_PROVIDER_IMPL_H_ |
| 7 | 7 |
| 8 #include <list> | 8 #include <list> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 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/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
| 16 #include "device/geolocation/geolocation_export.h" | 16 #include "device/geolocation/geolocation_export.h" |
| 17 #include "device/geolocation/geolocation_provider.h" | 17 #include "device/geolocation/geolocation_provider.h" |
| 18 #include "device/geolocation/geoposition.h" | 18 #include "device/geolocation/geoposition.h" |
| 19 #include "device/geolocation/location_provider.h" | |
| 19 | 20 |
| 20 namespace base { | 21 namespace base { |
| 21 template <typename Type> | 22 template <typename Type> |
| 22 struct DefaultSingletonTraits; | 23 struct DefaultSingletonTraits; |
| 23 class SingleThreadTaskRunner; | 24 class SingleThreadTaskRunner; |
| 24 } | 25 } |
| 25 | 26 |
| 26 namespace device { | 27 namespace device { |
| 27 class LocationArbitrator; | |
| 28 | 28 |
| 29 class DEVICE_GEOLOCATION_EXPORT GeolocationProviderImpl | 29 class DEVICE_GEOLOCATION_EXPORT GeolocationProviderImpl |
| 30 : public NON_EXPORTED_BASE(GeolocationProvider), | 30 : public NON_EXPORTED_BASE(GeolocationProvider), |
| 31 public base::Thread { | 31 public base::Thread { |
| 32 public: | 32 public: |
| 33 // GeolocationProvider implementation: | 33 // GeolocationProvider implementation: |
| 34 std::unique_ptr<GeolocationProvider::Subscription> AddLocationUpdateCallback( | 34 std::unique_ptr<GeolocationProvider::Subscription> AddLocationUpdateCallback( |
| 35 const LocationUpdateCallback& callback, | 35 const LocationUpdateCallback& callback, |
| 36 bool enable_high_accuracy) override; | 36 bool enable_high_accuracy) override; |
| 37 void UserDidOptIntoLocationServices() override; | 37 void UserDidOptIntoLocationServices() override; |
| 38 void OverrideLocationForTesting(const Geoposition& position) override; | 38 void OverrideLocationForTesting(const Geoposition& position) override; |
| 39 | 39 |
| 40 // Callback from the LocationArbitrator. Public for testing. | 40 // Callback from the LocationArbitrator. Public for testing. |
| 41 void OnLocationUpdate(const Geoposition& position); | 41 void OnLocationUpdate(const Geoposition& position); |
| 42 | 42 |
| 43 // Gets a pointer to the singleton instance of the location relayer, which | 43 // Gets a pointer to the singleton instance of the location relayer, which |
| 44 // is in turn bound to the browser's global context objects. This must only be | 44 // is in turn bound to the browser's global context objects. This must only be |
| 45 // called on the UI thread so that the GeolocationProviderImpl is always | 45 // called on the UI thread so that the GeolocationProviderImpl is always |
| 46 // instantiated on the same thread. Ownership is NOT returned. | 46 // instantiated on the same thread. Ownership is NOT returned. |
| 47 static GeolocationProviderImpl* GetInstance(); | 47 static GeolocationProviderImpl* GetInstance(); |
| 48 | 48 |
| 49 bool user_did_opt_into_location_services_for_testing() { | 49 bool user_did_opt_into_location_services_for_testing() { |
| 50 return user_did_opt_into_location_services_; | 50 return user_did_opt_into_location_services_; |
| 51 } | 51 } |
| 52 | 52 |
| 53 protected: | 53 // Safe to call from the caller thread while there are no |
|
Kevin M
2016/08/15 19:12:46
Safe to call from any thread?
CJ
2016/08/15 20:11:51
Done.
| |
| 54 // GeolocationProviderImpl clients registered. | |
| 55 void SetArbitratorForTesting(std::unique_ptr<LocationProvider> arbitrator); | |
| 56 | |
| 57 private: | |
| 54 friend struct base::DefaultSingletonTraits<GeolocationProviderImpl>; | 58 friend struct base::DefaultSingletonTraits<GeolocationProviderImpl>; |
| 55 GeolocationProviderImpl(); | 59 GeolocationProviderImpl(); |
| 56 ~GeolocationProviderImpl() override; | 60 ~GeolocationProviderImpl() override; |
| 57 | 61 |
| 58 // Useful for injecting mock geolocation arbitrator in tests. | |
| 59 // TODO(mvanouwerkerk): Use something like SetArbitratorForTesting instead. | |
| 60 virtual std::unique_ptr<LocationArbitrator> CreateArbitrator(); | |
| 61 | |
| 62 private: | |
| 63 bool OnGeolocationThread() const; | 62 bool OnGeolocationThread() const; |
| 64 | 63 |
| 65 // Start and stop providers as needed when clients are added or removed. | 64 // Start and stop providers as needed when clients are added or removed. |
| 66 void OnClientsChanged(); | 65 void OnClientsChanged(); |
| 67 | 66 |
| 68 // Stops the providers when there are no more registered clients. Note that | 67 // Stops the providers when there are no more registered clients. Note that |
| 69 // once the Geolocation thread is started, it will stay alive (but sitting | 68 // once the Geolocation thread is started, it will stay alive (but sitting |
| 70 // idle without any pending messages). | 69 // idle without any pending messages). |
| 71 void StopProviders(); | 70 void StopProviders(); |
| 72 | 71 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 86 | 85 |
| 87 base::CallbackList<void(const Geoposition&)> high_accuracy_callbacks_; | 86 base::CallbackList<void(const Geoposition&)> high_accuracy_callbacks_; |
| 88 base::CallbackList<void(const Geoposition&)> low_accuracy_callbacks_; | 87 base::CallbackList<void(const Geoposition&)> low_accuracy_callbacks_; |
| 89 | 88 |
| 90 bool user_did_opt_into_location_services_; | 89 bool user_did_opt_into_location_services_; |
| 91 Geoposition position_; | 90 Geoposition position_; |
| 92 | 91 |
| 93 // True only in testing, where we want to use a custom position. | 92 // True only in testing, where we want to use a custom position. |
| 94 bool ignore_location_updates_; | 93 bool ignore_location_updates_; |
| 95 | 94 |
| 96 // Only to be used on the geolocation thread. | |
| 97 std::unique_ptr<LocationArbitrator> arbitrator_; | |
| 98 | |
| 99 // Used to PostTask()s from the geolocation thread to creation thread. | 95 // Used to PostTask()s from the geolocation thread to creation thread. |
| 100 const scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; | 96 const scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; |
| 101 | 97 |
| 98 // Only to be used on the geolocation thread. | |
| 99 std::unique_ptr<LocationProvider> arbitrator_; | |
| 100 | |
| 102 DISALLOW_COPY_AND_ASSIGN(GeolocationProviderImpl); | 101 DISALLOW_COPY_AND_ASSIGN(GeolocationProviderImpl); |
| 103 }; | 102 }; |
| 104 | 103 |
| 105 } // namespace device | 104 } // namespace device |
| 106 | 105 |
| 107 #endif // DEVICE_GEOLOCATION_GEOLOCATION_PROVIDER_IMPL_H_ | 106 #endif // DEVICE_GEOLOCATION_GEOLOCATION_PROVIDER_IMPL_H_ |
| OLD | NEW |