From 4eb23bbbe31fb7e02195c6af3ab0923f8dfd986e Mon Sep 17 00:00:00 2001 From: Florian Gauger Date: Wed, 11 May 2022 15:51:03 +0000 Subject: [PATCH] [CEX] Add autofill error info return value to js flows RunNativeAction Bug: b/231918430 Change-Id: I6a53202ae8931cd7129a4c81df51e773ea238f4c Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3640572 Reviewed-by: Clemens Arbesser Commit-Queue: Florian Gauger Auto-Submit: Florian Gauger Cr-Commit-Position: refs/heads/main@{#1002115} --- .../actions/js_flow_action_unittest.cc | 44 +++++++++++++++++++ .../browser/js_flow_util.cc | 21 +++++++-- .../browser/js_flow_util_unittest.cc | 17 +++++++ 3 files changed, 79 insertions(+), 3 deletions(-) diff --git a/components/autofill_assistant/browser/actions/js_flow_action_unittest.cc b/components/autofill_assistant/browser/actions/js_flow_action_unittest.cc index a39302e577f1d6..0811cf9e226bdb 100644 --- a/components/autofill_assistant/browser/actions/js_flow_action_unittest.cc +++ b/components/autofill_assistant/browser/actions/js_flow_action_unittest.cc @@ -363,4 +363,48 @@ TEST_F(JsFlowActionTest, NativeActionReturnsActionResult) { action->ProcessAction(callback_.Get()); } +TEST_F(JsFlowActionTest, NativeActionReturnsAutofillErrorInfo) { + auto mock_js_flow_executor = std::make_unique(); + auto* mock_js_flow_executor_ptr = mock_js_flow_executor.get(); + auto action = CreateAction(std::move(mock_js_flow_executor)); + + EXPECT_CALL(*mock_js_flow_executor_ptr, Start) + .WillOnce(WithArg<1>([&](auto finished_callback) { + ActionProto native_action; + native_action.mutable_wait_for_dom()->mutable_wait_condition(); + + action->RunNativeAction( + /* action_id = */ static_cast( + native_action.action_info_case()), + /* action = */ + native_action.wait_for_dom().SerializeAsString(), + native_action_callback_.Get()); + + std::move(finished_callback).Run(ClientStatus(ACTION_APPLIED), nullptr); + })); + + AutofillErrorInfoProto autofill_error_info; + autofill_error_info.set_client_memory_address_key_names("key_names"); + std::string autofill_error_info_base64; + base::Base64Encode(autofill_error_info.SerializeAsString(), + &autofill_error_info_base64); + + EXPECT_CALL(mock_action_delegate_, WaitForDomWithSlowWarning) + .WillOnce(WithArg<4>([&](auto dom_finished_callback) { + *GetInnerProcessedAction(*action) + ->mutable_status_details() + ->mutable_autofill_error_info() = autofill_error_info; + std::move(dom_finished_callback) + .Run(ClientStatus(ACTION_APPLIED), base::Seconds(0)); + })); + + EXPECT_CALL(native_action_callback_, Run(_, Pointee(IsJson(R"( + { + "navigationStarted": false, + "autofillErrorInfo": ")" + autofill_error_info_base64 + + "\"}")))); + + action->ProcessAction(callback_.Get()); +} + } // namespace autofill_assistant diff --git a/components/autofill_assistant/browser/js_flow_util.cc b/components/autofill_assistant/browser/js_flow_util.cc index ee0a5ecc6b252e..e99d986a79c2eb 100644 --- a/components/autofill_assistant/browser/js_flow_util.cc +++ b/components/autofill_assistant/browser/js_flow_util.cc @@ -28,6 +28,10 @@ const char kActionSpecificResultKey[] = "actionSpecificResult"; // by runNativeAction(). // DO NOT CHANGE const char kNavigationStartedKey[] = "navigationStarted"; +// The key for the autofill error info in the result object returned by +// runNativeAction(). +// DO NOT CHANGE +const char kAutofillErrorInfo[] = "autofillErrorInfo"; // Returns true for remote object types that flows are allowed to return. This // is mostly used to filter types like FUNCTION which would otherwise slip @@ -165,6 +169,12 @@ ClientStatus ExtractJsFlowActionReturnValue( namespace { +std::string SerializeToBase64(const google::protobuf::MessageLite* proto) { + std::string serialized_result_base64; + base::Base64Encode(proto->SerializeAsString(), &serialized_result_base64); + return serialized_result_base64; +} + absl::optional SerializeActionResult( const ProcessedActionProto& processed_action) { const google::protobuf::MessageLite* proto; @@ -209,9 +219,7 @@ absl::optional SerializeActionResult( return absl::nullopt; } - std::string serialized_result_base64; - base::Base64Encode(proto->SerializeAsString(), &serialized_result_base64); - return serialized_result_base64; + return SerializeToBase64(proto); } } // namespace @@ -228,6 +236,13 @@ std::unique_ptr NativeActionResultToResultValue( result_value.Set(kActionSpecificResultKey, *serialized_result); } + if (processed_action.status_details().has_autofill_error_info()) { + result_value.Set( + kAutofillErrorInfo, + SerializeToBase64( + &processed_action.status_details().autofill_error_info())); + } + return std::make_unique(std::move(result_value)); } diff --git a/components/autofill_assistant/browser/js_flow_util_unittest.cc b/components/autofill_assistant/browser/js_flow_util_unittest.cc index 2ab4229406507e..b7e7850165c538 100644 --- a/components/autofill_assistant/browser/js_flow_util_unittest.cc +++ b/components/autofill_assistant/browser/js_flow_util_unittest.cc @@ -250,6 +250,23 @@ TEST(JsFlowUtilTest, NativeActionResultToResultValueHasSerializedActionResult) { "actionSpecificResult": ")" + wait_for_dom_result_base64 + "\"}"))); } +TEST(JsFlowUtilTest, NativeActionResultToResultValueHasAutofillErrorInfo) { + ProcessedActionProto processed_action; + AutofillErrorInfoProto* autofill_error_info = + processed_action.mutable_status_details()->mutable_autofill_error_info(); + autofill_error_info->set_client_memory_address_key_names("key_names"); + + std::string autofill_error_info_base64; + base::Base64Encode(autofill_error_info->SerializeAsString(), + &autofill_error_info_base64); + + EXPECT_THAT( + NativeActionResultToResultValue(processed_action), Pointee(IsJson(R"( + { + "navigationStarted": false, + "autofillErrorInfo": ")" + autofill_error_info_base64 + "\"}"))); +} + TEST(JsFlowUtilTest, NativeActionResultToResultValueHasEmptyActionResult) { ProcessedActionProto processed_action;