OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "google_apis/gcm/engine/checkin_request.h" | 5 #include "google_apis/gcm/engine/checkin_request.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/metrics/histogram.h" | |
9 #include "google_apis/gcm/protocol/checkin.pb.h" | 10 #include "google_apis/gcm/protocol/checkin.pb.h" |
10 #include "net/http/http_status_code.h" | 11 #include "net/http/http_status_code.h" |
11 #include "net/url_request/url_fetcher.h" | 12 #include "net/url_request/url_fetcher.h" |
12 #include "net/url_request/url_request_status.h" | 13 #include "net/url_request/url_request_status.h" |
13 #include "url/gurl.h" | 14 #include "url/gurl.h" |
14 | 15 |
15 namespace gcm { | 16 namespace gcm { |
16 | 17 |
17 namespace { | 18 namespace { |
18 const char kCheckinURL[] = "https://android.clients.google.com/checkin"; | 19 const char kCheckinURL[] = "https://android.clients.google.com/checkin"; |
19 const char kRequestContentType[] = "application/x-protobuf"; | 20 const char kRequestContentType[] = "application/x-protobuf"; |
20 const int kRequestVersionValue = 2; | 21 const int kRequestVersionValue = 2; |
22 | |
23 // This enum is also used in an UMA histogram (GCMRegistrationRequestStatus | |
fgorski
2014/02/12 18:22:58
GCMRegistrationRequestStatus?
juyik
2014/02/12 18:31:54
Done.
| |
24 // enum defined in tools/metrics/histograms/histogram.xml). Hence the entries | |
25 // here shouldn't be deleted or re-ordered and new ones should be added to | |
26 // the end. | |
27 enum CheckinRequestStatus { | |
28 SUCCESS, // Checkin completed successfully. | |
29 URL_FETCHING_FAILED, // URL fetching failed. | |
30 HTTP_BAD_REQUEST, // The request was malformed. | |
31 HTTP_UNAUTHORIZED, // The security token didn't match the android id. | |
32 HTTP_NOT_OK, // HTTP status was not OK. | |
33 FAILED_PARSING_RESPONSE, // Failed to parse the checkin response. | |
fgorski
2014/02/12 18:22:58
RESPONSE_PARSIN_FAILED
will be consistent with URL
juyik
2014/02/12 18:31:54
Done.
juyik
2014/02/12 18:31:54
Done.
| |
34 ZERO_ID_OR_TOKEN, // Either returned android id or security token | |
35 // was zero. | |
36 // NOTE: always keep this entry at the end. Add new status types only | |
37 // immediately above this line. Make sure to update the corresponding | |
38 // histogram enum accordingly. | |
39 STATUS_COUNT | |
40 }; | |
41 | |
42 void RecordCheckinStatusToUMA(CheckinRequestStatus status) { | |
43 UMA_HISTOGRAM_ENUMERATION("GCM.CheckinRequestStatus", status, STATUS_COUNT); | |
44 } | |
45 | |
21 } // namespace | 46 } // namespace |
22 | 47 |
23 CheckinRequest::CheckinRequest( | 48 CheckinRequest::CheckinRequest( |
24 const CheckinRequestCallback& callback, | 49 const CheckinRequestCallback& callback, |
25 const net::BackoffEntry::Policy& backoff_policy, | 50 const net::BackoffEntry::Policy& backoff_policy, |
26 const checkin_proto::ChromeBuildProto& chrome_build_proto, | 51 const checkin_proto::ChromeBuildProto& chrome_build_proto, |
27 int64 user_serial_number, | 52 int64 user_serial_number, |
28 uint64 android_id, | 53 uint64 android_id, |
29 uint64 security_token, | 54 uint64 security_token, |
30 net::URLRequestContextGetter* request_context_getter) | 55 net::URLRequestContextGetter* request_context_getter) |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
87 } | 112 } |
88 | 113 |
89 Start(); | 114 Start(); |
90 } | 115 } |
91 | 116 |
92 void CheckinRequest::OnURLFetchComplete(const net::URLFetcher* source) { | 117 void CheckinRequest::OnURLFetchComplete(const net::URLFetcher* source) { |
93 std::string response_string; | 118 std::string response_string; |
94 checkin_proto::AndroidCheckinResponse response_proto; | 119 checkin_proto::AndroidCheckinResponse response_proto; |
95 if (!source->GetStatus().is_success()) { | 120 if (!source->GetStatus().is_success()) { |
96 LOG(ERROR) << "Failed to get checkin response. Fetcher failed. Retrying."; | 121 LOG(ERROR) << "Failed to get checkin response. Fetcher failed. Retrying."; |
122 RecordCheckinStatusToUMA(URL_FETCHING_FAILED); | |
97 RetryWithBackoff(true); | 123 RetryWithBackoff(true); |
98 return; | 124 return; |
99 } | 125 } |
100 | 126 |
101 net::HttpStatusCode response_status = static_cast<net::HttpStatusCode>( | 127 net::HttpStatusCode response_status = static_cast<net::HttpStatusCode>( |
102 source->GetResponseCode()); | 128 source->GetResponseCode()); |
103 if (response_status == net::HTTP_BAD_REQUEST || | 129 if (response_status == net::HTTP_BAD_REQUEST || |
104 response_status == net::HTTP_UNAUTHORIZED) { | 130 response_status == net::HTTP_UNAUTHORIZED) { |
105 // BAD_REQUEST indicates that the request was malformed. | 131 // BAD_REQUEST indicates that the request was malformed. |
106 // UNAUTHORIZED indicates that security token didn't match the android id. | 132 // UNAUTHORIZED indicates that security token didn't match the android id. |
107 LOG(ERROR) << "No point retrying the checkin with status: " | 133 LOG(ERROR) << "No point retrying the checkin with status: " |
108 << response_status << ". Checkin failed."; | 134 << response_status << ". Checkin failed."; |
135 if (response_status == net::HTTP_BAD_REQUEST) { | |
136 RecordCheckinStatusToUMA(HTTP_BAD_REQUEST); | |
137 } else { | |
138 RecordCheckinStatusToUMA(HTTP_UNAUTHORIZED); | |
139 } | |
109 callback_.Run(0,0); | 140 callback_.Run(0,0); |
110 return; | 141 return; |
111 } | 142 } |
112 | 143 |
113 if (response_status != net::HTTP_OK || | 144 if (response_status != net::HTTP_OK || |
114 !source->GetResponseAsString(&response_string) || | 145 !source->GetResponseAsString(&response_string) || |
115 !response_proto.ParseFromString(response_string)) { | 146 !response_proto.ParseFromString(response_string)) { |
116 LOG(ERROR) << "Failed to get checkin response. HTTP Status: " | 147 LOG(ERROR) << "Failed to get checkin response. HTTP Status: " |
117 << response_status << ". Retrying."; | 148 << response_status << ". Retrying."; |
149 if (response_status != net::HTTP_OK) { | |
150 RecordCheckinStatusToUMA(HTTP_NOT_OK); | |
151 } else { | |
152 RecordCheckinStatusToUMA(FAILED_PARSING_RESPONSE); | |
153 } | |
118 RetryWithBackoff(true); | 154 RetryWithBackoff(true); |
119 return; | 155 return; |
120 } | 156 } |
121 | 157 |
122 if (!response_proto.has_android_id() || | 158 if (!response_proto.has_android_id() || |
123 !response_proto.has_security_token() || | 159 !response_proto.has_security_token() || |
124 response_proto.android_id() == 0 || | 160 response_proto.android_id() == 0 || |
125 response_proto.security_token() == 0) { | 161 response_proto.security_token() == 0) { |
126 LOG(ERROR) << "Android ID or security token is 0. Retrying."; | 162 LOG(ERROR) << "Android ID or security token is 0. Retrying."; |
163 RecordCheckinStatusToUMA(ZERO_ID_OR_TOKEN); | |
127 RetryWithBackoff(true); | 164 RetryWithBackoff(true); |
128 return; | 165 return; |
129 } | 166 } |
130 | 167 |
168 RecordCheckinStatusToUMA(SUCCESS); | |
131 callback_.Run(response_proto.android_id(), response_proto.security_token()); | 169 callback_.Run(response_proto.android_id(), response_proto.security_token()); |
132 } | 170 } |
133 | 171 |
134 } // namespace gcm | 172 } // namespace gcm |
OLD | NEW |