Skip to content

Commit

Permalink
[CEX] Add autofill error info return value to js flows RunNativeAction
Browse files Browse the repository at this point in the history
Bug: b/231918430
Change-Id: I6a53202ae8931cd7129a4c81df51e773ea238f4c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3640572
Reviewed-by: Clemens Arbesser <arbesser@google.com>
Commit-Queue: Florian Gauger <fga@google.com>
Auto-Submit: Florian Gauger <fga@google.com>
Cr-Commit-Position: refs/heads/main@{#1002115}
  • Loading branch information
Florian Gauger authored and Chromium LUCI CQ committed May 11, 2022
1 parent 3731c77 commit 4eb23bb
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -363,4 +363,48 @@ TEST_F(JsFlowActionTest, NativeActionReturnsActionResult) {
action->ProcessAction(callback_.Get());
}

TEST_F(JsFlowActionTest, NativeActionReturnsAutofillErrorInfo) {
auto mock_js_flow_executor = std::make_unique<MockJsFlowExecutor>();
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<int>(
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
21 changes: 18 additions & 3 deletions components/autofill_assistant/browser/js_flow_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<std::string> SerializeActionResult(
const ProcessedActionProto& processed_action) {
const google::protobuf::MessageLite* proto;
Expand Down Expand Up @@ -209,9 +219,7 @@ absl::optional<std::string> SerializeActionResult(
return absl::nullopt;
}

std::string serialized_result_base64;
base::Base64Encode(proto->SerializeAsString(), &serialized_result_base64);
return serialized_result_base64;
return SerializeToBase64(proto);
}

} // namespace
Expand All @@ -228,6 +236,13 @@ std::unique_ptr<base::Value> 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<base::Value>(std::move(result_value));
}

Expand Down
17 changes: 17 additions & 0 deletions components/autofill_assistant/browser/js_flow_util_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 4eb23bb

Please sign in to comment.