OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014 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/checkin_request.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" |
| 9 #include "google_apis/gcm/protocol/checkin.pb.h" |
| 10 #include "net/http/http_status_code.h" |
| 11 #include "net/url_request/url_fetcher.h" |
| 12 #include "net/url_request/url_request_status.h" |
| 13 #include "url/gurl.h" |
| 14 |
| 15 namespace gcm { |
| 16 |
| 17 namespace { |
| 18 const char kCheckinURL[] = "https://android.clients.google.com/checkin"; |
| 19 const char kRequestContentType[] = "application/x-protobuf"; |
| 20 const int kRequestVersionValue = 2; |
| 21 } // namespace |
| 22 |
| 23 CheckinRequest::CheckinRequest( |
| 24 const CheckinRequestCallback& callback, |
| 25 const checkin_proto::ChromeBuildProto& chrome_build_proto, |
| 26 int64 user_serial_number, |
| 27 uint64 android_id, |
| 28 uint64 security_token, |
| 29 net::URLRequestContextGetter* request_context_getter) |
| 30 : request_context_getter_(request_context_getter), |
| 31 callback_(callback), |
| 32 chrome_build_proto_(chrome_build_proto), |
| 33 android_id_(android_id), |
| 34 security_token_(security_token), |
| 35 user_serial_number_(user_serial_number) {} |
| 36 |
| 37 CheckinRequest::~CheckinRequest() {} |
| 38 |
| 39 void CheckinRequest::Start() { |
| 40 DCHECK(!url_fetcher_.get()); |
| 41 |
| 42 checkin_proto::AndroidCheckinRequest request; |
| 43 request.set_id(android_id_); |
| 44 request.set_security_token(security_token_); |
| 45 request.set_user_serial_number(user_serial_number_); |
| 46 request.set_version(kRequestVersionValue); |
| 47 |
| 48 checkin_proto::AndroidCheckinProto* checkin = request.mutable_checkin(); |
| 49 checkin->mutable_chrome_build()->CopyFrom(chrome_build_proto_); |
| 50 #if defined(CHROME_OS) |
| 51 checkin->set_type(checkin_proto::DEVICE_CHROME_OS); |
| 52 #else |
| 53 checkin->set_type(checkin_proto::DEVICE_CHROME_BROWSER); |
| 54 #endif |
| 55 |
| 56 |
| 57 std::string upload_data; |
| 58 CHECK(request.SerializeToString(&upload_data)); |
| 59 |
| 60 url_fetcher_.reset( |
| 61 net::URLFetcher::Create(GURL(kCheckinURL), net::URLFetcher::POST, this)); |
| 62 url_fetcher_->SetRequestContext(request_context_getter_); |
| 63 url_fetcher_->SetUploadData(kRequestContentType, upload_data); |
| 64 url_fetcher_->Start(); |
| 65 } |
| 66 |
| 67 void CheckinRequest::OnURLFetchComplete(const net::URLFetcher* source) { |
| 68 std::string response_string; |
| 69 checkin_proto::AndroidCheckinResponse response_proto; |
| 70 if (!source->GetStatus().is_success() || |
| 71 source->GetResponseCode() != net::HTTP_OK || |
| 72 !source->GetResponseAsString(&response_string) || |
| 73 !response_proto.ParseFromString(response_string)) { |
| 74 LOG(ERROR) << "Failed to get checkin response."; |
| 75 // TODO(fgorski): Handle retry logic for certain responses. |
| 76 callback_.Run(0, 0); |
| 77 return; |
| 78 } |
| 79 |
| 80 if (!response_proto.has_android_id() || |
| 81 !response_proto.has_security_token() || |
| 82 response_proto.android_id() == 0 || |
| 83 response_proto.security_token() == 0) { |
| 84 LOG(ERROR) << "Badly formatted checkin response."; |
| 85 callback_.Run(0, 0); |
| 86 return; |
| 87 } |
| 88 |
| 89 callback_.Run(response_proto.android_id(), response_proto.security_token()); |
| 90 } |
| 91 |
| 92 } // namespace gcm |
OLD | NEW |