OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/autofill/autofill_download.h" | 5 #include "chrome/browser/autofill/autofill_download.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <ostream> | 8 #include <ostream> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 FormRequestData request_data; | 130 FormRequestData request_data; |
131 request_data.form_signatures.push_back(form.FormSignature()); | 131 request_data.form_signatures.push_back(form.FormSignature()); |
132 request_data.request_type = AutofillDownloadManager::REQUEST_UPLOAD; | 132 request_data.request_type = AutofillDownloadManager::REQUEST_UPLOAD; |
133 | 133 |
134 return StartRequest(form_xml, request_data); | 134 return StartRequest(form_xml, request_data); |
135 } | 135 } |
136 | 136 |
137 bool AutofillDownloadManager::CancelRequest( | 137 bool AutofillDownloadManager::CancelRequest( |
138 const std::string& form_signature, | 138 const std::string& form_signature, |
139 AutofillDownloadManager::AutofillRequestType request_type) { | 139 AutofillDownloadManager::AutofillRequestType request_type) { |
140 for (std::map<URLFetcher *, FormRequestData>::iterator it = | 140 for (std::map<content::URLFetcher*, FormRequestData>::iterator it = |
141 url_fetchers_.begin(); | 141 url_fetchers_.begin(); |
142 it != url_fetchers_.end(); | 142 it != url_fetchers_.end(); |
143 ++it) { | 143 ++it) { |
144 if (std::find(it->second.form_signatures.begin(), | 144 if (std::find(it->second.form_signatures.begin(), |
145 it->second.form_signatures.end(), form_signature) != | 145 it->second.form_signatures.end(), form_signature) != |
146 it->second.form_signatures.end() && | 146 it->second.form_signatures.end() && |
147 it->second.request_type == request_type) { | 147 it->second.request_type == request_type) { |
148 delete it->first; | 148 delete it->first; |
149 url_fetchers_.erase(it); | 149 url_fetchers_.erase(it); |
150 return true; | 150 return true; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 else | 194 else |
195 request_url = kAutofillUploadServerRequestUrl; | 195 request_url = kAutofillUploadServerRequestUrl; |
196 | 196 |
197 // Id is ignored for regular chrome, in unit test id's for fake fetcher | 197 // Id is ignored for regular chrome, in unit test id's for fake fetcher |
198 // factory will be 0, 1, 2, ... | 198 // factory will be 0, 1, 2, ... |
199 URLFetcher *fetcher = URLFetcher::Create(fetcher_id_for_unittest_++, | 199 URLFetcher *fetcher = URLFetcher::Create(fetcher_id_for_unittest_++, |
200 GURL(request_url), | 200 GURL(request_url), |
201 URLFetcher::POST, | 201 URLFetcher::POST, |
202 this); | 202 this); |
203 url_fetchers_[fetcher] = request_data; | 203 url_fetchers_[fetcher] = request_data; |
204 fetcher->set_automatically_retry_on_5xx(false); | 204 fetcher->SetAutomaticallyRetryOn5xx(false); |
205 fetcher->set_request_context(request_context); | 205 fetcher->SetRequestContext(request_context); |
206 fetcher->set_upload_data("text/plain", form_xml); | 206 fetcher->SetUploadData("text/plain", form_xml); |
207 fetcher->Start(); | 207 fetcher->Start(); |
208 return true; | 208 return true; |
209 } | 209 } |
210 | 210 |
211 void AutofillDownloadManager::CacheQueryRequest( | 211 void AutofillDownloadManager::CacheQueryRequest( |
212 const std::vector<std::string>& forms_in_query, | 212 const std::vector<std::string>& forms_in_query, |
213 const std::string& query_data) { | 213 const std::string& query_data) { |
214 std::string signature = GetCombinedSignature(forms_in_query); | 214 std::string signature = GetCombinedSignature(forms_in_query); |
215 for (QueryRequestCache::iterator it = cached_forms_.begin(); | 215 for (QueryRequestCache::iterator it = cached_forms_.begin(); |
216 it != cached_forms_.end(); ++it) { | 216 it != cached_forms_.end(); ++it) { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 signature.reserve(total_size); | 255 signature.reserve(total_size); |
256 | 256 |
257 for (size_t i = 0; i < forms_in_query.size(); ++i) { | 257 for (size_t i = 0; i < forms_in_query.size(); ++i) { |
258 if (i) | 258 if (i) |
259 signature.append(","); | 259 signature.append(","); |
260 signature.append(forms_in_query[i]); | 260 signature.append(forms_in_query[i]); |
261 } | 261 } |
262 return signature; | 262 return signature; |
263 } | 263 } |
264 | 264 |
265 void AutofillDownloadManager::OnURLFetchComplete(const URLFetcher* source) { | 265 void AutofillDownloadManager::OnURLFetchComplete( |
266 std::map<URLFetcher *, FormRequestData>::iterator it = | 266 const content::URLFetcher* source) { |
267 url_fetchers_.find(const_cast<URLFetcher*>(source)); | 267 std::map<content::URLFetcher *, FormRequestData>::iterator it = |
| 268 url_fetchers_.find(const_cast<content::URLFetcher*>(source)); |
268 if (it == url_fetchers_.end()) { | 269 if (it == url_fetchers_.end()) { |
269 // Looks like crash on Mac is possibly caused with callback entering here | 270 // Looks like crash on Mac is possibly caused with callback entering here |
270 // with unknown fetcher when network is refreshed. | 271 // with unknown fetcher when network is refreshed. |
271 return; | 272 return; |
272 } | 273 } |
273 std::string type_of_request( | 274 std::string type_of_request( |
274 it->second.request_type == AutofillDownloadManager::REQUEST_QUERY ? | 275 it->second.request_type == AutofillDownloadManager::REQUEST_QUERY ? |
275 "query" : "upload"); | 276 "query" : "upload"); |
276 const int kHttpResponseOk = 200; | 277 const int kHttpResponseOk = 200; |
277 const int kHttpInternalServerError = 500; | 278 const int kHttpInternalServerError = 500; |
278 const int kHttpBadGateway = 502; | 279 const int kHttpBadGateway = 502; |
279 const int kHttpServiceUnavailable = 503; | 280 const int kHttpServiceUnavailable = 503; |
280 | 281 |
281 CHECK(it->second.form_signatures.size()); | 282 CHECK(it->second.form_signatures.size()); |
282 if (source->response_code() != kHttpResponseOk) { | 283 if (source->GetResponseCode() != kHttpResponseOk) { |
283 bool back_off = false; | 284 bool back_off = false; |
284 std::string server_header; | 285 std::string server_header; |
285 switch (source->response_code()) { | 286 switch (source->GetResponseCode()) { |
286 case kHttpBadGateway: | 287 case kHttpBadGateway: |
287 if (!source->response_headers()->EnumerateHeader(NULL, "server", | 288 if (!source->GetResponseHeaders()->EnumerateHeader(NULL, "server", |
288 &server_header) || | 289 &server_header) || |
289 StartsWithASCII(server_header.c_str(), | 290 StartsWithASCII(server_header.c_str(), |
290 kAutofillQueryServerNameStartInHeader, | 291 kAutofillQueryServerNameStartInHeader, |
291 false) != 0) | 292 false) != 0) |
292 break; | 293 break; |
293 // Bad gateway was received from Autofill servers. Fall through to back | 294 // Bad gateway was received from Autofill servers. Fall through to back |
294 // off. | 295 // off. |
295 case kHttpInternalServerError: | 296 case kHttpInternalServerError: |
296 case kHttpServiceUnavailable: | 297 case kHttpServiceUnavailable: |
297 back_off = true; | 298 back_off = true; |
298 break; | 299 break; |
299 } | 300 } |
300 | 301 |
301 if (back_off) { | 302 if (back_off) { |
302 base::Time back_off_time(base::Time::Now() + source->backoff_delay()); | 303 base::Time back_off_time(base::Time::Now() + source->GetBackoffDelay()); |
303 if (it->second.request_type == AutofillDownloadManager::REQUEST_QUERY) { | 304 if (it->second.request_type == AutofillDownloadManager::REQUEST_QUERY) { |
304 next_query_request_ = back_off_time; | 305 next_query_request_ = back_off_time; |
305 } else { | 306 } else { |
306 next_upload_request_ = back_off_time; | 307 next_upload_request_ = back_off_time; |
307 } | 308 } |
308 } | 309 } |
309 | 310 |
310 LOG(WARNING) << "AutofillDownloadManager: " << type_of_request | 311 LOG(WARNING) << "AutofillDownloadManager: " << type_of_request |
311 << " request has failed with response " | 312 << " request has failed with response " |
312 << source->response_code(); | 313 << source->GetResponseCode(); |
313 if (observer_) { | 314 if (observer_) { |
314 observer_->OnServerRequestError(it->second.form_signatures[0], | 315 observer_->OnServerRequestError(it->second.form_signatures[0], |
315 it->second.request_type, | 316 it->second.request_type, |
316 source->response_code()); | 317 source->GetResponseCode()); |
317 } | 318 } |
318 } else { | 319 } else { |
319 VLOG(1) << "AutofillDownloadManager: " << type_of_request | 320 VLOG(1) << "AutofillDownloadManager: " << type_of_request |
320 << " request has succeeded"; | 321 << " request has succeeded"; |
321 std::string response_body; | 322 std::string response_body; |
322 source->GetResponseAsString(&response_body); | 323 source->GetResponseAsString(&response_body); |
323 if (it->second.request_type == AutofillDownloadManager::REQUEST_QUERY) { | 324 if (it->second.request_type == AutofillDownloadManager::REQUEST_QUERY) { |
324 CacheQueryRequest(it->second.form_signatures, response_body); | 325 CacheQueryRequest(it->second.form_signatures, response_body); |
325 if (observer_) | 326 if (observer_) |
326 observer_->OnLoadedServerPredictions(response_body); | 327 observer_->OnLoadedServerPredictions(response_body); |
327 } else { | 328 } else { |
328 double new_positive_upload_rate = 0; | 329 double new_positive_upload_rate = 0; |
329 double new_negative_upload_rate = 0; | 330 double new_negative_upload_rate = 0; |
330 AutofillUploadXmlParser parse_handler(&new_positive_upload_rate, | 331 AutofillUploadXmlParser parse_handler(&new_positive_upload_rate, |
331 &new_negative_upload_rate); | 332 &new_negative_upload_rate); |
332 buzz::XmlParser parser(&parse_handler); | 333 buzz::XmlParser parser(&parse_handler); |
333 parser.Parse(response_body.data(), response_body.length(), true); | 334 parser.Parse(response_body.data(), response_body.length(), true); |
334 if (parse_handler.succeeded()) { | 335 if (parse_handler.succeeded()) { |
335 SetPositiveUploadRate(new_positive_upload_rate); | 336 SetPositiveUploadRate(new_positive_upload_rate); |
336 SetNegativeUploadRate(new_negative_upload_rate); | 337 SetNegativeUploadRate(new_negative_upload_rate); |
337 } | 338 } |
338 | 339 |
339 if (observer_) | 340 if (observer_) |
340 observer_->OnUploadedPossibleFieldTypes(); | 341 observer_->OnUploadedPossibleFieldTypes(); |
341 } | 342 } |
342 } | 343 } |
343 delete it->first; | 344 delete it->first; |
344 url_fetchers_.erase(it); | 345 url_fetchers_.erase(it); |
345 } | 346 } |
OLD | NEW |