Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 "use strict";
7 const { Cc, Ci, Cu } = require("chrome");
9 const { Promise: promise } = require("resource://gre/modules/Promise.jsm");
11 const { OS } = Cu.import("resource://gre/modules/osfile.jsm", {});
12 const { TextEncoder, TextDecoder } = Cu.import('resource://gre/modules/commonjs/toolkit/loader.js', {});
13 const gcli = require("gcli/index");
15 loader.lazyGetter(this, "prefBranch", function() {
16 let prefService = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService);
17 return prefService.getBranch(null).QueryInterface(Ci.nsIPrefBranch2);
18 });
20 loader.lazyGetter(this, "supportsString", function() {
21 return Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
22 });
24 loader.lazyImporter(this, "NetUtil", "resource://gre/modules/NetUtil.jsm");
26 const PREF_DIR = "devtools.commands.dir";
28 /**
29 * Load all the .mozcmd files in the directory pointed to by PREF_DIR
30 * @return A promise of an array of items suitable for gcli.addItems or
31 * using in gcli.addItemsByModule
32 */
33 function loadItemsFromMozDir() {
34 let dirName = prefBranch.getComplexValue(PREF_DIR,
35 Ci.nsISupportsString).data.trim();
36 if (dirName == "") {
37 return promise.resolve([]);
38 }
40 // replaces ~ with the home directory path in unix and windows
41 if (dirName.indexOf("~") == 0) {
42 let dirService = Cc["@mozilla.org/file/directory_service;1"]
43 .getService(Ci.nsIProperties);
44 let homeDirFile = dirService.get("Home", Ci.nsIFile);
45 let homeDir = homeDirFile.path;
46 dirName = dirName.substr(1);
47 dirName = homeDir + dirName;
48 }
50 // statPromise resolves to nothing if dirName is a directory, or it
51 // rejects with an error message otherwise
52 let statPromise = OS.File.stat(dirName);
53 statPromise = statPromise.then(
54 function onSuccess(stat) {
55 if (!stat.isDir) {
56 throw new Error("'" + dirName + "' is not a directory.");
57 }
58 },
59 function onFailure(reason) {
60 if (reason instanceof OS.File.Error && reason.becauseNoSuchFile) {
61 throw new Error("'" + dirName + "' does not exist.");
62 } else {
63 throw reason;
64 }
65 }
66 );
68 // We need to return (a promise of) an array of items from the *.mozcmd
69 // files in dirName (which we can assume to be a valid directory now)
70 return statPromise.then(() => {
71 let itemPromises = [];
73 let iterator = new OS.File.DirectoryIterator(dirName);
74 let iterPromise = iterator.forEach(entry => {
75 if (entry.name.match(/.*\.mozcmd$/) && !entry.isDir) {
76 itemPromises.push(loadCommandFile(entry));
77 }
78 });
80 return iterPromise.then(() => {
81 iterator.close();
82 return promise.all(itemPromises).then((itemsArray) => {
83 return itemsArray.reduce((prev, curr) => {
84 return prev.concat(curr);
85 }, []);
86 });
87 }, reason => { iterator.close(); throw reason; });
88 });
89 }
91 exports.mozDirLoader = function(name) {
92 return loadItemsFromMozDir().then(items => {
93 return { items: items };
94 });
95 };
97 /**
98 * Load the commands from a single file
99 * @param OS.File.DirectoryIterator.Entry entry The DirectoryIterator
100 * Entry of the file containing the commands that we should read
101 */
102 function loadCommandFile(entry) {
103 let readPromise = OS.File.read(entry.path);
104 return readPromise = readPromise.then(array => {
105 let decoder = new TextDecoder();
106 let source = decoder.decode(array);
107 var principal = Cc["@mozilla.org/systemprincipal;1"]
108 .createInstance(Ci.nsIPrincipal);
110 let sandbox = new Cu.Sandbox(principal, {
111 sandboxName: entry.path
112 });
113 let data = Cu.evalInSandbox(source, sandbox, "1.8", entry.name, 1);
115 if (!Array.isArray(data)) {
116 console.error("Command file '" + entry.name + "' does not have top level array.");
117 return;
118 }
120 return data;
121 });
122 }
124 exports.items = [
125 {
126 name: "cmd",
127 get hidden() {
128 return !prefBranch.prefHasUserValue(PREF_DIR);
129 },
130 description: gcli.lookup("cmdDesc")
131 },
132 {
133 name: "cmd refresh",
134 description: gcli.lookup("cmdRefreshDesc"),
135 get hidden() {
136 return !prefBranch.prefHasUserValue(PREF_DIR);
137 },
138 exec: function(args, context) {
139 gcli.load();
141 let dirName = prefBranch.getComplexValue(PREF_DIR,
142 Ci.nsISupportsString).data.trim();
143 return gcli.lookupFormat("cmdStatus2", [ dirName ]);
144 }
145 },
146 {
147 name: "cmd setdir",
148 description: gcli.lookup("cmdSetdirDesc"),
149 params: [
150 {
151 name: "directory",
152 description: gcli.lookup("cmdSetdirDirectoryDesc"),
153 type: {
154 name: "file",
155 filetype: "directory",
156 existing: "yes"
157 },
158 defaultValue: null
159 }
160 ],
161 returnType: "string",
162 get hidden() {
163 return true; // !prefBranch.prefHasUserValue(PREF_DIR);
164 },
165 exec: function(args, context) {
166 supportsString.data = args.directory;
167 prefBranch.setComplexValue(PREF_DIR, Ci.nsISupportsString, supportsString);
169 gcli.load();
171 return gcli.lookupFormat("cmdStatus2", [ args.directory ]);
172 }
173 }
174 ];