Bug 1551278 - Make WebExtension listeners per-extension. r=snorp

Right now listeners for a given native app receive messages from all
WebExtensions. This is wrong as listeners should be extension specific so that
only the intended extension can send messages to the app.

Differential Revision: https://phabricator.services.mozilla.com/D35948

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Agi Sferro 2019-07-08 21:08:15 +00:00
Родитель 32c65ee29f
Коммит faad566428
7 изменённых файлов: 70 добавлений и 16 удалений

Просмотреть файл

@ -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);

Просмотреть файл

@ -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");

Просмотреть файл

@ -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);

Просмотреть файл

@ -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<String, WebExtension.MessageDelegate> mMessageDelegates;
final private HashMap<WebExtensionSender, WebExtension.MessageDelegate> 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 <code>nativeApp</code>.
*
* @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
* <code>nativeApp</code>. <code>null</code> 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<ContentDelegate> mContentHandler =

Просмотреть файл

@ -50,6 +50,14 @@ public class WebExtension {
*/
/* package */ final @NonNull Map<String, MessageDelegate> messageDelegates;
@Override
public String toString() {
return "WebExtension {" +
"location=" + location + ", " +
"id=" + id + ", " +
"flags=" + flags + "}";
}
private final static String LOGTAG = "WebExtension";
public static class Flags {

Просмотреть файл

@ -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);
}

Просмотреть файл

@ -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