Chromium Code Reviews| 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 "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" |
| 11 #include "base/strings/string_piece.h" | 11 #include "base/strings/string_piece.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "google_apis/gcm/base/gcm_util.h" | |
| 13 #include "google_apis/gcm/monitoring/gcm_stats_recorder.h" | 14 #include "google_apis/gcm/monitoring/gcm_stats_recorder.h" |
| 14 #include "net/base/escape.h" | 15 #include "net/base/escape.h" |
| 15 #include "net/http/http_request_headers.h" | 16 #include "net/http/http_request_headers.h" |
| 16 #include "net/http/http_status_code.h" | 17 #include "net/http/http_status_code.h" |
| 17 #include "net/url_request/url_fetcher.h" | 18 #include "net/url_request/url_fetcher.h" |
| 18 #include "net/url_request/url_request_context_getter.h" | 19 #include "net/url_request/url_request_context_getter.h" |
| 19 #include "net/url_request/url_request_status.h" | 20 #include "net/url_request/url_request_status.h" |
| 20 | 21 |
| 21 namespace gcm { | 22 namespace gcm { |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 const char kRequestContentType[] = "application/x-www-form-urlencoded"; | 26 const char kRequestContentType[] = "application/x-www-form-urlencoded"; |
| 26 | 27 |
| 27 // Request constants. | 28 // Request constants. |
| 28 const char kAppIdKey[] = "app"; | 29 const char kAppIdKey[] = "app"; |
| 29 const char kDeleteKey[] = "delete"; | 30 const char kDeleteKey[] = "delete"; |
| 30 const char kDeleteValue[] = "true"; | 31 const char kDeleteValue[] = "true"; |
| 31 const char kDeviceIdKey[] = "device"; | 32 const char kDeviceIdKey[] = "device"; |
| 32 const char kLoginHeader[] = "AidLogin"; | 33 const char kLoginHeader[] = "AidLogin"; |
| 33 const char kUnregistrationCallerKey[] = "gcm_unreg_caller"; | |
| 34 // We are going to set the value to "false" in order to forcefully unregister | |
| 35 // the application. | |
| 36 const char kUnregistrationCallerValue[] = "false"; | |
| 37 | |
| 38 // Response constants. | |
| 39 const char kDeletedPrefix[] = "deleted="; | |
| 40 const char kErrorPrefix[] = "Error="; | |
| 41 const char kInvalidParameters[] = "INVALID_PARAMETERS"; | |
| 42 | |
| 43 | |
| 44 void BuildFormEncoding(const std::string& key, | |
| 45 const std::string& value, | |
| 46 std::string* out) { | |
| 47 if (!out->empty()) | |
| 48 out->append("&"); | |
| 49 out->append(key + "=" + net::EscapeUrlEncodedData(value, true)); | |
| 50 } | |
| 51 | |
| 52 UnregistrationRequest::Status ParseFetcherResponse( | |
| 53 const net::URLFetcher* source, | |
| 54 std::string request_app_id) { | |
| 55 if (!source->GetStatus().is_success()) { | |
| 56 DVLOG(1) << "Fetcher failed"; | |
| 57 return UnregistrationRequest::URL_FETCHING_FAILED; | |
| 58 } | |
| 59 | |
| 60 net::HttpStatusCode response_status = static_cast<net::HttpStatusCode>( | |
| 61 source->GetResponseCode()); | |
| 62 if (response_status != net::HTTP_OK) { | |
| 63 DVLOG(1) << "HTTP Status code is not OK, but: " << response_status; | |
| 64 if (response_status == net::HTTP_SERVICE_UNAVAILABLE) | |
| 65 return UnregistrationRequest::SERVICE_UNAVAILABLE; | |
| 66 else if (response_status == net::HTTP_INTERNAL_SERVER_ERROR) | |
| 67 return UnregistrationRequest::INTERNAL_SERVER_ERROR; | |
| 68 return UnregistrationRequest::HTTP_NOT_OK; | |
| 69 } | |
| 70 | |
| 71 std::string response; | |
| 72 if (!source->GetResponseAsString(&response)) { | |
| 73 DVLOG(1) << "Failed to get response body."; | |
| 74 return UnregistrationRequest::NO_RESPONSE_BODY; | |
| 75 } | |
| 76 | |
| 77 DVLOG(1) << "Parsing unregistration response."; | |
| 78 if (response.find(kDeletedPrefix) != std::string::npos) { | |
| 79 std::string app_id = response.substr( | |
| 80 response.find(kDeletedPrefix) + arraysize(kDeletedPrefix) - 1); | |
| 81 if (app_id == request_app_id) | |
| 82 return UnregistrationRequest::SUCCESS; | |
| 83 return UnregistrationRequest::INCORRECT_APP_ID; | |
| 84 } | |
| 85 | |
| 86 if (response.find(kErrorPrefix) != std::string::npos) { | |
| 87 std::string error = response.substr( | |
| 88 response.find(kErrorPrefix) + arraysize(kErrorPrefix) - 1); | |
| 89 if (error == kInvalidParameters) | |
| 90 return UnregistrationRequest::INVALID_PARAMETERS; | |
| 91 return UnregistrationRequest::UNKNOWN_ERROR; | |
| 92 } | |
| 93 | |
| 94 DVLOG(1) << "Not able to parse a meaningful output from response body." | |
| 95 << response; | |
| 96 return UnregistrationRequest::RESPONSE_PARSING_FAILED; | |
| 97 } | |
| 98 | 34 |
| 99 } // namespace | 35 } // namespace |
| 100 | 36 |
| 101 UnregistrationRequest::RequestInfo::RequestInfo( | 37 UnregistrationRequest::RequestInfo::RequestInfo( |
| 102 uint64 android_id, | 38 uint64 android_id, |
| 103 uint64 security_token, | 39 uint64 security_token, |
| 104 const std::string& app_id) | 40 const std::string& app_id) |
| 105 : android_id(android_id), | 41 : android_id(android_id), |
| 106 security_token(security_token), | 42 security_token(security_token), |
| 107 app_id(app_id) { | 43 app_id(app_id) { |
| 44 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.
| |
| 108 } | 45 } |
| 109 | 46 |
| 110 UnregistrationRequest::RequestInfo::~RequestInfo() {} | 47 UnregistrationRequest::RequestInfo::~RequestInfo() {} |
| 111 | 48 |
| 49 UnregistrationRequest::CustomRequestHandler::CustomRequestHandler() {} | |
| 50 | |
| 51 UnregistrationRequest::CustomRequestHandler::~CustomRequestHandler() {} | |
| 52 | |
| 112 UnregistrationRequest::UnregistrationRequest( | 53 UnregistrationRequest::UnregistrationRequest( |
| 113 const GURL& registration_url, | 54 const GURL& registration_url, |
| 114 const RequestInfo& request_info, | 55 const RequestInfo& request_info, |
| 56 scoped_ptr<CustomRequestHandler> custom_request_handler, | |
| 115 const net::BackoffEntry::Policy& backoff_policy, | 57 const net::BackoffEntry::Policy& backoff_policy, |
| 116 const UnregistrationCallback& callback, | 58 const UnregistrationCallback& callback, |
| 117 scoped_refptr<net::URLRequestContextGetter> request_context_getter, | 59 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
| 118 GCMStatsRecorder* recorder) | 60 GCMStatsRecorder* recorder) |
| 119 : callback_(callback), | 61 : callback_(callback), |
| 120 request_info_(request_info), | 62 request_info_(request_info), |
| 63 custom_request_handler_(custom_request_handler.Pass()), | |
| 121 registration_url_(registration_url), | 64 registration_url_(registration_url), |
| 122 backoff_entry_(&backoff_policy), | 65 backoff_entry_(&backoff_policy), |
| 123 request_context_getter_(request_context_getter), | 66 request_context_getter_(request_context_getter), |
| 124 recorder_(recorder), | 67 recorder_(recorder), |
| 125 weak_ptr_factory_(this) { | 68 weak_ptr_factory_(this) { |
| 126 } | 69 } |
| 127 | 70 |
| 128 UnregistrationRequest::~UnregistrationRequest() {} | 71 UnregistrationRequest::~UnregistrationRequest() {} |
| 129 | 72 |
| 130 void UnregistrationRequest::Start() { | 73 void UnregistrationRequest::Start() { |
| 131 DCHECK(!callback_.is_null()); | 74 DCHECK(!callback_.is_null()); |
| 132 DCHECK(request_info_.android_id != 0UL); | |
| 133 DCHECK(request_info_.security_token != 0UL); | |
| 134 DCHECK(!url_fetcher_.get()); | 75 DCHECK(!url_fetcher_.get()); |
| 135 | 76 |
| 136 url_fetcher_ = | 77 url_fetcher_ = |
| 137 net::URLFetcher::Create(registration_url_, net::URLFetcher::POST, this); | 78 net::URLFetcher::Create(registration_url_, net::URLFetcher::POST, this); |
| 138 url_fetcher_->SetRequestContext(request_context_getter_.get()); | 79 url_fetcher_->SetRequestContext(request_context_getter_.get()); |
| 139 | 80 |
| 140 std::string android_id = base::Uint64ToString(request_info_.android_id); | 81 std::string extra_headers; |
| 141 std::string auth_header = | 82 BuildRequestHeaders(&extra_headers); |
| 142 std::string(kLoginHeader) + " " + android_id + ":" + | 83 url_fetcher_->SetExtraRequestHeaders(extra_headers); |
| 143 base::Uint64ToString(request_info_.security_token); | |
| 144 net::HttpRequestHeaders headers; | |
| 145 headers.SetHeader(net::HttpRequestHeaders::kAuthorization, auth_header); | |
| 146 headers.SetHeader(kAppIdKey, request_info_.app_id); | |
| 147 url_fetcher_->SetExtraRequestHeaders(headers.ToString()); | |
| 148 | 84 |
| 149 std::string body; | 85 std::string body; |
| 150 BuildFormEncoding(kAppIdKey, request_info_.app_id, &body); | 86 BuildRequestBody(&body); |
| 151 BuildFormEncoding(kDeviceIdKey, android_id, &body); | |
| 152 BuildFormEncoding(kDeleteKey, kDeleteValue, &body); | |
| 153 BuildFormEncoding(kUnregistrationCallerKey, | |
| 154 kUnregistrationCallerValue, | |
| 155 &body); | |
| 156 | 87 |
| 157 DVLOG(1) << "Unregistration request: " << body; | 88 DVLOG(1) << "Unregistration request: " << body; |
| 158 url_fetcher_->SetUploadData(kRequestContentType, body); | 89 url_fetcher_->SetUploadData(kRequestContentType, body); |
| 159 | 90 |
| 160 DVLOG(1) << "Performing unregistration for: " << request_info_.app_id; | 91 DVLOG(1) << "Performing unregistration for: " << request_info_.app_id; |
| 161 recorder_->RecordUnregistrationSent(request_info_.app_id); | 92 recorder_->RecordUnregistrationSent(request_info_.app_id); |
| 162 request_start_time_ = base::TimeTicks::Now(); | 93 request_start_time_ = base::TimeTicks::Now(); |
| 163 url_fetcher_->Start(); | 94 url_fetcher_->Start(); |
| 164 } | 95 } |
| 165 | 96 |
| 97 void UnregistrationRequest::BuildRequestHeaders(std::string* extra_headers) { | |
| 98 net::HttpRequestHeaders headers; | |
| 99 headers.SetHeader( | |
| 100 net::HttpRequestHeaders::kAuthorization, | |
| 101 std::string(kLoginHeader) + " " + | |
| 102 base::Uint64ToString(request_info_.android_id) + ":" + | |
| 103 base::Uint64ToString(request_info_.security_token)); | |
| 104 headers.SetHeader(kAppIdKey, request_info_.app_id); | |
| 105 *extra_headers = headers.ToString(); | |
| 106 } | |
| 107 | |
| 108 void UnregistrationRequest::BuildRequestBody(std::string* body) { | |
| 109 BuildFormEncoding(kAppIdKey, request_info_.app_id, body); | |
| 110 BuildFormEncoding(kDeviceIdKey, | |
| 111 base::Uint64ToString(request_info_.android_id), | |
| 112 body); | |
| 113 BuildFormEncoding(kDeleteKey, kDeleteValue, body); | |
| 114 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.
| |
| 115 } | |
| 116 | |
| 117 UnregistrationRequest::Status UnregistrationRequest::ParseResponse( | |
| 118 const net::URLFetcher* source) { | |
| 119 if (!source->GetStatus().is_success()) { | |
| 120 DVLOG(1) << "Fetcher failed"; | |
| 121 return URL_FETCHING_FAILED; | |
| 122 } | |
| 123 | |
| 124 net::HttpStatusCode response_status = static_cast<net::HttpStatusCode>( | |
| 125 source->GetResponseCode()); | |
| 126 if (response_status != net::HTTP_OK) { | |
| 127 DVLOG(1) << "HTTP Status code is not OK, but: " << response_status; | |
| 128 if (response_status == net::HTTP_SERVICE_UNAVAILABLE) | |
| 129 return SERVICE_UNAVAILABLE; | |
| 130 if (response_status == net::HTTP_INTERNAL_SERVER_ERROR) | |
| 131 return INTERNAL_SERVER_ERROR; | |
| 132 return HTTP_NOT_OK; | |
| 133 } | |
| 134 | |
| 135 return custom_request_handler_->ParseResponse(source); | |
| 136 } | |
| 137 | |
| 166 void UnregistrationRequest::RetryWithBackoff(bool update_backoff) { | 138 void UnregistrationRequest::RetryWithBackoff(bool update_backoff) { |
| 167 if (update_backoff) { | 139 if (update_backoff) { |
| 168 url_fetcher_.reset(); | 140 url_fetcher_.reset(); |
| 169 backoff_entry_.InformOfRequest(false); | 141 backoff_entry_.InformOfRequest(false); |
| 170 } | 142 } |
| 171 | 143 |
| 172 if (backoff_entry_.ShouldRejectRequest()) { | 144 if (backoff_entry_.ShouldRejectRequest()) { |
| 173 DVLOG(1) << "Delaying GCM unregistration of app: " | 145 DVLOG(1) << "Delaying GCM unregistration of app: " |
| 174 << request_info_.app_id << ", for " | 146 << request_info_.app_id << ", for " |
| 175 << backoff_entry_.GetTimeUntilRelease().InMilliseconds() | 147 << backoff_entry_.GetTimeUntilRelease().InMilliseconds() |
| 176 << " milliseconds."; | 148 << " milliseconds."; |
| 177 recorder_->RecordUnregistrationRetryDelayed( | 149 recorder_->RecordUnregistrationRetryDelayed( |
| 178 request_info_.app_id, | 150 request_info_.app_id, |
| 179 backoff_entry_.GetTimeUntilRelease().InMilliseconds()); | 151 backoff_entry_.GetTimeUntilRelease().InMilliseconds()); |
| 180 base::MessageLoop::current()->PostDelayedTask( | 152 base::MessageLoop::current()->PostDelayedTask( |
| 181 FROM_HERE, | 153 FROM_HERE, |
| 182 base::Bind(&UnregistrationRequest::RetryWithBackoff, | 154 base::Bind(&UnregistrationRequest::RetryWithBackoff, |
| 183 weak_ptr_factory_.GetWeakPtr(), | 155 weak_ptr_factory_.GetWeakPtr(), |
| 184 false), | 156 false), |
| 185 backoff_entry_.GetTimeUntilRelease()); | 157 backoff_entry_.GetTimeUntilRelease()); |
| 186 return; | 158 return; |
| 187 } | 159 } |
| 188 | 160 |
| 189 Start(); | 161 Start(); |
| 190 } | 162 } |
| 191 | 163 |
| 192 void UnregistrationRequest::OnURLFetchComplete(const net::URLFetcher* source) { | 164 void UnregistrationRequest::OnURLFetchComplete(const net::URLFetcher* source) { |
| 193 UnregistrationRequest::Status status = | 165 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.
| |
| 194 ParseFetcherResponse(source, request_info_.app_id); | |
| 195 | 166 |
| 196 DVLOG(1) << "UnregistrationRequestStauts: " << status; | 167 DVLOG(1) << "UnregistrationRequestStauts: " << status; |
| 197 UMA_HISTOGRAM_ENUMERATION("GCM.UnregistrationRequestStatus", | 168 UMA_HISTOGRAM_ENUMERATION("GCM.UnregistrationRequestStatus", |
| 198 status, | 169 status, |
| 199 UNREGISTRATION_STATUS_COUNT); | 170 UNREGISTRATION_STATUS_COUNT); |
| 200 recorder_->RecordUnregistrationResponse(request_info_.app_id, status); | 171 recorder_->RecordUnregistrationResponse(request_info_.app_id, status); |
| 201 | 172 |
| 202 if (status == URL_FETCHING_FAILED || | 173 if (status == URL_FETCHING_FAILED || |
| 203 status == SERVICE_UNAVAILABLE || | 174 status == SERVICE_UNAVAILABLE || |
| 204 status == INTERNAL_SERVER_ERROR || | 175 status == INTERNAL_SERVER_ERROR || |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 215 UMA_HISTOGRAM_COUNTS("GCM.UnregistrationRetryCount", | 186 UMA_HISTOGRAM_COUNTS("GCM.UnregistrationRetryCount", |
| 216 backoff_entry_.failure_count()); | 187 backoff_entry_.failure_count()); |
| 217 UMA_HISTOGRAM_TIMES("GCM.UnregistrationCompleteTime", | 188 UMA_HISTOGRAM_TIMES("GCM.UnregistrationCompleteTime", |
| 218 base::TimeTicks::Now() - request_start_time_); | 189 base::TimeTicks::Now() - request_start_time_); |
| 219 } | 190 } |
| 220 | 191 |
| 221 callback_.Run(status); | 192 callback_.Run(status); |
| 222 } | 193 } |
| 223 | 194 |
| 224 } // namespace gcm | 195 } // namespace gcm |
| OLD | NEW |