1 // Copyright (c) 2008-2010 Kris Maglione <maglione.k at Gmail>
3 // This work is licensed for reuse under an MIT license. Details are
4 // given in the LICENSE.txt file included with this file.
6 function reportError(e) {
7 dump("dactyl: protocols: " + e + "\n" + (e.stack || Error().stack));
11 /* Adds support for data: URIs with chrome privileges
12 * and fragment identifiers.
14 * "chrome-data:" <content-type> [; <flag>]* "," [<data>]
16 * By Kris Maglione, ideas from Ed Anuff's nsChromeExtensionHandler.
19 var NAME = "protocols";
21 var Cc = Components.classes;
22 var Ci = Components.interfaces;
23 var Cu = Components.utils;
25 var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
26 var systemPrincipal = Cc["@mozilla.org/systemprincipal;1"].getService(Ci.nsIPrincipal);
28 var DNE = "resource://dactyl/content/does/not/exist";
31 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
33 function makeChannel(url, orig) {
36 return fakeChannel(orig);
38 if (typeof url === "function")
39 return let ([type, data] = url(orig)) StringChannel(data, type, orig);
42 return let ([type, data] = url) StringChannel(data, type, orig);
44 let uri = ioService.newURI(url, null, null);
45 return (new XMLChannel(uri)).channel;
52 function fakeChannel(orig) {
53 let channel = ioService.newChannel(DNE, null, null);
54 channel.originalURI = orig;
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));
62 function Factory(clas) ({
63 __proto__: clas.prototype,
64 createInstance: function (outer, iid) {
67 throw Components.results.NS_ERROR_NO_AGGREGATION;
69 clas.instance = new clas();
70 return clas.instance.QueryInterface(iid);
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),
87 scheme: "chrome-data",
89 allowPort: function (port, scheme) false,
90 protocolFlags: Ci.nsIProtocolHandler.URI_NORELATIVE
91 | Ci.nsIProtocolHandler.URI_NOAUTH
92 | Ci.nsIProtocolHandler.URI_IS_UI_RESOURCE,
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);
102 newChannel: function (uri) {
104 if (uri.scheme == this.scheme) {
105 let channel = ioService.newChannel(uri.spec.replace(/^.*?:\/*(.*)(?:#.*)?/, "data:$1"),
107 channel.contentCharset = "UTF-8";
108 channel.owner = systemPrincipal;
109 channel.originalURI = uri;
114 return fakeChannel(uri);
119 // Kill stupid validator warning.
120 this["wrapped" + "JSObject"] = this;
124 this.OVERLAY_MAP = {};
129 Cu.import("resource://dactyl/bootstrap.jsm");
130 if (!JSMLoader.initialized)
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;
138 // Doesn't belong here:
139 AboutHandler.prototype.register();
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),
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] || {}))
154 this.initialized = true;
159 allowPort: function (port, scheme) false,
161 | Ci.nsIProtocolHandler.URI_IS_UI_RESOURCE
162 | Ci.nsIProtocolHandler.URI_IS_LOCAL_RESOURCE,
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")
170 uri.init(uri.URLTYPE_STANDARD, this.defaultPort, spec, charset, baseURI);
174 newChannel: function newChannel(uri) {
176 if (/^help/.test(uri.host) && !("all" in this.FILE_MAP))
177 return redirect(uri.spec, uri, 1);
179 if (uri.host in this.providers)
180 return makeChannel(this.providers[uri.host](uri), uri);
182 let path = decodeURIComponent(uri.path.replace(/^\/|#.*/g, ""));
185 return makeChannel(this.pages[path] || "resource://dactyl-content/" + path, uri);
188 var channel = ioService.newChannel(uri.path.replace(/^\/(.*)(?:#.*)?/, "data:$1"),
195 channel.contentCharset = "UTF-8";
196 channel.owner = systemPrincipal;
197 channel.originalURI = uri;
200 return makeChannel(this.FILE_MAP[path], uri);
202 return makeChannel(this.OVERLAY_MAP[path], uri);
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);
211 return LocaleChannel("dactyl-locale", path, uri);
213 return LocaleChannel("dactyl-local-locale", path, uri);
221 return fakeChannel(uri);
224 // FIXME: Belongs elsewhere
225 _xpcom_categories: [{
226 category: "profile-after-change",
230 observe: function observe(subject, topic, data) {
231 if (topic === "profile-after-change") {
232 Cu.import("resource://dactyl/bootstrap.jsm");
234 require(global, "overlay");
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)
249 function StringChannel(data, contentType, uri) {
250 let channel = services.StreamChannel(uri);
251 channel.contentStream = services.CharsetConv("UTF-8").convertToInputStream(data);
253 channel.contentType = contentType;
254 channel.contentCharset = "UTF-8";
255 channel.owner = systemPrincipal;
257 channel.originalURI = uri;
261 function XMLChannel(uri, contentType) {
263 var channel = services.io.newChannelFromURI(uri);
264 var channelStream = channel.open();
267 this.channel = fakeChannel(uri);
272 this.sourceChannel = services.io.newChannelFromURI(uri);
273 this.pipe = services.Pipe(true, true, 0, 0, null);
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;
282 let stream = services.InputStream(channelStream);
283 let [, pre, doctype, url, open, post] = util.regexp(<![CDATA[
286 (<!DOCTYPE \s+ \S+ \s+) SYSTEM \s+ "([^"]*)"
291 ]]>, "x").exec(stream.read(4096));
292 this.writes.push(pre);
294 this.writes.push(doctype + "[\n");
296 this.writes.push(services.io.newChannel(url, null, null).open());
300 this.writes.push("\n]");
301 this.writes.push(post);
303 this.writes.push(channelStream);
307 XMLChannel.prototype = {
308 QueryInterface: XPCOMUtils.generateQI([Ci.nsIRequestObserver]),
309 writeNext: function () {
311 if (!this.writes.length)
312 this.pipe.outputStream.close();
314 let stream = this.writes.shift();
315 if (isString(stream))
316 stream = services.StringStream(stream);
318 services.StreamCopier(stream, this.pipe.outputStream, null,
319 false, true, 4096, true, false)
320 .asyncCopy(this, null);
328 onStartRequest: function (request, context) {},
329 onStopRequest: function (request, context, statusCode) {
334 function AboutHandler() {}
335 AboutHandler.prototype = {
336 register: function () {
338 JSMLoader.registerFactory(Factory(AboutHandler));
345 get classDescription() "About " + config.appName + " Page",
347 classID: Components.ID("81495d80-89ee-4c36-a88d-ea7c4e5ac63f"),
349 get contractID() "@mozilla.org/network/protocol/about;1?what=" + config.name,
351 QueryInterface: XPCOMUtils.generateQI([Ci.nsIAboutModule]),
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;
360 getURIFlags: function (uri) Ci.nsIAboutModule.ALLOW_SCRIPT,
363 // A hack to get information about interfaces.
364 // Doesn't belong here.
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;
375 getHelperForLanguage: function () null,
376 getInterfaces: function (count) { count.value = 0; }
379 if (XPCOMUtils.generateNSGetFactory)
380 var NSGetFactory = XPCOMUtils.generateNSGetFactory([ChromeData, Dactyl, Shim]);
382 var NSGetModule = XPCOMUtils.generateNSGetModule([ChromeData, Dactyl, Shim]);
383 var EXPORTED_SYMBOLS = ["NSGetFactory", "global"];
385 // vim: set fdm=marker sw=4 ts=4 et: