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" |
11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "google_apis/gcm/base/gcm_util.h" |
12 #include "google_apis/gcm/monitoring/gcm_stats_recorder.h" | 13 #include "google_apis/gcm/monitoring/gcm_stats_recorder.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 const char kAppIdKey[] = "app"; | 30 const char kAppIdKey[] = "app"; |
31 const char kDeviceIdKey[] = "device"; | 31 const char kDeviceIdKey[] = "device"; |
32 const char kLoginHeader[] = "AidLogin"; | 32 const char kLoginHeader[] = "AidLogin"; |
33 const char kSenderKey[] = "sender"; | |
34 | |
35 // Request validation constants. | |
36 const size_t kMaxSenders = 100; | |
37 | 33 |
38 // Response constants. | 34 // Response constants. |
39 const char kErrorPrefix[] = "Error="; | 35 const char kErrorPrefix[] = "Error="; |
40 const char kTokenPrefix[] = "token="; | 36 const char kTokenPrefix[] = "token="; |
41 const char kDeviceRegistrationError[] = "PHONE_REGISTRATION_ERROR"; | 37 const char kDeviceRegistrationError[] = "PHONE_REGISTRATION_ERROR"; |
42 const char kAuthenticationFailed[] = "AUTHENTICATION_FAILED"; | 38 const char kAuthenticationFailed[] = "AUTHENTICATION_FAILED"; |
43 const char kInvalidSender[] = "INVALID_SENDER"; | 39 const char kInvalidSender[] = "INVALID_SENDER"; |
44 const char kInvalidParameters[] = "INVALID_PARAMETERS"; | 40 const char kInvalidParameters[] = "INVALID_PARAMETERS"; |
45 | 41 |
46 void BuildFormEncoding(const std::string& key, | |
47 const std::string& value, | |
48 std::string* out) { | |
49 if (!out->empty()) | |
50 out->append("&"); | |
51 out->append(key + "=" + net::EscapeUrlEncodedData(value, true)); | |
52 } | |
53 | |
54 // Gets correct status from the error message. | 42 // Gets correct status from the error message. |
55 RegistrationRequest::Status GetStatusFromError(const std::string& error) { | 43 RegistrationRequest::Status GetStatusFromError(const std::string& error) { |
56 // TODO(fgorski): Improve error parsing in case there is nore then just an | 44 // TODO(fgorski): Improve error parsing in case there is nore then just an |
57 // Error=ERROR_STRING in response. | 45 // Error=ERROR_STRING in response. |
58 if (error.find(kDeviceRegistrationError) != std::string::npos) | 46 if (error.find(kDeviceRegistrationError) != std::string::npos) |
59 return RegistrationRequest::DEVICE_REGISTRATION_ERROR; | 47 return RegistrationRequest::DEVICE_REGISTRATION_ERROR; |
60 if (error.find(kAuthenticationFailed) != std::string::npos) | 48 if (error.find(kAuthenticationFailed) != std::string::npos) |
61 return RegistrationRequest::AUTHENTICATION_FAILED; | 49 return RegistrationRequest::AUTHENTICATION_FAILED; |
62 if (error.find(kInvalidSender) != std::string::npos) | 50 if (error.find(kInvalidSender) != std::string::npos) |
63 return RegistrationRequest::INVALID_SENDER; | 51 return RegistrationRequest::INVALID_SENDER; |
(...skipping 16 matching lines...) Expand all Loading... |
80 void RecordRegistrationStatusToUMA(RegistrationRequest::Status status) { | 68 void RecordRegistrationStatusToUMA(RegistrationRequest::Status status) { |
81 UMA_HISTOGRAM_ENUMERATION("GCM.RegistrationRequestStatus", status, | 69 UMA_HISTOGRAM_ENUMERATION("GCM.RegistrationRequestStatus", status, |
82 RegistrationRequest::STATUS_COUNT); | 70 RegistrationRequest::STATUS_COUNT); |
83 } | 71 } |
84 | 72 |
85 } // namespace | 73 } // namespace |
86 | 74 |
87 RegistrationRequest::RequestInfo::RequestInfo( | 75 RegistrationRequest::RequestInfo::RequestInfo( |
88 uint64 android_id, | 76 uint64 android_id, |
89 uint64 security_token, | 77 uint64 security_token, |
90 const std::string& app_id, | 78 const std::string& app_id) |
91 const std::vector<std::string>& sender_ids) | |
92 : android_id(android_id), | 79 : android_id(android_id), |
93 security_token(security_token), | 80 security_token(security_token), |
94 app_id(app_id), | 81 app_id(app_id) { |
95 sender_ids(sender_ids) { | 82 DCHECK(android_id != 0UL); |
| 83 DCHECK(security_token != 0UL); |
96 } | 84 } |
97 | 85 |
98 RegistrationRequest::RequestInfo::~RequestInfo() {} | 86 RegistrationRequest::RequestInfo::~RequestInfo() {} |
99 | 87 |
| 88 RegistrationRequest::CustomRequestHandler::CustomRequestHandler() {} |
| 89 |
| 90 RegistrationRequest::CustomRequestHandler::~CustomRequestHandler() {} |
| 91 |
100 RegistrationRequest::RegistrationRequest( | 92 RegistrationRequest::RegistrationRequest( |
101 const GURL& registration_url, | 93 const GURL& registration_url, |
102 const RequestInfo& request_info, | 94 const RequestInfo& request_info, |
| 95 scoped_ptr<CustomRequestHandler> custom_request_handler, |
103 const net::BackoffEntry::Policy& backoff_policy, | 96 const net::BackoffEntry::Policy& backoff_policy, |
104 const RegistrationCallback& callback, | 97 const RegistrationCallback& callback, |
105 int max_retry_count, | 98 int max_retry_count, |
106 scoped_refptr<net::URLRequestContextGetter> request_context_getter, | 99 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
107 GCMStatsRecorder* recorder) | 100 GCMStatsRecorder* recorder, |
| 101 const std::string& source_to_record) |
108 : callback_(callback), | 102 : callback_(callback), |
109 request_info_(request_info), | 103 request_info_(request_info), |
| 104 custom_request_handler_(custom_request_handler.Pass()), |
110 registration_url_(registration_url), | 105 registration_url_(registration_url), |
111 backoff_entry_(&backoff_policy), | 106 backoff_entry_(&backoff_policy), |
112 request_context_getter_(request_context_getter), | 107 request_context_getter_(request_context_getter), |
113 retries_left_(max_retry_count), | 108 retries_left_(max_retry_count), |
114 recorder_(recorder), | 109 recorder_(recorder), |
| 110 source_to_record_(source_to_record), |
115 weak_ptr_factory_(this) { | 111 weak_ptr_factory_(this) { |
116 DCHECK_GE(max_retry_count, 0); | 112 DCHECK_GE(max_retry_count, 0); |
117 } | 113 } |
118 | 114 |
119 RegistrationRequest::~RegistrationRequest() {} | 115 RegistrationRequest::~RegistrationRequest() {} |
120 | 116 |
121 void RegistrationRequest::Start() { | 117 void RegistrationRequest::Start() { |
122 DCHECK(!callback_.is_null()); | 118 DCHECK(!callback_.is_null()); |
123 DCHECK(request_info_.android_id != 0UL); | 119 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 | 120 |
128 DCHECK(!url_fetcher_.get()); | |
129 url_fetcher_ = | 121 url_fetcher_ = |
130 net::URLFetcher::Create(registration_url_, net::URLFetcher::POST, this); | 122 net::URLFetcher::Create(registration_url_, net::URLFetcher::POST, this); |
131 url_fetcher_->SetRequestContext(request_context_getter_.get()); | 123 url_fetcher_->SetRequestContext(request_context_getter_.get()); |
132 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 124 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
133 net::LOAD_DO_NOT_SAVE_COOKIES); | 125 net::LOAD_DO_NOT_SAVE_COOKIES); |
134 | 126 |
135 std::string android_id = base::Uint64ToString(request_info_.android_id); | 127 std::string extra_headers; |
136 std::string auth_header = | 128 BuildRequestHeaders(&extra_headers); |
137 std::string(net::HttpRequestHeaders::kAuthorization) + ": " + | 129 url_fetcher_->SetExtraRequestHeaders(extra_headers); |
138 kLoginHeader + " " + android_id + ":" + | |
139 base::Uint64ToString(request_info_.security_token); | |
140 url_fetcher_->SetExtraRequestHeaders(auth_header); | |
141 | 130 |
142 std::string body; | 131 std::string body; |
143 BuildFormEncoding(kAppIdKey, request_info_.app_id, &body); | 132 BuildRequestBody(&body); |
144 BuildFormEncoding(kDeviceIdKey, android_id, &body); | |
145 | |
146 std::string senders; | |
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 | 133 |
160 DVLOG(1) << "Performing registration for: " << request_info_.app_id; | 134 DVLOG(1) << "Performing registration for: " << request_info_.app_id; |
161 DVLOG(1) << "Registration request: " << body; | 135 DVLOG(1) << "Registration request: " << body; |
162 url_fetcher_->SetUploadData(kRegistrationRequestContentType, body); | 136 url_fetcher_->SetUploadData(kRegistrationRequestContentType, body); |
163 recorder_->RecordRegistrationSent(request_info_.app_id, senders); | 137 recorder_->RecordRegistrationSent(request_info_.app_id, source_to_record_); |
164 request_start_time_ = base::TimeTicks::Now(); | 138 request_start_time_ = base::TimeTicks::Now(); |
165 url_fetcher_->Start(); | 139 url_fetcher_->Start(); |
166 } | 140 } |
167 | 141 |
| 142 void RegistrationRequest::BuildRequestHeaders(std::string* extra_headers) { |
| 143 net::HttpRequestHeaders headers; |
| 144 headers.SetHeader( |
| 145 net::HttpRequestHeaders::kAuthorization, |
| 146 std::string(kLoginHeader) + " " + |
| 147 base::Uint64ToString(request_info_.android_id) + ":" + |
| 148 base::Uint64ToString(request_info_.security_token)); |
| 149 *extra_headers = headers.ToString(); |
| 150 } |
| 151 |
| 152 void RegistrationRequest::BuildRequestBody(std::string* body) { |
| 153 BuildFormEncoding(kAppIdKey, request_info_.app_id, body); |
| 154 BuildFormEncoding(kDeviceIdKey, |
| 155 base::Uint64ToString(request_info_.android_id), |
| 156 body); |
| 157 |
| 158 DCHECK(custom_request_handler_.get()); |
| 159 custom_request_handler_->BuildRequestBody(body); |
| 160 } |
| 161 |
168 void RegistrationRequest::RetryWithBackoff(bool update_backoff) { | 162 void RegistrationRequest::RetryWithBackoff(bool update_backoff) { |
169 if (update_backoff) { | 163 if (update_backoff) { |
170 DCHECK_GT(retries_left_, 0); | 164 DCHECK_GT(retries_left_, 0); |
171 --retries_left_; | 165 --retries_left_; |
172 url_fetcher_.reset(); | 166 url_fetcher_.reset(); |
173 backoff_entry_.InformOfRequest(false); | 167 backoff_entry_.InformOfRequest(false); |
174 } | 168 } |
175 | 169 |
176 if (backoff_entry_.ShouldRejectRequest()) { | 170 if (backoff_entry_.ShouldRejectRequest()) { |
177 DVLOG(1) << "Delaying GCM registration of app: " | 171 DVLOG(1) << "Delaying GCM registration of app: " |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 | 224 |
231 return UNKNOWN_ERROR; | 225 return UNKNOWN_ERROR; |
232 } | 226 } |
233 | 227 |
234 void RegistrationRequest::OnURLFetchComplete(const net::URLFetcher* source) { | 228 void RegistrationRequest::OnURLFetchComplete(const net::URLFetcher* source) { |
235 std::string token; | 229 std::string token; |
236 Status status = ParseResponse(source, &token); | 230 Status status = ParseResponse(source, &token); |
237 RecordRegistrationStatusToUMA(status); | 231 RecordRegistrationStatusToUMA(status); |
238 recorder_->RecordRegistrationResponse( | 232 recorder_->RecordRegistrationResponse( |
239 request_info_.app_id, | 233 request_info_.app_id, |
240 request_info_.sender_ids, | 234 source_to_record_, |
241 status); | 235 status); |
242 | 236 |
243 if (ShouldRetryWithStatus(status)) { | 237 if (ShouldRetryWithStatus(status)) { |
244 if (retries_left_ > 0) { | 238 if (retries_left_ > 0) { |
245 recorder_->RecordRegistrationRetryRequested( | 239 recorder_->RecordRegistrationRetryRequested( |
246 request_info_.app_id, | 240 request_info_.app_id, |
247 request_info_.sender_ids, | 241 source_to_record_, |
248 retries_left_); | 242 retries_left_); |
249 RetryWithBackoff(true); | 243 RetryWithBackoff(true); |
250 return; | 244 return; |
251 } | 245 } |
252 | 246 |
253 status = REACHED_MAX_RETRIES; | 247 status = REACHED_MAX_RETRIES; |
254 recorder_->RecordRegistrationResponse( | 248 recorder_->RecordRegistrationResponse( |
255 request_info_.app_id, | 249 request_info_.app_id, |
256 request_info_.sender_ids, | 250 source_to_record_, |
257 status); | 251 status); |
258 RecordRegistrationStatusToUMA(status); | 252 RecordRegistrationStatusToUMA(status); |
259 } | 253 } |
260 | 254 |
261 if (status == SUCCESS) { | 255 if (status == SUCCESS) { |
262 UMA_HISTOGRAM_COUNTS("GCM.RegistrationRetryCount", | 256 UMA_HISTOGRAM_COUNTS("GCM.RegistrationRetryCount", |
263 backoff_entry_.failure_count()); | 257 backoff_entry_.failure_count()); |
264 UMA_HISTOGRAM_TIMES("GCM.RegistrationCompleteTime", | 258 UMA_HISTOGRAM_TIMES("GCM.RegistrationCompleteTime", |
265 base::TimeTicks::Now() - request_start_time_); | 259 base::TimeTicks::Now() - request_start_time_); |
266 } | 260 } |
267 callback_.Run(status, token); | 261 callback_.Run(status, token); |
268 } | 262 } |
269 | 263 |
270 } // namespace gcm | 264 } // namespace gcm |
OLD | NEW |