Bug 276588 - Rework command line handling in the toolkit to use a generic API - see COMMANDLINES_20050109_BRANCH for more detailed cvs logs r=darin,caillon,mscott,mconnor

This commit is contained in:
bsmedberg@covad.net
2005-01-17 18:50:18 +00:00
parent d9491d1f0b
commit 7b598ff009
45 changed files with 2442 additions and 1163 deletions

View File

@@ -35,140 +35,107 @@
*
* ***** END LICENSE BLOCK ***** */
/* This file implements the nsICmdLineHandler interface. See nsICmdLineHandler.idl
* at http://lxr.mozilla.org/seamonkey/source/xpfe/appshell/public/nsICmdLineHandler.idl.
/* This file implements the nsICommandLineHandler interface.
*
* This component handles the startup command line argument of the form:
* -setDefaultBrowser
* by making the current executable the "default browser." It accomplishes
* that via use of the nsIWindowsHooks interface (see implementation below).
*
* The module is registered under the contractid
* "@mozilla.org/commandlinehandler/general-startup;1?type=setDefaultBrowser"
*
* The implementation consists of a JavaScript "class" named nsKillAll,
* comprised of:
* - a JS constructor function
* - a prototype providing all the interface methods and implementation stuff
*
* In addition, this file implements an nsIModule object that registers the
* nsSetDefaultBrowser component.
* by making the current executable the "default browser."
*/
/* ctor
*/
function nsSetDefaultBrowser() {
}
nsSetDefaultBrowser.prototype = {
/* nsISupports */
QueryInterface: function nsSetDefault_QI(iid) {
if (!iid.equals(Components.interfaces.nsICommandLineHandler) &&
!iid.equals(Components.interfaces.nsISupports))
throw Components.results.NS_ERROR_NO_INTERFACE;
// nsICmdLineHandler interface
get commandLineArgument() { throw Components.results.NS_ERROR_NOT_IMPLEMENTED; },
get prefNameForStartup() { throw Components.results.NS_ERROR_NOT_IMPLEMENTED; },
return this;
},
get chromeUrlForTask() {
// First, get shell service
var shell;
try {
shell = Components.classes["@mozilla.org/browser/shell-service;1"]
/* nsICommandLineHandler */
handle : function nsSetDefault_handle(cmdline) {
if (cmdline.handleFlag("setDefaultBrowser", false)) {
var shell = Components.classes["@mozilla.org/browser/shell-service;1"]
.getService(Components.interfaces.nsIShellService);
} catch(e) { }
if (!shell)
throw Components.results.NS_ERROR_NOT_AVAILABLE;
shell.setDefaultBrowser(true, true);
// Now, get the cmd line service.
var cmdLineService = Components.classes[ "@mozilla.org/appshell/commandLineService;1" ]
.getService( Components.interfaces.nsICmdLineService );
// See if "-setDefaultBrowser" was specified. The value will be "1" if
// -setDefaultBrowser is in the service's list of cmd line arguments, and
// null otherwise. -setDefaultBrowser will only be in the service's
// arg list if the application was not already running. That's because
// if it was already running, then the service reflects the arguments
// that were specified when *that* process was started, *not* the ones
// passed via IPC from the second instance.
var option = cmdLineService.getCmdLineValue( "-setDefaultBrowser" );
if (!option) {
// Already running, so we don't have to worry about opening
// another window, etc.
throw Components.results.NS_ERROR_NOT_AVAILABLE;
}
// Return URL for dummy window that will auto-close. We do this rather
// than throw NS_ERROR_NOT_AVAILABLE, which *should* work, because it
// seems that if we don't open a window, we get a crash when trying to
// release this (or some other) JS component during XPCOM shutdown.
return "chrome://global/content/dummyWindow.xul";
},
get helpText() { throw Components.results.NS_ERROR_NOT_IMPLEMENTED; },
get handlesArgs() { return false; },
get defaultArgs() { throw Components.results.NS_ERROR_NOT_IMPLEMENTED; },
get openWindowWithArgs() { throw Components.results.NS_ERROR_NOT_IMPLEMENTED; },
// nsISupports interface
// This "class" supports nsICmdLineHandler and nsISupports.
QueryInterface: function (iid) {
if (!iid.equals(Components.interfaces.nsICmdLineHandler) &&
!iid.equals(Components.interfaces.nsISupports)) {
throw Components.results.NS_ERROR_NO_INTERFACE;
}
return this;
},
// This Component's module implementation. All the code below is used to get this
// component registered and accessible via XPCOM.
module: {
// registerSelf: Register this component.
registerSelf: function (compMgr, fileSpec, location, type) {
var compReg = compMgr.QueryInterface( Components.interfaces.nsIComponentRegistrar );
compReg.registerFactoryLocation( this.cid,
"Default Browser Component",
this.contractId,
fileSpec,
location,
type );
},
// getClassObject: Return this component's factory object.
getClassObject: function (compMgr, cid, iid) {
if (!cid.equals(this.cid))
throw Components.results.NS_ERROR_NO_INTERFACE;
if (!iid.equals(Components.interfaces.nsIFactory))
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
return this.factory;
},
/* CID for this class */
cid: Components.ID("{C66E05DC-509C-4972-A1F2-EE5AC34B9800}"),
/* Contract ID for this class */
contractId: "@mozilla.org/commandlinehandler/general-startup;1?type=setDefaultBrowser",
/* factory object */
factory: {
// createInstance: Return a new nsSetDefaultBrowser object.
createInstance: function (outer, iid) {
if (outer != null)
throw Components.results.NS_ERROR_NO_AGGREGATION;
return (new nsSetDefaultBrowser()).QueryInterface(iid);
}
},
// canUnload: n/a (returns true)
canUnload: function(compMgr) {
return true;
}
}
},
helpText : " -setDefaultBrowser Set this app as the default browser.\n",
};
// This Component's module and factory implementation.
const contractID = "@mozilla.org/browser/default-browser-clh;1";
const CID = Components.ID("{F57899D0-4E2C-4ac6-9E29-50C736103B0C}");
var ModuleAndFactory = {
/* nsISupports */
QueryInterface: function nsSetDefault_QI(iid) {
if (!iid.equals(Components.interfaces.nsIModule) &&
!iid.equals(Components.interfaces.nsIFactory) &&
!iid.equals(Components.interfaces.nsISupports))
throw Components.results.NS_ERROR_NO_INTERFACE;
return this;
},
/* nsIModule */
getClassObject: function (compMgr, cid, iid) {
if (!cid.equals(CID))
throw Components.results.NS_ERROR_NO_INTERFACE;
return this.QueryInterface(iid);
},
registerSelf: function mod_regself(compMgr, fileSpec, location, type) {
var compReg =
compMgr.QueryInterface( Components.interfaces.nsIComponentRegistrar );
compReg.registerFactoryLocation( CID,
"Default Browser Cmdline Handler",
contractID,
fileSpec,
location,
type );
var catMan = Components.classes["@mozilla.org/categorymanager;1"]
.getService(Components.interfaces.nsICategoryManager);
catMan.addCategoryEntry("command-line-handler",
"m-setdefaultbrowser",
contractID, true, true);
},
unregisterSelf : function mod_unregself(compMgr, location, type) {
var catMan = Components.classes["@mozilla.org/categorymanager;1"]
.getService(Components.interfaces.nsICategoryManager);
catMan.deleteCategoryEntry("command-line-handler",
"m-setdefaultbrowser", true);
},
canUnload: function(compMgr) {
return true;
},
/* nsIFactory */
createInstance: function mod_CI(outer, iid) {
if (outer != null)
throw Components.results.NS_ERROR_NO_AGGREGATION;
return new nsSetDefaultBrowser().QueryInterface(iid);
},
lockFactory : function mod_lock(lock) {
/* no-op */
}
}
// NSGetModule: Return the nsIModule object.
function NSGetModule(compMgr, fileSpec) {
return nsSetDefaultBrowser.prototype.module;
return ModuleAndFactory;
}