backout 29799f914cab, Bug 917642 - [Helix] Please update the helix blobs
[gecko.git] / webapprt / CommandLineHandler.js
blobf1ac29523be16acc8c6507d4f46b0918adb94dfe
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 const Cc = Components.classes;
6 const Ci = Components.interfaces;
7 const Cu = Components.utils;
9 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
10 Cu.import("resource://gre/modules/Services.jsm");
12 function CommandLineHandler() {}
14 CommandLineHandler.prototype = {
15   classID: Components.ID("{6d69c782-40a3-469b-8bfd-3ee366105a4a}"),
17   QueryInterface: XPCOMUtils.generateQI([Ci.nsICommandLineHandler]),
19   handle: function handle(cmdLine) {
20     let args = Cc["@mozilla.org/hash-property-bag;1"].
21                createInstance(Ci.nsIWritablePropertyBag);
22     let inTestMode = this._handleTestMode(cmdLine, args);
24     let debugPort = this._handleDebugMode(cmdLine);
25     if (!isNaN(debugPort)) {
26       Cu.import("resource://webapprt/modules/RemoteDebugger.jsm");
27       RemoteDebugger.init(debugPort);
28     }
30     if (inTestMode) {
31       // Open the mochitest shim window, which configures the runtime for tests.
32       Services.ww.openWindow(null,
33                              "chrome://webapprt/content/mochitest.xul",
34                              "_blank",
35                              "chrome,dialog=no",
36                              args);
37     } else {
38       // We're opening the window here in order to show it as soon as possible.
39       let window = Services.ww.openWindow(null,
40                                           "chrome://webapprt/content/webapp.xul",
41                                           "_blank",
42                                           "chrome,dialog=no,resizable,scrollbars,centerscreen",
43                                           null);
44       // Load the module to start up the app
45       Cu.import("resource://webapprt/modules/Startup.jsm");
46       startup(window);
47     }
48   },
50   /**
51    * Handle debug command line option.
52    *
53    * @param cmdLine A nsICommandLine object.
54    *
55    * @returns the port number if it's specified, the default port number if
56    *          the debug option is specified, NaN if the debug option isn't
57    *          specified or the port number isn't valid.
58    */
59   _handleDebugMode: function(cmdLine) {
60     // -debug [port]
61     let idx = cmdLine.findFlag("debug", true);
62     if (idx < 0) {
63       return NaN;
64     }
66     let port;
67     let portIdx = idx + 1;
68     if (portIdx < cmdLine.length) {
69       port = parseInt(cmdLine.getArgument(portIdx));
70       if (port != NaN) {
71         return port;
72       }
73     }
75     return Services.prefs.getIntPref('devtools.debugger.remote-port');
76   },
78   _handleTestMode: function _handleTestMode(cmdLine, args) {
79     // -test-mode [url]
80     let idx = cmdLine.findFlag("test-mode", true);
81     if (idx < 0)
82       return false;
83     let url;
84     let urlIdx = idx + 1;
85     if (urlIdx < cmdLine.length) {
86       let potentialURL = cmdLine.getArgument(urlIdx);
87       if (potentialURL && potentialURL[0] != "-") {
88         try {
89           url = Services.io.newURI(potentialURL, null, null);
90         } catch (err) {
91           throw Components.Exception(
92             "-test-mode argument is not a valid URL: " + potentialURL,
93             Components.results.NS_ERROR_INVALID_ARG);
94         }
95         cmdLine.removeArguments(urlIdx, urlIdx);
96         args.setProperty("url", url.spec);
97       }
98     }
99     cmdLine.removeArguments(idx, idx);
100     return true;
101   },
103   helpInfo : "",
106 let components = [CommandLineHandler];
107 this.NSGetFactory = XPCOMUtils.generateNSGetFactory(components);