diff --git a/mobile/android/geckoview/api.txt b/mobile/android/geckoview/api.txt index 6eed3ed51945..3269cd999518 100644 --- a/mobile/android/geckoview/api.txt +++ b/mobile/android/geckoview/api.txt @@ -399,7 +399,7 @@ package org.mozilla.geckoview { method @AnyThread @NonNull public SessionFinder getFinder(); method @AnyThread @Nullable public GeckoSession.HistoryDelegate getHistoryDelegate(); method @AnyThread @Nullable public GeckoSession.MediaDelegate getMediaDelegate(); - method @AnyThread @Nullable public WebExtension.MessageDelegate getMessageDelegate(@NonNull String); + method @AnyThread @Nullable public WebExtension.MessageDelegate getMessageDelegate(@NonNull WebExtension, @NonNull String); method @UiThread @Nullable public GeckoSession.NavigationDelegate getNavigationDelegate(); method @UiThread @NonNull public OverscrollEdgeEffect getOverscrollEdgeEffect(); method @UiThread public void getPageToScreenMatrix(@NonNull Matrix); @@ -437,7 +437,7 @@ package org.mozilla.geckoview { method @AnyThread public void setFocused(boolean); method @AnyThread public void setHistoryDelegate(@Nullable GeckoSession.HistoryDelegate); method @AnyThread public void setMediaDelegate(@Nullable GeckoSession.MediaDelegate); - method @AnyThread public void setMessageDelegate(@Nullable WebExtension.MessageDelegate, @NonNull String); + method @AnyThread public void setMessageDelegate(@NonNull WebExtension, @Nullable WebExtension.MessageDelegate, @NonNull String); method @UiThread public void setNavigationDelegate(@Nullable GeckoSession.NavigationDelegate); method @UiThread public void setPermissionDelegate(@Nullable GeckoSession.PermissionDelegate); method @UiThread public void setProgressDelegate(@Nullable GeckoSession.ProgressDelegate); diff --git a/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/WebExtensionTest.kt b/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/WebExtensionTest.kt index b0fe0ae34728..9015d3484488 100644 --- a/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/WebExtensionTest.kt +++ b/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/WebExtensionTest.kt @@ -127,7 +127,7 @@ class WebExtensionTest : BaseSessionTest() { } else { webExtension = WebExtension(MESSAGING_CONTENT, uuid, WebExtension.Flags.ALLOW_CONTENT_MESSAGING) - sessionRule.session.setMessageDelegate(messageDelegate, "browser"); + sessionRule.session.setMessageDelegate(webExtension, messageDelegate, "browser"); } return webExtension @@ -426,7 +426,7 @@ class WebExtensionTest : BaseSessionTest() { messaging = WebExtension("resource://android/assets/web_extensions/messaging-iframe/", "{${UUID.randomUUID()}}", WebExtension.Flags.ALLOW_CONTENT_MESSAGING) - sessionRule.session.setMessageDelegate(messageDelegate, "browser"); + sessionRule.session.setMessageDelegate(messaging, messageDelegate, "browser"); sessionRule.waitForResult(sessionRule.runtime.registerWebExtension(messaging)) sessionRule.waitForResult(portTopLevel) @@ -471,7 +471,7 @@ class WebExtensionTest : BaseSessionTest() { extension = WebExtension("resource://android/assets/web_extensions/extension-page-update/") sessionRule.waitForResult(sessionRule.runtime.registerWebExtension(extension)) - mainSession.setMessageDelegate(messageDelegate, "browser") + mainSession.setMessageDelegate(extension, messageDelegate, "browser") mainSession.loadUri("http://example.com"); diff --git a/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/rule/GeckoSessionTestRule.java b/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/rule/GeckoSessionTestRule.java index 9f9604673826..d2b3dd31f56f 100644 --- a/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/rule/GeckoSessionTestRule.java +++ b/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/rule/GeckoSessionTestRule.java @@ -1113,7 +1113,8 @@ public class GeckoSessionTestRule implements TestRule { } protected void prepareSession(final GeckoSession session) { - session.setMessageDelegate(mMessageDelegate, "browser"); + session.setMessageDelegate(RuntimeCreator.TEST_SUPPORT_WEB_EXTENSION, mMessageDelegate, + "browser"); for (final Class cls : DEFAULT_DELEGATES) { try { setDelegate(cls, session, mNullDelegates.contains(cls) ? null : mCallbackProxy); 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 bd76e8009052..3209bb17c0e1 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 @@ -317,8 +317,37 @@ 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 mMessageDelegates; public WebExtensionListener() { mMessageDelegates = new HashMap<>(); @@ -332,13 +361,15 @@ public class GeckoSession implements Parcelable { null); } - public void setDelegate(final WebExtension.MessageDelegate delegate, + public void setDelegate(final WebExtension webExtension, + final WebExtension.MessageDelegate delegate, final String nativeApp) { - mMessageDelegates.put(nativeApp, delegate); + mMessageDelegates.put(new WebExtensionSender(webExtension.id, nativeApp), delegate); } - public WebExtension.MessageDelegate getDelegate(final String nativeApp) { - return mMessageDelegates.get(nativeApp); + public WebExtension.MessageDelegate getDelegate(final WebExtension webExtension, + final String nativeApp) { + return mMessageDelegates.get(new WebExtensionSender(webExtension.id, nativeApp)); } @Override @@ -362,6 +393,7 @@ public class GeckoSession implements Parcelable { /** * Get the message delegate for nativeApp. * + * @param webExtension {@link WebExtension} that this delegate receives messages from. * @param nativeApp identifier for the native app * @return The {@link WebExtension.MessageDelegate} attached to the * nativeApp. null if no delegate is @@ -369,8 +401,9 @@ public class GeckoSession implements Parcelable { */ @AnyThread public @Nullable WebExtension.MessageDelegate getMessageDelegate( + final @NonNull WebExtension webExtension, final @NonNull String nativeApp) { - return mWebExtensionListener.getDelegate(nativeApp); + return mWebExtensionListener.getDelegate(webExtension, nativeApp); } /** @@ -386,6 +419,9 @@ public class GeckoSession implements Parcelable { * to explicitely allow it in {@link WebExtension#WebExtension} by setting * {@link WebExtension.Flags#ALLOW_CONTENT_MESSAGING}. * + * @param webExtension {@link WebExtension} that this delegate receives + * messages from. + * * @param delegate {@link WebExtension.MessageDelegate} that will receive * messages from this session. * @param nativeApp which native app id this message delegate will handle @@ -393,9 +429,10 @@ public class GeckoSession implements Parcelable { * @see WebExtension#setMessageDelegate */ @AnyThread - public void setMessageDelegate(final @Nullable WebExtension.MessageDelegate delegate, + public void setMessageDelegate(final @NonNull WebExtension webExtension, + final @Nullable WebExtension.MessageDelegate delegate, final @NonNull String nativeApp) { - mWebExtensionListener.setDelegate(delegate, nativeApp); + mWebExtensionListener.setDelegate(webExtension, delegate, nativeApp); } private final GeckoSessionHandler mContentHandler = 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 87a2abd6ee53..c307b5627813 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 @@ -50,6 +50,14 @@ public class WebExtension { */ /* package */ final @NonNull Map messageDelegates; + @Override + public String toString() { + return "WebExtension {" + + "location=" + location + ", " + + "id=" + id + ", " + + "flags=" + flags + "}"; + } + private final static String LOGTAG = "WebExtension"; public static class Flags { diff --git a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/WebExtensionEventDispatcher.java b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/WebExtensionEventDispatcher.java index 434786562440..89d96e7854bb 100644 --- a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/WebExtensionEventDispatcher.java +++ b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/WebExtensionEventDispatcher.java @@ -148,7 +148,7 @@ import java.util.Map; WebExtension.MessageDelegate delegate = null; if (sender.session != null) { - delegate = sender.session.getMessageDelegate(nativeApp); + delegate = sender.session.getMessageDelegate(sender.webExtension, nativeApp); } else if (sender.environmentType == WebExtension.MessageSender.ENV_TYPE_EXTENSION) { delegate = sender.webExtension.messageDelegates.get(nativeApp); } diff --git a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/doc-files/CHANGELOG.md b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/doc-files/CHANGELOG.md index c04603184c6b..5cbdfea897c6 100644 --- a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/doc-files/CHANGELOG.md +++ b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/doc-files/CHANGELOG.md @@ -20,6 +20,14 @@ exclude: true [69.3]: ../GeckoResult.html#accept-org.mozilla.geckoview.GeckoResult.Consumer-org.mozilla.geckoview.GeckoResult.Consumer- +- [`GeckoSession.setMessageDelegate`][69.2] callers must now specify the + [`WebExtension`][69.3] that the [`MessageDelegate`][69.4] will receive + messages from. + +[69.2]: ../GeckoSession.html#setMessageDelegate-org.mozilla.geckoview.WebExtension-org.mozilla.geckoview.WebExtension.MessageDelegate-java.lang.String- +[69.3]: ../WebExtension.html +[69.4]: ../WebExtension.MessageDelegate.html + ## v68 - Added [`GeckoRuntime#configurationChanged`][68.1] to notify the device configuration has changed. @@ -332,4 +340,4 @@ exclude: true [65.24]: ../CrashReporter.html#sendCrashReport-android.content.Context-android.os.Bundle-java.lang.String- [65.25]: ../GeckoResult.html -[api-version]: 783f253fda7287f55497c15f867dc14cd1622666 +[api-version]: 5728f5c65b2be03fcf31d7da12050bf26d07e30f