| 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/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
| 10 #include "content/public/browser/access_token_store.h" | 10 #include "content/public/browser/access_token_store.h" |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 access_token_store, context, url, access_token); | 101 access_token_store, context, url, access_token); |
| 102 } | 102 } |
| 103 | 103 |
| 104 // NetworkLocationProvider | 104 // NetworkLocationProvider |
| 105 NetworkLocationProvider::NetworkLocationProvider( | 105 NetworkLocationProvider::NetworkLocationProvider( |
| 106 AccessTokenStore* access_token_store, | 106 AccessTokenStore* access_token_store, |
| 107 net::URLRequestContextGetter* url_context_getter, | 107 net::URLRequestContextGetter* url_context_getter, |
| 108 const GURL& url, | 108 const GURL& url, |
| 109 const base::string16& access_token) | 109 const base::string16& access_token) |
| 110 : access_token_store_(access_token_store), | 110 : access_token_store_(access_token_store), |
| 111 wifi_data_provider_(NULL), | 111 wifi_data_provider_manager_(NULL), |
| 112 wifi_data_update_callback_( | 112 wifi_data_update_callback_( |
| 113 base::Bind(&NetworkLocationProvider::OnWifiDataUpdate, | 113 base::Bind(&NetworkLocationProvider::OnWifiDataUpdate, |
| 114 base::Unretained(this))), | 114 base::Unretained(this))), |
| 115 is_wifi_data_complete_(false), | 115 is_wifi_data_complete_(false), |
| 116 access_token_(access_token), | 116 access_token_(access_token), |
| 117 is_permission_granted_(false), | 117 is_permission_granted_(false), |
| 118 is_new_data_available_(false), | 118 is_new_data_available_(false), |
| 119 weak_factory_(this) { | 119 weak_factory_(this) { |
| 120 // Create the position cache. | 120 // Create the position cache. |
| 121 position_cache_.reset(new PositionCache()); | 121 position_cache_.reset(new PositionCache()); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 147 } | 147 } |
| 148 | 148 |
| 149 void NetworkLocationProvider::OnPermissionGranted() { | 149 void NetworkLocationProvider::OnPermissionGranted() { |
| 150 const bool was_permission_granted = is_permission_granted_; | 150 const bool was_permission_granted = is_permission_granted_; |
| 151 is_permission_granted_ = true; | 151 is_permission_granted_ = true; |
| 152 if (!was_permission_granted && IsStarted()) { | 152 if (!was_permission_granted && IsStarted()) { |
| 153 RequestRefresh(); | 153 RequestRefresh(); |
| 154 } | 154 } |
| 155 } | 155 } |
| 156 | 156 |
| 157 void NetworkLocationProvider::OnWifiDataUpdate(WifiDataProvider* provider) { | 157 void NetworkLocationProvider::OnWifiDataUpdate( |
| 158 DCHECK(provider == wifi_data_provider_); | 158 WifiDataProviderManager* manager) { |
| 159 is_wifi_data_complete_ = wifi_data_provider_->GetData(&wifi_data_); | 159 DCHECK(manager == wifi_data_provider_manager_); |
| 160 is_wifi_data_complete_ = wifi_data_provider_manager_->GetData(&wifi_data_); |
| 160 OnWifiDataUpdated(); | 161 OnWifiDataUpdated(); |
| 161 } | 162 } |
| 162 | 163 |
| 163 void NetworkLocationProvider::OnLocationResponse( | 164 void NetworkLocationProvider::OnLocationResponse( |
| 164 const Geoposition& position, | 165 const Geoposition& position, |
| 165 bool server_error, | 166 bool server_error, |
| 166 const base::string16& access_token, | 167 const base::string16& access_token, |
| 167 const WifiData& wifi_data) { | 168 const WifiData& wifi_data) { |
| 168 DCHECK(CalledOnValidThread()); | 169 DCHECK(CalledOnValidThread()); |
| 169 // Record the position and update our cache. | 170 // Record the position and update our cache. |
| 170 position_ = position; | 171 position_ = position; |
| 171 if (position.Validate()) { | 172 if (position.Validate()) { |
| 172 position_cache_->CachePosition(wifi_data, position); | 173 position_cache_->CachePosition(wifi_data, position); |
| 173 } | 174 } |
| 174 | 175 |
| 175 // Record access_token if it's set. | 176 // Record access_token if it's set. |
| 176 if (!access_token.empty() && access_token_ != access_token) { | 177 if (!access_token.empty() && access_token_ != access_token) { |
| 177 access_token_ = access_token; | 178 access_token_ = access_token; |
| 178 access_token_store_->SaveAccessToken(request_->url(), access_token); | 179 access_token_store_->SaveAccessToken(request_->url(), access_token); |
| 179 } | 180 } |
| 180 | 181 |
| 181 // Let listeners know that we now have a position available. | 182 // Let listeners know that we now have a position available. |
| 182 NotifyCallback(position_); | 183 NotifyCallback(position_); |
| 183 } | 184 } |
| 184 | 185 |
| 185 bool NetworkLocationProvider::StartProvider(bool high_accuracy) { | 186 bool NetworkLocationProvider::StartProvider(bool high_accuracy) { |
| 186 DCHECK(CalledOnValidThread()); | 187 DCHECK(CalledOnValidThread()); |
| 187 if (IsStarted()) | 188 if (IsStarted()) |
| 188 return true; | 189 return true; |
| 189 DCHECK(wifi_data_provider_ == NULL); | 190 DCHECK(wifi_data_provider_manager_ == NULL); |
| 190 if (!request_->url().is_valid()) { | 191 if (!request_->url().is_valid()) { |
| 191 LOG(WARNING) << "StartProvider() : Failed, Bad URL: " | 192 LOG(WARNING) << "StartProvider() : Failed, Bad URL: " |
| 192 << request_->url().possibly_invalid_spec(); | 193 << request_->url().possibly_invalid_spec(); |
| 193 return false; | 194 return false; |
| 194 } | 195 } |
| 195 | 196 |
| 196 // Registers a callback with the data provider. The first call to Register | 197 // Registers a callback with the data provider. The first call to Register |
| 197 // will create a singleton data provider and it will be deleted when the last | 198 // will create a singleton data provider and it will be deleted when the last |
| 198 // callback is removed with Unregister. | 199 // callback is removed with Unregister. |
| 199 wifi_data_provider_ = WifiDataProvider::Register(&wifi_data_update_callback_); | 200 wifi_data_provider_manager_ = |
| 201 WifiDataProviderManager::Register(&wifi_data_update_callback_); |
| 200 | 202 |
| 201 base::MessageLoop::current()->PostDelayedTask( | 203 base::MessageLoop::current()->PostDelayedTask( |
| 202 FROM_HERE, | 204 FROM_HERE, |
| 203 base::Bind(&NetworkLocationProvider::RequestPosition, | 205 base::Bind(&NetworkLocationProvider::RequestPosition, |
| 204 weak_factory_.GetWeakPtr()), | 206 weak_factory_.GetWeakPtr()), |
| 205 base::TimeDelta::FromSeconds(kDataCompleteWaitSeconds)); | 207 base::TimeDelta::FromSeconds(kDataCompleteWaitSeconds)); |
| 206 // Get the wifi data. | 208 // Get the wifi data. |
| 207 is_wifi_data_complete_ = wifi_data_provider_->GetData(&wifi_data_); | 209 is_wifi_data_complete_ = wifi_data_provider_manager_->GetData(&wifi_data_); |
| 208 if (is_wifi_data_complete_) | 210 if (is_wifi_data_complete_) |
| 209 OnWifiDataUpdated(); | 211 OnWifiDataUpdated(); |
| 210 return true; | 212 return true; |
| 211 } | 213 } |
| 212 | 214 |
| 213 void NetworkLocationProvider::OnWifiDataUpdated() { | 215 void NetworkLocationProvider::OnWifiDataUpdated() { |
| 214 DCHECK(CalledOnValidThread()); | 216 DCHECK(CalledOnValidThread()); |
| 215 wifi_data_updated_timestamp_ = base::Time::Now(); | 217 wifi_data_updated_timestamp_ = base::Time::Now(); |
| 216 | 218 |
| 217 is_new_data_available_ = is_wifi_data_complete_; | 219 is_new_data_available_ = is_wifi_data_complete_; |
| 218 RequestRefresh(); | 220 RequestRefresh(); |
| 219 } | 221 } |
| 220 | 222 |
| 221 void NetworkLocationProvider::StopProvider() { | 223 void NetworkLocationProvider::StopProvider() { |
| 222 DCHECK(CalledOnValidThread()); | 224 DCHECK(CalledOnValidThread()); |
| 223 if (IsStarted()) { | 225 if (IsStarted()) { |
| 224 wifi_data_provider_->Unregister(&wifi_data_update_callback_); | 226 wifi_data_provider_manager_->Unregister(&wifi_data_update_callback_); |
| 225 } | 227 } |
| 226 wifi_data_provider_ = NULL; | 228 wifi_data_provider_manager_ = NULL; |
| 227 weak_factory_.InvalidateWeakPtrs(); | 229 weak_factory_.InvalidateWeakPtrs(); |
| 228 } | 230 } |
| 229 | 231 |
| 230 // Other methods | 232 // Other methods |
| 231 void NetworkLocationProvider::RequestPosition() { | 233 void NetworkLocationProvider::RequestPosition() { |
| 232 DCHECK(CalledOnValidThread()); | 234 DCHECK(CalledOnValidThread()); |
| 233 if (!is_new_data_available_) | 235 if (!is_new_data_available_) |
| 234 return; | 236 return; |
| 235 | 237 |
| 236 const Geoposition* cached_position = | 238 const Geoposition* cached_position = |
| (...skipping 25 matching lines...) Expand all Loading... |
| 262 if (request_->is_request_pending()) { | 264 if (request_->is_request_pending()) { |
| 263 DVLOG(1) << "NetworkLocationProvider - pre-empting pending network request " | 265 DVLOG(1) << "NetworkLocationProvider - pre-empting pending network request " |
| 264 "with new data. Wifi APs: " | 266 "with new data. Wifi APs: " |
| 265 << wifi_data_.access_point_data.size(); | 267 << wifi_data_.access_point_data.size(); |
| 266 } | 268 } |
| 267 request_->MakeRequest(access_token_, wifi_data_, | 269 request_->MakeRequest(access_token_, wifi_data_, |
| 268 wifi_data_updated_timestamp_); | 270 wifi_data_updated_timestamp_); |
| 269 } | 271 } |
| 270 | 272 |
| 271 bool NetworkLocationProvider::IsStarted() const { | 273 bool NetworkLocationProvider::IsStarted() const { |
| 272 return wifi_data_provider_ != NULL; | 274 return wifi_data_provider_manager_ != NULL; |
| 273 } | 275 } |
| 274 | 276 |
| 275 } // namespace content | 277 } // namespace content |
| OLD | NEW |