Skip to content

Commit

Permalink
[Autofill] Local heuristics as map
Browse files Browse the repository at this point in the history
This allows storing additional predictions, which we can use to
make hypothetical metrics.

Bug: 1310255
Change-Id: Ifde711db034680c53065f40bf1c83a8d70bc0387
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3553106
Reviewed-by: Christoph Schwering <schwering@google.com>
Commit-Queue: Christos Froussios <cfroussios@chromium.org>
Cr-Commit-Position: refs/heads/main@{#988420}
  • Loading branch information
Froussios authored and Chromium LUCI CQ committed Apr 4, 2022
1 parent 38c8e2a commit 5364f8f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 23 deletions.
51 changes: 31 additions & 20 deletions components/autofill/core/browser/autofill_field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,21 @@

namespace autofill {

AutofillField::AutofillField() = default;
AutofillField::AutofillField() {
local_type_predictions_.fill(NO_SERVER_DATA);
}

AutofillField::AutofillField(FieldSignature field_signature)
: field_signature_(field_signature) {}
AutofillField::AutofillField(FieldSignature field_signature) : AutofillField() {
field_signature_ = field_signature;
}

AutofillField::AutofillField(const FormFieldData& field)
: FormFieldData(field),
parseable_name_(field.name),
parseable_label_(field.label) {
field_signature_ =
CalculateFieldSignatureByNameAndType(name, form_control_type);
local_type_predictions_.fill(NO_SERVER_DATA);
}

AutofillField::~AutofillField() = default;
Expand All @@ -37,6 +41,13 @@ std::unique_ptr<AutofillField> AutofillField::CreateForPasswordManagerUpload(
return field;
}

ServerFieldType AutofillField::heuristic_type() const {
ServerFieldType type = get_prediction(PredictionSource::kDefaultHeuristics);
// `NO_SERVER_DATA` would mean that there is no heuristic type. Client code
// presumes there is a prediction, therefore we coalesce to `UNKNOWN_TYPE`.
return type > 0 ? type : UNKNOWN_TYPE;
}

ServerFieldType AutofillField::server_type() const {
return server_predictions_.empty()
? NO_SERVER_DATA
Expand All @@ -52,12 +63,12 @@ bool AutofillField::server_type_prediction_is_override() const {
void AutofillField::set_heuristic_type(ServerFieldType type) {
if (type >= 0 && type < MAX_VALID_FIELD_TYPE &&
type != FIELD_WITH_DEFAULT_VALUE) {
heuristic_type_ = type;
set_prediction(PredictionSource::kDefaultHeuristics, type);
} else {
NOTREACHED();
// This case should not be reachable; but since this has potential
// implications on data uploaded to the server, better safe than sorry.
heuristic_type_ = UNKNOWN_TYPE;
set_prediction(PredictionSource::kDefaultHeuristics, UNKNOWN_TYPE);
}
overall_type_ = AutofillType(NO_SERVER_DATA);
}
Expand Down Expand Up @@ -129,9 +140,9 @@ AutofillType AutofillField::ComputedType() const {
server_type() == CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR) {
return AutofillType(server_type());
}
if (heuristic_type_ == CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR ||
heuristic_type_ == CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR) {
return AutofillType(heuristic_type_);
if (heuristic_type() == CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR ||
heuristic_type() == CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR) {
return AutofillType(heuristic_type());
}
}

Expand Down Expand Up @@ -159,20 +170,20 @@ AutofillType AutofillField::ComputedType() const {
// it might be better to fix this server-side.
// See http://crbug.com/429236 for background.
bool believe_server = !(server_type() == NAME_FULL &&
heuristic_type_ == CREDIT_CARD_NAME_FULL) &&
heuristic_type() == CREDIT_CARD_NAME_FULL) &&
!(server_type() == CREDIT_CARD_NAME_FULL &&
heuristic_type_ == NAME_FULL) &&
heuristic_type() == NAME_FULL) &&
!(server_type() == NAME_FIRST &&
heuristic_type_ == CREDIT_CARD_NAME_FIRST) &&
heuristic_type() == CREDIT_CARD_NAME_FIRST) &&
!(server_type() == NAME_LAST &&
heuristic_type_ == CREDIT_CARD_NAME_LAST);
heuristic_type() == CREDIT_CARD_NAME_LAST);

// Either way, retain a preference for the the CVC heuristic over the
// server's password predictions (http://crbug.com/469007)
believe_server =
believe_server && !(AutofillType(server_type()).group() ==
FieldTypeGroup::kPasswordField &&
heuristic_type_ == CREDIT_CARD_VERIFICATION_CODE);
heuristic_type() == CREDIT_CARD_VERIFICATION_CODE);

// For new name tokens the heuristic predictions get precedence over the
// server predictions.
Expand All @@ -181,8 +192,8 @@ AutofillType AutofillField::ComputedType() const {
believe_server &&
!(base::FeatureList::IsEnabled(
features::kAutofillEnableSupportForMoreStructureInNames) &&
(heuristic_type_ == NAME_LAST_SECOND ||
heuristic_type_ == NAME_LAST_FIRST));
(heuristic_type() == NAME_LAST_SECOND ||
heuristic_type() == NAME_LAST_FIRST));

// For new address tokens the heuristic predictions get precedence over the
// server predictions.
Expand All @@ -191,17 +202,17 @@ AutofillType AutofillField::ComputedType() const {
believe_server &&
!(base::FeatureList::IsEnabled(
features::kAutofillEnableSupportForMoreStructureInAddresses) &&
(heuristic_type_ == ADDRESS_HOME_STREET_NAME ||
heuristic_type_ == ADDRESS_HOME_HOUSE_NUMBER));
(heuristic_type() == ADDRESS_HOME_STREET_NAME ||
heuristic_type() == ADDRESS_HOME_HOUSE_NUMBER));

believe_server =
believe_server && !(heuristic_type_ == MERCHANT_PROMO_CODE);
believe_server && !(heuristic_type() == MERCHANT_PROMO_CODE);

if (believe_server)
return AutofillType(server_type());
}

return AutofillType(heuristic_type_);
return AutofillType(heuristic_type());
}

AutofillType AutofillField::Type() const {
Expand Down Expand Up @@ -263,7 +274,7 @@ void AutofillField::NormalizePossibleTypesValidities() {

bool AutofillField::IsCreditCardPrediction() const {
return AutofillType(server_type()).group() == FieldTypeGroup::kCreditCard ||
AutofillType(heuristic_type_).group() == FieldTypeGroup::kCreditCard;
AutofillType(heuristic_type()).group() == FieldTypeGroup::kCreditCard;
}

} // namespace autofill
21 changes: 18 additions & 3 deletions components/autofill/core/browser/autofill_field.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class AutofillField : public FormFieldData {
PHONE_SUFFIX = 2,
};

enum class PredictionSource {
kDefaultHeuristics,
kMaxValue = kDefaultHeuristics
};

AutofillField();
explicit AutofillField(const FormFieldData& field);

Expand All @@ -51,8 +56,11 @@ class AutofillField : public FormFieldData {
static std::unique_ptr<AutofillField> CreateForPasswordManagerUpload(
FieldSignature field_signature);

ServerFieldType heuristic_type() const { return heuristic_type_; }
ServerFieldType heuristic_type() const;
ServerFieldType server_type() const;
ServerFieldType get_prediction(PredictionSource s) const {
return local_type_predictions_[static_cast<size_t>(s)];
}
bool server_type_prediction_is_override() const;
const std::vector<
AutofillQueryResponse::FormSuggestion::FieldSuggestion::FieldPrediction>&
Expand Down Expand Up @@ -81,6 +89,9 @@ class AutofillField : public FormFieldData {
void set_server_predictions(
std::vector<AutofillQueryResponse::FormSuggestion::FieldSuggestion::
FieldPrediction> predictions);
void set_prediction(PredictionSource s, ServerFieldType t) {
local_type_predictions_[static_cast<size_t>(s)] = t;
}

void set_may_use_prefilled_placeholder(bool may_use_prefilled_placeholder) {
may_use_prefilled_placeholder_ = may_use_prefilled_placeholder;
Expand Down Expand Up @@ -248,8 +259,12 @@ class AutofillField : public FormFieldData {
// Corresponds to the requirements determined by the Autofill server.
absl::optional<PasswordRequirementsSpec> password_requirements_;

// The type of the field, as determined by the local heuristics.
ServerFieldType heuristic_type_ = UNKNOWN_TYPE;
// Predictions which where calculated on the client. This is initialized to
// `NO_SERVER_DATA`, which means "NO_DATA", i.e. no classification was
// attempted.
std::array<ServerFieldType,
static_cast<size_t>(PredictionSource::kMaxValue) + 1>
local_type_predictions_;

// The type of the field. Overrides all other types (html_type_,
// heuristic_type_).
Expand Down

0 comments on commit 5364f8f

Please sign in to comment.