| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "google_apis/gcm/engine/gcm_unregistration_request_handler.h" | 5 #include "google_apis/gcm/engine/gcm_unregistration_request_handler.h" |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 9 #include "google_apis/gcm/base/gcm_util.h" | 9 #include "google_apis/gcm/base/gcm_util.h" |
| 10 #include "net/url_request/url_fetcher.h" | 10 #include "net/url_request/url_fetcher.h" |
| 11 | 11 |
| 12 namespace gcm { | 12 namespace gcm { |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 // Request constants. | 16 // Request constants. |
| 17 const char kUnregistrationCallerKey[] = "gcm_unreg_caller"; | 17 const char kUnregistrationCallerKey[] = "gcm_unreg_caller"; |
| 18 // We are going to set the value to "false" in order to forcefully unregister | 18 // We are going to set the value to "false" in order to forcefully unregister |
| 19 // the application. | 19 // the application. |
| 20 const char kUnregistrationCallerValue[] = "false"; | 20 const char kUnregistrationCallerValue[] = "false"; |
| 21 | 21 |
| 22 // Response constants. | 22 // Response constants. |
| 23 const char kDeletedPrefix[] = "deleted="; | 23 const char kDeletedPrefix[] = "deleted="; |
| 24 const char kErrorPrefix[] = "Error="; | |
| 25 const char kInvalidParameters[] = "INVALID_PARAMETERS"; | |
| 26 | 24 |
| 27 } // namespace | 25 } // namespace |
| 28 | 26 |
| 29 GCMUnregistrationRequestHandler::GCMUnregistrationRequestHandler( | 27 GCMUnregistrationRequestHandler::GCMUnregistrationRequestHandler( |
| 30 const std::string& app_id) | 28 const std::string& app_id) |
| 31 : app_id_(app_id) { | 29 : app_id_(app_id) { |
| 32 } | 30 } |
| 33 | 31 |
| 34 GCMUnregistrationRequestHandler::~GCMUnregistrationRequestHandler() {} | 32 GCMUnregistrationRequestHandler::~GCMUnregistrationRequestHandler() {} |
| 35 | 33 |
| 36 void GCMUnregistrationRequestHandler::BuildRequestBody(std::string* body){ | 34 void GCMUnregistrationRequestHandler::BuildRequestBody(std::string* body){ |
| 37 BuildFormEncoding(kUnregistrationCallerKey, kUnregistrationCallerValue, body); | 35 BuildFormEncoding(kUnregistrationCallerKey, kUnregistrationCallerValue, body); |
| 38 } | 36 } |
| 39 | 37 |
| 40 UnregistrationRequest::Status GCMUnregistrationRequestHandler::ParseResponse( | 38 UnregistrationRequest::Status GCMUnregistrationRequestHandler::ParseResponse( |
| 41 const net::URLFetcher* source) { | 39 std::string response) { |
| 42 std::string response; | |
| 43 if (!source->GetResponseAsString(&response)) { | |
| 44 DVLOG(1) << "Failed to get response body."; | |
| 45 return UnregistrationRequest::NO_RESPONSE_BODY; | |
| 46 } | |
| 47 | |
| 48 DVLOG(1) << "Parsing unregistration response."; | 40 DVLOG(1) << "Parsing unregistration response."; |
| 49 if (response.find(kDeletedPrefix) != std::string::npos) { | 41 if (response.find(kDeletedPrefix) != std::string::npos) { |
| 50 std::string deleted_app_id = response.substr( | 42 std::string deleted_app_id = response.substr( |
| 51 response.find(kDeletedPrefix) + arraysize(kDeletedPrefix) - 1); | 43 response.find(kDeletedPrefix) + arraysize(kDeletedPrefix) - 1); |
| 52 return deleted_app_id == app_id_ ? | 44 return deleted_app_id == app_id_ ? |
| 53 UnregistrationRequest::SUCCESS : | 45 UnregistrationRequest::SUCCESS : |
| 54 UnregistrationRequest::INCORRECT_APP_ID; | 46 UnregistrationRequest::INCORRECT_APP_ID; |
| 55 } | 47 } |
| 56 | 48 |
| 57 if (response.find(kErrorPrefix) != std::string::npos) { | |
| 58 std::string error = response.substr( | |
| 59 response.find(kErrorPrefix) + arraysize(kErrorPrefix) - 1); | |
| 60 return error == kInvalidParameters ? | |
| 61 UnregistrationRequest::INVALID_PARAMETERS : | |
| 62 UnregistrationRequest::UNKNOWN_ERROR; | |
| 63 } | |
| 64 | |
| 65 DVLOG(1) << "Not able to parse a meaningful output from response body." | 49 DVLOG(1) << "Not able to parse a meaningful output from response body." |
| 66 << response; | 50 << response; |
| 67 return UnregistrationRequest::RESPONSE_PARSING_FAILED; | 51 return UnregistrationRequest::RESPONSE_PARSING_FAILED; |
| 68 } | 52 } |
| 69 | 53 |
| 70 void GCMUnregistrationRequestHandler::ReportUMAs( | 54 void GCMUnregistrationRequestHandler::ReportUMAs( |
| 71 UnregistrationRequest::Status status, | 55 UnregistrationRequest::Status status, |
| 72 int retry_count, | 56 int retry_count, |
| 73 base::TimeDelta complete_time) { | 57 base::TimeDelta complete_time) { |
| 74 UMA_HISTOGRAM_ENUMERATION("GCM.UnregistrationRequestStatus", | 58 UMA_HISTOGRAM_ENUMERATION("GCM.UnregistrationRequestStatus", |
| 75 status, | 59 status, |
| 76 UnregistrationRequest::UNREGISTRATION_STATUS_COUNT); | 60 UnregistrationRequest::UNREGISTRATION_STATUS_COUNT); |
| 77 | 61 |
| 78 // Other UMAs are only reported when the request succeeds. | 62 // Other UMAs are only reported when the request succeeds. |
| 79 if (status != UnregistrationRequest::SUCCESS) | 63 if (status != UnregistrationRequest::SUCCESS) |
| 80 return; | 64 return; |
| 81 | 65 |
| 82 UMA_HISTOGRAM_COUNTS("GCM.UnregistrationRetryCount", retry_count); | 66 UMA_HISTOGRAM_COUNTS("GCM.UnregistrationRetryCount", retry_count); |
| 83 UMA_HISTOGRAM_TIMES("GCM.UnregistrationCompleteTime", complete_time); | 67 UMA_HISTOGRAM_TIMES("GCM.UnregistrationCompleteTime", complete_time); |
| 84 } | 68 } |
| 85 | 69 |
| 86 } // namespace gcm | 70 } // namespace gcm |
| OLD | NEW |