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