Closed Bug 1382069 Opened 8 years ago Closed 5 years ago

Connection-based native messaging in popups fails with NS_ERROR_NOT_INITIALIZED

Categories

(WebExtensions :: General, defect, P3)

55 Branch
defect

Tracking

(Not tracked)

RESOLVED WORKSFORME

People

(Reporter: eric.mccormick, Assigned: robwu)

Details

Attachments

(1 obsolete file)

User Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0 Build ID: 20170717063821 Steps to reproduce: I've setup a port in a popup through browser.runtime.connectNative("MyApp"), added a listener for port.onMessage and tried to send a message using port.postMessage("ping"). Actual results: An exception is thrown: [Exception... "Component returned failure code: 0xc1f30001 (NS_ERROR_NOT_INITIALIZED) [nsIMessageSender.sendAsyncMessage]" nsresult: "0xc1f30001 (NS_ERROR_NOT_INITIALIZED)" location: "JS frame :: resource://gre/modules/MessageChannel.jsm :: sendMessage :: line 530" data: no] (unknown) And while my native application receives the sent message, my WebExtension doesn't get the response. Expected results: I can successfully send and receive messages running the same code in background scripts, sidebars and option pages without errors. In popups however, the same code produces an exception. I don't think that this is an intended restriction, since connectionless messaging in popups works fine.
Workaround: browser.extension.getBachgroundPage().browser.runtime.connectNative("MyApp"); from https://discourse.mozilla-community.org/t/connection-based-native-messaging-doesnt-work-in-popups/17185/5
The issue is related to preloading popups and not actually tracking the changing message manager when we swap in the preloaded page.
Priority: -- → P2
Priority: P2 → P3
Native messaging has this bug too, and whether Connection-based or One-off messages. I don't have any way to do - popup site gets the response of native site except that I change my native app to use Connection-based API and use browser.extension.getBackgroundPage().browser.runtime.connectNative("MyApp") in extension site. When can I use this API by normal?
Ok, I changed my native app for support Connection-based, and I got some other bug. I defined this function to imitate One-off messages method: // popup script App.sendNativeMessage =function(appName ,data){ var portMap ={}; return new Promise(function(resolve ,reject){ var port =portMap[appName] || function(){ return browser.extension.getBackgroundPage().browser.runtime.connectNative(appName); }(); port.onMessage.addListener(function _self(response){ resolve(response); port.onMessage.removeListener(_self); }); port.postMessage(data); }); }; but it don't work. The Promise is not be fulfilled and Firefox tell me : > can't access dead object I'm guessing the problem by browser.extension.getBackgroundPage(), but I can't call cloneInto or exportFunction APIs. Now, I don't have any way except nothing.
My mistake... I call window.close() is too early. It works in One-off messages mode.
Product: Toolkit → WebExtensions
Component: Untriaged → General
Assignee: nobody → rob
Status: UNCONFIRMED → ASSIGNED
Ever confirmed: true

The implementation has been refactored, and the bug probably got fixed when we moved from the message manager to conduits.

The unit test from the patch above passes on the latest version of m-c.

Status: ASSIGNED → RESOLVED
Closed: 5 years ago
Resolution: --- → WORKSFORME
Attachment #9057883 - Attachment is obsolete: true
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: