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
3 // found in the LICENSE file.
5 #include "components/autofill/core/browser/autofill_download_manager.h"
7 #include "base/logging.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/rand_util.h"
10 #include "base/stl_util.h"
11 #include "base/strings/string_util.h"
12 #include "components/autofill/core/browser/autofill_driver.h"
13 #include "components/autofill/core/browser/autofill_metrics.h"
14 #include "components/autofill/core/browser/autofill_xml_parser.h"
15 #include "components/autofill/core/browser/form_structure.h"
16 #include "components/autofill/core/common/autofill_pref_names.h"
17 #include "components/data_use_measurement/core/data_use_user_data.h"
18 #include "components/variations/net/variations_http_header_provider.h"
19 #include "net/base/load_flags.h"
20 #include "net/http/http_request_headers.h"
21 #include "net/http/http_response_headers.h"
22 #include "net/url_request/url_fetcher.h"
23 #include "third_party/webrtc/libjingle/xmllite/xmlparser.h"
30 const char kAutofillQueryServerNameStartInHeader
[] = "GFE/";
31 const size_t kMaxFormCacheSize
= 16;
33 #if defined(GOOGLE_CHROME_BUILD)
34 const char kClientName
[] = "Google Chrome";
36 const char kClientName
[] = "Chromium";
37 #endif // defined(GOOGLE_CHROME_BUILD)
39 std::string
RequestTypeToString(AutofillDownloadManager::RequestType type
) {
41 case AutofillDownloadManager::REQUEST_QUERY
:
43 case AutofillDownloadManager::REQUEST_UPLOAD
:
50 GURL
GetRequestUrl(AutofillDownloadManager::RequestType request_type
) {
51 return GURL("https://clients1.google.com/tbproxy/af/" +
52 RequestTypeToString(request_type
) + "?client=" + kClientName
);
57 struct AutofillDownloadManager::FormRequestData
{
58 std::vector
<std::string
> form_signatures
;
59 RequestType request_type
;
62 AutofillDownloadManager::AutofillDownloadManager(AutofillDriver
* driver
,
63 PrefService
* pref_service
,
66 pref_service_(pref_service
),
68 max_form_cache_size_(kMaxFormCacheSize
),
69 next_query_request_(base::Time::Now()),
70 next_upload_request_(base::Time::Now()),
71 positive_upload_rate_(0),
72 negative_upload_rate_(0),
73 fetcher_id_for_unittest_(0) {
75 positive_upload_rate_
=
76 pref_service_
->GetDouble(prefs::kAutofillPositiveUploadRate
);
77 negative_upload_rate_
=
78 pref_service_
->GetDouble(prefs::kAutofillNegativeUploadRate
);
81 AutofillDownloadManager::~AutofillDownloadManager() {
82 STLDeleteContainerPairFirstPointers(url_fetchers_
.begin(),
86 bool AutofillDownloadManager::StartQueryRequest(
87 const std::vector
<FormStructure
*>& forms
) {
88 if (next_query_request_
> base::Time::Now()) {
89 // We are in back-off mode: do not do the request.
93 FormRequestData request_data
;
94 if (!FormStructure::EncodeQueryRequest(forms
, &request_data
.form_signatures
,
99 request_data
.request_type
= AutofillDownloadManager::REQUEST_QUERY
;
100 AutofillMetrics::LogServerQueryMetric(AutofillMetrics::QUERY_SENT
);
102 std::string query_data
;
103 if (CheckCacheForQueryRequest(request_data
.form_signatures
, &query_data
)) {
104 VLOG(1) << "AutofillDownloadManager: query request has been retrieved "
105 << "from the cache, form signatures: "
106 << GetCombinedSignature(request_data
.form_signatures
);
107 observer_
->OnLoadedServerPredictions(query_data
);
111 return StartRequest(form_xml
, request_data
);
114 bool AutofillDownloadManager::StartUploadRequest(
115 const FormStructure
& form
,
116 bool form_was_autofilled
,
117 const ServerFieldTypeSet
& available_field_types
,
118 const std::string
& login_form_signature
) {
119 std::string form_xml
;
120 if (!form
.EncodeUploadRequest(available_field_types
, form_was_autofilled
,
121 login_form_signature
, &form_xml
))
124 if (next_upload_request_
> base::Time::Now()) {
125 // We are in back-off mode: do not do the request.
126 VLOG(1) << "AutofillDownloadManager: Upload request is throttled.";
130 // Flip a coin to see if we should upload this form.
131 double upload_rate
= form_was_autofilled
? GetPositiveUploadRate() :
132 GetNegativeUploadRate();
133 if (form
.upload_required() == UPLOAD_NOT_REQUIRED
||
134 (form
.upload_required() == USE_UPLOAD_RATES
&&
135 base::RandDouble() > upload_rate
)) {
136 VLOG(1) << "AutofillDownloadManager: Upload request is ignored.";
137 // If we ever need notification that upload was skipped, add it here.
141 FormRequestData request_data
;
142 request_data
.form_signatures
.push_back(form
.FormSignature());
143 request_data
.request_type
= AutofillDownloadManager::REQUEST_UPLOAD
;
145 return StartRequest(form_xml
, request_data
);
148 double AutofillDownloadManager::GetPositiveUploadRate() const {
149 return positive_upload_rate_
;
152 double AutofillDownloadManager::GetNegativeUploadRate() const {
153 return negative_upload_rate_
;
156 void AutofillDownloadManager::SetPositiveUploadRate(double rate
) {
157 if (rate
== positive_upload_rate_
)
159 positive_upload_rate_
= rate
;
160 DCHECK_GE(rate
, 0.0);
161 DCHECK_LE(rate
, 1.0);
162 pref_service_
->SetDouble(prefs::kAutofillPositiveUploadRate
, rate
);
165 void AutofillDownloadManager::SetNegativeUploadRate(double rate
) {
166 if (rate
== negative_upload_rate_
)
168 negative_upload_rate_
= rate
;
169 DCHECK_GE(rate
, 0.0);
170 DCHECK_LE(rate
, 1.0);
171 pref_service_
->SetDouble(prefs::kAutofillNegativeUploadRate
, rate
);
174 bool AutofillDownloadManager::StartRequest(
175 const std::string
& form_xml
,
176 const FormRequestData
& request_data
) {
177 net::URLRequestContextGetter
* request_context
=
178 driver_
->GetURLRequestContext();
179 DCHECK(request_context
);
180 GURL request_url
= GetRequestUrl(request_data
.request_type
);
182 // Id is ignored for regular chrome, in unit test id's for fake fetcher
183 // factory will be 0, 1, 2, ...
184 net::URLFetcher
* fetcher
=
185 net::URLFetcher::Create(fetcher_id_for_unittest_
++, request_url
,
186 net::URLFetcher::POST
, this).release();
187 data_use_measurement::DataUseUserData::AttachToFetcher(
188 fetcher
, data_use_measurement::DataUseUserData::AUTOFILL
);
189 url_fetchers_
[fetcher
] = request_data
;
190 fetcher
->SetAutomaticallyRetryOn5xx(false);
191 fetcher
->SetRequestContext(request_context
);
192 fetcher
->SetUploadData("text/plain", form_xml
);
193 fetcher
->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES
|
194 net::LOAD_DO_NOT_SEND_COOKIES
);
195 // Add Chrome experiment state to the request headers.
196 net::HttpRequestHeaders headers
;
197 variations::VariationsHttpHeaderProvider::GetInstance()->AppendHeaders(
198 fetcher
->GetOriginalURL(), driver_
->IsOffTheRecord(), false, &headers
);
199 fetcher
->SetExtraRequestHeaders(headers
.ToString());
202 VLOG(1) << "Sending AutofillDownloadManager "
203 << RequestTypeToString(request_data
.request_type
)
204 << " request: " << form_xml
;
209 void AutofillDownloadManager::CacheQueryRequest(
210 const std::vector
<std::string
>& forms_in_query
,
211 const std::string
& query_data
) {
212 std::string signature
= GetCombinedSignature(forms_in_query
);
213 for (QueryRequestCache::iterator it
= cached_forms_
.begin();
214 it
!= cached_forms_
.end(); ++it
) {
215 if (it
->first
== signature
) {
216 // We hit the cache, move to the first position and return.
217 std::pair
<std::string
, std::string
> data
= *it
;
218 cached_forms_
.erase(it
);
219 cached_forms_
.push_front(data
);
223 std::pair
<std::string
, std::string
> data
;
224 data
.first
= signature
;
225 data
.second
= query_data
;
226 cached_forms_
.push_front(data
);
227 while (cached_forms_
.size() > max_form_cache_size_
)
228 cached_forms_
.pop_back();
231 bool AutofillDownloadManager::CheckCacheForQueryRequest(
232 const std::vector
<std::string
>& forms_in_query
,
233 std::string
* query_data
) const {
234 std::string signature
= GetCombinedSignature(forms_in_query
);
235 for (QueryRequestCache::const_iterator it
= cached_forms_
.begin();
236 it
!= cached_forms_
.end(); ++it
) {
237 if (it
->first
== signature
) {
238 // We hit the cache, fill the data and return.
239 *query_data
= it
->second
;
246 std::string
AutofillDownloadManager::GetCombinedSignature(
247 const std::vector
<std::string
>& forms_in_query
) const {
248 size_t total_size
= forms_in_query
.size();
249 for (size_t i
= 0; i
< forms_in_query
.size(); ++i
)
250 total_size
+= forms_in_query
[i
].length();
251 std::string signature
;
253 signature
.reserve(total_size
);
255 for (size_t i
= 0; i
< forms_in_query
.size(); ++i
) {
257 signature
.append(",");
258 signature
.append(forms_in_query
[i
]);
263 void AutofillDownloadManager::OnURLFetchComplete(
264 const net::URLFetcher
* source
) {
265 std::map
<net::URLFetcher
*, FormRequestData
>::iterator it
=
266 url_fetchers_
.find(const_cast<net::URLFetcher
*>(source
));
267 if (it
== url_fetchers_
.end()) {
268 // Looks like crash on Mac is possibly caused with callback entering here
269 // with unknown fetcher when network is refreshed.
272 std::string
request_type(RequestTypeToString(it
->second
.request_type
));
273 const int kHttpResponseOk
= 200;
274 const int kHttpInternalServerError
= 500;
275 const int kHttpBadGateway
= 502;
276 const int kHttpServiceUnavailable
= 503;
278 CHECK(it
->second
.form_signatures
.size());
279 if (source
->GetResponseCode() != kHttpResponseOk
) {
280 bool back_off
= false;
281 std::string server_header
;
282 switch (source
->GetResponseCode()) {
283 case kHttpBadGateway
:
284 if (!source
->GetResponseHeaders()->EnumerateHeader(NULL
, "server",
286 base::StartsWith(server_header
.c_str(),
287 kAutofillQueryServerNameStartInHeader
,
288 base::CompareCase::INSENSITIVE_ASCII
) != 0)
290 // Bad gateway was received from Autofill servers. Fall through to back
292 case kHttpInternalServerError
:
293 case kHttpServiceUnavailable
:
299 base::Time
back_off_time(base::Time::Now() + source
->GetBackoffDelay());
300 if (it
->second
.request_type
== AutofillDownloadManager::REQUEST_QUERY
) {
301 next_query_request_
= back_off_time
;
303 next_upload_request_
= back_off_time
;
307 VLOG(1) << "AutofillDownloadManager: " << request_type
308 << " request has failed with response "
309 << source
->GetResponseCode();
310 observer_
->OnServerRequestError(it
->second
.form_signatures
[0],
311 it
->second
.request_type
,
312 source
->GetResponseCode());
314 std::string response_body
;
315 source
->GetResponseAsString(&response_body
);
316 VLOG(1) << "AutofillDownloadManager: " << request_type
317 << " request has succeeded with response body: " << response_body
;
318 if (it
->second
.request_type
== AutofillDownloadManager::REQUEST_QUERY
) {
319 CacheQueryRequest(it
->second
.form_signatures
, response_body
);
320 observer_
->OnLoadedServerPredictions(response_body
);
322 double new_positive_upload_rate
= 0;
323 double new_negative_upload_rate
= 0;
324 AutofillUploadXmlParser
parse_handler(&new_positive_upload_rate
,
325 &new_negative_upload_rate
);
326 buzz::XmlParser
parser(&parse_handler
);
327 parser
.Parse(response_body
.data(), response_body
.length(), true);
328 if (parse_handler
.succeeded()) {
329 SetPositiveUploadRate(new_positive_upload_rate
);
330 SetNegativeUploadRate(new_negative_upload_rate
);
333 observer_
->OnUploadedPossibleFieldTypes();
337 url_fetchers_
.erase(it
);
340 } // namespace autofill