| 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/timezone/timezone_request.h" | 5 #include "chromeos/timezone/timezone_request.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | |
| 9 #include <string> | 8 #include <string> |
| 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "base/metrics/sparse_histogram.h" | 13 #include "base/metrics/sparse_histogram.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 17 #include "base/values.h" | 17 #include "base/values.h" |
| 18 #include "google_apis/google_api_keys.h" | 18 #include "google_apis/google_api_keys.h" |
| 19 #include "net/base/escape.h" | 19 #include "net/base/escape.h" |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 int status_code, | 268 int status_code, |
| 269 const std::string& response_body, | 269 const std::string& response_body, |
| 270 const GURL& server_url) { | 270 const GURL& server_url) { |
| 271 scoped_ptr<TimeZoneResponseData> timezone(new TimeZoneResponseData); | 271 scoped_ptr<TimeZoneResponseData> timezone(new TimeZoneResponseData); |
| 272 | 272 |
| 273 // HttpPost can fail for a number of reasons. Most likely this is because | 273 // HttpPost can fail for a number of reasons. Most likely this is because |
| 274 // we're offline, or there was no response. | 274 // we're offline, or there was no response. |
| 275 if (!http_success) { | 275 if (!http_success) { |
| 276 PrintTimeZoneError(server_url, "No response received", timezone.get()); | 276 PrintTimeZoneError(server_url, "No response received", timezone.get()); |
| 277 RecordUmaEvent(TIMEZONE_REQUEST_EVENT_RESPONSE_EMPTY); | 277 RecordUmaEvent(TIMEZONE_REQUEST_EVENT_RESPONSE_EMPTY); |
| 278 return timezone.Pass(); | 278 return timezone; |
| 279 } | 279 } |
| 280 if (status_code != net::HTTP_OK) { | 280 if (status_code != net::HTTP_OK) { |
| 281 std::string message = "Returned error code "; | 281 std::string message = "Returned error code "; |
| 282 message += base::IntToString(status_code); | 282 message += base::IntToString(status_code); |
| 283 PrintTimeZoneError(server_url, message, timezone.get()); | 283 PrintTimeZoneError(server_url, message, timezone.get()); |
| 284 RecordUmaEvent(TIMEZONE_REQUEST_EVENT_RESPONSE_NOT_OK); | 284 RecordUmaEvent(TIMEZONE_REQUEST_EVENT_RESPONSE_NOT_OK); |
| 285 return timezone.Pass(); | 285 return timezone; |
| 286 } | 286 } |
| 287 | 287 |
| 288 if (!ParseServerResponse(server_url, response_body, timezone.get())) | 288 if (!ParseServerResponse(server_url, response_body, timezone.get())) |
| 289 return timezone.Pass(); | 289 return timezone; |
| 290 | 290 |
| 291 RecordUmaEvent(TIMEZONE_REQUEST_EVENT_RESPONSE_SUCCESS); | 291 RecordUmaEvent(TIMEZONE_REQUEST_EVENT_RESPONSE_SUCCESS); |
| 292 return timezone.Pass(); | 292 return timezone; |
| 293 } | 293 } |
| 294 | 294 |
| 295 } // namespace | 295 } // namespace |
| 296 | 296 |
| 297 TimeZoneResponseData::TimeZoneResponseData() | 297 TimeZoneResponseData::TimeZoneResponseData() |
| 298 : dstOffset(0), rawOffset(0), status(ZERO_RESULTS) { | 298 : dstOffset(0), rawOffset(0), status(ZERO_RESULTS) { |
| 299 } | 299 } |
| 300 | 300 |
| 301 GURL DefaultTimezoneProviderURL() { | 301 GURL DefaultTimezoneProviderURL() { |
| 302 return GURL(kDefaultTimezoneProviderUrl); | 302 return GURL(kDefaultTimezoneProviderUrl); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 393 : TIMEZONE_REQUEST_RESULT_FAILURE)); | 393 : TIMEZONE_REQUEST_RESULT_FAILURE)); |
| 394 RecordUmaResult(result, retries_); | 394 RecordUmaResult(result, retries_); |
| 395 | 395 |
| 396 TimeZoneResponseCallback callback = callback_; | 396 TimeZoneResponseCallback callback = callback_; |
| 397 | 397 |
| 398 // Empty callback is used to identify "completed or not yet started request". | 398 // Empty callback is used to identify "completed or not yet started request". |
| 399 callback_.Reset(); | 399 callback_.Reset(); |
| 400 | 400 |
| 401 // callback.Run() usually destroys TimeZoneRequest, because this is the way | 401 // callback.Run() usually destroys TimeZoneRequest, because this is the way |
| 402 // callback is implemented in TimeZoneProvider. | 402 // callback is implemented in TimeZoneProvider. |
| 403 callback.Run(timezone.Pass(), server_error); | 403 callback.Run(std::move(timezone), server_error); |
| 404 // "this" is already destroyed here. | 404 // "this" is already destroyed here. |
| 405 } | 405 } |
| 406 | 406 |
| 407 std::string TimeZoneResponseData::ToStringForDebug() const { | 407 std::string TimeZoneResponseData::ToStringForDebug() const { |
| 408 static const char* const status2string[] = { | 408 static const char* const status2string[] = { |
| 409 "OK", | 409 "OK", |
| 410 "INVALID_REQUEST", | 410 "INVALID_REQUEST", |
| 411 "OVER_QUERY_LIMIT", | 411 "OVER_QUERY_LIMIT", |
| 412 "REQUEST_DENIED", | 412 "REQUEST_DENIED", |
| 413 "UNKNOWN_ERROR", | 413 "UNKNOWN_ERROR", |
| 414 "ZERO_RESULTS", | 414 "ZERO_RESULTS", |
| 415 "REQUEST_ERROR" | 415 "REQUEST_ERROR" |
| 416 }; | 416 }; |
| 417 | 417 |
| 418 return base::StringPrintf( | 418 return base::StringPrintf( |
| 419 "dstOffset=%f, rawOffset=%f, timeZoneId='%s', timeZoneName='%s', " | 419 "dstOffset=%f, rawOffset=%f, timeZoneId='%s', timeZoneName='%s', " |
| 420 "error_message='%s', status=%u (%s)", | 420 "error_message='%s', status=%u (%s)", |
| 421 dstOffset, | 421 dstOffset, |
| 422 rawOffset, | 422 rawOffset, |
| 423 timeZoneId.c_str(), | 423 timeZoneId.c_str(), |
| 424 timeZoneName.c_str(), | 424 timeZoneName.c_str(), |
| 425 error_message.c_str(), | 425 error_message.c_str(), |
| 426 (unsigned)status, | 426 (unsigned)status, |
| 427 (status < arraysize(status2string) ? status2string[status] : "unknown")); | 427 (status < arraysize(status2string) ? status2string[status] : "unknown")); |
| 428 } | 428 } |
| 429 | 429 |
| 430 } // namespace chromeos | 430 } // namespace chromeos |
| OLD | NEW |