| 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_NETWORK_LOCATION_PROVIDER_H_ | 5 #ifndef DEVICE_GEOLOCATION_NETWORK_LOCATION_PROVIDER_H_ |
| 6 #define DEVICE_GEOLOCATION_NETWORK_LOCATION_PROVIDER_H_ | 6 #define DEVICE_GEOLOCATION_NETWORK_LOCATION_PROVIDER_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <list> | 10 #include <list> |
| 11 #include <map> | 11 #include <map> |
| 12 #include <memory> | 12 #include <memory> |
| 13 | 13 |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 17 #include "base/strings/string16.h" | 17 #include "base/strings/string16.h" |
| 18 #include "base/threading/non_thread_safe.h" | |
| 19 #include "base/threading/thread.h" | 18 #include "base/threading/thread.h" |
| 19 #include "base/threading/thread_checker.h" |
| 20 #include "device/geolocation/geolocation_export.h" | 20 #include "device/geolocation/geolocation_export.h" |
| 21 #include "device/geolocation/geoposition.h" | 21 #include "device/geolocation/geoposition.h" |
| 22 #include "device/geolocation/location_provider.h" | 22 #include "device/geolocation/location_provider.h" |
| 23 #include "device/geolocation/network_location_request.h" | 23 #include "device/geolocation/network_location_request.h" |
| 24 #include "device/geolocation/wifi_data_provider_manager.h" | 24 #include "device/geolocation/wifi_data_provider_manager.h" |
| 25 | 25 |
| 26 namespace device { | 26 namespace device { |
| 27 class AccessTokenStore; | 27 class AccessTokenStore; |
| 28 | 28 |
| 29 class NetworkLocationProvider : public base::NonThreadSafe, | 29 class NetworkLocationProvider : public LocationProvider { |
| 30 public LocationProvider { | |
| 31 public: | 30 public: |
| 32 // Cache of recently resolved locations. Public for tests. | 31 // Cache of recently resolved locations. Public for tests. |
| 33 class DEVICE_GEOLOCATION_EXPORT PositionCache { | 32 class DEVICE_GEOLOCATION_EXPORT PositionCache { |
| 34 public: | 33 public: |
| 35 // The maximum size of the cache of positions. | 34 // The maximum size of the cache of positions. |
| 36 static const size_t kMaximumSize; | 35 static const size_t kMaximumSize; |
| 37 | 36 |
| 38 PositionCache(); | 37 PositionCache(); |
| 39 ~PositionCache(); | 38 ~PositionCache(); |
| 40 | 39 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 64 }; | 63 }; |
| 65 | 64 |
| 66 NetworkLocationProvider( | 65 NetworkLocationProvider( |
| 67 const scoped_refptr<AccessTokenStore>& access_token_store, | 66 const scoped_refptr<AccessTokenStore>& access_token_store, |
| 68 const scoped_refptr<net::URLRequestContextGetter>& context, | 67 const scoped_refptr<net::URLRequestContextGetter>& context, |
| 69 const GURL& url, | 68 const GURL& url, |
| 70 const base::string16& access_token); | 69 const base::string16& access_token); |
| 71 ~NetworkLocationProvider() override; | 70 ~NetworkLocationProvider() override; |
| 72 | 71 |
| 73 // LocationProvider implementation | 72 // LocationProvider implementation |
| 74 void SetUpdateCallback( | 73 void SetUpdateCallback(const LocationProviderUpdateCallback& cb) override; |
| 75 const LocationProviderUpdateCallback& callback) override; | |
| 76 bool StartProvider(bool high_accuracy) override; | 74 bool StartProvider(bool high_accuracy) override; |
| 77 void StopProvider() override; | 75 void StopProvider() override; |
| 78 const Geoposition& GetPosition() override; | 76 const Geoposition& GetPosition() override; |
| 79 void OnPermissionGranted() override; | 77 void OnPermissionGranted() override; |
| 80 | 78 |
| 81 private: | 79 private: |
| 82 // Satisfies a position request from cache or network. | 80 // Tries to update |position_| request from cache or network. |
| 83 void RequestPosition(); | 81 void RequestPosition(); |
| 84 | 82 |
| 85 // Gets called when new wifi data is available. | 83 // Gets called when new wifi data is available, either via explicit request to |
| 84 // or callback from |wifi_data_provider_manager_|. |
| 86 void OnWifiDataUpdate(); | 85 void OnWifiDataUpdate(); |
| 87 | 86 |
| 88 // Internal helper used by OnWifiDataUpdate. | |
| 89 void OnWifiDataUpdated(); | |
| 90 | |
| 91 bool IsStarted() const; | 87 bool IsStarted() const; |
| 92 | 88 |
| 93 void OnLocationResponse(const Geoposition& position, | 89 void OnLocationResponse(const Geoposition& position, |
| 94 bool server_error, | 90 bool server_error, |
| 95 const base::string16& access_token, | 91 const base::string16& access_token, |
| 96 const WifiData& wifi_data); | 92 const WifiData& wifi_data); |
| 97 | 93 |
| 98 const scoped_refptr<AccessTokenStore> access_token_store_; | 94 const scoped_refptr<AccessTokenStore> access_token_store_; |
| 99 | 95 |
| 100 // The wifi data provider, acquired via global factories. | 96 // The wifi data provider, acquired via global factories. Valid between |
| 97 // StartProvider() and StopProvider(), and checked via IsStarted(). |
| 101 WifiDataProviderManager* wifi_data_provider_manager_; | 98 WifiDataProviderManager* wifi_data_provider_manager_; |
| 102 | 99 |
| 103 WifiDataProviderManager::WifiDataUpdateCallback wifi_data_update_callback_; | 100 WifiDataProviderManager::WifiDataUpdateCallback wifi_data_update_callback_; |
| 104 | 101 |
| 105 // The wifi data and a flag to indicate if the data set is complete. | 102 // The wifi data and a flag to indicate if the data set is complete. |
| 106 WifiData wifi_data_; | 103 WifiData wifi_data_; |
| 107 bool is_wifi_data_complete_; | 104 bool is_wifi_data_complete_; |
| 108 | 105 |
| 109 // The timestamp for the latest wifi data update. | 106 // The timestamp for the latest wifi data update. |
| 110 base::Time wifi_timestamp_; | 107 base::Time wifi_timestamp_; |
| 111 | 108 |
| 112 // Cached value loaded from the token store or set by a previous server | 109 // Cached value loaded from the token store or set by a previous server |
| 113 // response, and sent in each subsequent network request. | 110 // response, and sent in each subsequent network request. |
| 114 base::string16 access_token_; | 111 base::string16 access_token_; |
| 115 | 112 |
| 116 // The current best position estimate. | 113 // The current best position estimate. |
| 117 Geoposition position_; | 114 Geoposition position_; |
| 118 | 115 |
| 119 LocationProvider::LocationProviderUpdateCallback | 116 LocationProvider::LocationProviderUpdateCallback |
| 120 location_provider_update_callback_; | 117 location_provider_update_callback_; |
| 121 | 118 |
| 122 // Whether permission has been granted for the provider to operate. | 119 // Whether permission has been granted for the provider to operate. |
| 123 bool is_permission_granted_; | 120 bool is_permission_granted_; |
| 124 | 121 |
| 125 bool is_new_data_available_; | 122 bool is_new_data_available_; |
| 126 | 123 |
| 127 // The network location request object, and the url it uses. | 124 // The network location request object, and the url it uses. |
| 128 std::unique_ptr<NetworkLocationRequest> request_; | 125 const std::unique_ptr<NetworkLocationRequest> request_; |
| 129 | 126 |
| 130 // The cache of positions. | 127 // The cache of positions. |
| 131 const std::unique_ptr<PositionCache> position_cache_; | 128 const std::unique_ptr<PositionCache> position_cache_; |
| 132 | 129 |
| 130 base::ThreadChecker thread_checker_; |
| 131 |
| 133 base::WeakPtrFactory<NetworkLocationProvider> weak_factory_; | 132 base::WeakPtrFactory<NetworkLocationProvider> weak_factory_; |
| 134 | 133 |
| 135 DISALLOW_COPY_AND_ASSIGN(NetworkLocationProvider); | 134 DISALLOW_COPY_AND_ASSIGN(NetworkLocationProvider); |
| 136 }; | 135 }; |
| 137 | 136 |
| 138 // Factory functions for the various types of location provider to abstract | 137 // Factory functions for the various types of location provider to abstract |
| 139 // over the platform-dependent implementations. | 138 // over the platform-dependent implementations. |
| 140 DEVICE_GEOLOCATION_EXPORT LocationProvider* NewNetworkLocationProvider( | 139 DEVICE_GEOLOCATION_EXPORT LocationProvider* NewNetworkLocationProvider( |
| 141 const scoped_refptr<AccessTokenStore>& access_token_store, | 140 const scoped_refptr<AccessTokenStore>& access_token_store, |
| 142 const scoped_refptr<net::URLRequestContextGetter>& context, | 141 const scoped_refptr<net::URLRequestContextGetter>& context, |
| 143 const GURL& url, | 142 const GURL& url, |
| 144 const base::string16& access_token); | 143 const base::string16& access_token); |
| 145 | 144 |
| 146 } // namespace device | 145 } // namespace device |
| 147 | 146 |
| 148 #endif // DEVICE_GEOLOCATION_NETWORK_LOCATION_PROVIDER_H_ | 147 #endif // DEVICE_GEOLOCATION_NETWORK_LOCATION_PROVIDER_H_ |
| OLD | NEW |