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/registration_request.h" | 5 #include "google_apis/gcm/engine/registration_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 "base/metrics/histogram.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 if (error == kInvalidParameters) | 63 if (error == kInvalidParameters) |
64 return RegistrationRequest::INVALID_PARAMETERS; | 64 return RegistrationRequest::INVALID_PARAMETERS; |
65 return RegistrationRequest::UNKNOWN_ERROR; | 65 return RegistrationRequest::UNKNOWN_ERROR; |
66 } | 66 } |
67 | 67 |
68 // Indicates whether a retry attempt should be made based on the status of the | 68 // Indicates whether a retry attempt should be made based on the status of the |
69 // last request. | 69 // last request. |
70 bool ShouldRetryWithStatus(RegistrationRequest::Status status) { | 70 bool ShouldRetryWithStatus(RegistrationRequest::Status status) { |
71 return status == RegistrationRequest::UNKNOWN_ERROR || | 71 return status == RegistrationRequest::UNKNOWN_ERROR || |
72 status == RegistrationRequest::AUTHENTICATION_FAILED || | 72 status == RegistrationRequest::AUTHENTICATION_FAILED || |
73 status == RegistrationRequest::DEVICE_REGISTRATION_ERROR; | 73 status == RegistrationRequest::DEVICE_REGISTRATION_ERROR || |
| 74 status == RegistrationRequest::HTTP_NOT_OK || |
| 75 status == RegistrationRequest::URL_FETCHING_FAILED || |
| 76 status == RegistrationRequest::RESPONSE_PARSING_FAILED; |
74 } | 77 } |
75 | 78 |
76 void RecordRegistrationStatusToUMA(RegistrationRequest::Status status) { | 79 void RecordRegistrationStatusToUMA(RegistrationRequest::Status status) { |
77 UMA_HISTOGRAM_ENUMERATION("GCM.RegistrationRequestStatus", status, | 80 UMA_HISTOGRAM_ENUMERATION("GCM.RegistrationRequestStatus", status, |
78 RegistrationRequest::STATUS_COUNT); | 81 RegistrationRequest::STATUS_COUNT); |
79 } | 82 } |
80 | 83 |
81 } // namespace | 84 } // namespace |
82 | 85 |
83 RegistrationRequest::RequestInfo::RequestInfo( | 86 RegistrationRequest::RequestInfo::RequestInfo( |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 base::Bind(&RegistrationRequest::RetryWithBackoff, | 171 base::Bind(&RegistrationRequest::RetryWithBackoff, |
169 weak_ptr_factory_.GetWeakPtr(), | 172 weak_ptr_factory_.GetWeakPtr(), |
170 false), | 173 false), |
171 backoff_entry_.GetTimeUntilRelease()); | 174 backoff_entry_.GetTimeUntilRelease()); |
172 return; | 175 return; |
173 } | 176 } |
174 | 177 |
175 Start(); | 178 Start(); |
176 } | 179 } |
177 | 180 |
178 void RegistrationRequest::OnURLFetchComplete(const net::URLFetcher* source) { | 181 RegistrationRequest::Status RegistrationRequest::ParseResponse( |
| 182 const net::URLFetcher* source, std::string* token) { |
| 183 if (!source->GetStatus().is_success()) { |
| 184 LOG(ERROR) << "URL fetching failed."; |
| 185 return URL_FETCHING_FAILED; |
| 186 } |
| 187 if (source->GetResponseCode() != net::HTTP_OK) { |
| 188 LOG(ERROR) << "URL fetching HTTP response code is not OK. It is " |
| 189 << source->GetResponseCode(); |
| 190 return HTTP_NOT_OK; |
| 191 } |
179 std::string response; | 192 std::string response; |
180 if (!source->GetStatus().is_success() || | 193 if (!source->GetResponseAsString(&response)) { |
181 source->GetResponseCode() != net::HTTP_OK || | 194 LOG(ERROR) << "Failed to parse registration response as a string."; |
182 !source->GetResponseAsString(&response)) { | 195 return RESPONSE_PARSING_FAILED; |
183 LOG(ERROR) << "Failed to get registration response: " | |
184 << source->GetStatus().is_success() << " " | |
185 << source->GetResponseCode(); | |
186 RetryWithBackoff(true); | |
187 return; | |
188 } | 196 } |
189 | 197 |
190 DVLOG(1) << "Parsing registration response: " << response; | 198 DVLOG(1) << "Parsing registration response: " << response; |
191 size_t token_pos = response.find(kTokenPrefix); | 199 size_t token_pos = response.find(kTokenPrefix); |
192 if (token_pos != std::string::npos) { | 200 if (token_pos != std::string::npos) { |
193 std::string token = | 201 *token = response.substr(token_pos + arraysize(kTokenPrefix) - 1); |
194 response.substr(token_pos + arraysize(kTokenPrefix) - 1); | 202 return SUCCESS; |
195 RecordRegistrationStatusToUMA(SUCCESS); | |
196 callback_.Run(SUCCESS, token); | |
197 return; | |
198 } | 203 } |
199 | 204 |
200 size_t error_pos = response.find(kErrorPrefix); | 205 size_t error_pos = response.find(kErrorPrefix); |
201 Status status = UNKNOWN_ERROR; | 206 if (error_pos == std::string::npos) |
202 if (error_pos != std::string::npos) { | 207 return UNKNOWN_ERROR; |
203 std::string error = | 208 std::string error = response.substr(error_pos + arraysize(kErrorPrefix) - 1); |
204 response.substr(error_pos + arraysize(kErrorPrefix) - 1); | 209 return GetStatusFromError(error); |
205 status = GetStatusFromError(error); | 210 } |
206 } | |
207 RecordRegistrationStatusToUMA(status); | |
208 | 211 |
209 if (ShouldRetryWithStatus(status)) { | 212 void RegistrationRequest::OnURLFetchComplete(const net::URLFetcher* source) { |
| 213 std::string token; |
| 214 Status status = ParseResponse(source, &token); |
| 215 RecordRegistrationStatusToUMA(status ); |
| 216 if (ShouldRetryWithStatus(status)) |
210 RetryWithBackoff(true); | 217 RetryWithBackoff(true); |
211 return; | 218 else |
212 } | 219 callback_.Run(status, token); |
213 | |
214 callback_.Run(status, std::string()); | |
215 } | 220 } |
216 | 221 |
217 } // namespace gcm | 222 } // namespace gcm |
OLD | NEW |