Bug 1852754: part 9) Add tests for dynamically loading <link rel="prefetch"> elements...
[gecko.git] / toolkit / components / DefaultCLH.sys.mjs
blob2603c5a50cd0879aac7701ed9359e151d08f1975
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
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 const nsICommandLineHandler = Ci.nsICommandLineHandler;
6 const nsIPrefBranch = Ci.nsIPrefBranch;
7 const nsIWindowWatcher = Ci.nsIWindowWatcher;
8 const nsIProperties = Ci.nsIProperties;
9 const nsIFile = Ci.nsIFile;
10 const nsISimpleEnumerator = Ci.nsISimpleEnumerator;
12 /**
13  * This file provides a generic default command-line handler.
14  *
15  * It opens the chrome window specified by the pref "toolkit.defaultChromeURI"
16  * with the flags specified by the pref "toolkit.defaultChromeFeatures"
17  * or "chrome,dialog=no,all" is it is not available.
18  * The arguments passed to the window are the nsICommandLine instance.
19  *
20  * It doesn't do anything if the pref "toolkit.defaultChromeURI" is unset.
21  */
23 function getDirectoryService() {
24   return Cc["@mozilla.org/file/directory_service;1"].getService(nsIProperties);
27 export function nsDefaultCLH() {}
28 nsDefaultCLH.prototype = {
29   classID: Components.ID("{6ebc941a-f2ff-4d56-b3b6-f7d0b9d73344}"),
31   /* nsISupports */
33   QueryInterface: ChromeUtils.generateQI([nsICommandLineHandler]),
35   /* nsICommandLineHandler */
37   handle: function clh_handle(cmdLine) {
38     var printDir;
39     while ((printDir = cmdLine.handleFlagWithParam("print-xpcom-dir", false))) {
40       var out = 'print-xpcom-dir("' + printDir + '"): ';
41       try {
42         out += getDirectoryService().get(printDir, nsIFile).path;
43       } catch (e) {
44         out += "<Not Provided>";
45       }
47       dump(out + "\n");
48       console.error(out);
49     }
51     var printDirList;
52     while (
53       (printDirList = cmdLine.handleFlagWithParam("print-xpcom-dirlist", false))
54     ) {
55       out = 'print-xpcom-dirlist("' + printDirList + '"): ';
56       try {
57         for (let file of getDirectoryService().get(
58           printDirList,
59           nsISimpleEnumerator
60         )) {
61           out += file.path + ";";
62         }
63       } catch (e) {
64         out += "<Not Provided>";
65       }
67       dump(out + "\n");
68       console.error(out);
69     }
71     if (cmdLine.handleFlag("silent", false)) {
72       cmdLine.preventDefault = true;
73     }
75     if (cmdLine.preventDefault) {
76       return;
77     }
79     var prefs =
80       Cc["@mozilla.org/preferences-service;1"].getService(nsIPrefBranch);
82     try {
83       var singletonWindowType = prefs.getCharPref(
84         "toolkit.singletonWindowType"
85       );
87       var win = Services.wm.getMostRecentWindow(singletonWindowType);
88       if (win) {
89         win.focus();
90         cmdLine.preventDefault = true;
91         return;
92       }
93     } catch (e) {}
95     // if the pref is missing, ignore the exception
96     try {
97       var chromeURI = prefs.getCharPref("toolkit.defaultChromeURI");
99       var flags = prefs.getCharPref(
100         "toolkit.defaultChromeFeatures",
101         "chrome,dialog=no,all"
102       );
104       var wwatch =
105         Cc["@mozilla.org/embedcomp/window-watcher;1"].getService(
106           nsIWindowWatcher
107         );
108       wwatch.openWindow(null, chromeURI, "_blank", flags, cmdLine);
109     } catch (e) {}
110   },
112   helpInfo: "",