Commit 6d0a8e5f authored by Luca Hunkeler's avatar Luca Hunkeler Committed by Commit Bot

[Autofill Assistant] Support interactions with multiple triggers

Before this cl if you wanted a callback to trigger when any of a list of
events happened, you'd have to add multiple interactions. Supporting
multiple events allows for a more compact proto with less duplication
in those cases.

Bug: b/168195199
Change-Id: I5614fc741ab83801a1102e1e4c2b99a4add04e86
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2435639
Commit-Queue: Luca Hunkeler <hluca@google.com>
Reviewed-by: default avatarMarian Fechete <marianfe@google.com>
Reviewed-by: default avatarClemens Arbesser <arbesser@google.com>
Cr-Commit-Position: refs/heads/master@{#811954}
parent 14092f34
...@@ -30,32 +30,33 @@ bool CreateJavaListenersFromProto( ...@@ -30,32 +30,33 @@ bool CreateJavaListenersFromProto(
base::android::ScopedJavaGlobalRef<jobject> jdelegate, base::android::ScopedJavaGlobalRef<jobject> jdelegate,
const InteractionsProto& proto) { const InteractionsProto& proto) {
for (const auto& interaction_proto : proto.interactions()) { for (const auto& interaction_proto : proto.interactions()) {
const auto& event_proto = interaction_proto.trigger_event(); for (const auto& event_proto : interaction_proto.trigger_event()) {
switch (event_proto.kind_case()) { switch (event_proto.kind_case()) {
case EventProto::kOnViewClicked: { case EventProto::kOnViewClicked: {
auto jview = view_handler->GetView( auto jview = view_handler->GetView(
event_proto.on_view_clicked().view_identifier()); event_proto.on_view_clicked().view_identifier());
if (!jview.has_value()) { if (!jview.has_value()) {
VLOG(1) << "Invalid click event, no view with id='" VLOG(1) << "Invalid click event, no view with id='"
<< event_proto.on_view_clicked().view_identifier() << event_proto.on_view_clicked().view_identifier()
<< "' found"; << "' found";
return false; return false;
}
SetOnClickListener(env, *jview, jdelegate,
event_proto.on_view_clicked());
break;
} }
SetOnClickListener(env, *jview, jdelegate, case EventProto::kOnValueChanged:
event_proto.on_view_clicked()); case EventProto::kOnUserActionCalled:
break; case EventProto::kOnTextLinkClicked:
case EventProto::kOnPopupDismissed:
case EventProto::kOnViewContainerCleared:
// Skip events that do not require registering java-side listeners.
break;
case EventProto::KIND_NOT_SET:
VLOG(1)
<< "Error creating java listener for trigger event: kind not set";
return false;
} }
case EventProto::kOnValueChanged:
case EventProto::kOnUserActionCalled:
case EventProto::kOnTextLinkClicked:
case EventProto::kOnPopupDismissed:
case EventProto::kOnViewContainerCleared:
// Skip events that do not require registering java-side listeners.
break;
case EventProto::KIND_NOT_SET:
VLOG(1)
<< "Error creating java listener for trigger event: kind not set";
return false;
} }
} }
return true; return true;
......
...@@ -315,7 +315,7 @@ bool CreateImplicitInteractionsForView( ...@@ -315,7 +315,7 @@ bool CreateImplicitInteractionsForView(
// Auto-update the text of the view whenever the corresponding value in // Auto-update the text of the view whenever the corresponding value in
// the model changes. // the model changes.
InteractionProto implicit_set_text_interaction; InteractionProto implicit_set_text_interaction;
implicit_set_text_interaction.mutable_trigger_event() implicit_set_text_interaction.add_trigger_event()
->mutable_on_value_changed() ->mutable_on_value_changed()
->set_model_identifier(proto.text_input_view().model_identifier()); ->set_model_identifier(proto.text_input_view().model_identifier());
SetTextProto set_text_callback; SetTextProto set_text_callback;
...@@ -339,7 +339,7 @@ bool CreateImplicitInteractionsForView( ...@@ -339,7 +339,7 @@ bool CreateImplicitInteractionsForView(
} }
// Auto-update text view content. // Auto-update text view content.
InteractionProto implicit_set_text_interaction; InteractionProto implicit_set_text_interaction;
implicit_set_text_interaction.mutable_trigger_event() implicit_set_text_interaction.add_trigger_event()
->mutable_on_value_changed() ->mutable_on_value_changed()
->set_model_identifier(proto.text_view().model_identifier()); ->set_model_identifier(proto.text_view().model_identifier());
SetTextProto set_text_callback; SetTextProto set_text_callback;
......
...@@ -131,12 +131,7 @@ bool InteractionHandlerAndroid::AddInteractionsFromProto( ...@@ -131,12 +131,7 @@ bool InteractionHandlerAndroid::AddInteractionsFromProto(
NOTREACHED() << "Interactions can not be added while listening to events!"; NOTREACHED() << "Interactions can not be added while listening to events!";
return false; return false;
} }
auto key = EventHandler::CreateEventKeyFromProto(proto.trigger_event()); std::vector<InteractionHandlerAndroid::InteractionCallback> callbacks;
if (!key) {
VLOG(1) << "Invalid trigger event for interaction";
return false;
}
for (const auto& callback_proto : proto.callbacks()) { for (const auto& callback_proto : proto.callbacks()) {
auto callback = CreateInteractionCallbackFromProto(callback_proto); auto callback = CreateInteractionCallbackFromProto(callback_proto);
if (!callback) { if (!callback) {
...@@ -150,7 +145,19 @@ bool InteractionHandlerAndroid::AddInteractionsFromProto( ...@@ -150,7 +145,19 @@ bool InteractionHandlerAndroid::AddInteractionsFromProto(
basic_interactions_->GetWeakPtr(), basic_interactions_->GetWeakPtr(),
callback_proto.condition_model_identifier(), *callback)); callback_proto.condition_model_identifier(), *callback));
} }
AddInteraction(*key, *callback); callbacks.push_back(std::move(*callback));
}
for (const auto& trigger_event : proto.trigger_event()) {
auto key = EventHandler::CreateEventKeyFromProto(trigger_event);
if (!key) {
VLOG(1) << "Invalid trigger event of type " << trigger_event.kind_case();
return false;
}
for (const auto& callback : callbacks) {
AddInteraction(*key, callback);
}
} }
return true; return true;
} }
......
...@@ -26,12 +26,13 @@ message InteractionsProto { ...@@ -26,12 +26,13 @@ message InteractionsProto {
repeated InteractionProto interactions = 1; repeated InteractionProto interactions = 1;
} }
// An interaction consists of a trigger event and a series of actions. // An interaction consists of a set of trigger events and a series of actions.
message InteractionProto { message InteractionProto {
// Functions to call each time the event happens, in the specified order. // Functions to call each time one of the events happens, in the specified
// order.
repeated CallbackProto callbacks = 1; repeated CallbackProto callbacks = 1;
// The trigger event for |callbacks|. // The trigger events for |callbacks|.
optional EventProto trigger_event = 2; repeated EventProto trigger_event = 2;
} }
// UI Actions to invoke. // UI Actions to invoke.
......
...@@ -157,9 +157,8 @@ void ReplacePlaceholdersInValue( ...@@ -157,9 +157,8 @@ void ReplacePlaceholdersInValue(
void ReplacePlaceholdersInInteraction( void ReplacePlaceholdersInInteraction(
InteractionProto* in_out_proto, InteractionProto* in_out_proto,
const std::map<std::string, std::string>& placeholders) { const std::map<std::string, std::string>& placeholders) {
if (in_out_proto->has_trigger_event()) { for (auto& trigger_event : *in_out_proto->mutable_trigger_event()) {
ReplacePlaceholdersInEvent(in_out_proto->mutable_trigger_event(), ReplacePlaceholdersInEvent(&trigger_event, placeholders);
placeholders);
} }
for (auto& callback : *in_out_proto->mutable_callbacks()) { for (auto& callback : *in_out_proto->mutable_callbacks()) {
......
...@@ -82,25 +82,25 @@ TEST(GenericUiReplacePlaceholdersTest, ReplacePlaceholdersInEvents) { ...@@ -82,25 +82,25 @@ TEST(GenericUiReplacePlaceholdersTest, ReplacePlaceholdersInEvents) {
GenericUserInterfaceProto input; GenericUserInterfaceProto input;
auto* on_value_changed = input.mutable_interactions() auto* on_value_changed = input.mutable_interactions()
->add_interactions() ->add_interactions()
->mutable_trigger_event() ->add_trigger_event()
->mutable_on_value_changed(); ->mutable_on_value_changed();
on_value_changed->set_model_identifier("value_${i}"); on_value_changed->set_model_identifier("value_${i}");
auto* on_view_clicked = input.mutable_interactions() auto* on_view_clicked = input.mutable_interactions()
->add_interactions() ->add_interactions()
->mutable_trigger_event() ->add_trigger_event()
->mutable_on_view_clicked(); ->mutable_on_view_clicked();
on_view_clicked->set_view_identifier("view_${i}"); on_view_clicked->set_view_identifier("view_${i}");
auto* on_view_container_cleared = input.mutable_interactions() auto* on_view_container_cleared = input.mutable_interactions()
->add_interactions() ->add_interactions()
->mutable_trigger_event() ->add_trigger_event()
->mutable_on_view_container_cleared(); ->mutable_on_view_container_cleared();
on_view_container_cleared->set_view_identifier("view_${i}"); on_view_container_cleared->set_view_identifier("view_${i}");
auto* on_popup_dismissed = input.mutable_interactions() auto* on_popup_dismissed = input.mutable_interactions()
->add_interactions() ->add_interactions()
->mutable_trigger_event() ->add_trigger_event()
->mutable_on_popup_dismissed(); ->mutable_on_popup_dismissed();
on_popup_dismissed->set_popup_identifier("popup_${i}"); on_popup_dismissed->set_popup_identifier("popup_${i}");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment