| 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_NETWORK_LOCATION_PROVIDER_H_ | |
| 6 #define DEVICE_GEOLOCATION_NETWORK_LOCATION_PROVIDER_H_ | |
| 7 | |
| 8 #include <stddef.h> | |
| 9 | |
| 10 #include <list> | |
| 11 #include <map> | |
| 12 #include <memory> | |
| 13 | |
| 14 #include "base/macros.h" | |
| 15 #include "base/memory/ref_counted.h" | |
| 16 #include "base/memory/weak_ptr.h" | |
| 17 #include "base/strings/string16.h" | |
| 18 #include "base/threading/non_thread_safe.h" | |
| 19 #include "base/threading/thread.h" | |
| 20 #include "device/geolocation/geolocation_export.h" | |
| 21 #include "device/geolocation/geoposition.h" | |
| 22 #include "device/geolocation/location_provider_base.h" | |
| 23 #include "device/geolocation/network_location_request.h" | |
| 24 #include "device/geolocation/wifi_data_provider_manager.h" | |
| 25 | |
| 26 namespace device { | |
| 27 class AccessTokenStore; | |
| 28 | |
| 29 class NetworkLocationProvider : public base::NonThreadSafe, | |
| 30 public LocationProviderBase { | |
| 31 public: | |
| 32 // Cache of recently resolved locations. Public for tests. | |
| 33 class DEVICE_GEOLOCATION_EXPORT PositionCache { | |
| 34 public: | |
| 35 // The maximum size of the cache of positions. | |
| 36 static const size_t kMaximumSize; | |
| 37 | |
| 38 PositionCache(); | |
| 39 ~PositionCache(); | |
| 40 | |
| 41 // Caches the current position response for the current set of cell ID and | |
| 42 // WiFi data. In the case of the cache exceeding kMaximumSize this will | |
| 43 // evict old entries in FIFO orderer of being added. | |
| 44 // Returns true on success, false otherwise. | |
| 45 bool CachePosition(const WifiData& wifi_data, | |
| 46 const Geoposition& position); | |
| 47 | |
| 48 // Searches for a cached position response for the current set of data. | |
| 49 // Returns NULL if the position is not in the cache, or the cached | |
| 50 // position if available. Ownership remains with the cache. | |
| 51 const Geoposition* FindPosition(const WifiData& wifi_data); | |
| 52 | |
| 53 private: | |
| 54 // Makes the key for the map of cached positions, using a set of | |
| 55 // data. Returns true if a good key was generated, false otherwise. | |
| 56 static bool MakeKey(const WifiData& wifi_data, | |
| 57 base::string16* key); | |
| 58 | |
| 59 // The cache of positions. This is stored as a map keyed on a string that | |
| 60 // represents a set of data, and a list to provide | |
| 61 // least-recently-added eviction. | |
| 62 typedef std::map<base::string16, Geoposition> CacheMap; | |
| 63 CacheMap cache_; | |
| 64 typedef std::list<CacheMap::iterator> CacheAgeList; | |
| 65 CacheAgeList cache_age_list_; // Oldest first. | |
| 66 }; | |
| 67 | |
| 68 NetworkLocationProvider( | |
| 69 const scoped_refptr<AccessTokenStore>& access_token_store, | |
| 70 const scoped_refptr<net::URLRequestContextGetter>& context, | |
| 71 const GURL& url, | |
| 72 const base::string16& access_token); | |
| 73 ~NetworkLocationProvider() override; | |
| 74 | |
| 75 // LocationProvider implementation | |
| 76 bool StartProvider(bool high_accuracy) override; | |
| 77 void StopProvider() override; | |
| 78 void GetPosition(Geoposition* position) override; | |
| 79 void RequestRefresh() override; | |
| 80 void OnPermissionGranted() override; | |
| 81 | |
| 82 private: | |
| 83 // Satisfies a position request from cache or network. | |
| 84 void RequestPosition(); | |
| 85 | |
| 86 // Gets called when new wifi data is available. | |
| 87 void OnWifiDataUpdate(); | |
| 88 | |
| 89 // Internal helper used by OnWifiDataUpdate. | |
| 90 void OnWifiDataUpdated(); | |
| 91 | |
| 92 bool IsStarted() const; | |
| 93 | |
| 94 void OnLocationResponse(const Geoposition& position, | |
| 95 bool server_error, | |
| 96 const base::string16& access_token, | |
| 97 const WifiData& wifi_data); | |
| 98 | |
| 99 const scoped_refptr<AccessTokenStore> access_token_store_; | |
| 100 | |
| 101 // The wifi data provider, acquired via global factories. | |
| 102 WifiDataProviderManager* wifi_data_provider_manager_; | |
| 103 | |
| 104 WifiDataProviderManager::WifiDataUpdateCallback wifi_data_update_callback_; | |
| 105 | |
| 106 // The wifi data and a flag to indicate if the data set is complete. | |
| 107 WifiData wifi_data_; | |
| 108 bool is_wifi_data_complete_; | |
| 109 | |
| 110 // The timestamp for the latest wifi data update. | |
| 111 base::Time wifi_timestamp_; | |
| 112 | |
| 113 // Cached value loaded from the token store or set by a previous server | |
| 114 // response, and sent in each subsequent network request. | |
| 115 base::string16 access_token_; | |
| 116 | |
| 117 // The current best position estimate. | |
| 118 Geoposition position_; | |
| 119 | |
| 120 // Whether permission has been granted for the provider to operate. | |
| 121 bool is_permission_granted_; | |
| 122 | |
| 123 bool is_new_data_available_; | |
| 124 | |
| 125 // The network location request object, and the url it uses. | |
| 126 std::unique_ptr<NetworkLocationRequest> request_; | |
| 127 | |
| 128 // The cache of positions. | |
| 129 const std::unique_ptr<PositionCache> position_cache_; | |
| 130 | |
| 131 base::WeakPtrFactory<NetworkLocationProvider> weak_factory_; | |
| 132 | |
| 133 DISALLOW_COPY_AND_ASSIGN(NetworkLocationProvider); | |
| 134 }; | |
| 135 | |
| 136 // Factory functions for the various types of location provider to abstract | |
| 137 // over the platform-dependent implementations. | |
| 138 DEVICE_GEOLOCATION_EXPORT LocationProviderBase* NewNetworkLocationProvider( | |
| 139 const scoped_refptr<AccessTokenStore>& access_token_store, | |
| 140 const scoped_refptr<net::URLRequestContextGetter>& context, | |
| 141 const GURL& url, | |
| 142 const base::string16& access_token); | |
| 143 | |
| 144 } // namespace device | |
| 145 | |
| 146 #endif // DEVICE_GEOLOCATION_NETWORK_LOCATION_PROVIDER_H_ | |
| OLD | NEW |