| 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" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 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 kCertKey[] = "cert"; | 31 const char kCertKey[] = "cert"; |
| 32 const char kDeviceIdKey[] = "device"; | 32 const char kDeviceIdKey[] = "device"; |
| 33 const char kLoginHeader[] = "AidLogin"; | 33 const char kLoginHeader[] = "AidLogin"; |
| 34 const char kSenderKey[] = "sender"; | 34 const char kSenderKey[] = "sender"; |
| 35 | 35 |
| 36 // Request validation constants. | |
| 37 const size_t kMaxSenders = 100; | |
| 38 | |
| 39 // Response constants. | 36 // Response constants. |
| 40 const char kErrorPrefix[] = "Error="; | 37 const char kErrorPrefix[] = "Error="; |
| 41 const char kTokenPrefix[] = "token="; | 38 const char kTokenPrefix[] = "token="; |
| 42 const char kDeviceRegistrationError[] = "PHONE_REGISTRATION_ERROR"; | 39 const char kDeviceRegistrationError[] = "PHONE_REGISTRATION_ERROR"; |
| 43 const char kAuthenticationFailed[] = "AUTHENTICATION_FAILED"; | 40 const char kAuthenticationFailed[] = "AUTHENTICATION_FAILED"; |
| 44 const char kInvalidSender[] = "INVALID_SENDER"; | 41 const char kInvalidSender[] = "INVALID_SENDER"; |
| 45 const char kInvalidParameters[] = "INVALID_PARAMETERS"; | 42 const char kInvalidParameters[] = "INVALID_PARAMETERS"; |
| 46 | 43 |
| 47 void BuildFormEncoding(const std::string& key, | 44 void BuildFormEncoding(const std::string& key, |
| 48 const std::string& value, | 45 const std::string& value, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 RegistrationRequest::STATUS_COUNT); | 78 RegistrationRequest::STATUS_COUNT); |
| 82 } | 79 } |
| 83 | 80 |
| 84 } // namespace | 81 } // namespace |
| 85 | 82 |
| 86 RegistrationRequest::RequestInfo::RequestInfo( | 83 RegistrationRequest::RequestInfo::RequestInfo( |
| 87 uint64 android_id, | 84 uint64 android_id, |
| 88 uint64 security_token, | 85 uint64 security_token, |
| 89 const std::string& app_id, | 86 const std::string& app_id, |
| 90 const std::string& cert, | 87 const std::string& cert, |
| 91 const std::vector<std::string>& sender_ids) | 88 const std::string& sender_id) |
| 92 : android_id(android_id), | 89 : android_id(android_id), |
| 93 security_token(security_token), | 90 security_token(security_token), |
| 94 app_id(app_id), | 91 app_id(app_id), |
| 95 cert(cert), | 92 cert(cert), |
| 96 sender_ids(sender_ids) { | 93 sender_id(sender_id) { |
| 97 } | 94 } |
| 98 | 95 |
| 99 RegistrationRequest::RequestInfo::~RequestInfo() {} | 96 RegistrationRequest::RequestInfo::~RequestInfo() {} |
| 100 | 97 |
| 101 RegistrationRequest::RegistrationRequest( | 98 RegistrationRequest::RegistrationRequest( |
| 102 const RequestInfo& request_info, | 99 const RequestInfo& request_info, |
| 103 const net::BackoffEntry::Policy& backoff_policy, | 100 const net::BackoffEntry::Policy& backoff_policy, |
| 104 const RegistrationCallback& callback, | 101 const RegistrationCallback& callback, |
| 105 int max_retry_count, | 102 int max_retry_count, |
| 106 scoped_refptr<net::URLRequestContextGetter> request_context_getter) | 103 scoped_refptr<net::URLRequestContextGetter> request_context_getter) |
| 107 : callback_(callback), | 104 : callback_(callback), |
| 108 request_info_(request_info), | 105 request_info_(request_info), |
| 109 backoff_entry_(&backoff_policy), | 106 backoff_entry_(&backoff_policy), |
| 110 request_context_getter_(request_context_getter), | 107 request_context_getter_(request_context_getter), |
| 111 retries_left_(max_retry_count), | 108 retries_left_(max_retry_count), |
| 112 weak_ptr_factory_(this) { | 109 weak_ptr_factory_(this) { |
| 113 DCHECK_GE(max_retry_count, 0); | 110 DCHECK_GE(max_retry_count, 0); |
| 114 } | 111 } |
| 115 | 112 |
| 116 RegistrationRequest::~RegistrationRequest() {} | 113 RegistrationRequest::~RegistrationRequest() {} |
| 117 | 114 |
| 118 void RegistrationRequest::Start() { | 115 void RegistrationRequest::Start() { |
| 119 DCHECK(!callback_.is_null()); | 116 DCHECK(!callback_.is_null()); |
| 120 DCHECK(request_info_.android_id != 0UL); | 117 DCHECK(request_info_.android_id != 0UL); |
| 121 DCHECK(request_info_.security_token != 0UL); | 118 DCHECK(request_info_.security_token != 0UL); |
| 122 DCHECK(!request_info_.cert.empty()); | 119 DCHECK(!request_info_.cert.empty()); |
| 123 DCHECK(0 < request_info_.sender_ids.size() && | 120 DCHECK(!request_info_.sender_id.empty()); |
| 124 request_info_.sender_ids.size() <= kMaxSenders); | |
| 125 | 121 |
| 126 DCHECK(!url_fetcher_.get()); | 122 DCHECK(!url_fetcher_.get()); |
| 127 url_fetcher_.reset(net::URLFetcher::Create( | 123 url_fetcher_.reset(net::URLFetcher::Create( |
| 128 GURL(kRegistrationURL), net::URLFetcher::POST, this)); | 124 GURL(kRegistrationURL), net::URLFetcher::POST, this)); |
| 129 url_fetcher_->SetRequestContext(request_context_getter_); | 125 url_fetcher_->SetRequestContext(request_context_getter_); |
| 130 | 126 |
| 131 std::string android_id = base::Uint64ToString(request_info_.android_id); | 127 std::string android_id = base::Uint64ToString(request_info_.android_id); |
| 132 std::string auth_header = | 128 std::string auth_header = |
| 133 std::string(net::HttpRequestHeaders::kAuthorization) + ": " + | 129 std::string(net::HttpRequestHeaders::kAuthorization) + ": " + |
| 134 kLoginHeader + " " + android_id + ":" + | 130 kLoginHeader + " " + android_id + ":" + |
| 135 base::Uint64ToString(request_info_.security_token); | 131 base::Uint64ToString(request_info_.security_token); |
| 136 url_fetcher_->SetExtraRequestHeaders(auth_header); | 132 url_fetcher_->SetExtraRequestHeaders(auth_header); |
| 137 | 133 |
| 138 std::string body; | 134 std::string body; |
| 139 BuildFormEncoding(kAppIdKey, request_info_.app_id, &body); | 135 BuildFormEncoding(kAppIdKey, request_info_.app_id, &body); |
| 140 BuildFormEncoding(kCertKey, request_info_.cert, &body); | 136 BuildFormEncoding(kCertKey, request_info_.cert, &body); |
| 141 BuildFormEncoding(kDeviceIdKey, android_id, &body); | 137 BuildFormEncoding(kDeviceIdKey, android_id, &body); |
| 142 | 138 BuildFormEncoding(kSenderKey, request_info_.sender_id, &body); |
| 143 std::string senders; | |
| 144 for (std::vector<std::string>::const_iterator iter = | |
| 145 request_info_.sender_ids.begin(); | |
| 146 iter != request_info_.sender_ids.end(); | |
| 147 ++iter) { | |
| 148 DCHECK(!iter->empty()); | |
| 149 if (!senders.empty()) | |
| 150 senders.append(","); | |
| 151 senders.append(*iter); | |
| 152 } | |
| 153 BuildFormEncoding(kSenderKey, senders, &body); | |
| 154 | 139 |
| 155 DVLOG(1) << "Performing registration for: " << request_info_.app_id; | 140 DVLOG(1) << "Performing registration for: " << request_info_.app_id; |
| 156 DVLOG(1) << "Registration request: " << body; | 141 DVLOG(1) << "Registration request: " << body; |
| 157 url_fetcher_->SetUploadData(kRegistrationRequestContentType, body); | 142 url_fetcher_->SetUploadData(kRegistrationRequestContentType, body); |
| 158 url_fetcher_->Start(); | 143 url_fetcher_->Start(); |
| 159 } | 144 } |
| 160 | 145 |
| 161 void RegistrationRequest::RetryWithBackoff(bool update_backoff) { | 146 void RegistrationRequest::RetryWithBackoff(bool update_backoff) { |
| 162 if (update_backoff) { | 147 if (update_backoff) { |
| 163 DCHECK_GT(retries_left_, 0); | 148 DCHECK_GT(retries_left_, 0); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 } | 211 } |
| 227 | 212 |
| 228 status = REACHED_MAX_RETRIES; | 213 status = REACHED_MAX_RETRIES; |
| 229 RecordRegistrationStatusToUMA(status); | 214 RecordRegistrationStatusToUMA(status); |
| 230 } | 215 } |
| 231 | 216 |
| 232 callback_.Run(status, token); | 217 callback_.Run(status, token); |
| 233 } | 218 } |
| 234 | 219 |
| 235 } // namespace gcm | 220 } // namespace gcm |
| OLD | NEW |