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..74ec60dbd455ce2485618aed98068c1b38a56fe1 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 |
@@ -129,30 +65,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 +87,51 @@ 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){ |
+ DCHECK(request_info_.android_id != 0UL && |
+ request_info_.security_token != 0UL); |
+ |
+ BuildFormEncoding(kAppIdKey, request_info_.app_id, body); |
+ BuildFormEncoding(kDeviceIdKey, |
+ base::Uint64ToString(request_info_.android_id), |
+ body); |
+ BuildFormEncoding(kDeleteKey, kDeleteValue, body); |
+} |
+ |
+bool UnregistrationRequest::ParseResponse(const net::URLFetcher* source, |
+ Status* status) { |
+ if (!source->GetStatus().is_success()) { |
+ DVLOG(1) << "Fetcher failed"; |
+ *status = UnregistrationRequest::URL_FETCHING_FAILED; |
+ return true; |
+ } |
+ |
+ 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) |
+ *status = UnregistrationRequest::SERVICE_UNAVAILABLE; |
+ else if (response_status == net::HTTP_INTERNAL_SERVER_ERROR) |
+ *status = UnregistrationRequest::INTERNAL_SERVER_ERROR; |
+ else |
+ *status = UnregistrationRequest::HTTP_NOT_OK; |
+ return true; |
+ } |
+ return false; |
+} |
+ |
void UnregistrationRequest::RetryWithBackoff(bool update_backoff) { |
if (update_backoff) { |
url_fetcher_.reset(); |
@@ -190,8 +159,8 @@ 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, &status); |
DVLOG(1) << "UnregistrationRequestStauts: " << status; |
UMA_HISTOGRAM_ENUMERATION("GCM.UnregistrationRequestStatus", |
@@ -221,4 +190,8 @@ void UnregistrationRequest::OnURLFetchComplete(const net::URLFetcher* source) { |
callback_.Run(status); |
} |
+std::string UnregistrationRequest::app_id() const { |
+ return request_info_.app_id; |
+} |
+ |
} // namespace gcm |