OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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/gcm_checkin_request.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/message_loop/message_loop.h" | |
9 #include "base/values.h" | |
10 #include "google_apis/gcm/protocol/checkin.pb.h" | |
11 #include "net/http/http_status_code.h" | |
12 #include "net/url_request/url_fetcher.h" | |
13 #include "net/url_request/url_request_status.h" | |
14 #include "url/gurl.h" | |
15 | |
16 namespace checkin_proto { | |
17 class AndroidCheckinProto; | |
18 class AndroidCheckinRequest; | |
19 class ChromeBuildProto; | |
20 }; | |
21 | |
22 namespace gcm { | |
23 | |
24 namespace { | |
25 const char kCheckinURL[] = "https://android.clients.google.com/checkin"; | |
26 | |
27 // Checkin request constants. | |
28 const char kRequestDataFormat[] = "application/x-protobuf"; | |
jianli
2014/01/04 01:34:10
nit: kRequestContentType
fgorski
2014/01/06 20:34:12
Done.
| |
29 const int kRequestVersionValue = 2; | |
30 | |
31 } // namespace | |
32 | |
33 GCMCheckinRequest::GCMCheckinRequest( | |
34 const base::Callback<void(uint64, uint64)>& callback, | |
35 net::URLRequestContextGetter* request_context_getter, | |
36 const GCMClient::ChromeCheckInInfo& chrome_checkin_info, | |
37 int64 user_serial_number, | |
38 const GCMClient::CheckInInfo& checkin_info) | |
39 : request_context_getter_(request_context_getter), | |
40 callback_(callback), | |
41 chrome_checkin_info_(chrome_checkin_info), | |
42 android_id_(checkin_info.android_id), | |
43 security_token_(checkin_info.secret), | |
44 user_serial_number_(user_serial_number) {} | |
45 | |
46 GCMCheckinRequest::~GCMCheckinRequest() {} | |
47 | |
48 void GCMCheckinRequest::Start() { | |
49 DCHECK(!url_fetcher_.get()); | |
50 | |
51 checkin_proto::AndroidCheckinRequest request; | |
52 request.set_id(android_id_); | |
53 request.set_security_token(security_token_); | |
54 request.set_user_serial_number(user_serial_number_); | |
55 | |
56 checkin_proto::ChromeBuildProto* chrome_build = | |
57 new checkin_proto::ChromeBuildProto(); | |
58 chrome_build->set_platform(chrome_checkin_info_.platform); | |
59 chrome_build->set_channel(chrome_checkin_info_.channel); | |
60 chrome_build->set_chrome_version(chrome_checkin_info_.chrome_version); | |
61 | |
62 checkin_proto::AndroidCheckinProto* checkin = | |
63 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
| |
64 checkin->set_allocated_chrome_build(chrome_build); | |
65 #if defined(CHROME_OS) | |
66 checkin->set_type(checkin_proto::DEVICE_CHROME_OS); | |
67 #else | |
68 checkin->set_type(checkin_proto::DEVICE_CHROME_BROWSER); | |
69 #endif | |
70 | |
71 request.set_allocated_checkin(checkin); | |
72 request.set_version(kRequestVersionValue); | |
73 | |
74 std::string upload_data; | |
75 CHECK(request.SerializeToString(&upload_data)); | |
76 | |
77 url_fetcher_.reset( | |
78 net::URLFetcher::Create(GURL(kCheckinURL), net::URLFetcher::POST, this)); | |
79 url_fetcher_->SetRequestContext(request_context_getter_); | |
80 url_fetcher_->SetUploadData(kRequestDataFormat, upload_data); | |
81 url_fetcher_->Start(); | |
82 } | |
83 | |
84 void GCMCheckinRequest::OnURLFetchComplete(const net::URLFetcher* source) { | |
85 std::string response_string; | |
86 checkin_proto::AndroidCheckinResponse response_proto; | |
87 if (!source->GetStatus().is_success() || | |
88 source->GetResponseCode() != net::HTTP_OK || | |
89 !source->GetResponseAsString(&response_string) || | |
90 !response_proto.ParseFromString(response_string)) { | |
91 LOG(ERROR) << "Failed to get checkin response."; | |
92 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.
| |
93 return; | |
94 } | |
95 | |
96 if (!response_proto.has_android_id() || | |
97 !response_proto.has_security_token() || | |
98 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
| |
99 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
| |
100 callback_.Run(0, 0); | |
101 return; | |
102 } | |
103 | |
104 callback_.Run(response_proto.android_id(), response_proto.security_token()); | |
105 } | |
106 | |
107 } // namespace gcm | |
OLD | NEW |