1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
// Copyright 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/checkin_request.h"
#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "base/metrics/histogram.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;
const int kDefaultUserSerialNumber = 0;
// This enum is also used in an UMA histogram (GCMCheckinRequestStatus
// enum defined in tools/metrics/histograms/histogram.xml). Hence the entries
// here shouldn't be deleted or re-ordered and new ones should be added to
// the end.
enum CheckinRequestStatus {
SUCCESS, // Checkin completed successfully.
URL_FETCHING_FAILED, // URL fetching failed.
HTTP_BAD_REQUEST, // The request was malformed.
HTTP_UNAUTHORIZED, // The security token didn't match the android id.
HTTP_NOT_OK, // HTTP status was not OK.
RESPONSE_PARSING_FAILED, // Check in response parsing failed.
ZERO_ID_OR_TOKEN, // Either returned android id or security token
// was zero.
// NOTE: always keep this entry at the end. Add new status types only
// immediately above this line. Make sure to update the corresponding
// histogram enum accordingly.
STATUS_COUNT
};
void RecordCheckinStatusToUMA(CheckinRequestStatus status) {
UMA_HISTOGRAM_ENUMERATION("GCM.CheckinRequestStatus", status, STATUS_COUNT);
}
} // namespace
CheckinRequest::CheckinRequest(
const CheckinRequestCallback& callback,
const net::BackoffEntry::Policy& backoff_policy,
const checkin_proto::ChromeBuildProto& chrome_build_proto,
uint64 android_id,
uint64 security_token,
net::URLRequestContextGetter* request_context_getter)
: request_context_getter_(request_context_getter),
callback_(callback),
backoff_entry_(&backoff_policy),
chrome_build_proto_(chrome_build_proto),
android_id_(android_id),
security_token_(security_token),
weak_ptr_factory_(this) {
}
CheckinRequest::~CheckinRequest() {}
void CheckinRequest::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(kDefaultUserSerialNumber);
request.set_version(kRequestVersionValue);
checkin_proto::AndroidCheckinProto* checkin = request.mutable_checkin();
checkin->mutable_chrome_build()->CopyFrom(chrome_build_proto_);
#if defined(CHROME_OS)
checkin->set_type(checkin_proto::DEVICE_CHROME_OS);
#else
checkin->set_type(checkin_proto::DEVICE_CHROME_BROWSER);
#endif
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 CheckinRequest::RetryWithBackoff(bool update_backoff) {
if (update_backoff) {
backoff_entry_.InformOfRequest(false);
url_fetcher_.reset();
}
if (backoff_entry_.ShouldRejectRequest()) {
DVLOG(1) << "Delay GCM checkin for: "
<< backoff_entry_.GetTimeUntilRelease().InMilliseconds()
<< " milliseconds.";
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&CheckinRequest::RetryWithBackoff,
weak_ptr_factory_.GetWeakPtr(),
false),
backoff_entry_.GetTimeUntilRelease());
return;
}
Start();
}
void CheckinRequest::OnURLFetchComplete(const net::URLFetcher* source) {
std::string response_string;
checkin_proto::AndroidCheckinResponse response_proto;
if (!source->GetStatus().is_success()) {
LOG(ERROR) << "Failed to get checkin response. Fetcher failed. Retrying.";
RecordCheckinStatusToUMA(URL_FETCHING_FAILED);
RetryWithBackoff(true);
return;
}
net::HttpStatusCode response_status = static_cast<net::HttpStatusCode>(
source->GetResponseCode());
if (response_status == net::HTTP_BAD_REQUEST ||
response_status == net::HTTP_UNAUTHORIZED) {
// BAD_REQUEST indicates that the request was malformed.
// UNAUTHORIZED indicates that security token didn't match the android id.
LOG(ERROR) << "No point retrying the checkin with status: "
<< response_status << ". Checkin failed.";
RecordCheckinStatusToUMA(response_status == net::HTTP_BAD_REQUEST ?
HTTP_BAD_REQUEST : HTTP_UNAUTHORIZED);
callback_.Run(0,0);
return;
}
if (response_status != net::HTTP_OK ||
!source->GetResponseAsString(&response_string) ||
!response_proto.ParseFromString(response_string)) {
LOG(ERROR) << "Failed to get checkin response. HTTP Status: "
<< response_status << ". Retrying.";
RecordCheckinStatusToUMA(response_status != net::HTTP_OK ?
HTTP_NOT_OK : RESPONSE_PARSING_FAILED);
RetryWithBackoff(true);
return;
}
if (!response_proto.has_android_id() ||
!response_proto.has_security_token() ||
response_proto.android_id() == 0 ||
response_proto.security_token() == 0) {
LOG(ERROR) << "Android ID or security token is 0. Retrying.";
RecordCheckinStatusToUMA(ZERO_ID_OR_TOKEN);
RetryWithBackoff(true);
return;
}
RecordCheckinStatusToUMA(SUCCESS);
callback_.Run(response_proto.android_id(), response_proto.security_token());
}
} // namespace gcm
|