Skip to content

Commit

Permalink
Bug 1826739 - Implement more AddonManager events. r=rpl,geckoview-rev…
Browse files Browse the repository at this point in the history
…iewers,zmckenney

Differential Revision: https://phabricator.services.mozilla.com/D174874
  • Loading branch information
willdurand committed Apr 11, 2023
1 parent 43ae5b9 commit e93e623
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 11 deletions.
5 changes: 5 additions & 0 deletions mobile/android/geckoview/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2339,8 +2339,13 @@ package org.mozilla.geckoview {

public static interface WebExtensionController.AddonManagerDelegate {
method @UiThread default public void onDisabled(@NonNull WebExtension);
method @UiThread default public void onDisabling(@NonNull WebExtension);
method @UiThread default public void onEnabled(@NonNull WebExtension);
method @UiThread default public void onEnabling(@NonNull WebExtension);
method @UiThread default public void onInstalled(@NonNull WebExtension);
method @UiThread default public void onInstalling(@NonNull WebExtension);
method @UiThread default public void onUninstalled(@NonNull WebExtension);
method @UiThread default public void onUninstalling(@NonNull WebExtension);
}

public static interface WebExtensionController.DebuggerDelegate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,29 @@ class WebExtensionTest : BaseSessionTest() {
{ delegate -> controller.setAddonManagerDelegate(delegate) },
{ controller.setAddonManagerDelegate(null) },
object : WebExtensionController.AddonManagerDelegate {
@AssertCalled(count = 3)
override fun onEnabling(extension: WebExtension) {}

@AssertCalled(count = 3)
override fun onEnabled(extension: WebExtension) {}

@AssertCalled(count = 3)
override fun onDisabling(extension: WebExtension) {}

@AssertCalled(count = 3)
override fun onDisabled(extension: WebExtension) {}

@AssertCalled(count = 1)
override fun onUninstalling(extension: WebExtension) {}

@AssertCalled(count = 1)
override fun onUninstalled(extension: WebExtension) {}

@AssertCalled(count = 1)
override fun onInstalling(extension: WebExtension) {}

@AssertCalled(count = 1)
override fun onInstalled(extension: WebExtension) {}
}
)

Expand Down Expand Up @@ -2510,29 +2525,47 @@ class WebExtensionTest : BaseSessionTest() {
}
})

val webExtension = sessionRule.waitForResult(
controller.install("https://example.org/tests/junit/update-1.xpi")
)

mainSession.reload()
sessionRule.waitForPageStop()

sessionRule.addExternalDelegateUntilTestEnd(
WebExtensionController.AddonManagerDelegate::class,
{ delegate -> controller.setAddonManagerDelegate(delegate) },
{ controller.setAddonManagerDelegate(null) },
object : WebExtensionController.AddonManagerDelegate {
@AssertCalled(count = 0)
override fun onEnabling(extension: WebExtension) {}

@AssertCalled(count = 0)
override fun onEnabled(extension: WebExtension) {}

@AssertCalled(count = 1)
override fun onDisabling(extension: WebExtension) {}

@AssertCalled(count = 1)
override fun onDisabled(extension: WebExtension) {}

@AssertCalled(count = 1)
override fun onUninstalling(extension: WebExtension) {}

@AssertCalled(count = 1)
override fun onUninstalled(extension: WebExtension) {}

// We expect onInstalling/onInstalled to be invoked twice
// because we first install the extension and then we update
// it, which results in a second install.
@AssertCalled(count = 2)
override fun onInstalling(extension: WebExtension) {}

@AssertCalled(count = 2)
override fun onInstalled(extension: WebExtension) {}
}
)

val webExtension = sessionRule.waitForResult(
controller.install("https://example.org/tests/junit/update-1.xpi")
)

mainSession.reload()
sessionRule.waitForPageStop()

val disabledWebExtension = sessionRule.waitForResult(controller.disable(webExtension, source))

when (source) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,14 @@ default void onExtensionListUpdated() {}

/** This delegate will be called whenever the state of an extension has changed. */
public interface AddonManagerDelegate {
/**
* Called whenever an extension is being disabled.
*
* @param extension The {@link WebExtension} that is being disabled.
*/
@UiThread
default void onDisabling(@NonNull WebExtension extension) {}

/**
* Called whenever an extension has been disabled.
*
Expand All @@ -336,6 +344,14 @@ public interface AddonManagerDelegate {
@UiThread
default void onDisabled(final @NonNull WebExtension extension) {}

/**
* Called whenever an extension is being enabled.
*
* @param extension The {@link WebExtension} that is being enabled.
*/
@UiThread
default void onEnabling(final @NonNull WebExtension extension) {}

/**
* Called whenever an extension has been enabled.
*
Expand All @@ -344,13 +360,37 @@ default void onDisabled(final @NonNull WebExtension extension) {}
@UiThread
default void onEnabled(final @NonNull WebExtension extension) {}

/**
* Called whenever an extension is being uninstalled.
*
* @param extension The {@link WebExtension} that is being uninstalled.
*/
@UiThread
default void onUninstalling(final @NonNull WebExtension extension) {}

/**
* Called whenever an extension has been uninstalled.
*
* @param extension The {@link WebExtension} that is being uninstalled.
*/
@UiThread
default void onUninstalled(final @NonNull WebExtension extension) {}

/**
* Called whenever an extension is being installed.
*
* @param extension The {@link WebExtension} that is being installed.
*/
@UiThread
default void onInstalling(final @NonNull WebExtension extension) {}

/**
* Called whenever an extension has been installed.
*
* @param extension The {@link WebExtension} that is being installed.
*/
@UiThread
default void onInstalled(final @NonNull WebExtension extension) {}
}

/**
Expand Down Expand Up @@ -423,16 +463,26 @@ public void setAddonManagerDelegate(final @Nullable AddonManagerDelegate delegat
EventDispatcher.getInstance()
.unregisterUiThreadListener(
mInternals,
"GeckoView:WebExtension:OnDisabling",
"GeckoView:WebExtension:OnDisabled",
"GeckoView:WebExtension:OnEnabling",
"GeckoView:WebExtension:OnEnabled",
"GeckoView:WebExtension:OnUninstalled");
"GeckoView:WebExtension:OnUninstalling",
"GeckoView:WebExtension:OnUninstalled",
"GeckoView:WebExtension:OnInstalling",
"GeckoView:WebExtension:OnInstalled");
} else if (delegate != null && mAddonManagerDelegate == null) {
EventDispatcher.getInstance()
.registerUiThreadListener(
mInternals,
"GeckoView:WebExtension:OnDisabling",
"GeckoView:WebExtension:OnDisabled",
"GeckoView:WebExtension:OnEnabling",
"GeckoView:WebExtension:OnEnabled",
"GeckoView:WebExtension:OnUninstalled");
"GeckoView:WebExtension:OnUninstalling",
"GeckoView:WebExtension:OnUninstalled",
"GeckoView:WebExtension:OnInstalling",
"GeckoView:WebExtension:OnInstalled");
}

mAddonManagerDelegate = delegate;
Expand Down Expand Up @@ -786,15 +836,30 @@ public GeckoResult<WebExtension> update(final @NonNull WebExtension extension) {
mDebuggerDelegate.onExtensionListUpdated();
}
return;
} else if ("GeckoView:WebExtension:OnDisabling".equals(event)) {
onDisabling(bundle);
return;
} else if ("GeckoView:WebExtension:OnDisabled".equals(event)) {
onDisabled(bundle);
return;
} else if ("GeckoView:WebExtension:OnEnabling".equals(event)) {
onEnabling(bundle);
return;
} else if ("GeckoView:WebExtension:OnEnabled".equals(event)) {
onEnabled(bundle);
return;
} else if ("GeckoView:WebExtension:OnUninstalling".equals(event)) {
onUninstalling(bundle);
return;
} else if ("GeckoView:WebExtension:OnUninstalled".equals(event)) {
onUninstalled(bundle);
return;
} else if ("GeckoView:WebExtension:OnInstalling".equals(event)) {
onInstalling(bundle);
return;
} else if ("GeckoView:WebExtension:OnInstalled".equals(event)) {
onInstalled(bundle);
return;
}

extensionFromBundle(bundle)
Expand Down Expand Up @@ -980,6 +1045,17 @@ private void optionalPrompt(final Message message, final WebExtension extension)
}));
}

private void onDisabling(final GeckoBundle bundle) {
if (mAddonManagerDelegate == null) {
Log.e(LOGTAG, "no AddonManager delegate registered");
return;
}

final GeckoBundle extensionBundle = bundle.getBundle("extension");
final WebExtension extension = new WebExtension(mDelegateControllerProvider, extensionBundle);
mAddonManagerDelegate.onDisabling(extension);
}

private void onDisabled(final GeckoBundle bundle) {
if (mAddonManagerDelegate == null) {
Log.e(LOGTAG, "no AddonManager delegate registered");
Expand All @@ -991,6 +1067,17 @@ private void onDisabled(final GeckoBundle bundle) {
mAddonManagerDelegate.onDisabled(extension);
}

private void onEnabling(final GeckoBundle bundle) {
if (mAddonManagerDelegate == null) {
Log.e(LOGTAG, "no AddonManager delegate registered");
return;
}

final GeckoBundle extensionBundle = bundle.getBundle("extension");
final WebExtension extension = new WebExtension(mDelegateControllerProvider, extensionBundle);
mAddonManagerDelegate.onEnabling(extension);
}

private void onEnabled(final GeckoBundle bundle) {
if (mAddonManagerDelegate == null) {
Log.e(LOGTAG, "no AddonManager delegate registered");
Expand All @@ -1002,6 +1089,17 @@ private void onEnabled(final GeckoBundle bundle) {
mAddonManagerDelegate.onEnabled(extension);
}

private void onUninstalling(final GeckoBundle bundle) {
if (mAddonManagerDelegate == null) {
Log.e(LOGTAG, "no AddonManager delegate registered");
return;
}

final GeckoBundle extensionBundle = bundle.getBundle("extension");
final WebExtension extension = new WebExtension(mDelegateControllerProvider, extensionBundle);
mAddonManagerDelegate.onUninstalling(extension);
}

private void onUninstalled(final GeckoBundle bundle) {
if (mAddonManagerDelegate == null) {
Log.e(LOGTAG, "no AddonManager delegate registered");
Expand All @@ -1013,6 +1111,28 @@ private void onUninstalled(final GeckoBundle bundle) {
mAddonManagerDelegate.onUninstalled(extension);
}

private void onInstalling(final GeckoBundle bundle) {
if (mAddonManagerDelegate == null) {
Log.e(LOGTAG, "no AddonManager delegate registered");
return;
}

final GeckoBundle extensionBundle = bundle.getBundle("extension");
final WebExtension extension = new WebExtension(mDelegateControllerProvider, extensionBundle);
mAddonManagerDelegate.onInstalling(extension);
}

private void onInstalled(final GeckoBundle bundle) {
if (mAddonManagerDelegate == null) {
Log.e(LOGTAG, "no AddonManager delegate registered");
return;
}

final GeckoBundle extensionBundle = bundle.getBundle("extension");
final WebExtension extension = new WebExtension(mDelegateControllerProvider, extensionBundle);
mAddonManagerDelegate.onInstalled(extension);
}

@SuppressLint("WrongThread") // for .toGeckoBundle
private void getSettings(final Message message, final WebExtension extension) {
final WebExtension.BrowsingDataDelegate delegate = mListener.getBrowsingDataDelegate(extension);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ exclude: true
([bug 1820567]({{bugzilla}}1820567))
- Add `ViewportMode` annotation to [`viewportMode`][113.7], [`getViewportMode`][113.8] and [`setViewportMode`][113.9].
([bug 1820567]({{bugzilla}}1820567))
- Add [`WebExtensionController.AddonManagerDelegate`][113.10] ([bug 1822763]({{bugzilla}}1822763))
- Add [`WebExtensionController.AddonManagerDelegate`][113.10] ([bug 1822763]({{bugzilla}}1822763), [bug 1826739]({{bugzilla}}1826739))

[113.1]: {{javadoc_uri}}/GeckoSessionSettings.Builder.html#displayMode(int)
[113.2]: {{javadoc_uri}}/GeckoSessionSettings.html#getDisplayMode()
Expand Down Expand Up @@ -1346,4 +1346,4 @@ to allow adding gecko profiler markers.
[65.24]: {{javadoc_uri}}/CrashReporter.html#sendCrashReport(android.content.Context,android.os.Bundle,java.lang.String)
[65.25]: {{javadoc_uri}}/GeckoResult.html

[api-version]: 04715b0dddf7887089933ed90637737e336fe190
[api-version]: 3867469003995d18051e031bdc669690295c9171
Loading

0 comments on commit e93e623

Please sign in to comment.