File: CommandLineHandler.js

package info (click to toggle)
iceweasel 31.8.0esr-1~deb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,373,164 kB
  • sloc: cpp: 3,717,015; ansic: 1,797,386; python: 206,412; java: 180,622; asm: 133,557; xml: 89,501; sh: 72,014; perl: 22,087; makefile: 21,970; objc: 4,014; yacc: 1,995; pascal: 1,292; lex: 950; exp: 449; lisp: 228; awk: 211; php: 113; sed: 43; csh: 31; ada: 16; ruby: 3
file content (110 lines) | stat: -rw-r--r-- 3,701 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;

Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");

function CommandLineHandler() {}

CommandLineHandler.prototype = {
  classID: Components.ID("{6d69c782-40a3-469b-8bfd-3ee366105a4a}"),

  QueryInterface: XPCOMUtils.generateQI([Ci.nsICommandLineHandler]),

  handle: function handle(cmdLine) {
    let args = Cc["@mozilla.org/hash-property-bag;1"].
               createInstance(Ci.nsIWritablePropertyBag);
    let inTestMode = this._handleTestMode(cmdLine, args);

    let debugPort = this._handleDebugMode(cmdLine);
    if (!isNaN(debugPort)) {
      Cu.import("resource://webapprt/modules/RemoteDebugger.jsm");
      RemoteDebugger.init(debugPort);
    }

    if (inTestMode) {
      // Open the mochitest shim window, which configures the runtime for tests.
      Services.ww.openWindow(null,
                             "chrome://webapprt/content/mochitest.xul",
                             "_blank",
                             "chrome,dialog=no",
                             args);
    } else {
      // We're opening the window here in order to show it as soon as possible.
      let window = Services.ww.openWindow(null,
                                          "chrome://webapprt/content/webapp.xul",
                                          "_blank",
                                          "chrome,dialog=no,resizable,scrollbars,centerscreen",
                                          null);
      // Load the module to start up the app
      Cu.import("resource://webapprt/modules/Startup.jsm");
      startup(window).then(null, function (aError) {
        dump("Error: " + aError + "\n");
        Services.startup.quit(Ci.nsIAppStartup.eAttemptQuit);
      });
    }
  },

  /**
   * Handle debug command line option.
   *
   * @param cmdLine A nsICommandLine object.
   *
   * @returns the port number if it's specified, the default port number if
   *          the debug option is specified, NaN if the debug option isn't
   *          specified or the port number isn't valid.
   */
  _handleDebugMode: function(cmdLine) {
    // -debug [port]
    let idx = cmdLine.findFlag("debug", true);
    if (idx < 0) {
      return NaN;
    }

    let port;
    let portIdx = idx + 1;
    if (portIdx < cmdLine.length) {
      port = parseInt(cmdLine.getArgument(portIdx));
      if (port != NaN) {
        return port;
      }
    }

    return Services.prefs.getIntPref('devtools.debugger.remote-port');
  },

  _handleTestMode: function _handleTestMode(cmdLine, args) {
    // -test-mode [url]
    let idx = cmdLine.findFlag("test-mode", true);
    if (idx < 0)
      return false;
    let url;
    let urlIdx = idx + 1;
    if (urlIdx < cmdLine.length) {
      let potentialURL = cmdLine.getArgument(urlIdx);
      if (potentialURL && potentialURL[0] != "-") {
        try {
          url = Services.io.newURI(potentialURL, null, null);
        } catch (err) {
          throw Components.Exception(
            "-test-mode argument is not a valid URL: " + potentialURL,
            Components.results.NS_ERROR_INVALID_ARG);
        }
        cmdLine.removeArguments(urlIdx, urlIdx);
        args.setProperty("url", url.spec);
      }
    }
    cmdLine.removeArguments(idx, idx);
    return true;
  },

  helpInfo : "",
};

let components = [CommandLineHandler];
this.NSGetFactory = XPCOMUtils.generateNSGetFactory(components);