Class WebExtensionController
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
This delegate will be called whenever the state of an extension has changed.static interface
static class
Contains the possible values for thesource
parameter inenable(org.mozilla.geckoview.WebExtension, int)
anddisable(org.mozilla.geckoview.WebExtension, int)
.static @interface
static interface
This delegate is used to notify of extension process state changes.static @interface
The method used by the embedder to install theWebExtension
.static interface
This delegate will be called whenever an extension is about to be installed or it needs new permissions, e.g during an update or because it calledpermissions.request
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
Indicates theWebExtension
was installed from a file.static final String
Indicates theWebExtension
was installed using from the embedder's add-ons manager.static final String
Indicates theWebExtension
was installed from the embedder's onboarding feature. -
Method Summary
Modifier and TypeMethodDescriptionaddOptionalPermissions
(String extensionId, String[] permissions, String[] origins) Add the provided permissions to theWebExtension
with the given id.createDownload
(int id) disable
(WebExtension extension, int source) Disable an extension that is enabled.void
Disable extension process spawning.enable
(WebExtension extension, int source) Enable an extension that has been disabled.void
Enable extension process spawning.ensureBuiltIn
(String uri, String id) Ensure that a built-in extension is installed.Install an extension.Install an extension.installBuiltIn
(String uri) Install a built-in extension.list()
List installed extensions for thisGeckoRuntime
.removeOptionalPermissions
(String extensionId, String[] permissions, String[] origins) Remove the provided permissions from theWebExtension
with the given id.void
Set theWebExtensionController.AddonManagerDelegate
for this instance.setAllowedInPrivateBrowsing
(WebExtension extension, boolean allowed) Set whether an extension should be allowed to run in private browsing or not.void
Set theWebExtensionController.DebuggerDelegate
for this instance.void
Set theWebExtensionController.ExtensionProcessDelegate
for this instance.void
Set theWebExtensionController.PromptDelegate
for this instance.void
setTabActive
(GeckoSession session, boolean active) Notifies extensions about a active tab change over the `tabs.onActivated` event.uninstall
(WebExtension extension) Uninstall an extension.update
(WebExtension extension) Update a web extension.
-
Field Details
-
INSTALLATION_METHOD_MANAGER
Indicates theWebExtension
was installed using from the embedder's add-ons manager.- See Also:
-
INSTALLATION_METHOD_FROM_FILE
Indicates theWebExtension
was installed from a file.- See Also:
-
INSTALLATION_METHOD_ONBOARDING
Indicates theWebExtension
was installed from the embedder's onboarding feature.- See Also:
-
-
Method Details
-
getPromptDelegate
- Returns:
- the current
WebExtensionController.PromptDelegate
instance. - See Also:
-
setPromptDelegate
Set theWebExtensionController.PromptDelegate
for this instance. This delegate will be used to be notified whenever an extension is being installed or needs new permissions.- Parameters:
delegate
- the delegate instance.- See Also:
-
setDebuggerDelegate
@UiThread public void setDebuggerDelegate(@NonNull WebExtensionController.DebuggerDelegate delegate) Set theWebExtensionController.DebuggerDelegate
for this instance. This delegate will receive updates about extension changes using developer tools.- Parameters:
delegate
- the Delegate instance
-
setAddonManagerDelegate
@UiThread public void setAddonManagerDelegate(@Nullable WebExtensionController.AddonManagerDelegate delegate) Set theWebExtensionController.AddonManagerDelegate
for this instance. This delegate will be used to be notified whenever the state of an extension has changed.- Parameters:
delegate
- the delegate instance- See Also:
-
setExtensionProcessDelegate
@UiThread public void setExtensionProcessDelegate(@Nullable WebExtensionController.ExtensionProcessDelegate delegate) Set theWebExtensionController.ExtensionProcessDelegate
for this instance. This delegate will be used to notify when the state of the extension process has changed.- Parameters:
delegate
- the extension process delegate- See Also:
-
enableExtensionProcessSpawning
@AnyThread public void enableExtensionProcessSpawning()Enable extension process spawning.Extension process spawning can be disabled when the extension process has been killed or crashed beyond the threshold set for Gecko. This method can be called to reset the threshold count and allow the spawning again. If the threshold is reached again,
WebExtensionController.ExtensionProcessDelegate.onDisabledProcessSpawning()
will still be called. -
disableExtensionProcessSpawning
@AnyThread public void disableExtensionProcessSpawning()Disable extension process spawning.Extension process spawning can be re-enabled with
enableExtensionProcessSpawning()
. This method does the opposite and stops the extension process. This method can be called when we no longer want to run extensions for the rest of the session. -
install
@NonNull @AnyThread public GeckoResult<WebExtension> install(@NonNull String uri, @Nullable String installationMethod) Install an extension.An installed extension will persist and will be available even when restarting the
GeckoRuntime
.Installed extensions through this method need to be signed by Mozilla, see Distributing your add-on .
When calling this method, the GeckoView library will download the extension, validate its manifest and signature, and give you an opportunity to verify its permissions through
WebExtensionController.PromptDelegate.onInstallPromptRequest(org.mozilla.geckoview.WebExtension, java.lang.String[], java.lang.String[])
, you can use this method to prompt the user if appropriate.- Parameters:
uri
- URI to the extension's.xpi
package. This can be a remotehttps:
URI or a localfile:
orresource:
URI. Note: the app needs the appropriate permissions for local URIs.installationMethod
- The method used by the embedder to install theWebExtension
.- Returns:
- A
GeckoResult
that will complete when the installation process finishes. For successful installations, the GeckoResult will return theWebExtension
object that you can use to set delegates and retrieve information about the WebExtension usingWebExtension.metaData
.If an error occurs during the installation process, the GeckoResult will complete exceptionally with a
InstallException
that will contain the relevant error code inInstallException#code
. - See Also:
-
install
Install an extension.An installed extension will persist and will be available even when restarting the
GeckoRuntime
.Installed extensions through this method need to be signed by Mozilla, see Distributing your add-on .
When calling this method, the GeckoView library will download the extension, validate its manifest and signature, and give you an opportunity to verify its permissions through
installPromptRequest(GeckoBundle, EventCallback)
, you can use this method to prompt the user if appropriate. If you are looking to provide anWebExtensionController.InstallationMethod
, please useinstall(String, String)
- Parameters:
uri
- URI to the extension's.xpi
package. This can be a remotehttps:
URI or a localfile:
orresource:
URI. Note: the app needs the appropriate permissions for local URIs.- Returns:
- A
GeckoResult
that will complete when the installation process finishes. For successful installations, the GeckoResult will return theWebExtension
object that you can use to set delegates and retrieve information about the WebExtension usingWebExtension.metaData
.If an error occurs during the installation process, the GeckoResult will complete exceptionally with a
InstallException
that will contain the relevant error code inInstallException#code
. - See Also:
-
installPromptRequest(org.mozilla.gecko.util.GeckoBundle, org.mozilla.gecko.util.EventCallback)
WebExtension.InstallException.ErrorCodes
WebExtension.metaData
-
setAllowedInPrivateBrowsing
@NonNull @AnyThread public GeckoResult<WebExtension> setAllowedInPrivateBrowsing(@NonNull WebExtension extension, boolean allowed) Set whether an extension should be allowed to run in private browsing or not.- Parameters:
extension
- theWebExtension
instance to modify.allowed
- true if this extension should be allowed to run in private browsing pages, false otherwise.- Returns:
- the updated
WebExtension
instance.
-
addOptionalPermissions
@NonNull @AnyThread public GeckoResult<WebExtension> addOptionalPermissions(@NonNull String extensionId, @NonNull String[] permissions, @NonNull String[] origins) Add the provided permissions to theWebExtension
with the given id.- Parameters:
extensionId
- the id ofWebExtension
instance to modify.permissions
- the permissions to add, pass an empty array to not update.origins
- the origins to add, pass an empty array to not update.- Returns:
- the updated
WebExtension
instance.
-
removeOptionalPermissions
@NonNull @AnyThread public GeckoResult<WebExtension> removeOptionalPermissions(@NonNull String extensionId, @NonNull String[] permissions, @NonNull String[] origins) Remove the provided permissions from theWebExtension
with the given id.- Parameters:
extensionId
- the id ofWebExtension
instance to modify.permissions
- the permissions to remove, pass an empty array to not update.origins
- the origins to remove, pass an empty array to not update.- Returns:
- the updated
WebExtension
instance.
-
installBuiltIn
Install a built-in extension.Built-in extensions have access to native messaging, don't need to be signed and are installed from a folder in the APK instead of a .xpi bundle.
Example:
controller.installBuiltIn("resource://android/assets/example/");
Will install the built-in extension located at/assets/example/
in the app's APK.- Parameters:
uri
- Folder where the extension is located. To ensure this folder is inside the APK, onlyresource://android
URIs are allowed.- Returns:
- A
GeckoResult
that completes with the extension once it's installed. - See Also:
-
ensureBuiltIn
@NonNull @AnyThread public GeckoResult<WebExtension> ensureBuiltIn(@NonNull String uri, @Nullable String id) Ensure that a built-in extension is installed.Similar to
installBuiltIn(java.lang.String)
, except the extension is not re-installed if it's already present and it has the same version.Example:
controller.ensureBuiltIn("resource://android/assets/example/", "example@example.com");
Will install the built-in extension located at/assets/example/
in the app's APK.- Parameters:
uri
- Folder where the extension is located. To ensure this folder is inside the APK, onlyresource://android
URIs are allowed.id
- Extension ID as present in the manifest.json file.- Returns:
- A
GeckoResult
that completes with the extension once it's installed. - See Also:
-
uninstall
Uninstall an extension.Uninstalling an extension will remove it from the current
GeckoRuntime
instance, delete all its data and trigger a request to close all extension pages currently open.- Parameters:
extension
- TheWebExtension
to be uninstalled.- Returns:
- A
GeckoResult
that will complete when the uninstall process is completed.
-
enable
@AnyThread @NonNull public GeckoResult<WebExtension> enable(@NonNull WebExtension extension, int source) Enable an extension that has been disabled. If the extension is already enabled, this method has no effect.- Parameters:
extension
- TheWebExtension
to be enabled.source
- The agent that initiated this action, e.g. if the action has been initiated by the user,useWebExtensionController.EnableSource.USER
.- Returns:
- the new
WebExtension
instance, updated to reflect the enablement.
-
disable
@AnyThread @NonNull public GeckoResult<WebExtension> disable(@NonNull WebExtension extension, int source) Disable an extension that is enabled. If the extension is already disabled, this method has no effect.- Parameters:
extension
- TheWebExtension
to be disabled.source
- The agent that initiated this action, e.g. if the action has been initiated by the user, useWebExtensionController.EnableSource.USER
.- Returns:
- the new
WebExtension
instance, updated to reflect the disablement.
-
list
List installed extensions for thisGeckoRuntime
.The returned list can be used to set delegates on the
WebExtension
objects usingWebExtension.setActionDelegate(org.mozilla.geckoview.WebExtension.ActionDelegate)
,WebExtension.setMessageDelegate(org.mozilla.geckoview.WebExtension.MessageDelegate, java.lang.String)
.- Returns:
- a
GeckoResult
that will resolve when the list of extensions is available.
-
update
Update a web extension.When checking for an update, GeckoView will download the update manifest that is defined by the web extension's manifest property browser_specific_settings.gecko.update_url. If an update is found it will be downloaded and installed. If the extension needs any new permissions the
updatePrompt(org.mozilla.gecko.util.GeckoBundle, org.mozilla.gecko.util.EventCallback)
will be triggered.More information about the update manifest format is available here.
- Parameters:
extension
- The extension to update.- Returns:
- A
GeckoResult
that will complete when the update process finishes. If an update is found and installed successfully, the GeckoResult will return the updatedWebExtension
. If no update is available, null will be returned. If the updated extension requires new permissions, theinstallPromptRequest(org.mozilla.gecko.util.GeckoBundle, org.mozilla.gecko.util.EventCallback)
will be called. - See Also:
-
updatePrompt(org.mozilla.gecko.util.GeckoBundle, org.mozilla.gecko.util.EventCallback)
-
setTabActive
Notifies extensions about a active tab change over the `tabs.onActivated` event.- Parameters:
session
- TheGeckoSession
of the newly selected session/tab.active
- true if the tab became active, false if the tab became inactive.
-
createDownload
-