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 "google_apis/gcm/engine/registration_request.h" | 5 #include "google_apis/gcm/engine/registration_request.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 const char kRegistrationURL[] = | 24 const char kRegistrationURL[] = |
25 "https://android.clients.google.com/c2dm/register3"; | 25 "https://android.clients.google.com/c2dm/register3"; |
26 const char kRegistrationRequestContentType[] = | 26 const char kRegistrationRequestContentType[] = |
27 "application/x-www-form-urlencoded"; | 27 "application/x-www-form-urlencoded"; |
28 | 28 |
29 // Request constants. | 29 // Request constants. |
30 const char kAppIdKey[] = "app"; | 30 const char kAppIdKey[] = "app"; |
31 const char kCertKey[] = "cert"; | |
32 const char kDeviceIdKey[] = "device"; | 31 const char kDeviceIdKey[] = "device"; |
33 const char kLoginHeader[] = "AidLogin"; | 32 const char kLoginHeader[] = "AidLogin"; |
34 const char kSenderKey[] = "sender"; | 33 const char kSenderKey[] = "sender"; |
35 | 34 |
36 // Request validation constants. | 35 // Request validation constants. |
37 const size_t kMaxSenders = 100; | 36 const size_t kMaxSenders = 100; |
38 | 37 |
39 // Response constants. | 38 // Response constants. |
40 const char kErrorPrefix[] = "Error="; | 39 const char kErrorPrefix[] = "Error="; |
41 const char kTokenPrefix[] = "token="; | 40 const char kTokenPrefix[] = "token="; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 UMA_HISTOGRAM_ENUMERATION("GCM.RegistrationRequestStatus", status, | 79 UMA_HISTOGRAM_ENUMERATION("GCM.RegistrationRequestStatus", status, |
81 RegistrationRequest::STATUS_COUNT); | 80 RegistrationRequest::STATUS_COUNT); |
82 } | 81 } |
83 | 82 |
84 } // namespace | 83 } // namespace |
85 | 84 |
86 RegistrationRequest::RequestInfo::RequestInfo( | 85 RegistrationRequest::RequestInfo::RequestInfo( |
87 uint64 android_id, | 86 uint64 android_id, |
88 uint64 security_token, | 87 uint64 security_token, |
89 const std::string& app_id, | 88 const std::string& app_id, |
90 const std::string& cert, | |
91 const std::vector<std::string>& sender_ids) | 89 const std::vector<std::string>& sender_ids) |
92 : android_id(android_id), | 90 : android_id(android_id), |
93 security_token(security_token), | 91 security_token(security_token), |
94 app_id(app_id), | 92 app_id(app_id), |
95 cert(cert), | |
96 sender_ids(sender_ids) { | 93 sender_ids(sender_ids) { |
97 } | 94 } |
98 | 95 |
99 RegistrationRequest::RequestInfo::~RequestInfo() {} | 96 RegistrationRequest::RequestInfo::~RequestInfo() {} |
100 | 97 |
101 RegistrationRequest::RegistrationRequest( | 98 RegistrationRequest::RegistrationRequest( |
102 const RequestInfo& request_info, | 99 const RequestInfo& request_info, |
103 const net::BackoffEntry::Policy& backoff_policy, | 100 const net::BackoffEntry::Policy& backoff_policy, |
104 const RegistrationCallback& callback, | 101 const RegistrationCallback& callback, |
105 int max_retry_count, | 102 int max_retry_count, |
106 scoped_refptr<net::URLRequestContextGetter> request_context_getter) | 103 scoped_refptr<net::URLRequestContextGetter> request_context_getter) |
107 : callback_(callback), | 104 : callback_(callback), |
108 request_info_(request_info), | 105 request_info_(request_info), |
109 backoff_entry_(&backoff_policy), | 106 backoff_entry_(&backoff_policy), |
110 request_context_getter_(request_context_getter), | 107 request_context_getter_(request_context_getter), |
111 retries_left_(max_retry_count), | 108 retries_left_(max_retry_count), |
112 weak_ptr_factory_(this) { | 109 weak_ptr_factory_(this) { |
113 DCHECK_GE(max_retry_count, 0); | 110 DCHECK_GE(max_retry_count, 0); |
114 } | 111 } |
115 | 112 |
116 RegistrationRequest::~RegistrationRequest() {} | 113 RegistrationRequest::~RegistrationRequest() {} |
117 | 114 |
118 void RegistrationRequest::Start() { | 115 void RegistrationRequest::Start() { |
119 DCHECK(!callback_.is_null()); | 116 DCHECK(!callback_.is_null()); |
120 DCHECK(request_info_.android_id != 0UL); | 117 DCHECK(request_info_.android_id != 0UL); |
121 DCHECK(request_info_.security_token != 0UL); | 118 DCHECK(request_info_.security_token != 0UL); |
122 DCHECK(!request_info_.cert.empty()); | |
123 DCHECK(0 < request_info_.sender_ids.size() && | 119 DCHECK(0 < request_info_.sender_ids.size() && |
124 request_info_.sender_ids.size() <= kMaxSenders); | 120 request_info_.sender_ids.size() <= kMaxSenders); |
125 | 121 |
126 DCHECK(!url_fetcher_.get()); | 122 DCHECK(!url_fetcher_.get()); |
127 url_fetcher_.reset(net::URLFetcher::Create( | 123 url_fetcher_.reset(net::URLFetcher::Create( |
128 GURL(kRegistrationURL), net::URLFetcher::POST, this)); | 124 GURL(kRegistrationURL), net::URLFetcher::POST, this)); |
129 url_fetcher_->SetRequestContext(request_context_getter_); | 125 url_fetcher_->SetRequestContext(request_context_getter_); |
130 | 126 |
131 std::string android_id = base::Uint64ToString(request_info_.android_id); | 127 std::string android_id = base::Uint64ToString(request_info_.android_id); |
132 std::string auth_header = | 128 std::string auth_header = |
133 std::string(net::HttpRequestHeaders::kAuthorization) + ": " + | 129 std::string(net::HttpRequestHeaders::kAuthorization) + ": " + |
134 kLoginHeader + " " + android_id + ":" + | 130 kLoginHeader + " " + android_id + ":" + |
135 base::Uint64ToString(request_info_.security_token); | 131 base::Uint64ToString(request_info_.security_token); |
136 url_fetcher_->SetExtraRequestHeaders(auth_header); | 132 url_fetcher_->SetExtraRequestHeaders(auth_header); |
137 | 133 |
138 std::string body; | 134 std::string body; |
139 BuildFormEncoding(kAppIdKey, request_info_.app_id, &body); | 135 BuildFormEncoding(kAppIdKey, request_info_.app_id, &body); |
140 BuildFormEncoding(kCertKey, request_info_.cert, &body); | |
141 BuildFormEncoding(kDeviceIdKey, android_id, &body); | 136 BuildFormEncoding(kDeviceIdKey, android_id, &body); |
142 | 137 |
143 std::string senders; | 138 std::string senders; |
144 for (std::vector<std::string>::const_iterator iter = | 139 for (std::vector<std::string>::const_iterator iter = |
145 request_info_.sender_ids.begin(); | 140 request_info_.sender_ids.begin(); |
146 iter != request_info_.sender_ids.end(); | 141 iter != request_info_.sender_ids.end(); |
147 ++iter) { | 142 ++iter) { |
148 DCHECK(!iter->empty()); | 143 DCHECK(!iter->empty()); |
149 if (!senders.empty()) | 144 if (!senders.empty()) |
150 senders.append(","); | 145 senders.append(","); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 } | 221 } |
227 | 222 |
228 status = REACHED_MAX_RETRIES; | 223 status = REACHED_MAX_RETRIES; |
229 RecordRegistrationStatusToUMA(status); | 224 RecordRegistrationStatusToUMA(status); |
230 } | 225 } |
231 | 226 |
232 callback_.Run(status, token); | 227 callback_.Run(status, token); |
233 } | 228 } |
234 | 229 |
235 } // namespace gcm | 230 } // namespace gcm |
OLD | NEW |