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

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: Add new files 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) + " " + base::Uint64ToString(android_id) + ":" +
105 base::Uint64ToString(security_token));
106 *extra_headers = headers.ToString();
107 }
108
109 void RegistrationRequest::RequestInfo::BuildRequestBody(std::string* body){
110 BuildFormEncoding(kAppIdKey, app_id, body);
111 BuildFormEncoding(kDeviceIdKey, base::Uint64ToString(android_id), body);
112 }
113
114 RegistrationRequest::GCMRequestInfo::GCMRequestInfo() {}
115
116 RegistrationRequest::GCMRequestInfo::~GCMRequestInfo() {}
117
118 void RegistrationRequest::GCMRequestInfo::BuildRequestBody(std::string* body){
119 DCHECK(0 < sender_ids.size() && sender_ids.size() <= kMaxSenders);
120
121 RequestInfo::BuildRequestBody(body);
122
123 std::string senders;
124 for (auto iter = sender_ids.begin(); iter != sender_ids.end(); ++iter) {
125 if (!senders.empty())
126 senders.append(",");
127 senders.append(*iter);
128 }
129 BuildFormEncoding(kSenderKey, senders, body);
130 UMA_HISTOGRAM_COUNTS("GCM.RegistrationSenderIdCount", sender_ids.size());
131 }
132
133 std::string RegistrationRequest::GCMRequestInfo::GetSenders() const {
134 std::string senders;
135 for (auto iter = sender_ids.begin(); iter != sender_ids.end(); ++iter) {
136 if (!senders.empty())
137 senders.append(",");
138 senders.append(*iter);
139 }
140 return senders;
141 }
142
143 RegistrationRequest::InstanceIDRequestInfo::InstanceIDRequestInfo() {}
144
145 RegistrationRequest::InstanceIDRequestInfo::~InstanceIDRequestInfo() {}
146
147 void RegistrationRequest::InstanceIDRequestInfo::BuildRequestBody(
148 std::string* body){
149 RequestInfo::BuildRequestBody(body);
150
151 BuildFormEncoding(kGMSVersionKey, chrome_version, body);
152 BuildFormEncoding(kInstanceIDKey, instance_id, body);
153 BuildFormEncoding(kSenderKey, authorized_entity, body);
154 BuildFormEncoding(kScopeKey, scope, body);
155 for (auto iter = options.begin(); iter != options.end(); ++iter)
156 BuildFormEncoding(kOptionKeyPrefix + iter->first, iter->second, body);
157 }
158
159 std::string RegistrationRequest::InstanceIDRequestInfo::GetSenders()
160 const {
161 return authorized_entity;
162 }
163
100 RegistrationRequest::RegistrationRequest( 164 RegistrationRequest::RegistrationRequest(
101 const GURL& registration_url, 165 const GURL& registration_url,
102 const RequestInfo& request_info, 166 scoped_ptr<RequestInfo> request_info,
103 const net::BackoffEntry::Policy& backoff_policy, 167 const net::BackoffEntry::Policy& backoff_policy,
104 const RegistrationCallback& callback, 168 const RegistrationCallback& callback,
105 int max_retry_count, 169 int max_retry_count,
106 scoped_refptr<net::URLRequestContextGetter> request_context_getter, 170 scoped_refptr<net::URLRequestContextGetter> request_context_getter,
107 GCMStatsRecorder* recorder) 171 GCMStatsRecorder* recorder)
108 : callback_(callback), 172 : callback_(callback),
109 request_info_(request_info), 173 request_info_(request_info.Pass()),
110 registration_url_(registration_url), 174 registration_url_(registration_url),
111 backoff_entry_(&backoff_policy), 175 backoff_entry_(&backoff_policy),
112 request_context_getter_(request_context_getter), 176 request_context_getter_(request_context_getter),
113 retries_left_(max_retry_count), 177 retries_left_(max_retry_count),
114 recorder_(recorder), 178 recorder_(recorder),
115 weak_ptr_factory_(this) { 179 weak_ptr_factory_(this) {
116 DCHECK_GE(max_retry_count, 0); 180 DCHECK_GE(max_retry_count, 0);
117 } 181 }
118 182
119 RegistrationRequest::~RegistrationRequest() {} 183 RegistrationRequest::~RegistrationRequest() {}
120 184
121 void RegistrationRequest::Start() { 185 void RegistrationRequest::Start() {
122 DCHECK(!callback_.is_null()); 186 DCHECK(!callback_.is_null());
123 DCHECK(request_info_.android_id != 0UL); 187 DCHECK(request_info_->android_id != 0UL);
124 DCHECK(request_info_.security_token != 0UL); 188 DCHECK(request_info_->security_token != 0UL);
125 DCHECK(0 < request_info_.sender_ids.size() && 189 DCHECK(!url_fetcher_.get());
126 request_info_.sender_ids.size() <= kMaxSenders);
127 190
128 DCHECK(!url_fetcher_.get());
129 url_fetcher_ = 191 url_fetcher_ =
130 net::URLFetcher::Create(registration_url_, net::URLFetcher::POST, this); 192 net::URLFetcher::Create(registration_url_, net::URLFetcher::POST, this);
131 url_fetcher_->SetRequestContext(request_context_getter_.get()); 193 url_fetcher_->SetRequestContext(request_context_getter_.get());
132 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | 194 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
133 net::LOAD_DO_NOT_SAVE_COOKIES); 195 net::LOAD_DO_NOT_SAVE_COOKIES);
134 196
135 std::string android_id = base::Uint64ToString(request_info_.android_id); 197 std::string extra_headers;
136 std::string auth_header = 198 request_info_->BuildRequestHeaders(&extra_headers);
137 std::string(net::HttpRequestHeaders::kAuthorization) + ": " + 199 url_fetcher_->SetExtraRequestHeaders(extra_headers);
138 kLoginHeader + " " + android_id + ":" +
139 base::Uint64ToString(request_info_.security_token);
140 url_fetcher_->SetExtraRequestHeaders(auth_header);
141 200
142 std::string body; 201 std::string body;
143 BuildFormEncoding(kAppIdKey, request_info_.app_id, &body); 202 request_info_->BuildRequestBody(&body);
144 BuildFormEncoding(kDeviceIdKey, android_id, &body);
145 203
146 std::string senders; 204 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; 205 DVLOG(1) << "Registration request: " << body;
162 url_fetcher_->SetUploadData(kRegistrationRequestContentType, body); 206 url_fetcher_->SetUploadData(kRegistrationRequestContentType, body);
163 recorder_->RecordRegistrationSent(request_info_.app_id, senders); 207 recorder_->RecordRegistrationSent(request_info_->app_id,
208 request_info_->GetSenders());
164 request_start_time_ = base::TimeTicks::Now(); 209 request_start_time_ = base::TimeTicks::Now();
165 url_fetcher_->Start(); 210 url_fetcher_->Start();
166 } 211 }
167 212
168 void RegistrationRequest::RetryWithBackoff(bool update_backoff) { 213 void RegistrationRequest::RetryWithBackoff(bool update_backoff) {
169 if (update_backoff) { 214 if (update_backoff) {
170 DCHECK_GT(retries_left_, 0); 215 DCHECK_GT(retries_left_, 0);
171 --retries_left_; 216 --retries_left_;
172 url_fetcher_.reset(); 217 url_fetcher_.reset();
173 backoff_entry_.InformOfRequest(false); 218 backoff_entry_.InformOfRequest(false);
174 } 219 }
175 220
176 if (backoff_entry_.ShouldRejectRequest()) { 221 if (backoff_entry_.ShouldRejectRequest()) {
177 DVLOG(1) << "Delaying GCM registration of app: " 222 DVLOG(1) << "Delaying GCM registration of app: "
178 << request_info_.app_id << ", for " 223 << request_info_->app_id << ", for "
179 << backoff_entry_.GetTimeUntilRelease().InMilliseconds() 224 << backoff_entry_.GetTimeUntilRelease().InMilliseconds()
180 << " milliseconds."; 225 << " milliseconds.";
181 base::MessageLoop::current()->PostDelayedTask( 226 base::MessageLoop::current()->PostDelayedTask(
182 FROM_HERE, 227 FROM_HERE,
183 base::Bind(&RegistrationRequest::RetryWithBackoff, 228 base::Bind(&RegistrationRequest::RetryWithBackoff,
184 weak_ptr_factory_.GetWeakPtr(), 229 weak_ptr_factory_.GetWeakPtr(),
185 false), 230 false),
186 backoff_entry_.GetTimeUntilRelease()); 231 backoff_entry_.GetTimeUntilRelease());
187 return; 232 return;
188 } 233 }
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 } 274 }
230 275
231 return UNKNOWN_ERROR; 276 return UNKNOWN_ERROR;
232 } 277 }
233 278
234 void RegistrationRequest::OnURLFetchComplete(const net::URLFetcher* source) { 279 void RegistrationRequest::OnURLFetchComplete(const net::URLFetcher* source) {
235 std::string token; 280 std::string token;
236 Status status = ParseResponse(source, &token); 281 Status status = ParseResponse(source, &token);
237 RecordRegistrationStatusToUMA(status); 282 RecordRegistrationStatusToUMA(status);
238 recorder_->RecordRegistrationResponse( 283 recorder_->RecordRegistrationResponse(
239 request_info_.app_id, 284 request_info_->app_id,
240 request_info_.sender_ids, 285 request_info_->GetSenders(),
241 status); 286 status);
242 287
243 if (ShouldRetryWithStatus(status)) { 288 if (ShouldRetryWithStatus(status)) {
244 if (retries_left_ > 0) { 289 if (retries_left_ > 0) {
245 recorder_->RecordRegistrationRetryRequested( 290 recorder_->RecordRegistrationRetryRequested(
246 request_info_.app_id, 291 request_info_->app_id,
247 request_info_.sender_ids, 292 request_info_->GetSenders(),
248 retries_left_); 293 retries_left_);
249 RetryWithBackoff(true); 294 RetryWithBackoff(true);
250 return; 295 return;
251 } 296 }
252 297
253 status = REACHED_MAX_RETRIES; 298 status = REACHED_MAX_RETRIES;
254 recorder_->RecordRegistrationResponse( 299 recorder_->RecordRegistrationResponse(
255 request_info_.app_id, 300 request_info_->app_id,
256 request_info_.sender_ids, 301 request_info_->GetSenders(),
257 status); 302 status);
258 RecordRegistrationStatusToUMA(status); 303 RecordRegistrationStatusToUMA(status);
259 } 304 }
260 305
261 if (status == SUCCESS) { 306 if (status == SUCCESS) {
262 UMA_HISTOGRAM_COUNTS("GCM.RegistrationRetryCount", 307 UMA_HISTOGRAM_COUNTS("GCM.RegistrationRetryCount",
263 backoff_entry_.failure_count()); 308 backoff_entry_.failure_count());
264 UMA_HISTOGRAM_TIMES("GCM.RegistrationCompleteTime", 309 UMA_HISTOGRAM_TIMES("GCM.RegistrationCompleteTime",
265 base::TimeTicks::Now() - request_start_time_); 310 base::TimeTicks::Now() - request_start_time_);
266 } 311 }
267 callback_.Run(status, token); 312 callback_.Run(status, token);
268 } 313 }
269 314
270 } // namespace gcm 315 } // namespace gcm
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698