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

Side by Side Diff: chromeos/geolocation/simple_geolocation_request.cc

Issue 2624843003: Add support for cellular geolocation (Closed)
Patch Set: Add support for cellular geolocation Created 3 years, 10 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "chromeos/geolocation/simple_geolocation_request.h" 5 #include "chromeos/geolocation/simple_geolocation_request.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <string> 10 #include <string>
(...skipping 26 matching lines...) Expand all
37 base::TimeDelta::FromMinutes(2), \ 37 base::TimeDelta::FromMinutes(2), \
38 50) 38 50)
39 39
40 namespace chromeos { 40 namespace chromeos {
41 41
42 namespace { 42 namespace {
43 43
44 // The full request text. (no parameters are supported by now) 44 // The full request text. (no parameters are supported by now)
45 const char kSimpleGeolocationRequestBody[] = "{\"considerIp\": \"true\"}"; 45 const char kSimpleGeolocationRequestBody[] = "{\"considerIp\": \"true\"}";
46 46
47 // Request data 47 // TODO(skylarc): kill these and use dbus-constants instead?
48 // Top-level request data fields
stevenjb 2017/02/03 02:12:02 That would be nice :) In the meanwhile, we should
can Skylar cook 2017/02/03 21:47:06 Done.
48 const char kConsiderIp[] = "considerIp"; 49 const char kConsiderIp[] = "considerIp";
49 const char kWifiAccessPoints[] = "wifiAccessPoints"; 50 const char kWifiAccessPoints[] = "wifiAccessPoints";
51 const char kCellTowers[] = "cellTowers";
52
53 // Shared Wifi and Cell Tower objects
54 const char kAge[] = "age";
55 const char kSignalStrength[] = "signalStrength";
50 56
51 // WiFi access point objects. 57 // WiFi access point objects.
52 const char kMacAddress[] = "macAddress"; 58 const char kMacAddress[] = "macAddress";
53 const char kSignalStrength[] = "signalStrength";
54 const char kAge[] = "age";
55 const char kChannel[] = "channel"; 59 const char kChannel[] = "channel";
56 const char kSignalToNoiseRatio[] = "signalToNoiseRatio"; 60 const char kSignalToNoiseRatio[] = "signalToNoiseRatio";
57 61
62 // Cell tower objects
63 const char kCellId[] = "cellId";
64 const char kLocationAreaCode[] = "locationAreaCode";
65 const char kMobileCountryCode[] = "mobileCountryCode";
66 const char kMobileNetworkCode[] = "mobileNetworkCode";
67
58 // Response data. 68 // Response data.
59 const char kLocationString[] = "location"; 69 const char kLocationString[] = "location";
60 const char kLatString[] = "lat"; 70 const char kLatString[] = "lat";
61 const char kLngString[] = "lng"; 71 const char kLngString[] = "lng";
62 const char kAccuracyString[] = "accuracy"; 72 const char kAccuracyString[] = "accuracy";
63 // Error object and its contents. 73 // Error object and its contents.
64 const char kErrorString[] = "error"; 74 const char kErrorString[] = "error";
65 // "errors" array in "erorr" object is ignored. 75 // "errors" array in "erorr" object is ignored.
66 const char kCodeString[] = "code"; 76 const char kCodeString[] = "code";
67 const char kMessageString[] = "message"; 77 const char kMessageString[] = "message";
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 RecordUmaEvent(SIMPLE_GEOLOCATION_REQUEST_EVENT_RESPONSE_NOT_OK); 294 RecordUmaEvent(SIMPLE_GEOLOCATION_REQUEST_EVENT_RESPONSE_NOT_OK);
285 return false; 295 return false;
286 } 296 }
287 297
288 return ParseServerResponse(server_url, response_body, position); 298 return ParseServerResponse(server_url, response_body, position);
289 } 299 }
290 300
291 void ReportUmaHasWiFiAccessPoints(bool value) { 301 void ReportUmaHasWiFiAccessPoints(bool value) {
292 UMA_HISTOGRAM_BOOLEAN("SimpleGeolocation.Request.HasWiFiAccessPoints", value); 302 UMA_HISTOGRAM_BOOLEAN("SimpleGeolocation.Request.HasWiFiAccessPoints", value);
293 } 303 }
304 void ReportUmaHasCellTowers(bool value) {
305 UMA_HISTOGRAM_BOOLEAN("SimpleGeolocation.Request.HasCellTowers", value);
306 }
294 307
295 } // namespace 308 } // namespace
296 309
297 SimpleGeolocationRequest::SimpleGeolocationRequest( 310 SimpleGeolocationRequest::SimpleGeolocationRequest(
298 net::URLRequestContextGetter* url_context_getter, 311 net::URLRequestContextGetter* url_context_getter,
299 const GURL& service_url, 312 const GURL& service_url,
300 base::TimeDelta timeout, 313 base::TimeDelta timeout,
301 std::unique_ptr<WifiAccessPointVector> wifi_data) 314 std::unique_ptr<WifiAccessPointVector> wifi_data,
315 std::unique_ptr<CellTowerVector> cell_tower_data)
302 : url_context_getter_(url_context_getter), 316 : url_context_getter_(url_context_getter),
303 service_url_(service_url), 317 service_url_(service_url),
304 retry_sleep_on_server_error_(base::TimeDelta::FromSeconds( 318 retry_sleep_on_server_error_(base::TimeDelta::FromSeconds(
305 kResolveGeolocationRetrySleepOnServerErrorSeconds)), 319 kResolveGeolocationRetrySleepOnServerErrorSeconds)),
306 retry_sleep_on_bad_response_(base::TimeDelta::FromSeconds( 320 retry_sleep_on_bad_response_(base::TimeDelta::FromSeconds(
307 kResolveGeolocationRetrySleepBadResponseSeconds)), 321 kResolveGeolocationRetrySleepBadResponseSeconds)),
308 timeout_(timeout), 322 timeout_(timeout),
309 retries_(0), 323 retries_(0),
310 wifi_data_(wifi_data.release()) {} 324 wifi_data_(wifi_data.release()),
325 cell_tower_data_(cell_tower_data.release()) {}
311 326
312 SimpleGeolocationRequest::~SimpleGeolocationRequest() { 327 SimpleGeolocationRequest::~SimpleGeolocationRequest() {
313 DCHECK(thread_checker_.CalledOnValidThread()); 328 DCHECK(thread_checker_.CalledOnValidThread());
314 329
315 // If callback is not empty, request is cancelled. 330 // If callback is not empty, request is cancelled.
316 if (!callback_.is_null()) { 331 if (!callback_.is_null()) {
317 RecordUmaResponseTime(base::Time::Now() - request_started_at_, false); 332 RecordUmaResponseTime(base::Time::Now() - request_started_at_, false);
318 RecordUmaResult(SIMPLE_GEOLOCATION_REQUEST_RESULT_CANCELLED, retries_); 333 RecordUmaResult(SIMPLE_GEOLOCATION_REQUEST_RESULT_CANCELLED, retries_);
319 } 334 }
320 335
321 if (g_test_request_hook) 336 if (g_test_request_hook)
322 g_test_request_hook->OnRequestCreated(this); 337 g_test_request_hook->OnRequestCreated(this);
323 } 338 }
324 339
325 std::string SimpleGeolocationRequest::FormatRequestBody() const { 340 std::string SimpleGeolocationRequest::FormatRequestBody() const {
326 if (!wifi_data_) { 341 if (!wifi_data_)
327 ReportUmaHasWiFiAccessPoints(false); 342 ReportUmaHasWiFiAccessPoints(false);
343
344 if (!cell_tower_data_)
345 ReportUmaHasCellTowers(false);
346
347 if (!cell_tower_data_ && !wifi_data_)
328 return std::string(kSimpleGeolocationRequestBody); 348 return std::string(kSimpleGeolocationRequestBody);
329 }
330 349
331 std::unique_ptr<base::DictionaryValue> request(new base::DictionaryValue); 350 std::unique_ptr<base::DictionaryValue> request(new base::DictionaryValue);
332 request->SetBooleanWithoutPathExpansion(kConsiderIp, true); 351 request->SetBooleanWithoutPathExpansion(kConsiderIp, true);
333 352
334 base::ListValue* wifi_access_points(new base::ListValue); 353 if (wifi_data_) {
335 request->SetWithoutPathExpansion(kWifiAccessPoints, wifi_access_points); 354 base::ListValue* wifi_access_points(new base::ListValue);
355 request->SetWithoutPathExpansion(kWifiAccessPoints, wifi_access_points);
stevenjb 2017/02/03 02:12:02 This is deprecated; use a unique_ptr to hold wifi_
can Skylar cook 2017/02/03 21:47:06 Whoops, meant to change that after Ben's comment o
356 for (const WifiAccessPoint& access_point : *wifi_data_) {
357 auto access_point_dictionary = base::MakeUnique<base::DictionaryValue>();
336 358
337 for (const WifiAccessPoint& access_point : *wifi_data_) { 359 access_point_dictionary->SetStringWithoutPathExpansion(
338 auto access_point_dictionary = base::MakeUnique<base::DictionaryValue>(); 360 kMacAddress, access_point.mac_address);
361 access_point_dictionary->SetIntegerWithoutPathExpansion(
362 kSignalStrength, access_point.signal_strength);
363 if (!access_point.timestamp.is_null()) {
364 access_point_dictionary->SetStringWithoutPathExpansion(
365 kAge,
366 base::Int64ToString(
367 (base::Time::Now() - access_point.timestamp).InMilliseconds()));
368 }
339 369
340 access_point_dictionary->SetStringWithoutPathExpansion( 370 access_point_dictionary->SetIntegerWithoutPathExpansion(
341 kMacAddress, access_point.mac_address); 371 kChannel, access_point.channel);
342 access_point_dictionary->SetIntegerWithoutPathExpansion( 372 access_point_dictionary->SetIntegerWithoutPathExpansion(
343 kSignalStrength, access_point.signal_strength); 373 kSignalToNoiseRatio, access_point.signal_to_noise);
stevenjb 2017/02/03 02:12:02 nit: This would be more readable with the above pu
can Skylar cook 2017/02/03 21:47:06 Done.
344 if (!access_point.timestamp.is_null()) { 374
345 access_point_dictionary->SetStringWithoutPathExpansion( 375 wifi_access_points->Append(std::move(access_point_dictionary));
346 kAge,
347 base::Int64ToString(
348 (base::Time::Now() - access_point.timestamp).InMilliseconds()));
349 } 376 }
377 }
350 378
351 access_point_dictionary->SetIntegerWithoutPathExpansion( 379 if (cell_tower_data_) {
352 kChannel, access_point.channel); 380 auto cell_towers = base::MakeUnique<base::ListValue>();
stevenjb 2017/02/03 02:12:02 Like this :)
can Skylar cook 2017/02/03 21:47:06 Acknowledged.
353 access_point_dictionary->SetIntegerWithoutPathExpansion(
354 kSignalToNoiseRatio, access_point.signal_to_noise);
355 381
356 wifi_access_points->Append(std::move(access_point_dictionary)); 382 for (const CellTower& cell_tower : *cell_tower_data_) {
383 auto cell_tower_dictionary = base::MakeUnique<base::DictionaryValue>();
384 cell_tower_dictionary->SetStringWithoutPathExpansion(kCellId,
385 cell_tower.ci);
386 cell_tower_dictionary->SetStringWithoutPathExpansion(kLocationAreaCode,
387 cell_tower.lac);
388 cell_tower_dictionary->SetStringWithoutPathExpansion(kMobileCountryCode,
389 cell_tower.mcc);
390 cell_tower_dictionary->SetStringWithoutPathExpansion(kMobileNetworkCode,
391 cell_tower.mnc);
392
393 if (!cell_tower.timestamp.is_null()) {
394 cell_tower_dictionary->SetStringWithoutPathExpansion(
395 kAge,
396 base::Int64ToString(
397 (base::Time::Now() - cell_tower.timestamp).InMilliseconds()));
398 }
stevenjb 2017/02/03 02:12:02 Ditto about moving the above to a helper function.
can Skylar cook 2017/02/03 21:47:06 Done.
399 cell_towers->Append(std::move(cell_tower_dictionary));
400 request->SetWithoutPathExpansion(kCellTowers, std::move(cell_towers));
401 }
357 } 402 }
403
358 std::string result; 404 std::string result;
359 if (!base::JSONWriter::Write(*request, &result)) { 405 if (!base::JSONWriter::Write(*request, &result)) {
360 ReportUmaHasWiFiAccessPoints(false); 406 // If there's no data for a network type, we will have already reported
407 // false above
408 if (wifi_data_)
409 ReportUmaHasWiFiAccessPoints(false);
410 if (cell_tower_data_)
411 ReportUmaHasCellTowers(false);
412
361 return std::string(kSimpleGeolocationRequestBody); 413 return std::string(kSimpleGeolocationRequestBody);
362 } 414 }
363 ReportUmaHasWiFiAccessPoints(wifi_data_->size()); 415
416 if (wifi_data_)
417 ReportUmaHasWiFiAccessPoints(wifi_data_->size());
418 if (cell_tower_data_)
419 ReportUmaHasCellTowers(cell_tower_data_->size());
364 420
365 return result; 421 return result;
366 } 422 }
367 423
368 void SimpleGeolocationRequest::StartRequest() { 424 void SimpleGeolocationRequest::StartRequest() {
369 DCHECK(thread_checker_.CalledOnValidThread()); 425 DCHECK(thread_checker_.CalledOnValidThread());
370 RecordUmaEvent(SIMPLE_GEOLOCATION_REQUEST_EVENT_REQUEST_START); 426 RecordUmaEvent(SIMPLE_GEOLOCATION_REQUEST_EVENT_REQUEST_START);
371 ++retries_; 427 ++retries_;
372 428
373 const std::string request_body = FormatRequestBody(); 429 const std::string request_body = FormatRequestBody();
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
474 ? SIMPLE_GEOLOCATION_REQUEST_RESULT_SERVER_ERROR 530 ? SIMPLE_GEOLOCATION_REQUEST_RESULT_SERVER_ERROR
475 : SIMPLE_GEOLOCATION_REQUEST_RESULT_FAILURE); 531 : SIMPLE_GEOLOCATION_REQUEST_RESULT_FAILURE);
476 RecordUmaResult(result, retries_); 532 RecordUmaResult(result, retries_);
477 position_.status = Geoposition::STATUS_TIMEOUT; 533 position_.status = Geoposition::STATUS_TIMEOUT;
478 const base::TimeDelta elapsed = base::Time::Now() - request_started_at_; 534 const base::TimeDelta elapsed = base::Time::Now() - request_started_at_;
479 ReplyAndDestroySelf(elapsed, true /* server_error */); 535 ReplyAndDestroySelf(elapsed, true /* server_error */);
480 // "this" is already destroyed here. 536 // "this" is already destroyed here.
481 } 537 }
482 538
483 } // namespace chromeos 539 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698