[go: nahoru, domu]

[mullhet] Create PasswordSuggestionRequest mojom type.

This CL is a scaffolding for the subsequent refactoring of
the password suggestion triggering logic. The end goal is to
pass all parameters from PasswordAutofillAgent to
PasswordAutofillManager in a struct called PasswordSuggestionRequest.

Bug: b:321679106
Change-Id: I9d4d9972507584e870e912e8b50b249e8f56ad6e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5243652
Reviewed-by: Florian Leimgruber <fleimgruber@google.com>
Reviewed-by: Vasilii Sukhanov <vasilii@chromium.org>
Reviewed-by: Kinuko Yasuda <kinuko@chromium.org>
Commit-Queue: Timofey Chudakov <tchudakov@google.com>
Cr-Commit-Position: refs/heads/main@{#1255576}
diff --git a/components/autofill/core/common/mojom/BUILD.gn b/components/autofill/core/common/mojom/BUILD.gn
index 088f9f5..38a66cb 100644
--- a/components/autofill/core/common/mojom/BUILD.gn
+++ b/components/autofill/core/common/mojom/BUILD.gn
@@ -84,6 +84,10 @@
           mojom = "autofill.mojom.PasswordGenerationUIData"
           cpp = "::autofill::password_generation::PasswordGenerationUIData"
         },
+        {
+          mojom = "autofill.mojom.PasswordSuggestionRequest"
+          cpp = "::autofill::PasswordSuggestionRequest"
+        },
       ]
       traits_headers = [ "autofill_types_mojom_traits.h" ]
       traits_sources = [ "autofill_types_mojom_traits.cc" ]
diff --git a/components/autofill/core/common/mojom/autofill_types.mojom b/components/autofill/core/common/mojom/autofill_types.mojom
index 05cc845..5a787db 100644
--- a/components/autofill/core/common/mojom/autofill_types.mojom
+++ b/components/autofill/core/common/mojom/autofill_types.mojom
@@ -500,6 +500,19 @@
   bool input_field_empty;
 };
 
+// autofill::PasswordSuggestionRequest
+// (components/autofill/core/common/password_form_fill_data.h)
+struct PasswordSuggestionRequest {
+  FieldRendererId element_id;
+  FormData form_data;
+  uint64 username_field_index;
+  uint64 password_field_index;
+  mojo_base.mojom.TextDirection text_direction;
+  mojo_base.mojom.String16 typed_username;
+  int32 options;
+  gfx.mojom.RectF bounds;
+};
+
 // autofill::ParsingResult
 // (components/autofill/core/common/password_form_fill_data.h)
 struct ParsingResult {
diff --git a/components/autofill/core/common/mojom/autofill_types_mojom_traits.cc b/components/autofill/core/common/mojom/autofill_types_mojom_traits.cc
index 64defab..40231b5 100644
--- a/components/autofill/core/common/mojom/autofill_types_mojom_traits.cc
+++ b/components/autofill/core/common/mojom/autofill_types_mojom_traits.cc
@@ -458,6 +458,22 @@
          data.ReadFormData(&out->form_data);
 }
 
+// static
+bool StructTraits<autofill::mojom::PasswordSuggestionRequestDataView,
+                  autofill::PasswordSuggestionRequest>::
+    Read(autofill::mojom::PasswordSuggestionRequestDataView data,
+         autofill::PasswordSuggestionRequest* out) {
+  out->username_field_index = data.username_field_index();
+  out->password_field_index = data.password_field_index();
+  out->options = data.options();
+
+  return data.ReadElementId(&out->element_id) &&
+         data.ReadFormData(&out->form_data) &&
+         data.ReadTextDirection(&out->text_direction) &&
+         data.ReadTypedUsername(&out->typed_username) &&
+         data.ReadBounds(&out->bounds);
+}
+
 bool StructTraits<
     autofill::mojom::ParsingResultDataView,
     autofill::ParsingResult>::Read(autofill::mojom::ParsingResultDataView data,
diff --git a/components/autofill/core/common/mojom/autofill_types_mojom_traits.h b/components/autofill/core/common/mojom/autofill_types_mojom_traits.h
index d1cc038..6874866f 100644
--- a/components/autofill/core/common/mojom/autofill_types_mojom_traits.h
+++ b/components/autofill/core/common/mojom/autofill_types_mojom_traits.h
@@ -696,6 +696,52 @@
 };
 
 template <>
+struct StructTraits<autofill::mojom::PasswordSuggestionRequestDataView,
+                    autofill::PasswordSuggestionRequest> {
+  static autofill::FieldRendererId element_id(
+      const autofill::PasswordSuggestionRequest& r) {
+    return r.element_id;
+  }
+
+  static const autofill::FormData& form_data(
+      const autofill::PasswordSuggestionRequest& r) {
+    return r.form_data;
+  }
+
+  static uint64_t username_field_index(
+      const autofill::PasswordSuggestionRequest& r) {
+    return r.username_field_index;
+  }
+
+  static uint64_t password_field_index(
+      const autofill::PasswordSuggestionRequest& r) {
+    return r.password_field_index;
+  }
+
+  static base::i18n::TextDirection text_direction(
+      const autofill::PasswordSuggestionRequest& r) {
+    return r.text_direction;
+  }
+
+  static const std::u16string& typed_username(
+      const autofill::PasswordSuggestionRequest& r) {
+    return r.typed_username;
+  }
+
+  static int options(const autofill::PasswordSuggestionRequest& r) {
+    return r.options;
+  }
+
+  static const gfx::RectF& bounds(
+      const autofill::PasswordSuggestionRequest& r) {
+    return r.bounds;
+  }
+
+  static bool Read(autofill::mojom::PasswordSuggestionRequestDataView data,
+                   autofill::PasswordSuggestionRequest* out);
+};
+
+template <>
 struct StructTraits<autofill::mojom::ParsingResultDataView,
                     autofill::ParsingResult> {
   static autofill::FieldRendererId username_renderer_id(
diff --git a/components/autofill/core/common/mojom/autofill_types_mojom_traits_unittest.cc b/components/autofill/core/common/mojom/autofill_types_mojom_traits_unittest.cc
index 5d1cc93..c586173 100644
--- a/components/autofill/core/common/mojom/autofill_types_mojom_traits_unittest.cc
+++ b/components/autofill/core/common/mojom/autofill_types_mojom_traits_unittest.cc
@@ -10,6 +10,7 @@
 #include "base/test/task_environment.h"
 #include "components/autofill/core/browser/autofill_test_utils.h"
 #include "components/autofill/core/common/autofill_clock.h"
+#include "components/autofill/core/common/autofill_constants.h"
 #include "components/autofill/core/common/form_data.h"
 #include "components/autofill/core/common/form_field_data.h"
 #include "components/autofill/core/common/html_field_types.h"
@@ -87,6 +88,17 @@
   data->form_data = test::CreateTestAddressFormData();
 }
 
+void CreatePasswordSuggestionRequest(PasswordSuggestionRequest* data) {
+  data->element_id = FieldRendererId(123);
+  data->form_data = test::CreateTestAddressFormData();
+  data->username_field_index = 0ul;
+  data->password_field_index = 1ul;
+  data->text_direction = base::i18n::RIGHT_TO_LEFT;
+  data->typed_username = u"username";
+  data->options = SHOW_ALL | IS_PASSWORD_FIELD | ACCEPTS_WEBAUTHN_CREDENTIALS;
+  data->form_data = test::CreateTestAddressFormData();
+}
+
 void CheckEqualPasswordFormFillData(const PasswordFormFillData& expected,
                                     const PasswordFormFillData& actual) {
   EXPECT_EQ(expected.form_renderer_id, actual.form_renderer_id);
@@ -112,6 +124,20 @@
                   .SameFormAs(actual.form_data));
 }
 
+void CheckEqualPasswordSuggestionRequest(
+    const PasswordSuggestionRequest& expected,
+    const PasswordSuggestionRequest& actual) {
+  EXPECT_EQ(expected.element_id, actual.element_id);
+  EXPECT_TRUE(test::WithoutUnserializedData(expected.form_data)
+                  .SameFormAs(actual.form_data));
+  EXPECT_EQ(expected.username_field_index, actual.username_field_index);
+  EXPECT_EQ(expected.password_field_index, actual.password_field_index);
+  EXPECT_EQ(expected.text_direction, actual.text_direction);
+  EXPECT_EQ(expected.typed_username, actual.typed_username);
+  EXPECT_EQ(expected.options, actual.options);
+  EXPECT_EQ(expected.bounds, actual.bounds);
+}
+
 }  // namespace
 
 class AutofillTypeTraitsTestImpl : public testing::Test,
@@ -169,6 +195,12 @@
     std::move(callback).Run(s);
   }
 
+  void PassPasswordSuggestionRequest(
+      const PasswordSuggestionRequest& s,
+      PassPasswordSuggestionRequestCallback callback) override {
+    std::move(callback).Run(s);
+  }
+
  private:
   base::test::TaskEnvironment task_environment_;
   test::AutofillUnitTestEnvironment autofill_test_environment_;
@@ -236,6 +268,13 @@
   std::move(closure).Run();
 }
 
+void ExpectPasswordSuggestionRequest(const PasswordSuggestionRequest& expected,
+                                     base::OnceClosure closure,
+                                     const PasswordSuggestionRequest& passed) {
+  CheckEqualPasswordSuggestionRequest(expected, passed);
+  std::move(closure).Run();
+}
+
 // Test all Section::SectionPrefix states.
 class AutofillTypeTraitsTestImplSectionTest
     : public AutofillTypeTraitsTestImpl,
@@ -451,4 +490,16 @@
   loop.Run();
 }
 
+TEST_F(AutofillTypeTraitsTestImpl, PassPasswordSuggestionRequest) {
+  PasswordSuggestionRequest input;
+  CreatePasswordSuggestionRequest(&input);
+
+  base::RunLoop loop;
+  mojo::Remote<mojom::TypeTraitsTest> remote(GetTypeTraitsTestRemote());
+  remote->PassPasswordSuggestionRequest(
+      input, base::BindOnce(&ExpectPasswordSuggestionRequest, input,
+                            loop.QuitClosure()));
+  loop.Run();
+}
+
 }  // namespace autofill
diff --git a/components/autofill/core/common/mojom/test_autofill_types.mojom b/components/autofill/core/common/mojom/test_autofill_types.mojom
index 8e4effa..4bddeed 100644
--- a/components/autofill/core/common/mojom/test_autofill_types.mojom
+++ b/components/autofill/core/common/mojom/test_autofill_types.mojom
@@ -20,4 +20,6 @@
       (PasswordFormGenerationData passed);
   PassPasswordGenerationUIData(PasswordGenerationUIData s) =>
       (PasswordGenerationUIData passed);
+  PassPasswordSuggestionRequest(PasswordSuggestionRequest s) =>
+      (PasswordSuggestionRequest passed);
 };
diff --git a/components/autofill/core/common/password_form_fill_data.cc b/components/autofill/core/common/password_form_fill_data.cc
index 14aba32..640a943b 100644
--- a/components/autofill/core/common/password_form_fill_data.cc
+++ b/components/autofill/core/common/password_form_fill_data.cc
@@ -21,6 +21,35 @@
     default;
 PasswordAndMetadata::~PasswordAndMetadata() = default;
 
+PasswordSuggestionRequest::PasswordSuggestionRequest(
+    FieldRendererId element_id,
+    const FormData& form_data,
+    uint64_t username_field_index,
+    uint64_t password_field_index,
+    base::i18n::TextDirection text_direction,
+    const std::u16string& typed_username,
+    int options,
+    const gfx::RectF& bounds)
+    : element_id(element_id),
+      form_data(form_data),
+      username_field_index(username_field_index),
+      password_field_index(password_field_index),
+      text_direction(text_direction),
+      typed_username(typed_username),
+      options(options),
+      bounds(bounds) {}
+
+PasswordSuggestionRequest::PasswordSuggestionRequest() = default;
+PasswordSuggestionRequest::PasswordSuggestionRequest(
+    const PasswordSuggestionRequest&) = default;
+PasswordSuggestionRequest& PasswordSuggestionRequest::operator=(
+    const PasswordSuggestionRequest&) = default;
+PasswordSuggestionRequest::PasswordSuggestionRequest(
+    PasswordSuggestionRequest&&) = default;
+PasswordSuggestionRequest& PasswordSuggestionRequest::operator=(
+    PasswordSuggestionRequest&&) = default;
+PasswordSuggestionRequest::~PasswordSuggestionRequest() = default;
+
 PasswordFormFillData::PasswordFormFillData() = default;
 PasswordFormFillData::PasswordFormFillData(const PasswordFormFillData&) =
     default;
diff --git a/components/autofill/core/common/password_form_fill_data.h b/components/autofill/core/common/password_form_fill_data.h
index 0a089f0..8a611399 100644
--- a/components/autofill/core/common/password_form_fill_data.h
+++ b/components/autofill/core/common/password_form_fill_data.h
@@ -35,6 +35,48 @@
   bool uses_account_store = false;
 };
 
+// Structure used to trigger password suggestion generation.
+struct PasswordSuggestionRequest {
+  PasswordSuggestionRequest(FieldRendererId element_id,
+                            const FormData& form_data,
+                            uint64_t username_field_index,
+                            uint64_t password_field_index,
+                            base::i18n::TextDirection text_direction,
+                            const std::u16string& typed_username,
+                            int options,
+                            const gfx::RectF& bounds);
+
+  PasswordSuggestionRequest();
+  PasswordSuggestionRequest(const PasswordSuggestionRequest&);
+  PasswordSuggestionRequest& operator=(const PasswordSuggestionRequest&);
+  PasswordSuggestionRequest(PasswordSuggestionRequest&&);
+  PasswordSuggestionRequest& operator=(PasswordSuggestionRequest&&);
+  ~PasswordSuggestionRequest();
+
+  // The unique renderer id of the field that the user has clicked.
+  FieldRendererId element_id;
+  // A web form extracted from the DOM that contains the triggering field.
+  FormData form_data;
+  // The index of the username field in the `form_data.fields`. If the password
+  // form doesn't contain the username field, this value will be equal to
+  // `form_data.fields.size()`.
+  uint64_t username_field_index;
+  // The index of the password field in the `form_data.fields`. If the password
+  // form doesn't contain the password field, this value will be equal to
+  // `form_data.fields.size()`.
+  uint64_t password_field_index;
+  // Direction of the text for the triggering field.
+  base::i18n::TextDirection text_direction;
+  // The value of the username field. This will be empty if the suggestion
+  // generation is triggered on a password field.
+  std::u16string typed_username;
+  // Options for password suggestion generation, see
+  // `ShowPasswordSuggestionsOptions` for more details.
+  int options;
+  // Location at which to display the popup.
+  gfx::RectF bounds;
+};
+
 // Structure used for autofilling password forms. Note that the realms in this
 // struct are only set when the password's realm differs from the realm of the
 // form that we are filling.