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