Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "google_apis/gcm/engine/registration_request.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/message_loop/message_loop.h" | |
| 9 #include "base/strings/string_number_conversions.h" | |
| 10 #include "base/values.h" | |
| 11 #include "net/base/escape.h" | |
| 12 #include "net/http/http_request_headers.h" | |
| 13 #include "net/http/http_status_code.h" | |
| 14 #include "net/url_request/url_fetcher.h" | |
| 15 #include "net/url_request/url_request_context_getter.h" | |
| 16 #include "net/url_request/url_request_status.h" | |
| 17 #include "url/gurl.h" | |
| 18 | |
| 19 namespace gcm { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 const char kRegistrationURL[] = | |
| 24 "https://android.clients.google.com/c2dm/register3"; | |
| 25 const char kRegistrationDataFormat[] = "application/x-www-form-urlencoded"; | |
|
jianli
2014/01/10 23:23:47
nit: kRegistrationRequestContentType
fgorski
2014/01/11 00:10:36
Done.
| |
| 26 | |
| 27 // Request constants. | |
| 28 const char kLoginHeader[] = "AidLogin"; | |
| 29 | |
| 30 const char kAppIdKey[] = "app"; | |
| 31 const char kSenderKey[] = "sender"; | |
| 32 const char kCertKey[] = "cert"; | |
| 33 const char kDeviceIdKey[] = "device"; | |
| 34 const long kMaxSenders = 100; | |
| 35 const char kUserSerialNumberKey[] = "device_user_id"; | |
| 36 const char kUserAndroidIdKey[] = "X-GOOG.USER_AID"; | |
| 37 | |
| 38 // Response constants. | |
| 39 const char kTokenPrefix[] = "token="; | |
| 40 | |
| 41 void BuildFormEncoding(const std::string& key, | |
| 42 const std::string& value, | |
| 43 std::string* out) { | |
| 44 if (!out->empty()) | |
| 45 out->append("&"); | |
| 46 out->append(key + "=" + net::EscapeUrlEncodedData(value, true)); | |
| 47 } | |
| 48 | |
| 49 } // namespace | |
| 50 | |
| 51 RegistrationRequest::Parameters::Parameters( | |
| 52 const RegistrationCallback& callback, | |
| 53 uint64 android_id, | |
| 54 uint64 security_token, | |
| 55 uint64 user_android_id, | |
| 56 int64 user_serial_number, | |
| 57 const std::string& app_id, | |
| 58 const std::string& cert, | |
| 59 const std::vector<std::string>& sender_ids) | |
| 60 : callback(callback), | |
| 61 android_id(android_id), | |
| 62 security_token(security_token), | |
| 63 user_android_id(user_android_id), | |
| 64 user_serial_number(user_serial_number), | |
| 65 app_id(app_id), | |
| 66 cert(cert), | |
| 67 sender_ids(sender_ids) {} | |
| 68 | |
| 69 RegistrationRequest::Parameters::~Parameters() {} | |
| 70 | |
| 71 RegistrationRequest::RegistrationRequest( | |
| 72 const Parameters& parameters, | |
| 73 scoped_refptr<net::URLRequestContextGetter> request_context_getter) | |
| 74 : parameters_(parameters), | |
| 75 request_context_getter_(request_context_getter) {} | |
| 76 | |
| 77 RegistrationRequest::~RegistrationRequest() {} | |
| 78 | |
| 79 void RegistrationRequest::Start() { | |
| 80 DCHECK(!parameters_.callback.is_null()); | |
| 81 DCHECK(parameters_.android_id != 0UL); | |
| 82 DCHECK(parameters_.security_token != 0UL); | |
| 83 DCHECK(!parameters_.cert.empty()); | |
| 84 DCHECK(0 < parameters_.sender_ids.size() && | |
| 85 parameters_.sender_ids.size() <= kMaxSenders); | |
| 86 | |
| 87 DCHECK(!url_fetcher_.get()); | |
| 88 url_fetcher_.reset(net::URLFetcher::Create( | |
| 89 GURL(kRegistrationURL), net::URLFetcher::POST, this)); | |
| 90 url_fetcher_->SetRequestContext(request_context_getter_); | |
| 91 | |
| 92 std::string android_id = base::Uint64ToString(parameters_.android_id); | |
| 93 std::string auth_header = | |
| 94 std::string(net::HttpRequestHeaders::kAuthorization) + ": " + | |
| 95 kLoginHeader + " " + android_id + ":" + | |
| 96 base::Uint64ToString(parameters_.security_token); | |
| 97 url_fetcher_->SetExtraRequestHeaders(auth_header); | |
| 98 | |
| 99 std::string body; | |
| 100 BuildFormEncoding(kAppIdKey, parameters_.app_id, &body); | |
| 101 BuildFormEncoding(kCertKey, parameters_.cert, &body); | |
| 102 BuildFormEncoding(kDeviceIdKey, android_id, &body); | |
| 103 | |
| 104 std::string senders; | |
| 105 for (std::vector<std::string>::const_iterator iter = | |
| 106 parameters_.sender_ids.begin(); | |
| 107 iter != parameters_.sender_ids.end(); | |
| 108 ++iter) { | |
| 109 if (!senders.empty()) | |
| 110 senders.append(","); | |
|
jianli
2014/01/10 23:23:47
What if one sender is empty or ","? Do we need so
fgorski
2014/01/11 00:10:36
We already have validation upstream, but I see no
| |
| 111 senders.append(net::EscapeUrlEncodedData(*iter, true)); | |
| 112 } | |
| 113 BuildFormEncoding(kSenderKey, senders, &body); | |
| 114 | |
| 115 if (parameters_.user_serial_number != 0) { | |
| 116 DCHECK(parameters_.user_android_id != 0); | |
| 117 BuildFormEncoding(kUserSerialNumberKey, | |
| 118 base::IntToString(parameters_.user_serial_number), | |
| 119 &body); | |
| 120 BuildFormEncoding(kUserAndroidIdKey, | |
| 121 base::Uint64ToString(parameters_.user_android_id), | |
| 122 &body); | |
| 123 } | |
| 124 | |
| 125 DVLOG(1) << "Registration request: " << body; | |
| 126 url_fetcher_->SetUploadData(kRegistrationDataFormat, body); | |
| 127 | |
| 128 DVLOG(1) << "Performing registration for: " << parameters_.app_id; | |
| 129 url_fetcher_->Start(); | |
| 130 } | |
| 131 | |
| 132 void RegistrationRequest::OnURLFetchComplete(const net::URLFetcher* source) { | |
| 133 std::string response; | |
| 134 if (!source->GetStatus().is_success() || | |
| 135 source->GetResponseCode() != net::HTTP_OK || | |
| 136 !source->GetResponseAsString(&response)) { | |
| 137 // TODO(fgoski): Introduce retry logic. | |
| 138 LOG(ERROR) << "Failed to get registration response."; | |
| 139 parameters_.callback.Run(""); | |
| 140 return; | |
| 141 } | |
| 142 | |
| 143 DVLOG(1) << "Parsing registration response: " << response; | |
| 144 size_t token_pos = response.find(kTokenPrefix); | |
| 145 std::string token; | |
| 146 if (token_pos != std::string::npos) | |
| 147 token = response.substr(token_pos + strlen(kTokenPrefix)); | |
| 148 else | |
| 149 LOG(ERROR) << "Failed to extract token."; | |
| 150 parameters_.callback.Run(token); | |
| 151 } | |
| 152 | |
| 153 } // namespace gcm | |
| OLD | NEW |