Commit a75bf740 authored by Mathieu Perreault's avatar Mathieu Perreault Committed by Commit Bot

[Autofill] Create FieldFiller helper class

Currently holding on to app_locale but will soon also
gain Address normalization tools.

This is functionality neutral.

Bug: 767931
Test: existing
Change-Id: I2dfbf93b4e6b8b891c0f78b4fcde615dedf37b1e
Reviewed-on: https://chromium-review.googlesource.com/775813
Commit-Queue: Mathieu Perreault <mathp@chromium.org>
Reviewed-by: default avatarSebastien Seguin-Gagnon <sebsg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#517521}
parent 9d7de5fe
...@@ -93,9 +93,9 @@ static_library("browser") { ...@@ -93,9 +93,9 @@ static_library("browser") {
"email_field.h", "email_field.h",
"field_candidates.cc", "field_candidates.cc",
"field_candidates.h", "field_candidates.h",
"field_filler.cc",
"field_filler.h",
"field_types.h", "field_types.h",
"fill_util.cc",
"fill_util.h",
"form_data_importer.cc", "form_data_importer.cc",
"form_data_importer.h", "form_data_importer.h",
"form_field.cc", "form_field.cc",
...@@ -386,7 +386,7 @@ source_set("unit_tests") { ...@@ -386,7 +386,7 @@ source_set("unit_tests") {
"credit_card_save_manager_unittest.cc", "credit_card_save_manager_unittest.cc",
"credit_card_unittest.cc", "credit_card_unittest.cc",
"field_candidates_unittest.cc", "field_candidates_unittest.cc",
"fill_util_unittest.cc", "field_filler_unittest.cc",
"form_data_importer_unittest.cc", "form_data_importer_unittest.cc",
"form_field_unittest.cc", "form_field_unittest.cc",
"form_structure_unittest.cc", "form_structure_unittest.cc",
......
...@@ -48,7 +48,6 @@ ...@@ -48,7 +48,6 @@
#include "components/autofill/core/browser/country_names.h" #include "components/autofill/core/browser/country_names.h"
#include "components/autofill/core/browser/credit_card.h" #include "components/autofill/core/browser/credit_card.h"
#include "components/autofill/core/browser/field_types.h" #include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/browser/fill_util.h"
#include "components/autofill/core/browser/form_structure.h" #include "components/autofill/core/browser/form_structure.h"
#include "components/autofill/core/browser/personal_data_manager.h" #include "components/autofill/core/browser/personal_data_manager.h"
#include "components/autofill/core/browser/phone_number.h" #include "components/autofill/core/browser/phone_number.h"
...@@ -210,6 +209,7 @@ AutofillManager::AutofillManager( ...@@ -210,6 +209,7 @@ AutofillManager::AutofillManager(
payments_client_.get(), payments_client_.get(),
client->GetPersonalDataManager(), client->GetPersonalDataManager(),
app_locale)), app_locale)),
field_filler_(app_locale),
autocomplete_history_manager_( autocomplete_history_manager_(
std::make_unique<AutocompleteHistoryManager>(driver, client)), std::make_unique<AutocompleteHistoryManager>(driver, client)),
form_interactions_ukm_logger_( form_interactions_ukm_logger_(
...@@ -1228,6 +1228,7 @@ AutofillManager::AutofillManager(AutofillDriver* driver, ...@@ -1228,6 +1228,7 @@ AutofillManager::AutofillManager(AutofillDriver* driver,
payments_client_.get(), payments_client_.get(),
personal_data, personal_data,
app_locale_)), app_locale_)),
field_filler_("en-US"),
autocomplete_history_manager_( autocomplete_history_manager_(
std::make_unique<AutocompleteHistoryManager>(driver, client)), std::make_unique<AutocompleteHistoryManager>(driver, client)),
form_interactions_ukm_logger_( form_interactions_ukm_logger_(
...@@ -1606,7 +1607,7 @@ std::vector<Suggestion> AutofillManager::GetProfileSuggestions( ...@@ -1606,7 +1607,7 @@ std::vector<Suggestion> AutofillManager::GetProfileSuggestions(
// Adjust phone number to display in prefix/suffix case. // Adjust phone number to display in prefix/suffix case.
if (autofill_field.Type().GetStorableType() == PHONE_HOME_NUMBER) { if (autofill_field.Type().GetStorableType() == PHONE_HOME_NUMBER) {
for (size_t i = 0; i < suggestions.size(); ++i) { for (size_t i = 0; i < suggestions.size(); ++i) {
suggestions[i].value = fill_util::GetPhoneNumberValue( suggestions[i].value = FieldFiller::GetPhoneNumberValue(
autofill_field, suggestions[i].value, field); autofill_field, suggestions[i].value, field);
} }
} }
...@@ -1979,8 +1980,8 @@ void AutofillManager::FillFieldWithValue( ...@@ -1979,8 +1980,8 @@ void AutofillManager::FillFieldWithValue(
const base::string16& profile_full_name, const base::string16& profile_full_name,
FormFieldData* field_data, FormFieldData* field_data,
bool should_notify) { bool should_notify) {
if (fill_util::FillFormField(*autofill_field, value, profile_language_code, if (field_filler_.FillFormField(*autofill_field, value, profile_language_code,
app_locale_, field_data)) { field_data)) {
// Mark the cached field as autofilled, so that we can detect when a // Mark the cached field as autofilled, so that we can detect when a
// user edits an autofilled field (for metrics). // user edits an autofilled field (for metrics).
autofill_field->is_autofilled = true; autofill_field->is_autofilled = true;
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include "components/autofill/core/browser/autofill_handler.h" #include "components/autofill/core/browser/autofill_handler.h"
#include "components/autofill/core/browser/autofill_metrics.h" #include "components/autofill/core/browser/autofill_metrics.h"
#include "components/autofill/core/browser/card_unmask_delegate.h" #include "components/autofill/core/browser/card_unmask_delegate.h"
#include "components/autofill/core/browser/field_filler.h"
#include "components/autofill/core/browser/form_data_importer.h" #include "components/autofill/core/browser/form_data_importer.h"
#include "components/autofill/core/browser/form_structure.h" #include "components/autofill/core/browser/form_structure.h"
#include "components/autofill/core/browser/form_types.h" #include "components/autofill/core/browser/form_types.h"
...@@ -471,6 +472,9 @@ class AutofillManager : public AutofillHandler, ...@@ -471,6 +472,9 @@ class AutofillManager : public AutofillHandler,
// Must be initialized (and thus listed) after payments_client_. // Must be initialized (and thus listed) after payments_client_.
std::unique_ptr<FormDataImporter> form_data_importer_; std::unique_ptr<FormDataImporter> form_data_importer_;
// Used to help fill data into fields.
FieldFiller field_filler_;
base::circular_deque<std::string> autofilled_form_signatures_; base::circular_deque<std::string> autofilled_form_signatures_;
// Handles queries and uploads to Autofill servers. Will be NULL if // Handles queries and uploads to Autofill servers. Will be NULL if
......
...@@ -17,8 +17,8 @@ ...@@ -17,8 +17,8 @@
#include "base/time/time.h" #include "base/time/time.h"
#include "components/autofill/core/browser/autofill_field.h" #include "components/autofill/core/browser/autofill_field.h"
#include "components/autofill/core/browser/autofill_scanner.h" #include "components/autofill/core/browser/autofill_scanner.h"
#include "components/autofill/core/browser/field_filler.h"
#include "components/autofill/core/browser/field_types.h" #include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/browser/fill_util.h"
#include "components/autofill/core/common/autofill_clock.h" #include "components/autofill/core/common/autofill_clock.h"
#include "components/autofill/core/common/autofill_regex_constants.h" #include "components/autofill/core/common/autofill_regex_constants.h"
#include "components/autofill/core/common/autofill_regexes.h" #include "components/autofill/core/common/autofill_regexes.h"
...@@ -314,10 +314,10 @@ bool CreditCardField::LikelyCardTypeSelectField(AutofillScanner* scanner) { ...@@ -314,10 +314,10 @@ bool CreditCardField::LikelyCardTypeSelectField(AutofillScanner* scanner) {
// We set |ignore_whitespace| to true on these calls because this is actually // We set |ignore_whitespace| to true on these calls because this is actually
// a pretty common mistake; e.g., "Master card" instead of "Mastercard". // a pretty common mistake; e.g., "Master card" instead of "Mastercard".
bool isSelect = (fill_util::FindShortestSubstringMatchInSelect( bool isSelect = (FieldFiller::FindShortestSubstringMatchInSelect(
l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_VISA), true, l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_VISA), true,
field) >= 0) || field) >= 0) ||
(fill_util::FindShortestSubstringMatchInSelect( (FieldFiller::FindShortestSubstringMatchInSelect(
l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_MASTERCARD), l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_MASTERCARD),
true, field) >= 0); true, field) >= 0);
return isSelect; return isSelect;
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "components/autofill/core/browser/fill_util.h" #include "components/autofill/core/browser/field_filler.h"
#include <stdint.h> #include <stdint.h>
...@@ -33,7 +33,6 @@ using base::ASCIIToUTF16; ...@@ -33,7 +33,6 @@ using base::ASCIIToUTF16;
using base::StringToInt; using base::StringToInt;
namespace autofill { namespace autofill {
namespace fill_util {
namespace { namespace {
...@@ -72,8 +71,8 @@ bool SetSelectControlValue(const base::string16& value, FormFieldData* field) { ...@@ -72,8 +71,8 @@ bool SetSelectControlValue(const base::string16& value, FormFieldData* field) {
bool SetSelectControlValueSubstringMatch(const base::string16& value, bool SetSelectControlValueSubstringMatch(const base::string16& value,
bool ignore_whitespace, bool ignore_whitespace,
FormFieldData* field) { FormFieldData* field) {
int best_match = int best_match = FieldFiller::FindShortestSubstringMatchInSelect(
FindShortestSubstringMatchInSelect(value, ignore_whitespace, field); value, ignore_whitespace, field);
if (best_match >= 0) { if (best_match >= 0) {
field->value = field->option_values[best_match]; field->value = field->option_values[best_match];
...@@ -309,7 +308,8 @@ bool FillCreditCardTypeSelectControl(const base::string16& value, ...@@ -309,7 +308,8 @@ bool FillCreditCardTypeSelectControl(const base::string16& value,
void FillPhoneNumberField(const AutofillField& field, void FillPhoneNumberField(const AutofillField& field,
const base::string16& number, const base::string16& number,
FormFieldData* field_data) { FormFieldData* field_data) {
field_data->value = GetPhoneNumberValue(field, number, *field_data); field_data->value =
FieldFiller::GetPhoneNumberValue(field, number, *field_data);
} }
// Set |field_data|'s value to |number|, or possibly an appropriate substring // Set |field_data|'s value to |number|, or possibly an appropriate substring
...@@ -530,11 +530,15 @@ base::string16 RemoveWhitespace(const base::string16& value) { ...@@ -530,11 +530,15 @@ base::string16 RemoveWhitespace(const base::string16& value) {
} // namespace } // namespace
bool FillFormField(const AutofillField& field, FieldFiller::FieldFiller(const std::string& app_locale)
const base::string16& value, : app_locale_(app_locale) {}
const std::string& address_language_code,
const std::string& app_locale, FieldFiller::~FieldFiller() {}
FormFieldData* field_data) {
bool FieldFiller::FillFormField(const AutofillField& field,
const base::string16& value,
const std::string& address_language_code,
FormFieldData* field_data) {
AutofillType type = field.Type(); AutofillType type = field.Type();
// Don't fill if autocomplete=off is set on |field| on desktop for non credit // Don't fill if autocomplete=off is set on |field| on desktop for non credit
...@@ -549,7 +553,7 @@ bool FillFormField(const AutofillField& field, ...@@ -549,7 +553,7 @@ bool FillFormField(const AutofillField& field,
FillPhoneNumberField(field, value, field_data); FillPhoneNumberField(field, value, field_data);
return true; return true;
} else if (field_data->form_control_type == "select-one") { } else if (field_data->form_control_type == "select-one") {
return FillSelectControl(type, value, app_locale, field_data); return FillSelectControl(type, value, app_locale_, field_data);
} else if (field_data->form_control_type == "month") { } else if (field_data->form_control_type == "month") {
return FillMonthControl(value, field_data); return FillMonthControl(value, field_data);
} else if (type.GetStorableType() == ADDRESS_HOME_STREET_ADDRESS) { } else if (type.GetStorableType() == ADDRESS_HOME_STREET_ADDRESS) {
...@@ -577,9 +581,11 @@ bool FillFormField(const AutofillField& field, ...@@ -577,9 +581,11 @@ bool FillFormField(const AutofillField& field,
// TODO(crbug.com/581514): Add support for filling only the prefix/suffix for // TODO(crbug.com/581514): Add support for filling only the prefix/suffix for
// phone numbers with 10 or 11 digits. // phone numbers with 10 or 11 digits.
base::string16 GetPhoneNumberValue(const AutofillField& field, // static
const base::string16& number, base::string16 FieldFiller::GetPhoneNumberValue(
const FormFieldData& field_data) { const AutofillField& field,
const base::string16& number,
const FormFieldData& field_data) {
// TODO(crbug.com/581485): Investigate the use of libphonenumber here. // TODO(crbug.com/581485): Investigate the use of libphonenumber here.
// Check to see if the |field| size matches the "prefix" or "suffix" size or // Check to see if the |field| size matches the "prefix" or "suffix" size or
// if // if
...@@ -615,9 +621,11 @@ base::string16 GetPhoneNumberValue(const AutofillField& field, ...@@ -615,9 +621,11 @@ base::string16 GetPhoneNumberValue(const AutofillField& field,
return number; return number;
} }
int FindShortestSubstringMatchInSelect(const base::string16& value, // static
bool ignore_whitespace, int FieldFiller::FindShortestSubstringMatchInSelect(
const FormFieldData* field) { const base::string16& value,
bool ignore_whitespace,
const FormFieldData* field) {
DCHECK_EQ(field->option_values.size(), field->option_contents.size()); DCHECK_EQ(field->option_values.size(), field->option_contents.size());
int best_match = -1; int best_match = -1;
...@@ -645,5 +653,4 @@ int FindShortestSubstringMatchInSelect(const base::string16& value, ...@@ -645,5 +653,4 @@ int FindShortestSubstringMatchInSelect(const base::string16& value,
return best_match; return best_match;
} }
} // namespace fill_util
} // namespace autofill } // namespace autofill
// Copyright 2017 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.
#ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_FIELD_FILLER_H_
#define COMPONENTS_AUTOFILL_CORE_BROWSER_FIELD_FILLER_H_
#include <string>
#include "base/macros.h"
#include "base/strings/string16.h"
#include "components/autofill/core/common/form_field_data.h"
namespace autofill {
class AutofillField;
// Helper class to put user content in fields, to eventually send to the
// renderer.
class FieldFiller {
public:
explicit FieldFiller(const std::string& app_locale);
~FieldFiller();
// Set |field_data|'s value to |value|. Uses |field|, |address_language_code|,
// and |app_locale_| as hints when filling exceptional cases like phone number
// values and <select> fields. Returns |true| if the field has been filled,
// |false| otherwise.
bool FillFormField(const AutofillField& field,
const base::string16& value,
const std::string& address_language_code,
FormFieldData* field_data);
// Returns the phone number value for the given |field|. The returned value
// might be |number|, or could possibly be a meaningful subset |number|, if
// that's appropriate for the field.
static base::string16 GetPhoneNumberValue(const AutofillField& field,
const base::string16& number,
const FormFieldData& field_data);
// Returns the index of the shortest entry in the given select field of which
// |value| is a substring. Returns -1 if no such entry exists.
static int FindShortestSubstringMatchInSelect(const base::string16& value,
bool ignore_whitespace,
const FormFieldData* field);
private:
const std::string app_locale_;
};
} // namespace autofill
#endif // COMPONENTS_AUTOFILL_CORE_BROWSER_FIELD_FILLER_H_
// Copyright 2017 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.
#ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_FILL_UTIL_H_
#define COMPONENTS_AUTOFILL_CORE_BROWSER_FILL_UTIL_H_
#include <stddef.h>
#include <string>
#include "base/macros.h"
#include "base/strings/string16.h"
#include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/common/form_field_data.h"
namespace autofill {
class AutofillField;
namespace fill_util {
// Set |field_data|'s value to |value|. Uses |field|, |address_language_code|,
// and |app_locale| as hints when filling exceptional cases like phone number
// values and <select> fields. Returns |true| if the field has been filled,
// |false| otherwise.
bool FillFormField(const AutofillField& field,
const base::string16& value,
const std::string& address_language_code,
const std::string& app_locale,
FormFieldData* field_data);
// Returns the phone number value for the given |field|. The returned value
// might be |number|, or could possibly be a meaningful subset |number|, if
// that's appropriate for the field.
base::string16 GetPhoneNumberValue(const AutofillField& field,
const base::string16& number,
const FormFieldData& field_data);
// Returns the index of the shortest entry in the given select field of which
// |value| is a substring. Returns -1 if no such entry exists.
int FindShortestSubstringMatchInSelect(const base::string16& value,
bool ignore_whitespace,
const FormFieldData* field);
} // namespace fill_util
} // namespace autofill
#endif // COMPONENTS_AUTOFILL_CORE_BROWSER_FILL_UTIL_H_
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