| 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 "device/geolocation/network_location_provider.h" | 5 #include "device/geolocation/network_location_provider.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 *key += separator; | 86 *key += separator; |
| 87 *key += access_point_data.mac_address; | 87 *key += access_point_data.mac_address; |
| 88 *key += separator; | 88 *key += separator; |
| 89 } | 89 } |
| 90 // If the key is the empty string, return false, as we don't want to cache a | 90 // If the key is the empty string, return false, as we don't want to cache a |
| 91 // position for such data. | 91 // position for such data. |
| 92 return !key->empty(); | 92 return !key->empty(); |
| 93 } | 93 } |
| 94 | 94 |
| 95 // NetworkLocationProvider factory function | 95 // NetworkLocationProvider factory function |
| 96 LocationProviderBase* NewNetworkLocationProvider( | 96 LocationProvider* NewNetworkLocationProvider( |
| 97 const scoped_refptr<AccessTokenStore>& access_token_store, | 97 const scoped_refptr<AccessTokenStore>& access_token_store, |
| 98 const scoped_refptr<net::URLRequestContextGetter>& context, | 98 const scoped_refptr<net::URLRequestContextGetter>& context, |
| 99 const GURL& url, | 99 const GURL& url, |
| 100 const base::string16& access_token) { | 100 const base::string16& access_token) { |
| 101 return new NetworkLocationProvider(access_token_store, context, url, | 101 return new NetworkLocationProvider(access_token_store, context, url, |
| 102 access_token); | 102 access_token); |
| 103 } | 103 } |
| 104 | 104 |
| 105 // NetworkLocationProvider | 105 // NetworkLocationProvider |
| 106 NetworkLocationProvider::NetworkLocationProvider( | 106 NetworkLocationProvider::NetworkLocationProvider( |
| (...skipping 20 matching lines...) Expand all Loading... |
| 127 | 127 |
| 128 NetworkLocationProvider::~NetworkLocationProvider() { | 128 NetworkLocationProvider::~NetworkLocationProvider() { |
| 129 StopProvider(); | 129 StopProvider(); |
| 130 } | 130 } |
| 131 | 131 |
| 132 // LocationProvider implementation | 132 // LocationProvider implementation |
| 133 const Geoposition& NetworkLocationProvider::GetPosition() { | 133 const Geoposition& NetworkLocationProvider::GetPosition() { |
| 134 return position_; | 134 return position_; |
| 135 } | 135 } |
| 136 | 136 |
| 137 void NetworkLocationProvider::SetUpdateCallback( |
| 138 const LocationProvider::LocationProviderUpdateCallback& callback) { |
| 139 location_provider_update_callback_ = callback; |
| 140 } |
| 141 |
| 137 void NetworkLocationProvider::OnPermissionGranted() { | 142 void NetworkLocationProvider::OnPermissionGranted() { |
| 138 const bool was_permission_granted = is_permission_granted_; | 143 const bool was_permission_granted = is_permission_granted_; |
| 139 is_permission_granted_ = true; | 144 is_permission_granted_ = true; |
| 140 if (!was_permission_granted && IsStarted()) { | 145 if (!was_permission_granted && IsStarted()) { |
| 141 RequestPosition(); | 146 RequestPosition(); |
| 142 } | 147 } |
| 143 } | 148 } |
| 144 | 149 |
| 145 void NetworkLocationProvider::OnWifiDataUpdate() { | 150 void NetworkLocationProvider::OnWifiDataUpdate() { |
| 146 DCHECK(wifi_data_provider_manager_); | 151 DCHECK(wifi_data_provider_manager_); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 160 position_cache_->CachePosition(wifi_data, position); | 165 position_cache_->CachePosition(wifi_data, position); |
| 161 } | 166 } |
| 162 | 167 |
| 163 // Record access_token if it's set. | 168 // Record access_token if it's set. |
| 164 if (!access_token.empty() && access_token_ != access_token) { | 169 if (!access_token.empty() && access_token_ != access_token) { |
| 165 access_token_ = access_token; | 170 access_token_ = access_token; |
| 166 access_token_store_->SaveAccessToken(request_->url(), access_token); | 171 access_token_store_->SaveAccessToken(request_->url(), access_token); |
| 167 } | 172 } |
| 168 | 173 |
| 169 // Let listeners know that we now have a position available. | 174 // Let listeners know that we now have a position available. |
| 170 NotifyCallback(position_); | 175 if (!location_provider_update_callback_.is_null()) |
| 176 location_provider_update_callback_.Run(this, position_); |
| 171 } | 177 } |
| 172 | 178 |
| 173 bool NetworkLocationProvider::StartProvider(bool high_accuracy) { | 179 bool NetworkLocationProvider::StartProvider(bool high_accuracy) { |
| 174 DCHECK(CalledOnValidThread()); | 180 DCHECK(CalledOnValidThread()); |
| 175 if (IsStarted()) | 181 if (IsStarted()) |
| 176 return true; | 182 return true; |
| 177 DCHECK(!wifi_data_provider_manager_); | 183 DCHECK(!wifi_data_provider_manager_); |
| 178 if (!request_->url().is_valid()) { | 184 if (!request_->url().is_valid()) { |
| 179 LOG(WARNING) << "StartProvider() : Failed, Bad URL: " | 185 LOG(WARNING) << "StartProvider() : Failed, Bad URL: " |
| 180 << request_->url().possibly_invalid_spec(); | 186 << request_->url().possibly_invalid_spec(); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 // Record the position and update its timestamp. | 240 // Record the position and update its timestamp. |
| 235 position_ = *cached_position; | 241 position_ = *cached_position; |
| 236 | 242 |
| 237 // The timestamp of a position fix is determined by the timestamp | 243 // The timestamp of a position fix is determined by the timestamp |
| 238 // of the source data update. (The value of position_.timestamp from | 244 // of the source data update. (The value of position_.timestamp from |
| 239 // the cache could be from weeks ago!) | 245 // the cache could be from weeks ago!) |
| 240 position_.timestamp = wifi_timestamp_; | 246 position_.timestamp = wifi_timestamp_; |
| 241 is_new_data_available_ = false; | 247 is_new_data_available_ = false; |
| 242 | 248 |
| 243 // Let listeners know that we now have a position available. | 249 // Let listeners know that we now have a position available. |
| 244 NotifyCallback(position_); | 250 if (!location_provider_update_callback_.is_null()) |
| 251 location_provider_update_callback_.Run(this, position_); |
| 245 return; | 252 return; |
| 246 } | 253 } |
| 247 // Don't send network requests until authorized. http://crbug.com/39171 | 254 // Don't send network requests until authorized. http://crbug.com/39171 |
| 248 if (!is_permission_granted_) | 255 if (!is_permission_granted_) |
| 249 return; | 256 return; |
| 250 | 257 |
| 251 weak_factory_.InvalidateWeakPtrs(); | 258 weak_factory_.InvalidateWeakPtrs(); |
| 252 is_new_data_available_ = false; | 259 is_new_data_available_ = false; |
| 253 | 260 |
| 254 // TODO(joth): Rather than cancel pending requests, we should create a new | 261 // TODO(joth): Rather than cancel pending requests, we should create a new |
| 255 // NetworkLocationRequest for each and hold a set of pending requests. | 262 // NetworkLocationRequest for each and hold a set of pending requests. |
| 256 if (request_->is_request_pending()) { | 263 if (request_->is_request_pending()) { |
| 257 DVLOG(1) << "NetworkLocationProvider - pre-empting pending network request " | 264 DVLOG(1) << "NetworkLocationProvider - pre-empting pending network request " |
| 258 "with new data. Wifi APs: " | 265 "with new data. Wifi APs: " |
| 259 << wifi_data_.access_point_data.size(); | 266 << wifi_data_.access_point_data.size(); |
| 260 } | 267 } |
| 261 request_->MakeRequest(access_token_, wifi_data_, wifi_timestamp_); | 268 request_->MakeRequest(access_token_, wifi_data_, wifi_timestamp_); |
| 262 } | 269 } |
| 263 | 270 |
| 264 bool NetworkLocationProvider::IsStarted() const { | 271 bool NetworkLocationProvider::IsStarted() const { |
| 265 return wifi_data_provider_manager_ != nullptr; | 272 return wifi_data_provider_manager_ != nullptr; |
| 266 } | 273 } |
| 267 | 274 |
| 268 } // namespace device | 275 } // namespace device |
| OLD | NEW |