Bug 1441279 - 3. Add selection action handler in GeckoSession; r?esawin draft
authorJim Chen <nchen@mozilla.com>
Mon, 02 Apr 2018 17:13:45 -0400
changeset 776312 af0562409fdc0954e05a9435fe319d07f8c0c237
parent 776311 63d496ee0e4c82e83e0c04f15325a7c0e353698b
child 776313 85dc57584892bf1b6e28e35f814618a46db07415
push id104840
push userbmo:nchen@mozilla.com
push dateMon, 02 Apr 2018 21:15:36 +0000
reviewersesawin
bugs1441279
milestone61.0a1
Bug 1441279 - 3. Add selection action handler in GeckoSession; r?esawin Add a handler for selection action delegate in GeckoSession that calls the callback methods. MozReview-Commit-ID: C2mMHHheTJT
mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoSession.java
--- a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoSession.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoSession.java
@@ -333,21 +333,73 @@ public class GeckoSession extends LayerS
 
                     delegate.onMediaPermissionRequest(
                             GeckoSession.this, message.getString("uri"),
                             videos, audios, new PermissionCallback("media", callback));
                 }
             }
         };
 
+    private final GeckoSessionHandler<SelectionActionDelegate> mSelectionActionDelegate =
+        new GeckoSessionHandler<SelectionActionDelegate>(
+            "GeckoViewSelectionAction", this,
+            new String[] {
+                "GeckoView:HideSelectionAction",
+                "GeckoView:ShowSelectionAction",
+            }
+        ) {
+            @Override
+            public void handleMessage(final SelectionActionDelegate delegate,
+                                      final String event,
+                                      final GeckoBundle message,
+                                      final EventCallback callback) {
+                if ("GeckoView:ShowSelectionAction".equals(event)) {
+                    final SelectionActionDelegate.Selection selection =
+                            new SelectionActionDelegate.Selection(message);
+
+                    final String[] actions = message.getStringArray("actions");
+                    final int seqNo = message.getInt("seqNo");
+                    final Response<String> response = new Response<String>() {
+                        @Override
+                        public void respond(final String action) {
+                            final GeckoBundle response = new GeckoBundle(2);
+                            response.putString("id", action);
+                            response.putInt("seqNo", seqNo);
+                            callback.sendSuccess(response);
+                        }
+                    };
+
+                    delegate.onShowActionRequest(GeckoSession.this, selection,
+                                                 actions, response);
+
+                } else if ("GeckoView:HideSelectionAction".equals(event)) {
+                    final String reasonString = message.getString("reason");
+                    final int reason;
+                    if ("invisibleselection".equals(reasonString)) {
+                        reason = SelectionActionDelegate.HIDE_REASON_INVISIBLE_SELECTION;
+                    } else if ("presscaret".equals(reasonString)) {
+                        reason = SelectionActionDelegate.HIDE_REASON_ACTIVE_SELECTION;
+                    } else if ("scroll".equals(reasonString)) {
+                        reason = SelectionActionDelegate.HIDE_REASON_ACTIVE_SCROLL;
+                    } else if ("visibilitychange".equals(reasonString)) {
+                        reason = SelectionActionDelegate.HIDE_REASON_NO_SELECTION;
+                    } else {
+                        throw new IllegalArgumentException();
+                    }
+
+                    delegate.onHideAction(GeckoSession.this, reason);
+                }
+            }
+        };
+
     /* package */ int handlersCount;
 
     private final GeckoSessionHandler<?>[] mSessionHandlers = new GeckoSessionHandler<?>[] {
         mContentHandler, mNavigationHandler, mProgressHandler, mScrollHandler,
-        mTrackingProtectionHandler, mPermissionHandler
+        mTrackingProtectionHandler, mPermissionHandler, mSelectionActionDelegate
     };
 
     private static class PermissionCallback implements
         PermissionDelegate.Callback, PermissionDelegate.MediaCallback {
 
         private final String mType;
         private EventCallback mCallback;
 
@@ -1130,16 +1182,34 @@ public class GeckoSession extends LayerS
     /**
      * Get the current prompt delegate for this GeckoSession.
      * @return PromptDelegate instance or null if using built-in delegate.
      */
     public PromptDelegate getPromptDelegate() {
         return mPromptDelegate;
     }
 
+    /**
+     * Set the current selection action delegate for this GeckoSession.
+     *
+     * @param delegate SelectionActionDelegate instance or null to unset.
+     */
+    public void setSelectionActionDelegate(@Nullable SelectionActionDelegate delegate) {
+        mSelectionActionDelegate.setDelegate(delegate, this);
+    }
+
+    /**
+     * Get the current selection action delegate for this GeckoSession.
+     *
+     * @return SelectionActionDelegate instance or null if not set.
+     */
+    public @Nullable SelectionActionDelegate getSelectionActionDelegate() {
+        return mSelectionActionDelegate.getDelegate();
+    }
+
     private static class PromptCallback implements
         PromptDelegate.AlertCallback, PromptDelegate.ButtonCallback,
         PromptDelegate.TextCallback, PromptDelegate.AuthCallback,
         PromptDelegate.ChoiceCallback, PromptDelegate.FileCallback {
 
         private final String mType;
         private final String mMode;
         private final boolean mHasCheckbox;