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
3 // found in the LICENSE file.
5 #include "google_apis/gcm/engine/instance_id_get_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_request_context_getter.h"
17 const char kAuthorizedEntityKey
[] = "sender";
18 const char kSubtypeKey
[] = "X-subtype";
19 const char kGMSVersionKey
[] = "gmsv";
20 const char kInstanceIDKey
[] = "appid";
21 const char kScopeKey
[] = "scope";
22 const char kExtraScopeKey
[] = "X-scope";
23 // Prefix that needs to be added for each option key.
24 const char kOptionKeyPrefix
[] = "X-";
28 InstanceIDGetTokenRequestHandler::InstanceIDGetTokenRequestHandler(
29 const std::string
& instance_id
,
30 const std::string
& authorized_entity
,
31 const std::string
& scope
,
33 const std::map
<std::string
, std::string
>& options
)
34 : instance_id_(instance_id
),
35 authorized_entity_(authorized_entity
),
37 gcm_version_(gcm_version
),
39 DCHECK(!instance_id
.empty());
40 DCHECK(!authorized_entity
.empty());
41 DCHECK(!scope
.empty());
44 InstanceIDGetTokenRequestHandler::~InstanceIDGetTokenRequestHandler() {}
46 void InstanceIDGetTokenRequestHandler::BuildRequestBody(std::string
* body
){
47 BuildFormEncoding(kScopeKey
, scope_
, body
);
48 BuildFormEncoding(kExtraScopeKey
, scope_
, body
);
49 for (auto iter
= options_
.begin(); iter
!= options_
.end(); ++iter
)
50 BuildFormEncoding(kOptionKeyPrefix
+ iter
->first
, iter
->second
, body
);
51 BuildFormEncoding(kGMSVersionKey
, base::IntToString(gcm_version_
), body
);
52 BuildFormEncoding(kInstanceIDKey
, instance_id_
, body
);
53 BuildFormEncoding(kAuthorizedEntityKey
, authorized_entity_
, body
);
54 // TODO(jianli): To work around server bug. To be removed when the server fix
56 BuildFormEncoding(kSubtypeKey
, authorized_entity_
, body
);
59 void InstanceIDGetTokenRequestHandler::ReportUMAs(
60 RegistrationRequest::Status status
,
62 base::TimeDelta complete_time
) {
63 UMA_HISTOGRAM_ENUMERATION("InstanceID.GetToken.RequestStatus",
65 RegistrationRequest::STATUS_COUNT
);
67 // Other UMAs are only reported when the request succeeds.
68 if (status
!= RegistrationRequest::SUCCESS
)
71 UMA_HISTOGRAM_COUNTS("InstanceID.GetToken.RetryCount", retry_count
);
72 UMA_HISTOGRAM_TIMES("InstanceID.GetToken.CompleteTime", complete_time
);