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_LOCATION_ARBITRATOR_IMPL_H_ | 5 #ifndef DEVICE_GEOLOCATION_LOCATION_ARBITRATOR_IMPL_H_ |
6 #define DEVICE_GEOLOCATION_LOCATION_ARBITRATOR_IMPL_H_ | 6 #define DEVICE_GEOLOCATION_LOCATION_ARBITRATOR_IMPL_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <memory> |
9 #include <vector> | 10 #include <vector> |
10 | 11 |
11 #include "base/callback_forward.h" | 12 #include "base/callback_forward.h" |
12 #include "base/cancelable_callback.h" | 13 #include "base/cancelable_callback.h" |
13 #include "base/macros.h" | 14 #include "base/macros.h" |
14 #include "base/memory/scoped_vector.h" | 15 #include "base/memory/scoped_vector.h" |
15 #include "base/strings/string16.h" | 16 #include "base/strings/string16.h" |
16 #include "base/time/time.h" | 17 #include "base/time/time.h" |
17 #include "device/geolocation/access_token_store.h" | 18 #include "device/geolocation/access_token_store.h" |
18 #include "device/geolocation/geolocation_export.h" | 19 #include "device/geolocation/geolocation_export.h" |
19 #include "device/geolocation/geolocation_provider.h" | 20 #include "device/geolocation/geolocation_provider.h" |
20 #include "device/geolocation/geoposition.h" | 21 #include "device/geolocation/geoposition.h" |
21 #include "device/geolocation/location_arbitrator.h" | |
22 #include "device/geolocation/location_provider.h" | 22 #include "device/geolocation/location_provider.h" |
23 #include "net/url_request/url_request_context_getter.h" | 23 #include "net/url_request/url_request_context_getter.h" |
24 | 24 |
25 namespace net { | 25 namespace net { |
26 class URLRequestContextGetter; | 26 class URLRequestContextGetter; |
27 } | 27 } |
28 | 28 |
29 namespace device { | 29 namespace device { |
30 class AccessTokenStore; | 30 class AccessTokenStore; |
31 class GeolocationDelegate; | 31 class GeolocationDelegate; |
32 class LocationProvider; | 32 class LocationProvider; |
33 | 33 |
34 // This class is responsible for handling updates from multiple underlying | 34 // This class is responsible for handling updates from multiple underlying |
35 // providers and resolving them to a single 'best' location fix at any given | 35 // providers and resolving them to a single 'best' location fix at any given |
36 // moment. | 36 // moment. |
37 class DEVICE_GEOLOCATION_EXPORT LocationArbitratorImpl | 37 class DEVICE_GEOLOCATION_EXPORT LocationArbitratorImpl |
38 : public LocationArbitrator { | 38 : public LocationProvider { |
39 public: | 39 public: |
40 // Number of milliseconds newer a location provider has to be that it's worth | 40 // Number of milliseconds newer a location provider has to be that it's worth |
41 // switching to this location provider on the basis of it being fresher | 41 // switching to this location provider on the basis of it being fresher |
42 // (regardles of relative accuracy). Public for tests. | 42 // (regardles of relative accuracy). Public for tests. |
43 static const int64_t kFixStaleTimeoutMilliseconds; | 43 static const int64_t kFixStaleTimeoutMilliseconds; |
44 | 44 |
45 typedef base::Callback<void(const Geoposition&)> LocationUpdateCallback; | 45 explicit LocationArbitratorImpl(GeolocationDelegate* delegate); |
46 | |
47 LocationArbitratorImpl(const LocationUpdateCallback& callback, | |
48 GeolocationDelegate* delegate); | |
49 ~LocationArbitratorImpl() override; | 46 ~LocationArbitratorImpl() override; |
50 | 47 |
51 static GURL DefaultNetworkProviderURL(); | 48 static GURL DefaultNetworkProviderURL(); |
| 49 bool HasPermissionBeenGrantedForTest() const; |
52 | 50 |
53 // LocationArbitrator | 51 // LocationProvider implementation. |
54 void StartProviders(bool enable_high_accuracy) override; | 52 void SetUpdateCallback( |
55 void StopProviders() override; | 53 const LocationProviderUpdateCallback& callback) override; |
| 54 bool StartProvider(bool enable_high_accuracy) override; |
| 55 void StopProvider() override; |
| 56 const Geoposition& GetPosition() override; |
56 void OnPermissionGranted() override; | 57 void OnPermissionGranted() override; |
57 bool HasPermissionBeenGranted() const override; | |
58 | 58 |
59 protected: | 59 protected: |
60 // These functions are useful for injection of dependencies in derived | 60 // These functions are useful for injection of dependencies in derived |
61 // testing classes. | 61 // testing classes. |
62 virtual scoped_refptr<AccessTokenStore> NewAccessTokenStore(); | 62 virtual scoped_refptr<AccessTokenStore> NewAccessTokenStore(); |
63 virtual std::unique_ptr<LocationProvider> NewNetworkLocationProvider( | 63 virtual std::unique_ptr<LocationProvider> NewNetworkLocationProvider( |
64 const scoped_refptr<AccessTokenStore>& access_token_store, | 64 const scoped_refptr<AccessTokenStore>& access_token_store, |
65 const scoped_refptr<net::URLRequestContextGetter>& context, | 65 const scoped_refptr<net::URLRequestContextGetter>& context, |
66 const GURL& url, | 66 const GURL& url, |
67 const base::string16& access_token); | 67 const base::string16& access_token); |
68 virtual std::unique_ptr<LocationProvider> NewSystemLocationProvider(); | 68 virtual std::unique_ptr<LocationProvider> NewSystemLocationProvider(); |
69 virtual base::Time GetTimeNow() const; | 69 virtual base::Time GetTimeNow() const; |
70 | 70 |
71 private: | 71 private: |
72 friend class TestingLocationArbitrator; | 72 friend class TestingLocationArbitrator; |
73 | 73 |
74 scoped_refptr<AccessTokenStore> GetAccessTokenStore(); | 74 scoped_refptr<AccessTokenStore> GetAccessTokenStore(); |
75 | 75 |
76 // Provider will either be added to |providers_| or | 76 // Provider will either be added to |providers_| or |
77 // deleted on error (e.g. it fails to start). | 77 // deleted on error (e.g. it fails to start). |
78 void RegisterProvider(std::unique_ptr<LocationProvider> provider); | 78 void RegisterProvider(std::unique_ptr<LocationProvider> provider); |
79 | 79 |
80 void RegisterSystemProvider(); | 80 void RegisterSystemProvider(); |
81 void OnAccessTokenStoresLoaded( | 81 void OnAccessTokenStoresLoaded( |
82 AccessTokenStore::AccessTokenMap access_token_map, | 82 AccessTokenStore::AccessTokenMap access_token_map, |
83 const scoped_refptr<net::URLRequestContextGetter>& context_getter); | 83 const scoped_refptr<net::URLRequestContextGetter>& context_getter); |
84 void DoStartProviders(); | 84 bool DoStartProviders(); |
85 | 85 |
86 // Gets called when a provider has a new position. | 86 // Gets called when a provider has a new position. |
87 void OnLocationUpdate(const LocationProvider* provider, | 87 void OnLocationUpdate(const LocationProvider* provider, |
88 const Geoposition& new_position); | 88 const Geoposition& new_position); |
89 | 89 |
90 // Returns true if |new_position| is an improvement over |old_position|. | 90 // Returns true if |new_position| is an improvement over |old_position|. |
91 // Set |from_same_provider| to true if both the positions came from the same | 91 // Set |from_same_provider| to true if both the positions came from the same |
92 // provider. | 92 // provider. |
93 bool IsNewPositionBetter(const Geoposition& old_position, | 93 bool IsNewPositionBetter(const Geoposition& old_position, |
94 const Geoposition& new_position, | 94 const Geoposition& new_position, |
95 bool from_same_provider) const; | 95 bool from_same_provider) const; |
96 | 96 |
97 GeolocationDelegate* delegate_; | 97 GeolocationDelegate* delegate_; |
98 | 98 |
99 scoped_refptr<AccessTokenStore> access_token_store_; | 99 scoped_refptr<AccessTokenStore> access_token_store_; |
100 LocationUpdateCallback arbitrator_update_callback_; | 100 LocationProvider::LocationProviderUpdateCallback arbitrator_update_callback_; |
101 LocationProvider::LocationProviderUpdateCallback provider_update_callback_; | |
102 | 101 |
103 // The CancelableCallback will prevent OnAccessTokenStoresLoaded from being | 102 // The CancelableCallback will prevent OnAccessTokenStoresLoaded from being |
104 // called multiple times by calling Reset() at the time of binding. | 103 // called multiple times by calling Reset() at the time of binding. |
105 base::CancelableCallback<void( | 104 base::CancelableCallback<void( |
106 AccessTokenStore::AccessTokenMap, | 105 AccessTokenStore::AccessTokenMap, |
107 const scoped_refptr<net::URLRequestContextGetter>&)> | 106 const scoped_refptr<net::URLRequestContextGetter>&)> |
108 token_store_callback_; | 107 token_store_callback_; |
109 std::vector<std::unique_ptr<LocationProvider>> providers_; | 108 std::vector<std::unique_ptr<LocationProvider>> providers_; |
110 bool enable_high_accuracy_; | 109 bool enable_high_accuracy_; |
111 // The provider which supplied the current |position_| | 110 // The provider which supplied the current |position_| |
112 const LocationProvider* position_provider_; | 111 const LocationProvider* position_provider_; |
113 bool is_permission_granted_; | 112 bool is_permission_granted_; |
114 // The current best estimate of our position. | 113 // The current best estimate of our position. |
115 Geoposition position_; | 114 Geoposition position_; |
116 | 115 |
117 // Tracks whether providers should be running. | 116 // Tracks whether providers should be running. |
118 bool is_running_; | 117 bool is_running_; |
119 | 118 |
120 DISALLOW_COPY_AND_ASSIGN(LocationArbitratorImpl); | 119 DISALLOW_COPY_AND_ASSIGN(LocationArbitratorImpl); |
121 }; | 120 }; |
122 | 121 |
123 // Factory functions for the various types of location provider to abstract | 122 // Factory functions for the various types of location provider to abstract |
124 // over the platform-dependent implementations. | 123 // over the platform-dependent implementations. |
125 std::unique_ptr<LocationProvider> NewSystemLocationProvider(); | 124 std::unique_ptr<LocationProvider> NewSystemLocationProvider(); |
126 | 125 |
127 } // namespace device | 126 } // namespace device |
128 | 127 |
129 #endif // DEVICE_GEOLOCATION_LOCATION_ARBITRATOR_IMPL_H_ | 128 #endif // DEVICE_GEOLOCATION_LOCATION_ARBITRATOR_IMPL_H_ |
OLD | NEW |