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) { |
(...skipping 14 matching lines...) Expand all Loading... |
122 backoff_entry_(&backoff_policy), | 58 backoff_entry_(&backoff_policy), |
123 request_context_getter_(request_context_getter), | 59 request_context_getter_(request_context_getter), |
124 recorder_(recorder), | 60 recorder_(recorder), |
125 weak_ptr_factory_(this) { | 61 weak_ptr_factory_(this) { |
126 } | 62 } |
127 | 63 |
128 UnregistrationRequest::~UnregistrationRequest() {} | 64 UnregistrationRequest::~UnregistrationRequest() {} |
129 | 65 |
130 void UnregistrationRequest::Start() { | 66 void UnregistrationRequest::Start() { |
131 DCHECK(!callback_.is_null()); | 67 DCHECK(!callback_.is_null()); |
132 DCHECK(request_info_.android_id != 0UL); | |
133 DCHECK(request_info_.security_token != 0UL); | |
134 DCHECK(!url_fetcher_.get()); | 68 DCHECK(!url_fetcher_.get()); |
135 | 69 |
136 url_fetcher_ = | 70 url_fetcher_ = |
137 net::URLFetcher::Create(registration_url_, net::URLFetcher::POST, this); | 71 net::URLFetcher::Create(registration_url_, net::URLFetcher::POST, this); |
138 url_fetcher_->SetRequestContext(request_context_getter_.get()); | 72 url_fetcher_->SetRequestContext(request_context_getter_.get()); |
139 | 73 |
140 std::string android_id = base::Uint64ToString(request_info_.android_id); | 74 std::string extra_headers; |
141 std::string auth_header = | 75 BuildRequestHeaders(&extra_headers); |
142 std::string(kLoginHeader) + " " + android_id + ":" + | 76 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 | 77 |
149 std::string body; | 78 std::string body; |
150 BuildFormEncoding(kAppIdKey, request_info_.app_id, &body); | 79 BuildRequestBody(&body); |
151 BuildFormEncoding(kDeviceIdKey, android_id, &body); | |
152 BuildFormEncoding(kDeleteKey, kDeleteValue, &body); | |
153 BuildFormEncoding(kUnregistrationCallerKey, | |
154 kUnregistrationCallerValue, | |
155 &body); | |
156 | 80 |
157 DVLOG(1) << "Unregistration request: " << body; | 81 DVLOG(1) << "Unregistration request: " << body; |
158 url_fetcher_->SetUploadData(kRequestContentType, body); | 82 url_fetcher_->SetUploadData(kRequestContentType, body); |
159 | 83 |
160 DVLOG(1) << "Performing unregistration for: " << request_info_.app_id; | 84 DVLOG(1) << "Performing unregistration for: " << request_info_.app_id; |
161 recorder_->RecordUnregistrationSent(request_info_.app_id); | 85 recorder_->RecordUnregistrationSent(request_info_.app_id); |
162 request_start_time_ = base::TimeTicks::Now(); | 86 request_start_time_ = base::TimeTicks::Now(); |
163 url_fetcher_->Start(); | 87 url_fetcher_->Start(); |
164 } | 88 } |
165 | 89 |
| 90 void UnregistrationRequest::BuildRequestHeaders(std::string* extra_headers) { |
| 91 net::HttpRequestHeaders headers; |
| 92 headers.SetHeader( |
| 93 net::HttpRequestHeaders::kAuthorization, |
| 94 std::string(kLoginHeader) + " " + |
| 95 base::Uint64ToString(request_info_.android_id) + ":" + |
| 96 base::Uint64ToString(request_info_.security_token)); |
| 97 headers.SetHeader(kAppIdKey, request_info_.app_id); |
| 98 *extra_headers = headers.ToString(); |
| 99 } |
| 100 |
| 101 void UnregistrationRequest::BuildRequestBody(std::string* body){ |
| 102 DCHECK(request_info_.android_id != 0UL && |
| 103 request_info_.security_token != 0UL); |
| 104 |
| 105 BuildFormEncoding(kAppIdKey, request_info_.app_id, body); |
| 106 BuildFormEncoding(kDeviceIdKey, |
| 107 base::Uint64ToString(request_info_.android_id), |
| 108 body); |
| 109 BuildFormEncoding(kDeleteKey, kDeleteValue, body); |
| 110 } |
| 111 |
| 112 bool UnregistrationRequest::ParseResponse(const net::URLFetcher* source, |
| 113 Status* status) { |
| 114 if (!source->GetStatus().is_success()) { |
| 115 DVLOG(1) << "Fetcher failed"; |
| 116 *status = UnregistrationRequest::URL_FETCHING_FAILED; |
| 117 return true; |
| 118 } |
| 119 |
| 120 net::HttpStatusCode response_status = static_cast<net::HttpStatusCode>( |
| 121 source->GetResponseCode()); |
| 122 if (response_status != net::HTTP_OK) { |
| 123 DVLOG(1) << "HTTP Status code is not OK, but: " << response_status; |
| 124 if (response_status == net::HTTP_SERVICE_UNAVAILABLE) |
| 125 *status = UnregistrationRequest::SERVICE_UNAVAILABLE; |
| 126 else if (response_status == net::HTTP_INTERNAL_SERVER_ERROR) |
| 127 *status = UnregistrationRequest::INTERNAL_SERVER_ERROR; |
| 128 else |
| 129 *status = UnregistrationRequest::HTTP_NOT_OK; |
| 130 return true; |
| 131 } |
| 132 return false; |
| 133 } |
| 134 |
166 void UnregistrationRequest::RetryWithBackoff(bool update_backoff) { | 135 void UnregistrationRequest::RetryWithBackoff(bool update_backoff) { |
167 if (update_backoff) { | 136 if (update_backoff) { |
168 url_fetcher_.reset(); | 137 url_fetcher_.reset(); |
169 backoff_entry_.InformOfRequest(false); | 138 backoff_entry_.InformOfRequest(false); |
170 } | 139 } |
171 | 140 |
172 if (backoff_entry_.ShouldRejectRequest()) { | 141 if (backoff_entry_.ShouldRejectRequest()) { |
173 DVLOG(1) << "Delaying GCM unregistration of app: " | 142 DVLOG(1) << "Delaying GCM unregistration of app: " |
174 << request_info_.app_id << ", for " | 143 << request_info_.app_id << ", for " |
175 << backoff_entry_.GetTimeUntilRelease().InMilliseconds() | 144 << backoff_entry_.GetTimeUntilRelease().InMilliseconds() |
176 << " milliseconds."; | 145 << " milliseconds."; |
177 recorder_->RecordUnregistrationRetryDelayed( | 146 recorder_->RecordUnregistrationRetryDelayed( |
178 request_info_.app_id, | 147 request_info_.app_id, |
179 backoff_entry_.GetTimeUntilRelease().InMilliseconds()); | 148 backoff_entry_.GetTimeUntilRelease().InMilliseconds()); |
180 base::MessageLoop::current()->PostDelayedTask( | 149 base::MessageLoop::current()->PostDelayedTask( |
181 FROM_HERE, | 150 FROM_HERE, |
182 base::Bind(&UnregistrationRequest::RetryWithBackoff, | 151 base::Bind(&UnregistrationRequest::RetryWithBackoff, |
183 weak_ptr_factory_.GetWeakPtr(), | 152 weak_ptr_factory_.GetWeakPtr(), |
184 false), | 153 false), |
185 backoff_entry_.GetTimeUntilRelease()); | 154 backoff_entry_.GetTimeUntilRelease()); |
186 return; | 155 return; |
187 } | 156 } |
188 | 157 |
189 Start(); | 158 Start(); |
190 } | 159 } |
191 | 160 |
192 void UnregistrationRequest::OnURLFetchComplete(const net::URLFetcher* source) { | 161 void UnregistrationRequest::OnURLFetchComplete(const net::URLFetcher* source) { |
193 UnregistrationRequest::Status status = | 162 UnregistrationRequest::Status status; |
194 ParseFetcherResponse(source, request_info_.app_id); | 163 ParseResponse(source, &status); |
195 | 164 |
196 DVLOG(1) << "UnregistrationRequestStauts: " << status; | 165 DVLOG(1) << "UnregistrationRequestStauts: " << status; |
197 UMA_HISTOGRAM_ENUMERATION("GCM.UnregistrationRequestStatus", | 166 UMA_HISTOGRAM_ENUMERATION("GCM.UnregistrationRequestStatus", |
198 status, | 167 status, |
199 UNREGISTRATION_STATUS_COUNT); | 168 UNREGISTRATION_STATUS_COUNT); |
200 recorder_->RecordUnregistrationResponse(request_info_.app_id, status); | 169 recorder_->RecordUnregistrationResponse(request_info_.app_id, status); |
201 | 170 |
202 if (status == URL_FETCHING_FAILED || | 171 if (status == URL_FETCHING_FAILED || |
203 status == SERVICE_UNAVAILABLE || | 172 status == SERVICE_UNAVAILABLE || |
204 status == INTERNAL_SERVER_ERROR || | 173 status == INTERNAL_SERVER_ERROR || |
205 status == INCORRECT_APP_ID || | 174 status == INCORRECT_APP_ID || |
206 status == RESPONSE_PARSING_FAILED) { | 175 status == RESPONSE_PARSING_FAILED) { |
207 RetryWithBackoff(true); | 176 RetryWithBackoff(true); |
208 return; | 177 return; |
209 } | 178 } |
210 | 179 |
211 // status == SUCCESS || HTTP_NOT_OK || NO_RESPONSE_BODY || | 180 // status == SUCCESS || HTTP_NOT_OK || NO_RESPONSE_BODY || |
212 // INVALID_PARAMETERS || UNKNOWN_ERROR | 181 // INVALID_PARAMETERS || UNKNOWN_ERROR |
213 | 182 |
214 if (status == SUCCESS) { | 183 if (status == SUCCESS) { |
215 UMA_HISTOGRAM_COUNTS("GCM.UnregistrationRetryCount", | 184 UMA_HISTOGRAM_COUNTS("GCM.UnregistrationRetryCount", |
216 backoff_entry_.failure_count()); | 185 backoff_entry_.failure_count()); |
217 UMA_HISTOGRAM_TIMES("GCM.UnregistrationCompleteTime", | 186 UMA_HISTOGRAM_TIMES("GCM.UnregistrationCompleteTime", |
218 base::TimeTicks::Now() - request_start_time_); | 187 base::TimeTicks::Now() - request_start_time_); |
219 } | 188 } |
220 | 189 |
221 callback_.Run(status); | 190 callback_.Run(status); |
222 } | 191 } |
223 | 192 |
| 193 std::string UnregistrationRequest::app_id() const { |
| 194 return request_info_.app_id; |
| 195 } |
| 196 |
224 } // namespace gcm | 197 } // namespace gcm |
OLD | NEW |