Refactor handleMenuItemClick into a non static method

This allows other SelectionActionMenuDelegate implementations to handle
menu item clicks if they would like to.

Bug: 413195996, 445155873
Change-Id: Ie9b600870cf591b925ba9d35413a8dc1665ace17
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6885064
Reviewed-by: Peter Beverloo <peter@chromium.org>
Commit-Queue: Alex Mitra <alexmitra@chromium.org>
Reviewed-by: Ted Choc <tedchoc@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1519372}
diff --git a/android_webview/java/src/org/chromium/android_webview/AwActionModeCallback.java b/android_webview/java/src/org/chromium/android_webview/AwActionModeCallback.java
index 496511d..7f0e004 100644
--- a/android_webview/java/src/org/chromium/android_webview/AwActionModeCallback.java
+++ b/android_webview/java/src/org/chromium/android_webview/AwActionModeCallback.java
@@ -21,7 +21,6 @@
 import androidx.annotation.Nullable;
 
 import org.chromium.android_webview.common.Lifetime;
-import org.chromium.android_webview.selection.SamsungSelectionActionMenuDelegate;
 import org.chromium.base.ContextUtils;
 import org.chromium.base.PackageManagerUtils;
 import org.chromium.base.metrics.RecordUserAction;
@@ -29,6 +28,7 @@
 import org.chromium.content_public.browser.ActionModeCallbackHelper;
 import org.chromium.content_public.browser.SelectionPopupController;
 import org.chromium.content_public.browser.WebContents;
+import org.chromium.content_public.browser.selection.SelectionActionMenuDelegate;
 import org.chromium.ui.base.WindowAndroid.IntentCallback;
 
 /** A class that handles selection action mode for Android WebView. */
@@ -37,13 +37,19 @@
     private final Context mContext;
     private final AwContents mAwContents;
     private final SelectionPopupController mSelectionPopupController;
+    private final SelectionActionMenuDelegate mDelegate;
     private final ActionModeCallbackHelper mHelper;
     private int mAllowedMenuItems;
 
-    public AwActionModeCallback(Context context, AwContents awContents, WebContents webContents) {
+    public AwActionModeCallback(
+            Context context,
+            AwContents awContents,
+            WebContents webContents,
+            SelectionActionMenuDelegate delegate) {
         mContext = context;
         mAwContents = awContents;
         mSelectionPopupController = SelectionPopupController.fromWebContents(webContents);
+        mDelegate = delegate;
         mHelper = mSelectionPopupController.getActionModeCallbackHelper();
         mHelper.setAllowedMenuItems(0); // No item is allowed by default for WebView.
     }
@@ -94,7 +100,7 @@
         }
 
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU
-                && SamsungSelectionActionMenuDelegate.handleMenuItemClick(
+                && mDelegate.handleMenuItemClick(
                         item, mAwContents.getWebContents(), mAwContents.getContainerView())) {
             return true;
         }
diff --git a/android_webview/java/src/org/chromium/android_webview/AwContents.java b/android_webview/java/src/org/chromium/android_webview/AwContents.java
index bed0c3e..312db02 100644
--- a/android_webview/java/src/org/chromium/android_webview/AwContents.java
+++ b/android_webview/java/src/org/chromium/android_webview/AwContents.java
@@ -1117,7 +1117,8 @@
         mViewEventSink = ViewEventSink.from(mWebContents);
         mViewEventSink.setHideKeyboardOnBlur(false);
         SelectionPopupController controller = SelectionPopupController.fromWebContents(webContents);
-        controller.setActionModeCallback(new AwActionModeCallback(mContext, this, webContents));
+        controller.setActionModeCallback(
+                new AwActionModeCallback(mContext, this, webContents, selectionActionMenuDelegate));
         controller.setSelectionClient(SelectionClient.createSmartSelectionClient(webContents));
         controller.setSelectionActionMenuDelegate(selectionActionMenuDelegate);
         AwSelectionDropdownMenuDelegate.maybeSetWebViewDropdownSelectionMenuDelegate(controller);
diff --git a/android_webview/java/src/org/chromium/android_webview/selection/SamsungSelectionActionMenuDelegate.java b/android_webview/java/src/org/chromium/android_webview/selection/SamsungSelectionActionMenuDelegate.java
index 40b9127..9cb71c4 100644
--- a/android_webview/java/src/org/chromium/android_webview/selection/SamsungSelectionActionMenuDelegate.java
+++ b/android_webview/java/src/org/chromium/android_webview/selection/SamsungSelectionActionMenuDelegate.java
@@ -324,7 +324,8 @@
         };
     }
 
-    public static boolean handleMenuItemClick(
+    @Override
+    public boolean handleMenuItemClick(
             MenuItem item, WebContents webContents, ViewGroup containerView) {
         if (!isWritingToolKitMenuItem(item)) return false;
         SelectionPopupController selectionPopupController =
diff --git a/content/public/android/java/src/org/chromium/content/browser/selection/SelectionPopupControllerImpl.java b/content/public/android/java/src/org/chromium/content/browser/selection/SelectionPopupControllerImpl.java
index 60f07868..f78adb1 100644
--- a/content/public/android/java/src/org/chromium/content/browser/selection/SelectionPopupControllerImpl.java
+++ b/content/public/android/java/src/org/chromium/content/browser/selection/SelectionPopupControllerImpl.java
@@ -228,6 +228,8 @@
     // Cached selection menu items to check against new selections.
     private @Nullable SelectionMenuCachedResult mSelectionMenuCachedResult;
 
+    // TODO(crbug.com/445155873): Move menu items from using custom click listeners to handling them
+    //  manually in the handleMenuItemClick callback.
     /** Custom {@link android.view.View.OnClickListener} map for ActionMode menu items. */
     private final Map<MenuItem, View.OnClickListener> mCustomActionMenuItemClickListeners;
 
@@ -959,6 +961,8 @@
         return true;
     }
 
+    // TODO(crbug.com/445155873): Refactor this to directly populate the Menu for ActionMode and the
+    //  MVCListAdapter.ModelList for dropdown menus.
     @VisibleForTesting
     public SortedSet<SelectionMenuGroup> getMenuItems() {
         TextProcessingIntentHandler textProcessingIntentHandler =
diff --git a/content/public/android/java/src/org/chromium/content_public/browser/selection/SelectionActionMenuDelegate.java b/content/public/android/java/src/org/chromium/content_public/browser/selection/SelectionActionMenuDelegate.java
index 873984d..13a28d6a 100644
--- a/content/public/android/java/src/org/chromium/content_public/browser/selection/SelectionActionMenuDelegate.java
+++ b/content/public/android/java/src/org/chromium/content_public/browser/selection/SelectionActionMenuDelegate.java
@@ -5,10 +5,13 @@
 package org.chromium.content_public.browser.selection;
 
 import android.content.pm.ResolveInfo;
+import android.view.MenuItem;
+import android.view.ViewGroup;
 
 import org.chromium.build.annotations.NullMarked;
 import org.chromium.content_public.browser.SelectionMenuItem;
 import org.chromium.content_public.browser.SelectionPopupController;
+import org.chromium.content_public.browser.WebContents;
 
 import java.util.List;
 
@@ -73,4 +76,15 @@
      *     otherwise.
      */
     boolean canReuseCachedSelectionMenu();
+
+    /**
+     * Handles when an item in the selection menu is clicked by the user or activated using the
+     * relevant shortcut.
+     *
+     * @return True if the click was handled by this class or false otherwise.
+     */
+    default boolean handleMenuItemClick(
+            MenuItem item, WebContents webContents, ViewGroup containerView) {
+        return false;
+    }
 }