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/unregistration_request.h" | 5 #include "google_apis/gcm/engine/unregistration_request.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
(...skipping 18 matching lines...) Expand all Loading... | |
29 const char kRequestContentType[] = "application/x-www-form-urlencoded"; | 29 const char kRequestContentType[] = "application/x-www-form-urlencoded"; |
30 | 30 |
31 // Request constants. | 31 // Request constants. |
32 const char kCategoryKey[] = "app"; | 32 const char kCategoryKey[] = "app"; |
33 const char kSubtypeKey[] = "X-subtype"; | 33 const char kSubtypeKey[] = "X-subtype"; |
34 const char kDeleteKey[] = "delete"; | 34 const char kDeleteKey[] = "delete"; |
35 const char kDeleteValue[] = "true"; | 35 const char kDeleteValue[] = "true"; |
36 const char kDeviceIdKey[] = "device"; | 36 const char kDeviceIdKey[] = "device"; |
37 const char kLoginHeader[] = "AidLogin"; | 37 const char kLoginHeader[] = "AidLogin"; |
38 | 38 |
39 // Response constants. | |
40 const char kErrorPrefix[] = "Error="; | |
41 const char kInvalidParameters[] = "INVALID_PARAMETERS"; | |
42 const char kInternalServerError[] = "InternalServerError"; | |
43 const char kPhoneRegistrationError[] = "PHONE_REGISTRATION_ERROR"; | |
Nicolas Zea
2016/10/20 21:05:23
nit: Given this maps to DEVICE_REGISTRATION_ERROR,
johnme
2016/11/01 14:15:40
Done.
| |
44 | |
45 // Gets correct status from the error message. | |
46 UnregistrationRequest::Status GetStatusFromError(const std::string& error) { | |
47 if (error.find(kInvalidParameters) != std::string::npos) | |
48 return UnregistrationRequest::INVALID_PARAMETERS; | |
49 if (error.find(kInternalServerError) != std::string::npos) | |
50 return UnregistrationRequest::INTERNAL_SERVER_ERROR; | |
51 if (error.find(kPhoneRegistrationError) != std::string::npos) | |
52 return UnregistrationRequest::DEVICE_REGISTRATION_ERROR; | |
53 // Should not be reached, unless the server adds new error types. | |
54 return UnregistrationRequest::UNKNOWN_ERROR; | |
55 } | |
56 | |
39 // Determines whether to retry based on the status of the last request. | 57 // Determines whether to retry based on the status of the last request. |
40 bool ShouldRetryWithStatus(UnregistrationRequest::Status status) { | 58 bool ShouldRetryWithStatus(UnregistrationRequest::Status status) { |
41 switch (status) { | 59 switch (status) { |
42 case UnregistrationRequest::URL_FETCHING_FAILED: | 60 case UnregistrationRequest::URL_FETCHING_FAILED: |
43 case UnregistrationRequest::NO_RESPONSE_BODY: | 61 case UnregistrationRequest::NO_RESPONSE_BODY: |
44 case UnregistrationRequest::RESPONSE_PARSING_FAILED: | 62 case UnregistrationRequest::RESPONSE_PARSING_FAILED: |
45 case UnregistrationRequest::INCORRECT_APP_ID: | 63 case UnregistrationRequest::INCORRECT_APP_ID: |
46 case UnregistrationRequest::SERVICE_UNAVAILABLE: | 64 case UnregistrationRequest::SERVICE_UNAVAILABLE: |
47 case UnregistrationRequest::INTERNAL_SERVER_ERROR: | 65 case UnregistrationRequest::INTERNAL_SERVER_ERROR: |
48 case UnregistrationRequest::HTTP_NOT_OK: | 66 case UnregistrationRequest::HTTP_NOT_OK: |
49 return true; | 67 return true; |
50 case UnregistrationRequest::SUCCESS: | 68 case UnregistrationRequest::SUCCESS: |
51 case UnregistrationRequest::INVALID_PARAMETERS: | 69 case UnregistrationRequest::INVALID_PARAMETERS: |
70 case UnregistrationRequest::DEVICE_REGISTRATION_ERROR: | |
52 case UnregistrationRequest::UNKNOWN_ERROR: | 71 case UnregistrationRequest::UNKNOWN_ERROR: |
53 case UnregistrationRequest::REACHED_MAX_RETRIES: | 72 case UnregistrationRequest::REACHED_MAX_RETRIES: |
54 return false; | 73 return false; |
55 case UnregistrationRequest::UNREGISTRATION_STATUS_COUNT: | 74 case UnregistrationRequest::UNREGISTRATION_STATUS_COUNT: |
56 NOTREACHED(); | 75 NOTREACHED(); |
57 break; | 76 break; |
58 } | 77 } |
59 return false; | 78 return false; |
60 } | 79 } |
61 | 80 |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
152 body); | 171 body); |
153 BuildFormEncoding(kDeleteKey, kDeleteValue, body); | 172 BuildFormEncoding(kDeleteKey, kDeleteValue, body); |
154 | 173 |
155 DCHECK(custom_request_handler_.get()); | 174 DCHECK(custom_request_handler_.get()); |
156 custom_request_handler_->BuildRequestBody(body); | 175 custom_request_handler_->BuildRequestBody(body); |
157 } | 176 } |
158 | 177 |
159 UnregistrationRequest::Status UnregistrationRequest::ParseResponse( | 178 UnregistrationRequest::Status UnregistrationRequest::ParseResponse( |
160 const net::URLFetcher* source) { | 179 const net::URLFetcher* source) { |
161 if (!source->GetStatus().is_success()) { | 180 if (!source->GetStatus().is_success()) { |
162 DVLOG(1) << "Fetcher failed"; | 181 DVLOG(1) << "Unregistration URL fetching failed."; |
163 return URL_FETCHING_FAILED; | 182 return URL_FETCHING_FAILED; |
164 } | 183 } |
165 | 184 |
185 std::string response; | |
186 if (!source->GetResponseAsString(&response)) { | |
187 DVLOG(1) << "Failed to get unregistration response body."; | |
188 return NO_RESPONSE_BODY; | |
189 } | |
190 | |
191 // If we are able to parse a meaningful known error, let's do so. Note that | |
192 // some errors will have HTTP_OK response code! | |
193 if (response.find(kErrorPrefix) != std::string::npos) { | |
194 std::string error = response.substr( | |
195 response.find(kErrorPrefix) + arraysize(kErrorPrefix) - 1); | |
196 DVLOG(1) << "Unregistration response error message: " << error; | |
197 return GetStatusFromError(error); | |
198 } | |
199 | |
166 net::HttpStatusCode response_status = static_cast<net::HttpStatusCode>( | 200 net::HttpStatusCode response_status = static_cast<net::HttpStatusCode>( |
167 source->GetResponseCode()); | 201 source->GetResponseCode()); |
168 if (response_status != net::HTTP_OK) { | 202 if (response_status != net::HTTP_OK) { |
169 DVLOG(1) << "HTTP Status code is not OK, but: " << response_status; | 203 DVLOG(1) << "Unregistration HTTP response code not OK: " << response_status; |
170 if (response_status == net::HTTP_SERVICE_UNAVAILABLE) | 204 if (response_status == net::HTTP_SERVICE_UNAVAILABLE) |
171 return SERVICE_UNAVAILABLE; | 205 return SERVICE_UNAVAILABLE; |
172 if (response_status == net::HTTP_INTERNAL_SERVER_ERROR) | 206 if (response_status == net::HTTP_INTERNAL_SERVER_ERROR) |
173 return INTERNAL_SERVER_ERROR; | 207 return INTERNAL_SERVER_ERROR; |
174 return HTTP_NOT_OK; | 208 return HTTP_NOT_OK; |
175 } | 209 } |
176 | 210 |
177 DCHECK(custom_request_handler_.get()); | 211 DCHECK(custom_request_handler_.get()); |
178 return custom_request_handler_->ParseResponse(source); | 212 return custom_request_handler_->ParseResponse(response); |
179 } | 213 } |
180 | 214 |
181 void UnregistrationRequest::RetryWithBackoff() { | 215 void UnregistrationRequest::RetryWithBackoff() { |
182 DCHECK_GT(retries_left_, 0); | 216 DCHECK_GT(retries_left_, 0); |
183 --retries_left_; | 217 --retries_left_; |
184 url_fetcher_.reset(); | 218 url_fetcher_.reset(); |
185 backoff_entry_.InformOfRequest(false); | 219 backoff_entry_.InformOfRequest(false); |
186 | 220 |
187 DVLOG(1) << "Delaying GCM unregistration of app: " << request_info_.app_id() | 221 DVLOG(1) << "Delaying GCM unregistration of app: " << request_info_.app_id() |
188 << ", for " << backoff_entry_.GetTimeUntilRelease().InMilliseconds() | 222 << ", for " << backoff_entry_.GetTimeUntilRelease().InMilliseconds() |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
224 // Only REACHED_MAX_RETRIES is reported because the function will skip | 258 // Only REACHED_MAX_RETRIES is reported because the function will skip |
225 // reporting count and time when status is not SUCCESS. | 259 // reporting count and time when status is not SUCCESS. |
226 DCHECK(custom_request_handler_.get()); | 260 DCHECK(custom_request_handler_.get()); |
227 custom_request_handler_->ReportUMAs(status, 0, base::TimeDelta()); | 261 custom_request_handler_->ReportUMAs(status, 0, base::TimeDelta()); |
228 } | 262 } |
229 | 263 |
230 callback_.Run(status); | 264 callback_.Run(status); |
231 } | 265 } |
232 | 266 |
233 } // namespace gcm | 267 } // namespace gcm |
OLD | NEW |