Web extension native messaging from content script is broken
sinking-point opened this issue · 2 comments
I'm using GeckoView to build a web browser. I'm using a built in extension to interact with web content. I want to use native messaging for communication between the browser and the extension.
let response = await chrome.runtime.sendNativeMessage("browser", { type: "getSquenoAccessToken" });
This works as expected in the background script, and my MessageDelegate's onMessage function runs. However, the same code in the content script never resolves and the onMessage function is never called.
I have the following permissions in the manifest as per the docs:
"nativeMessaging",
"nativeMessagingFromContent",
"geckoViewAddons",
The docs suggest I should be able to do native messaging from the content script, so I'm pretty confident this is a bug in GeckoView.
I'm using version 113.0.20230504192738
Perhaps you could take Fenix's usage as a reference. See https://github.com/mozilla-mobile/firefox-android/blob/main/android-components/components/feature/accounts/src/main/assets/extensions/fxawebchannel/fxawebchannel.js and https://github.com/mozilla-mobile/firefox-android/blob/1ddd339f2661e4ec75318767a1b622cb66eeb55f/android-components/components/support/webextensions/src/main/java/mozilla/components/support/webextensions/WebExtensionController.kt
for native messaging from content_scripts, make sure you call setMessageDelegate from TabSession.WebExtensionController like this
runtime.getWebExtensionController().ensureBuiltIn(EXTENSION_LOCATION, "messaging@example.com").accept(
extension -> session.getWebExtensionController()
.setMessageDelegate(extension, messageDelegate, "browser"),
e -> Log.e("MessageDelegate", "Error registering extension", e)
);
instead of
runtime.getWebExtensionController().ensureBuiltIn(EXTENSION_LOCATION, "messaging@example.com").accept(
extension -> extension.setMessageDelegate(messageDelegate, "browser"),
e -> Log.e("MessageDelegate", "Error registering extension", e)
);