| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/autofill/core/browser/autofill_download.h" | 5 #include "components/autofill/core/browser/autofill_download.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
| 9 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "components/autofill/core/browser/autofill_download_url.h" | |
| 13 #include "components/autofill/core/browser/autofill_driver.h" | 12 #include "components/autofill/core/browser/autofill_driver.h" |
| 14 #include "components/autofill/core/browser/autofill_metrics.h" | 13 #include "components/autofill/core/browser/autofill_metrics.h" |
| 15 #include "components/autofill/core/browser/autofill_xml_parser.h" | 14 #include "components/autofill/core/browser/autofill_xml_parser.h" |
| 16 #include "components/autofill/core/browser/form_structure.h" | 15 #include "components/autofill/core/browser/form_structure.h" |
| 17 #include "components/autofill/core/common/autofill_pref_names.h" | 16 #include "components/autofill/core/common/autofill_pref_names.h" |
| 18 #include "net/base/load_flags.h" | 17 #include "net/base/load_flags.h" |
| 19 #include "net/http/http_response_headers.h" | 18 #include "net/http/http_response_headers.h" |
| 20 #include "net/url_request/url_fetcher.h" | 19 #include "net/url_request/url_fetcher.h" |
| 21 #include "third_party/libjingle/source/talk/xmllite/xmlparser.h" | 20 #include "third_party/libjingle/source/talk/xmllite/xmlparser.h" |
| 22 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 23 | 22 |
| 24 namespace autofill { | 23 namespace autofill { |
| 25 | 24 |
| 26 namespace { | 25 namespace { |
| 27 | 26 |
| 28 const char kAutofillQueryServerNameStartInHeader[] = "GFE/"; | 27 const char kAutofillQueryServerNameStartInHeader[] = "GFE/"; |
| 29 const size_t kMaxFormCacheSize = 16; | 28 const size_t kMaxFormCacheSize = 16; |
| 30 | 29 |
| 31 std::string AutofillRequestTypeToString( | 30 #if defined(GOOGLE_CHROME_BUILD) |
| 32 AutofillDownloadManager::AutofillRequestType type) { | 31 const char kClientName[] = "Google Chrome"; |
| 32 #else |
| 33 const char kClientName[] = "Chromium"; |
| 34 #endif // defined(GOOGLE_CHROME_BUILD) |
| 35 |
| 36 std::string RequestTypeToString(AutofillDownloadManager::RequestType type) { |
| 33 switch (type) { | 37 switch (type) { |
| 34 case AutofillDownloadManager::REQUEST_QUERY: | 38 case AutofillDownloadManager::REQUEST_QUERY: |
| 35 return "query"; | 39 return "query"; |
| 36 case AutofillDownloadManager::REQUEST_UPLOAD: | 40 case AutofillDownloadManager::REQUEST_UPLOAD: |
| 37 return "upload"; | 41 return "upload"; |
| 38 } | 42 } |
| 39 NOTREACHED(); | 43 NOTREACHED(); |
| 40 return std::string(); | 44 return std::string(); |
| 41 } | 45 } |
| 42 | 46 |
| 47 GURL GetRequestUrl(AutofillDownloadManager::RequestType request_type) { |
| 48 return GURL("https://clients1.google.com/tbproxy/af/" + |
| 49 RequestTypeToString(request_type) + "?client=" + kClientName); |
| 50 } |
| 51 |
| 43 } // namespace | 52 } // namespace |
| 44 | 53 |
| 45 struct AutofillDownloadManager::FormRequestData { | 54 struct AutofillDownloadManager::FormRequestData { |
| 46 std::vector<std::string> form_signatures; | 55 std::vector<std::string> form_signatures; |
| 47 AutofillRequestType request_type; | 56 RequestType request_type; |
| 48 }; | 57 }; |
| 49 | 58 |
| 50 AutofillDownloadManager::AutofillDownloadManager(AutofillDriver* driver, | 59 AutofillDownloadManager::AutofillDownloadManager(AutofillDriver* driver, |
| 51 PrefService* pref_service, | 60 PrefService* pref_service, |
| 52 Observer* observer) | 61 Observer* observer) |
| 53 : driver_(driver), | 62 : driver_(driver), |
| 54 pref_service_(pref_service), | 63 pref_service_(pref_service), |
| 55 observer_(observer), | 64 observer_(observer), |
| 56 max_form_cache_size_(kMaxFormCacheSize), | 65 max_form_cache_size_(kMaxFormCacheSize), |
| 57 next_query_request_(base::Time::Now()), | 66 next_query_request_(base::Time::Now()), |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 DCHECK_LE(rate, 1.0); | 167 DCHECK_LE(rate, 1.0); |
| 159 pref_service_->SetDouble(prefs::kAutofillNegativeUploadRate, rate); | 168 pref_service_->SetDouble(prefs::kAutofillNegativeUploadRate, rate); |
| 160 } | 169 } |
| 161 | 170 |
| 162 bool AutofillDownloadManager::StartRequest( | 171 bool AutofillDownloadManager::StartRequest( |
| 163 const std::string& form_xml, | 172 const std::string& form_xml, |
| 164 const FormRequestData& request_data) { | 173 const FormRequestData& request_data) { |
| 165 net::URLRequestContextGetter* request_context = | 174 net::URLRequestContextGetter* request_context = |
| 166 driver_->GetURLRequestContext(); | 175 driver_->GetURLRequestContext(); |
| 167 DCHECK(request_context); | 176 DCHECK(request_context); |
| 168 GURL request_url; | 177 GURL request_url = GetRequestUrl(request_data.request_type); |
| 169 if (request_data.request_type == AutofillDownloadManager::REQUEST_QUERY) | |
| 170 request_url = autofill::GetAutofillQueryUrl(); | |
| 171 else | |
| 172 request_url = autofill::GetAutofillUploadUrl(); | |
| 173 | 178 |
| 174 // Id is ignored for regular chrome, in unit test id's for fake fetcher | 179 // Id is ignored for regular chrome, in unit test id's for fake fetcher |
| 175 // factory will be 0, 1, 2, ... | 180 // factory will be 0, 1, 2, ... |
| 176 net::URLFetcher* fetcher = net::URLFetcher::Create( | 181 net::URLFetcher* fetcher = net::URLFetcher::Create( |
| 177 fetcher_id_for_unittest_++, request_url, net::URLFetcher::POST, | 182 fetcher_id_for_unittest_++, request_url, net::URLFetcher::POST, |
| 178 this); | 183 this); |
| 179 url_fetchers_[fetcher] = request_data; | 184 url_fetchers_[fetcher] = request_data; |
| 180 fetcher->SetAutomaticallyRetryOn5xx(false); | 185 fetcher->SetAutomaticallyRetryOn5xx(false); |
| 181 fetcher->SetRequestContext(request_context); | 186 fetcher->SetRequestContext(request_context); |
| 182 fetcher->SetUploadData("text/plain", form_xml); | 187 fetcher->SetUploadData("text/plain", form_xml); |
| 183 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES | | 188 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES | |
| 184 net::LOAD_DO_NOT_SEND_COOKIES); | 189 net::LOAD_DO_NOT_SEND_COOKIES); |
| 185 fetcher->Start(); | 190 fetcher->Start(); |
| 186 | 191 |
| 187 DVLOG(1) << "Sending AutofillDownloadManager " | 192 DVLOG(1) << "Sending AutofillDownloadManager " |
| 188 << AutofillRequestTypeToString(request_data.request_type) | 193 << RequestTypeToString(request_data.request_type) |
| 189 << " request: " << form_xml; | 194 << " request: " << form_xml; |
| 190 | 195 |
| 191 return true; | 196 return true; |
| 192 } | 197 } |
| 193 | 198 |
| 194 void AutofillDownloadManager::CacheQueryRequest( | 199 void AutofillDownloadManager::CacheQueryRequest( |
| 195 const std::vector<std::string>& forms_in_query, | 200 const std::vector<std::string>& forms_in_query, |
| 196 const std::string& query_data) { | 201 const std::string& query_data) { |
| 197 std::string signature = GetCombinedSignature(forms_in_query); | 202 std::string signature = GetCombinedSignature(forms_in_query); |
| 198 for (QueryRequestCache::iterator it = cached_forms_.begin(); | 203 for (QueryRequestCache::iterator it = cached_forms_.begin(); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 | 252 |
| 248 void AutofillDownloadManager::OnURLFetchComplete( | 253 void AutofillDownloadManager::OnURLFetchComplete( |
| 249 const net::URLFetcher* source) { | 254 const net::URLFetcher* source) { |
| 250 std::map<net::URLFetcher *, FormRequestData>::iterator it = | 255 std::map<net::URLFetcher *, FormRequestData>::iterator it = |
| 251 url_fetchers_.find(const_cast<net::URLFetcher*>(source)); | 256 url_fetchers_.find(const_cast<net::URLFetcher*>(source)); |
| 252 if (it == url_fetchers_.end()) { | 257 if (it == url_fetchers_.end()) { |
| 253 // Looks like crash on Mac is possibly caused with callback entering here | 258 // Looks like crash on Mac is possibly caused with callback entering here |
| 254 // with unknown fetcher when network is refreshed. | 259 // with unknown fetcher when network is refreshed. |
| 255 return; | 260 return; |
| 256 } | 261 } |
| 257 std::string type_of_request( | 262 std::string request_type(RequestTypeToString(it->second.request_type)); |
| 258 AutofillRequestTypeToString(it->second.request_type)); | |
| 259 const int kHttpResponseOk = 200; | 263 const int kHttpResponseOk = 200; |
| 260 const int kHttpInternalServerError = 500; | 264 const int kHttpInternalServerError = 500; |
| 261 const int kHttpBadGateway = 502; | 265 const int kHttpBadGateway = 502; |
| 262 const int kHttpServiceUnavailable = 503; | 266 const int kHttpServiceUnavailable = 503; |
| 263 | 267 |
| 264 CHECK(it->second.form_signatures.size()); | 268 CHECK(it->second.form_signatures.size()); |
| 265 if (source->GetResponseCode() != kHttpResponseOk) { | 269 if (source->GetResponseCode() != kHttpResponseOk) { |
| 266 bool back_off = false; | 270 bool back_off = false; |
| 267 std::string server_header; | 271 std::string server_header; |
| 268 switch (source->GetResponseCode()) { | 272 switch (source->GetResponseCode()) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 283 | 287 |
| 284 if (back_off) { | 288 if (back_off) { |
| 285 base::Time back_off_time(base::Time::Now() + source->GetBackoffDelay()); | 289 base::Time back_off_time(base::Time::Now() + source->GetBackoffDelay()); |
| 286 if (it->second.request_type == AutofillDownloadManager::REQUEST_QUERY) { | 290 if (it->second.request_type == AutofillDownloadManager::REQUEST_QUERY) { |
| 287 next_query_request_ = back_off_time; | 291 next_query_request_ = back_off_time; |
| 288 } else { | 292 } else { |
| 289 next_upload_request_ = back_off_time; | 293 next_upload_request_ = back_off_time; |
| 290 } | 294 } |
| 291 } | 295 } |
| 292 | 296 |
| 293 DVLOG(1) << "AutofillDownloadManager: " << type_of_request | 297 DVLOG(1) << "AutofillDownloadManager: " << request_type |
| 294 << " request has failed with response " | 298 << " request has failed with response " |
| 295 << source->GetResponseCode(); | 299 << source->GetResponseCode(); |
| 296 observer_->OnServerRequestError(it->second.form_signatures[0], | 300 observer_->OnServerRequestError(it->second.form_signatures[0], |
| 297 it->second.request_type, | 301 it->second.request_type, |
| 298 source->GetResponseCode()); | 302 source->GetResponseCode()); |
| 299 } else { | 303 } else { |
| 300 std::string response_body; | 304 std::string response_body; |
| 301 source->GetResponseAsString(&response_body); | 305 source->GetResponseAsString(&response_body); |
| 302 DVLOG(1) << "AutofillDownloadManager: " << type_of_request | 306 DVLOG(1) << "AutofillDownloadManager: " << request_type |
| 303 << " request has succeeded with response body: " | 307 << " request has succeeded with response body: " << response_body; |
| 304 << response_body; | |
| 305 if (it->second.request_type == AutofillDownloadManager::REQUEST_QUERY) { | 308 if (it->second.request_type == AutofillDownloadManager::REQUEST_QUERY) { |
| 306 CacheQueryRequest(it->second.form_signatures, response_body); | 309 CacheQueryRequest(it->second.form_signatures, response_body); |
| 307 observer_->OnLoadedServerPredictions(response_body); | 310 observer_->OnLoadedServerPredictions(response_body); |
| 308 } else { | 311 } else { |
| 309 double new_positive_upload_rate = 0; | 312 double new_positive_upload_rate = 0; |
| 310 double new_negative_upload_rate = 0; | 313 double new_negative_upload_rate = 0; |
| 311 AutofillUploadXmlParser parse_handler(&new_positive_upload_rate, | 314 AutofillUploadXmlParser parse_handler(&new_positive_upload_rate, |
| 312 &new_negative_upload_rate); | 315 &new_negative_upload_rate); |
| 313 buzz::XmlParser parser(&parse_handler); | 316 buzz::XmlParser parser(&parse_handler); |
| 314 parser.Parse(response_body.data(), response_body.length(), true); | 317 parser.Parse(response_body.data(), response_body.length(), true); |
| 315 if (parse_handler.succeeded()) { | 318 if (parse_handler.succeeded()) { |
| 316 SetPositiveUploadRate(new_positive_upload_rate); | 319 SetPositiveUploadRate(new_positive_upload_rate); |
| 317 SetNegativeUploadRate(new_negative_upload_rate); | 320 SetNegativeUploadRate(new_negative_upload_rate); |
| 318 } | 321 } |
| 319 | 322 |
| 320 observer_->OnUploadedPossibleFieldTypes(); | 323 observer_->OnUploadedPossibleFieldTypes(); |
| 321 } | 324 } |
| 322 } | 325 } |
| 323 delete it->first; | 326 delete it->first; |
| 324 url_fetchers_.erase(it); | 327 url_fetchers_.erase(it); |
| 325 } | 328 } |
| 326 | 329 |
| 327 } // namespace autofill | 330 } // namespace autofill |
| OLD | NEW |