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

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

Issue 98173009: GCM Checkin implementation with unit tests and protobufs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@gcm-store
Patch Set: Adding protobufs, request/response implementation and unit tests Created 6 years, 12 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/gcm_checkin_request.cc
diff --git a/google_apis/gcm/engine/gcm_checkin_request.cc b/google_apis/gcm/engine/gcm_checkin_request.cc
new file mode 100644
index 0000000000000000000000000000000000000000..1f9da8ede37373168f13f8b7e1f8433a0277b470
--- /dev/null
+++ b/google_apis/gcm/engine/gcm_checkin_request.cc
@@ -0,0 +1,107 @@
+// 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/gcm_checkin_request.h"
+
+#include "base/bind.h"
+#include "base/message_loop/message_loop.h"
+#include "base/values.h"
+#include "google_apis/gcm/protocol/checkin.pb.h"
+#include "net/http/http_status_code.h"
+#include "net/url_request/url_fetcher.h"
+#include "net/url_request/url_request_status.h"
+#include "url/gurl.h"
+
+namespace checkin_proto {
+class AndroidCheckinProto;
+class AndroidCheckinRequest;
+class ChromeBuildProto;
+};
+
+namespace gcm {
+
+namespace {
+const char kCheckinURL[] = "https://android.clients.google.com/checkin";
+
+// Checkin request constants.
+const char kRequestDataFormat[] = "application/x-protobuf";
jianli 2014/01/04 01:34:10 nit: kRequestContentType
fgorski 2014/01/06 20:34:12 Done.
+const int kRequestVersionValue = 2;
+
+} // namespace
+
+GCMCheckinRequest::GCMCheckinRequest(
+ const base::Callback<void(uint64, uint64)>& callback,
+ net::URLRequestContextGetter* request_context_getter,
+ const GCMClient::ChromeCheckInInfo& chrome_checkin_info,
+ int64 user_serial_number,
+ const GCMClient::CheckInInfo& checkin_info)
+ : request_context_getter_(request_context_getter),
+ callback_(callback),
+ chrome_checkin_info_(chrome_checkin_info),
+ android_id_(checkin_info.android_id),
+ security_token_(checkin_info.secret),
+ user_serial_number_(user_serial_number) {}
+
+GCMCheckinRequest::~GCMCheckinRequest() {}
+
+void GCMCheckinRequest::Start() {
+ DCHECK(!url_fetcher_.get());
+
+ checkin_proto::AndroidCheckinRequest request;
+ request.set_id(android_id_);
+ request.set_security_token(security_token_);
+ request.set_user_serial_number(user_serial_number_);
+
+ checkin_proto::ChromeBuildProto* chrome_build =
+ new checkin_proto::ChromeBuildProto();
+ chrome_build->set_platform(chrome_checkin_info_.platform);
+ chrome_build->set_channel(chrome_checkin_info_.channel);
+ chrome_build->set_chrome_version(chrome_checkin_info_.chrome_version);
+
+ checkin_proto::AndroidCheckinProto* checkin =
+ new checkin_proto::AndroidCheckinProto();
Nicolas Zea 2014/01/04 01:40:19 you can just do request->mutable_checkin_proto and
fgorski 2014/01/06 20:34:12 Yep. That is my first time manipulating complex pr
+ checkin->set_allocated_chrome_build(chrome_build);
+#if defined(CHROME_OS)
+ checkin->set_type(checkin_proto::DEVICE_CHROME_OS);
+#else
+ checkin->set_type(checkin_proto::DEVICE_CHROME_BROWSER);
+#endif
+
+ request.set_allocated_checkin(checkin);
+ request.set_version(kRequestVersionValue);
+
+ std::string upload_data;
+ CHECK(request.SerializeToString(&upload_data));
+
+ url_fetcher_.reset(
+ net::URLFetcher::Create(GURL(kCheckinURL), net::URLFetcher::POST, this));
+ url_fetcher_->SetRequestContext(request_context_getter_);
+ url_fetcher_->SetUploadData(kRequestDataFormat, upload_data);
+ url_fetcher_->Start();
+}
+
+void GCMCheckinRequest::OnURLFetchComplete(const net::URLFetcher* source) {
+ std::string response_string;
+ checkin_proto::AndroidCheckinResponse response_proto;
+ if (!source->GetStatus().is_success() ||
+ source->GetResponseCode() != net::HTTP_OK ||
+ !source->GetResponseAsString(&response_string) ||
+ !response_proto.ParseFromString(response_string)) {
+ LOG(ERROR) << "Failed to get checkin response.";
+ callback_.Run(0, 0);
Nicolas Zea 2014/01/04 01:40:19 I wonder if we should have the retry logic built i
fgorski 2014/01/06 20:34:12 Let's add that in a separate step.
+ return;
+ }
+
+ if (!response_proto.has_android_id() ||
+ !response_proto.has_security_token() ||
+ response_proto.android_id() <= 0) {
jianli 2014/01/04 01:34:10 android_id is uint64. So the check should be == 0.
fgorski 2014/01/06 20:34:12 As mentioned elsewhere, I suspect it actually is i
+ LOG(ERROR) << "Badly formatted checkin response.";
Nicolas Zea 2014/01/04 01:40:19 Will these always be there on non-first time check
fgorski 2014/01/06 20:34:12 What do you mean. Even if it is the first time che
+ callback_.Run(0, 0);
+ return;
+ }
+
+ callback_.Run(response_proto.android_id(), response_proto.security_token());
+}
+
+} // namespace gcm

Powered by Google App Engine
This is Rietveld 408576698