Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(993)

Unified Diff: google_apis/gcm/engine/registration_request.cc

Issue 126513007: GCM Registration Request implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Adding user android id to the request and renaming the serial number Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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
new file mode 100644
index 0000000000000000000000000000000000000000..79457cb0ad4f2223715fb29e30caceaa435b81b9
--- /dev/null
+++ b/google_apis/gcm/engine/registration_request.cc
@@ -0,0 +1,139 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "google_apis/gcm/engine/registration_request.h"
+
+#include "base/bind.h"
+#include "base/message_loop/message_loop.h"
+#include "base/strings/string_number_conversions.h"
+#include "base/values.h"
+#include "net/base/escape.h"
+#include "net/http/http_request_headers.h"
+#include "net/http/http_status_code.h"
+#include "net/url_request/url_fetcher.h"
+#include "net/url_request/url_request_context_getter.h"
+#include "net/url_request/url_request_status.h"
+#include "url/gurl.h"
+
+namespace gcm {
+
+namespace {
Nicolas Zea 2014/01/10 19:45:12 nit: newline after
fgorski 2014/01/10 22:46:58 Done.
+const char kRegistrationURL[] =
+ "https://android.clients.google.com/c2dm/register3";
+const char kRegistrationDataFormat[] = "application/x-www-form-urlencoded";
+
+// Request constants.
+const char kLoginHeader[] = "AidLogin";
+
+const char kAppIdKey[] = "app";
+const char kSenderKey[] = "sender";
+const char kCertKey[] = "cert";
+const char kDeviceIdKey[] = "device";
+const char kUserSerialNumberKey[] = "device_user_id";
+const char kUserAndroidIdKey[] = "X-GOOG.USER_AID";
+
+// Response constants.
+const char kTokenPrefix[] = "token=";
+
+void BuildFormEncoding(const std::string& key,
+ const std::string& value,
+ std::string* out) {
+ if (!out->empty())
+ out->append("&" + key + "=" + net::EscapeUrlEncodedData(value, true));
jianli 2014/01/10 20:17:43 It seems to be more efficient to write like: if
fgorski 2014/01/10 22:46:58 Done.
+ else
+ out->append(key + "=" + net::EscapeUrlEncodedData(value, true));
+}
+
+} // namespace
+
+RegistrationRequest::RegistrationRequest(
+ const RegistrationCallback& callback,
+ uint64 android_id,
+ uint64 security_token,
+ uint64 user_android_id,
+ int64 user_serial_number,
+ const std::string& package_id,
+ const std::string& cert,
+ const std::vector<std::string>& sender_ids,
+ const std::map<std::string, std::string>& extras,
+ scoped_refptr<net::URLRequestContextGetter> request_context_getter)
+ : callback_(callback),
+ android_id_(android_id),
+ security_token_(security_token),
+ package_id_(package_id),
+ sender_ids_(sender_ids),
+ cert_(cert),
+ user_serial_number_(user_serial_number),
+ user_android_id_(user_android_id),
+ request_context_getter_(request_context_getter) {}
+
+RegistrationRequest::~RegistrationRequest() {}
+
+void RegistrationRequest::Start() {
+ DCHECK(!url_fetcher_.get());
+ url_fetcher_.reset(net::URLFetcher::Create(
+ GURL(kRegistrationURL), net::URLFetcher::POST, this));
+ url_fetcher_->SetRequestContext(request_context_getter_);
+
+ std::string android_id = base::Uint64ToString(android_id_);
+ std::string auth_header =
+ std::string(net::HttpRequestHeaders::kAuthorization) + ": " +
+ kLoginHeader + " " + android_id + ":" +
+ base::Uint64ToString(security_token_);
+ url_fetcher_->SetExtraRequestHeaders(auth_header);
+
+ std::string body;
+ BuildFormEncoding(kAppIdKey, package_id_, &body);
+
+ std::string senders;
+ for (std::vector<std::string>::const_iterator iter = sender_ids_.begin();
+ iter != sender_ids_.end();
+ ++iter) {
+ if (!senders.empty())
+ senders.append(",");
+ senders.append(net::EscapeUrlEncodedData(*iter, true));
+ }
+ BuildFormEncoding(kSenderKey, senders, &body);
+
+ if (!cert_.empty())
+ BuildFormEncoding(kCertKey, cert_, &body);
+
+ BuildFormEncoding(kDeviceIdKey, android_id, &body);
+
+ if (user_serial_number_ != 0) {
+ DCHECK(user_android_id_ != 0);
+ BuildFormEncoding(
+ kUserSerialNumberKey, base::IntToString(user_serial_number_), &body);
+ BuildFormEncoding(
+ kUserAndroidIdKey, base::Uint64ToString(user_android_id_), &body);
+ }
+
+ DVLOG(1) << "Registration request: " << body;
+ url_fetcher_->SetUploadData(kRegistrationDataFormat, body);
+
+ DVLOG(1) << "Performing registration for: " << package_id_;
+ url_fetcher_->Start();
+}
+
+void RegistrationRequest::OnURLFetchComplete(const net::URLFetcher* source) {
+ std::string response;
+ if (!source->GetStatus().is_success() ||
+ source->GetResponseCode() != net::HTTP_OK ||
+ !source->GetResponseAsString(&response)) {
+ LOG(ERROR) << "Failed to get registration response.";
Nicolas Zea 2014/01/10 19:45:12 nit: add todo to have retry logic. It'll probably
fgorski 2014/01/10 22:46:58 Done.
+ callback_.Run("");
+ return;
+ }
+
+ DVLOG(1) << "Parsing registration response: " << response;
+ size_t token_pos = response.find(kTokenPrefix);
+ std::string token;
+ if (token_pos != std::string::npos)
+ token = response.substr(token_pos + strlen(kTokenPrefix));
+ else
+ LOG(ERROR) << "Failed to extract token.";
+ callback_.Run(token);
+}
+
+} // namespace gcm

Powered by Google App Engine
This is Rietveld 408576698