| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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 CONTENT_BROWSER_GEOLOCATION_NETWORK_LOCATION_PROVIDER_H_ | |
| 6 #define CONTENT_BROWSER_GEOLOCATION_NETWORK_LOCATION_PROVIDER_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <list> | |
| 10 #include <string> | |
| 11 | |
| 12 #include "base/basictypes.h" | |
| 13 #include "base/ref_counted.h" | |
| 14 #include "base/scoped_ptr.h" | |
| 15 #include "base/string16.h" | |
| 16 #include "base/threading/thread.h" | |
| 17 #include "chrome/common/geoposition.h" | |
| 18 #include "content/browser/geolocation/device_data_provider.h" | |
| 19 #include "content/browser/geolocation/location_provider.h" | |
| 20 #include "content/browser/geolocation/network_location_request.h" | |
| 21 | |
| 22 class URLFetcherProtectEntry; | |
| 23 | |
| 24 class NetworkLocationProvider | |
| 25 : public LocationProviderBase, | |
| 26 public GatewayDataProvider::ListenerInterface, | |
| 27 public RadioDataProvider::ListenerInterface, | |
| 28 public WifiDataProvider::ListenerInterface, | |
| 29 public NetworkLocationRequest::ListenerInterface { | |
| 30 public: | |
| 31 // Cache of recently resolved locations. Public for tests. | |
| 32 class PositionCache { | |
| 33 public: | |
| 34 // The maximum size of the cache of positions for previously requested | |
| 35 // device data. | |
| 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 GatewayData& gateway_data, | |
| 46 const WifiData& wifi_data, | |
| 47 const Geoposition& position); | |
| 48 | |
| 49 // Searches for a cached position response for the current set of device | |
| 50 // data. Returns NULL if the position is not in the cache, or the cached | |
| 51 // position if available. Ownership remains with the cache. | |
| 52 const Geoposition* FindPosition(const GatewayData& gateway_data, | |
| 53 const WifiData& wifi_data); | |
| 54 | |
| 55 private: | |
| 56 // Makes the key for the map of cached positions, using a set of | |
| 57 // device data. Returns true if a good key was generated, false otherwise. | |
| 58 static bool MakeKey(const GatewayData& gateway_data, | |
| 59 const WifiData& wifi_data, | |
| 60 string16* key); | |
| 61 | |
| 62 // The cache of positions. This is stored as a map keyed on a string that | |
| 63 // represents a set of device data, and a list to provide | |
| 64 // least-recently-added eviction. | |
| 65 typedef std::map<string16, Geoposition> CacheMap; | |
| 66 CacheMap cache_; | |
| 67 typedef std::list<CacheMap::iterator> CacheAgeList; | |
| 68 CacheAgeList cache_age_list_; // Oldest first. | |
| 69 }; | |
| 70 | |
| 71 NetworkLocationProvider(AccessTokenStore* access_token_store, | |
| 72 URLRequestContextGetter* context, | |
| 73 const GURL& url, | |
| 74 const string16& access_token); | |
| 75 virtual ~NetworkLocationProvider(); | |
| 76 | |
| 77 // LocationProviderBase implementation | |
| 78 virtual bool StartProvider(bool high_accuracy); | |
| 79 virtual void StopProvider(); | |
| 80 virtual void GetPosition(Geoposition *position); | |
| 81 virtual void UpdatePosition(); | |
| 82 virtual void OnPermissionGranted(const GURL& requesting_frame); | |
| 83 | |
| 84 private: | |
| 85 // Satisfies a position request from cache or network. | |
| 86 void RequestPosition(); | |
| 87 | |
| 88 // Internal helper used by DeviceDataUpdateAvailable | |
| 89 void OnDeviceDataUpdated(); | |
| 90 | |
| 91 bool IsStarted() const; | |
| 92 | |
| 93 // DeviceDataProvider::ListenerInterface implementation. | |
| 94 virtual void DeviceDataUpdateAvailable(GatewayDataProvider* provider); | |
| 95 virtual void DeviceDataUpdateAvailable(RadioDataProvider* provider); | |
| 96 virtual void DeviceDataUpdateAvailable(WifiDataProvider* provider); | |
| 97 | |
| 98 // NetworkLocationRequest::ListenerInterface implementation. | |
| 99 virtual void LocationResponseAvailable(const Geoposition& position, | |
| 100 bool server_error, | |
| 101 const string16& access_token, | |
| 102 const GatewayData& gateway_data, | |
| 103 const RadioData& radio_data, | |
| 104 const WifiData& wifi_data); | |
| 105 | |
| 106 scoped_refptr<AccessTokenStore> access_token_store_; | |
| 107 | |
| 108 // The device data providers, acquired via global factories. | |
| 109 GatewayDataProvider* gateway_data_provider_; | |
| 110 RadioDataProvider* radio_data_provider_; | |
| 111 WifiDataProvider* wifi_data_provider_; | |
| 112 | |
| 113 // The radio and wifi data, flags to indicate if each data set is complete. | |
| 114 GatewayData gateway_data_; | |
| 115 RadioData radio_data_; | |
| 116 WifiData wifi_data_; | |
| 117 bool is_gateway_data_complete_; | |
| 118 bool is_radio_data_complete_; | |
| 119 bool is_wifi_data_complete_; | |
| 120 | |
| 121 // The timestamp for the latest device data update. | |
| 122 base::Time device_data_updated_timestamp_; | |
| 123 | |
| 124 // Cached value loaded from the token store or set by a previous server | |
| 125 // response, and sent in each subsequent network request. | |
| 126 string16 access_token_; | |
| 127 | |
| 128 // The current best position estimate. | |
| 129 Geoposition position_; | |
| 130 | |
| 131 bool is_new_data_available_; | |
| 132 | |
| 133 std::string most_recent_authorized_host_; | |
| 134 | |
| 135 // The network location request object, and the url it uses. | |
| 136 scoped_ptr<NetworkLocationRequest> request_; | |
| 137 | |
| 138 ScopedRunnableMethodFactory<NetworkLocationProvider> delayed_start_task_; | |
| 139 // The cache of positions. | |
| 140 scoped_ptr<PositionCache> position_cache_; | |
| 141 | |
| 142 DISALLOW_COPY_AND_ASSIGN(NetworkLocationProvider); | |
| 143 }; | |
| 144 | |
| 145 #endif // CONTENT_BROWSER_GEOLOCATION_NETWORK_LOCATION_PROVIDER_H_ | |
| OLD | NEW |