Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(73)

Side by Side Diff: device/geolocation/network_location_provider.cc

Issue 2820863002: Geolocation: cleanup NetworkLocationProvider (Closed)
Patch Set: restoring returning in StartProvider() if (!request_->url().is_valid()), used in components_browsertests Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « device/geolocation/network_location_provider.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 const base::string16& access_token) 110 const base::string16& access_token)
111 : access_token_store_(access_token_store), 111 : access_token_store_(access_token_store),
112 wifi_data_provider_manager_(nullptr), 112 wifi_data_provider_manager_(nullptr),
113 wifi_data_update_callback_( 113 wifi_data_update_callback_(
114 base::Bind(&NetworkLocationProvider::OnWifiDataUpdate, 114 base::Bind(&NetworkLocationProvider::OnWifiDataUpdate,
115 base::Unretained(this))), 115 base::Unretained(this))),
116 is_wifi_data_complete_(false), 116 is_wifi_data_complete_(false),
117 access_token_(access_token), 117 access_token_(access_token),
118 is_permission_granted_(false), 118 is_permission_granted_(false),
119 is_new_data_available_(false), 119 is_new_data_available_(false),
120 request_(new NetworkLocationRequest(
121 url_context_getter,
122 url,
123 base::Bind(&NetworkLocationProvider::OnLocationResponse,
124 base::Unretained(this)))),
120 position_cache_(new PositionCache), 125 position_cache_(new PositionCache),
121 weak_factory_(this) { 126 weak_factory_(this) {
122 request_.reset(new NetworkLocationRequest( 127 DLOG_IF(WARNING, url.is_valid())
scheib 2017/04/18 03:33:17 DLOG if the url is valid? That doesn't seem to mat
mcasas 2017/04/18 05:39:02 Done.
123 url_context_getter, url, 128 << __func__ << " Bad URL: " << url.possibly_invalid_spec();
124 base::Bind(&NetworkLocationProvider::OnLocationResponse,
125 base::Unretained(this))));
126 } 129 }
127 130
128 NetworkLocationProvider::~NetworkLocationProvider() { 131 NetworkLocationProvider::~NetworkLocationProvider() {
129 StopProvider(); 132 DCHECK(thread_checker_.CalledOnValidThread());
130 } 133 if (IsStarted())
131 134 StopProvider();
132 // LocationProvider implementation
133 const Geoposition& NetworkLocationProvider::GetPosition() {
134 return position_;
135 } 135 }
136 136
137 void NetworkLocationProvider::SetUpdateCallback( 137 void NetworkLocationProvider::SetUpdateCallback(
138 const LocationProvider::LocationProviderUpdateCallback& callback) { 138 const LocationProvider::LocationProviderUpdateCallback& callback) {
139 DCHECK(thread_checker_.CalledOnValidThread());
139 location_provider_update_callback_ = callback; 140 location_provider_update_callback_ = callback;
140 } 141 }
141 142
142 void NetworkLocationProvider::OnPermissionGranted() {
143 const bool was_permission_granted = is_permission_granted_;
144 is_permission_granted_ = true;
145 if (!was_permission_granted && IsStarted()) {
146 RequestPosition();
147 }
148 }
149
150 void NetworkLocationProvider::OnWifiDataUpdate() {
151 DCHECK(wifi_data_provider_manager_);
152 is_wifi_data_complete_ = wifi_data_provider_manager_->GetData(&wifi_data_);
153 OnWifiDataUpdated();
154 }
155
156 void NetworkLocationProvider::OnLocationResponse(
157 const Geoposition& position,
158 bool server_error,
159 const base::string16& access_token,
160 const WifiData& wifi_data) {
161 DCHECK(CalledOnValidThread());
162 // Record the position and update our cache.
163 position_ = position;
164 if (position.Validate()) {
165 position_cache_->CachePosition(wifi_data, position);
166 }
167
168 // Record access_token if it's set.
169 if (!access_token.empty() && access_token_ != access_token) {
170 access_token_ = access_token;
171 access_token_store_->SaveAccessToken(request_->url(), access_token);
172 }
173
174 // Let listeners know that we now have a position available.
175 if (!location_provider_update_callback_.is_null())
176 location_provider_update_callback_.Run(this, position_);
177 }
178
179 bool NetworkLocationProvider::StartProvider(bool high_accuracy) { 143 bool NetworkLocationProvider::StartProvider(bool high_accuracy) {
180 DCHECK(CalledOnValidThread()); 144 DCHECK(thread_checker_.CalledOnValidThread());
181 if (IsStarted()) 145 if (IsStarted())
182 return true; 146 return true;
183 DCHECK(!wifi_data_provider_manager_); 147 if (!request_->url().is_valid())
184 if (!request_->url().is_valid()) {
185 LOG(WARNING) << "StartProvider() : Failed, Bad URL: "
186 << request_->url().possibly_invalid_spec();
187 return false; 148 return false;
188 }
189 149
190 // Registers a callback with the data provider. The first call to Register 150 // Registers a callback with the data provider. The first call to Register()
191 // will create a singleton data provider and it will be deleted when the last 151 // will create a singleton data provider that will be deleted on Unregister().
192 // callback is removed with Unregister.
193 wifi_data_provider_manager_ = 152 wifi_data_provider_manager_ =
194 WifiDataProviderManager::Register(&wifi_data_update_callback_); 153 WifiDataProviderManager::Register(&wifi_data_update_callback_);
195 154
196 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( 155 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
197 FROM_HERE, base::Bind(&NetworkLocationProvider::RequestPosition, 156 FROM_HERE, base::Bind(&NetworkLocationProvider::RequestPosition,
198 weak_factory_.GetWeakPtr()), 157 weak_factory_.GetWeakPtr()),
199 base::TimeDelta::FromSeconds(kDataCompleteWaitSeconds)); 158 base::TimeDelta::FromSeconds(kDataCompleteWaitSeconds));
200 // Get the wifi data. 159
201 is_wifi_data_complete_ = wifi_data_provider_manager_->GetData(&wifi_data_); 160 OnWifiDataUpdate();
202 if (is_wifi_data_complete_)
203 OnWifiDataUpdated();
204 return true; 161 return true;
205 } 162 }
206 163
207 void NetworkLocationProvider::OnWifiDataUpdated() {
208 DCHECK(CalledOnValidThread());
209 wifi_timestamp_ = base::Time::Now();
210
211 is_new_data_available_ = is_wifi_data_complete_;
212 RequestPosition();
213 }
214
215 void NetworkLocationProvider::StopProvider() { 164 void NetworkLocationProvider::StopProvider() {
216 DCHECK(CalledOnValidThread()); 165 DCHECK(thread_checker_.CalledOnValidThread());
217 if (IsStarted()) { 166 DCHECK(IsStarted());
218 wifi_data_provider_manager_->Unregister(&wifi_data_update_callback_); 167 wifi_data_provider_manager_->Unregister(&wifi_data_update_callback_);
219 }
220 wifi_data_provider_manager_ = nullptr; 168 wifi_data_provider_manager_ = nullptr;
221 weak_factory_.InvalidateWeakPtrs(); 169 weak_factory_.InvalidateWeakPtrs();
222 } 170 }
223 171
224 // Other methods 172 const Geoposition& NetworkLocationProvider::GetPosition() {
173 return position_;
174 }
175
176 void NetworkLocationProvider::OnPermissionGranted() {
177 const bool was_permission_granted = is_permission_granted_;
178 is_permission_granted_ = true;
179 if (!was_permission_granted && IsStarted())
180 RequestPosition();
181 }
182
225 void NetworkLocationProvider::RequestPosition() { 183 void NetworkLocationProvider::RequestPosition() {
226 DCHECK(CalledOnValidThread()); 184 DCHECK(thread_checker_.CalledOnValidThread());
227 185
228 // TODO(mcasas): consider not using HasWeakPtrs() https://crbug.com/629158. 186 if (!is_new_data_available_ || !is_wifi_data_complete_)
229 if (weak_factory_.HasWeakPtrs() && !is_wifi_data_complete_)
230 return; 187 return;
231 if (!is_new_data_available_) 188 DCHECK(!wifi_timestamp_.is_null())
232 return; 189 << "|wifi_timestamp_| must be set before looking up position";
233 190
234 const Geoposition* cached_position = 191 const Geoposition* cached_position =
235 position_cache_->FindPosition(wifi_data_); 192 position_cache_->FindPosition(wifi_data_);
236 DCHECK(!wifi_timestamp_.is_null())
237 << "Timestamp must be set before looking up position";
238 if (cached_position) { 193 if (cached_position) {
239 DCHECK(cached_position->Validate()); 194 DCHECK(cached_position->Validate());
240 // Record the position and update its timestamp. 195 // Record the position and update its timestamp.
241 position_ = *cached_position; 196 position_ = *cached_position;
242 197
243 // The timestamp of a position fix is determined by the timestamp 198 // The timestamp of a position fix is determined by the timestamp
244 // of the source data update. (The value of position_.timestamp from 199 // of the source data update. (The value of position_.timestamp from
245 // the cache could be from weeks ago!) 200 // the cache could be from weeks ago!)
246 position_.timestamp = wifi_timestamp_; 201 position_.timestamp = wifi_timestamp_;
247 is_new_data_available_ = false; 202 is_new_data_available_ = false;
248 203
249 // Let listeners know that we now have a position available. 204 // Let listeners know that we now have a position available.
250 if (!location_provider_update_callback_.is_null()) 205 if (!location_provider_update_callback_.is_null())
251 location_provider_update_callback_.Run(this, position_); 206 location_provider_update_callback_.Run(this, position_);
252 return; 207 return;
253 } 208 }
209
254 // Don't send network requests until authorized. http://crbug.com/39171 210 // Don't send network requests until authorized. http://crbug.com/39171
255 if (!is_permission_granted_) 211 if (!is_permission_granted_)
256 return; 212 return;
257 213
258 weak_factory_.InvalidateWeakPtrs();
259 is_new_data_available_ = false; 214 is_new_data_available_ = false;
scheib 2017/04/18 03:33:17 Describe why we don't need to invalidate weak ptrs
260 215
261 // TODO(joth): Rather than cancel pending requests, we should create a new 216 // TODO(joth): Rather than cancel pending requests, we should create a new
262 // NetworkLocationRequest for each and hold a set of pending requests. 217 // NetworkLocationRequest for each and hold a set of pending requests.
263 if (request_->is_request_pending()) { 218 DLOG_IF(WARNING, request_->is_request_pending())
264 DVLOG(1) << "NetworkLocationProvider - pre-empting pending network request " 219 << "NetworkLocationProvider - pre-empting pending network request "
265 "with new data. Wifi APs: " 220 "with new data. Wifi APs: "
266 << wifi_data_.access_point_data.size(); 221 << wifi_data_.access_point_data.size();
267 } 222
268 request_->MakeRequest(access_token_, wifi_data_, wifi_timestamp_); 223 request_->MakeRequest(access_token_, wifi_data_, wifi_timestamp_);
269 } 224 }
270 225
226 void NetworkLocationProvider::OnWifiDataUpdate() {
227 DCHECK(thread_checker_.CalledOnValidThread());
228 DCHECK(IsStarted());
229 is_wifi_data_complete_ = wifi_data_provider_manager_->GetData(&wifi_data_);
230 if (!is_wifi_data_complete_)
231 return;
232
233 wifi_timestamp_ = base::Time::Now();
234 is_new_data_available_ = true;
235 RequestPosition();
236 }
237
271 bool NetworkLocationProvider::IsStarted() const { 238 bool NetworkLocationProvider::IsStarted() const {
272 return wifi_data_provider_manager_ != nullptr; 239 return wifi_data_provider_manager_ != nullptr;
273 } 240 }
274 241
242 void NetworkLocationProvider::OnLocationResponse(
243 const Geoposition& position,
244 bool server_error,
245 const base::string16& access_token,
246 const WifiData& wifi_data) {
247 DCHECK(thread_checker_.CalledOnValidThread());
248 // Record the position and update our cache.
249 position_ = position;
250 if (position.Validate())
251 position_cache_->CachePosition(wifi_data, position);
252
253 // Record access_token if it's set.
254 if (!access_token.empty() && access_token_ != access_token) {
255 access_token_ = access_token;
256 access_token_store_->SaveAccessToken(request_->url(), access_token);
257 }
258
259 // Let listeners know that we now have a position available.
260 if (!location_provider_update_callback_.is_null())
261 location_provider_update_callback_.Run(this, position_);
262 }
263
275 } // namespace device 264 } // namespace device
OLDNEW
« no previous file with comments | « device/geolocation/network_location_provider.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698