From 059c94874639c08d0c034248072921a73c2834a6 Mon Sep 17 00:00:00 2001 From: Agi Sferro Date: Fri, 15 Nov 2019 16:34:02 +0000 Subject: [PATCH] Bug 1530402 - Move WebExtensionListener to WebExtension.java. r=esawin Differential Revision: https://phabricator.services.mozilla.com/D52709 --HG-- extra : moz-landing-system : lando --- .../org/mozilla/geckoview/GeckoSession.java | 104 +----------------- .../org/mozilla/geckoview/WebExtension.java | 100 +++++++++++++++++ 2 files changed, 104 insertions(+), 100 deletions(-) diff --git a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoSession.java b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoSession.java index 36b4668d5624..3ca86b151b80 100644 --- a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoSession.java +++ b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoSession.java @@ -14,7 +14,6 @@ import java.util.AbstractSequentialList; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; @@ -332,104 +331,7 @@ public class GeckoSession implements Parcelable { } }; - private static class WebExtensionSender { - public String webExtensionId; - public String nativeApp; - - public WebExtensionSender(final String webExtensionId, final String nativeApp) { - this.webExtensionId = webExtensionId; - this.nativeApp = nativeApp; - } - - @Override - public boolean equals(final Object other) { - if (!(other instanceof WebExtensionSender)) { - return false; - } - - WebExtensionSender o = (WebExtensionSender) other; - return webExtensionId.equals(o.webExtensionId) && - nativeApp.equals(o.nativeApp); - } - - @Override - public int hashCode() { - int result = 17; - result = 31 * result + (webExtensionId != null ? webExtensionId.hashCode() : 0); - result = 31 * result + (nativeApp != null ? nativeApp.hashCode() : 0); - return result; - } - } - - private final class WebExtensionListener implements BundleEventListener { - final private HashMap mMessageDelegates; - final private HashMap mActionDelegates; - - public WebExtensionListener() { - mMessageDelegates = new HashMap<>(); - mActionDelegates = new HashMap<>(); - } - - /* package */ void registerListeners() { - getEventDispatcher().registerUiThreadListener(this, - "GeckoView:WebExtension:Message", - "GeckoView:WebExtension:PortMessage", - "GeckoView:WebExtension:Connect", - "GeckoView:WebExtension:CloseTab", - - // Browser and Page Actions - "GeckoView:BrowserAction:Update", - "GeckoView:BrowserAction:OpenPopup", - "GeckoView:PageAction:Update", - "GeckoView:PageAction:OpenPopup", - null); - } - - public void setActionDelegate(final WebExtension webExtension, - final WebExtension.ActionDelegate delegate) { - mActionDelegates.put(webExtension.id, delegate); - } - - public WebExtension.ActionDelegate getActionDelegate(final WebExtension webExtension) { - return mActionDelegates.get(webExtension.id); - } - - public void setMessageDelegate(final WebExtension webExtension, - final WebExtension.MessageDelegate delegate, - final String nativeApp) { - mMessageDelegates.put(new WebExtensionSender(webExtension.id, nativeApp), delegate); - } - - public WebExtension.MessageDelegate getMessageDelegate(final WebExtension webExtension, - final String nativeApp) { - return mMessageDelegates.get(new WebExtensionSender(webExtension.id, nativeApp)); - } - - @Override - public void handleMessage(final String event, final GeckoBundle message, - final EventCallback callback) { - if (mWindow == null) { - return; - } - - if ("GeckoView:WebExtension:Message".equals(event) - || "GeckoView:WebExtension:PortMessage".equals(event) - || "GeckoView:WebExtension:Connect".equals(event) - || "GeckoView:PageAction:Update".equals(event) - || "GeckoView:PageAction:OpenPopup".equals(event) - || "GeckoView:BrowserAction:Update".equals(event) - || "GeckoView:BrowserAction:OpenPopup".equals(event)) { - mWindow.runtime.getWebExtensionDispatcher() - .handleMessage(event, message, callback, GeckoSession.this); - return; - } else if ("GeckoView:WebExtension:CloseTab".equals(event)) { - mWindow.runtime.getWebExtensionController().closeTab(message, callback, GeckoSession.this); - return; - } - } - } - - private final WebExtensionListener mWebExtensionListener; + private final WebExtension.Listener mWebExtensionListener; /** * Get the message delegate for nativeApp. @@ -1351,7 +1253,7 @@ public class GeckoSession implements Parcelable { mSettings = new GeckoSessionSettings(settings, this); mListener.registerListeners(); - mWebExtensionListener = new WebExtensionListener(); + mWebExtensionListener = new WebExtension.Listener(this); mWebExtensionListener.registerListeners(); if (BuildConfig.DEBUG && handlersCount != mSessionHandlers.length) { @@ -1397,6 +1299,7 @@ public class GeckoSession implements Parcelable { mEventDispatcher, mAccessibility != null ? mAccessibility.nativeProvider : null, createInitData()); onWindowChanged(WINDOW_TRANSFER_IN, /* inProgress */ false); + mWebExtensionListener.runtime = mWindow.runtime; } } @@ -1517,6 +1420,7 @@ public class GeckoSession implements Parcelable { final boolean isRemote = mSettings.getUseMultiprocess(); mWindow = new Window(runtime, this, mNativeQueue); + mWebExtensionListener.runtime = runtime; onWindowChanged(WINDOW_OPEN, /* inProgress */ true); diff --git a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/WebExtension.java b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/WebExtension.java index ecf91176ab81..cefd6fa56d4c 100644 --- a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/WebExtension.java +++ b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/WebExtension.java @@ -13,6 +13,8 @@ import android.util.Log; import org.json.JSONException; import org.json.JSONObject; import org.mozilla.gecko.EventDispatcher; +import org.mozilla.gecko.util.BundleEventListener; +import org.mozilla.gecko.util.EventCallback; import org.mozilla.gecko.util.GeckoBundle; import java.lang.annotation.Retention; @@ -400,6 +402,104 @@ public class WebExtension { } }; + private static class Sender { + public String webExtensionId; + public String nativeApp; + + public Sender(final String webExtensionId, final String nativeApp) { + this.webExtensionId = webExtensionId; + this.nativeApp = nativeApp; + } + + @Override + public boolean equals(final Object other) { + if (!(other instanceof Sender)) { + return false; + } + + Sender o = (Sender) other; + return webExtensionId.equals(o.webExtensionId) && + nativeApp.equals(o.nativeApp); + } + + @Override + public int hashCode() { + int result = 17; + result = 31 * result + (webExtensionId != null ? webExtensionId.hashCode() : 0); + result = 31 * result + (nativeApp != null ? nativeApp.hashCode() : 0); + return result; + } + } + + /* package */ final static class Listener implements BundleEventListener { + final private HashMap mMessageDelegates; + final private HashMap mActionDelegates; + final private GeckoSession mSession; + public GeckoRuntime runtime; + + public Listener(final GeckoSession session) { + mMessageDelegates = new HashMap<>(); + mActionDelegates = new HashMap<>(); + mSession = session; + } + + /* package */ void registerListeners() { + mSession.getEventDispatcher().registerUiThreadListener(this, + "GeckoView:WebExtension:Message", + "GeckoView:WebExtension:PortMessage", + "GeckoView:WebExtension:Connect", + "GeckoView:WebExtension:CloseTab", + + // Browser and Page Actions + "GeckoView:BrowserAction:Update", + "GeckoView:BrowserAction:OpenPopup", + "GeckoView:PageAction:Update", + "GeckoView:PageAction:OpenPopup"); + } + + public void setActionDelegate(final WebExtension webExtension, + final WebExtension.ActionDelegate delegate) { + mActionDelegates.put(webExtension.id, delegate); + } + + public WebExtension.ActionDelegate getActionDelegate(final WebExtension webExtension) { + return mActionDelegates.get(webExtension.id); + } + + public void setMessageDelegate(final WebExtension webExtension, + final WebExtension.MessageDelegate delegate, + final String nativeApp) { + mMessageDelegates.put(new Sender(webExtension.id, nativeApp), delegate); + } + + public WebExtension.MessageDelegate getMessageDelegate(final WebExtension webExtension, + final String nativeApp) { + return mMessageDelegates.get(new Sender(webExtension.id, nativeApp)); + } + + @Override + public void handleMessage(final String event, final GeckoBundle message, + final EventCallback callback) { + if (runtime == null) { + return; + } + + if ("GeckoView:WebExtension:Message".equals(event) + || "GeckoView:WebExtension:PortMessage".equals(event) + || "GeckoView:WebExtension:Connect".equals(event) + || "GeckoView:PageAction:Update".equals(event) + || "GeckoView:PageAction:OpenPopup".equals(event) + || "GeckoView:BrowserAction:Update".equals(event) + || "GeckoView:BrowserAction:OpenPopup".equals(event)) { + runtime.getWebExtensionDispatcher() + .handleMessage(event, message, callback, mSession); + return; + } else if ("GeckoView:WebExtension:CloseTab".equals(event)) { + runtime.getWebExtensionController().closeTab(message, callback, mSession); + return; + } + } + } /** * Describes the sender of a message from a WebExtension.