File: form_classifier.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (265 lines) | stat: -rw-r--r-- 9,925 bytes parent folder | download
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
// Copyright 2016 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_classifier.h"

#include <algorithm>

#include "base/macros.h"
#include "base/strings/string16.h"
#include "base/strings/string_util.h"
#include "components/autofill/content/renderer/form_autofill_util.h"
#include "third_party/WebKit/public/platform/WebString.h"
#include "third_party/WebKit/public/platform/WebVector.h"
#include "third_party/WebKit/public/web/WebFormControlElement.h"
#include "third_party/WebKit/public/web/WebInputElement.h"

using autofill::form_util::WebFormControlElementToFormField;
using blink::WebFormControlElement;
using blink::WebInputElement;
using blink::WebString;
using blink::WebVector;

namespace autofill {

namespace {

// The words that frequently appear in attribute values of signin forms.
const char* const kSigninTextFeatures[] = {"signin", "login", "logon", "auth"};
constexpr size_t kNumberOfSigninFeatures = arraysize(kSigninTextFeatures);

// The words that frequently appear in attribute values of signup forms.
const char* const kSignupTextFeatures[] = {"signup", "regist", "creat"};
constexpr size_t kNumberOfSignupFeatures = arraysize(kSignupTextFeatures);

// The words that frequently appear in attribute values of captcha elements.
const char* const kCaptchaFeatures[] = {"captcha", "security", "code"};
constexpr size_t kNumberOfCaptchaFeatures = arraysize(kCaptchaFeatures);

// Minimal number of input fields to classify form as signup or change password
// form. If at least one of the listed thresholds is reached or exceeded, the
// form is classified as a form where password generation should be available.
constexpr size_t MINIMAL_NUMBER_OF_TEXT_FIELDS = 2;
constexpr size_t MINIMAL_NUMBER_OF_PASSWORD_FIELDS = 2;
constexpr size_t MINIMAL_NUMBER_OF_CHECKBOX_FIELDS = 3;
constexpr size_t MINIMAL_NUMBER_OF_OTHER_FIELDS = 2;

// Removes some characters from attribute value.
void ClearAttributeValue(std::string* value) {
  value->erase(std::remove_if(value->begin(), value->end(),
                              [](char x) { return x == '-' || x == '_'; }),
               value->end());
}

// Find |features| in |element|'s attribute values. Returns true if at least one
// text feature was found.
bool FindTextFeaturesForClass(const blink::WebElement& element,
                              const char* const features[],
                              size_t number_of_features) {
  DCHECK(features);

  for (unsigned i = 0; i < element.attributeCount(); ++i) {
    std::string filtered_value =
        base::ToLowerASCII(element.attributeValue(i).utf8());
    ClearAttributeValue(&filtered_value);

    if (filtered_value.empty())
      continue;
    for (size_t j = 0; j < number_of_features; ++j) {
      if (filtered_value.find(features[j]) != std::string::npos)
        return true;
    }
  }
  return false;
}

// Returns true if at least one captcha feature was found in |element|'s
// attribute values.
bool IsCaptchaInput(const blink::WebInputElement& element) {
  return FindTextFeaturesForClass(element, kCaptchaFeatures,
                                  kNumberOfCaptchaFeatures);
}

// Finds <img>'s inside |form| and checks if <img>'s attributes contains captcha
// text features. Returns true, if at least one occurrence was found.
bool FindCaptchaInImgElements(const blink::WebElement& form,
                              bool ingnore_invisible) {
  CR_DEFINE_STATIC_LOCAL(WebString, kImageTag, ("img"));

  blink::WebElementCollection img_elements =
      form.getElementsByHTMLTagName(kImageTag);
  for (blink::WebElement element = img_elements.firstItem(); !element.isNull();
       element = img_elements.nextItem()) {
    if (ingnore_invisible && !form_util::IsWebNodeVisible(element))
      continue;
    if (FindTextFeaturesForClass(element, kCaptchaFeatures,
                                 kNumberOfCaptchaFeatures)) {
      return true;
    }
  }
  return false;
}

// Finds signin and signup features in |element|'s attribute values. Sets to
// true |found_signin_text_features| or |found_signup_text_features| if
// appropriate features were found.
void FindTextFeaturesInElement(const blink::WebElement& element,
                               bool* found_signin_text_features,
                               bool* found_signup_text_features) {
  DCHECK(found_signin_text_features);
  DCHECK(found_signup_text_features);

  if (!*found_signin_text_features) {
    *found_signin_text_features = FindTextFeaturesForClass(
        element, kSigninTextFeatures, kNumberOfSigninFeatures);
  }
  if (!*found_signup_text_features) {
    *found_signup_text_features = FindTextFeaturesForClass(
        element, kSignupTextFeatures, kNumberOfSignupFeatures);
  }
}

// Returns true if |element| has type "button" or "image".
bool IsButtonOrImageElement(const WebFormControlElement& element) {
  CR_DEFINE_STATIC_LOCAL(WebString, kButton, ("button"));
  CR_DEFINE_STATIC_LOCAL(WebString, kImage, ("image"));

  return element.formControlType() == kButton ||
         element.formControlType() == kImage;
}

// Returns true if |element| has type "submit".
bool IsSubmitElement(const WebFormControlElement& element) {
  CR_DEFINE_STATIC_LOCAL(WebString, kSubmit, ("submit"));

  return element.formControlType() == kSubmit;
}

// Returns true if |element| has type "hidden";
bool IsHiddenElement(const WebFormControlElement& element) {
  CR_DEFINE_STATIC_LOCAL(WebString, kHidden, ("hidden"));

  return element.formControlType() == kHidden;
}

// Returns true if |element| has type "select-multiple" or "select-one".
bool IsSelectElement(const WebFormControlElement& element) {
  CR_DEFINE_STATIC_LOCAL(WebString, kSelectOne, ("select-one"));
  CR_DEFINE_STATIC_LOCAL(WebString, kSelectMultiple, ("select-multiple"));

  return element.formControlType() == kSelectOne ||
         element.formControlType() == kSelectMultiple;
}

// Return true if |form| contains at least one visible password element.
bool FormContainsVisiblePasswordFields(const blink::WebFormElement& form) {
  WebVector<WebFormControlElement> control_elements;
  form.getFormControlElements(control_elements);
  for (auto& control_element : control_elements) {
    const WebInputElement* input_element = toWebInputElement(&control_element);
    if (!input_element)
      continue;
    if (input_element->isPasswordField() &&
        form_util::IsWebNodeVisible(*input_element)) {
      return true;
    }
  }
  return false;
}

}  // namespace

bool ClassifyFormAndFindGenerationField(const blink::WebFormElement& form,
                                        base::string16* generation_field) {
  DCHECK(generation_field);

  if (form.isNull())
    return false;

  bool ignore_invisible_elements = FormContainsVisiblePasswordFields(form);

  bool found_signin_text_features = false;
  bool found_signup_text_features = false;
  size_t number_of_text_input_fields = 0;
  size_t number_of_password_input_fields = 0;
  size_t number_of_checkbox_input_fields = 0;
  size_t number_of_other_input_fields = 0;
  bool found_captcha =
      FindCaptchaInImgElements(form, ignore_invisible_elements);

  FindTextFeaturesInElement(form, &found_signin_text_features,
                            &found_signup_text_features);

  std::vector<WebInputElement> passwords;
  WebVector<WebFormControlElement> control_elements;
  form.getFormControlElements(control_elements);

  for (const WebFormControlElement& control_element : control_elements) {
    if (IsHiddenElement(control_element))
      continue;
    if (ignore_invisible_elements) {
      if (!form_util::IsWebNodeVisible(control_element))
        continue;
    }

    // If type="button" or "image", skip them, because it might be a link
    // to another form.
    if (IsButtonOrImageElement(control_element))
      continue;

    FindTextFeaturesInElement(control_element, &found_signin_text_features,
                              &found_signup_text_features);

    // Since <select> is not WebInputElement, but WebSelectElement, process
    // them as a special case.
    if (IsSelectElement(control_element)) {
      number_of_other_input_fields++;
    } else {
      const WebInputElement* input_element =
          toWebInputElement(&control_element);
      if (!input_element)
        continue;

      if (input_element->isTextField()) {
        if (input_element->isPasswordField()) {
          ++number_of_password_input_fields;
          passwords.push_back(*input_element);
        } else {
          ++number_of_text_input_fields;
          found_captcha = found_captcha || IsCaptchaInput(*input_element);
        }
      } else {  // Non-text fields.
        if (input_element->isCheckbox())
          ++number_of_checkbox_input_fields;
        else if (!IsSubmitElement(*input_element))
          ++number_of_other_input_fields;
      }
    }
  }

  if (number_of_password_input_fields == 0 ||
      number_of_password_input_fields > 3)
    return false;

  if ((number_of_text_input_fields - found_captcha >=
           MINIMAL_NUMBER_OF_TEXT_FIELDS ||
       number_of_password_input_fields >= MINIMAL_NUMBER_OF_PASSWORD_FIELDS ||
       number_of_checkbox_input_fields >= MINIMAL_NUMBER_OF_CHECKBOX_FIELDS ||
       number_of_other_input_fields >= MINIMAL_NUMBER_OF_OTHER_FIELDS) ||
      (found_signup_text_features && !found_signin_text_features)) {
    WebInputElement password_creation_field;

    // TODO(crbug.com/618309): Improve local classifier to distinguish password
    // creation and password usage fields on the change password forms.
    if (passwords.size() == 3)
      password_creation_field = passwords[1];
    else
      password_creation_field = passwords[0];

    *generation_field = password_creation_field.nameForAutofill();
    return true;
  }
  return false;
}
}