diff options
author | jhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-02-25 03:29:15 +0000 |
---|---|---|
committer | jhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-02-25 03:29:15 +0000 |
commit | f94e82788f40ca0b6e8ffbbc79948c097de5876f (patch) | |
tree | 92fe51ef8eaf8daa7c61ed13a839b3dc1d185fcc | |
parent | 3916c97e1bb1ef3fecfdcaf9d80f239016756c06 (diff) | |
download | chromium_src-f94e82788f40ca0b6e8ffbbc79948c097de5876f.zip chromium_src-f94e82788f40ca0b6e8ffbbc79948c097de5876f.tar.gz chromium_src-f94e82788f40ca0b6e8ffbbc79948c097de5876f.tar.bz2 |
Only load text fields in the form structure. This prevents us from trying to autofill username/password forms.
BUG=none
TEST=FormStructureTest
TEST=log in to gmail. AutoFill infobar should not show up.
Review URL: http://codereview.chromium.org/661026
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@39985 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/autofill/form_structure.cc | 27 | ||||
-rw-r--r-- | chrome/browser/autofill/form_structure_unittest.cc | 93 |
2 files changed, 108 insertions, 12 deletions
diff --git a/chrome/browser/autofill/form_structure.cc b/chrome/browser/autofill/form_structure.cc index 705c6a1..33fa742 100644 --- a/chrome/browser/autofill/form_structure.cc +++ b/chrome/browser/autofill/form_structure.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 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. @@ -16,10 +16,11 @@ #include "webkit/glue/form_field.h" #include "webkit/glue/form_field_values.h" -const char* kFormMethodGet = "get"; +namespace { + const char* kFormMethodPost = "post"; -// XML attribute names +// XML attribute names. const char* const kAttributeClientVersion = "clientversion"; const char* const kAttributeAutoFillUsed = "autofillused"; const char* const kAttributeSignature = "signature"; @@ -30,7 +31,11 @@ const char* const kXMLElementForm = "form"; const char* const kXMLElementField = "field"; const char* const kAttributeAutoFillType = "autofilltype"; -namespace { +// The only form control type we handle currently. +const char* const kControlTypeText = "text"; + +// The number of fillable fields necessary for a form to be fillable. +const size_t kRequiredFillableFields = 3; static std::string Hash64Bit(const std::string& str) { std::string hash_bin = base::SHA1HashString(str); @@ -58,6 +63,11 @@ FormStructure::FormStructure(const webkit_glue::FormFieldValues& values) std::vector<webkit_glue::FormField>::const_iterator field; for (field = values.elements.begin(); field != values.elements.end(); field++) { + // We currently only handle text fields. This prevents us from thinking we + // can autofill other types of controls, e.g., select, password, hidden. + if (!LowerCaseEqualsASCII(field->form_control_type(), kControlTypeText)) + continue; + // Generate a unique name for this field by appending a counter to the name. string16 unique_name = field->name() + IntToString16(fields_.size() + 1); fields_.push_back(new AutoFillField(*field, unique_name)); @@ -165,7 +175,7 @@ std::string FormStructure::FormSignature() const { } bool FormStructure::IsAutoFillable() const { - if (field_count() == 0) + if (field_count() < kRequiredFillableFields) return false; // Rule out http(s)://*/search?... @@ -174,13 +184,6 @@ bool FormStructure::IsAutoFillable() const { if (target_url_.path() == "/search") return false; - // Disqualify all forms that are likely to be search boxes (like google.com). - if (field_count() == 1) { - std::string name = UTF16ToUTF8(fields_[0]->name()); - if (name == "q") - return false; - } - if (method_ == GET) return false; diff --git a/chrome/browser/autofill/form_structure_unittest.cc b/chrome/browser/autofill/form_structure_unittest.cc new file mode 100644 index 0000000..7f1fd37 --- /dev/null +++ b/chrome/browser/autofill/form_structure_unittest.cc @@ -0,0 +1,93 @@ +// Copyright (c) 2010 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 "base/scoped_ptr.h" +#include "base/string_util.h" +#include "chrome/browser/autofill/form_structure.h" +#include "googleurl/src/gurl.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "third_party/WebKit/WebKit/chromium/public/WebInputElement.h" +#include "webkit/glue/form_field_values.h" + +using WebKit::WebInputElement; + +TEST(FormStructureTest, FieldCount) { + webkit_glue::FormFieldValues values; + values.method = ASCIIToUTF16("post"); + values.elements.push_back(webkit_glue::FormField(ASCIIToUTF16("username"), + ASCIIToUTF16("username"), + string16(), + ASCIIToUTF16("text"), + WebInputElement::Text)); + values.elements.push_back(webkit_glue::FormField(ASCIIToUTF16("password"), + ASCIIToUTF16("password"), + string16(), + ASCIIToUTF16("password"), + WebInputElement::Password)); + values.elements.push_back(webkit_glue::FormField(string16(), + ASCIIToUTF16("Submit"), + string16(), + ASCIIToUTF16("submit"), + WebInputElement::Submit)); + + FormStructure form_structure(values); + + // Only text fields are counted. + EXPECT_EQ(1U, form_structure.field_count()); +} + +TEST(FormStructureTest, IsAutoFillable) { + scoped_ptr<FormStructure> form_structure; + webkit_glue::FormFieldValues values; + + // We need at least three text fields to be auto-fillable. + values.method = ASCIIToUTF16("post"); + values.elements.push_back(webkit_glue::FormField(ASCIIToUTF16("username"), + ASCIIToUTF16("username"), + string16(), + ASCIIToUTF16("text"), + WebInputElement::Text)); + values.elements.push_back(webkit_glue::FormField(ASCIIToUTF16("password"), + ASCIIToUTF16("password"), + string16(), + ASCIIToUTF16("password"), + WebInputElement::Password)); + values.elements.push_back(webkit_glue::FormField(string16(), + ASCIIToUTF16("Submit"), + string16(), + ASCIIToUTF16("submit"), + WebInputElement::Submit)); + form_structure.reset(new FormStructure(values)); + EXPECT_FALSE(form_structure->IsAutoFillable()); + + // We now have three text fields. + values.elements.push_back(webkit_glue::FormField(ASCIIToUTF16("First Name"), + ASCIIToUTF16("firstname"), + string16(), + ASCIIToUTF16("text"), + WebInputElement::Text)); + values.elements.push_back(webkit_glue::FormField(ASCIIToUTF16("Last Name"), + ASCIIToUTF16("lastname"), + string16(), + ASCIIToUTF16("text"), + WebInputElement::Text)); + form_structure.reset(new FormStructure(values)); + EXPECT_TRUE(form_structure->IsAutoFillable()); + + // The method must be 'post'. + values.method = ASCIIToUTF16("get"); + form_structure.reset(new FormStructure(values)); + EXPECT_FALSE(form_structure->IsAutoFillable()); + + // The target cannot include http(s)://*/search... + values.method = ASCIIToUTF16("post"); + values.target_url = GURL("http://google.com/search?q=hello"); + form_structure.reset(new FormStructure(values)); + EXPECT_FALSE(form_structure->IsAutoFillable()); + + // But search can be in the URL. + values.target_url = GURL("http://search.com/?q=hello"); + form_structure.reset(new FormStructure(values)); + EXPECT_TRUE(form_structure->IsAutoFillable()); +} |