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