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/instance_id_delete_token_request_handler.h"
7 #include "base/metrics/histogram.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "google_apis/gcm/base/gcm_util.h"
10 #include "net/url_request/url_fetcher.h"
11 #include "net/url_request/url_request_context_getter.h"
18 const char kGMSVersionKey
[] = "gmsv";
19 const char kInstanceIDKey
[] = "appid";
20 const char kSenderKey
[] = "sender";
21 const char kSubtypeKey
[] = "X-subtype";
22 const char kScopeKey
[] = "scope";
23 const char kExtraScopeKey
[] = "X-scope";
25 // Response constants.
26 const char kTokenPrefix
[] = "token=";
27 const char kErrorPrefix
[] = "Error=";
28 const char kInvalidParameters
[] = "INVALID_PARAMETERS";
32 InstanceIDDeleteTokenRequestHandler::InstanceIDDeleteTokenRequestHandler(
33 const std::string
& instance_id
,
34 const std::string
& authorized_entity
,
35 const std::string
& scope
,
37 : instance_id_(instance_id
),
38 authorized_entity_(authorized_entity
),
40 gcm_version_(gcm_version
) {
41 DCHECK(!instance_id
.empty());
42 DCHECK(!authorized_entity
.empty());
43 DCHECK(!scope
.empty());
46 InstanceIDDeleteTokenRequestHandler::~InstanceIDDeleteTokenRequestHandler() {}
48 void InstanceIDDeleteTokenRequestHandler::BuildRequestBody(std::string
* body
){
49 BuildFormEncoding(kInstanceIDKey
, instance_id_
, body
);
50 BuildFormEncoding(kSenderKey
, authorized_entity_
, body
);
51 BuildFormEncoding(kScopeKey
, scope_
, body
);
52 BuildFormEncoding(kExtraScopeKey
, scope_
, body
);
53 BuildFormEncoding(kGMSVersionKey
, base::IntToString(gcm_version_
), body
);
54 // TODO(jianli): To work around server bug. To be removed when the server fix
56 BuildFormEncoding(kSubtypeKey
, authorized_entity_
, body
);
59 UnregistrationRequest::Status
60 InstanceIDDeleteTokenRequestHandler::ParseResponse(
61 const net::URLFetcher
* source
) {
63 if (!source
->GetResponseAsString(&response
)) {
64 DVLOG(1) << "Failed to get response body.";
65 return UnregistrationRequest::NO_RESPONSE_BODY
;
68 if (response
.find(kErrorPrefix
) != std::string::npos
) {
69 std::string error
= response
.substr(
70 response
.find(kErrorPrefix
) + arraysize(kErrorPrefix
) - 1);
71 return error
== kInvalidParameters
?
72 UnregistrationRequest::INVALID_PARAMETERS
:
73 UnregistrationRequest::UNKNOWN_ERROR
;
76 if (response
.find(kTokenPrefix
) == std::string::npos
)
77 return UnregistrationRequest::RESPONSE_PARSING_FAILED
;
79 return UnregistrationRequest::SUCCESS
;
82 void InstanceIDDeleteTokenRequestHandler::ReportUMAs(
83 UnregistrationRequest::Status status
,
85 base::TimeDelta complete_time
) {
86 UMA_HISTOGRAM_ENUMERATION("InstanceID.DeleteToken.RequestStatus",
88 UnregistrationRequest::UNREGISTRATION_STATUS_COUNT
);
90 // Other UMAs are only reported when the request succeeds.
91 if (status
!= UnregistrationRequest::SUCCESS
)
94 UMA_HISTOGRAM_COUNTS("InstanceID.DeleteToken.RetryCount", retry_count
);
95 UMA_HISTOGRAM_TIMES("InstanceID.DeleteToken.CompleteTime", complete_time
);