Chromium Code Reviews| 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 && security_token != 0UL); |
| 96 } | 83 } |
| 97 | 84 |
| 98 RegistrationRequest::RequestInfo::~RequestInfo() {} | 85 RegistrationRequest::RequestInfo::~RequestInfo() {} |
| 99 | 86 |
| 87 RegistrationRequest::CustomRequestHandler::CustomRequestHandler() {} | |
| 88 | |
| 89 RegistrationRequest::CustomRequestHandler::~CustomRequestHandler() {} | |
| 90 | |
| 100 RegistrationRequest::RegistrationRequest( | 91 RegistrationRequest::RegistrationRequest( |
| 101 const GURL& registration_url, | 92 const GURL& registration_url, |
| 102 const RequestInfo& request_info, | 93 const RequestInfo& request_info, |
| 94 scoped_ptr<CustomRequestHandler> custom_request_handler, | |
| 103 const net::BackoffEntry::Policy& backoff_policy, | 95 const net::BackoffEntry::Policy& backoff_policy, |
| 104 const RegistrationCallback& callback, | 96 const RegistrationCallback& callback, |
| 105 int max_retry_count, | 97 int max_retry_count, |
| 106 scoped_refptr<net::URLRequestContextGetter> request_context_getter, | 98 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
| 107 GCMStatsRecorder* recorder) | 99 GCMStatsRecorder* recorder, |
| 100 const std::string& source_to_record) | |
| 108 : callback_(callback), | 101 : callback_(callback), |
| 109 request_info_(request_info), | 102 request_info_(request_info), |
| 103 custom_request_handler_(custom_request_handler.Pass()), | |
| 110 registration_url_(registration_url), | 104 registration_url_(registration_url), |
| 111 backoff_entry_(&backoff_policy), | 105 backoff_entry_(&backoff_policy), |
| 112 request_context_getter_(request_context_getter), | 106 request_context_getter_(request_context_getter), |
| 113 retries_left_(max_retry_count), | 107 retries_left_(max_retry_count), |
| 114 recorder_(recorder), | 108 recorder_(recorder), |
| 109 source_to_record_(source_to_record), | |
| 115 weak_ptr_factory_(this) { | 110 weak_ptr_factory_(this) { |
| 116 DCHECK_GE(max_retry_count, 0); | 111 DCHECK_GE(max_retry_count, 0); |
| 117 } | 112 } |
| 118 | 113 |
| 119 RegistrationRequest::~RegistrationRequest() {} | 114 RegistrationRequest::~RegistrationRequest() {} |
| 120 | 115 |
| 121 void RegistrationRequest::Start() { | 116 void RegistrationRequest::Start() { |
| 122 DCHECK(!callback_.is_null()); | 117 DCHECK(!callback_.is_null()); |
| 123 DCHECK(request_info_.android_id != 0UL); | 118 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 | 119 |
| 128 DCHECK(!url_fetcher_.get()); | |
| 129 url_fetcher_ = | 120 url_fetcher_ = |
| 130 net::URLFetcher::Create(registration_url_, net::URLFetcher::POST, this); | 121 net::URLFetcher::Create(registration_url_, net::URLFetcher::POST, this); |
| 131 url_fetcher_->SetRequestContext(request_context_getter_.get()); | 122 url_fetcher_->SetRequestContext(request_context_getter_.get()); |
| 132 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 123 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 133 net::LOAD_DO_NOT_SAVE_COOKIES); | 124 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 134 | 125 |
| 135 std::string android_id = base::Uint64ToString(request_info_.android_id); | 126 std::string extra_headers; |
| 136 std::string auth_header = | 127 BuildRequestHeaders(&extra_headers); |
| 137 std::string(net::HttpRequestHeaders::kAuthorization) + ": " + | 128 url_fetcher_->SetExtraRequestHeaders(extra_headers); |
| 138 kLoginHeader + " " + android_id + ":" + | |
| 139 base::Uint64ToString(request_info_.security_token); | |
| 140 url_fetcher_->SetExtraRequestHeaders(auth_header); | |
| 141 | 129 |
| 142 std::string body; | 130 std::string body; |
| 143 BuildFormEncoding(kAppIdKey, request_info_.app_id, &body); | 131 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 | 132 |
| 160 DVLOG(1) << "Performing registration for: " << request_info_.app_id; | 133 DVLOG(1) << "Performing registration for: " << request_info_.app_id; |
| 161 DVLOG(1) << "Registration request: " << body; | 134 DVLOG(1) << "Registration request: " << body; |
| 162 url_fetcher_->SetUploadData(kRegistrationRequestContentType, body); | 135 url_fetcher_->SetUploadData(kRegistrationRequestContentType, body); |
| 163 recorder_->RecordRegistrationSent(request_info_.app_id, senders); | 136 recorder_->RecordRegistrationSent(request_info_.app_id, source_to_record_); |
| 164 request_start_time_ = base::TimeTicks::Now(); | 137 request_start_time_ = base::TimeTicks::Now(); |
| 165 url_fetcher_->Start(); | 138 url_fetcher_->Start(); |
| 166 } | 139 } |
| 167 | 140 |
| 141 void RegistrationRequest::BuildRequestHeaders(std::string* extra_headers) { | |
| 142 net::HttpRequestHeaders headers; | |
| 143 headers.SetHeader( | |
| 144 net::HttpRequestHeaders::kAuthorization, | |
| 145 std::string(kLoginHeader) + " " + | |
| 146 base::Uint64ToString(request_info_.android_id) + ":" + | |
| 147 base::Uint64ToString(request_info_.security_token)); | |
| 148 *extra_headers = headers.ToString(); | |
| 149 } | |
| 150 | |
| 151 void RegistrationRequest::BuildRequestBody(std::string* body) { | |
| 152 BuildFormEncoding(kAppIdKey, request_info_.app_id, body); | |
| 153 BuildFormEncoding(kDeviceIdKey, | |
| 154 base::Uint64ToString(request_info_.android_id), | |
| 155 body); | |
| 156 custom_request_handler_->BuildRequestBody(body); | |
|
Nicolas Zea
2015/05/26 16:39:15
Check if it exists first?
jianli
2015/05/26 20:49:39
Done.
| |
| 157 } | |
| 158 | |
| 168 void RegistrationRequest::RetryWithBackoff(bool update_backoff) { | 159 void RegistrationRequest::RetryWithBackoff(bool update_backoff) { |
| 169 if (update_backoff) { | 160 if (update_backoff) { |
| 170 DCHECK_GT(retries_left_, 0); | 161 DCHECK_GT(retries_left_, 0); |
| 171 --retries_left_; | 162 --retries_left_; |
| 172 url_fetcher_.reset(); | 163 url_fetcher_.reset(); |
| 173 backoff_entry_.InformOfRequest(false); | 164 backoff_entry_.InformOfRequest(false); |
| 174 } | 165 } |
| 175 | 166 |
| 176 if (backoff_entry_.ShouldRejectRequest()) { | 167 if (backoff_entry_.ShouldRejectRequest()) { |
| 177 DVLOG(1) << "Delaying GCM registration of app: " | 168 DVLOG(1) << "Delaying GCM registration of app: " |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 230 | 221 |
| 231 return UNKNOWN_ERROR; | 222 return UNKNOWN_ERROR; |
| 232 } | 223 } |
| 233 | 224 |
| 234 void RegistrationRequest::OnURLFetchComplete(const net::URLFetcher* source) { | 225 void RegistrationRequest::OnURLFetchComplete(const net::URLFetcher* source) { |
| 235 std::string token; | 226 std::string token; |
| 236 Status status = ParseResponse(source, &token); | 227 Status status = ParseResponse(source, &token); |
| 237 RecordRegistrationStatusToUMA(status); | 228 RecordRegistrationStatusToUMA(status); |
| 238 recorder_->RecordRegistrationResponse( | 229 recorder_->RecordRegistrationResponse( |
| 239 request_info_.app_id, | 230 request_info_.app_id, |
| 240 request_info_.sender_ids, | 231 source_to_record_, |
| 241 status); | 232 status); |
| 242 | 233 |
| 243 if (ShouldRetryWithStatus(status)) { | 234 if (ShouldRetryWithStatus(status)) { |
| 244 if (retries_left_ > 0) { | 235 if (retries_left_ > 0) { |
| 245 recorder_->RecordRegistrationRetryRequested( | 236 recorder_->RecordRegistrationRetryRequested( |
| 246 request_info_.app_id, | 237 request_info_.app_id, |
| 247 request_info_.sender_ids, | 238 source_to_record_, |
| 248 retries_left_); | 239 retries_left_); |
| 249 RetryWithBackoff(true); | 240 RetryWithBackoff(true); |
| 250 return; | 241 return; |
| 251 } | 242 } |
| 252 | 243 |
| 253 status = REACHED_MAX_RETRIES; | 244 status = REACHED_MAX_RETRIES; |
| 254 recorder_->RecordRegistrationResponse( | 245 recorder_->RecordRegistrationResponse( |
| 255 request_info_.app_id, | 246 request_info_.app_id, |
| 256 request_info_.sender_ids, | 247 source_to_record_, |
| 257 status); | 248 status); |
| 258 RecordRegistrationStatusToUMA(status); | 249 RecordRegistrationStatusToUMA(status); |
| 259 } | 250 } |
| 260 | 251 |
| 261 if (status == SUCCESS) { | 252 if (status == SUCCESS) { |
| 262 UMA_HISTOGRAM_COUNTS("GCM.RegistrationRetryCount", | 253 UMA_HISTOGRAM_COUNTS("GCM.RegistrationRetryCount", |
| 263 backoff_entry_.failure_count()); | 254 backoff_entry_.failure_count()); |
| 264 UMA_HISTOGRAM_TIMES("GCM.RegistrationCompleteTime", | 255 UMA_HISTOGRAM_TIMES("GCM.RegistrationCompleteTime", |
| 265 base::TimeTicks::Now() - request_start_time_); | 256 base::TimeTicks::Now() - request_start_time_); |
| 266 } | 257 } |
| 267 callback_.Run(status, token); | 258 callback_.Run(status, token); |
| 268 } | 259 } |
| 269 | 260 |
| 270 } // namespace gcm | 261 } // namespace gcm |
| OLD | NEW |