[Autofill Assistant] Remove the profile cache data observers.

Before this patch, we kept adding new observers to the list of cache
data observers when closing the app (without the app being destroyed).
Resuming the activity by triggering another run would reuse the existing
profile cache and the default profile image was shown.

This patch introduces a destroy() function to the header coordinator so
that it can clean up internals when we go out of scope. In particular it
removes the profile data observer registered in the constructor.

Bug: b/132379462
Change-Id: I96c473b6748bd8b7105cab58bfdb497411e6f93c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1609779
Commit-Queue: Mathias Carlen <mcarlen@chromium.org>
Reviewed-by: Clemens Arbesser <arbesser@google.com>
Cr-Commit-Position: refs/heads/master@{#661323}
diff --git a/chrome/android/features/autofill_assistant/java/src/org/chromium/chrome/browser/autofill_assistant/AssistantBottomBarCoordinator.java b/chrome/android/features/autofill_assistant/java/src/org/chromium/chrome/browser/autofill_assistant/AssistantBottomBarCoordinator.java
index f7425f3..81f5241 100644
--- a/chrome/android/features/autofill_assistant/java/src/org/chromium/chrome/browser/autofill_assistant/AssistantBottomBarCoordinator.java
+++ b/chrome/android/features/autofill_assistant/java/src/org/chromium/chrome/browser/autofill_assistant/AssistantBottomBarCoordinator.java
@@ -169,6 +169,7 @@
         mInfoBoxCoordinator = null;
         mPaymentRequestCoordinator.destroy();
         mPaymentRequestCoordinator = null;
+        mHeaderCoordinator.destroy();
     }
 
     /**
diff --git a/chrome/android/features/autofill_assistant/java/src/org/chromium/chrome/browser/autofill_assistant/header/AssistantHeaderCoordinator.java b/chrome/android/features/autofill_assistant/java/src/org/chromium/chrome/browser/autofill_assistant/header/AssistantHeaderCoordinator.java
index f8dc51f..0ee8c5e 100644
--- a/chrome/android/features/autofill_assistant/java/src/org/chromium/chrome/browser/autofill_assistant/header/AssistantHeaderCoordinator.java
+++ b/chrome/android/features/autofill_assistant/java/src/org/chromium/chrome/browser/autofill_assistant/header/AssistantHeaderCoordinator.java
@@ -20,8 +20,10 @@
 /**
  * Coordinator for the header of the Autofill Assistant.
  */
-public class AssistantHeaderCoordinator {
-    private ProfileDataCache mProfileCache;
+public class AssistantHeaderCoordinator implements ProfileDataCache.Observer {
+    private final ProfileDataCache mProfileCache;
+    private final ImageView mProfileView;
+    private final String mSignedInAccountName;
 
     public AssistantHeaderCoordinator(
             Context context, ViewGroup bottomBarView, AssistantHeaderModel model) {
@@ -36,7 +38,9 @@
         addPoodle(bottomBarView, poodle.getView());
 
         mProfileCache = new ProfileDataCache(context, R.dimen.autofill_assistant_profile_size);
-        setupProfileImage(bottomBarView);
+        mProfileView = bottomBarView.findViewById(R.id.profile_image);
+        mSignedInAccountName = ChromeSigninController.get().getSignedInAccountName();
+        setupProfileImage(context, bottomBarView);
 
         // Bind view and mediator through the model.
         AssistantHeaderViewBinder.ViewHolder viewHolder =
@@ -48,6 +52,23 @@
         model.set(AssistantHeaderModel.PROGRESS_VISIBLE, true);
     }
 
+    @Override
+    public void onProfileDataUpdated(String account) {
+        if (!mSignedInAccountName.equals(account)) {
+            return;
+        }
+        setProfileImageFor(mSignedInAccountName);
+    }
+
+    /**
+     * Cleanup resources when this goes out of scope.
+     */
+    public void destroy() {
+        if (mSignedInAccountName != null) {
+            mProfileCache.removeObserver(this);
+        }
+    }
+
     private void addPoodle(ViewGroup root, View poodleView) {
         View statusMessage = root.findViewById(R.id.status_message);
         ViewGroup parent = (ViewGroup) statusMessage.getParent();
@@ -55,19 +76,16 @@
     }
 
     // TODO(b/130415092): Use image from AGSA if chrome is not signed in.
-    private void setupProfileImage(ViewGroup root) {
-        String signedInAccountName = ChromeSigninController.get().getSignedInAccountName();
-        if (signedInAccountName != null) {
-            mProfileCache.addObserver(account -> {
-                if (!signedInAccountName.equals(account)) {
-                    return;
-                }
-                DisplayableProfileData profileData =
-                        mProfileCache.getProfileDataOrDefault(signedInAccountName);
-                ImageView profileView = root.findViewById(R.id.profile_image);
-                profileView.setImageDrawable(profileData.getImage());
-            });
-            mProfileCache.update(Collections.singletonList(signedInAccountName));
+    private void setupProfileImage(Context context, ViewGroup root) {
+        if (mSignedInAccountName != null) {
+            mProfileCache.addObserver(this);
+            mProfileCache.update(Collections.singletonList(mSignedInAccountName));
         }
     }
+
+    private void setProfileImageFor(String signedInAccountName) {
+        DisplayableProfileData profileData =
+                mProfileCache.getProfileDataOrDefault(signedInAccountName);
+        mProfileView.setImageDrawable(profileData.getImage());
+    }
 }