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
3 // found in the LICENSE file.
4
5 #include "components/autofill/core/browser/autofill_download.h"
6
7 #include <algorithm>
8 #include <ostream>
9 #include <vector>
10
11 #include "base/logging.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/rand_util.h"
14 #include "base/stl_util.h"
15 #include "base/strings/string_util.h"
16 #include "components/autofill/core/browser/autofill_download_url.h"
17 #include "components/autofill/core/browser/autofill_driver.h"
18 #include "components/autofill/core/browser/autofill_metrics.h"
19 #include "components/autofill/core/browser/autofill_xml_parser.h"
20 #include "components/autofill/core/browser/form_structure.h"
21 #include "components/autofill/core/common/autofill_pref_names.h"
22 #include "net/base/load_flags.h"
23 #include "net/http/http_response_headers.h"
24 #include "net/url_request/url_fetcher.h"
25 #include "third_party/libjingle/source/talk/xmllite/xmlparser.h"
26 #include "url/gurl.h"
27
28 namespace autofill {
29
30 namespace {
31 const char kAutofillQueryServerNameStartInHeader[] = "GFE/";
32
33 const size_t kMaxFormCacheSize = 16;
34
35 // Generate field assignments xml that can be manually changed and then fed back
36 // into the Autofill server as experiment data.
LogFieldAssignments(const FormStructure & form,const ServerFieldTypeSet & available_field_types)37 static void LogFieldAssignments(
38 const FormStructure& form,
39 const ServerFieldTypeSet& available_field_types) {
40 std::string form_xml;
41 if (!form.EncodeFieldAssignments(available_field_types, &form_xml))
42 return;
43
44 VLOG(1) << "AutofillDownloadManager FieldAssignments for "
45 << form.source_url()
46 << " :\n"
47 << form_xml;
48 }
49
50 } // namespace
51
52 // static
AutofillRequestTypeToString(const AutofillRequestType type)53 std::string AutofillDownloadManager::AutofillRequestTypeToString(
54 const AutofillRequestType type) {
55 switch (type) {
56 case AutofillDownloadManager::REQUEST_QUERY:
57 return "query";
58 case AutofillDownloadManager::REQUEST_UPLOAD:
59 return "upload";
60 }
61 return std::string();
62 }
63
64 struct AutofillDownloadManager::FormRequestData {
65 std::vector<std::string> form_signatures;
66 AutofillRequestType request_type;
67 };
68
AutofillDownloadManager(AutofillDriver * driver,PrefService * pref_service,Observer * observer)69 AutofillDownloadManager::AutofillDownloadManager(AutofillDriver* driver,
70 PrefService* pref_service,
71 Observer* observer)
72 : driver_(driver),
73 pref_service_(pref_service),
74 observer_(observer),
75 max_form_cache_size_(kMaxFormCacheSize),
76 next_query_request_(base::Time::Now()),
77 next_upload_request_(base::Time::Now()),
78 positive_upload_rate_(0),
79 negative_upload_rate_(0),
80 fetcher_id_for_unittest_(0) {
81 DCHECK(observer_);
82 positive_upload_rate_ =
83 pref_service_->GetDouble(prefs::kAutofillPositiveUploadRate);
84 negative_upload_rate_ =
85 pref_service_->GetDouble(prefs::kAutofillNegativeUploadRate);
86 }
87
~AutofillDownloadManager()88 AutofillDownloadManager::~AutofillDownloadManager() {
89 STLDeleteContainerPairFirstPointers(url_fetchers_.begin(),
90 url_fetchers_.end());
91 }
92
StartQueryRequest(const std::vector<FormStructure * > & forms,const AutofillMetrics & metric_logger)93 bool AutofillDownloadManager::StartQueryRequest(
94 const std::vector<FormStructure*>& forms,
95 const AutofillMetrics& metric_logger) {
96 if (next_query_request_ > base::Time::Now()) {
97 // We are in back-off mode: do not do the request.
98 return false;
99 }
100 std::string form_xml;
101 FormRequestData request_data;
102 if (!FormStructure::EncodeQueryRequest(forms, &request_data.form_signatures,
103 &form_xml)) {
104 return false;
105 }
106
107 request_data.request_type = AutofillDownloadManager::REQUEST_QUERY;
108 metric_logger.LogServerQueryMetric(AutofillMetrics::QUERY_SENT);
109
110 std::string query_data;
111 if (CheckCacheForQueryRequest(request_data.form_signatures, &query_data)) {
112 DVLOG(1) << "AutofillDownloadManager: query request has been retrieved "
113 << "from the cache, form signatures: "
114 << GetCombinedSignature(request_data.form_signatures);
115 observer_->OnLoadedServerPredictions(query_data);
116 return true;
117 }
118
119 return StartRequest(form_xml, request_data);
120 }
121
StartUploadRequest(const FormStructure & form,bool form_was_autofilled,const ServerFieldTypeSet & available_field_types)122 bool AutofillDownloadManager::StartUploadRequest(
123 const FormStructure& form,
124 bool form_was_autofilled,
125 const ServerFieldTypeSet& available_field_types) {
126 std::string form_xml;
127 if (!form.EncodeUploadRequest(available_field_types, form_was_autofilled,
128 &form_xml))
129 return false;
130
131 LogFieldAssignments(form, available_field_types);
132
133 if (next_upload_request_ > base::Time::Now()) {
134 // We are in back-off mode: do not do the request.
135 DVLOG(1) << "AutofillDownloadManager: Upload request is throttled.";
136 return false;
137 }
138
139 // Flip a coin to see if we should upload this form.
140 double upload_rate = form_was_autofilled ? GetPositiveUploadRate() :
141 GetNegativeUploadRate();
142 if (form.upload_required() == UPLOAD_NOT_REQUIRED ||
143 (form.upload_required() == USE_UPLOAD_RATES &&
144 base::RandDouble() > upload_rate)) {
145 DVLOG(1) << "AutofillDownloadManager: Upload request is ignored.";
146 // If we ever need notification that upload was skipped, add it here.
147 return false;
148 }
149
150 FormRequestData request_data;
151 request_data.form_signatures.push_back(form.FormSignature());
152 request_data.request_type = AutofillDownloadManager::REQUEST_UPLOAD;
153
154 return StartRequest(form_xml, request_data);
155 }
156
GetPositiveUploadRate() const157 double AutofillDownloadManager::GetPositiveUploadRate() const {
158 return positive_upload_rate_;
159 }
160
GetNegativeUploadRate() const161 double AutofillDownloadManager::GetNegativeUploadRate() const {
162 return negative_upload_rate_;
163 }
164
SetPositiveUploadRate(double rate)165 void AutofillDownloadManager::SetPositiveUploadRate(double rate) {
166 if (rate == positive_upload_rate_)
167 return;
168 positive_upload_rate_ = rate;
169 DCHECK_GE(rate, 0.0);
170 DCHECK_LE(rate, 1.0);
171 pref_service_->SetDouble(prefs::kAutofillPositiveUploadRate, rate);
172 }
173
SetNegativeUploadRate(double rate)174 void AutofillDownloadManager::SetNegativeUploadRate(double rate) {
175 if (rate == negative_upload_rate_)
176 return;
177 negative_upload_rate_ = rate;
178 DCHECK_GE(rate, 0.0);
179 DCHECK_LE(rate, 1.0);
180 pref_service_->SetDouble(prefs::kAutofillNegativeUploadRate, rate);
181 }
182
StartRequest(const std::string & form_xml,const FormRequestData & request_data)183 bool AutofillDownloadManager::StartRequest(
184 const std::string& form_xml,
185 const FormRequestData& request_data) {
186 net::URLRequestContextGetter* request_context =
187 driver_->GetURLRequestContext();
188 DCHECK(request_context);
189 GURL request_url;
190 if (request_data.request_type == AutofillDownloadManager::REQUEST_QUERY)
191 request_url = autofill::GetAutofillQueryUrl();
192 else
193 request_url = autofill::GetAutofillUploadUrl();
194
195 // Id is ignored for regular chrome, in unit test id's for fake fetcher
196 // factory will be 0, 1, 2, ...
197 net::URLFetcher* fetcher = net::URLFetcher::Create(
198 fetcher_id_for_unittest_++, request_url, net::URLFetcher::POST,
199 this);
200 url_fetchers_[fetcher] = request_data;
201 fetcher->SetAutomaticallyRetryOn5xx(false);
202 fetcher->SetRequestContext(request_context);
203 fetcher->SetUploadData("text/plain", form_xml);
204 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES |
205 net::LOAD_DO_NOT_SEND_COOKIES);
206 fetcher->Start();
207
208 DVLOG(1) << "Sending AutofillDownloadManager "
209 << AutofillRequestTypeToString(request_data.request_type)
210 << " request: " << form_xml;
211
212 return true;
213 }
214
CacheQueryRequest(const std::vector<std::string> & forms_in_query,const std::string & query_data)215 void AutofillDownloadManager::CacheQueryRequest(
216 const std::vector<std::string>& forms_in_query,
217 const std::string& query_data) {
218 std::string signature = GetCombinedSignature(forms_in_query);
219 for (QueryRequestCache::iterator it = cached_forms_.begin();
220 it != cached_forms_.end(); ++it) {
221 if (it->first == signature) {
222 // We hit the cache, move to the first position and return.
223 std::pair<std::string, std::string> data = *it;
224 cached_forms_.erase(it);
225 cached_forms_.push_front(data);
226 return;
227 }
228 }
229 std::pair<std::string, std::string> data;
230 data.first = signature;
231 data.second = query_data;
232 cached_forms_.push_front(data);
233 while (cached_forms_.size() > max_form_cache_size_)
234 cached_forms_.pop_back();
235 }
236
CheckCacheForQueryRequest(const std::vector<std::string> & forms_in_query,std::string * query_data) const237 bool AutofillDownloadManager::CheckCacheForQueryRequest(
238 const std::vector<std::string>& forms_in_query,
239 std::string* query_data) const {
240 std::string signature = GetCombinedSignature(forms_in_query);
241 for (QueryRequestCache::const_iterator it = cached_forms_.begin();
242 it != cached_forms_.end(); ++it) {
243 if (it->first == signature) {
244 // We hit the cache, fill the data and return.
245 *query_data = it->second;
246 return true;
247 }
248 }
249 return false;
250 }
251
GetCombinedSignature(const std::vector<std::string> & forms_in_query) const252 std::string AutofillDownloadManager::GetCombinedSignature(
253 const std::vector<std::string>& forms_in_query) const {
254 size_t total_size = forms_in_query.size();
255 for (size_t i = 0; i < forms_in_query.size(); ++i)
256 total_size += forms_in_query[i].length();
257 std::string signature;
258
259 signature.reserve(total_size);
260
261 for (size_t i = 0; i < forms_in_query.size(); ++i) {
262 if (i)
263 signature.append(",");
264 signature.append(forms_in_query[i]);
265 }
266 return signature;
267 }
268
OnURLFetchComplete(const net::URLFetcher * source)269 void AutofillDownloadManager::OnURLFetchComplete(
270 const net::URLFetcher* source) {
271 std::map<net::URLFetcher *, FormRequestData>::iterator it =
272 url_fetchers_.find(const_cast<net::URLFetcher*>(source));
273 if (it == url_fetchers_.end()) {
274 // Looks like crash on Mac is possibly caused with callback entering here
275 // with unknown fetcher when network is refreshed.
276 return;
277 }
278 std::string type_of_request(
279 AutofillRequestTypeToString(it->second.request_type));
280 const int kHttpResponseOk = 200;
281 const int kHttpInternalServerError = 500;
282 const int kHttpBadGateway = 502;
283 const int kHttpServiceUnavailable = 503;
284
285 CHECK(it->second.form_signatures.size());
286 if (source->GetResponseCode() != kHttpResponseOk) {
287 bool back_off = false;
288 std::string server_header;
289 switch (source->GetResponseCode()) {
290 case kHttpBadGateway:
291 if (!source->GetResponseHeaders()->EnumerateHeader(NULL, "server",
292 &server_header) ||
293 StartsWithASCII(server_header.c_str(),
294 kAutofillQueryServerNameStartInHeader,
295 false) != 0)
296 break;
297 // Bad gateway was received from Autofill servers. Fall through to back
298 // off.
299 case kHttpInternalServerError:
300 case kHttpServiceUnavailable:
301 back_off = true;
302 break;
303 }
304
305 if (back_off) {
306 base::Time back_off_time(base::Time::Now() + source->GetBackoffDelay());
307 if (it->second.request_type == AutofillDownloadManager::REQUEST_QUERY) {
308 next_query_request_ = back_off_time;
309 } else {
310 next_upload_request_ = back_off_time;
311 }
312 }
313
314 DVLOG(1) << "AutofillDownloadManager: " << type_of_request
315 << " request has failed with response "
316 << source->GetResponseCode();
317 observer_->OnServerRequestError(it->second.form_signatures[0],
318 it->second.request_type,
319 source->GetResponseCode());
320 } else {
321 std::string response_body;
322 source->GetResponseAsString(&response_body);
323 DVLOG(1) << "AutofillDownloadManager: " << type_of_request
324 << " request has succeeded with response body: "
325 << response_body;
326 if (it->second.request_type == AutofillDownloadManager::REQUEST_QUERY) {
327 CacheQueryRequest(it->second.form_signatures, response_body);
328 observer_->OnLoadedServerPredictions(response_body);
329 } else {
330 double new_positive_upload_rate = 0;
331 double new_negative_upload_rate = 0;
332 AutofillUploadXmlParser parse_handler(&new_positive_upload_rate,
333 &new_negative_upload_rate);
334 buzz::XmlParser parser(&parse_handler);
335 parser.Parse(response_body.data(), response_body.length(), true);
336 if (parse_handler.succeeded()) {
337 SetPositiveUploadRate(new_positive_upload_rate);
338 SetNegativeUploadRate(new_negative_upload_rate);
339 }
340
341 observer_->OnUploadedPossibleFieldTypes();
342 }
343 }
344 delete it->first;
345 url_fetchers_.erase(it);
346 }
347
348 } // namespace autofill
349