Commit 25ce5ac5 authored by Vaclav Brozek's avatar Vaclav Brozek Committed by Commit Bot

Move FormParser to components

FormParser provides parsing PasswordForm structures from HTML data. It
currently is only used on iOS, and hence resides inside //ios. However,
in order to fuzz it, it has to be compiled on Linux and/or Mac.
Therefore the file itself is being moved to the component.

A side effect of this change is:
 * Dropping iOS PlatformTest fixture, because the test does not need it.
 (The fixture was responsible for draining the autorelease pool, but the
 test is pure C++, no Objective C).
 * Renaming to IOSFormParser, to ensure that iOS is still mentioned in
 the file path. It will also help to keep two different form parsers
 once the FormParser for desktop is added into the new target directory.

Bug: 827945
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: I114e01af40d5d29f221e15e44f0fdf635ea1c8b4
Reviewed-on: https://chromium-review.googlesource.com/989952Reviewed-by: default avatarVadym Doroshenko <dvadym@chromium.org>
Commit-Queue: Vaclav Brozek <vabr@chromium.org>
Cr-Commit-Position: refs/heads/master@{#547650}
parent d67c9b32
......@@ -412,6 +412,11 @@ source_set("unit_tests") {
"//ui/base",
"//url",
]
if (is_ios) {
deps +=
[ "//components/password_manager/core/browser/form_parsing:unit_tests" ]
}
}
fuzzer_test("csv_reader_fuzzer") {
......
# Copyright 2018 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.
if (is_ios) {
static_library("form_parsing") {
sources = [
"ios_form_parser.cc",
"ios_form_parser.h",
]
deps = [
"//base",
"//components/autofill/core/common",
]
}
source_set("unit_tests") {
testonly = true
sources = [
"ios_form_parser_unittest.cc",
]
deps = [
":form_parsing",
"//base",
"//components/autofill/core/common",
"//testing/gmock",
"//testing/gtest",
"//url",
]
}
} # if (is_ios)
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ios/chrome/browser/passwords/form_parser.h"
#include "components/password_manager/core/browser/form_parsing/ios_form_parser.h"
#include <algorithm>
#include <utility>
......@@ -18,6 +18,8 @@ using autofill::PasswordForm;
using FieldPointersVector = std::vector<const FormFieldData*>;
namespace password_manager {
namespace {
constexpr char kAutocompleteUsername[] = "username";
......@@ -305,10 +307,10 @@ void SetFields(const ParseResult& parse_result, PasswordForm* password_form) {
} // namespace
FormParser::FormParser() = default;
IOSFormParser::IOSFormParser() = default;
std::unique_ptr<PasswordForm> FormParser::Parse(const FormData& form_data,
FormParsingMode mode) {
std::unique_ptr<PasswordForm> IOSFormParser::Parse(const FormData& form_data,
FormParsingMode mode) {
FieldPointersVector fields = GetNonCreditCardFields(form_data.fields);
// Skip forms without password fields.
......@@ -338,3 +340,5 @@ std::unique_ptr<PasswordForm> FormParser::Parse(const FormData& form_data,
SetFields(*base_heuristics_parse_result, result.get());
return result;
}
} // namespace password_manager
......@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_CHROME_BROWSER_PASSWORDS_FORM_PARSER_H_
#define IOS_CHROME_BROWSER_PASSWORDS_FORM_PARSER_H_
#ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_FORM_PARSING_IOS_FORM_PARSER_H_
#define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_FORM_PARSING_IOS_FORM_PARSER_H_
#include <memory>
......@@ -12,11 +12,13 @@ struct FormData;
struct PasswordForm;
} // namespace autofill
namespace password_manager {
enum class FormParsingMode { FILLING, SAVING };
class FormParser {
class IOSFormParser {
public:
FormParser();
IOSFormParser();
// Parse DOM information |form_data| to Password Manager form representation
// PasswordForm.
......@@ -26,4 +28,6 @@ class FormParser {
FormParsingMode mode);
};
#endif // IOS_CHROME_BROWSER_PASSWORDS_FORM_PARSER_H_
} // namespace password_manager
#endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_FORM_PARSING_IOS_FORM_PARSER_H_
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ios/chrome/browser/passwords/form_parser.h"
#include "components/password_manager/core/browser/form_parsing/ios_form_parser.h"
#include "base/strings/string16.h"
#include "base/strings/string_number_conversions.h"
......@@ -12,7 +12,6 @@
#include "components/autofill/core/common/password_form.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
#include "url/gurl.h"
using autofill::FormData;
......@@ -21,6 +20,8 @@ using autofill::PasswordForm;
using base::ASCIIToUTF16;
using base::UintToString16;
namespace password_manager {
namespace {
constexpr int kFieldNotFound = -1;
......@@ -57,12 +58,12 @@ struct FormParsingTestCase {
ParseResultIndices save_result;
};
class FormParserTest : public PlatformTest {
class IOSFormParserTest : public testing::Test {
public:
FormParserTest() {}
IOSFormParserTest() {}
protected:
FormParser form_parser_;
IOSFormParser form_parser_;
void CheckTestData(const std::vector<FormParsingTestCase>& test_cases);
};
......@@ -133,7 +134,7 @@ void CheckPasswordFormFields(const PasswordForm& password_form,
password_form.confirmation_password_element, nullptr);
}
void FormParserTest::CheckTestData(
void IOSFormParserTest::CheckTestData(
const std::vector<FormParsingTestCase>& test_cases) {
for (const FormParsingTestCase& test_case : test_cases) {
const FormData form_data = GetFormData(test_case);
......@@ -165,7 +166,7 @@ void FormParserTest::CheckTestData(
}
}
TEST_F(FormParserTest, NotPasswordForm) {
TEST_F(IOSFormParserTest, NotPasswordForm) {
std::vector<FormParsingTestCase> test_data = {
{
"No fields",
......@@ -184,7 +185,7 @@ TEST_F(FormParserTest, NotPasswordForm) {
CheckTestData(test_data);
}
TEST_F(FormParserTest, OnlyPasswordFields) {
TEST_F(IOSFormParserTest, OnlyPasswordFields) {
std::vector<FormParsingTestCase> test_data = {
{
"1 password field",
......@@ -248,7 +249,7 @@ TEST_F(FormParserTest, OnlyPasswordFields) {
CheckTestData(test_data);
}
TEST_F(FormParserTest, TestFocusability) {
TEST_F(IOSFormParserTest, TestFocusability) {
std::vector<FormParsingTestCase> test_data = {
{
"non-focusable fields are considered when there are no focusable "
......@@ -294,7 +295,7 @@ TEST_F(FormParserTest, TestFocusability) {
CheckTestData(test_data);
}
TEST_F(FormParserTest, TextAndPasswordFields) {
TEST_F(IOSFormParserTest, TextAndPasswordFields) {
std::vector<FormParsingTestCase> test_data = {
{
"Simple empty sign-in form",
......@@ -358,7 +359,7 @@ TEST_F(FormParserTest, TextAndPasswordFields) {
CheckTestData(test_data);
}
TEST_F(FormParserTest, TestAutocomplete) {
TEST_F(IOSFormParserTest, TestAutocomplete) {
std::vector<FormParsingTestCase> test_data = {
{
"All possible password autocomplete attributes and some fields "
......@@ -426,7 +427,7 @@ TEST_F(FormParserTest, TestAutocomplete) {
CheckTestData(test_data);
}
TEST_F(FormParserTest, SkippingFieldsWithCreditCardFields) {
TEST_F(IOSFormParserTest, SkippingFieldsWithCreditCardFields) {
std::vector<FormParsingTestCase> test_data = {
{
"Simple form with all fields are credit card related",
......@@ -453,3 +454,5 @@ TEST_F(FormParserTest, SkippingFieldsWithCreditCardFields) {
}
} // namespace
} // namespace password_manager
......@@ -15,8 +15,6 @@ source_set("passwords") {
"credential_manager_features.h",
"credential_manager_util.h",
"credential_manager_util.mm",
"form_parser.cc",
"form_parser.h",
"ios_chrome_password_manager_client.h",
"ios_chrome_password_manager_client.mm",
"ios_chrome_password_manager_driver.h",
......@@ -61,6 +59,7 @@ source_set("passwords") {
"//components/keyed_service/core",
"//components/keyed_service/ios",
"//components/password_manager/core/browser",
"//components/password_manager/core/browser/form_parsing",
"//components/password_manager/core/common",
"//components/password_manager/sync/browser",
"//components/prefs",
......@@ -120,7 +119,6 @@ source_set("unit_tests") {
"account_select_fill_data_unittest.cc",
"credential_manager_unittest.mm",
"credential_manager_util_unittest.cc",
"form_parser_unittest.cc",
"js_credential_manager_unittest.mm",
"password_controller_js_unittest.mm",
"password_controller_unittest.mm",
......
......@@ -28,6 +28,7 @@
#include "components/autofill/ios/browser/autofill_util.h"
#include "components/browser_sync/profile_sync_service.h"
#include "components/infobars/core/infobar_manager.h"
#include "components/password_manager/core/browser/form_parsing/ios_form_parser.h"
#include "components/password_manager/core/browser/password_bubble_experiment.h"
#include "components/password_manager/core/browser/password_manager.h"
#include "components/password_manager/core/browser/password_manager_client.h"
......@@ -38,7 +39,6 @@
#include "ios/chrome/browser/passwords/account_select_fill_data.h"
#include "ios/chrome/browser/passwords/credential_manager.h"
#include "ios/chrome/browser/passwords/credential_manager_features.h"
#include "ios/chrome/browser/passwords/form_parser.h"
#import "ios/chrome/browser/passwords/ios_chrome_save_password_infobar_delegate.h"
#import "ios/chrome/browser/passwords/ios_chrome_update_password_infobar_delegate.h"
#import "ios/chrome/browser/passwords/js_password_manager.h"
......@@ -272,7 +272,7 @@ bool GetPageURLAndCheckTrustLevel(web::WebState* web_state, GURL* page_url) {
AccountSelectFillData fillData_;
FormParser formParser_;
password_manager::IOSFormParser formParser_;
// The WebState this instance is observing. Will be null after
// -webStateDestroyed: has been called.
......@@ -546,7 +546,7 @@ bool GetPageURLAndCheckTrustLevel(web::WebState* web_state, GURL* page_url) {
for (const auto& formData : formsData) {
std::unique_ptr<PasswordForm> form =
formParser_.Parse(formData, FormParsingMode::FILLING);
formParser_.Parse(formData, password_manager::FormParsingMode::FILLING);
if (form)
forms->push_back(*form);
}
......@@ -583,7 +583,7 @@ bool GetPageURLAndCheckTrustLevel(web::WebState* web_state, GURL* page_url) {
}
std::unique_ptr<PasswordForm> form =
formParser_.Parse(formData, FormParsingMode::SAVING);
formParser_.Parse(formData, password_manager::FormParsingMode::SAVING);
if (!form) {
completionHandler(NO, PasswordForm());
return;
......@@ -863,7 +863,7 @@ bool GetPageURLAndCheckTrustLevel(web::WebState* web_state, GURL* page_url) {
}
std::unique_ptr<PasswordForm> form =
formParser_.Parse(formData, FormParsingMode::SAVING);
formParser_.Parse(formData, password_manager::FormParsingMode::SAVING);
if (!form)
return NO;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment