Noitidart
Noitidart

Reputation: 37238

How to catch error when firefox startup fails with nsIProcess

The below code uses nsIProcess and runAsync to launch a profile. If its in use it throws shows an alert. I want to suppress that alert and just focus that window. I tried giving it the -silent command line option but it doesn't work. Also if nsiProcess fails to launch it never gives me the error reason in the observer, it always just says finished. :(

Here's the copy paste code runnable from scratchpad:

var exe = FileUtils.getFile('XREExeF', []); //this gives path to executable
var process = Cc['@mozilla.org/process/util;1'].createInstance(Ci.nsIProcess);
process.init(exe);

var obsToLaunchProfAfterCreate = {
    observe: function (aSubject, aTopic, aData) {
        console.info('incoming obsToLaunchProfAfterCreate: aSubject = ' + aSubject + ' | aTopic = ' + aTopic + ' | aData = ' + aData, {
            aSubject: aSubject,
            aTopic: aTopic,
            aData: aData
        });
    }
};

var args = ['-P', 'default', '-no-remote'];
var a = process.runAsync(args, args.length, obsToLaunchProfAfterCreate);
console.log('a', a)

Upvotes: 1

Views: 163

Answers (1)

nmaier
nmaier

Reputation: 33162

I was looking through the nsAppRunner code and it appears there is no way to silence the error.

You might still use nsIToolkitService.profiles and then profile.lock() for named profiles, or nsIToolkitService.lockProfilePath() for arbitrary profiles to see if the profile can be opened before actually spawning the process. No real idea how to use that stuff correctly, just do some trial-and-error ;)

As for nsIProcess: Well, the process was successfully created or else it couldn't have shown the warning dialog ;) So in this case it is no wonder you get process-finished and not process-failed. process-failed will only be dispatched when either the OS failed to create the process or the process exited with (int32_t)exitCode < 0 (an OS will usually return just that when it fails to create the process).

To check for errors in general, use something along the lines of:

if (!(subject instanceof Ci.nsIProcess) || subject.exitValue != 0) {
  // error handling;
}

(Consult the docs for the process in question about return exit codes).

Unfortunately when Firefox exits due to a locked profile Firefox still does return an exit code of 0, so unfortunately you cannot check for the dialog specifically... So back to the idea of checking the lock before spawning the process.

Upvotes: 1

Related Questions