diff --git a/common/bootstrap.js b/common/bootstrap.js index 76f9c5c7..a09e50ec 100755 --- a/common/bootstrap.js +++ b/common/bootstrap.js @@ -88,28 +88,16 @@ FactoryProxy.prototype = { QueryInterface: XPCOMUtils.generateQI(Ci.nsIFactory), register: function () { dump("dactyl: bootstrap: register: " + this.classID + " " + this.contractID + "\n"); - manager.registerFactory(this.classID, - String(this.classID), - this.contractID, - this); - }, - unregister: function () { - dump("dactyl: bootstrap: unregister: " + this.classID + " " + this.contractID + "\n"); - manager.unregisterFactory(this.classID, this); + + JSMLoader.registerFactory(this); }, get module() { - try { - dump("dactyl: bootstrap: create module: " + this.contractID + "\n"); - Object.defineProperty(this, "module", { value: {}, enumerable: true }); - JSMLoader.load(this.url, this.module); - JSMLoader.registerGlobal(this.url, this.module.global); - return this.module; - } - catch (e) { - delete this.module; - reportError(e); - throw e; - } + dump("dactyl: bootstrap: create module: " + this.contractID + "\n"); + + Object.defineProperty(this, "module", { value: {}, enumerable: true }); + JSMLoader.load(this.url, this.module); + JSMLoader.registerGlobal(this.url, this.module.global); + return this.module; }, createInstance: function (iids) { return let (factory = this.module.NSGetFactory(this.classID)) @@ -194,9 +182,6 @@ function shutdown(data, reason) { services.observer.notifyObservers(null, "dactyl-cleanup", null); services.observer.notifyObservers(null, "dactyl-cleanup-modules", null); - for (let factory in values(components)) - // TODO: Categories; - factory.unregister(); } } diff --git a/common/components/protocols.js b/common/components/protocols.js index 2b47d414..4cb99a95 100644 --- a/common/components/protocols.js +++ b/common/components/protocols.js @@ -30,15 +30,14 @@ Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService); var systemPrincipal = Cc["@mozilla.org/systemprincipal;1"].getService(Ci.nsIPrincipal); -function dataURL(type, data) "data:" + (type || "application/xml;encoding=UTF-8") + "," + escape(data); function makeChannel(url, orig) { try { if (url == null) return fakeChannel(orig); if (typeof url === "function") - url = dataURL.apply(null, url()); - let uri = ioService.newURI(url, null, null); - let channel = ioService.newChannelFromURI(uri); + return let ([type, data] = url()) StringChannel(data, type, orig); + + let channel = ioService.newChannel(url, null, null); channel.contentCharset = "UTF-8"; channel.owner = systemPrincipal; channel.originalURI = orig; @@ -52,10 +51,11 @@ function makeChannel(url, orig) { function fakeChannel(orig) makeChannel("chrome://dactyl/content/does/not/exist", orig); function redirect(to, orig, time) { let html = .toXMLString(); - return makeChannel(dataURL('text/html', html), ioService.newURI(to, null, null)); + return StringChannel(html, "text/html", ioService.newURI(to, null, null)); } function Factory(clas) ({ + __proto__: clas.prototype, createInstance: function (outer, iid) { if (outer != null) throw Components.results.NS_ERROR_NO_AGGREGATION; @@ -108,7 +108,12 @@ function Dactyl() { this.pages = {}; Cu.import("resource://dactyl/base.jsm"); + require(global, "config"); + require(global, "services"); require(global, "util"); + + // Doesn't belong here: + AboutHandler.prototype.register(); } Dactyl.prototype = { contractID: "@mozilla.org/network/protocol;1?name=dactyl", @@ -180,14 +185,47 @@ Dactyl.prototype = { } }; +function StringChannel(data, contentType, uri) { + let channel = services.StreamChannel(uri); + channel.contentStream = services.StringStream(data); + channel.contentType = contentType; + channel.contentCharset = "UTF-8"; + channel.owner = systemPrincipal; + if (uri) + channel.originalURI = uri; + return channel; +} + +function XMLChannel(uri, contentType) { + this.sourceChannel = services.io.newChannelFromURI(uri); + this.pipe = services.Pipe(true, true, 0, 0, null); + + this.channel = services.StreamChannel(uri); + this.channel.contentStream = this.pipe.outputStream; + this.channel.contentType = contentType; + this.channel.contentCharset = "UTF-8"; + this.channel.owner = systemPrincipal; +} +XMLChannel.prototype = { + QueryInterface: XPCOMUtils.generateQI([Ci.nsIRequestObserver]), +}; + function AboutHandler() {} AboutHandler.prototype = { + register: function () { + try { + JSMLoader.registerFactory(Factory(AboutHandler)); + } + catch (e) { + util.reportError(e); + } + }, - classDescription: "About " + Dactyl.prototype.appName + " Page", + get classDescription() "About " + config.appName + " Page", classID: Components.ID("81495d80-89ee-4c36-a88d-ea7c4e5ac63f"), - contractID: "@mozilla.org/network/protocol/about;1?what=" + Dactyl.prototype.name, + get contractID() "@mozilla.org/network/protocol/about;1?what=" + config.name, QueryInterface: XPCOMUtils.generateQI([Ci.nsIAboutModule]), @@ -218,9 +256,9 @@ Shim.prototype = { }; if (XPCOMUtils.generateNSGetFactory) - var NSGetFactory = XPCOMUtils.generateNSGetFactory([AboutHandler, ChromeData, Dactyl, Shim]); + var NSGetFactory = XPCOMUtils.generateNSGetFactory([ChromeData, Dactyl, Shim]); else - var NSGetModule = XPCOMUtils.generateNSGetModule([AboutHandler, ChromeData, Dactyl, Shim]); + var NSGetModule = XPCOMUtils.generateNSGetModule([ChromeData, Dactyl, Shim]); var EXPORTED_SYMBOLS = ["NSGetFactory", "global"]; } catch (e) { reportError(e) } diff --git a/common/content/buffer.js b/common/content/buffer.js index 1fa14e19..9faa6f76 100644 --- a/common/content/buffer.js +++ b/common/content/buffer.js @@ -176,7 +176,7 @@ var Buffer = Module("buffer", { if (!(uri || doc.location)) return; - uri = uri || doc.documentURIObject; + uri = uri || util.newURI(doc.location.href); let args = { url: { toString: function () uri.spec, valueOf: function () uri }, title: doc.title @@ -225,7 +225,7 @@ var Buffer = Module("buffer", { else { // code which should happen for all (also background) newly loaded tabs goes here: if (doc != config.browser.contentDocument) - dactyl.echomsg({ domains: [doc.location.host], message: "Background tab loaded: " + (doc.title || doc.location.href) }, 3); + dactyl.echomsg({ domains: [util.getHost(doc.location)], message: "Background tab loaded: " + (doc.title || doc.location.href) }, 3); this._triggerLoadAutocmd("PageLoad", doc); } @@ -1012,7 +1012,7 @@ var Buffer = Module("buffer", { return true; }; - let url = isString(doc) ? util.newURI(doc) : doc.documentURIObject; + let url = isString(doc) ? util.newURI(doc) : util.newURI(doc.location.href); if (!isString(doc)) return io.withTempFiles(function (temp) { @@ -1358,7 +1358,7 @@ var Buffer = Module("buffer", { dactyl.assert(args.bang || !file.exists(), "E13: File exists (add ! to override)"); - chosenData = { file: file, uri: doc.documentURIObject }; + chosenData = { file: file, uri: util.newURI(doc.location.href) }; } // if browser.download.useDownloadDir = false then the "Save As" diff --git a/common/content/dactyl.js b/common/content/dactyl.js index 478c8384..b0723506 100644 --- a/common/content/dactyl.js +++ b/common/content/dactyl.js @@ -536,7 +536,7 @@ var Dactyl = Module("dactyl", XPCOM(Ci.nsISupportsWeakReference, ModuleBase), { * @private */ initDocument: function initDocument(doc) { - if (doc.documentURIObject.scheme === "dactyl") { + if (doc.location.protocol === "dactyl:") { dactyl.initHelp(); config.styleHelp(); } diff --git a/common/modules/base.jsm b/common/modules/base.jsm index 76e6f704..5455ebfc 100644 --- a/common/modules/base.jsm +++ b/common/modules/base.jsm @@ -7,7 +7,9 @@ if (!JSMLoader) var JSMLoader = { builtin: Components.utils.Sandbox(this), + factories: [], globals: {}, + manager: Components.manager.QueryInterface(Components.interfaces.nsIComponentRegistrar), stale: {}, load: function load(url, target) { if (this.stale[url]) { @@ -32,6 +34,10 @@ if (!JSMLoader) } Components.utils.import(url, target); }, + cleanup: function unregister() { + for each (let factory in this.factories.splice(0)) + this.manager.unregisterFactory(factory.classID, factory); + }, purge: function purge() { for (let [url, global] in Iterator(this.globals)) this.stale[url] = true; @@ -39,6 +45,13 @@ if (!JSMLoader) registerGlobal: function registerGlobal(uri, obj) { if (Cu.getGlobalForObject) this.globals[uri.replace(/.* -> /, "")] = Cu.getGlobalForObject(obj); + }, + registerFactory: function registerFactory(factory) { + this.manager.registerFactory(factory.classID, + String(factory.classID), + factory.contractID, + factory); + this.factories.push(factory); } }; diff --git a/common/modules/overlay.jsm b/common/modules/overlay.jsm index b871a7bf..9ce2bb93 100644 --- a/common/modules/overlay.jsm +++ b/common/modules/overlay.jsm @@ -26,6 +26,8 @@ var ModuleBase = Class("ModuleBase", { var Overlay = Module("Overlay", { init: function () { + services["dactyl:"]; // Hack. Force module initialization. + util.overlayWindow(config.overlayChrome, function (window) ({ init: function (document) { /** diff --git a/common/modules/services.jsm b/common/modules/services.jsm index f9190a3d..343b89ea 100644 --- a/common/modules/services.jsm +++ b/common/modules/services.jsm @@ -40,8 +40,8 @@ var Services = Module("Services", { this.add("favicon", "@mozilla.org/browser/favicon-service;1", Ci.nsIFaviconService); this.add("focus", "@mozilla.org/focus-manager;1", Ci.nsIFocusManager); this.add("fuel", "@mozilla.org/fuel/application;1", Ci.extIApplication); - this.add("history", "@mozilla.org/browser/global-history;2", [Ci.nsIBrowserHistory, Ci.nsIGlobalHistory3, - Ci.nsINavHistoryService, Ci.nsPIPlacesDatabase]); + this.add("history", "@mozilla.org/browser/global-history;2", + [Ci.nsIBrowserHistory, Ci.nsIGlobalHistory3, Ci.nsINavHistoryService, Ci.nsPIPlacesDatabase]); this.add("io", "@mozilla.org/network/io-service;1", Ci.nsIIOService); this.add("json", "@mozilla.org/dom/json;1", Ci.nsIJSON, "createInstance"); this.add("livemark", "@mozilla.org/browser/livemark-service;2", Ci.nsILivemarkService); @@ -69,15 +69,20 @@ var Services = Module("Services", { this.addClass("Find", "@mozilla.org/embedcomp/rangefind;1", Ci.nsIFind); this.addClass("HtmlConverter","@mozilla.org/widget/htmlformatconverter;1", Ci.nsIFormatConverter); this.addClass("HtmlEncoder", "@mozilla.org/layout/htmlCopyEncoder;1", Ci.nsIDocumentEncoder); + this.addClass("StreamChannel","@mozilla.org/network/input-stream-channel;1", + [Ci.nsIChannel, Ci.nsIInputStreamChannel, Ci.nsIRequest], "setURI"); this.addClass("Persist", "@mozilla.org/embedding/browser/nsWebBrowserPersist;1", Ci.nsIWebBrowserPersist); + this.addClass("Pipe", "@mozilla.org/pipe;1", Ci.nsIPipe, "init"); this.addClass("Process", "@mozilla.org/process/util;1", Ci.nsIProcess, "init"); this.addClass("String", "@mozilla.org/supports-string;1", Ci.nsISupportsString, "data"); + this.addClass("StringStream", "@mozilla.org/io/string-input-stream;1", Ci.nsIStringInputStream, "data"); this.addClass("Timer", "@mozilla.org/timer;1", Ci.nsITimer, "initWithCallback"); + this.addClass("StreamCopier", "@mozilla.org/network/async-stream-copier;1",Ci.nsIAsyncStreamCopier, "init"); this.addClass("Xmlhttp", "@mozilla.org/xmlextras/xmlhttprequest;1", Ci.nsIXMLHttpRequest); this.addClass("ZipReader", "@mozilla.org/libjar/zip-reader;1", Ci.nsIZipReader, "open"); this.addClass("ZipWriter", "@mozilla.org/zipwriter;1", Ci.nsIZipWriter); - if (!this.extensionManager) + if (!Ci.nsIExtensionManager || !this.extensionManager) Components.utils.import("resource://gre/modules/AddonManager.jsm"); else global.AddonManager = { @@ -162,15 +167,24 @@ var Services = Module("Services", { if (!ifaces) return res.wrappedJSObject; Array.concat(ifaces).forEach(function (iface) res.QueryInterface(iface)); - if (init && args.length) - if (callable(res[init])) + if (init && args.length) { + try { + var isCallable = callable(res[init]); + } + catch (e) {} // Ugh. + + if (isCallable) res[init].apply(res, args); else res[init] = args[0]; + } return res; } catch (e) { - dump("dactyl: Service creation failed for '" + classes + "': " + e + "\n"); + if (typeof util !== "undefined") + util.reportError(e); + else + dump("dactyl: Service creation failed for '" + classes + "': " + e + "\n" + (e.stack || Error(e).stack)); return null; } }, diff --git a/common/modules/util.jsm b/common/modules/util.jsm index 5ec8fbc4..df758275 100644 --- a/common/modules/util.jsm +++ b/common/modules/util.jsm @@ -478,7 +478,7 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]), stackLines: function (stack) { let lines = []; - let match, re = /([^]*?)(@.*?)(?:\n|$)/g; + let match, re = /([^]*?)(@[^@\n]*)(?:\n|$)/g; while (match = re.exec(stack)) lines.push(match[1].replace(/\n/g, "\\n").substr(0, 80) + match[2]); return lines; @@ -930,6 +930,8 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]), util.trapErrors(module.cleanup, module); } + JSMLoader.cleanup(); + services.observer.addObserver(this, "dactyl-rehash", true); }, "dactyl-rehash": function () { diff --git a/pentadactyl/chrome.manifest b/pentadactyl/chrome.manifest index cf9463d3..e1759241 100644 --- a/pentadactyl/chrome.manifest +++ b/pentadactyl/chrome.manifest @@ -21,6 +21,4 @@ component {9c8f2530-51c8-4d41-b356-319e0b155c44} components/protocols. contract @mozilla.org/network/protocol;1?name=dactyl {9c8f2530-51c8-4d41-b356-319e0b155c44} component {f4506a17-5b4d-4cd9-92d4-2eb4630dc388} components/protocols.js contract @dactyl.googlecode.com/base/xpc-interface-shim {f4506a17-5b4d-4cd9-92d4-2eb4630dc388} -component {81495d80-89ee-4c36-a88d-ea7c4e5ac63f} components/protocols.js -contract @mozilla.org/network/protocol/about;1?what=pentadactyl {81495d80-89ee-4c36-a88d-ea7c4e5ac63f}