Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(146)

Side by Side Diff: google_apis/gcm/engine/registration_request.cc

Issue 1137463003: Support getting and deleting token for Instance ID. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sync Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
11 #include "base/values.h" 11 #include "base/values.h"
12 #include "google_apis/gcm/monitoring/gcm_stats_recorder.h" 12 #include "google_apis/gcm/monitoring/gcm_stats_recorder.h"
13 #include "net/base/escape.h" 13 #include "net/base/escape.h"
14 #include "net/base/load_flags.h" 14 #include "net/base/load_flags.h"
15 #include "net/http/http_request_headers.h" 15 #include "net/http/http_request_headers.h"
16 #include "net/http/http_status_code.h" 16 #include "net/http/http_status_code.h"
17 #include "net/url_request/url_fetcher.h" 17 #include "net/url_request/url_fetcher.h"
18 #include "net/url_request/url_request_context_getter.h" 18 #include "net/url_request/url_request_context_getter.h"
19 #include "net/url_request/url_request_status.h" 19 #include "net/url_request/url_request_status.h"
20 #include "url/gurl.h" 20 #include "url/gurl.h"
21 21
22 namespace gcm { 22 namespace gcm {
23 23
24 namespace { 24 namespace {
25 25
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 // Common keys.
30 const char kAppIdKey[] = "app"; 31 const char kAppIdKey[] = "app";
31 const char kDeviceIdKey[] = "device"; 32 const char kDeviceIdKey[] = "device";
32 const char kLoginHeader[] = "AidLogin"; 33 const char kLoginHeader[] = "AidLogin";
33 const char kSenderKey[] = "sender"; 34 const char kSenderKey[] = "sender";
35 // Keys specific to InstanceID's GetToken request.
36 const char kGMSVersionKey[] = "gmsv";
37 const char kInstanceIDKey[] = "appid";
38 const char kScopeKey[] = "scope";
39
40 // Prefix that needs to be added for each option key.
41 const char kOptionKeyPrefix[] = "X-";
34 42
35 // Request validation constants. 43 // Request validation constants.
36 const size_t kMaxSenders = 100; 44 const size_t kMaxSenders = 100;
37 45
38 // Response constants. 46 // Response constants.
39 const char kErrorPrefix[] = "Error="; 47 const char kErrorPrefix[] = "Error=";
40 const char kTokenPrefix[] = "token="; 48 const char kTokenPrefix[] = "token=";
41 const char kDeviceRegistrationError[] = "PHONE_REGISTRATION_ERROR"; 49 const char kDeviceRegistrationError[] = "PHONE_REGISTRATION_ERROR";
42 const char kAuthenticationFailed[] = "AUTHENTICATION_FAILED"; 50 const char kAuthenticationFailed[] = "AUTHENTICATION_FAILED";
43 const char kInvalidSender[] = "INVALID_SENDER"; 51 const char kInvalidSender[] = "INVALID_SENDER";
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 status == RegistrationRequest::RESPONSE_PARSING_FAILED; 85 status == RegistrationRequest::RESPONSE_PARSING_FAILED;
78 } 86 }
79 87
80 void RecordRegistrationStatusToUMA(RegistrationRequest::Status status) { 88 void RecordRegistrationStatusToUMA(RegistrationRequest::Status status) {
81 UMA_HISTOGRAM_ENUMERATION("GCM.RegistrationRequestStatus", status, 89 UMA_HISTOGRAM_ENUMERATION("GCM.RegistrationRequestStatus", status,
82 RegistrationRequest::STATUS_COUNT); 90 RegistrationRequest::STATUS_COUNT);
83 } 91 }
84 92
85 } // namespace 93 } // namespace
86 94
87 RegistrationRequest::RequestInfo::RequestInfo( 95 RegistrationRequest::RequestInfo::RequestInfo() {}
88 uint64 android_id,
89 uint64 security_token,
90 const std::string& app_id,
91 const std::vector<std::string>& sender_ids)
92 : android_id(android_id),
93 security_token(security_token),
94 app_id(app_id),
95 sender_ids(sender_ids) {
96 }
97 96
98 RegistrationRequest::RequestInfo::~RequestInfo() {} 97 RegistrationRequest::RequestInfo::~RequestInfo() {}
99 98
99 void RegistrationRequest::RequestInfo::BuildRequestHeaders(
100 std::string* extra_headers) {
101 net::HttpRequestHeaders headers;
102 headers.SetHeader(
103 net::HttpRequestHeaders::kAuthorization,
104 std::string(kLoginHeader) + " " +
105 base::Uint64ToString(android_id_) + ":" +
106 base::Uint64ToString(security_token_));
107 *extra_headers = headers.ToString();
108 }
109
110 void RegistrationRequest::RequestInfo::BuildRequestBody(std::string* body){
111 DCHECK(android_id_ != 0UL && security_token_ != 0UL);
112
113 BuildFormEncoding(kAppIdKey, app_id_, body);
114 BuildFormEncoding(kDeviceIdKey, base::Uint64ToString(android_id_), body);
115 }
116
117 RegistrationRequest::GCMRequestInfo::GCMRequestInfo() {}
118
119 RegistrationRequest::GCMRequestInfo::~GCMRequestInfo() {}
120
121 void RegistrationRequest::GCMRequestInfo::BuildRequestBody(std::string* body){
122 DCHECK(0 < sender_ids_.size() && sender_ids_.size() <= kMaxSenders);
123
124 RequestInfo::BuildRequestBody(body);
125
126 std::string senders;
127 for (auto iter = sender_ids_.begin(); iter != sender_ids_.end(); ++iter) {
128 if (!senders.empty())
129 senders.append(",");
130 senders.append(*iter);
131 }
132 BuildFormEncoding(kSenderKey, senders, body);
133 UMA_HISTOGRAM_COUNTS("GCM.RegistrationSenderIdCount", sender_ids_.size());
134 }
135
136 std::string RegistrationRequest::GCMRequestInfo::GetSenders() const {
137 std::string senders;
138 for (auto iter = sender_ids_.begin(); iter != sender_ids_.end(); ++iter) {
139 if (!senders.empty())
140 senders.append(",");
141 senders.append(*iter);
142 }
143 return senders;
144 }
145
146 RegistrationRequest::InstanceIDRequestInfo::InstanceIDRequestInfo() {}
147
148 RegistrationRequest::InstanceIDRequestInfo::~InstanceIDRequestInfo() {}
149
150 void RegistrationRequest::InstanceIDRequestInfo::BuildRequestBody(
151 std::string* body){
152 DCHECK(!instance_id_.empty() && !authorized_entity_.empty() &&
153 !scope_.empty());
154
155 RequestInfo::BuildRequestBody(body);
156
157 BuildFormEncoding(kGMSVersionKey, chrome_version_, body);
158 BuildFormEncoding(kInstanceIDKey, instance_id_, body);
159 BuildFormEncoding(kSenderKey, authorized_entity_, body);
160 BuildFormEncoding(kScopeKey, scope_, body);
161 for (auto iter = options_.begin(); iter != options_.end(); ++iter)
162 BuildFormEncoding(kOptionKeyPrefix + iter->first, iter->second, body);
163 }
164
165 std::string RegistrationRequest::InstanceIDRequestInfo::GetSenders()
166 const {
167 return authorized_entity_;
168 }
169
100 RegistrationRequest::RegistrationRequest( 170 RegistrationRequest::RegistrationRequest(
101 const GURL& registration_url, 171 const GURL& registration_url,
102 const RequestInfo& request_info, 172 scoped_ptr<RequestInfo> request_info,
103 const net::BackoffEntry::Policy& backoff_policy, 173 const net::BackoffEntry::Policy& backoff_policy,
104 const RegistrationCallback& callback, 174 const RegistrationCallback& callback,
105 int max_retry_count, 175 int max_retry_count,
106 scoped_refptr<net::URLRequestContextGetter> request_context_getter, 176 scoped_refptr<net::URLRequestContextGetter> request_context_getter,
107 GCMStatsRecorder* recorder) 177 GCMStatsRecorder* recorder)
108 : callback_(callback), 178 : callback_(callback),
109 request_info_(request_info), 179 request_info_(request_info.Pass()),
110 registration_url_(registration_url), 180 registration_url_(registration_url),
111 backoff_entry_(&backoff_policy), 181 backoff_entry_(&backoff_policy),
112 request_context_getter_(request_context_getter), 182 request_context_getter_(request_context_getter),
113 retries_left_(max_retry_count), 183 retries_left_(max_retry_count),
114 recorder_(recorder), 184 recorder_(recorder),
115 weak_ptr_factory_(this) { 185 weak_ptr_factory_(this) {
116 DCHECK_GE(max_retry_count, 0); 186 DCHECK_GE(max_retry_count, 0);
117 } 187 }
118 188
119 RegistrationRequest::~RegistrationRequest() {} 189 RegistrationRequest::~RegistrationRequest() {}
120 190
121 void RegistrationRequest::Start() { 191 void RegistrationRequest::Start() {
122 DCHECK(!callback_.is_null()); 192 DCHECK(!callback_.is_null());
123 DCHECK(request_info_.android_id != 0UL); 193 DCHECK(!url_fetcher_.get());
124 DCHECK(request_info_.security_token != 0UL);
125 DCHECK(0 < request_info_.sender_ids.size() &&
126 request_info_.sender_ids.size() <= kMaxSenders);
127 194
128 DCHECK(!url_fetcher_.get());
129 url_fetcher_ = 195 url_fetcher_ =
130 net::URLFetcher::Create(registration_url_, net::URLFetcher::POST, this); 196 net::URLFetcher::Create(registration_url_, net::URLFetcher::POST, this);
131 url_fetcher_->SetRequestContext(request_context_getter_.get()); 197 url_fetcher_->SetRequestContext(request_context_getter_.get());
132 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | 198 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
133 net::LOAD_DO_NOT_SAVE_COOKIES); 199 net::LOAD_DO_NOT_SAVE_COOKIES);
134 200
135 std::string android_id = base::Uint64ToString(request_info_.android_id); 201 std::string extra_headers;
136 std::string auth_header = 202 request_info_->BuildRequestHeaders(&extra_headers);
137 std::string(net::HttpRequestHeaders::kAuthorization) + ": " + 203 url_fetcher_->SetExtraRequestHeaders(extra_headers);
138 kLoginHeader + " " + android_id + ":" +
139 base::Uint64ToString(request_info_.security_token);
140 url_fetcher_->SetExtraRequestHeaders(auth_header);
141 204
142 std::string body; 205 std::string body;
143 BuildFormEncoding(kAppIdKey, request_info_.app_id, &body); 206 request_info_->BuildRequestBody(&body);
144 BuildFormEncoding(kDeviceIdKey, android_id, &body);
145 207
146 std::string senders; 208 DVLOG(1) << "Performing registration for: " << request_info_->app_id();
147 for (std::vector<std::string>::const_iterator iter =
148 request_info_.sender_ids.begin();
149 iter != request_info_.sender_ids.end();
150 ++iter) {
151 DCHECK(!iter->empty());
152 if (!senders.empty())
153 senders.append(",");
154 senders.append(*iter);
155 }
156 BuildFormEncoding(kSenderKey, senders, &body);
157 UMA_HISTOGRAM_COUNTS("GCM.RegistrationSenderIdCount",
158 request_info_.sender_ids.size());
159
160 DVLOG(1) << "Performing registration for: " << request_info_.app_id;
161 DVLOG(1) << "Registration request: " << body; 209 DVLOG(1) << "Registration request: " << body;
162 url_fetcher_->SetUploadData(kRegistrationRequestContentType, body); 210 url_fetcher_->SetUploadData(kRegistrationRequestContentType, body);
163 recorder_->RecordRegistrationSent(request_info_.app_id, senders); 211 recorder_->RecordRegistrationSent(request_info_->app_id(),
212 request_info_->GetSenders());
164 request_start_time_ = base::TimeTicks::Now(); 213 request_start_time_ = base::TimeTicks::Now();
165 url_fetcher_->Start(); 214 url_fetcher_->Start();
166 } 215 }
167 216
168 void RegistrationRequest::RetryWithBackoff(bool update_backoff) { 217 void RegistrationRequest::RetryWithBackoff(bool update_backoff) {
169 if (update_backoff) { 218 if (update_backoff) {
170 DCHECK_GT(retries_left_, 0); 219 DCHECK_GT(retries_left_, 0);
171 --retries_left_; 220 --retries_left_;
172 url_fetcher_.reset(); 221 url_fetcher_.reset();
173 backoff_entry_.InformOfRequest(false); 222 backoff_entry_.InformOfRequest(false);
174 } 223 }
175 224
176 if (backoff_entry_.ShouldRejectRequest()) { 225 if (backoff_entry_.ShouldRejectRequest()) {
177 DVLOG(1) << "Delaying GCM registration of app: " 226 DVLOG(1) << "Delaying GCM registration of app: "
178 << request_info_.app_id << ", for " 227 << request_info_->app_id() << ", for "
179 << backoff_entry_.GetTimeUntilRelease().InMilliseconds() 228 << backoff_entry_.GetTimeUntilRelease().InMilliseconds()
180 << " milliseconds."; 229 << " milliseconds.";
181 base::MessageLoop::current()->PostDelayedTask( 230 base::MessageLoop::current()->PostDelayedTask(
182 FROM_HERE, 231 FROM_HERE,
183 base::Bind(&RegistrationRequest::RetryWithBackoff, 232 base::Bind(&RegistrationRequest::RetryWithBackoff,
184 weak_ptr_factory_.GetWeakPtr(), 233 weak_ptr_factory_.GetWeakPtr(),
185 false), 234 false),
186 backoff_entry_.GetTimeUntilRelease()); 235 backoff_entry_.GetTimeUntilRelease());
187 return; 236 return;
188 } 237 }
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 } 278 }
230 279
231 return UNKNOWN_ERROR; 280 return UNKNOWN_ERROR;
232 } 281 }
233 282
234 void RegistrationRequest::OnURLFetchComplete(const net::URLFetcher* source) { 283 void RegistrationRequest::OnURLFetchComplete(const net::URLFetcher* source) {
235 std::string token; 284 std::string token;
236 Status status = ParseResponse(source, &token); 285 Status status = ParseResponse(source, &token);
237 RecordRegistrationStatusToUMA(status); 286 RecordRegistrationStatusToUMA(status);
238 recorder_->RecordRegistrationResponse( 287 recorder_->RecordRegistrationResponse(
239 request_info_.app_id, 288 request_info_->app_id(),
240 request_info_.sender_ids, 289 request_info_->GetSenders(),
241 status); 290 status);
242 291
243 if (ShouldRetryWithStatus(status)) { 292 if (ShouldRetryWithStatus(status)) {
244 if (retries_left_ > 0) { 293 if (retries_left_ > 0) {
245 recorder_->RecordRegistrationRetryRequested( 294 recorder_->RecordRegistrationRetryRequested(
246 request_info_.app_id, 295 request_info_->app_id(),
247 request_info_.sender_ids, 296 request_info_->GetSenders(),
248 retries_left_); 297 retries_left_);
249 RetryWithBackoff(true); 298 RetryWithBackoff(true);
250 return; 299 return;
251 } 300 }
252 301
253 status = REACHED_MAX_RETRIES; 302 status = REACHED_MAX_RETRIES;
254 recorder_->RecordRegistrationResponse( 303 recorder_->RecordRegistrationResponse(
255 request_info_.app_id, 304 request_info_->app_id(),
256 request_info_.sender_ids, 305 request_info_->GetSenders(),
257 status); 306 status);
258 RecordRegistrationStatusToUMA(status); 307 RecordRegistrationStatusToUMA(status);
259 } 308 }
260 309
261 if (status == SUCCESS) { 310 if (status == SUCCESS) {
262 UMA_HISTOGRAM_COUNTS("GCM.RegistrationRetryCount", 311 UMA_HISTOGRAM_COUNTS("GCM.RegistrationRetryCount",
263 backoff_entry_.failure_count()); 312 backoff_entry_.failure_count());
264 UMA_HISTOGRAM_TIMES("GCM.RegistrationCompleteTime", 313 UMA_HISTOGRAM_TIMES("GCM.RegistrationCompleteTime",
265 base::TimeTicks::Now() - request_start_time_); 314 base::TimeTicks::Now() - request_start_time_);
266 } 315 }
267 callback_.Run(status, token); 316 callback_.Run(status, token);
268 } 317 }
269 318
270 } // namespace gcm 319 } // namespace gcm
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698