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
3 // found in the LICENSE file.
5 #include "google_apis/gcm/engine/gcm_unregistration_request_handler.h"
7 #include "base/metrics/histogram.h"
8 #include "google_apis/gcm/base/gcm_util.h"
9 #include "net/url_request/url_fetcher.h"
16 const char kUnregistrationCallerKey
[] = "gcm_unreg_caller";
17 // We are going to set the value to "false" in order to forcefully unregister
19 const char kUnregistrationCallerValue
[] = "false";
21 // Response constants.
22 const char kDeletedPrefix
[] = "deleted=";
23 const char kErrorPrefix
[] = "Error=";
24 const char kInvalidParameters
[] = "INVALID_PARAMETERS";
28 GCMUnregistrationRequestHandler::GCMUnregistrationRequestHandler(
29 const std::string
& app_id
)
33 GCMUnregistrationRequestHandler::~GCMUnregistrationRequestHandler() {}
35 void GCMUnregistrationRequestHandler::BuildRequestBody(std::string
* body
){
36 BuildFormEncoding(kUnregistrationCallerKey
, kUnregistrationCallerValue
, body
);
39 UnregistrationRequest::Status
GCMUnregistrationRequestHandler::ParseResponse(
40 const net::URLFetcher
* source
) {
42 if (!source
->GetResponseAsString(&response
)) {
43 DVLOG(1) << "Failed to get response body.";
44 return UnregistrationRequest::NO_RESPONSE_BODY
;
47 DVLOG(1) << "Parsing unregistration response.";
48 if (response
.find(kDeletedPrefix
) != std::string::npos
) {
49 std::string deleted_app_id
= response
.substr(
50 response
.find(kDeletedPrefix
) + arraysize(kDeletedPrefix
) - 1);
51 return deleted_app_id
== app_id_
?
52 UnregistrationRequest::SUCCESS
:
53 UnregistrationRequest::INCORRECT_APP_ID
;
56 if (response
.find(kErrorPrefix
) != std::string::npos
) {
57 std::string error
= response
.substr(
58 response
.find(kErrorPrefix
) + arraysize(kErrorPrefix
) - 1);
59 return error
== kInvalidParameters
?
60 UnregistrationRequest::INVALID_PARAMETERS
:
61 UnregistrationRequest::UNKNOWN_ERROR
;
64 DVLOG(1) << "Not able to parse a meaningful output from response body."
66 return UnregistrationRequest::RESPONSE_PARSING_FAILED
;
69 void GCMUnregistrationRequestHandler::ReportUMAs(
70 UnregistrationRequest::Status status
,
72 base::TimeDelta complete_time
) {
73 UMA_HISTOGRAM_ENUMERATION("GCM.UnregistrationRequestStatus",
75 UnregistrationRequest::UNREGISTRATION_STATUS_COUNT
);
77 // Other UMAs are only reported when the request succeeds.
78 if (status
!= UnregistrationRequest::SUCCESS
)
81 UMA_HISTOGRAM_COUNTS("GCM.UnregistrationRetryCount", retry_count
);
82 UMA_HISTOGRAM_TIMES("GCM.UnregistrationCompleteTime", complete_time
);