Chromium Code Reviews| Index: google_apis/gcm/engine/unregistration_request.cc |
| diff --git a/google_apis/gcm/engine/unregistration_request.cc b/google_apis/gcm/engine/unregistration_request.cc |
| index 9b337786f3e8a9c985fffd4206e242eeeb6566de..bd1dfe1a751ef14f87fe9912e0be460603e4100d 100644 |
| --- a/google_apis/gcm/engine/unregistration_request.cc |
| +++ b/google_apis/gcm/engine/unregistration_request.cc |
| @@ -10,6 +10,7 @@ |
| #include "base/strings/string_number_conversions.h" |
| #include "base/strings/string_piece.h" |
| #include "base/values.h" |
| +#include "google_apis/gcm/base/gcm_util.h" |
| #include "google_apis/gcm/monitoring/gcm_stats_recorder.h" |
| #include "net/base/escape.h" |
| #include "net/http/http_request_headers.h" |
| @@ -30,71 +31,6 @@ const char kDeleteKey[] = "delete"; |
| const char kDeleteValue[] = "true"; |
| const char kDeviceIdKey[] = "device"; |
| const char kLoginHeader[] = "AidLogin"; |
| -const char kUnregistrationCallerKey[] = "gcm_unreg_caller"; |
| -// We are going to set the value to "false" in order to forcefully unregister |
| -// the application. |
| -const char kUnregistrationCallerValue[] = "false"; |
| - |
| -// Response constants. |
| -const char kDeletedPrefix[] = "deleted="; |
| -const char kErrorPrefix[] = "Error="; |
| -const char kInvalidParameters[] = "INVALID_PARAMETERS"; |
| - |
| - |
| -void BuildFormEncoding(const std::string& key, |
| - const std::string& value, |
| - std::string* out) { |
| - if (!out->empty()) |
| - out->append("&"); |
| - out->append(key + "=" + net::EscapeUrlEncodedData(value, true)); |
| -} |
| - |
| -UnregistrationRequest::Status ParseFetcherResponse( |
| - const net::URLFetcher* source, |
| - std::string request_app_id) { |
| - if (!source->GetStatus().is_success()) { |
| - DVLOG(1) << "Fetcher failed"; |
| - return UnregistrationRequest::URL_FETCHING_FAILED; |
| - } |
| - |
| - net::HttpStatusCode response_status = static_cast<net::HttpStatusCode>( |
| - source->GetResponseCode()); |
| - if (response_status != net::HTTP_OK) { |
| - DVLOG(1) << "HTTP Status code is not OK, but: " << response_status; |
| - if (response_status == net::HTTP_SERVICE_UNAVAILABLE) |
| - return UnregistrationRequest::SERVICE_UNAVAILABLE; |
| - else if (response_status == net::HTTP_INTERNAL_SERVER_ERROR) |
| - return UnregistrationRequest::INTERNAL_SERVER_ERROR; |
| - return UnregistrationRequest::HTTP_NOT_OK; |
| - } |
| - |
| - std::string response; |
| - if (!source->GetResponseAsString(&response)) { |
| - DVLOG(1) << "Failed to get response body."; |
| - return UnregistrationRequest::NO_RESPONSE_BODY; |
| - } |
| - |
| - DVLOG(1) << "Parsing unregistration response."; |
| - if (response.find(kDeletedPrefix) != std::string::npos) { |
| - std::string app_id = response.substr( |
| - response.find(kDeletedPrefix) + arraysize(kDeletedPrefix) - 1); |
| - if (app_id == request_app_id) |
| - return UnregistrationRequest::SUCCESS; |
| - return UnregistrationRequest::INCORRECT_APP_ID; |
| - } |
| - |
| - if (response.find(kErrorPrefix) != std::string::npos) { |
| - std::string error = response.substr( |
| - response.find(kErrorPrefix) + arraysize(kErrorPrefix) - 1); |
| - if (error == kInvalidParameters) |
| - return UnregistrationRequest::INVALID_PARAMETERS; |
| - return UnregistrationRequest::UNKNOWN_ERROR; |
| - } |
| - |
| - DVLOG(1) << "Not able to parse a meaningful output from response body." |
| - << response; |
| - return UnregistrationRequest::RESPONSE_PARSING_FAILED; |
| -} |
| } // namespace |
| @@ -105,19 +41,26 @@ UnregistrationRequest::RequestInfo::RequestInfo( |
| : android_id(android_id), |
| security_token(security_token), |
| app_id(app_id) { |
| + DCHECK(android_id != 0UL && security_token != 0UL); |
|
Nicolas Zea
2015/05/26 16:39:15
nit: have as two separate DCHECKS
jianli
2015/05/26 20:49:39
Done.
|
| } |
| UnregistrationRequest::RequestInfo::~RequestInfo() {} |
| +UnregistrationRequest::CustomRequestHandler::CustomRequestHandler() {} |
| + |
| +UnregistrationRequest::CustomRequestHandler::~CustomRequestHandler() {} |
| + |
| UnregistrationRequest::UnregistrationRequest( |
| const GURL& registration_url, |
| const RequestInfo& request_info, |
| + scoped_ptr<CustomRequestHandler> custom_request_handler, |
| const net::BackoffEntry::Policy& backoff_policy, |
| const UnregistrationCallback& callback, |
| scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
| GCMStatsRecorder* recorder) |
| : callback_(callback), |
| request_info_(request_info), |
| + custom_request_handler_(custom_request_handler.Pass()), |
| registration_url_(registration_url), |
| backoff_entry_(&backoff_policy), |
| request_context_getter_(request_context_getter), |
| @@ -129,30 +72,18 @@ UnregistrationRequest::~UnregistrationRequest() {} |
| void UnregistrationRequest::Start() { |
| DCHECK(!callback_.is_null()); |
| - DCHECK(request_info_.android_id != 0UL); |
| - DCHECK(request_info_.security_token != 0UL); |
| DCHECK(!url_fetcher_.get()); |
| url_fetcher_ = |
| net::URLFetcher::Create(registration_url_, net::URLFetcher::POST, this); |
| url_fetcher_->SetRequestContext(request_context_getter_.get()); |
| - std::string android_id = base::Uint64ToString(request_info_.android_id); |
| - std::string auth_header = |
| - std::string(kLoginHeader) + " " + android_id + ":" + |
| - base::Uint64ToString(request_info_.security_token); |
| - net::HttpRequestHeaders headers; |
| - headers.SetHeader(net::HttpRequestHeaders::kAuthorization, auth_header); |
| - headers.SetHeader(kAppIdKey, request_info_.app_id); |
| - url_fetcher_->SetExtraRequestHeaders(headers.ToString()); |
| + std::string extra_headers; |
| + BuildRequestHeaders(&extra_headers); |
| + url_fetcher_->SetExtraRequestHeaders(extra_headers); |
| std::string body; |
| - BuildFormEncoding(kAppIdKey, request_info_.app_id, &body); |
| - BuildFormEncoding(kDeviceIdKey, android_id, &body); |
| - BuildFormEncoding(kDeleteKey, kDeleteValue, &body); |
| - BuildFormEncoding(kUnregistrationCallerKey, |
| - kUnregistrationCallerValue, |
| - &body); |
| + BuildRequestBody(&body); |
| DVLOG(1) << "Unregistration request: " << body; |
| url_fetcher_->SetUploadData(kRequestContentType, body); |
| @@ -163,6 +94,47 @@ void UnregistrationRequest::Start() { |
| url_fetcher_->Start(); |
| } |
| +void UnregistrationRequest::BuildRequestHeaders(std::string* extra_headers) { |
| + net::HttpRequestHeaders headers; |
| + headers.SetHeader( |
| + net::HttpRequestHeaders::kAuthorization, |
| + std::string(kLoginHeader) + " " + |
| + base::Uint64ToString(request_info_.android_id) + ":" + |
| + base::Uint64ToString(request_info_.security_token)); |
| + headers.SetHeader(kAppIdKey, request_info_.app_id); |
| + *extra_headers = headers.ToString(); |
| +} |
| + |
| +void UnregistrationRequest::BuildRequestBody(std::string* body) { |
| + BuildFormEncoding(kAppIdKey, request_info_.app_id, body); |
| + BuildFormEncoding(kDeviceIdKey, |
| + base::Uint64ToString(request_info_.android_id), |
| + body); |
| + BuildFormEncoding(kDeleteKey, kDeleteValue, body); |
| + custom_request_handler_->BuildRequestBody(body); |
|
Nicolas Zea
2015/05/26 16:39:15
Check if the pointer is valid first? (here and bel
jianli
2015/05/26 20:49:39
Done.
|
| +} |
| + |
| +UnregistrationRequest::Status UnregistrationRequest::ParseResponse( |
| + const net::URLFetcher* source) { |
| + if (!source->GetStatus().is_success()) { |
| + DVLOG(1) << "Fetcher failed"; |
| + return URL_FETCHING_FAILED; |
| + } |
| + |
| + net::HttpStatusCode response_status = static_cast<net::HttpStatusCode>( |
| + source->GetResponseCode()); |
| + if (response_status != net::HTTP_OK) { |
| + DVLOG(1) << "HTTP Status code is not OK, but: " << response_status; |
| + if (response_status == net::HTTP_SERVICE_UNAVAILABLE) |
| + return SERVICE_UNAVAILABLE; |
| + if (response_status == net::HTTP_INTERNAL_SERVER_ERROR) |
| + return INTERNAL_SERVER_ERROR; |
| + return HTTP_NOT_OK; |
| + } |
| + |
| + return custom_request_handler_->ParseResponse(source); |
| +} |
| + |
| void UnregistrationRequest::RetryWithBackoff(bool update_backoff) { |
| if (update_backoff) { |
| url_fetcher_.reset(); |
| @@ -190,8 +162,7 @@ void UnregistrationRequest::RetryWithBackoff(bool update_backoff) { |
| } |
| void UnregistrationRequest::OnURLFetchComplete(const net::URLFetcher* source) { |
| - UnregistrationRequest::Status status = |
| - ParseFetcherResponse(source, request_info_.app_id); |
| + UnregistrationRequest::Status status= ParseResponse(source); |
|
Nicolas Zea
2015/05/26 16:39:15
nit: space between "status" and "="
jianli
2015/05/26 20:49:39
Done.
|
| DVLOG(1) << "UnregistrationRequestStauts: " << status; |
| UMA_HISTOGRAM_ENUMERATION("GCM.UnregistrationRequestStatus", |