ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / components / autofill / core / browser / autofill_download_manager.cc
blobafe5679e49ddbdc2b2fcbd86a0f5a76a08f821d7
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 "net/base/load_flags.h"
18 #include "net/http/http_response_headers.h"
19 #include "net/url_request/url_fetcher.h"
20 #include "third_party/webrtc/libjingle/xmllite/xmlparser.h"
21 #include "url/gurl.h"
23 namespace autofill {
25 namespace {
27 const char kAutofillQueryServerNameStartInHeader[] = "GFE/";
28 const size_t kMaxFormCacheSize = 16;
30 #if defined(GOOGLE_CHROME_BUILD)
31 const char kClientName[] = "Google Chrome";
32 #else
33 const char kClientName[] = "Chromium";
34 #endif // defined(GOOGLE_CHROME_BUILD)
36 std::string RequestTypeToString(AutofillDownloadManager::RequestType type) {
37 switch (type) {
38 case AutofillDownloadManager::REQUEST_QUERY:
39 return "query";
40 case AutofillDownloadManager::REQUEST_UPLOAD:
41 return "upload";
43 NOTREACHED();
44 return std::string();
47 GURL GetRequestUrl(AutofillDownloadManager::RequestType request_type) {
48 return GURL("https://clients1.google.com/tbproxy/af/" +
49 RequestTypeToString(request_type) + "?client=" + kClientName);
52 } // namespace
54 struct AutofillDownloadManager::FormRequestData {
55 std::vector<std::string> form_signatures;
56 RequestType request_type;
59 AutofillDownloadManager::AutofillDownloadManager(AutofillDriver* driver,
60 PrefService* pref_service,
61 Observer* observer)
62 : driver_(driver),
63 pref_service_(pref_service),
64 observer_(observer),
65 max_form_cache_size_(kMaxFormCacheSize),
66 next_query_request_(base::Time::Now()),
67 next_upload_request_(base::Time::Now()),
68 positive_upload_rate_(0),
69 negative_upload_rate_(0),
70 fetcher_id_for_unittest_(0) {
71 DCHECK(observer_);
72 positive_upload_rate_ =
73 pref_service_->GetDouble(prefs::kAutofillPositiveUploadRate);
74 negative_upload_rate_ =
75 pref_service_->GetDouble(prefs::kAutofillNegativeUploadRate);
78 AutofillDownloadManager::~AutofillDownloadManager() {
79 STLDeleteContainerPairFirstPointers(url_fetchers_.begin(),
80 url_fetchers_.end());
83 bool AutofillDownloadManager::StartQueryRequest(
84 const std::vector<FormStructure*>& forms) {
85 if (next_query_request_ > base::Time::Now()) {
86 // We are in back-off mode: do not do the request.
87 return false;
89 std::string form_xml;
90 FormRequestData request_data;
91 if (!FormStructure::EncodeQueryRequest(forms, &request_data.form_signatures,
92 &form_xml)) {
93 return false;
96 request_data.request_type = AutofillDownloadManager::REQUEST_QUERY;
97 AutofillMetrics::LogServerQueryMetric(AutofillMetrics::QUERY_SENT);
99 std::string query_data;
100 if (CheckCacheForQueryRequest(request_data.form_signatures, &query_data)) {
101 VLOG(1) << "AutofillDownloadManager: query request has been retrieved "
102 << "from the cache, form signatures: "
103 << GetCombinedSignature(request_data.form_signatures);
104 observer_->OnLoadedServerPredictions(query_data);
105 return true;
108 return StartRequest(form_xml, request_data);
111 bool AutofillDownloadManager::StartUploadRequest(
112 const FormStructure& form,
113 bool form_was_autofilled,
114 const ServerFieldTypeSet& available_field_types) {
115 std::string form_xml;
116 if (!form.EncodeUploadRequest(available_field_types, form_was_autofilled,
117 &form_xml))
118 return false;
120 if (next_upload_request_ > base::Time::Now()) {
121 // We are in back-off mode: do not do the request.
122 VLOG(1) << "AutofillDownloadManager: Upload request is throttled.";
123 return false;
126 // Flip a coin to see if we should upload this form.
127 double upload_rate = form_was_autofilled ? GetPositiveUploadRate() :
128 GetNegativeUploadRate();
129 if (form.upload_required() == UPLOAD_NOT_REQUIRED ||
130 (form.upload_required() == USE_UPLOAD_RATES &&
131 base::RandDouble() > upload_rate)) {
132 VLOG(1) << "AutofillDownloadManager: Upload request is ignored.";
133 // If we ever need notification that upload was skipped, add it here.
134 return false;
137 FormRequestData request_data;
138 request_data.form_signatures.push_back(form.FormSignature());
139 request_data.request_type = AutofillDownloadManager::REQUEST_UPLOAD;
141 return StartRequest(form_xml, request_data);
144 double AutofillDownloadManager::GetPositiveUploadRate() const {
145 return positive_upload_rate_;
148 double AutofillDownloadManager::GetNegativeUploadRate() const {
149 return negative_upload_rate_;
152 void AutofillDownloadManager::SetPositiveUploadRate(double rate) {
153 if (rate == positive_upload_rate_)
154 return;
155 positive_upload_rate_ = rate;
156 DCHECK_GE(rate, 0.0);
157 DCHECK_LE(rate, 1.0);
158 pref_service_->SetDouble(prefs::kAutofillPositiveUploadRate, rate);
161 void AutofillDownloadManager::SetNegativeUploadRate(double rate) {
162 if (rate == negative_upload_rate_)
163 return;
164 negative_upload_rate_ = rate;
165 DCHECK_GE(rate, 0.0);
166 DCHECK_LE(rate, 1.0);
167 pref_service_->SetDouble(prefs::kAutofillNegativeUploadRate, rate);
170 bool AutofillDownloadManager::StartRequest(
171 const std::string& form_xml,
172 const FormRequestData& request_data) {
173 net::URLRequestContextGetter* request_context =
174 driver_->GetURLRequestContext();
175 DCHECK(request_context);
176 GURL request_url = GetRequestUrl(request_data.request_type);
178 // Id is ignored for regular chrome, in unit test id's for fake fetcher
179 // factory will be 0, 1, 2, ...
180 net::URLFetcher* fetcher = net::URLFetcher::Create(
181 fetcher_id_for_unittest_++, request_url, net::URLFetcher::POST,
182 this);
183 url_fetchers_[fetcher] = request_data;
184 fetcher->SetAutomaticallyRetryOn5xx(false);
185 fetcher->SetRequestContext(request_context);
186 fetcher->SetUploadData("text/plain", form_xml);
187 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES |
188 net::LOAD_DO_NOT_SEND_COOKIES);
189 fetcher->Start();
191 VLOG(1) << "Sending AutofillDownloadManager "
192 << RequestTypeToString(request_data.request_type)
193 << " request: " << form_xml;
195 return true;
198 void AutofillDownloadManager::CacheQueryRequest(
199 const std::vector<std::string>& forms_in_query,
200 const std::string& query_data) {
201 std::string signature = GetCombinedSignature(forms_in_query);
202 for (QueryRequestCache::iterator it = cached_forms_.begin();
203 it != cached_forms_.end(); ++it) {
204 if (it->first == signature) {
205 // We hit the cache, move to the first position and return.
206 std::pair<std::string, std::string> data = *it;
207 cached_forms_.erase(it);
208 cached_forms_.push_front(data);
209 return;
212 std::pair<std::string, std::string> data;
213 data.first = signature;
214 data.second = query_data;
215 cached_forms_.push_front(data);
216 while (cached_forms_.size() > max_form_cache_size_)
217 cached_forms_.pop_back();
220 bool AutofillDownloadManager::CheckCacheForQueryRequest(
221 const std::vector<std::string>& forms_in_query,
222 std::string* query_data) const {
223 std::string signature = GetCombinedSignature(forms_in_query);
224 for (QueryRequestCache::const_iterator it = cached_forms_.begin();
225 it != cached_forms_.end(); ++it) {
226 if (it->first == signature) {
227 // We hit the cache, fill the data and return.
228 *query_data = it->second;
229 return true;
232 return false;
235 std::string AutofillDownloadManager::GetCombinedSignature(
236 const std::vector<std::string>& forms_in_query) const {
237 size_t total_size = forms_in_query.size();
238 for (size_t i = 0; i < forms_in_query.size(); ++i)
239 total_size += forms_in_query[i].length();
240 std::string signature;
242 signature.reserve(total_size);
244 for (size_t i = 0; i < forms_in_query.size(); ++i) {
245 if (i)
246 signature.append(",");
247 signature.append(forms_in_query[i]);
249 return signature;
252 void AutofillDownloadManager::OnURLFetchComplete(
253 const net::URLFetcher* source) {
254 std::map<net::URLFetcher *, FormRequestData>::iterator it =
255 url_fetchers_.find(const_cast<net::URLFetcher*>(source));
256 if (it == url_fetchers_.end()) {
257 // Looks like crash on Mac is possibly caused with callback entering here
258 // with unknown fetcher when network is refreshed.
259 return;
261 std::string request_type(RequestTypeToString(it->second.request_type));
262 const int kHttpResponseOk = 200;
263 const int kHttpInternalServerError = 500;
264 const int kHttpBadGateway = 502;
265 const int kHttpServiceUnavailable = 503;
267 CHECK(it->second.form_signatures.size());
268 if (source->GetResponseCode() != kHttpResponseOk) {
269 bool back_off = false;
270 std::string server_header;
271 switch (source->GetResponseCode()) {
272 case kHttpBadGateway:
273 if (!source->GetResponseHeaders()->EnumerateHeader(NULL, "server",
274 &server_header) ||
275 StartsWithASCII(server_header.c_str(),
276 kAutofillQueryServerNameStartInHeader,
277 false) != 0)
278 break;
279 // Bad gateway was received from Autofill servers. Fall through to back
280 // off.
281 case kHttpInternalServerError:
282 case kHttpServiceUnavailable:
283 back_off = true;
284 break;
287 if (back_off) {
288 base::Time back_off_time(base::Time::Now() + source->GetBackoffDelay());
289 if (it->second.request_type == AutofillDownloadManager::REQUEST_QUERY) {
290 next_query_request_ = back_off_time;
291 } else {
292 next_upload_request_ = back_off_time;
296 VLOG(1) << "AutofillDownloadManager: " << request_type
297 << " request has failed with response "
298 << source->GetResponseCode();
299 observer_->OnServerRequestError(it->second.form_signatures[0],
300 it->second.request_type,
301 source->GetResponseCode());
302 } else {
303 std::string response_body;
304 source->GetResponseAsString(&response_body);
305 VLOG(1) << "AutofillDownloadManager: " << request_type
306 << " request has succeeded with response body: " << response_body;
307 if (it->second.request_type == AutofillDownloadManager::REQUEST_QUERY) {
308 CacheQueryRequest(it->second.form_signatures, response_body);
309 observer_->OnLoadedServerPredictions(response_body);
310 } else {
311 double new_positive_upload_rate = 0;
312 double new_negative_upload_rate = 0;
313 AutofillUploadXmlParser parse_handler(&new_positive_upload_rate,
314 &new_negative_upload_rate);
315 buzz::XmlParser parser(&parse_handler);
316 parser.Parse(response_body.data(), response_body.length(), true);
317 if (parse_handler.succeeded()) {
318 SetPositiveUploadRate(new_positive_upload_rate);
319 SetNegativeUploadRate(new_negative_upload_rate);
322 observer_->OnUploadedPossibleFieldTypes();
325 delete it->first;
326 url_fetchers_.erase(it);
329 } // namespace autofill