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 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
143 void NetworkLocationProvider::OnPermissionGranted() { | 143 void NetworkLocationProvider::OnPermissionGranted() { |
144 const bool was_permission_granted = is_permission_granted_; | 144 const bool was_permission_granted = is_permission_granted_; |
145 is_permission_granted_ = true; | 145 is_permission_granted_ = true; |
146 if (!was_permission_granted && IsStarted()) | 146 if (!was_permission_granted && IsStarted()) |
147 RequestPosition(); | 147 RequestPosition(); |
148 } | 148 } |
149 | 149 |
150 void NetworkLocationProvider::OnWifiDataUpdate() { | 150 void NetworkLocationProvider::OnWifiDataUpdate() { |
151 DCHECK(thread_checker_.CalledOnValidThread()); | 151 DCHECK(thread_checker_.CalledOnValidThread()); |
152 DCHECK(IsStarted()); | 152 DCHECK(IsStarted()); |
153 | |
154 if (!high_accuracy_enabled_) { | |
155 // This should never be called if this provider was started with low | |
156 // accuracy. | |
157 NOTREACHED(); | |
158 wifi_data_.access_point_data.clear(); | |
159 return; | |
160 } | |
161 | |
153 is_wifi_data_complete_ = wifi_data_provider_manager_->GetData(&wifi_data_); | 162 is_wifi_data_complete_ = wifi_data_provider_manager_->GetData(&wifi_data_); |
154 if (!is_wifi_data_complete_) | 163 if (!is_wifi_data_complete_) |
155 return; | 164 return; |
156 | 165 |
157 wifi_timestamp_ = base::Time::Now(); | 166 wifi_timestamp_ = base::Time::Now(); |
158 is_new_data_available_ = true; | 167 is_new_data_available_ = true; |
159 RequestPosition(); | 168 RequestPosition(); |
160 } | 169 } |
161 | 170 |
162 void NetworkLocationProvider::OnLocationResponse( | 171 void NetworkLocationProvider::OnLocationResponse( |
(...skipping 13 matching lines...) Expand all Loading... | |
176 access_token_store_->SaveAccessToken(request_->url(), access_token); | 185 access_token_store_->SaveAccessToken(request_->url(), access_token); |
177 } | 186 } |
178 | 187 |
179 // Let listeners know that we now have a position available. | 188 // Let listeners know that we now have a position available. |
180 if (!location_provider_update_callback_.is_null()) | 189 if (!location_provider_update_callback_.is_null()) |
181 location_provider_update_callback_.Run(this, position_); | 190 location_provider_update_callback_.Run(this, position_); |
182 } | 191 } |
183 | 192 |
184 bool NetworkLocationProvider::StartProvider(bool high_accuracy) { | 193 bool NetworkLocationProvider::StartProvider(bool high_accuracy) { |
185 DCHECK(thread_checker_.CalledOnValidThread()); | 194 DCHECK(thread_checker_.CalledOnValidThread()); |
186 if (IsStarted()) | 195 if (IsStarted()) |
blundell
2017/05/31 14:49:35
Here you're never changing the accuracy once the N
| |
187 return true; | 196 return true; |
188 if (!request_->url().is_valid()) | 197 if (!request_->url().is_valid()) |
189 return false; | 198 return false; |
190 | 199 |
200 is_started_ = true; | |
201 high_accuracy_enabled_ = high_accuracy; | |
202 | |
203 if (!high_accuracy_enabled_) { | |
204 RequestPosition(); | |
205 return true; | |
206 } | |
207 | |
191 // Registers a callback with the data provider. The first call to Register() | 208 // Registers a callback with the data provider. The first call to Register() |
192 // will create a singleton data provider that will be deleted on Unregister(). | 209 // will create a singleton data provider that will be deleted on Unregister(). |
193 wifi_data_provider_manager_ = | 210 wifi_data_provider_manager_ = |
194 WifiDataProviderManager::Register(&wifi_data_update_callback_); | 211 WifiDataProviderManager::Register(&wifi_data_update_callback_); |
195 | 212 |
196 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 213 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
197 FROM_HERE, base::Bind(&NetworkLocationProvider::RequestPosition, | 214 FROM_HERE, base::Bind(&NetworkLocationProvider::RequestPosition, |
198 weak_factory_.GetWeakPtr()), | 215 weak_factory_.GetWeakPtr()), |
199 base::TimeDelta::FromSeconds(kDataCompleteWaitSeconds)); | 216 base::TimeDelta::FromSeconds(kDataCompleteWaitSeconds)); |
200 | 217 |
201 OnWifiDataUpdate(); | 218 OnWifiDataUpdate(); |
202 return true; | 219 return true; |
203 } | 220 } |
204 | 221 |
205 void NetworkLocationProvider::StopProvider() { | 222 void NetworkLocationProvider::StopProvider() { |
206 DCHECK(thread_checker_.CalledOnValidThread()); | 223 DCHECK(thread_checker_.CalledOnValidThread()); |
207 DCHECK(IsStarted()); | 224 DCHECK(IsStarted()); |
208 wifi_data_provider_manager_->Unregister(&wifi_data_update_callback_); | 225 if (high_accuracy_enabled_) { |
209 wifi_data_provider_manager_ = nullptr; | 226 wifi_data_provider_manager_->Unregister(&wifi_data_update_callback_); |
227 wifi_data_provider_manager_ = nullptr; | |
228 } | |
210 weak_factory_.InvalidateWeakPtrs(); | 229 weak_factory_.InvalidateWeakPtrs(); |
230 is_started_ = false; | |
211 } | 231 } |
212 | 232 |
213 const Geoposition& NetworkLocationProvider::GetPosition() { | 233 const Geoposition& NetworkLocationProvider::GetPosition() { |
214 return position_; | 234 return position_; |
215 } | 235 } |
216 | 236 |
217 void NetworkLocationProvider::RequestPosition() { | 237 void NetworkLocationProvider::RequestPosition() { |
218 DCHECK(thread_checker_.CalledOnValidThread()); | 238 DCHECK(thread_checker_.CalledOnValidThread()); |
219 | 239 |
220 if (!is_new_data_available_ || !is_wifi_data_complete_) | 240 if (high_accuracy_enabled_) { |
221 return; | 241 if (!is_new_data_available_ || !is_wifi_data_complete_) |
222 DCHECK(!wifi_timestamp_.is_null()) | 242 return; |
223 << "|wifi_timestamp_| must be set before looking up position"; | 243 |
244 DCHECK(!wifi_timestamp_.is_null()) | |
245 << "|wifi_timestamp_| must be set before looking up position"; | |
246 } else { | |
247 wifi_timestamp_ = base::Time::Now(); | |
248 wifi_data_.access_point_data.clear(); | |
249 } | |
224 | 250 |
225 const Geoposition* cached_position = | 251 const Geoposition* cached_position = |
226 position_cache_->FindPosition(wifi_data_); | 252 position_cache_->FindPosition(wifi_data_); |
227 if (cached_position) { | 253 if (cached_position) { |
228 DCHECK(cached_position->Validate()); | 254 DCHECK(cached_position->Validate()); |
229 // Record the position and update its timestamp. | 255 // Record the position and update its timestamp. |
230 position_ = *cached_position; | 256 position_ = *cached_position; |
231 | 257 |
232 // The timestamp of a position fix is determined by the timestamp | 258 // The timestamp of a position fix is determined by the timestamp |
233 // of the source data update. (The value of position_.timestamp from | 259 // of the source data update. (The value of position_.timestamp from |
(...skipping 16 matching lines...) Expand all Loading... | |
250 // NetworkLocationRequest for each and hold a set of pending requests. | 276 // NetworkLocationRequest for each and hold a set of pending requests. |
251 DLOG_IF(WARNING, request_->is_request_pending()) | 277 DLOG_IF(WARNING, request_->is_request_pending()) |
252 << "NetworkLocationProvider - pre-empting pending network request " | 278 << "NetworkLocationProvider - pre-empting pending network request " |
253 "with new data. Wifi APs: " | 279 "with new data. Wifi APs: " |
254 << wifi_data_.access_point_data.size(); | 280 << wifi_data_.access_point_data.size(); |
255 | 281 |
256 request_->MakeRequest(access_token_, wifi_data_, wifi_timestamp_); | 282 request_->MakeRequest(access_token_, wifi_data_, wifi_timestamp_); |
257 } | 283 } |
258 | 284 |
259 bool NetworkLocationProvider::IsStarted() const { | 285 bool NetworkLocationProvider::IsStarted() const { |
260 return wifi_data_provider_manager_ != nullptr; | 286 return is_started_; |
261 } | 287 } |
262 | 288 |
263 } // namespace device | 289 } // namespace device |
OLD | NEW |