Commit 14add241 authored by Clemens Arbesser's avatar Clemens Arbesser Committed by Commit Bot

[Autofill Assistant] Moved view management into dedicated class.

This CL moves ownership and management of the map containing all views
with proto-defined identifiers to a dedicated class. This is in
preparation for an upcoming CL, where retrieving views by their ID will
become a bit more complicated.

This is a refactoring only and should have no user-facing changes.

Bug: b/145043394
Change-Id: I1c16182bee7036a153a13c54f2c718b45e73e851
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2232718
Commit-Queue: Clemens Arbesser <arbesser@google.com>
Reviewed-by: default avatarSandro Maggi <sandromaggi@google.com>
Cr-Commit-Position: refs/heads/master@{#775998}
parent b3be4f3d
...@@ -2380,6 +2380,8 @@ static_library("browser") { ...@@ -2380,6 +2380,8 @@ static_library("browser") {
"android/autofill_assistant/ui_controller_android.h", "android/autofill_assistant/ui_controller_android.h",
"android/autofill_assistant/ui_controller_android_utils.cc", "android/autofill_assistant/ui_controller_android_utils.cc",
"android/autofill_assistant/ui_controller_android_utils.h", "android/autofill_assistant/ui_controller_android_utils.h",
"android/autofill_assistant/view_handler_android.cc",
"android/autofill_assistant/view_handler_android.h",
"android/background_sync_launcher_android.cc", "android/background_sync_launcher_android.cc",
"android/background_sync_launcher_android.h", "android/background_sync_launcher_android.h",
"android/background_tab_manager.cc", "android/background_tab_manager.cc",
......
...@@ -19,6 +19,7 @@ class InteractionHandlerAndroid; ...@@ -19,6 +19,7 @@ class InteractionHandlerAndroid;
class BasicInteractions; class BasicInteractions;
class EventHandler; class EventHandler;
class UserModel; class UserModel;
class ViewHandlerAndroid;
class GenericUiControllerAndroid { class GenericUiControllerAndroid {
public: public:
...@@ -39,19 +40,13 @@ class GenericUiControllerAndroid { ...@@ -39,19 +40,13 @@ class GenericUiControllerAndroid {
GenericUiControllerAndroid( GenericUiControllerAndroid(
base::android::ScopedJavaGlobalRef<jobject> jroot_view, base::android::ScopedJavaGlobalRef<jobject> jroot_view,
std::unique_ptr< std::unique_ptr<ViewHandlerAndroid> view_handler,
std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>>
views,
std::unique_ptr<InteractionHandlerAndroid> interaction_handler); std::unique_ptr<InteractionHandlerAndroid> interaction_handler);
~GenericUiControllerAndroid(); ~GenericUiControllerAndroid();
private: private:
base::android::ScopedJavaGlobalRef<jobject> jroot_view_; base::android::ScopedJavaGlobalRef<jobject> jroot_view_;
std::unique_ptr<ViewHandlerAndroid> view_handler_;
// Maps view-ids to android views.
std::unique_ptr<
std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>>
views_;
std::unique_ptr<InteractionHandlerAndroid> interaction_handler_; std::unique_ptr<InteractionHandlerAndroid> interaction_handler_;
DISALLOW_COPY_AND_ASSIGN(GenericUiControllerAndroid); DISALLOW_COPY_AND_ASSIGN(GenericUiControllerAndroid);
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "chrome/browser/android/autofill_assistant/generic_ui_events_android.h" #include "chrome/browser/android/autofill_assistant/generic_ui_events_android.h"
#include "base/android/jni_string.h" #include "base/android/jni_string.h"
#include "chrome/android/features/autofill_assistant/jni_headers/AssistantViewEvents_jni.h" #include "chrome/android/features/autofill_assistant/jni_headers/AssistantViewEvents_jni.h"
#include "chrome/browser/android/autofill_assistant/view_handler_android.h"
namespace autofill_assistant { namespace autofill_assistant {
namespace android_events { namespace android_events {
...@@ -25,22 +26,22 @@ void SetOnClickListener(JNIEnv* env, ...@@ -25,22 +26,22 @@ void SetOnClickListener(JNIEnv* env,
bool CreateJavaListenersFromProto( bool CreateJavaListenersFromProto(
JNIEnv* env, JNIEnv* env,
std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>* views, ViewHandlerAndroid* view_handler,
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(); 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 = auto jview = view_handler->GetView(
views->find(event_proto.on_view_clicked().view_identifier()); event_proto.on_view_clicked().view_identifier());
if (jview == views->end()) { 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->second, jdelegate, SetOnClickListener(env, *jview, jdelegate,
event_proto.on_view_clicked()); event_proto.on_view_clicked());
break; break;
} }
......
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
#include "components/autofill_assistant/browser/generic_ui.pb.h" #include "components/autofill_assistant/browser/generic_ui.pb.h"
namespace autofill_assistant { namespace autofill_assistant {
class ViewHandlerAndroid;
namespace android_events { namespace android_events {
// Creates java listeners for all view events in |proto| such that |jdelegate| // Creates java listeners for all view events in |proto| such that |jdelegate|
...@@ -19,7 +21,7 @@ namespace android_events { ...@@ -19,7 +21,7 @@ namespace android_events {
// success, false on failure. // success, false on failure.
bool CreateJavaListenersFromProto( bool CreateJavaListenersFromProto(
JNIEnv* env, JNIEnv* env,
std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>* views, ViewHandlerAndroid* view_handler,
base::android::ScopedJavaGlobalRef<jobject> jdelegate, base::android::ScopedJavaGlobalRef<jobject> jdelegate,
const InteractionsProto& proto); const InteractionsProto& proto);
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "chrome/android/features/autofill_assistant/jni_headers/AssistantViewInteractions_jni.h" #include "chrome/android/features/autofill_assistant/jni_headers/AssistantViewInteractions_jni.h"
#include "chrome/browser/android/autofill_assistant/generic_ui_controller_android.h" #include "chrome/browser/android/autofill_assistant/generic_ui_controller_android.h"
#include "chrome/browser/android/autofill_assistant/ui_controller_android_utils.h" #include "chrome/browser/android/autofill_assistant/ui_controller_android_utils.h"
#include "chrome/browser/android/autofill_assistant/view_handler_android.h"
#include "components/autofill_assistant/browser/user_model.h" #include "components/autofill_assistant/browser/user_model.h"
namespace autofill_assistant { namespace autofill_assistant {
...@@ -218,11 +219,10 @@ void ShowGenericPopup(const ShowGenericUiPopupProto& proto, ...@@ -218,11 +219,10 @@ void ShowGenericPopup(const ShowGenericUiPopupProto& proto,
base::android::ConvertUTF8ToJavaString(env, proto.popup_identifier())); base::android::ConvertUTF8ToJavaString(env, proto.popup_identifier()));
} }
void SetViewText( void SetViewText(base::WeakPtr<UserModel> user_model,
base::WeakPtr<UserModel> user_model, const SetTextProto& proto,
const SetTextProto& proto, ViewHandlerAndroid* view_handler,
std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>* views, base::android::ScopedJavaGlobalRef<jobject> jdelegate) {
base::android::ScopedJavaGlobalRef<jobject> jdelegate) {
if (!user_model) { if (!user_model) {
return; return;
} }
...@@ -240,8 +240,8 @@ void SetViewText( ...@@ -240,8 +240,8 @@ void SetViewText(
return; return;
} }
auto jview = views->find(proto.view_identifier()); auto jview = view_handler->GetView(proto.view_identifier());
if (jview == views->end()) { if (!jview.has_value()) {
DVLOG(2) << "Failed to set text for " << proto.view_identifier() << ": " DVLOG(2) << "Failed to set text for " << proto.view_identifier() << ": "
<< " view not found"; << " view not found";
return; return;
...@@ -249,21 +249,20 @@ void SetViewText( ...@@ -249,21 +249,20 @@ void SetViewText(
JNIEnv* env = base::android::AttachCurrentThread(); JNIEnv* env = base::android::AttachCurrentThread();
Java_AssistantViewInteractions_setViewText( Java_AssistantViewInteractions_setViewText(
env, jview->second, env, *jview,
base::android::ConvertUTF8ToJavaString(env, text->strings().values(0)), base::android::ConvertUTF8ToJavaString(env, text->strings().values(0)),
jdelegate); jdelegate);
} }
void SetViewVisibility( void SetViewVisibility(base::WeakPtr<UserModel> user_model,
base::WeakPtr<UserModel> user_model, const SetViewVisibilityProto& proto,
const SetViewVisibilityProto& proto, ViewHandlerAndroid* view_handler) {
std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>* views) {
if (!user_model) { if (!user_model) {
return; return;
} }
auto jview = views->find(proto.view_identifier()); auto jview = view_handler->GetView(proto.view_identifier());
if (jview == views->end()) { if (!jview.has_value()) {
DVLOG(2) << "Failed to set view visibility for " << proto.view_identifier() DVLOG(2) << "Failed to set view visibility for " << proto.view_identifier()
<< ": view not found"; << ": view not found";
return; return;
...@@ -279,20 +278,19 @@ void SetViewVisibility( ...@@ -279,20 +278,19 @@ void SetViewVisibility(
JNIEnv* env = base::android::AttachCurrentThread(); JNIEnv* env = base::android::AttachCurrentThread();
Java_AssistantViewInteractions_setViewVisibility( Java_AssistantViewInteractions_setViewVisibility(
env, jview->second, env, *jview,
ui_controller_android_utils::ToJavaValue(env, *visible_value)); ui_controller_android_utils::ToJavaValue(env, *visible_value));
} }
void SetViewEnabled( void SetViewEnabled(base::WeakPtr<UserModel> user_model,
base::WeakPtr<UserModel> user_model, const SetViewEnabledProto& proto,
const SetViewEnabledProto& proto, ViewHandlerAndroid* view_handler) {
std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>* views) {
if (!user_model) { if (!user_model) {
return; return;
} }
auto jview = views->find(proto.view_identifier()); auto jview = view_handler->GetView(proto.view_identifier());
if (jview == views->end()) { if (!jview.has_value()) {
DVLOG(2) << "Failed to enable/disable view " << proto.view_identifier() DVLOG(2) << "Failed to enable/disable view " << proto.view_identifier()
<< ": view not found"; << ": view not found";
return; return;
...@@ -308,7 +306,7 @@ void SetViewEnabled( ...@@ -308,7 +306,7 @@ void SetViewEnabled(
JNIEnv* env = base::android::AttachCurrentThread(); JNIEnv* env = base::android::AttachCurrentThread();
Java_AssistantViewInteractions_setViewEnabled( Java_AssistantViewInteractions_setViewEnabled(
env, jview->second, env, *jview,
ui_controller_android_utils::ToJavaValue(env, *enabled_value)); ui_controller_android_utils::ToJavaValue(env, *enabled_value));
} }
...@@ -322,17 +320,16 @@ void RunConditionalCallback( ...@@ -322,17 +320,16 @@ void RunConditionalCallback(
basic_interactions->RunConditionalCallback(condition_identifier, callback); basic_interactions->RunConditionalCallback(condition_identifier, callback);
} }
void SetToggleButtonChecked( void SetToggleButtonChecked(base::WeakPtr<UserModel> user_model,
base::WeakPtr<UserModel> user_model, const std::string& view_identifier,
const std::string& view_identifier, const std::string& model_identifier,
const std::string& model_identifier, ViewHandlerAndroid* view_handler) {
std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>* views) {
if (!user_model) { if (!user_model) {
return; return;
} }
auto jview = views->find(view_identifier); auto jview = view_handler->GetView(view_identifier);
if (jview == views->end()) { if (!jview.has_value()) {
DVLOG(2) << "Failed to set toggle state for " << view_identifier DVLOG(2) << "Failed to set toggle state for " << view_identifier
<< ": view not found"; << ": view not found";
return; return;
...@@ -348,19 +345,18 @@ void SetToggleButtonChecked( ...@@ -348,19 +345,18 @@ void SetToggleButtonChecked(
JNIEnv* env = base::android::AttachCurrentThread(); JNIEnv* env = base::android::AttachCurrentThread();
if (!Java_AssistantViewInteractions_setToggleButtonChecked( if (!Java_AssistantViewInteractions_setToggleButtonChecked(
env, jview->second, env, *jview,
ui_controller_android_utils::ToJavaValue(env, *checked_value))) { ui_controller_android_utils::ToJavaValue(env, *checked_value))) {
DVLOG(2) << "Failed to set toggle state for " << view_identifier DVLOG(2) << "Failed to set toggle state for " << view_identifier
<< ": JNI call failed"; << ": JNI call failed";
} }
} }
void ClearViewContainer( void ClearViewContainer(const std::string& view_identifier,
const std::string& view_identifier, ViewHandlerAndroid* view_handler,
std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>* views, base::android::ScopedJavaGlobalRef<jobject> jdelegate) {
base::android::ScopedJavaGlobalRef<jobject> jdelegate) { auto jview = view_handler->GetView(view_identifier);
auto jview = views->find(view_identifier); if (!jview.has_value()) {
if (jview == views->end()) {
DVLOG(2) << "Failed to clear view container " << view_identifier DVLOG(2) << "Failed to clear view container " << view_identifier
<< ": view not found"; << ": view not found";
return; return;
...@@ -368,7 +364,7 @@ void ClearViewContainer( ...@@ -368,7 +364,7 @@ void ClearViewContainer(
JNIEnv* env = base::android::AttachCurrentThread(); JNIEnv* env = base::android::AttachCurrentThread();
if (!Java_AssistantViewInteractions_clearViewContainer( if (!Java_AssistantViewInteractions_clearViewContainer(
env, jview->second, env, *jview,
base::android::ConvertUTF8ToJavaString(env, view_identifier), base::android::ConvertUTF8ToJavaString(env, view_identifier),
jdelegate)) { jdelegate)) {
DVLOG(2) << "Failed to clear view container " << view_identifier DVLOG(2) << "Failed to clear view container " << view_identifier
...@@ -377,20 +373,19 @@ void ClearViewContainer( ...@@ -377,20 +373,19 @@ void ClearViewContainer(
} }
} }
bool AttachViewToParent( bool AttachViewToParent(base::android::ScopedJavaGlobalRef<jobject> jview,
base::android::ScopedJavaGlobalRef<jobject> jview, const std::string& parent_view_identifier,
const std::string& parent_view_identifier, ViewHandlerAndroid* view_handler) {
std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>* views) { auto jparent_view = view_handler->GetView(parent_view_identifier);
auto jparent_view = views->find(parent_view_identifier); if (!jparent_view.has_value()) {
if (jparent_view == views->end()) {
DVLOG(2) << "Failed to attach view to " << parent_view_identifier DVLOG(2) << "Failed to attach view to " << parent_view_identifier
<< ": parent not found"; << ": parent not found";
return false; return false;
} }
JNIEnv* env = base::android::AttachCurrentThread(); JNIEnv* env = base::android::AttachCurrentThread();
if (!Java_AssistantViewInteractions_attachViewToParent( if (!Java_AssistantViewInteractions_attachViewToParent(env, *jparent_view,
env, jparent_view->second, jview)) { jview)) {
DVLOG(2) << "Failed to attach view to " << parent_view_identifier DVLOG(2) << "Failed to attach view to " << parent_view_identifier
<< ": JNI call failed"; << ": JNI call failed";
return false; return false;
......
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
#include "components/autofill_assistant/browser/generic_ui.pb.h" #include "components/autofill_assistant/browser/generic_ui.pb.h"
namespace autofill_assistant { namespace autofill_assistant {
class ViewHandlerAndroid;
namespace android_interactions { namespace android_interactions {
// Writes a value to the model. // Writes a value to the model.
...@@ -61,23 +63,20 @@ void ShowGenericPopup(const ShowGenericUiPopupProto& proto, ...@@ -61,23 +63,20 @@ void ShowGenericPopup(const ShowGenericUiPopupProto& proto,
base::android::ScopedJavaGlobalRef<jobject> jdelegate); base::android::ScopedJavaGlobalRef<jobject> jdelegate);
// Sets the text of a view. // Sets the text of a view.
void SetViewText( void SetViewText(base::WeakPtr<UserModel> user_model,
base::WeakPtr<UserModel> user_model, const SetTextProto& proto,
const SetTextProto& proto, ViewHandlerAndroid* view_handler,
std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>* views, base::android::ScopedJavaGlobalRef<jobject> jdelegate);
base::android::ScopedJavaGlobalRef<jobject> jdelegate);
// Sets the visibility of a view. // Sets the visibility of a view.
void SetViewVisibility( void SetViewVisibility(base::WeakPtr<UserModel> user_model,
base::WeakPtr<UserModel> user_model, const SetViewVisibilityProto& proto,
const SetViewVisibilityProto& proto, ViewHandlerAndroid* view_handler);
std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>* views);
// Enables or disables a view. // Enables or disables a view.
void SetViewEnabled( void SetViewEnabled(base::WeakPtr<UserModel> user_model,
base::WeakPtr<UserModel> user_model, const SetViewEnabledProto& proto,
const SetViewEnabledProto& proto, ViewHandlerAndroid* view_handler);
std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>* views);
// A simple wrapper around a basic interaction, needed because we can't directly // A simple wrapper around a basic interaction, needed because we can't directly
// bind a repeating callback to a method with non-void return value. // bind a repeating callback to a method with non-void return value.
...@@ -87,23 +86,20 @@ void RunConditionalCallback( ...@@ -87,23 +86,20 @@ void RunConditionalCallback(
InteractionHandlerAndroid::InteractionCallback callback); InteractionHandlerAndroid::InteractionCallback callback);
// Sets the checked state of a toggle button. // Sets the checked state of a toggle button.
void SetToggleButtonChecked( void SetToggleButtonChecked(base::WeakPtr<UserModel> user_model,
base::WeakPtr<UserModel> user_model, const std::string& view_identifier,
const std::string& view_identifier, const std::string& model_identifier,
const std::string& model_identifier, ViewHandlerAndroid* view_handler);
std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>* views);
// Removes all child views from |view_identifier|. // Removes all child views from |view_identifier|.
void ClearViewContainer( void ClearViewContainer(const std::string& view_identifier,
const std::string& view_identifier, ViewHandlerAndroid* view_handler,
std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>* views, base::android::ScopedJavaGlobalRef<jobject> jdelegate);
base::android::ScopedJavaGlobalRef<jobject> jdelegate);
// Attaches |jview| to a parent view. // Attaches |jview| to a parent view.
bool AttachViewToParent( bool AttachViewToParent(base::android::ScopedJavaGlobalRef<jobject> jview,
base::android::ScopedJavaGlobalRef<jobject> jview, const std::string& parent_view_identifier,
const std::string& parent_view_identifier, ViewHandlerAndroid* view_handler);
std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>* views);
} // namespace android_interactions } // namespace android_interactions
} // namespace autofill_assistant } // namespace autofill_assistant
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "chrome/browser/android/autofill_assistant/generic_ui_controller_android.h" #include "chrome/browser/android/autofill_assistant/generic_ui_controller_android.h"
#include "chrome/browser/android/autofill_assistant/generic_ui_interactions_android.h" #include "chrome/browser/android/autofill_assistant/generic_ui_interactions_android.h"
#include "chrome/browser/android/autofill_assistant/view_handler_android.h"
#include "components/autofill_assistant/browser/basic_interactions.h" #include "components/autofill_assistant/browser/basic_interactions.h"
#include "components/autofill_assistant/browser/generic_ui.pb.h" #include "components/autofill_assistant/browser/generic_ui.pb.h"
#include "components/autofill_assistant/browser/ui_delegate.h" #include "components/autofill_assistant/browser/ui_delegate.h"
...@@ -24,7 +25,6 @@ namespace { ...@@ -24,7 +25,6 @@ namespace {
base::Optional<EventHandler::EventKey> CreateEventKeyFromProto( base::Optional<EventHandler::EventKey> CreateEventKeyFromProto(
const EventProto& proto, const EventProto& proto,
std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>* views,
base::android::ScopedJavaGlobalRef<jobject> jdelegate) { base::android::ScopedJavaGlobalRef<jobject> jdelegate) {
switch (proto.kind_case()) { switch (proto.kind_case()) {
case EventProto::kOnValueChanged: case EventProto::kOnValueChanged:
...@@ -89,13 +89,13 @@ InteractionHandlerAndroid::InteractionHandlerAndroid( ...@@ -89,13 +89,13 @@ InteractionHandlerAndroid::InteractionHandlerAndroid(
EventHandler* event_handler, EventHandler* event_handler,
UserModel* user_model, UserModel* user_model,
BasicInteractions* basic_interactions, BasicInteractions* basic_interactions,
std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>* views, ViewHandlerAndroid* view_handler,
base::android::ScopedJavaGlobalRef<jobject> jcontext, base::android::ScopedJavaGlobalRef<jobject> jcontext,
base::android::ScopedJavaGlobalRef<jobject> jdelegate) base::android::ScopedJavaGlobalRef<jobject> jdelegate)
: event_handler_(event_handler), : event_handler_(event_handler),
user_model_(user_model), user_model_(user_model),
basic_interactions_(basic_interactions), basic_interactions_(basic_interactions),
views_(views), view_handler_(view_handler),
jcontext_(jcontext), jcontext_(jcontext),
jdelegate_(jdelegate) {} jdelegate_(jdelegate) {}
...@@ -132,7 +132,7 @@ bool InteractionHandlerAndroid::AddInteractionsFromProto( ...@@ -132,7 +132,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 = CreateEventKeyFromProto(proto.trigger_event(), views_, jdelegate_); auto key = CreateEventKeyFromProto(proto.trigger_event(), jdelegate_);
if (!key) { if (!key) {
VLOG(1) << "Invalid trigger event for interaction"; VLOG(1) << "Invalid trigger event for interaction";
return false; return false;
...@@ -265,7 +265,7 @@ InteractionHandlerAndroid::CreateInteractionCallbackFromProto( ...@@ -265,7 +265,7 @@ InteractionHandlerAndroid::CreateInteractionCallbackFromProto(
} }
return base::Optional<InteractionCallback>(base::BindRepeating( return base::Optional<InteractionCallback>(base::BindRepeating(
&android_interactions::SetViewText, user_model_->GetWeakPtr(), &android_interactions::SetViewText, user_model_->GetWeakPtr(),
proto.set_text(), views_, jdelegate_)); proto.set_text(), view_handler_, jdelegate_));
case CallbackProto::kToggleUserAction: case CallbackProto::kToggleUserAction:
if (proto.toggle_user_action().user_actions_model_identifier().empty()) { if (proto.toggle_user_action().user_actions_model_identifier().empty()) {
VLOG(1) << "Error creating ToggleUserAction interaction: " VLOG(1) << "Error creating ToggleUserAction interaction: "
...@@ -298,7 +298,7 @@ InteractionHandlerAndroid::CreateInteractionCallbackFromProto( ...@@ -298,7 +298,7 @@ InteractionHandlerAndroid::CreateInteractionCallbackFromProto(
} }
return base::Optional<InteractionCallback>(base::BindRepeating( return base::Optional<InteractionCallback>(base::BindRepeating(
&android_interactions::SetViewVisibility, user_model_->GetWeakPtr(), &android_interactions::SetViewVisibility, user_model_->GetWeakPtr(),
proto.set_view_visibility(), views_)); proto.set_view_visibility(), view_handler_));
case CallbackProto::kSetViewEnabled: case CallbackProto::kSetViewEnabled:
if (proto.set_view_enabled().view_identifier().empty()) { if (proto.set_view_enabled().view_identifier().empty()) {
VLOG(1) << "Error creating SetViewEnabled interaction: " VLOG(1) << "Error creating SetViewEnabled interaction: "
...@@ -312,7 +312,7 @@ InteractionHandlerAndroid::CreateInteractionCallbackFromProto( ...@@ -312,7 +312,7 @@ InteractionHandlerAndroid::CreateInteractionCallbackFromProto(
} }
return base::Optional<InteractionCallback>(base::BindRepeating( return base::Optional<InteractionCallback>(base::BindRepeating(
&android_interactions::SetViewEnabled, user_model_->GetWeakPtr(), &android_interactions::SetViewEnabled, user_model_->GetWeakPtr(),
proto.set_view_enabled(), views_)); proto.set_view_enabled(), view_handler_));
case CallbackProto::kShowGenericPopup: case CallbackProto::kShowGenericPopup:
if (proto.show_generic_popup().popup_identifier().empty()) { if (proto.show_generic_popup().popup_identifier().empty()) {
VLOG(1) << "Error creating ShowGenericPopup interaction: " VLOG(1) << "Error creating ShowGenericPopup interaction: "
...@@ -337,9 +337,10 @@ InteractionHandlerAndroid::CreateInteractionCallbackFromProto( ...@@ -337,9 +337,10 @@ InteractionHandlerAndroid::CreateInteractionCallbackFromProto(
"view_identifier not set"; "view_identifier not set";
return base::nullopt; return base::nullopt;
} }
return base::Optional<InteractionCallback>(base::BindRepeating( return base::Optional<InteractionCallback>(
&android_interactions::ClearViewContainer, base::BindRepeating(&android_interactions::ClearViewContainer,
proto.clear_view_container().view_identifier(), views_, jdelegate_)); proto.clear_view_container().view_identifier(),
view_handler_, jdelegate_));
case CallbackProto::KIND_NOT_SET: case CallbackProto::KIND_NOT_SET:
VLOG(1) << "Error creating interaction: kind not set"; VLOG(1) << "Error creating interaction: kind not set";
return base::nullopt; return base::nullopt;
...@@ -383,8 +384,9 @@ void InteractionHandlerAndroid::CreateAndAttachNestedGenericUi( ...@@ -383,8 +384,9 @@ void InteractionHandlerAndroid::CreateAndAttachNestedGenericUi(
return; return;
} }
if (!android_interactions::AttachViewToParent( if (!android_interactions::AttachViewToParent(nested_ui->GetRootView(),
nested_ui->GetRootView(), proto.parent_view_identifier(), views_)) { proto.parent_view_identifier(),
view_handler_)) {
DeleteNestedUi(proto.generic_ui_identifier()); DeleteNestedUi(proto.generic_ui_identifier());
return; return;
} }
......
...@@ -22,6 +22,7 @@ namespace autofill_assistant { ...@@ -22,6 +22,7 @@ namespace autofill_assistant {
class BasicInteractions; class BasicInteractions;
class GenericUiControllerAndroid; class GenericUiControllerAndroid;
class UserModel; class UserModel;
class ViewHandlerAndroid;
// Receives incoming events and runs the corresponding set of callbacks. // Receives incoming events and runs the corresponding set of callbacks.
// //
...@@ -33,13 +34,12 @@ class InteractionHandlerAndroid : public EventHandler::Observer { ...@@ -33,13 +34,12 @@ class InteractionHandlerAndroid : public EventHandler::Observer {
public: public:
using InteractionCallback = base::RepeatingCallback<void()>; using InteractionCallback = base::RepeatingCallback<void()>;
// Constructor. |event_handler|, |user_model|, |basic_interactions|, // Constructor. All dependencies must outlive this instance.
// |views|, |jcontext| and |jdelegate| must outlive this instance.
InteractionHandlerAndroid( InteractionHandlerAndroid(
EventHandler* event_handler, EventHandler* event_handler,
UserModel* user_model, UserModel* user_model,
BasicInteractions* basic_interactions, BasicInteractions* basic_interactions,
std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>* views, ViewHandlerAndroid* view_handler,
base::android::ScopedJavaGlobalRef<jobject> jcontext, base::android::ScopedJavaGlobalRef<jobject> jcontext,
base::android::ScopedJavaGlobalRef<jobject> jdelegate); base::android::ScopedJavaGlobalRef<jobject> jdelegate);
~InteractionHandlerAndroid() override; ~InteractionHandlerAndroid() override;
...@@ -101,7 +101,7 @@ class InteractionHandlerAndroid : public EventHandler::Observer { ...@@ -101,7 +101,7 @@ class InteractionHandlerAndroid : public EventHandler::Observer {
EventHandler* event_handler_ = nullptr; EventHandler* event_handler_ = nullptr;
UserModel* user_model_ = nullptr; UserModel* user_model_ = nullptr;
BasicInteractions* basic_interactions_ = nullptr; BasicInteractions* basic_interactions_ = nullptr;
std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>* views_; ViewHandlerAndroid* view_handler_;
base::android::ScopedJavaGlobalRef<jobject> jcontext_ = nullptr; base::android::ScopedJavaGlobalRef<jobject> jcontext_ = nullptr;
base::android::ScopedJavaGlobalRef<jobject> jdelegate_ = nullptr; base::android::ScopedJavaGlobalRef<jobject> jdelegate_ = nullptr;
bool is_listening_ = false; bool is_listening_ = false;
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/android/autofill_assistant/view_handler_android.h"
namespace autofill_assistant {
ViewHandlerAndroid::ViewHandlerAndroid() = default;
ViewHandlerAndroid::~ViewHandlerAndroid() = default;
base::Optional<base::android::ScopedJavaGlobalRef<jobject>>
ViewHandlerAndroid::GetView(const std::string& view_identifier) const {
auto it = views_.find(view_identifier);
if (it == views_.end()) {
return base::nullopt;
}
return it->second;
}
// Adds a view to the set of managed views.
void ViewHandlerAndroid::AddView(
const std::string& view_identifier,
base::android::ScopedJavaGlobalRef<jobject> jview) {
DCHECK(views_.find(view_identifier) == views_.end());
views_.emplace(view_identifier, jview);
}
} // namespace autofill_assistant
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_ANDROID_AUTOFILL_ASSISTANT_VIEW_HANDLER_ANDROID_H_
#define CHROME_BROWSER_ANDROID_AUTOFILL_ASSISTANT_VIEW_HANDLER_ANDROID_H_
#include <map>
#include <memory>
#include <string>
#include "base/android/jni_android.h"
#include "base/optional.h"
namespace autofill_assistant {
// Manages a map of view-identifier -> android view instances.
class ViewHandlerAndroid {
public:
ViewHandlerAndroid();
~ViewHandlerAndroid();
ViewHandlerAndroid(const ViewHandlerAndroid&) = delete;
ViewHandlerAndroid& operator=(const ViewHandlerAndroid&) = delete;
// Returns the view associated with |view_identifier| or base::nullopt if
// there is no such view.
base::Optional<base::android::ScopedJavaGlobalRef<jobject>> GetView(
const std::string& view_identifier) const;
// Adds a view to the set of managed views.
void AddView(const std::string& view_identifier,
base::android::ScopedJavaGlobalRef<jobject> jview);
private:
std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>> views_;
};
} // namespace autofill_assistant
#endif // CHROME_BROWSER_ANDROID_AUTOFILL_ASSISTANT_VIEW_HANDLER_ANDROID_H_
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