This is the cherry pick merge for the M73 branch which was approved here: http://crbug.com/926893

[Autofill Assistant] WebContents null bugfix.

Before this patch we initialized AA and its UI via the webContents from
the currently active tab. A current tab is not immediately set and we
were using a null webContents object.

This patch uses the existing tab instead of a "current" tab instead. AA
no longer crashes when a payment request UI is shown.

Bug: 806868
Issue: b/123338819
Change-Id: Ibf14792b43dcd317f99236bc5368215de8f9f4c1
Reviewed-on: https://chromium-review.googlesource.com/c/1437077
Reviewed-by: Stephane Zermatten <szermatt@chromium.org>
Commit-Queue: Mathias Carlen <mcarlen@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#626470}(cherry picked from commit ba7338d997a445466f014ac3cf827cde8a047a1b)
Reviewed-on: https://chromium-review.googlesource.com/c/1448183
Cr-Commit-Position: refs/branch-heads/3683@{#90}
Cr-Branched-From: e51029943e0a38dd794b73caaf6373d5496ae783-refs/heads/master@{#625896}
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/autofill_assistant/AssistantBottomBarCoordinator.java b/chrome/android/java/src/org/chromium/chrome/browser/autofill_assistant/AssistantBottomBarCoordinator.java
index cb82257..046e227 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/autofill_assistant/AssistantBottomBarCoordinator.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/autofill_assistant/AssistantBottomBarCoordinator.java
@@ -17,6 +17,7 @@
 import org.chromium.chrome.browser.autofill_assistant.details.AssistantDetailsCoordinator;
 import org.chromium.chrome.browser.autofill_assistant.header.AssistantHeaderCoordinator;
 import org.chromium.chrome.browser.autofill_assistant.payment.AssistantPaymentRequestCoordinator;
+import org.chromium.content_public.browser.WebContents;
 
 /**
  * Coordinator responsible for the Autofill Assistant bottom bar. This coordinator allows to enable
@@ -50,8 +51,8 @@
     private final AssistantPaymentRequestCoordinator mPaymentRequestCoordinator;
     private final AssistantCarouselCoordinator mCarouselCoordinator;
 
-    AssistantBottomBarCoordinator(
-            ChromeActivity activity, View assistantView, AssistantModel model) {
+    AssistantBottomBarCoordinator(ChromeActivity activity, WebContents webContents,
+            View assistantView, AssistantModel model) {
         mBottomBarView = assistantView.findViewById(
                 org.chromium.chrome.autofill_assistant.R.id.autofill_assistant_bottombar);
         mSwipeIndicatorView = mBottomBarView.findViewById(
@@ -73,7 +74,7 @@
         mHeaderCoordinator =
                 new AssistantHeaderCoordinator(activity, mBottomBarView, model.getHeaderModel());
         mDetailsCoordinator = new AssistantDetailsCoordinator(activity, model.getDetailsModel());
-        mPaymentRequestCoordinator = new AssistantPaymentRequestCoordinator(activity);
+        mPaymentRequestCoordinator = new AssistantPaymentRequestCoordinator(activity, webContents);
         mCarouselCoordinator = new AssistantCarouselCoordinator(activity, model.getCarouselModel());
 
         // Add child views to bottom bar.
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/autofill_assistant/AssistantCoordinator.java b/chrome/android/java/src/org/chromium/chrome/browser/autofill_assistant/AssistantCoordinator.java
index ed014c8..828ef20 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/autofill_assistant/AssistantCoordinator.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/autofill_assistant/AssistantCoordinator.java
@@ -15,6 +15,7 @@
 import org.chromium.chrome.browser.profiles.Profile;
 import org.chromium.chrome.browser.snackbar.Snackbar;
 import org.chromium.chrome.browser.snackbar.SnackbarManager;
+import org.chromium.content_public.browser.WebContents;
 
 /**
  * The main coordinator for the Autofill Assistant, responsible for instantiating all other
@@ -56,7 +57,7 @@
 
     private boolean mIsShuttingDownGracefully;
 
-    AssistantCoordinator(ChromeActivity activity, Delegate delegate) {
+    AssistantCoordinator(ChromeActivity activity, WebContents webContents, Delegate delegate) {
         mActivity = activity;
         mDelegate = delegate;
         mModel = new AssistantModel();
@@ -69,7 +70,7 @@
 
         // Instantiate child components.
         mBottomBarCoordinator =
-                new AssistantBottomBarCoordinator(mActivity, mAssistantView, mModel);
+                new AssistantBottomBarCoordinator(activity, webContents, mAssistantView, mModel);
         mKeyboardCoordinator = new AssistantKeyboardCoordinator(activity);
         mOverlayCoordinator = new AssistantOverlayCoordinator(activity, mAssistantView, this);
 
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/autofill_assistant/AutofillAssistantFacade.java b/chrome/android/java/src/org/chromium/chrome/browser/autofill_assistant/AutofillAssistantFacade.java
index fa34574..a2304a5 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/autofill_assistant/AutofillAssistantFacade.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/autofill_assistant/AutofillAssistantFacade.java
@@ -58,12 +58,12 @@
     /** Starts Autofill Assistant on the given {@code activity}. */
     public static void start(ChromeActivity activity) {
         if (canStart(activity.getInitialIntent())) {
-            getCurrentTab(activity, tab -> startNow(activity, tab));
+            getTab(activity, tab -> startNow(activity, tab));
             return;
         }
 
         if (AutofillAssistantPreferencesUtil.getShowOnboarding()) {
-            getCurrentTab(activity, tab -> {
+            getTab(activity, tab -> {
                 AutofillAssistantClient client =
                         AutofillAssistantClient.fromWebContents(tab.getWebContents());
                 client.showOnboarding(() -> startNow(activity, tab));
@@ -82,8 +82,9 @@
         client.start(initialUrl, parameters, activity.getInitialIntent().getExtras());
     }
 
-    private static void getCurrentTab(ChromeActivity activity, Callback<Tab> callback) {
-        if (activity.getActivityTab() != null) {
+    private static void getTab(ChromeActivity activity, Callback<Tab> callback) {
+        if (activity.getActivityTab() != null
+                && activity.getActivityTab().getWebContents() != null) {
             callback.onResult(activity.getActivityTab());
             return;
         }
@@ -95,6 +96,7 @@
                     public void onActivityTabChanged(Tab tab) {
                         if (tab == null) return;
                         activity.getActivityTabProvider().removeObserver(this);
+                        assert tab.getWebContents() != null;
                         callback.onResult(tab);
                     }
                 });
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/autofill_assistant/AutofillAssistantUiController.java b/chrome/android/java/src/org/chromium/chrome/browser/autofill_assistant/AutofillAssistantUiController.java
index 6abd68f..23bcbf4 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/autofill_assistant/AutofillAssistantUiController.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/autofill_assistant/AutofillAssistantUiController.java
@@ -43,12 +43,13 @@
     private static AutofillAssistantUiController createAndStartUi(
             WebContents webContents, long nativeUiController) {
         return new AutofillAssistantUiController(
-                ChromeActivity.fromWebContents(webContents), nativeUiController);
+                ChromeActivity.fromWebContents(webContents), webContents, nativeUiController);
     }
 
-    private AutofillAssistantUiController(ChromeActivity activity, long nativeUiController) {
+    private AutofillAssistantUiController(
+            ChromeActivity activity, WebContents webContents, long nativeUiController) {
         mNativeUiController = nativeUiController;
-        mCoordinator = new AssistantCoordinator(activity, this);
+        mCoordinator = new AssistantCoordinator(activity, webContents, this);
 
         initForCustomTab(activity);
     }
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/autofill_assistant/payment/AssistantPaymentRequestCoordinator.java b/chrome/android/java/src/org/chromium/chrome/browser/autofill_assistant/payment/AssistantPaymentRequestCoordinator.java
index c7b5682..9be5c33 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/autofill_assistant/payment/AssistantPaymentRequestCoordinator.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/autofill_assistant/payment/AssistantPaymentRequestCoordinator.java
@@ -28,8 +28,9 @@
 
     private Promise<SelectedPaymentInformation> mCurrentPromise;
 
-    public AssistantPaymentRequestCoordinator(ChromeActivity activity) {
-        mWebContents = activity.getCurrentWebContents();
+    public AssistantPaymentRequestCoordinator(ChromeActivity activity, WebContents webContents) {
+        mWebContents = webContents;
+        assert webContents != null;
 
         // TODO(crbug.com/806868): Remove this.
         mView = new LinearLayout(activity);
diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/autofill_assistant/AutofillAssistantUiTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/autofill_assistant/AutofillAssistantUiTest.java
index f006648..b8a1c34 100644
--- a/chrome/android/javatests/src/org/chromium/chrome/browser/autofill_assistant/AutofillAssistantUiTest.java
+++ b/chrome/android/javatests/src/org/chromium/chrome/browser/autofill_assistant/AutofillAssistantUiTest.java
@@ -108,7 +108,10 @@
 
         mCustomTabActivityTestRule.startCustomTabActivityWithIntent(createMinimalCustomTabIntent());
         AssistantCoordinator assistantCoordinator = ThreadUtils.runOnUiThreadBlocking(
-                () -> new AssistantCoordinator(getActivity(), mCoordinatorDelegateMock));
+                ()
+                        -> new AssistantCoordinator(getActivity(),
+                                mCustomTabActivityTestRule.getWebContents(),
+                                mCoordinatorDelegateMock));
 
         // Bottom sheet is shown when creating the AssistantCoordinator.
         View bottomSheet = findViewByIdInMainCoordinator(R.id.autofill_assistant);