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 const GCMClient::CheckinInfo& checkin_info, | |
28 net::URLRequestContextGetter* request_context_getter) | |
29 : request_context_getter_(request_context_getter), | |
30 callback_(callback), | |
31 chrome_build_proto_(chrome_build_proto), | |
32 android_id_(checkin_info.android_id), | |
33 security_token_(checkin_info.secret), | |
34 user_serial_number_(user_serial_number) {} | |
35 | |
36 CheckinRequest::~CheckinRequest() {} | |
37 | |
38 void CheckinRequest::Start() { | |
39 DCHECK(!url_fetcher_.get()); | |
40 | |
41 checkin_proto::AndroidCheckinRequest request; | |
42 request.set_id(android_id_); | |
43 request.set_security_token(security_token_); | |
44 request.set_user_serial_number(user_serial_number_); | |
45 request.set_version(kRequestVersionValue); | |
46 | |
47 checkin_proto::AndroidCheckinProto* checkin = request.mutable_checkin(); | |
48 #if defined(CHROME_OS) | |
49 checkin->set_type(checkin_proto::DEVICE_CHROME_OS); | |
50 #else | |
51 checkin->set_type(checkin_proto::DEVICE_CHROME_BROWSER); | |
52 #endif | |
53 | |
54 checkin_proto::ChromeBuildProto* chrome_build = | |
55 checkin->mutable_chrome_build(); | |
Nicolas Zea
2014/01/07 00:41:52
can't you actually just do checkin->mutable_chrome
fgorski
2014/01/07 23:46:44
Done.
| |
56 chrome_build->set_platform(chrome_build_proto_.platform()); | |
57 chrome_build->set_channel(chrome_build_proto_.channel()); | |
58 chrome_build->set_chrome_version(chrome_build_proto_.chrome_version()); | |
59 | |
60 std::string upload_data; | |
61 CHECK(request.SerializeToString(&upload_data)); | |
62 | |
63 url_fetcher_.reset( | |
64 net::URLFetcher::Create(GURL(kCheckinURL), net::URLFetcher::POST, this)); | |
65 url_fetcher_->SetRequestContext(request_context_getter_); | |
66 url_fetcher_->SetUploadData(kRequestContentType, upload_data); | |
67 url_fetcher_->Start(); | |
68 } | |
69 | |
70 void CheckinRequest::OnURLFetchComplete(const net::URLFetcher* source) { | |
71 std::string response_string; | |
72 checkin_proto::AndroidCheckinResponse response_proto; | |
73 if (!source->GetStatus().is_success() || | |
74 source->GetResponseCode() != net::HTTP_OK || | |
75 !source->GetResponseAsString(&response_string) || | |
76 !response_proto.ParseFromString(response_string)) { | |
77 LOG(ERROR) << "Failed to get checkin response."; | |
78 // TODO(fgorski): Handle retry logic for certain responses. | |
79 callback_.Run(0, 0); | |
80 return; | |
81 } | |
82 | |
83 if (!response_proto.has_android_id() || | |
84 !response_proto.has_security_token() || | |
85 response_proto.android_id() == 0 || | |
86 response_proto.security_token() == 0) { | |
87 LOG(ERROR) << "Badly formatted checkin response."; | |
88 callback_.Run(0, 0); | |
89 return; | |
90 } | |
91 | |
92 callback_.Run(response_proto.android_id(), response_proto.security_token()); | |
93 } | |
94 | |
95 } // namespace gcm | |
OLD | NEW |