Chromium Code Reviews| 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/location.h" | 8 #include "base/location.h" |
| 9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 50 NOTREACHED(); // We never try to add the same key twice. | 50 NOTREACHED(); // We never try to add the same key twice. |
| 51 CHECK_EQ(cache_.size(), cache_age_list_.size()); | 51 CHECK_EQ(cache_.size(), cache_age_list_.size()); |
| 52 return false; | 52 return false; |
| 53 } | 53 } |
| 54 cache_age_list_.push_back(result.first); | 54 cache_age_list_.push_back(result.first); |
| 55 DCHECK_EQ(cache_.size(), cache_age_list_.size()); | 55 DCHECK_EQ(cache_.size(), cache_age_list_.size()); |
| 56 return true; | 56 return true; |
| 57 } | 57 } |
| 58 | 58 |
| 59 // Searches for a cached position response for the current WiFi data. Returns | 59 // Searches for a cached position response for the current WiFi data. Returns |
| 60 // the cached position if available, NULL otherwise. | 60 // the cached position if available, nullptr otherwise. |
| 61 const Geoposition* NetworkLocationProvider::PositionCache::FindPosition( | 61 const Geoposition* NetworkLocationProvider::PositionCache::FindPosition( |
| 62 const WifiData& wifi_data) { | 62 const WifiData& wifi_data) { |
| 63 base::string16 key; | 63 base::string16 key; |
| 64 if (!MakeKey(wifi_data, &key)) { | 64 if (!MakeKey(wifi_data, &key)) { |
| 65 return NULL; | 65 return nullptr; |
| 66 } | 66 } |
| 67 CacheMap::const_iterator iter = cache_.find(key); | 67 CacheMap::const_iterator iter = cache_.find(key); |
| 68 return iter == cache_.end() ? NULL : &iter->second; | 68 return iter == cache_.end() ? nullptr : &iter->second; |
| 69 } | 69 } |
| 70 | 70 |
| 71 // Makes the key for the map of cached positions, using the available data. | 71 // Makes the key for the map of cached positions, using the available data. |
| 72 // Returns true if a good key was generated, false otherwise. | 72 // Returns true if a good key was generated, false otherwise. |
| 73 // | 73 // |
| 74 // static | 74 // static |
| 75 bool NetworkLocationProvider::PositionCache::MakeKey( | 75 bool NetworkLocationProvider::PositionCache::MakeKey( |
| 76 const WifiData& wifi_data, | 76 const WifiData& wifi_data, |
| 77 base::string16* key) { | 77 base::string16* key) { |
| 78 // Currently we use only WiFi data and base the key only on the MAC addresses. | 78 // Currently we use only WiFi data and base the key only on the MAC addresses. |
| 79 DCHECK(key); | 79 DCHECK(key); |
| 80 key->clear(); | 80 key->clear(); |
| 81 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|" |
| 82 key->reserve(wifi_data.access_point_data.size() * kCharsPerMacAddress); | 82 key->reserve(wifi_data.access_point_data.size() * kCharsPerMacAddress); |
| 83 const base::string16 separator(base::ASCIIToUTF16("|")); | 83 const base::string16 separator(base::ASCIIToUTF16("|")); |
| 84 for (const auto& access_point_data : wifi_data.access_point_data) { | 84 for (const auto& access_point_data : wifi_data.access_point_data) { |
| 85 *key += separator; | 85 *key += separator; |
| 86 *key += access_point_data.mac_address; | 86 *key += access_point_data.mac_address; |
| 87 *key += separator; | 87 *key += separator; |
| 88 } | 88 } |
| 89 // If the key is the empty string, return false, as we don't want to cache a | 89 // If the key is the empty string, return false, as we don't want to cache a |
| 90 // position for such data. | 90 // position for such data. |
| 91 return !key->empty(); | 91 return !key->empty(); |
| 92 } | 92 } |
| 93 | 93 |
| 94 // NetworkLocationProvider factory function | 94 // NetworkLocationProvider factory function |
| 95 LocationProviderBase* NewNetworkLocationProvider( | 95 LocationProviderBase* NewNetworkLocationProvider( |
| 96 AccessTokenStore* access_token_store, | 96 const scoped_refptr<AccessTokenStore>& access_token_store, |
| 97 net::URLRequestContextGetter* context, | 97 const scoped_refptr<net::URLRequestContextGetter>& context, |
| 98 const GURL& url, | 98 const GURL& url, |
| 99 const base::string16& access_token) { | 99 const base::string16& access_token) { |
| 100 return new NetworkLocationProvider( | 100 return new NetworkLocationProvider( |
| 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 const scoped_refptr<AccessTokenStore>& access_token_store, |
| 107 net::URLRequestContextGetter* url_context_getter, | 107 const scoped_refptr<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_manager_(NULL), | 111 wifi_data_provider_manager_(nullptr), |
| 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 position_cache_(new PositionCache()), | |
| 119 weak_factory_(this) { | 120 weak_factory_(this) { |
| 120 // Create the position cache. | |
| 121 position_cache_.reset(new PositionCache()); | |
| 122 | |
| 123 request_.reset(new NetworkLocationRequest( | 121 request_.reset(new NetworkLocationRequest( |
| 124 url_context_getter, | 122 url_context_getter, |
| 125 url, | 123 url, |
| 126 base::Bind(&NetworkLocationProvider::OnLocationResponse, | 124 base::Bind(&NetworkLocationProvider::OnLocationResponse, |
| 127 base::Unretained(this)))); | 125 base::Unretained(this)))); |
| 128 } | 126 } |
| 129 | 127 |
| 130 NetworkLocationProvider::~NetworkLocationProvider() { | 128 NetworkLocationProvider::~NetworkLocationProvider() { |
| 131 StopProvider(); | 129 StopProvider(); |
| 132 } | 130 } |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 161 } | 159 } |
| 162 | 160 |
| 163 void NetworkLocationProvider::OnLocationResponse( | 161 void NetworkLocationProvider::OnLocationResponse( |
| 164 const Geoposition& position, | 162 const Geoposition& position, |
| 165 bool server_error, | 163 bool server_error, |
| 166 const base::string16& access_token, | 164 const base::string16& access_token, |
| 167 const WifiData& wifi_data) { | 165 const WifiData& wifi_data) { |
| 168 DCHECK(CalledOnValidThread()); | 166 DCHECK(CalledOnValidThread()); |
| 169 // Record the position and update our cache. | 167 // Record the position and update our cache. |
| 170 position_ = position; | 168 position_ = position; |
| 171 if (position.Validate()) { | 169 if (position.Validate()) |
|
Wez
2016/07/16 00:35:10
nit: Keep the { } for consistency with the rest of
mcasas
2016/07/16 01:17:46
Done.
| |
| 172 position_cache_->CachePosition(wifi_data, position); | 170 position_cache_->CachePosition(wifi_data, position); |
| 173 } | |
| 174 | 171 |
| 175 // Record access_token if it's set. | 172 // Record access_token if it's set. |
| 176 if (!access_token.empty() && access_token_ != access_token) { | 173 if (!access_token.empty() && access_token_ != access_token) { |
| 177 access_token_ = access_token; | 174 access_token_ = access_token; |
| 178 access_token_store_->SaveAccessToken(request_->url(), access_token); | 175 access_token_store_->SaveAccessToken(request_->url(), access_token); |
| 179 } | 176 } |
| 180 | 177 |
| 181 // Let listeners know that we now have a position available. | 178 // Let listeners know that we now have a position available. |
| 182 NotifyCallback(position_); | 179 NotifyCallback(position_); |
| 183 } | 180 } |
| 184 | 181 |
| 185 bool NetworkLocationProvider::StartProvider(bool high_accuracy) { | 182 bool NetworkLocationProvider::StartProvider(bool high_accuracy) { |
| 186 DCHECK(CalledOnValidThread()); | 183 DCHECK(CalledOnValidThread()); |
| 187 if (IsStarted()) | 184 if (IsStarted()) |
| 188 return true; | 185 return true; |
| 189 DCHECK(wifi_data_provider_manager_ == NULL); | 186 DCHECK(wifi_data_provider_manager_ == nullptr); |
|
Wez
2016/07/16 00:35:10
nit: !wifi_data_provider_manager_
mcasas
2016/07/16 01:17:46
Done.
| |
| 190 if (!request_->url().is_valid()) { | 187 if (!request_->url().is_valid()) { |
| 191 LOG(WARNING) << "StartProvider() : Failed, Bad URL: " | 188 LOG(WARNING) << "StartProvider() : Failed, Bad URL: " |
| 192 << request_->url().possibly_invalid_spec(); | 189 << request_->url().possibly_invalid_spec(); |
| 193 return false; | 190 return false; |
| 194 } | 191 } |
| 195 | 192 |
| 196 // Registers a callback with the data provider. The first call to Register | 193 // 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 | 194 // will create a singleton data provider and it will be deleted when the last |
| 198 // callback is removed with Unregister. | 195 // callback is removed with Unregister. |
| 199 wifi_data_provider_manager_ = | 196 wifi_data_provider_manager_ = |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 216 | 213 |
| 217 is_new_data_available_ = is_wifi_data_complete_; | 214 is_new_data_available_ = is_wifi_data_complete_; |
| 218 RequestRefresh(); | 215 RequestRefresh(); |
| 219 } | 216 } |
| 220 | 217 |
| 221 void NetworkLocationProvider::StopProvider() { | 218 void NetworkLocationProvider::StopProvider() { |
| 222 DCHECK(CalledOnValidThread()); | 219 DCHECK(CalledOnValidThread()); |
| 223 if (IsStarted()) { | 220 if (IsStarted()) { |
| 224 wifi_data_provider_manager_->Unregister(&wifi_data_update_callback_); | 221 wifi_data_provider_manager_->Unregister(&wifi_data_update_callback_); |
| 225 } | 222 } |
| 226 wifi_data_provider_manager_ = NULL; | 223 wifi_data_provider_manager_ = nullptr; |
| 227 weak_factory_.InvalidateWeakPtrs(); | 224 weak_factory_.InvalidateWeakPtrs(); |
| 228 } | 225 } |
| 229 | 226 |
| 230 // Other methods | 227 // Other methods |
| 231 void NetworkLocationProvider::RequestPosition() { | 228 void NetworkLocationProvider::RequestPosition() { |
| 232 DCHECK(CalledOnValidThread()); | 229 DCHECK(CalledOnValidThread()); |
| 233 if (!is_new_data_available_) | 230 if (!is_new_data_available_) |
| 234 return; | 231 return; |
| 235 | 232 |
| 236 const Geoposition* cached_position = | 233 const Geoposition* cached_position = |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 261 // NetworkLocationRequest for each and hold a set of pending requests. | 258 // NetworkLocationRequest for each and hold a set of pending requests. |
| 262 if (request_->is_request_pending()) { | 259 if (request_->is_request_pending()) { |
| 263 DVLOG(1) << "NetworkLocationProvider - pre-empting pending network request " | 260 DVLOG(1) << "NetworkLocationProvider - pre-empting pending network request " |
| 264 "with new data. Wifi APs: " | 261 "with new data. Wifi APs: " |
| 265 << wifi_data_.access_point_data.size(); | 262 << wifi_data_.access_point_data.size(); |
| 266 } | 263 } |
| 267 request_->MakeRequest(access_token_, wifi_data_, wifi_timestamp_); | 264 request_->MakeRequest(access_token_, wifi_data_, wifi_timestamp_); |
| 268 } | 265 } |
| 269 | 266 |
| 270 bool NetworkLocationProvider::IsStarted() const { | 267 bool NetworkLocationProvider::IsStarted() const { |
| 271 return wifi_data_provider_manager_ != NULL; | 268 return wifi_data_provider_manager_ != nullptr; |
| 272 } | 269 } |
| 273 | 270 |
| 274 } // namespace content | 271 } // namespace content |
| OLD | NEW |