Chromium Code Reviews| Index: google_apis/gcm/engine/registration_request.cc |
| diff --git a/google_apis/gcm/engine/registration_request.cc b/google_apis/gcm/engine/registration_request.cc |
| index 2e52fba53cfbb786c5a044fda29a906fb39ca8db..7762683b5cf47215ee23124e52889c57670500ca 100644 |
| --- a/google_apis/gcm/engine/registration_request.cc |
| +++ b/google_apis/gcm/engine/registration_request.cc |
| @@ -9,8 +9,8 @@ |
| #include "base/metrics/histogram.h" |
| #include "base/strings/string_number_conversions.h" |
| #include "base/values.h" |
| +#include "google_apis/gcm/base/gcm_util.h" |
| #include "google_apis/gcm/monitoring/gcm_stats_recorder.h" |
| -#include "net/base/escape.h" |
| #include "net/base/load_flags.h" |
| #include "net/http/http_request_headers.h" |
| #include "net/http/http_status_code.h" |
| @@ -30,10 +30,6 @@ const char kRegistrationRequestContentType[] = |
| const char kAppIdKey[] = "app"; |
| const char kDeviceIdKey[] = "device"; |
| const char kLoginHeader[] = "AidLogin"; |
| -const char kSenderKey[] = "sender"; |
| - |
| -// Request validation constants. |
| -const size_t kMaxSenders = 100; |
| // Response constants. |
| const char kErrorPrefix[] = "Error="; |
| @@ -43,14 +39,6 @@ const char kAuthenticationFailed[] = "AUTHENTICATION_FAILED"; |
| const char kInvalidSender[] = "INVALID_SENDER"; |
| const char kInvalidParameters[] = "INVALID_PARAMETERS"; |
| -void BuildFormEncoding(const std::string& key, |
| - const std::string& value, |
| - std::string* out) { |
| - if (!out->empty()) |
| - out->append("&"); |
| - out->append(key + "=" + net::EscapeUrlEncodedData(value, true)); |
| -} |
| - |
| // Gets correct status from the error message. |
| RegistrationRequest::Status GetStatusFromError(const std::string& error) { |
| // TODO(fgorski): Improve error parsing in case there is nore then just an |
| @@ -87,12 +75,10 @@ void RecordRegistrationStatusToUMA(RegistrationRequest::Status status) { |
| RegistrationRequest::RequestInfo::RequestInfo( |
| uint64 android_id, |
| uint64 security_token, |
| - const std::string& app_id, |
| - const std::vector<std::string>& sender_ids) |
| + const std::string& app_id) |
| : android_id(android_id), |
| security_token(security_token), |
| - app_id(app_id), |
| - sender_ids(sender_ids) { |
| + app_id(app_id) { |
|
Nicolas Zea
2015/05/21 21:07:51
Maybe DCHECK that the values are set?
jianli
2015/05/21 23:11:24
Done.
|
| } |
| RegistrationRequest::RequestInfo::~RequestInfo() {} |
| @@ -120,51 +106,50 @@ RegistrationRequest::~RegistrationRequest() {} |
| void RegistrationRequest::Start() { |
| DCHECK(!callback_.is_null()); |
| - DCHECK(request_info_.android_id != 0UL); |
| - DCHECK(request_info_.security_token != 0UL); |
| - DCHECK(0 < request_info_.sender_ids.size() && |
| - request_info_.sender_ids.size() <= kMaxSenders); |
| - |
| DCHECK(!url_fetcher_.get()); |
| + |
| url_fetcher_ = |
| net::URLFetcher::Create(registration_url_, net::URLFetcher::POST, this); |
| url_fetcher_->SetRequestContext(request_context_getter_.get()); |
| url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| net::LOAD_DO_NOT_SAVE_COOKIES); |
| - std::string android_id = base::Uint64ToString(request_info_.android_id); |
| - std::string auth_header = |
| - std::string(net::HttpRequestHeaders::kAuthorization) + ": " + |
| - kLoginHeader + " " + android_id + ":" + |
| - base::Uint64ToString(request_info_.security_token); |
| - url_fetcher_->SetExtraRequestHeaders(auth_header); |
| + std::string extra_headers; |
| + BuildRequestHeaders(&extra_headers); |
| + url_fetcher_->SetExtraRequestHeaders(extra_headers); |
| std::string body; |
| - BuildFormEncoding(kAppIdKey, request_info_.app_id, &body); |
| - BuildFormEncoding(kDeviceIdKey, android_id, &body); |
| - |
| - std::string senders; |
| - for (std::vector<std::string>::const_iterator iter = |
| - request_info_.sender_ids.begin(); |
| - iter != request_info_.sender_ids.end(); |
| - ++iter) { |
| - DCHECK(!iter->empty()); |
| - if (!senders.empty()) |
| - senders.append(","); |
| - senders.append(*iter); |
| - } |
| - BuildFormEncoding(kSenderKey, senders, &body); |
| - UMA_HISTOGRAM_COUNTS("GCM.RegistrationSenderIdCount", |
| - request_info_.sender_ids.size()); |
| + BuildRequestBody(&body); |
| DVLOG(1) << "Performing registration for: " << request_info_.app_id; |
| DVLOG(1) << "Registration request: " << body; |
| url_fetcher_->SetUploadData(kRegistrationRequestContentType, body); |
| - recorder_->RecordRegistrationSent(request_info_.app_id, senders); |
| + recorder_->RecordRegistrationSent(request_info_.app_id, |
| + GetSourceForRecorder()); |
| request_start_time_ = base::TimeTicks::Now(); |
| url_fetcher_->Start(); |
| } |
| +void RegistrationRequest::BuildRequestHeaders(std::string* extra_headers) { |
| + net::HttpRequestHeaders headers; |
| + headers.SetHeader( |
| + net::HttpRequestHeaders::kAuthorization, |
| + std::string(kLoginHeader) + " " + |
| + base::Uint64ToString(request_info_.android_id) + ":" + |
| + base::Uint64ToString(request_info_.security_token)); |
| + *extra_headers = headers.ToString(); |
| +} |
| + |
| +void RegistrationRequest::BuildRequestBody(std::string* body) { |
| + DCHECK(request_info_.android_id != 0UL && |
| + request_info_.security_token != 0UL); |
| + |
| + BuildFormEncoding(kAppIdKey, request_info_.app_id, body); |
| + BuildFormEncoding(kDeviceIdKey, |
| + base::Uint64ToString(request_info_.android_id), |
| + body); |
| +} |
| + |
| void RegistrationRequest::RetryWithBackoff(bool update_backoff) { |
| if (update_backoff) { |
| DCHECK_GT(retries_left_, 0); |
| @@ -237,14 +222,14 @@ void RegistrationRequest::OnURLFetchComplete(const net::URLFetcher* source) { |
| RecordRegistrationStatusToUMA(status); |
| recorder_->RecordRegistrationResponse( |
| request_info_.app_id, |
| - request_info_.sender_ids, |
| + GetSourceForRecorder(), |
| status); |
| if (ShouldRetryWithStatus(status)) { |
| if (retries_left_ > 0) { |
| recorder_->RecordRegistrationRetryRequested( |
| request_info_.app_id, |
| - request_info_.sender_ids, |
| + GetSourceForRecorder(), |
| retries_left_); |
| RetryWithBackoff(true); |
| return; |
| @@ -253,7 +238,7 @@ void RegistrationRequest::OnURLFetchComplete(const net::URLFetcher* source) { |
| status = REACHED_MAX_RETRIES; |
| recorder_->RecordRegistrationResponse( |
| request_info_.app_id, |
| - request_info_.sender_ids, |
| + GetSourceForRecorder(), |
| status); |
| RecordRegistrationStatusToUMA(status); |
| } |