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 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
// Copyright 2013 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/content/renderer/form_cache.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/stl_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/content/renderer/form_autofill_util.h"
#include "components/autofill/core/common/autofill_constants.h"
#include "components/autofill/core/common/form_data_predictions.h"
#include "grit/components_strings.h"
#include "third_party/WebKit/public/platform/WebString.h"
#include "third_party/WebKit/public/platform/WebVector.h"
#include "third_party/WebKit/public/web/WebConsoleMessage.h"
#include "third_party/WebKit/public/web/WebDocument.h"
#include "third_party/WebKit/public/web/WebFormControlElement.h"
#include "third_party/WebKit/public/web/WebFormElement.h"
#include "third_party/WebKit/public/web/WebInputElement.h"
#include "third_party/WebKit/public/web/WebLocalFrame.h"
#include "third_party/WebKit/public/web/WebSelectElement.h"
#include "ui/base/l10n/l10n_util.h"
using blink::WebConsoleMessage;
using blink::WebDocument;
using blink::WebElement;
using blink::WebFormControlElement;
using blink::WebFormElement;
using blink::WebFrame;
using blink::WebInputElement;
using blink::WebNode;
using blink::WebSelectElement;
using blink::WebString;
using blink::WebVector;
namespace autofill {
namespace {
void LogDeprecationMessages(const WebFormControlElement& element) {
std::string autocomplete_attribute =
base::UTF16ToUTF8(base::StringPiece16(
element.getAttribute("autocomplete")));
static const char* const deprecated[] = { "region", "locality" };
for (const char* str : deprecated) {
if (autocomplete_attribute.find(str) == std::string::npos)
continue;
std::string msg = std::string("autocomplete='") + str +
"' is deprecated and will soon be ignored. See http://goo.gl/YjeSsW";
WebConsoleMessage console_message = WebConsoleMessage(
WebConsoleMessage::LevelWarning,
WebString(base::ASCIIToUTF16(msg)));
element.document().frame()->addMessageToConsole(console_message);
}
}
// Determines whether the form is interesting enough to send to the browser
// for further operations.
bool IsFormInteresting(const FormData& form, size_t num_editable_elements) {
if (form.fields.empty())
return false;
// If the form has at least one field with an autocomplete attribute, it is a
// candidate for autofill.
bool all_fields_are_passwords = true;
for (const FormFieldData& field : form.fields) {
if (!field.autocomplete_attribute.empty())
return true;
if (field.form_control_type != "password")
all_fields_are_passwords = false;
}
// If there are no autocomplete attributes, the form needs to have at least
// the required number of editable fields for the prediction routines to be a
// candidate for autofill.
return num_editable_elements >= kRequiredFieldsForPredictionRoutines ||
(all_fields_are_passwords &&
num_editable_elements >=
kRequiredFieldsForFormsWithOnlyPasswordFields);
}
} // namespace
FormCache::FormCache(const WebFrame& frame) : frame_(frame) {
}
FormCache::~FormCache() {
}
std::vector<FormData> FormCache::ExtractNewForms() {
std::vector<FormData> forms;
WebDocument document = frame_.document();
if (document.isNull())
return forms;
initial_checked_state_.clear();
initial_select_values_.clear();
WebVector<WebFormElement> web_forms;
document.forms(web_forms);
// Log an error message for deprecated attributes, but only the first time
// the form is parsed.
bool log_deprecation_messages = parsed_forms_.empty();
const form_util::ExtractMask extract_mask =
static_cast<form_util::ExtractMask>(form_util::EXTRACT_VALUE |
form_util::EXTRACT_OPTIONS);
size_t num_fields_seen = 0;
for (size_t i = 0; i < web_forms.size(); ++i) {
const WebFormElement& form_element = web_forms[i];
std::vector<WebFormControlElement> control_elements =
form_util::ExtractAutofillableElementsInForm(form_element);
size_t num_editable_elements =
ScanFormControlElements(control_elements, log_deprecation_messages);
if (num_editable_elements == 0)
continue;
FormData form;
if (!WebFormElementToFormData(form_element, WebFormControlElement(),
nullptr, extract_mask, &form, nullptr)) {
continue;
}
num_fields_seen += form.fields.size();
if (num_fields_seen > form_util::kMaxParseableFields)
return forms;
if (!base::ContainsKey(parsed_forms_, form) &&
IsFormInteresting(form, num_editable_elements)) {
for (auto it = parsed_forms_.begin(); it != parsed_forms_.end(); ++it) {
if (it->SameFormAs(form)) {
parsed_forms_.erase(it);
break;
}
}
SaveInitialValues(control_elements);
forms.push_back(form);
parsed_forms_.insert(form);
}
}
// Look for more parseable fields outside of forms.
std::vector<WebElement> fieldsets;
std::vector<WebFormControlElement> control_elements =
form_util::GetUnownedAutofillableFormFieldElements(document.all(),
&fieldsets);
size_t num_editable_elements =
ScanFormControlElements(control_elements, log_deprecation_messages);
if (num_editable_elements == 0)
return forms;
FormData synthetic_form;
if (!UnownedCheckoutFormElementsAndFieldSetsToFormData(
fieldsets, control_elements, nullptr, document, extract_mask,
&synthetic_form, nullptr)) {
return forms;
}
num_fields_seen += synthetic_form.fields.size();
if (num_fields_seen > form_util::kMaxParseableFields)
return forms;
if (!parsed_forms_.count(synthetic_form) &&
IsFormInteresting(synthetic_form, num_editable_elements)) {
SaveInitialValues(control_elements);
forms.push_back(synthetic_form);
parsed_forms_.insert(synthetic_form);
parsed_forms_.erase(synthetic_form_);
synthetic_form_ = synthetic_form;
}
return forms;
}
void FormCache::Reset() {
synthetic_form_ = FormData();
parsed_forms_.clear();
initial_select_values_.clear();
initial_checked_state_.clear();
}
bool FormCache::ClearFormWithElement(const WebFormControlElement& element) {
WebFormElement form_element = element.form();
std::vector<WebFormControlElement> control_elements;
if (form_element.isNull()) {
control_elements = form_util::GetUnownedAutofillableFormFieldElements(
element.document().all(), nullptr);
} else {
control_elements =
form_util::ExtractAutofillableElementsInForm(form_element);
}
for (size_t i = 0; i < control_elements.size(); ++i) {
WebFormControlElement control_element = control_elements[i];
// Don't modify the value of disabled fields.
if (!control_element.isEnabled())
continue;
// Don't clear field that was not autofilled
if (!control_element.isAutofilled())
continue;
control_element.setAutofilled(false);
WebInputElement* input_element = toWebInputElement(&control_element);
if (form_util::IsTextInput(input_element) ||
form_util::IsMonthInput(input_element)) {
input_element->setValue(base::string16(), true);
// Clearing the value in the focused node (above) can cause selection
// to be lost. We force selection range to restore the text cursor.
if (element == *input_element) {
int length = input_element->value().length();
input_element->setSelectionRange(length, length);
}
} else if (form_util::IsTextAreaElement(control_element)) {
control_element.setValue(base::string16(), true);
} else if (form_util::IsSelectElement(control_element)) {
WebSelectElement select_element = control_element.to<WebSelectElement>();
std::map<const WebSelectElement, base::string16>::const_iterator
initial_value_iter = initial_select_values_.find(select_element);
if (initial_value_iter != initial_select_values_.end() &&
select_element.value() != initial_value_iter->second) {
select_element.setValue(initial_value_iter->second, true);
}
} else {
WebInputElement input_element = control_element.to<WebInputElement>();
DCHECK(form_util::IsCheckableElement(&input_element));
std::map<const WebInputElement, bool>::const_iterator it =
initial_checked_state_.find(input_element);
if (it != initial_checked_state_.end() &&
input_element.isChecked() != it->second) {
input_element.setChecked(it->second, true);
}
}
}
return true;
}
bool FormCache::ShowPredictions(const FormDataPredictions& form) {
DCHECK_EQ(form.data.fields.size(), form.fields.size());
std::vector<WebFormControlElement> control_elements;
// First check the synthetic form.
bool found_synthetic_form = false;
if (form.data.SameFormAs(synthetic_form_)) {
found_synthetic_form = true;
WebDocument document = frame_.document();
control_elements = form_util::GetUnownedAutofillableFormFieldElements(
document.all(), nullptr);
}
if (!found_synthetic_form) {
// Find the real form by searching through the WebDocuments.
bool found_form = false;
WebFormElement form_element;
WebVector<WebFormElement> web_forms;
frame_.document().forms(web_forms);
for (size_t i = 0; i < web_forms.size(); ++i) {
form_element = web_forms[i];
// Note: matching on the form name here which is not guaranteed to be
// unique for the page, nor is it guaranteed to be non-empty. Ideally,
// we would have a way to uniquely identify the form cross-process. For
// now, we'll check form name and form action for identity.
// Also note that WebString() == WebString(string16()) does not evaluate
// to |true| -- WebKit distinguishes between a "null" string (lhs) and
// an "empty" string (rhs). We don't want that distinction, so forcing
// to string16.
base::string16 element_name = form_util::GetFormIdentifier(form_element);
GURL action(form_element.document().completeURL(form_element.action()));
if (element_name == form.data.name && action == form.data.action) {
found_form = true;
control_elements =
form_util::ExtractAutofillableElementsInForm(form_element);
break;
}
}
if (!found_form)
return false;
}
if (control_elements.size() != form.fields.size()) {
// Keep things simple. Don't show predictions for forms that were modified
// between page load and the server's response to our query.
return false;
}
for (size_t i = 0; i < control_elements.size(); ++i) {
WebFormControlElement& element = control_elements[i];
const FormFieldData& field_data = form.data.fields[i];
if (base::string16(element.nameForAutofill()) != field_data.name) {
// Keep things simple. Don't show predictions for elements whose names
// were modified between page load and the server's response to our query.
continue;
}
static const size_t kMaxLabelSize = 100;
const base::string16 truncated_label = field_data.label.substr(
0, std::min(field_data.label.length(), kMaxLabelSize));
const FormFieldDataPredictions& field = form.fields[i];
std::vector<base::string16> replacements;
base::string16 overall_type = base::UTF8ToUTF16(field.overall_type);
replacements.push_back(overall_type);
replacements.push_back(base::UTF8ToUTF16(field.server_type));
replacements.push_back(base::UTF8ToUTF16(field.heuristic_type));
replacements.push_back(truncated_label);
replacements.push_back(base::UTF8ToUTF16(field.parseable_name));
replacements.push_back(base::UTF8ToUTF16(field.signature));
replacements.push_back(base::UTF8ToUTF16(form.signature));
const base::string16 title = l10n_util::GetStringFUTF16(
IDS_AUTOFILL_SHOW_PREDICTIONS_TITLE, replacements, nullptr);
element.setAttribute("title", WebString(title));
element.setAttribute("autofill-prediction", WebString(overall_type));
}
return true;
}
size_t FormCache::ScanFormControlElements(
const std::vector<WebFormControlElement>& control_elements,
bool log_deprecation_messages) {
size_t num_editable_elements = 0;
for (size_t i = 0; i < control_elements.size(); ++i) {
const WebFormControlElement& element = control_elements[i];
if (log_deprecation_messages)
LogDeprecationMessages(element);
// Save original values of <select> elements so we can restore them
// when |ClearFormWithNode()| is invoked.
if (form_util::IsSelectElement(element) ||
form_util::IsTextAreaElement(element)) {
++num_editable_elements;
} else {
const WebInputElement input_element = element.toConst<WebInputElement>();
if (!form_util::IsCheckableElement(&input_element))
++num_editable_elements;
}
}
return num_editable_elements;
}
void FormCache::SaveInitialValues(
const std::vector<WebFormControlElement>& control_elements) {
for (const WebFormControlElement& element : control_elements) {
if (form_util::IsSelectElement(element)) {
const WebSelectElement select_element =
element.toConst<WebSelectElement>();
initial_select_values_.insert(
std::make_pair(select_element, select_element.value()));
} else {
const WebInputElement* input_element = toWebInputElement(&element);
if (form_util::IsCheckableElement(input_element)) {
initial_checked_state_.insert(
std::make_pair(*input_element, input_element->isChecked()));
}
}
}
}
} // namespace autofill
|