| 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 #include "content/browser/geolocation/network_location_provider.h" | 5 #include "content/browser/geolocation/network_location_provider.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/time.h" | 8 #include "base/time.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "content/public/browser/access_token_store.h" | 10 #include "content/public/browser/access_token_store.h" |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 | 69 |
| 70 // Makes the key for the map of cached positions, using a set of | 70 // Makes the key for the map of cached positions, using a set of |
| 71 // device data. Returns true if a good key was generated, false otherwise. | 71 // device data. Returns true if a good key was generated, false otherwise. |
| 72 // | 72 // |
| 73 // static | 73 // static |
| 74 bool NetworkLocationProvider::PositionCache::MakeKey( | 74 bool NetworkLocationProvider::PositionCache::MakeKey( |
| 75 const WifiData& wifi_data, | 75 const WifiData& wifi_data, |
| 76 string16* key) { | 76 string16* key) { |
| 77 // Currently we use only the WiFi data, and base the key only on | 77 // Currently we use only the WiFi data, and base the key only on |
| 78 // the MAC addresses. | 78 // the MAC addresses. |
| 79 // TODO(joth): Make use of radio_data. | |
| 80 DCHECK(key); | 79 DCHECK(key); |
| 81 key->clear(); | 80 key->clear(); |
| 82 const size_t kCharsPerMacAddress = 6 * 3 + 1; // e.g. "11:22:33:44:55:66|" | 81 const size_t kCharsPerMacAddress = 6 * 3 + 1; // e.g. "11:22:33:44:55:66|" |
| 83 key->reserve(wifi_data.access_point_data.size() * kCharsPerMacAddress); | 82 key->reserve(wifi_data.access_point_data.size() * kCharsPerMacAddress); |
| 84 const string16 separator(ASCIIToUTF16("|")); | 83 const string16 separator(ASCIIToUTF16("|")); |
| 85 for (WifiData::AccessPointDataSet::const_iterator iter = | 84 for (WifiData::AccessPointDataSet::const_iterator iter = |
| 86 wifi_data.access_point_data.begin(); | 85 wifi_data.access_point_data.begin(); |
| 87 iter != wifi_data.access_point_data.end(); | 86 iter != wifi_data.access_point_data.end(); |
| 88 iter++) { | 87 iter++) { |
| 89 *key += separator; | 88 *key += separator; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 105 access_token_store, context, url, access_token); | 104 access_token_store, context, url, access_token); |
| 106 } | 105 } |
| 107 | 106 |
| 108 // NetworkLocationProvider | 107 // NetworkLocationProvider |
| 109 NetworkLocationProvider::NetworkLocationProvider( | 108 NetworkLocationProvider::NetworkLocationProvider( |
| 110 AccessTokenStore* access_token_store, | 109 AccessTokenStore* access_token_store, |
| 111 net::URLRequestContextGetter* url_context_getter, | 110 net::URLRequestContextGetter* url_context_getter, |
| 112 const GURL& url, | 111 const GURL& url, |
| 113 const string16& access_token) | 112 const string16& access_token) |
| 114 : access_token_store_(access_token_store), | 113 : access_token_store_(access_token_store), |
| 115 radio_data_provider_(NULL), | |
| 116 wifi_data_provider_(NULL), | 114 wifi_data_provider_(NULL), |
| 117 is_radio_data_complete_(false), | |
| 118 is_wifi_data_complete_(false), | 115 is_wifi_data_complete_(false), |
| 119 access_token_(access_token), | 116 access_token_(access_token), |
| 120 is_permission_granted_(false), | 117 is_permission_granted_(false), |
| 121 is_new_data_available_(false), | 118 is_new_data_available_(false), |
| 122 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 119 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 123 // Create the position cache. | 120 // Create the position cache. |
| 124 position_cache_.reset(new PositionCache()); | 121 position_cache_.reset(new PositionCache()); |
| 125 | 122 |
| 126 request_.reset(new NetworkLocationRequest(url_context_getter, url, this)); | 123 request_.reset(new NetworkLocationRequest(url_context_getter, url, this)); |
| 127 } | 124 } |
| 128 | 125 |
| 129 NetworkLocationProvider::~NetworkLocationProvider() { | 126 NetworkLocationProvider::~NetworkLocationProvider() { |
| 130 StopProvider(); | 127 StopProvider(); |
| 131 } | 128 } |
| 132 | 129 |
| 133 // LocationProviderBase implementation | 130 // LocationProviderBase implementation |
| 134 void NetworkLocationProvider::GetPosition(Geoposition *position) { | 131 void NetworkLocationProvider::GetPosition(Geoposition *position) { |
| 135 DCHECK(position); | 132 DCHECK(position); |
| 136 *position = position_; | 133 *position = position_; |
| 137 } | 134 } |
| 138 | 135 |
| 139 void NetworkLocationProvider::UpdatePosition() { | 136 void NetworkLocationProvider::UpdatePosition() { |
| 140 // TODO(joth): When called via the public (base class) interface, this should | 137 // TODO(joth): When called via the public (base class) interface, this should |
| 141 // poke each data provider to get them to expedite their next scan. | 138 // poke each data provider to get them to expedite their next scan. |
| 142 // Whilst in the delayed start, only send request if all data is ready. | 139 // Whilst in the delayed start, only send request if all data is ready. |
| 143 if (!weak_factory_.HasWeakPtrs() || | 140 if (!weak_factory_.HasWeakPtrs() || is_wifi_data_complete_) { |
| 144 (is_radio_data_complete_ && is_wifi_data_complete_)) { | |
| 145 RequestPosition(); | 141 RequestPosition(); |
| 146 } | 142 } |
| 147 } | 143 } |
| 148 | 144 |
| 149 void NetworkLocationProvider::OnPermissionGranted() { | 145 void NetworkLocationProvider::OnPermissionGranted() { |
| 150 const bool was_permission_granted = is_permission_granted_; | 146 const bool was_permission_granted = is_permission_granted_; |
| 151 is_permission_granted_ = true; | 147 is_permission_granted_ = true; |
| 152 if (!was_permission_granted && IsStarted()) { | 148 if (!was_permission_granted && IsStarted()) { |
| 153 UpdatePosition(); | 149 UpdatePosition(); |
| 154 } | 150 } |
| 155 } | 151 } |
| 156 | 152 |
| 157 // DeviceDataProviderInterface::ListenerInterface implementation. | 153 // DeviceDataProviderInterface::ListenerInterface implementation. |
| 158 void NetworkLocationProvider::DeviceDataUpdateAvailable( | 154 void NetworkLocationProvider::DeviceDataUpdateAvailable( |
| 159 RadioDataProvider* provider) { | |
| 160 DCHECK(provider == radio_data_provider_); | |
| 161 is_radio_data_complete_ = radio_data_provider_->GetData(&radio_data_); | |
| 162 OnDeviceDataUpdated(); | |
| 163 } | |
| 164 | |
| 165 void NetworkLocationProvider::DeviceDataUpdateAvailable( | |
| 166 WifiDataProvider* provider) { | 155 WifiDataProvider* provider) { |
| 167 DCHECK(provider == wifi_data_provider_); | 156 DCHECK(provider == wifi_data_provider_); |
| 168 is_wifi_data_complete_ = wifi_data_provider_->GetData(&wifi_data_); | 157 is_wifi_data_complete_ = wifi_data_provider_->GetData(&wifi_data_); |
| 169 OnDeviceDataUpdated(); | 158 OnDeviceDataUpdated(); |
| 170 } | 159 } |
| 171 | 160 |
| 172 // NetworkLocationRequest::ListenerInterface implementation. | 161 // NetworkLocationRequest::ListenerInterface implementation. |
| 173 void NetworkLocationProvider::LocationResponseAvailable( | 162 void NetworkLocationProvider::LocationResponseAvailable( |
| 174 const Geoposition& position, | 163 const Geoposition& position, |
| 175 bool server_error, | 164 bool server_error, |
| 176 const string16& access_token, | 165 const string16& access_token, |
| 177 const RadioData& radio_data, | |
| 178 const WifiData& wifi_data) { | 166 const WifiData& wifi_data) { |
| 179 DCHECK(CalledOnValidThread()); | 167 DCHECK(CalledOnValidThread()); |
| 180 // Record the position and update our cache. | 168 // Record the position and update our cache. |
| 181 position_ = position; | 169 position_ = position; |
| 182 if (position.Validate()) { | 170 if (position.Validate()) { |
| 183 position_cache_->CachePosition(wifi_data, position); | 171 position_cache_->CachePosition(wifi_data, position); |
| 184 } | 172 } |
| 185 | 173 |
| 186 // Record access_token if it's set. | 174 // Record access_token if it's set. |
| 187 if (!access_token.empty() && access_token_ != access_token) { | 175 if (!access_token.empty() && access_token_ != access_token) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 199 return true; | 187 return true; |
| 200 DCHECK(wifi_data_provider_ == NULL); | 188 DCHECK(wifi_data_provider_ == NULL); |
| 201 if (!request_->url().is_valid()) { | 189 if (!request_->url().is_valid()) { |
| 202 LOG(WARNING) << "StartProvider() : Failed, Bad URL: " | 190 LOG(WARNING) << "StartProvider() : Failed, Bad URL: " |
| 203 << request_->url().possibly_invalid_spec(); | 191 << request_->url().possibly_invalid_spec(); |
| 204 return false; | 192 return false; |
| 205 } | 193 } |
| 206 | 194 |
| 207 // Get the device data providers. The first call to Register will create the | 195 // Get the device data providers. The first call to Register will create the |
| 208 // provider and it will be deleted by ref counting. | 196 // provider and it will be deleted by ref counting. |
| 209 radio_data_provider_ = RadioDataProvider::Register(this); | |
| 210 wifi_data_provider_ = WifiDataProvider::Register(this); | 197 wifi_data_provider_ = WifiDataProvider::Register(this); |
| 211 | 198 |
| 212 MessageLoop::current()->PostDelayedTask( | 199 MessageLoop::current()->PostDelayedTask( |
| 213 FROM_HERE, | 200 FROM_HERE, |
| 214 base::Bind(&NetworkLocationProvider::RequestPosition, | 201 base::Bind(&NetworkLocationProvider::RequestPosition, |
| 215 weak_factory_.GetWeakPtr()), | 202 weak_factory_.GetWeakPtr()), |
| 216 base::TimeDelta::FromSeconds(kDataCompleteWaitSeconds)); | 203 base::TimeDelta::FromSeconds(kDataCompleteWaitSeconds)); |
| 217 // Get the device data. | 204 // Get the device data. |
| 218 is_radio_data_complete_ = radio_data_provider_->GetData(&radio_data_); | |
| 219 is_wifi_data_complete_ = wifi_data_provider_->GetData(&wifi_data_); | 205 is_wifi_data_complete_ = wifi_data_provider_->GetData(&wifi_data_); |
| 220 if (is_radio_data_complete_ || is_wifi_data_complete_) | 206 if (is_wifi_data_complete_) |
| 221 OnDeviceDataUpdated(); | 207 OnDeviceDataUpdated(); |
| 222 return true; | 208 return true; |
| 223 } | 209 } |
| 224 | 210 |
| 225 void NetworkLocationProvider::StopProvider() { | 211 void NetworkLocationProvider::StopProvider() { |
| 226 DCHECK(CalledOnValidThread()); | 212 DCHECK(CalledOnValidThread()); |
| 227 if (IsStarted()) { | 213 if (IsStarted()) { |
| 228 radio_data_provider_->Unregister(this); | |
| 229 wifi_data_provider_->Unregister(this); | 214 wifi_data_provider_->Unregister(this); |
| 230 } | 215 } |
| 231 radio_data_provider_ = NULL; | |
| 232 wifi_data_provider_ = NULL; | 216 wifi_data_provider_ = NULL; |
| 233 weak_factory_.InvalidateWeakPtrs(); | 217 weak_factory_.InvalidateWeakPtrs(); |
| 234 } | 218 } |
| 235 | 219 |
| 236 // Other methods | 220 // Other methods |
| 237 void NetworkLocationProvider::RequestPosition() { | 221 void NetworkLocationProvider::RequestPosition() { |
| 238 DCHECK(CalledOnValidThread()); | 222 DCHECK(CalledOnValidThread()); |
| 239 if (!is_new_data_available_) | 223 if (!is_new_data_available_) |
| 240 return; | 224 return; |
| 241 | 225 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 263 weak_factory_.InvalidateWeakPtrs(); | 247 weak_factory_.InvalidateWeakPtrs(); |
| 264 is_new_data_available_ = false; | 248 is_new_data_available_ = false; |
| 265 | 249 |
| 266 // TODO(joth): Rather than cancel pending requests, we should create a new | 250 // TODO(joth): Rather than cancel pending requests, we should create a new |
| 267 // NetworkLocationRequest for each and hold a set of pending requests. | 251 // NetworkLocationRequest for each and hold a set of pending requests. |
| 268 if (request_->is_request_pending()) { | 252 if (request_->is_request_pending()) { |
| 269 DVLOG(1) << "NetworkLocationProvider - pre-empting pending network request " | 253 DVLOG(1) << "NetworkLocationProvider - pre-empting pending network request " |
| 270 "with new data. Wifi APs: " | 254 "with new data. Wifi APs: " |
| 271 << wifi_data_.access_point_data.size(); | 255 << wifi_data_.access_point_data.size(); |
| 272 } | 256 } |
| 273 request_->MakeRequest(access_token_, | 257 request_->MakeRequest(access_token_, wifi_data_, |
| 274 radio_data_, wifi_data_, | |
| 275 device_data_updated_timestamp_); | 258 device_data_updated_timestamp_); |
| 276 } | 259 } |
| 277 | 260 |
| 278 void NetworkLocationProvider::OnDeviceDataUpdated() { | 261 void NetworkLocationProvider::OnDeviceDataUpdated() { |
| 279 DCHECK(CalledOnValidThread()); | 262 DCHECK(CalledOnValidThread()); |
| 280 device_data_updated_timestamp_ = base::Time::Now(); | 263 device_data_updated_timestamp_ = base::Time::Now(); |
| 281 | 264 |
| 282 is_new_data_available_ = is_radio_data_complete_ || is_wifi_data_complete_; | 265 is_new_data_available_ = is_wifi_data_complete_; |
| 283 UpdatePosition(); | 266 UpdatePosition(); |
| 284 } | 267 } |
| 285 | 268 |
| 286 bool NetworkLocationProvider::IsStarted() const { | 269 bool NetworkLocationProvider::IsStarted() const { |
| 287 DCHECK_EQ(!!radio_data_provider_, !!wifi_data_provider_); | |
| 288 return wifi_data_provider_ != NULL; | 270 return wifi_data_provider_ != NULL; |
| 289 } | 271 } |
| OLD | NEW |