Skip to content

Commit

Permalink
[Autofill Assistant] Add client memory key to ValueExpression
Browse files Browse the repository at this point in the history
This adds the option to use client memory in |ValueExpression|.
If the key is not found, it will return a new status:
|CLIENT_MEMORY_KEY_NOT_AVAILABLE|.

If the result of the resolved |ValueExpression| is empty, the
helper now returns an |EMPTY_VALUE_EXPRESSION_RESULT| error.

Bug: b/193758290
Change-Id: I16a08e1afb8acd958fa53967df5d4a3c45c61e77
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3041347
Commit-Queue: Sandro Maggi <sandromaggi@google.com>
Reviewed-by: Clemens Arbesser <arbesser@google.com>
Reviewed-by: Stephane Zermatten <szermatt@chromium.org>
Cr-Commit-Position: refs/heads/master@{#906679}
  • Loading branch information
sandromaggi authored and Chromium LUCI CQ committed Jul 29, 2021
1 parent b83a7db commit b343372
Show file tree
Hide file tree
Showing 17 changed files with 635 additions and 391 deletions.
3 changes: 3 additions & 0 deletions components/autofill_assistant/browser/action_value.proto
Expand Up @@ -30,6 +30,9 @@ message ValueExpression {
int32 key = 1;
// A plain text.
string text = 2;
// A client memory key. This looks up an entry in the previously stored
// values on the client.
string memory_key = 4;
}

// If the chunk fully matches the key, it will be replaced. When used
Expand Down
Expand Up @@ -71,11 +71,12 @@ ClientStatus& ErrorStatusWithDefault(ClientStatus& status) {
return status;
}

ClientStatus GetRe2Value(const RequiredField& required_field,
const std::map<std::string, std::string>& mappings,
bool use_contains,
std::string* re2_value,
bool* case_sensitive) {
ClientStatus GetRe2Value(
const RequiredField& required_field,
const std::map<field_formatter::Key, std::string>& mappings,
bool use_contains,
std::string* re2_value,
bool* case_sensitive) {
if (required_field.proto.has_option_comparison_value_expression_re2()) {
ClientStatus status = field_formatter::FormatExpression(
required_field.proto.option_comparison_value_expression_re2()
Expand Down Expand Up @@ -124,7 +125,7 @@ RequiredFieldsFallbackHandler::~RequiredFieldsFallbackHandler() = default;

RequiredFieldsFallbackHandler::RequiredFieldsFallbackHandler(
const std::vector<RequiredField>& required_fields,
const std::map<std::string, std::string>& fallback_values,
const std::map<field_formatter::Key, std::string>& fallback_values,
ActionDelegate* delegate)
: required_fields_(required_fields),
fallback_values_(fallback_values),
Expand Down
Expand Up @@ -17,6 +17,7 @@
#include "components/autofill_assistant/browser/actions/action.h"
#include "components/autofill_assistant/browser/actions/fallback_handler/required_field.h"
#include "components/autofill_assistant/browser/batch_element_checker.h"
#include "components/autofill_assistant/browser/field_formatter.h"
#include "components/autofill_assistant/browser/web/element_finder.h"

namespace autofill_assistant {
Expand All @@ -28,7 +29,7 @@ class RequiredFieldsFallbackHandler {
public:
explicit RequiredFieldsFallbackHandler(
const std::vector<RequiredField>& required_fields,
const std::map<std::string, std::string>& fallback_values,
const std::map<field_formatter::Key, std::string>& fallback_values,
ActionDelegate* delegate);

~RequiredFieldsFallbackHandler();
Expand Down Expand Up @@ -110,7 +111,7 @@ class RequiredFieldsFallbackHandler {
ClientStatus client_status_;

std::vector<RequiredField> required_fields_;
std::map<std::string, std::string> fallback_values_;
std::map<field_formatter::Key, std::string> fallback_values_;
base::OnceCallback<void(const ClientStatus&)> status_update_callback_;
ActionDelegate* action_delegate_;
std::unique_ptr<BatchElementChecker> batch_element_checker_;
Expand Down
Expand Up @@ -7,7 +7,6 @@
#include "base/bind.h"
#include "base/callback.h"
#include "base/guid.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/mock_callback.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
Expand Down Expand Up @@ -163,11 +162,11 @@ TEST_F(RequiredFieldsFallbackHandlerTest,
CreateRequiredField(52, {"#card_number"}),
CreateRequiredField(-3, {"#card_network"})};

std::map<std::string, std::string> fallback_values = {
{base::NumberToString(
std::map<field_formatter::Key, std::string> fallback_values = {
{field_formatter::Key(
static_cast<int>(autofill::ServerFieldType::CREDIT_CARD_NAME_FULL)),
"John Doe"},
{base::NumberToString(
{field_formatter::Key(
static_cast<int>(AutofillFormatProto::CREDIT_CARD_NETWORK)),
std::string()}};

Expand Down Expand Up @@ -214,11 +213,11 @@ TEST_F(RequiredFieldsFallbackHandlerTest, AddsFirstFieldFillingError) {
CreateRequiredField(51, {"#card_name"}),
CreateRequiredField(52, {"#card_number"})};

std::map<std::string, std::string> fallback_values = {
{base::NumberToString(
std::map<field_formatter::Key, std::string> fallback_values = {
{field_formatter::Key(
static_cast<int>(autofill::ServerFieldType::CREDIT_CARD_NAME_FULL)),
"John Doe"},
{base::NumberToString(
{field_formatter::Key(
static_cast<int>(autofill::ServerFieldType::CREDIT_CARD_NUMBER)),
"4111111111111111"}};

Expand Down Expand Up @@ -256,11 +255,11 @@ TEST_F(RequiredFieldsFallbackHandlerTest,
CreateRequiredField(51, {"#card_name"}),
CreateRequiredField(52, {"#card_number"})};

std::map<std::string, std::string> fallback_values = {
{base::NumberToString(
std::map<field_formatter::Key, std::string> fallback_values = {
{field_formatter::Key(
static_cast<int>(autofill::ServerFieldType::CREDIT_CARD_NAME_FULL)),
"John Doe"},
{base::NumberToString(
{field_formatter::Key(
static_cast<int>(autofill::ServerFieldType::CREDIT_CARD_NUMBER)),
"4111111111111111"}};

Expand Down Expand Up @@ -323,8 +322,8 @@ TEST_F(RequiredFieldsFallbackHandlerTest, FillsEmptyRequiredField) {
std::vector<RequiredField> required_fields = {
CreateRequiredField(51, {"#card_name"})};

std::map<std::string, std::string> fallback_values = {
{base::NumberToString(
std::map<field_formatter::Key, std::string> fallback_values = {
{field_formatter::Key(
static_cast<int>(autofill::ServerFieldType::CREDIT_CARD_NAME_FULL)),
"John Doe"}};

Expand All @@ -351,8 +350,8 @@ TEST_F(RequiredFieldsFallbackHandlerTest, FallsBackForForcedFilledField) {
CreateRequiredField(51, {"#card_name"})};
required_fields[0].proto.set_forced(true);

std::map<std::string, std::string> fallback_values = {
{base::NumberToString(
std::map<field_formatter::Key, std::string> fallback_values = {
{field_formatter::Key(
static_cast<int>(autofill::ServerFieldType::CREDIT_CARD_NAME_FULL)),
"John Doe"}};

Expand Down Expand Up @@ -420,12 +419,11 @@ TEST_F(RequiredFieldsFallbackHandlerTest, FillsFieldWithPattern) {
std::vector<RequiredField> required_fields = {
CreateRequiredField(value_expression, {"#card_expiry"})};

std::map<std::string, std::string> fallback_values = {
{base::NumberToString(
static_cast<int>(autofill::ServerFieldType::CREDIT_CARD_EXP_MONTH)),
std::map<field_formatter::Key, std::string> fallback_values = {
{field_formatter::Key(autofill::ServerFieldType::CREDIT_CARD_EXP_MONTH),
"08"},
{base::NumberToString(static_cast<int>(
autofill::ServerFieldType::CREDIT_CARD_EXP_4_DIGIT_YEAR)),
{field_formatter::Key(
autofill::ServerFieldType::CREDIT_CARD_EXP_4_DIGIT_YEAR),
"2050"}};

RequiredFieldsFallbackHandler fallback_handler(
Expand All @@ -447,10 +445,9 @@ TEST_F(RequiredFieldsFallbackHandlerTest,
CreateRequiredField(53, {"#card_expiry"}),
CreateRequiredField(-3, {"#card_network"})};

std::map<std::string, std::string> fallback_values;
fallback_values.emplace(base::NumberToString(static_cast<int>(
AutofillFormatProto::CREDIT_CARD_NETWORK)),
"");
std::map<field_formatter::Key, std::string> fallback_values;
fallback_values.emplace(
static_cast<int>(AutofillFormatProto::CREDIT_CARD_NETWORK), "");

RequiredFieldsFallbackHandler fallback_handler(
required_fields, fallback_values, &mock_action_delegate_);
Expand Down Expand Up @@ -521,9 +518,9 @@ TEST_F(RequiredFieldsFallbackHandlerTest, UsesSelectOptionForDropdowns) {
required_fields[0].proto.set_select_strategy(
DropdownSelectStrategy::VALUE_MATCH);

std::map<std::string, std::string> fallback_values = {
{base::NumberToString(static_cast<int>(
autofill::ServerFieldType::CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR)),
std::map<field_formatter::Key, std::string> fallback_values = {
{field_formatter::Key(
autofill::ServerFieldType::CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR),
"05/2050"}};

RequiredFieldsFallbackHandler fallback_handler(
Expand Down Expand Up @@ -577,9 +574,9 @@ TEST_F(RequiredFieldsFallbackHandlerTest,
value_expression_re2;
std::vector<RequiredField> required_fields = {required_field};

std::map<std::string, std::string> fallback_values = {
{base::NumberToString(static_cast<int>(
autofill::ServerFieldType::CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR)),
std::map<field_formatter::Key, std::string> fallback_values = {
{field_formatter::Key(
autofill::ServerFieldType::CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR),
"05/2050"}};

RequiredFieldsFallbackHandler fallback_handler(
Expand Down Expand Up @@ -621,9 +618,9 @@ TEST_F(RequiredFieldsFallbackHandlerTest, ClicksOnCustomDropdown) {
*required_fields[0].proto.mutable_option_element_to_click() =
ToSelectorProto(".option");

std::map<std::string, std::string> fallback_values = {
{base::NumberToString(static_cast<int>(
autofill::ServerFieldType::CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR)),
std::map<field_formatter::Key, std::string> fallback_values = {
{field_formatter::Key(
autofill::ServerFieldType::CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR),
"05/2050"}};

RequiredFieldsFallbackHandler fallback_handler(
Expand Down Expand Up @@ -665,9 +662,9 @@ TEST_F(RequiredFieldsFallbackHandlerTest, CustomDropdownClicksStopOnError) {
*required_fields[0].proto.mutable_option_element_to_click() =
ToSelectorProto(".option");

std::map<std::string, std::string> fallback_values = {
{base::NumberToString(static_cast<int>(
autofill::ServerFieldType::CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR)),
std::map<field_formatter::Key, std::string> fallback_values = {
{field_formatter::Key(
autofill::ServerFieldType::CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR),
"05/2050"}};

RequiredFieldsFallbackHandler fallback_handler(
Expand Down Expand Up @@ -707,7 +704,7 @@ TEST_F(RequiredFieldsFallbackHandlerTest, ClearsFilledField) {

std::vector<RequiredField> required_fields = {
CreateRequiredField(ValueExpression(), {"#field"})};
std::map<std::string, std::string> fallback_values;
std::map<field_formatter::Key, std::string> fallback_values;

RequiredFieldsFallbackHandler fallback_handler(
required_fields, fallback_values, &mock_action_delegate_);
Expand Down Expand Up @@ -747,7 +744,7 @@ TEST_F(RequiredFieldsFallbackHandlerTest, SkipsForcedFieldCheckOnFirstRun) {
forced_field.proto.set_forced(true);
std::vector<RequiredField> required_fields = {forced_field};

std::map<std::string, std::string> fallback_values;
std::map<field_formatter::Key, std::string> fallback_values;

RequiredFieldsFallbackHandler fallback_handler(
required_fields, fallback_values, &mock_action_delegate_);
Expand Down Expand Up @@ -784,8 +781,8 @@ TEST_F(RequiredFieldsFallbackHandlerTest,
CreateRequiredField(51, {"#card_name"}),
CreateRequiredField(52, {"#card_number"})};

std::map<std::string, std::string> fallback_values = {
{base::NumberToString(
std::map<field_formatter::Key, std::string> fallback_values = {
{field_formatter::Key(
static_cast<int>(autofill::ServerFieldType::CREDIT_CARD_NAME_FULL)),
"John Doe"}};

Expand Down Expand Up @@ -834,8 +831,8 @@ TEST_F(RequiredFieldsFallbackHandlerTest,
CreateRequiredField(52, {"#card_number"})};
required_fields[0].proto.set_is_optional(true);

std::map<std::string, std::string> fallback_values = {
{base::NumberToString(
std::map<field_formatter::Key, std::string> fallback_values = {
{field_formatter::Key(
static_cast<int>(autofill::ServerFieldType::CREDIT_CARD_NUMBER)),
"4111111111111111"}};

Expand Down Expand Up @@ -893,11 +890,11 @@ TEST_F(RequiredFieldsFallbackHandlerTest,
CreateRequiredField(52, {"#card_number"})};
required_fields[0].proto.set_is_optional(true);

std::map<std::string, std::string> fallback_values = {
{base::NumberToString(
std::map<field_formatter::Key, std::string> fallback_values = {
{field_formatter::Key(
static_cast<int>(autofill::ServerFieldType::CREDIT_CARD_NAME_FULL)),
"John Doe"},
{base::NumberToString(
{field_formatter::Key(
static_cast<int>(autofill::ServerFieldType::CREDIT_CARD_NUMBER)),
"4111111111111111"}};

Expand Down
Expand Up @@ -54,7 +54,7 @@ void SelectOptionAction::InternalProcessAction(ProcessActionCallback callback) {
case_sensitive_ = select_option.text_filter_value().case_sensitive();
break;
case SelectOptionProto::kAutofillRegexpValue: {
ClientStatus autofill_status = user_data::GetFormattedAutofillValue(
ClientStatus autofill_status = user_data::GetFormattedClientValue(
select_option.autofill_regexp_value(), delegate_->GetUserData(),
&value_);
if (!autofill_status.ok()) {
Expand Down
Expand Up @@ -7,8 +7,6 @@
#include <utility>

#include "base/guid.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/mock_callback.h"
Expand Down
Expand Up @@ -10,7 +10,6 @@

#include "base/bind.h"
#include "base/callback.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "components/autofill/core/browser/autofill_data_util.h"
Expand Down Expand Up @@ -192,22 +191,20 @@ void UseCreditCardAction::InitFallbackHandler(const autofill::CreditCard& card,
required_fields.emplace_back(required_field);
}

std::map<std::string, std::string> fallback_values =
auto fallback_values =
field_formatter::CreateAutofillMappings(card,
/* locale= */ "en-US");

if (is_resolved) {
fallback_values.emplace(
base::NumberToString(static_cast<int>(
AutofillFormatProto::CREDIT_CARD_VERIFICATION_CODE)),
field_formatter::Key(
AutofillFormatProto::CREDIT_CARD_VERIFICATION_CODE),
base::UTF16ToUTF8(cvc));
fallback_values.emplace(
base::NumberToString(
static_cast<int>(AutofillFormatProto::CREDIT_CARD_RAW_NUMBER)),
field_formatter::Key(AutofillFormatProto::CREDIT_CARD_RAW_NUMBER),
base::UTF16ToUTF8(card.GetRawInfo(autofill::CREDIT_CARD_NUMBER)));
} else {
fallback_values.erase(
base::NumberToString(static_cast<int>(autofill::CREDIT_CARD_NUMBER)));
fallback_values.erase(field_formatter::Key(autofill::CREDIT_CARD_NUMBER));
}

DCHECK(fallback_handler_ == nullptr);
Expand Down
Expand Up @@ -7,8 +7,6 @@
#include <utility>

#include "base/guid.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/mock_callback.h"
Expand Down
6 changes: 6 additions & 0 deletions components/autofill_assistant/browser/client_status.cc
Expand Up @@ -151,6 +151,12 @@ std::ostream& operator<<(std::ostream& out,
case ProcessedActionStatusProto::ELEMENT_POSITION_NOT_FOUND:
out << "ELEMENT_POSITION_NOT_FOUND";
break;
case ProcessedActionStatusProto::CLIENT_MEMORY_KEY_NOT_AVAILABLE:
out << "CLIENT_MEMORY_KEY_NOT_AVAILABLE";
break;
case ProcessedActionStatusProto::EMPTY_VALUE_EXPRESSION_RESULT:
out << "EMPTY_VALUE_EXPRESSION_RESULT";
break;

// Intentionally no default case to make compilation fail if a new value
// was added to the enum but not to this list.
Expand Down

0 comments on commit b343372

Please sign in to comment.