1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/webapprt/CommandLineHandler.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,110 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +const Cc = Components.classes; 1.9 +const Ci = Components.interfaces; 1.10 +const Cu = Components.utils; 1.11 + 1.12 +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 1.13 +Cu.import("resource://gre/modules/Services.jsm"); 1.14 + 1.15 +function CommandLineHandler() {} 1.16 + 1.17 +CommandLineHandler.prototype = { 1.18 + classID: Components.ID("{6d69c782-40a3-469b-8bfd-3ee366105a4a}"), 1.19 + 1.20 + QueryInterface: XPCOMUtils.generateQI([Ci.nsICommandLineHandler]), 1.21 + 1.22 + handle: function handle(cmdLine) { 1.23 + let args = Cc["@mozilla.org/hash-property-bag;1"]. 1.24 + createInstance(Ci.nsIWritablePropertyBag); 1.25 + let inTestMode = this._handleTestMode(cmdLine, args); 1.26 + 1.27 + let debugPort = this._handleDebugMode(cmdLine); 1.28 + if (!isNaN(debugPort)) { 1.29 + Cu.import("resource://webapprt/modules/RemoteDebugger.jsm"); 1.30 + RemoteDebugger.init(debugPort); 1.31 + } 1.32 + 1.33 + if (inTestMode) { 1.34 + // Open the mochitest shim window, which configures the runtime for tests. 1.35 + Services.ww.openWindow(null, 1.36 + "chrome://webapprt/content/mochitest.xul", 1.37 + "_blank", 1.38 + "chrome,dialog=no", 1.39 + args); 1.40 + } else { 1.41 + // We're opening the window here in order to show it as soon as possible. 1.42 + let window = Services.ww.openWindow(null, 1.43 + "chrome://webapprt/content/webapp.xul", 1.44 + "_blank", 1.45 + "chrome,dialog=no,resizable,scrollbars,centerscreen", 1.46 + null); 1.47 + // Load the module to start up the app 1.48 + Cu.import("resource://webapprt/modules/Startup.jsm"); 1.49 + startup(window).then(null, function (aError) { 1.50 + dump("Error: " + aError + "\n"); 1.51 + Services.startup.quit(Ci.nsIAppStartup.eAttemptQuit); 1.52 + }); 1.53 + } 1.54 + }, 1.55 + 1.56 + /** 1.57 + * Handle debug command line option. 1.58 + * 1.59 + * @param cmdLine A nsICommandLine object. 1.60 + * 1.61 + * @returns the port number if it's specified, the default port number if 1.62 + * the debug option is specified, NaN if the debug option isn't 1.63 + * specified or the port number isn't valid. 1.64 + */ 1.65 + _handleDebugMode: function(cmdLine) { 1.66 + // -debug [port] 1.67 + let idx = cmdLine.findFlag("debug", true); 1.68 + if (idx < 0) { 1.69 + return NaN; 1.70 + } 1.71 + 1.72 + let port; 1.73 + let portIdx = idx + 1; 1.74 + if (portIdx < cmdLine.length) { 1.75 + port = parseInt(cmdLine.getArgument(portIdx)); 1.76 + if (port != NaN) { 1.77 + return port; 1.78 + } 1.79 + } 1.80 + 1.81 + return Services.prefs.getIntPref('devtools.debugger.remote-port'); 1.82 + }, 1.83 + 1.84 + _handleTestMode: function _handleTestMode(cmdLine, args) { 1.85 + // -test-mode [url] 1.86 + let idx = cmdLine.findFlag("test-mode", true); 1.87 + if (idx < 0) 1.88 + return false; 1.89 + let url; 1.90 + let urlIdx = idx + 1; 1.91 + if (urlIdx < cmdLine.length) { 1.92 + let potentialURL = cmdLine.getArgument(urlIdx); 1.93 + if (potentialURL && potentialURL[0] != "-") { 1.94 + try { 1.95 + url = Services.io.newURI(potentialURL, null, null); 1.96 + } catch (err) { 1.97 + throw Components.Exception( 1.98 + "-test-mode argument is not a valid URL: " + potentialURL, 1.99 + Components.results.NS_ERROR_INVALID_ARG); 1.100 + } 1.101 + cmdLine.removeArguments(urlIdx, urlIdx); 1.102 + args.setProperty("url", url.spec); 1.103 + } 1.104 + } 1.105 + cmdLine.removeArguments(idx, idx); 1.106 + return true; 1.107 + }, 1.108 + 1.109 + helpInfo : "", 1.110 +}; 1.111 + 1.112 +let components = [CommandLineHandler]; 1.113 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory(components);