webapprt/CommandLineHandler.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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).then(null, function (aError) {
    47         dump("Error: " + aError + "\n");
    48         Services.startup.quit(Ci.nsIAppStartup.eAttemptQuit);
    49       });
    50     }
    51   },
    53   /**
    54    * Handle debug command line option.
    55    *
    56    * @param cmdLine A nsICommandLine object.
    57    *
    58    * @returns the port number if it's specified, the default port number if
    59    *          the debug option is specified, NaN if the debug option isn't
    60    *          specified or the port number isn't valid.
    61    */
    62   _handleDebugMode: function(cmdLine) {
    63     // -debug [port]
    64     let idx = cmdLine.findFlag("debug", true);
    65     if (idx < 0) {
    66       return NaN;
    67     }
    69     let port;
    70     let portIdx = idx + 1;
    71     if (portIdx < cmdLine.length) {
    72       port = parseInt(cmdLine.getArgument(portIdx));
    73       if (port != NaN) {
    74         return port;
    75       }
    76     }
    78     return Services.prefs.getIntPref('devtools.debugger.remote-port');
    79   },
    81   _handleTestMode: function _handleTestMode(cmdLine, args) {
    82     // -test-mode [url]
    83     let idx = cmdLine.findFlag("test-mode", true);
    84     if (idx < 0)
    85       return false;
    86     let url;
    87     let urlIdx = idx + 1;
    88     if (urlIdx < cmdLine.length) {
    89       let potentialURL = cmdLine.getArgument(urlIdx);
    90       if (potentialURL && potentialURL[0] != "-") {
    91         try {
    92           url = Services.io.newURI(potentialURL, null, null);
    93         } catch (err) {
    94           throw Components.Exception(
    95             "-test-mode argument is not a valid URL: " + potentialURL,
    96             Components.results.NS_ERROR_INVALID_ARG);
    97         }
    98         cmdLine.removeArguments(urlIdx, urlIdx);
    99         args.setProperty("url", url.spec);
   100       }
   101     }
   102     cmdLine.removeArguments(idx, idx);
   103     return true;
   104   },
   106   helpInfo : "",
   107 };
   109 let components = [CommandLineHandler];
   110 this.NSGetFactory = XPCOMUtils.generateNSGetFactory(components);

mercurial