]> git.donarmstrong.com Git - dactyl.git/blobdiff - common/modules/services.jsm
Import r6948 from upstream hg supporting Firefox up to 24.*
[dactyl.git] / common / modules / services.jsm
index 76c4b6bd7a69e9f1991da8c1700b5e21448f71bb..377cd13edec5345d5e8809a9b35474bcf89c7752 100644 (file)
@@ -1,16 +1,20 @@
-// Copyright (c) 2008-2011 by Kris Maglione <maglione.k at Gmail>
+// Copyright (c) 2008-2013 Kris Maglione <maglione.k at Gmail>
 //
 // This work is licensed for reuse under an MIT license. Details are
 // given in the LICENSE.txt file included with this file.
-/* use strict */
+"use strict";
 
 try {
 
 var global = this;
-Components.utils.import("resource://dactyl/bootstrap.jsm");
 defineModule("services", {
-    exports: ["services"]
-}, this);
+    exports: ["PrivateBrowsingUtils", "services"]
+});
+
+try {
+    var { PrivateBrowsingUtils } = Cu.import("resource://gre/modules/PrivateBrowsingUtils.jsm");
+}
+catch (e) {}
 
 /**
  * A lazily-instantiated XPCOM class and service cache.
@@ -27,7 +31,6 @@ var Services = Module("Services", {
         this.add("appShell",            "@mozilla.org/appshell/appShellService;1",          "nsIAppShellService");
         this.add("appStartup",          "@mozilla.org/toolkit/app-startup;1",               "nsIAppStartup");
         this.add("bookmarks",           "@mozilla.org/browser/nav-bookmarks-service;1",     "nsINavBookmarksService");
-        this.add("bootstrap",           "@dactyl.googlecode.com/base/bootstrap");
         this.add("browserSearch",       "@mozilla.org/browser/search-service;1",            "nsIBrowserSearchService");
         this.add("cache",               "@mozilla.org/network/cache-service;1",             "nsICacheService");
         this.add("charset",             "@mozilla.org/charset-converter-manager;1",         "nsICharsetConverterManager");
@@ -44,13 +47,13 @@ var Services = Module("Services", {
         this.add("downloadManager",     "@mozilla.org/download-manager;1",                  "nsIDownloadManager");
         this.add("environment",         "@mozilla.org/process/environment;1",               "nsIEnvironment");
         this.add("extensionManager",    "@mozilla.org/extensions/manager;1",                "nsIExtensionManager");
-        this.add("externalApp",         "@mozilla.org/uriloader/external-helper-app-service;1", "nsPIExternalAppLauncher")
+        this.add("externalApp",         "@mozilla.org/uriloader/external-helper-app-service;1", "nsPIExternalAppLauncher");
         this.add("externalProtocol",    "@mozilla.org/uriloader/external-protocol-service;1", "nsIExternalProtocolService");
         this.add("favicon",             "@mozilla.org/browser/favicon-service;1",           "nsIFaviconService");
         this.add("file:",               this.PROTOCOL + "file",                             "nsIFileProtocolHandler");
         this.add("focus",               "@mozilla.org/focus-manager;1",                     "nsIFocusManager");
-        this.add("history",             "@mozilla.org/browser/global-history;2",
-                 ["nsIBrowserHistory", "nsIGlobalHistory2", "nsINavHistoryService", "nsPIPlacesDatabase"]);
+        this.add("history",             "@mozilla.org/browser/nav-history-service;1",
+                 ["nsIBrowserHistory", "nsINavHistoryService", "nsPIPlacesDatabase"]);
         this.add("io",                  "@mozilla.org/network/io-service;1",                "nsIIOService");
         this.add("json",                "@mozilla.org/dom/json;1",                          "nsIJSON", "createInstance");
         this.add("listeners",           "@mozilla.org/eventlistenerservice;1",              "nsIEventListenerService");
@@ -96,7 +99,7 @@ var Services = Module("Services", {
         this.addClass("Persist",      "@mozilla.org/embedding/browser/nsWebBrowserPersist;1", "nsIWebBrowserPersist");
         this.addClass("Pipe",         "@mozilla.org/pipe;1",                       "nsIPipe", "init");
         this.addClass("Process",      "@mozilla.org/process/util;1",               "nsIProcess", "init");
-        this.addClass("Pump",         "@mozilla.org/network/input-stream-pump;1",  "nsIInputStreamPump", "init")
+        this.addClass("Pump",         "@mozilla.org/network/input-stream-pump;1",  "nsIInputStreamPump", "init");
         this.addClass("StreamChannel","@mozilla.org/network/input-stream-channel;1",
                       ["nsIInputStreamChannel", "nsIChannel"], "setURI");
         this.addClass("StreamCopier", "@mozilla.org/network/async-stream-copier;1","nsIAsyncStreamCopier", "init");
@@ -109,12 +112,13 @@ var Services = Module("Services", {
         this.addClass("Xmlhttp",      "@mozilla.org/xmlextras/xmlhttprequest;1",   [], "open");
         this.addClass("XPathEvaluator", "@mozilla.org/dom/xpath-evaluator;1",      "nsIDOMXPathEvaluator");
         this.addClass("XMLDocument",  "@mozilla.org/xml/xml-document;1",           ["nsIDOMXMLDocument", "nsIDOMNodeSelector"]);
+        this.addClass("XMLSerializer","@mozilla.org/xmlextras/xmlserializer;1",    ["nsIDOMSerializer"]);
         this.addClass("ZipReader",    "@mozilla.org/libjar/zip-reader;1",          "nsIZipReader", "open", false);
         this.addClass("ZipWriter",    "@mozilla.org/zipwriter;1",                  "nsIZipWriter", "open", false);
     },
     reinit: function () {},
 
-    _create: function (name, args) {
+    _create: function _create(name, args) {
         try {
             var service = this.services[name];
 
@@ -122,7 +126,7 @@ var Services = Module("Services", {
             if (!service.interfaces.length)
                 return res.wrappedJSObject || res;
 
-            service.interfaces.forEach(function (iface) res.QueryInterface(Ci[iface]));
+            service.interfaces.forEach(function (iface) res instanceof Ci[iface]);
             if (service.init && args.length) {
                 if (service.callable)
                     res[service.init].apply(res, args);
@@ -131,8 +135,11 @@ var Services = Module("Services", {
             }
             return res;
         }
-        catch (e if service.quiet !== false) {
-            if (typeof util !== "undefined")
+        catch (e) {
+            if (service.quiet === false)
+                throw e.stack ? e : Error(e);
+
+            if (typeof util !== "undefined" && util != null)
                 util.reportError(e);
             else
                 dump("dactyl: Service creation failed for '" + service.class + "': " + e + "\n" + (e.stack || Error(e).stack));
@@ -150,7 +157,7 @@ var Services = Module("Services", {
      * @param {string} meth The name of the function used to instantiate
      *     the service.
      */
-    add: function (name, class_, ifaces, meth) {
+    add: function add(name, class_, ifaces, meth) {
         const self = this;
         this.services[name] = { method: meth, class: class_, interfaces: Array.concat(ifaces || []) };
         if (name in this && ifaces && !this.__lookupGetter__(name) && !(this[name] instanceof Ci.nsISupports))
@@ -168,14 +175,13 @@ var Services = Module("Services", {
      * @param {string} init Name of a property or method used to initialize the
      *     class.
      */
-    addClass: function (name, class_, ifaces, init, quiet) {
-        const self = this;
+    addClass: function addClass(name, class_, ifaces, init, quiet) {
         this.services[name] = { class: class_, interfaces: Array.concat(ifaces || []), method: "createInstance", init: init, quiet: quiet };
         if (init)
             memoize(this.services[name], "callable",
                     function () callable(XPCOMShim(this.interfaces)[this.init]));
 
-        this[name] = function () self._create(name, arguments);
+        this[name] = (function Create() this._create(name, arguments)).bind(this);
         update.apply(null, [this[name]].concat([Ci[i] for each (i in Array.concat(ifaces))]));
         return this[name];
     },
@@ -199,7 +205,7 @@ var Services = Module("Services", {
      *
      * @param {string} name The service's cache key.
      */
-    has: function (name) Set.has(this.services, name) && this.services[name].class in Cc &&
+    has: function has(name) Set.has(this.services, name) && this.services[name].class in Cc &&
         this.services[name].interfaces.every(function (iface) iface in Ci)
 });
 
@@ -207,4 +213,4 @@ endModule();
 
 } catch(e){dump(e.fileName+":"+e.lineNumber+": "+e+"\n" + e.stack);}
 
-// vim: set fdm=marker sw=4 sts=4 et ft=javascript:
+// vim: set fdm=marker sw=4 sts=4 ts=8 et ft=javascript: