[Autofill Assistant] Refactored interaction handler dependencies.

All those parameters are in fact dependencies of the interaction
handler. Thus, it makes sense to inject them on creation. This also cuts
down on the number of parameters needed for subsequent calls.

This CL also refactores the passing of the java context. Since the
interaction handler needs to retain a reference, a global reference is
passed. All other calls will continue to use local references.

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

Bug: b/145043394
Change-Id: Id2a62944a584cfbe3c089a5c7f76166fc67ec574
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2120713
Reviewed-by: Marian Fechete <marianfe@google.com>
Commit-Queue: Clemens Arbesser <arbesser@google.com>
Cr-Commit-Position: refs/heads/master@{#753563}
diff --git a/chrome/browser/android/autofill_assistant/generic_ui_controller_android.cc b/chrome/browser/android/autofill_assistant/generic_ui_controller_android.cc
index ae68f8b..c612067 100644
--- a/chrome/browser/android/autofill_assistant/generic_ui_controller_android.cc
+++ b/chrome/browser/android/autofill_assistant/generic_ui_controller_android.cc
@@ -229,7 +229,7 @@
 std::unique_ptr<GenericUiControllerAndroid>
 GenericUiControllerAndroid::CreateFromProto(
     const GenericUserInterfaceProto& proto,
-    base::android::ScopedJavaLocalRef<jobject> jcontext,
+    base::android::ScopedJavaGlobalRef<jobject> jcontext,
     base::android::ScopedJavaGlobalRef<jobject> jdelegate,
     EventHandler* event_handler,
     UserModel* user_model,
@@ -238,17 +238,18 @@
   JNIEnv* env = base::android::AttachCurrentThread();
   auto views = std::make_unique<
       std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>>();
-  auto jroot_view = proto.has_root_view()
-                        ? CreateJavaView(env, jcontext, jdelegate,
-                                         proto.root_view(), views.get())
-                        : nullptr;
+  auto jroot_view =
+      proto.has_root_view()
+          ? CreateJavaView(env,
+                           base::android::ScopedJavaLocalRef<jobject>(jcontext),
+                           jdelegate, proto.root_view(), views.get())
+          : nullptr;
 
   // Create interactions.
-  auto interaction_handler =
-      std::make_unique<InteractionHandlerAndroid>(event_handler, jcontext);
-  if (!interaction_handler->AddInteractionsFromProto(
-          proto.interactions(), env, views.get(), jdelegate, user_model,
-          basic_interactions)) {
+  auto interaction_handler = std::make_unique<InteractionHandlerAndroid>(
+      event_handler, user_model, basic_interactions, views.get(), jcontext,
+      jdelegate);
+  if (!interaction_handler->AddInteractionsFromProto(proto.interactions())) {
     return nullptr;
   }
 
diff --git a/chrome/browser/android/autofill_assistant/generic_ui_controller_android.h b/chrome/browser/android/autofill_assistant/generic_ui_controller_android.h
index 3ddeba6..0af4949 100644
--- a/chrome/browser/android/autofill_assistant/generic_ui_controller_android.h
+++ b/chrome/browser/android/autofill_assistant/generic_ui_controller_android.h
@@ -27,8 +27,8 @@
   // Ownership of the arguments is not changed.
   static std::unique_ptr<GenericUiControllerAndroid> CreateFromProto(
       const GenericUserInterfaceProto& proto,
-      base::android::ScopedJavaLocalRef<jobject> jcontext,
-      base::android::ScopedJavaGlobalRef<jobject> delegate,
+      base::android::ScopedJavaGlobalRef<jobject> jcontext,
+      base::android::ScopedJavaGlobalRef<jobject> jdelegate,
       EventHandler* event_handler,
       UserModel* user_model,
       BasicInteractions* basic_interactions);
diff --git a/chrome/browser/android/autofill_assistant/interaction_handler_android.cc b/chrome/browser/android/autofill_assistant/interaction_handler_android.cc
index 071c124..d282b52 100644
--- a/chrome/browser/android/autofill_assistant/interaction_handler_android.cc
+++ b/chrome/browser/android/autofill_assistant/interaction_handler_android.cc
@@ -203,11 +203,17 @@
 
 InteractionHandlerAndroid::InteractionHandlerAndroid(
     EventHandler* event_handler,
-    base::android::ScopedJavaLocalRef<jobject> jcontext)
-    : event_handler_(event_handler) {
-  DCHECK(jcontext);
-  jcontext_ = base::android::ScopedJavaGlobalRef<jobject>(jcontext);
-}
+    UserModel* user_model,
+    BasicInteractions* basic_interactions,
+    std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>* views,
+    base::android::ScopedJavaGlobalRef<jobject> jcontext,
+    base::android::ScopedJavaGlobalRef<jobject> jdelegate)
+    : event_handler_(event_handler),
+      user_model_(user_model),
+      basic_interactions_(basic_interactions),
+      views_(views),
+      jcontext_(jcontext),
+      jdelegate_(jdelegate) {}
 
 InteractionHandlerAndroid::~InteractionHandlerAndroid() {
   event_handler_->RemoveObserver(this);
@@ -224,19 +230,15 @@
 }
 
 bool InteractionHandlerAndroid::AddInteractionsFromProto(
-    const InteractionsProto& proto,
-    JNIEnv* env,
-    std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>* views,
-    base::android::ScopedJavaGlobalRef<jobject> jdelegate,
-    UserModel* user_model,
-    BasicInteractions* basic_interactions) {
+    const InteractionsProto& proto) {
   if (is_listening_) {
     NOTREACHED() << "Interactions can not be added while listening to events!";
     return false;
   }
+  JNIEnv* env = base::android::AttachCurrentThread();
   for (const auto& interaction_proto : proto.interactions()) {
     auto key = CreateEventKeyFromProto(interaction_proto.trigger_event(), env,
-                                       views, jdelegate);
+                                       views_, jdelegate_);
     if (!key) {
       VLOG(1) << "Invalid trigger event for interaction";
       return false;
@@ -244,8 +246,8 @@
 
     for (const auto& callback_proto : interaction_proto.callbacks()) {
       auto callback = CreateInteractionCallbackFromProto(
-          callback_proto, user_model, basic_interactions, views, jcontext_,
-          jdelegate);
+          callback_proto, user_model_, basic_interactions_, views_, jcontext_,
+          jdelegate_);
       if (!callback) {
         VLOG(1) << "Invalid callback for interaction";
         return false;
@@ -255,7 +257,7 @@
         callback =
             base::Optional<InteractionHandlerAndroid::InteractionCallback>(
                 base::BindRepeating(&TryRunConditionalCallback,
-                                    basic_interactions->GetWeakPtr(),
+                                    basic_interactions_->GetWeakPtr(),
                                     callback_proto.condition_model_identifier(),
                                     *callback));
       }
diff --git a/chrome/browser/android/autofill_assistant/interaction_handler_android.h b/chrome/browser/android/autofill_assistant/interaction_handler_android.h
index 115fc07..17b126e 100644
--- a/chrome/browser/android/autofill_assistant/interaction_handler_android.h
+++ b/chrome/browser/android/autofill_assistant/interaction_handler_android.h
@@ -32,27 +32,25 @@
  public:
   using InteractionCallback = base::RepeatingCallback<void()>;
 
-  // Constructor. |event_handler| and |jcontext| must outlive this instance.
+  // Constructor. |event_handler|, |user_model|, |basic_interactions|,
+  // |views|, |jcontext| and |jdelegate| must outlive this instance.
   InteractionHandlerAndroid(
       EventHandler* event_handler,
-      base::android::ScopedJavaLocalRef<jobject> jcontext);
+      UserModel* user_model,
+      BasicInteractions* basic_interactions,
+      std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>* views,
+      base::android::ScopedJavaGlobalRef<jobject> jcontext,
+      base::android::ScopedJavaGlobalRef<jobject> jdelegate);
   ~InteractionHandlerAndroid() override;
 
   void StartListening();
   void StopListening();
 
-  // Creates callbacks for each interaction in |proto| as well as the
-  // corresponding view events in |views|. Returns false if |proto| is invalid.
-  // |views| must outlive this interaction handler.
-  bool AddInteractionsFromProto(
-      const InteractionsProto& proto,
-      JNIEnv* env,
-      std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>* views,
-      base::android::ScopedJavaGlobalRef<jobject> jdelegate,
-      UserModel* user_model,
-      BasicInteractions* basic_interactions);
+  // Creates callbacks for each interaction in |proto|. Returns false if |proto|
+  // is invalid.
+  bool AddInteractionsFromProto(const InteractionsProto& proto);
 
-  // Overrides autofill_assistant::EventHandler::Observer:
+  // Overrides autofill_assistant::EventHandler::Observer.
   void OnEvent(const EventHandler::EventKey& key) override;
 
  private:
@@ -64,7 +62,11 @@
       interactions_;
 
   EventHandler* event_handler_ = nullptr;
+  UserModel* user_model_ = nullptr;
+  BasicInteractions* basic_interactions_ = nullptr;
+  std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>* views_;
   base::android::ScopedJavaGlobalRef<jobject> jcontext_ = nullptr;
+  base::android::ScopedJavaGlobalRef<jobject> jdelegate_ = nullptr;
   bool is_listening_ = false;
   DISALLOW_COPY_AND_ASSIGN(InteractionHandlerAndroid);
 };
diff --git a/chrome/browser/android/autofill_assistant/ui_controller_android.cc b/chrome/browser/android/autofill_assistant/ui_controller_android.cc
index 242dcd5..589a3fc2 100644
--- a/chrome/browser/android/autofill_assistant/ui_controller_android.cc
+++ b/chrome/browser/android/autofill_assistant/ui_controller_android.cc
@@ -1645,9 +1645,9 @@
   auto jcontext =
       Java_AutofillAssistantUiController_getContext(env, java_object_);
   return GenericUiControllerAndroid::CreateFromProto(
-      proto, jcontext, generic_ui_delegate_.GetJavaObject(),
-      ui_delegate_->GetEventHandler(), ui_delegate_->GetUserModel(),
-      ui_delegate_->GetBasicInteractions());
+      proto, base::android::ScopedJavaGlobalRef<jobject>(jcontext),
+      generic_ui_delegate_.GetJavaObject(), ui_delegate_->GetEventHandler(),
+      ui_delegate_->GetUserModel(), ui_delegate_->GetBasicInteractions());
 }
 
 base::android::ScopedJavaLocalRef<jobject>