-->

Firefox Native Messaging runtime.LastError not giv

2019-09-16 15:25发布

问题:

I am trying to check whether the Native app is installed or not , If it is not I have to prompt the user to download it from the webpage. For chrome I used to achieve by checking the error messages from runtime.LastError. However in case of Firefox it gives error only in console No such native application extension_name and not catching it in the runtime.LastError method.

Is there any way that we can identify whether corresponding Native app is installed or not ? I am facing issue when Native app is not installed and browser.runtime.lastError is not giving any error.

Can you please suggest if there is any way in Firefox Webextension that we can catch such errors and identify it in code whether the corresponding Native app is installed or not on the user machine.

It will really helpful if someone can provide some info on this.

for e.g. :

startNativeApp: function(force){
        // note that when the native app is opened and ready, it will call "_ABC_onAgentReady"
        ABC.log('Starting native app.');
        if (!ABC.appConnected) {
            try {
                ABC.nativeAppPort = browser.runtime.connectNative(_ABC_native_app_id);
                ABC.nativeAppPort.onMessage.addListener(ABC.onNativeMessageReceived);
                ABC.nativeAppPort.onDisconnect.addListener(ABC.onNativeAppDisconnected);
                ABC.appInstalled = true;
                ABC.appConnected = true;
            } catch(e) {
                ABC.log('Error starting native app: ' + e.message, 'ERR');
            }
        } else if (force === true) {
            ABC.log('Native app is already running; attempting to stop and will restart in 750ms.');
            ABC.stopNativeApp();
            setTimeout(function() { ABC.startNativeApp(true); }, 750);
        }
},


onNativeAppDisconnected: function(message) {
        console.log("ABC LastError : "+browser.runtime.lastError);
        console.log("ABC LastError : "+ABC.nativeAppPort.error);
        console.log("ABC LastError : "+JSON.stringify(message));
        ABC.appConnected = false;
        ABC.nativeAppPort = null;
        ABC.appInstalled = false;
        if (browser.runtime.lastError && (browser.runtime.lastError.message.indexOf("No such native application") !== -1 )) {
            ABC.appInstalled = false;
        }
        // cleanup: reset the sig data so that it is re-requested on the next scan
        _ABC_sa_data = "";
        _ABC_sigs = "";
        if (browser.storage && browser.storage.local) {
            browser.storage.local.set({ uid: _ABC_be_uid }, null);
        }

        ABC.log('Send message to page to stop.');
        ABC.sendMessageToPage({ onNativeAppDisconnected: '' });
        ABC.log('Native app disconnected.');
    },

回答1:

Issue here was that port.error was not giving any error response in Firefox versions less than 52 , Due to which I was facing problem in identifying whether native app is installed or not.

After discussion on Mozilla Community (https://discourse.mozilla-community.org/t/firefox-native-messaging-runtime-lasterror-not-giving-any-errors-in-case-of-no-native-application-installed-on-connectnative/12880/4) , we found that it is actually missed and a bug is already reported : https://bugzilla.mozilla.org/show_bug.cgi?id=12994116 which will be resolved in Firefox 52.

However , I need to support Firefox 50 also , so the alternate I am using is to call native application in starting to find out whether it is installed or not.

If I got back response than it is installed otherwise it is not. However specific error messages will be available from Firefox52.



回答2:

In Firefox there is no runtime.lastError.

The listener function you pass to runtime.Port.onDisconnect isn't passed the message, it's passed the port itself.

You then want port.error.

See the documentation for onDisconnect here https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/runtime/Port