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/variations/net/variations_http_header_provider.h"
18 #include "net/base/load_flags.h"
19 #include "net/http/http_request_headers.h"
20 #include "net/http/http_response_headers.h"
21 #include "net/url_request/url_fetcher.h"
22 #include "third_party/webrtc/libjingle/xmllite/xmlparser.h"
29 const char kAutofillQueryServerNameStartInHeader
[] = "GFE/";
30 const size_t kMaxFormCacheSize
= 16;
32 #if defined(GOOGLE_CHROME_BUILD)
33 const char kClientName
[] = "Google Chrome";
35 const char kClientName
[] = "Chromium";
36 #endif // defined(GOOGLE_CHROME_BUILD)
38 std::string
RequestTypeToString(AutofillDownloadManager::RequestType type
) {
40 case AutofillDownloadManager::REQUEST_QUERY
:
42 case AutofillDownloadManager::REQUEST_UPLOAD
:
49 GURL
GetRequestUrl(AutofillDownloadManager::RequestType request_type
) {
50 return GURL("https://clients1.google.com/tbproxy/af/" +
51 RequestTypeToString(request_type
) + "?client=" + kClientName
);
56 struct AutofillDownloadManager::FormRequestData
{
57 std::vector
<std::string
> form_signatures
;
58 RequestType request_type
;
61 AutofillDownloadManager::AutofillDownloadManager(AutofillDriver
* driver
,
62 PrefService
* pref_service
,
65 pref_service_(pref_service
),
67 max_form_cache_size_(kMaxFormCacheSize
),
68 next_query_request_(base::Time::Now()),
69 next_upload_request_(base::Time::Now()),
70 positive_upload_rate_(0),
71 negative_upload_rate_(0),
72 fetcher_id_for_unittest_(0) {
74 positive_upload_rate_
=
75 pref_service_
->GetDouble(prefs::kAutofillPositiveUploadRate
);
76 negative_upload_rate_
=
77 pref_service_
->GetDouble(prefs::kAutofillNegativeUploadRate
);
80 AutofillDownloadManager::~AutofillDownloadManager() {
81 STLDeleteContainerPairFirstPointers(url_fetchers_
.begin(),
85 bool AutofillDownloadManager::StartQueryRequest(
86 const std::vector
<FormStructure
*>& forms
) {
87 if (next_query_request_
> base::Time::Now()) {
88 // We are in back-off mode: do not do the request.
92 FormRequestData request_data
;
93 if (!FormStructure::EncodeQueryRequest(forms
, &request_data
.form_signatures
,
98 request_data
.request_type
= AutofillDownloadManager::REQUEST_QUERY
;
99 AutofillMetrics::LogServerQueryMetric(AutofillMetrics::QUERY_SENT
);
101 std::string query_data
;
102 if (CheckCacheForQueryRequest(request_data
.form_signatures
, &query_data
)) {
103 VLOG(1) << "AutofillDownloadManager: query request has been retrieved "
104 << "from the cache, form signatures: "
105 << GetCombinedSignature(request_data
.form_signatures
);
106 observer_
->OnLoadedServerPredictions(query_data
);
110 return StartRequest(form_xml
, request_data
);
113 bool AutofillDownloadManager::StartUploadRequest(
114 const FormStructure
& form
,
115 bool form_was_autofilled
,
116 const ServerFieldTypeSet
& available_field_types
,
117 const std::string
& login_form_signature
) {
118 std::string form_xml
;
119 if (!form
.EncodeUploadRequest(available_field_types
, form_was_autofilled
,
120 login_form_signature
, &form_xml
))
123 if (next_upload_request_
> base::Time::Now()) {
124 // We are in back-off mode: do not do the request.
125 VLOG(1) << "AutofillDownloadManager: Upload request is throttled.";
129 // Flip a coin to see if we should upload this form.
130 double upload_rate
= form_was_autofilled
? GetPositiveUploadRate() :
131 GetNegativeUploadRate();
132 if (form
.upload_required() == UPLOAD_NOT_REQUIRED
||
133 (form
.upload_required() == USE_UPLOAD_RATES
&&
134 base::RandDouble() > upload_rate
)) {
135 VLOG(1) << "AutofillDownloadManager: Upload request is ignored.";
136 // If we ever need notification that upload was skipped, add it here.
140 FormRequestData request_data
;
141 request_data
.form_signatures
.push_back(form
.FormSignature());
142 request_data
.request_type
= AutofillDownloadManager::REQUEST_UPLOAD
;
144 return StartRequest(form_xml
, request_data
);
147 double AutofillDownloadManager::GetPositiveUploadRate() const {
148 return positive_upload_rate_
;
151 double AutofillDownloadManager::GetNegativeUploadRate() const {
152 return negative_upload_rate_
;
155 void AutofillDownloadManager::SetPositiveUploadRate(double rate
) {
156 if (rate
== positive_upload_rate_
)
158 positive_upload_rate_
= rate
;
159 DCHECK_GE(rate
, 0.0);
160 DCHECK_LE(rate
, 1.0);
161 pref_service_
->SetDouble(prefs::kAutofillPositiveUploadRate
, rate
);
164 void AutofillDownloadManager::SetNegativeUploadRate(double rate
) {
165 if (rate
== negative_upload_rate_
)
167 negative_upload_rate_
= rate
;
168 DCHECK_GE(rate
, 0.0);
169 DCHECK_LE(rate
, 1.0);
170 pref_service_
->SetDouble(prefs::kAutofillNegativeUploadRate
, rate
);
173 bool AutofillDownloadManager::StartRequest(
174 const std::string
& form_xml
,
175 const FormRequestData
& request_data
) {
176 net::URLRequestContextGetter
* request_context
=
177 driver_
->GetURLRequestContext();
178 DCHECK(request_context
);
179 GURL request_url
= GetRequestUrl(request_data
.request_type
);
181 // Id is ignored for regular chrome, in unit test id's for fake fetcher
182 // factory will be 0, 1, 2, ...
183 net::URLFetcher
* fetcher
=
184 net::URLFetcher::Create(fetcher_id_for_unittest_
++, request_url
,
185 net::URLFetcher::POST
, this).release();
186 url_fetchers_
[fetcher
] = request_data
;
187 fetcher
->SetAutomaticallyRetryOn5xx(false);
188 fetcher
->SetRequestContext(request_context
);
189 fetcher
->SetUploadData("text/plain", form_xml
);
190 fetcher
->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES
|
191 net::LOAD_DO_NOT_SEND_COOKIES
);
192 // Add Chrome experiment state to the request headers.
193 net::HttpRequestHeaders headers
;
194 variations::VariationsHttpHeaderProvider::GetInstance()->AppendHeaders(
195 fetcher
->GetOriginalURL(), driver_
->IsOffTheRecord(), false, &headers
);
196 fetcher
->SetExtraRequestHeaders(headers
.ToString());
199 VLOG(1) << "Sending AutofillDownloadManager "
200 << RequestTypeToString(request_data
.request_type
)
201 << " request: " << form_xml
;
206 void AutofillDownloadManager::CacheQueryRequest(
207 const std::vector
<std::string
>& forms_in_query
,
208 const std::string
& query_data
) {
209 std::string signature
= GetCombinedSignature(forms_in_query
);
210 for (QueryRequestCache::iterator it
= cached_forms_
.begin();
211 it
!= cached_forms_
.end(); ++it
) {
212 if (it
->first
== signature
) {
213 // We hit the cache, move to the first position and return.
214 std::pair
<std::string
, std::string
> data
= *it
;
215 cached_forms_
.erase(it
);
216 cached_forms_
.push_front(data
);
220 std::pair
<std::string
, std::string
> data
;
221 data
.first
= signature
;
222 data
.second
= query_data
;
223 cached_forms_
.push_front(data
);
224 while (cached_forms_
.size() > max_form_cache_size_
)
225 cached_forms_
.pop_back();
228 bool AutofillDownloadManager::CheckCacheForQueryRequest(
229 const std::vector
<std::string
>& forms_in_query
,
230 std::string
* query_data
) const {
231 std::string signature
= GetCombinedSignature(forms_in_query
);
232 for (QueryRequestCache::const_iterator it
= cached_forms_
.begin();
233 it
!= cached_forms_
.end(); ++it
) {
234 if (it
->first
== signature
) {
235 // We hit the cache, fill the data and return.
236 *query_data
= it
->second
;
243 std::string
AutofillDownloadManager::GetCombinedSignature(
244 const std::vector
<std::string
>& forms_in_query
) const {
245 size_t total_size
= forms_in_query
.size();
246 for (size_t i
= 0; i
< forms_in_query
.size(); ++i
)
247 total_size
+= forms_in_query
[i
].length();
248 std::string signature
;
250 signature
.reserve(total_size
);
252 for (size_t i
= 0; i
< forms_in_query
.size(); ++i
) {
254 signature
.append(",");
255 signature
.append(forms_in_query
[i
]);
260 void AutofillDownloadManager::OnURLFetchComplete(
261 const net::URLFetcher
* source
) {
262 std::map
<net::URLFetcher
*, FormRequestData
>::iterator it
=
263 url_fetchers_
.find(const_cast<net::URLFetcher
*>(source
));
264 if (it
== url_fetchers_
.end()) {
265 // Looks like crash on Mac is possibly caused with callback entering here
266 // with unknown fetcher when network is refreshed.
269 std::string
request_type(RequestTypeToString(it
->second
.request_type
));
270 const int kHttpResponseOk
= 200;
271 const int kHttpInternalServerError
= 500;
272 const int kHttpBadGateway
= 502;
273 const int kHttpServiceUnavailable
= 503;
275 CHECK(it
->second
.form_signatures
.size());
276 if (source
->GetResponseCode() != kHttpResponseOk
) {
277 bool back_off
= false;
278 std::string server_header
;
279 switch (source
->GetResponseCode()) {
280 case kHttpBadGateway
:
281 if (!source
->GetResponseHeaders()->EnumerateHeader(NULL
, "server",
283 base::StartsWith(server_header
.c_str(),
284 kAutofillQueryServerNameStartInHeader
,
285 base::CompareCase::INSENSITIVE_ASCII
) != 0)
287 // Bad gateway was received from Autofill servers. Fall through to back
289 case kHttpInternalServerError
:
290 case kHttpServiceUnavailable
:
296 base::Time
back_off_time(base::Time::Now() + source
->GetBackoffDelay());
297 if (it
->second
.request_type
== AutofillDownloadManager::REQUEST_QUERY
) {
298 next_query_request_
= back_off_time
;
300 next_upload_request_
= back_off_time
;
304 VLOG(1) << "AutofillDownloadManager: " << request_type
305 << " request has failed with response "
306 << source
->GetResponseCode();
307 observer_
->OnServerRequestError(it
->second
.form_signatures
[0],
308 it
->second
.request_type
,
309 source
->GetResponseCode());
311 std::string response_body
;
312 source
->GetResponseAsString(&response_body
);
313 VLOG(1) << "AutofillDownloadManager: " << request_type
314 << " request has succeeded with response body: " << response_body
;
315 if (it
->second
.request_type
== AutofillDownloadManager::REQUEST_QUERY
) {
316 CacheQueryRequest(it
->second
.form_signatures
, response_body
);
317 observer_
->OnLoadedServerPredictions(response_body
);
319 double new_positive_upload_rate
= 0;
320 double new_negative_upload_rate
= 0;
321 AutofillUploadXmlParser
parse_handler(&new_positive_upload_rate
,
322 &new_negative_upload_rate
);
323 buzz::XmlParser
parser(&parse_handler
);
324 parser
.Parse(response_body
.data(), response_body
.length(), true);
325 if (parse_handler
.succeeded()) {
326 SetPositiveUploadRate(new_positive_upload_rate
);
327 SetNegativeUploadRate(new_negative_upload_rate
);
330 observer_
->OnUploadedPossibleFieldTypes();
334 url_fetchers_
.erase(it
);
337 } // namespace autofill