]> git.donarmstrong.com Git - dactyl.git/blob - common/components/protocols.js
d14b3e4e804d5262a886415ea61c8d411e40cc93
[dactyl.git] / common / components / protocols.js
1 // Copyright (c) 2008-2010 Kris Maglione <maglione.k at Gmail>
2 //
3 // This work is licensed for reuse under an MIT license. Details are
4 // given in the LICENSE.txt file included with this file.
5 "use strict";
6 function reportError(e) {
7     dump("dactyl: protocols: " + e + "\n" + (e.stack || Error().stack));
8     Cu.reportError(e);
9 }
10
11 /* Adds support for data: URIs with chrome privileges
12  * and fragment identifiers.
13  *
14  * "chrome-data:" <content-type> [; <flag>]* "," [<data>]
15  *
16  * By Kris Maglione, ideas from Ed Anuff's nsChromeExtensionHandler.
17  */
18
19 var NAME = "protocols";
20 var global = this;
21 var Cc = Components.classes;
22 var Ci = Components.interfaces;
23 var Cu = Components.utils;
24
25 var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
26 var systemPrincipal = Cc["@mozilla.org/systemprincipal;1"].getService(Ci.nsIPrincipal);
27
28 var DNE = "resource://dactyl/content/does/not/exist";
29 var _DNE;
30
31 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
32
33 function makeChannel(url, orig) {
34     try {
35         if (url == null)
36             return fakeChannel(orig);
37
38         if (typeof url === "function")
39             return let ([type, data] = url(orig)) StringChannel(data, type, orig);
40
41         if (isArray(url))
42             return let ([type, data] = url) StringChannel(data, type, orig);
43
44         let uri = ioService.newURI(url, null, null);
45         return (new XMLChannel(uri)).channel;
46     }
47     catch (e) {
48         util.reportError(e);
49         throw e;
50     }
51 }
52 function fakeChannel(orig) {
53     let channel = ioService.newChannel(DNE, null, null);
54     channel.originalURI = orig;
55     return channel;
56 }
57 function redirect(to, orig, time) {
58     let html = <html><head><meta http-equiv="Refresh" content={(time || 0) + ";" + to}/></head></html>.toXMLString();
59     return StringChannel(html, "text/html", ioService.newURI(to, null, null));
60 }
61
62 function Factory(clas) ({
63     __proto__: clas.prototype,
64     createInstance: function (outer, iid) {
65         try {
66             if (outer != null)
67                 throw Components.results.NS_ERROR_NO_AGGREGATION;
68             if (!clas.instance)
69                 clas.instance = new clas();
70             return clas.instance.QueryInterface(iid);
71         }
72         catch (e) {
73             reportError(e);
74             throw e;
75         }
76     }
77 });
78
79 function ChromeData() {}
80 ChromeData.prototype = {
81     contractID:       "@mozilla.org/network/protocol;1?name=chrome-data",
82     classID:          Components.ID("{c1b67a07-18f7-4e13-b361-2edcc35a5a0d}"),
83     classDescription: "Data URIs with chrome privileges",
84     QueryInterface:   XPCOMUtils.generateQI([Ci.nsIProtocolHandler]),
85     _xpcom_factory:   Factory(ChromeData),
86
87     scheme: "chrome-data",
88     defaultPort: -1,
89     allowPort: function (port, scheme) false,
90     protocolFlags: Ci.nsIProtocolHandler.URI_NORELATIVE
91          | Ci.nsIProtocolHandler.URI_NOAUTH
92          | Ci.nsIProtocolHandler.URI_IS_UI_RESOURCE,
93
94     newURI: function (spec, charset, baseURI) {
95         var uri = Components.classes["@mozilla.org/network/standard-url;1"]
96                             .createInstance(Components.interfaces.nsIStandardURL)
97                             .QueryInterface(Components.interfaces.nsIURI);
98         uri.init(uri.URLTYPE_STANDARD, this.defaultPort, spec, charset, null);
99         return uri;
100     },
101
102     newChannel: function (uri) {
103         try {
104             if (uri.scheme == this.scheme) {
105                 let channel = ioService.newChannel(uri.spec.replace(/^.*?:\/*(.*)(?:#.*)?/, "data:$1"),
106                                                    null, null);
107                 channel.contentCharset = "UTF-8";
108                 channel.owner = systemPrincipal;
109                 channel.originalURI = uri;
110                 return channel;
111             }
112         }
113         catch (e) {}
114         return fakeChannel(uri);
115     }
116 };
117
118 function Dactyl() {
119     // Kill stupid validator warning.
120     this["wrapped" + "JSObject"] = this;
121
122     this.HELP_TAGS = {};
123     this.FILE_MAP = {};
124     this.OVERLAY_MAP = {};
125
126     this.pages = {};
127     this.providers = {};
128
129     Cu.import("resource://dactyl/bootstrap.jsm");
130     if (!JSMLoader.initialized)
131         JSMLoader.init();
132     JSMLoader.load("base.jsm", global);
133     require(global, "config");
134     require(global, "services");
135     require(global, "util");
136     _DNE = ioService.newChannel(DNE, null, null).name;
137
138     // Doesn't belong here:
139     AboutHandler.prototype.register();
140 }
141 Dactyl.prototype = {
142     contractID:       "@mozilla.org/network/protocol;1?name=dactyl",
143     classID:          Components.ID("{9c8f2530-51c8-4d41-b356-319e0b155c44}"),
144     classDescription: "Dactyl utility protocol",
145     QueryInterface:   XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsIProtocolHandler]),
146     _xpcom_factory:   Factory(Dactyl),
147
148     init: function (obj) {
149         for each (let prop in ["HELP_TAGS", "FILE_MAP", "OVERLAY_MAP"]) {
150             this[prop] = this[prop].constructor();
151             for (let [k, v] in Iterator(obj[prop] || {}))
152                 this[prop][k] = v;
153         }
154         this.initialized = true;
155     },
156
157     scheme: "dactyl",
158     defaultPort: -1,
159     allowPort: function (port, scheme) false,
160     protocolFlags: 0
161          | Ci.nsIProtocolHandler.URI_IS_UI_RESOURCE
162          | Ci.nsIProtocolHandler.URI_IS_LOCAL_RESOURCE,
163
164     newURI: function newURI(spec, charset, baseURI) {
165         var uri = Cc["@mozilla.org/network/standard-url;1"]
166                         .createInstance(Ci.nsIStandardURL)
167                         .QueryInterface(Ci.nsIURI);
168         if (baseURI && baseURI.host === "data")
169             baseURI = null;
170         uri.init(uri.URLTYPE_STANDARD, this.defaultPort, spec, charset, baseURI);
171         return uri;
172     },
173
174     newChannel: function newChannel(uri) {
175         try {
176             if (/^help/.test(uri.host) && !("all" in this.FILE_MAP))
177                 return redirect(uri.spec, uri, 1);
178
179             if (uri.host in this.providers)
180                 return makeChannel(this.providers[uri.host](uri), uri);
181
182             let path = decodeURIComponent(uri.path.replace(/^\/|#.*/g, ""));
183             switch(uri.host) {
184             case "content":
185                 return makeChannel(this.pages[path] || "resource://dactyl-content/" + path, uri);
186             case "data":
187                 try {
188                     var channel = ioService.newChannel(uri.path.replace(/^\/(.*)(?:#.*)?/, "data:$1"),
189                                                        null, null);
190                 }
191                 catch (e) {
192                     var error = e;
193                     break;
194                 }
195                 channel.contentCharset = "UTF-8";
196                 channel.owner = systemPrincipal;
197                 channel.originalURI = uri;
198                 return channel;
199             case "help":
200                 return makeChannel(this.FILE_MAP[path], uri);
201             case "help-overlay":
202                 return makeChannel(this.OVERLAY_MAP[path], uri);
203             case "help-tag":
204                 let tag = decodeURIComponent(uri.path.substr(1));
205                 if (tag in this.FILE_MAP)
206                     return redirect("dactyl://help/" + tag, uri);
207                 if (tag in this.HELP_TAGS)
208                     return redirect("dactyl://help/" + this.HELP_TAGS[tag] + "#" + tag.replace(/#/g, encodeURIComponent), uri);
209                 break;
210             case "locale":
211                 return LocaleChannel("dactyl-locale", path, uri);
212             case "locale-local":
213                 return LocaleChannel("dactyl-local-locale", path, uri);
214             }
215         }
216         catch (e) {
217             util.reportError(e);
218         }
219         if (error)
220             throw error;
221         return fakeChannel(uri);
222     },
223
224     // FIXME: Belongs elsewhere
225     _xpcom_categories: [{
226         category: "profile-after-change",
227         entry: "m-dactyl"
228     }],
229
230     observe: function observe(subject, topic, data) {
231         if (topic === "profile-after-change") {
232             Cu.import("resource://dactyl/bootstrap.jsm");
233             JSMLoader.init();
234             require(global, "overlay");
235         }
236     }
237 };
238
239 function LocaleChannel(pkg, path, orig) {
240     for each (let locale in [config.locale, "en-US"])
241         for each (let sep in "-/") {
242             var channel = makeChannel(["resource:/", pkg + sep + config.locale, path].join("/"), orig);
243             if (channel.name !== _DNE)
244                 return channel;
245         }
246     return channel;
247 }
248
249 function StringChannel(data, contentType, uri) {
250     let channel = services.StreamChannel(uri);
251     channel.contentStream = services.StringStream(data);
252     if (contentType)
253         channel.contentType = contentType;
254     channel.contentCharset = "UTF-8";
255     channel.owner = systemPrincipal;
256     if (uri)
257         channel.originalURI = uri;
258     return channel;
259 }
260
261 function XMLChannel(uri, contentType) {
262     try {
263         var channel = services.io.newChannelFromURI(uri);
264         var channelStream = channel.open();
265     }
266     catch (e) {
267         this.channel = fakeChannel(uri);
268         return;
269     }
270
271     this.uri = uri;
272     this.sourceChannel = services.io.newChannelFromURI(uri);
273     this.pipe = services.Pipe(true, true, 0, 0, null);
274     this.writes = [];
275
276     this.channel = services.StreamChannel(uri);
277     this.channel.contentStream = this.pipe.inputStream;
278     this.channel.contentType = contentType || channel.contentType;
279     this.channel.contentCharset = "UTF-8";
280     this.channel.owner = systemPrincipal;
281
282     let stream = services.InputStream(channelStream);
283     let [, pre, doctype, url, open, post] = util.regexp(<![CDATA[
284             ^ ([^]*?)
285             (?:
286                 (<!DOCTYPE \s+ \S+ \s+) SYSTEM \s+ "([^"]*)"
287                 (\s+ \[)?
288                 ([^]*)
289             )?
290             $
291         ]]>, "x").exec(stream.read(4096));
292     this.writes.push(pre);
293     if (doctype) {
294         this.writes.push(doctype + "[\n");
295         try {
296             this.writes.push(services.io.newChannel(url, null, null).open())
297         }
298         catch (e) {}
299         if (!open)
300             this.writes.push("\n]");
301         this.writes.push(post)
302     }
303     this.writes.push(channelStream);
304
305     this.writeNext();
306 }
307 XMLChannel.prototype = {
308     QueryInterface:   XPCOMUtils.generateQI([Ci.nsIRequestObserver]),
309     writeNext: function () {
310         try {
311             if (!this.writes.length)
312                 this.pipe.outputStream.close();
313             else {
314                 let stream = this.writes.shift();
315                 if (isString(stream))
316                     stream = services.StringStream(stream);
317
318                 services.StreamCopier(stream, this.pipe.outputStream, null,
319                                       false, true, 4096, true, false)
320                         .asyncCopy(this, null);
321             }
322         }
323         catch (e) {
324             util.reportError(e);
325         }
326     },
327
328     onStartRequest: function (request, context) {},
329     onStopRequest: function (request, context, statusCode) {
330         this.writeNext();
331     }
332 };
333
334 function AboutHandler() {}
335 AboutHandler.prototype = {
336     register: function () {
337         try {
338             JSMLoader.registerFactory(Factory(AboutHandler));
339         }
340         catch (e) {
341             util.reportError(e);
342         }
343     },
344
345     get classDescription() "About " + config.appName + " Page",
346
347     classID: Components.ID("81495d80-89ee-4c36-a88d-ea7c4e5ac63f"),
348
349     get contractID() "@mozilla.org/network/protocol/about;1?what=" + config.name,
350
351     QueryInterface: XPCOMUtils.generateQI([Ci.nsIAboutModule]),
352
353     newChannel: function (uri) {
354         let channel = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService)
355                           .newChannel("dactyl://content/about.xul", null, null);
356         channel.originalURI = uri;
357         return channel;
358     },
359
360     getURIFlags: function (uri) Ci.nsIAboutModule.ALLOW_SCRIPT,
361 };
362
363 // A hack to get information about interfaces.
364 // Doesn't belong here.
365 function Shim() {}
366 Shim.prototype = {
367     contractID:       "@dactyl.googlecode.com/base/xpc-interface-shim",
368     classID:          Components.ID("{f4506a17-5b4d-4cd9-92d4-2eb4630dc388}"),
369     classDescription: "XPCOM empty interface shim",
370     QueryInterface:   function (iid) {
371         if (iid.equals(Ci.nsISecurityCheckedComponent))
372             throw Components.results.NS_ERROR_NO_INTERFACE;
373         return this;
374     },
375     getHelperForLanguage: function () null,
376     getInterfaces: function (count) { count.value = 0; }
377 };
378
379 if (XPCOMUtils.generateNSGetFactory)
380     var NSGetFactory = XPCOMUtils.generateNSGetFactory([ChromeData, Dactyl, Shim]);
381 else
382     var NSGetModule = XPCOMUtils.generateNSGetModule([ChromeData, Dactyl, Shim]);
383 var EXPORTED_SYMBOLS = ["NSGetFactory", "global"];
384
385 // vim: set fdm=marker sw=4 ts=4 et: