1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/autofill/core/browser/autofill_download_manager.h"
#include <utility>
#include "base/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/numerics/safe_conversions.h"
#include "base/rand_util.h"
#include "base/strings/string_util.h"
#include "base/threading/thread_task_runner_handle.h"
#include "components/autofill/core/browser/autofill_driver.h"
#include "components/autofill/core/browser/autofill_metrics.h"
#include "components/autofill/core/browser/form_structure.h"
#include "components/autofill/core/browser/proto/server.pb.h"
#include "components/autofill/core/common/autofill_pref_names.h"
#include "components/data_use_measurement/core/data_use_user_data.h"
#include "components/variations/net/variations_http_headers.h"
#include "net/base/load_flags.h"
#include "net/http/http_request_headers.h"
#include "net/http/http_response_headers.h"
#include "net/http/http_status_code.h"
#include "net/url_request/url_fetcher.h"
#include "url/gurl.h"
namespace autofill {
namespace {
const size_t kMaxFormCacheSize = 16;
const size_t kMaxFieldsPerQueryRequest = 100;
const net::BackoffEntry::Policy kAutofillBackoffPolicy = {
// Number of initial errors (in sequence) to ignore before applying
// exponential back-off rules.
0,
// Initial delay for exponential back-off in ms.
1000, // 1 second.
// Factor by which the waiting time will be multiplied.
2,
// Fuzzing percentage. ex: 10% will spread requests randomly
// between 90%-100% of the calculated time.
0.33, // 33%.
// Maximum amount of time we are willing to delay our request in ms.
30 * 1000, // 30 seconds.
// Time to keep an entry from being discarded even when it
// has no significant state, -1 to never discard.
-1,
// Don't use initial delay unless the last request was an error.
false,
};
#if defined(GOOGLE_CHROME_BUILD)
const char kClientName[] = "Google Chrome";
#else
const char kClientName[] = "Chromium";
#endif // defined(GOOGLE_CHROME_BUILD)
size_t CountActiveFieldsInForms(const std::vector<FormStructure*>& forms) {
size_t active_field_count = 0;
for (const auto* form : forms)
active_field_count += form->active_field_count();
return active_field_count;
}
std::string RequestTypeToString(AutofillDownloadManager::RequestType type) {
switch (type) {
case AutofillDownloadManager::REQUEST_QUERY:
return "query";
case AutofillDownloadManager::REQUEST_UPLOAD:
return "upload";
}
NOTREACHED();
return std::string();
}
GURL GetRequestUrl(AutofillDownloadManager::RequestType request_type) {
return GURL("https://clients1.google.com/tbproxy/af/" +
RequestTypeToString(request_type) + "?client=" + kClientName);
}
std::ostream& operator<<(std::ostream& out,
const autofill::AutofillQueryContents& query) {
out << "client_version: " << query.client_version();
for (const auto& form : query.form()) {
out << "\nForm\n signature: " << form.signature();
for (const auto& field : form.field()) {
out << "\n Field\n signature: " << field.signature();
if (!field.name().empty())
out << "\n name: " << field.name();
if (!field.type().empty())
out << "\n type: " << field.type();
}
}
return out;
}
std::ostream& operator<<(std::ostream& out,
const autofill::AutofillUploadContents& upload) {
out << "client_version: " << upload.client_version() << "\n";
out << "form_signature: " << upload.form_signature() << "\n";
out << "data_present: " << upload.data_present() << "\n";
out << "submission: " << upload.submission() << "\n";
if (!upload.action_signature())
out << "action_signature: " << upload.action_signature() << "\n";
if (!upload.login_form_signature())
out << "login_form_signature: " << upload.login_form_signature() << "\n";
if (!upload.form_name().empty())
out << "form_name: " << upload.form_name() << "\n";
for (const auto& field : upload.field()) {
out << "\n Field"
<< "\n signature: " << field.signature()
<< "\n autofill_type: " << field.autofill_type();
if (!field.name().empty())
out << "\n name: " << field.name();
if (!field.autocomplete().empty())
out << "\n autocomplete: " << field.autocomplete();
if (!field.type().empty())
out << "\n type: " << field.type();
if (field.generation_type())
out << "\n generation_type: " << field.generation_type();
}
return out;
}
} // namespace
struct AutofillDownloadManager::FormRequestData {
std::vector<std::string> form_signatures;
RequestType request_type;
std::string payload;
};
AutofillDownloadManager::AutofillDownloadManager(AutofillDriver* driver,
Observer* observer)
: driver_(driver),
observer_(observer),
max_form_cache_size_(kMaxFormCacheSize),
fetcher_backoff_(&kAutofillBackoffPolicy),
fetcher_id_for_unittest_(0),
weak_factory_(this) {
DCHECK(observer_);
}
AutofillDownloadManager::~AutofillDownloadManager() = default;
bool AutofillDownloadManager::StartQueryRequest(
const std::vector<FormStructure*>& forms) {
// Do not send the request if it contains more fields than the server can
// accept.
if (CountActiveFieldsInForms(forms) > kMaxFieldsPerQueryRequest)
return false;
AutofillQueryContents query;
FormRequestData request_data;
if (!FormStructure::EncodeQueryRequest(forms, &request_data.form_signatures,
&query)) {
return false;
}
std::string payload;
if (!query.SerializeToString(&payload))
return false;
request_data.request_type = AutofillDownloadManager::REQUEST_QUERY;
request_data.payload = payload;
AutofillMetrics::LogServerQueryMetric(AutofillMetrics::QUERY_SENT);
std::string query_data;
if (CheckCacheForQueryRequest(request_data.form_signatures, &query_data)) {
VLOG(1) << "AutofillDownloadManager: query request has been retrieved "
<< "from the cache, form signatures: "
<< GetCombinedSignature(request_data.form_signatures);
observer_->OnLoadedServerPredictions(std::move(query_data),
request_data.form_signatures);
return true;
}
VLOG(1) << "Sending Autofill Query Request:\n" << query;
return StartRequest(request_data);
}
bool AutofillDownloadManager::StartUploadRequest(
const FormStructure& form,
bool form_was_autofilled,
const ServerFieldTypeSet& available_field_types,
const std::string& login_form_signature,
bool observed_submission) {
AutofillUploadContents upload;
if (!form.EncodeUploadRequest(available_field_types, form_was_autofilled,
login_form_signature, observed_submission,
&upload))
return false;
std::string payload;
if (!upload.SerializeToString(&payload))
return false;
if (form.upload_required() == UPLOAD_NOT_REQUIRED) {
VLOG(1) << "AutofillDownloadManager: Upload request is ignored.";
// If we ever need notification that upload was skipped, add it here.
return false;
}
FormRequestData request_data;
request_data.form_signatures.push_back(form.FormSignatureAsStr());
request_data.request_type = AutofillDownloadManager::REQUEST_UPLOAD;
request_data.payload = payload;
VLOG(1) << "Sending Autofill Upload Request:\n" << upload;
return StartRequest(request_data);
}
bool AutofillDownloadManager::StartRequest(
const FormRequestData& request_data) {
net::URLRequestContextGetter* request_context =
driver_->GetURLRequestContext();
DCHECK(request_context);
GURL request_url = GetRequestUrl(request_data.request_type);
// Id is ignored for regular chrome, in unit test id's for fake fetcher
// factory will be 0, 1, 2, ...
std::unique_ptr<net::URLFetcher> owned_fetcher = net::URLFetcher::Create(
fetcher_id_for_unittest_++, request_url, net::URLFetcher::POST, this);
net::URLFetcher* fetcher = owned_fetcher.get();
data_use_measurement::DataUseUserData::AttachToFetcher(
fetcher, data_use_measurement::DataUseUserData::AUTOFILL);
url_fetchers_[fetcher] =
std::make_pair(std::move(owned_fetcher), request_data);
fetcher->SetAutomaticallyRetryOn5xx(false);
fetcher->SetRequestContext(request_context);
fetcher->SetUploadData("text/proto", request_data.payload);
fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES |
net::LOAD_DO_NOT_SEND_COOKIES);
// Add Chrome experiment state to the request headers.
net::HttpRequestHeaders headers;
// Note: It's OK to pass |is_signed_in| false if it's unknown, as it does
// not affect transmission of experiments coming from the variations server.
bool is_signed_in = false;
variations::AppendVariationHeaders(fetcher->GetOriginalURL(),
driver_->IsOffTheRecord(), false,
is_signed_in, &headers);
fetcher->SetExtraRequestHeaders(headers.ToString());
fetcher->Start();
return true;
}
void AutofillDownloadManager::CacheQueryRequest(
const std::vector<std::string>& forms_in_query,
const std::string& query_data) {
std::string signature = GetCombinedSignature(forms_in_query);
for (auto it = cached_forms_.begin(); it != cached_forms_.end(); ++it) {
if (it->first == signature) {
// We hit the cache, move to the first position and return.
std::pair<std::string, std::string> data = *it;
cached_forms_.erase(it);
cached_forms_.push_front(data);
return;
}
}
std::pair<std::string, std::string> data;
data.first = signature;
data.second = query_data;
cached_forms_.push_front(data);
while (cached_forms_.size() > max_form_cache_size_)
cached_forms_.pop_back();
}
bool AutofillDownloadManager::CheckCacheForQueryRequest(
const std::vector<std::string>& forms_in_query,
std::string* query_data) const {
std::string signature = GetCombinedSignature(forms_in_query);
for (const auto& it : cached_forms_) {
if (it.first == signature) {
// We hit the cache, fill the data and return.
*query_data = it.second;
return true;
}
}
return false;
}
std::string AutofillDownloadManager::GetCombinedSignature(
const std::vector<std::string>& forms_in_query) const {
size_t total_size = forms_in_query.size();
for (size_t i = 0; i < forms_in_query.size(); ++i)
total_size += forms_in_query[i].length();
std::string signature;
signature.reserve(total_size);
for (size_t i = 0; i < forms_in_query.size(); ++i) {
if (i)
signature.append(",");
signature.append(forms_in_query[i]);
}
return signature;
}
void AutofillDownloadManager::OnURLFetchComplete(
const net::URLFetcher* source) {
auto it = url_fetchers_.find(const_cast<net::URLFetcher*>(source));
if (it == url_fetchers_.end()) {
// Looks like crash on Mac is possibly caused with callback entering here
// with unknown fetcher when network is refreshed.
return;
}
std::string request_type(RequestTypeToString(it->second.second.request_type));
CHECK(it->second.second.form_signatures.size());
bool success = source->GetResponseCode() == net::HTTP_OK;
fetcher_backoff_.InformOfRequest(success);
if (!success) {
// Reschedule with the appropriate delay, ignoring return value because
// payload is already well formed.
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::Bind(base::IgnoreResult(&AutofillDownloadManager::StartRequest),
weak_factory_.GetWeakPtr(), it->second.second),
fetcher_backoff_.GetTimeUntilRelease());
VLOG(1) << "AutofillDownloadManager: " << request_type
<< " request has failed with response "
<< source->GetResponseCode();
observer_->OnServerRequestError(it->second.second.form_signatures[0],
it->second.second.request_type,
source->GetResponseCode());
} else {
std::string response_body;
source->GetResponseAsString(&response_body);
if (it->second.second.request_type ==
AutofillDownloadManager::REQUEST_QUERY) {
CacheQueryRequest(it->second.second.form_signatures, response_body);
observer_->OnLoadedServerPredictions(std::move(response_body),
it->second.second.form_signatures);
} else {
VLOG(1) << "AutofillDownloadManager: upload request has succeeded.";
observer_->OnUploadedPossibleFieldTypes();
}
}
url_fetchers_.erase(it);
}
} // namespace autofill
|