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..a786d3986a4966a98b90875ca47a5afc6ddabc66 |
--- /dev/null |
+++ b/google_apis/gcm/engine/gcm_checkin_request.cc |
@@ -0,0 +1,95 @@ |
+// Copyright (c) 2014 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 "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 gcm { |
+ |
+namespace { |
+const char kCheckinURL[] = "https://android.clients.google.com/checkin"; |
+const char kRequestContentType[] = "application/x-protobuf"; |
+const int kRequestVersionValue = 2; |
+} // namespace |
+ |
+GCMCheckinRequest::GCMCheckinRequest( |
+ const CheckinRequestCallback& callback, |
+ const checkin_proto::ChromeBuildProto& chrome_build_proto, |
+ int64 user_serial_number, |
+ const GCMClient::CheckInInfo& checkin_info, |
+ net::URLRequestContextGetter* request_context_getter) |
+ : request_context_getter_(request_context_getter), |
+ callback_(callback), |
+ chrome_build_proto_(chrome_build_proto), |
+ 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_); |
+ request.set_version(kRequestVersionValue); |
+ |
+ checkin_proto::AndroidCheckinProto* checkin = request.mutable_checkin(); |
+#if defined(CHROME_OS) |
+ checkin->set_type(checkin_proto::DEVICE_CHROME_OS); |
+#else |
+ checkin->set_type(checkin_proto::DEVICE_CHROME_BROWSER); |
+#endif |
+ |
+ checkin_proto::ChromeBuildProto* chrome_build = |
+ checkin->mutable_chrome_build(); |
+ chrome_build->set_platform(chrome_build_proto_.platform()); |
+ chrome_build->set_channel(chrome_build_proto_.channel()); |
+ chrome_build->set_chrome_version(chrome_build_proto_.chrome_version()); |
+ |
+ 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(kRequestContentType, 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."; |
+ // TODO(fgorski): Handle retry logic for certain responses. |
+ callback_.Run(0, 0); |
+ return; |
+ } |
+ |
+ if (!response_proto.has_android_id() || |
+ !response_proto.has_security_token() || |
+ response_proto.android_id() == 0 || |
+ response_proto.security_token() == 0) { |
+ LOG(ERROR) << "Badly formatted checkin response."; |
+ callback_.Run(0, 0); |
+ return; |
+ } |
+ |
+ callback_.Run(response_proto.android_id(), response_proto.security_token()); |
+} |
+ |
+} // namespace gcm |