Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 | |
| 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 // const char kTimingAdvance[] = "timingAdvance"; | |
|
Ben Chan
2017/02/02 20:49:24
either drop this line, or add a TODO for it
comme
can Skylar cook
2017/02/03 00:15:17
Done.
| |
| 68 | |
| 58 // Response data. | 69 // Response data. |
| 59 const char kLocationString[] = "location"; | 70 const char kLocationString[] = "location"; |
| 60 const char kLatString[] = "lat"; | 71 const char kLatString[] = "lat"; |
| 61 const char kLngString[] = "lng"; | 72 const char kLngString[] = "lng"; |
| 62 const char kAccuracyString[] = "accuracy"; | 73 const char kAccuracyString[] = "accuracy"; |
| 63 // Error object and its contents. | 74 // Error object and its contents. |
| 64 const char kErrorString[] = "error"; | 75 const char kErrorString[] = "error"; |
| 65 // "errors" array in "erorr" object is ignored. | 76 // "errors" array in "erorr" object is ignored. |
| 66 const char kCodeString[] = "code"; | 77 const char kCodeString[] = "code"; |
| 67 const char kMessageString[] = "message"; | 78 const char kMessageString[] = "message"; |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 284 RecordUmaEvent(SIMPLE_GEOLOCATION_REQUEST_EVENT_RESPONSE_NOT_OK); | 295 RecordUmaEvent(SIMPLE_GEOLOCATION_REQUEST_EVENT_RESPONSE_NOT_OK); |
| 285 return false; | 296 return false; |
| 286 } | 297 } |
| 287 | 298 |
| 288 return ParseServerResponse(server_url, response_body, position); | 299 return ParseServerResponse(server_url, response_body, position); |
| 289 } | 300 } |
| 290 | 301 |
| 291 void ReportUmaHasWiFiAccessPoints(bool value) { | 302 void ReportUmaHasWiFiAccessPoints(bool value) { |
| 292 UMA_HISTOGRAM_BOOLEAN("SimpleGeolocation.Request.HasWiFiAccessPoints", value); | 303 UMA_HISTOGRAM_BOOLEAN("SimpleGeolocation.Request.HasWiFiAccessPoints", value); |
| 293 } | 304 } |
| 305 void ReportUmaHasCellTowers(bool value) { | |
| 306 UMA_HISTOGRAM_BOOLEAN("SimpleGeolocation.Request.HasCellTowers", value); | |
| 307 } | |
| 294 | 308 |
| 295 } // namespace | 309 } // namespace |
| 296 | 310 |
| 297 SimpleGeolocationRequest::SimpleGeolocationRequest( | 311 SimpleGeolocationRequest::SimpleGeolocationRequest( |
| 298 net::URLRequestContextGetter* url_context_getter, | 312 net::URLRequestContextGetter* url_context_getter, |
| 299 const GURL& service_url, | 313 const GURL& service_url, |
| 300 base::TimeDelta timeout, | 314 base::TimeDelta timeout, |
| 301 std::unique_ptr<WifiAccessPointVector> wifi_data) | 315 std::unique_ptr<WifiAccessPointVector> wifi_data, |
| 316 std::unique_ptr<CellTowerVector> cell_tower_data) | |
| 302 : url_context_getter_(url_context_getter), | 317 : url_context_getter_(url_context_getter), |
| 303 service_url_(service_url), | 318 service_url_(service_url), |
| 304 retry_sleep_on_server_error_(base::TimeDelta::FromSeconds( | 319 retry_sleep_on_server_error_(base::TimeDelta::FromSeconds( |
| 305 kResolveGeolocationRetrySleepOnServerErrorSeconds)), | 320 kResolveGeolocationRetrySleepOnServerErrorSeconds)), |
| 306 retry_sleep_on_bad_response_(base::TimeDelta::FromSeconds( | 321 retry_sleep_on_bad_response_(base::TimeDelta::FromSeconds( |
| 307 kResolveGeolocationRetrySleepBadResponseSeconds)), | 322 kResolveGeolocationRetrySleepBadResponseSeconds)), |
| 308 timeout_(timeout), | 323 timeout_(timeout), |
| 309 retries_(0), | 324 retries_(0), |
| 310 wifi_data_(wifi_data.release()) {} | 325 wifi_data_(wifi_data.release()), |
| 326 cell_tower_data_(cell_tower_data.release()) {} | |
| 311 | 327 |
| 312 SimpleGeolocationRequest::~SimpleGeolocationRequest() { | 328 SimpleGeolocationRequest::~SimpleGeolocationRequest() { |
| 313 DCHECK(thread_checker_.CalledOnValidThread()); | 329 DCHECK(thread_checker_.CalledOnValidThread()); |
| 314 | 330 |
| 315 // If callback is not empty, request is cancelled. | 331 // If callback is not empty, request is cancelled. |
| 316 if (!callback_.is_null()) { | 332 if (!callback_.is_null()) { |
| 317 RecordUmaResponseTime(base::Time::Now() - request_started_at_, false); | 333 RecordUmaResponseTime(base::Time::Now() - request_started_at_, false); |
| 318 RecordUmaResult(SIMPLE_GEOLOCATION_REQUEST_RESULT_CANCELLED, retries_); | 334 RecordUmaResult(SIMPLE_GEOLOCATION_REQUEST_RESULT_CANCELLED, retries_); |
| 319 } | 335 } |
| 320 | 336 |
| 321 if (g_test_request_hook) | 337 if (g_test_request_hook) |
| 322 g_test_request_hook->OnRequestCreated(this); | 338 g_test_request_hook->OnRequestCreated(this); |
| 323 } | 339 } |
| 324 | 340 |
| 325 std::string SimpleGeolocationRequest::FormatRequestBody() const { | 341 std::string SimpleGeolocationRequest::FormatRequestBody() const { |
| 326 if (!wifi_data_) { | 342 if (!wifi_data_) |
| 327 ReportUmaHasWiFiAccessPoints(false); | 343 ReportUmaHasWiFiAccessPoints(false); |
| 344 | |
| 345 if (!cell_tower_data_) | |
| 346 ReportUmaHasCellTowers(false); | |
| 347 | |
| 348 if (!cell_tower_data_ && !wifi_data_) | |
| 328 return std::string(kSimpleGeolocationRequestBody); | 349 return std::string(kSimpleGeolocationRequestBody); |
| 329 } | |
| 330 | 350 |
| 331 std::unique_ptr<base::DictionaryValue> request(new base::DictionaryValue); | 351 std::unique_ptr<base::DictionaryValue> request(new base::DictionaryValue); |
| 332 request->SetBooleanWithoutPathExpansion(kConsiderIp, true); | 352 request->SetBooleanWithoutPathExpansion(kConsiderIp, true); |
| 333 | 353 |
| 334 base::ListValue* wifi_access_points(new base::ListValue); | 354 if (wifi_data_) { |
| 335 request->SetWithoutPathExpansion(kWifiAccessPoints, wifi_access_points); | 355 base::ListValue* wifi_access_points(new base::ListValue); |
| 356 request->SetWithoutPathExpansion(kWifiAccessPoints, wifi_access_points); | |
| 357 for (const WifiAccessPoint& access_point : *wifi_data_) { | |
| 358 auto access_point_dictionary = base::MakeUnique<base::DictionaryValue>(); | |
| 336 | 359 |
| 337 for (const WifiAccessPoint& access_point : *wifi_data_) { | 360 access_point_dictionary->SetStringWithoutPathExpansion( |
| 338 auto access_point_dictionary = base::MakeUnique<base::DictionaryValue>(); | 361 kMacAddress, access_point.mac_address); |
| 362 access_point_dictionary->SetIntegerWithoutPathExpansion( | |
| 363 kSignalStrength, access_point.signal_strength); | |
| 364 if (!access_point.timestamp.is_null()) { | |
| 365 access_point_dictionary->SetStringWithoutPathExpansion( | |
| 366 kAge, | |
| 367 base::Int64ToString( | |
| 368 (base::Time::Now() - access_point.timestamp).InMilliseconds())); | |
| 369 } | |
| 339 | 370 |
| 340 access_point_dictionary->SetStringWithoutPathExpansion( | 371 access_point_dictionary->SetIntegerWithoutPathExpansion( |
| 341 kMacAddress, access_point.mac_address); | 372 kChannel, access_point.channel); |
| 342 access_point_dictionary->SetIntegerWithoutPathExpansion( | 373 access_point_dictionary->SetIntegerWithoutPathExpansion( |
| 343 kSignalStrength, access_point.signal_strength); | 374 kSignalToNoiseRatio, access_point.signal_to_noise); |
| 344 if (!access_point.timestamp.is_null()) { | 375 |
| 345 access_point_dictionary->SetStringWithoutPathExpansion( | 376 wifi_access_points->Append(std::move(access_point_dictionary)); |
| 346 kAge, | |
| 347 base::Int64ToString( | |
| 348 (base::Time::Now() - access_point.timestamp).InMilliseconds())); | |
| 349 } | 377 } |
| 378 } | |
| 350 | 379 |
| 351 access_point_dictionary->SetIntegerWithoutPathExpansion( | 380 if (cell_tower_data_) { |
| 352 kChannel, access_point.channel); | 381 base::ListValue* cell_towers(new base::ListValue); |
|
Ben Chan
2017/02/02 20:49:24
keep it in unique_ptr
auto cell_towers = base::Ma
can Skylar cook
2017/02/03 00:15:18
Done.
| |
| 353 access_point_dictionary->SetIntegerWithoutPathExpansion( | 382 request->SetWithoutPathExpansion(kCellTowers, cell_towers); |
| 354 kSignalToNoiseRatio, access_point.signal_to_noise); | |
| 355 | 383 |
| 356 wifi_access_points->Append(std::move(access_point_dictionary)); | 384 for (const CellTower& cell_tower : *cell_tower_data_) { |
| 385 auto cell_tower_dictionary = base::MakeUnique<base::DictionaryValue>(); | |
| 386 cell_tower_dictionary->SetStringWithoutPathExpansion(kCellId, | |
| 387 cell_tower.ci); | |
| 388 cell_tower_dictionary->SetStringWithoutPathExpansion(kLocationAreaCode, | |
| 389 cell_tower.lac); | |
| 390 cell_tower_dictionary->SetStringWithoutPathExpansion(kMobileCountryCode, | |
| 391 cell_tower.mcc); | |
| 392 cell_tower_dictionary->SetStringWithoutPathExpansion(kMobileNetworkCode, | |
| 393 cell_tower.mnc); | |
| 394 | |
| 395 if (!cell_tower.timestamp.is_null()) { | |
| 396 cell_tower_dictionary->SetStringWithoutPathExpansion( | |
| 397 kAge, | |
| 398 base::Int64ToString( | |
| 399 (base::Time::Now() - cell_tower.timestamp).InMilliseconds())); | |
| 400 } | |
| 401 cell_towers->Append(std::move(cell_tower_dictionary)); | |
| 402 } | |
| 357 } | 403 } |
| 404 | |
| 358 std::string result; | 405 std::string result; |
| 359 if (!base::JSONWriter::Write(*request, &result)) { | 406 if (!base::JSONWriter::Write(*request, &result)) { |
| 360 ReportUmaHasWiFiAccessPoints(false); | 407 // If there's no data for a network type, we will have already reported |
| 408 // false above | |
| 409 if (wifi_data_) | |
| 410 ReportUmaHasWiFiAccessPoints(false); | |
| 411 if (cell_tower_data_) | |
| 412 ReportUmaHasCellTowers(false); | |
| 413 | |
| 361 return std::string(kSimpleGeolocationRequestBody); | 414 return std::string(kSimpleGeolocationRequestBody); |
| 362 } | 415 } |
| 363 ReportUmaHasWiFiAccessPoints(wifi_data_->size()); | 416 |
| 417 if (wifi_data_) | |
| 418 ReportUmaHasWiFiAccessPoints(wifi_data_->size()); | |
| 419 if (cell_tower_data_) | |
| 420 ReportUmaHasCellTowers(cell_tower_data_->size()); | |
| 364 | 421 |
| 365 return result; | 422 return result; |
| 366 } | 423 } |
| 367 | 424 |
| 368 void SimpleGeolocationRequest::StartRequest() { | 425 void SimpleGeolocationRequest::StartRequest() { |
| 369 DCHECK(thread_checker_.CalledOnValidThread()); | 426 DCHECK(thread_checker_.CalledOnValidThread()); |
| 370 RecordUmaEvent(SIMPLE_GEOLOCATION_REQUEST_EVENT_REQUEST_START); | 427 RecordUmaEvent(SIMPLE_GEOLOCATION_REQUEST_EVENT_REQUEST_START); |
| 371 ++retries_; | 428 ++retries_; |
| 372 | 429 |
| 373 const std::string request_body = FormatRequestBody(); | 430 const std::string request_body = FormatRequestBody(); |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 474 ? SIMPLE_GEOLOCATION_REQUEST_RESULT_SERVER_ERROR | 531 ? SIMPLE_GEOLOCATION_REQUEST_RESULT_SERVER_ERROR |
| 475 : SIMPLE_GEOLOCATION_REQUEST_RESULT_FAILURE); | 532 : SIMPLE_GEOLOCATION_REQUEST_RESULT_FAILURE); |
| 476 RecordUmaResult(result, retries_); | 533 RecordUmaResult(result, retries_); |
| 477 position_.status = Geoposition::STATUS_TIMEOUT; | 534 position_.status = Geoposition::STATUS_TIMEOUT; |
| 478 const base::TimeDelta elapsed = base::Time::Now() - request_started_at_; | 535 const base::TimeDelta elapsed = base::Time::Now() - request_started_at_; |
| 479 ReplyAndDestroySelf(elapsed, true /* server_error */); | 536 ReplyAndDestroySelf(elapsed, true /* server_error */); |
| 480 // "this" is already destroyed here. | 537 // "this" is already destroyed here. |
| 481 } | 538 } |
| 482 | 539 |
| 483 } // namespace chromeos | 540 } // namespace chromeos |
| OLD | NEW |