]> git.donarmstrong.com Git - dactyl.git/blobdiff - common/modules/services.jsm
Import 1.0rc1 supporting Firefox up to 11.*
[dactyl.git] / common / modules / services.jsm
index b3372ddf5377db31298b81679c7590c226fe8442..d47da247ba409eb1149614f9568096fd7830fddf 100644 (file)
@@ -2,61 +2,71 @@
 //
 // 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"],
-    use: ["util"]
+    exports: ["services"]
 }, this);
 
 /**
  * A lazily-instantiated XPCOM class and service cache.
  */
 var Services = Module("Services", {
+    ABOUT: "@mozilla.org/network/protocol/about;1?what=",
+    AUTOCOMPLETE: "@mozilla.org/autocomplete/search;1?name=",
+    PROTOCOL: "@mozilla.org/network/protocol;1?name=",
+
     init: function () {
         this.services = {};
 
         this.add("annotation",          "@mozilla.org/browser/annotation-service;1",        "nsIAnnotationService");
         this.add("appShell",            "@mozilla.org/appshell/appShellService;1",          "nsIAppShellService");
         this.add("appStartup",          "@mozilla.org/toolkit/app-startup;1",               "nsIAppStartup");
-        this.add("autoCompleteSearch",  "@mozilla.org/autocomplete/search;1?name=history",  "nsIAutoCompleteSearch");
         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");
         this.add("chromeRegistry",      "@mozilla.org/chrome/chrome-registry;1",            "nsIXULChromeRegistry");
+        this.add("clipboard",           "@mozilla.org/widget/clipboard;1",                  "nsIClipboard");
+        this.add("clipboardHelper",     "@mozilla.org/widget/clipboardhelper;1",            "nsIClipboardHelper");
         this.add("commandLineHandler",  "@mozilla.org/commandlinehandler/general-startup;1?type=dactyl");
         this.add("console",             "@mozilla.org/consoleservice;1",                    "nsIConsoleService");
-        this.add("dactyl:",             "@mozilla.org/network/protocol;1?name=dactyl");
+        this.add("contentPrefs",        "@mozilla.org/content-pref/service;1",              "nsIContentPrefService");
+        this.add("dactyl",              "@dactyl.googlecode.com/extra/utils",               "dactylIUtils");
+        this.add("dactyl:",             this.PROTOCOL + "dactyl");
         this.add("debugger",            "@mozilla.org/js/jsd/debugger-service;1",           "jsdIDebuggerService");
         this.add("directory",           "@mozilla.org/file/directory_service;1",            "nsIProperties");
         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("externalProtocol",    "@mozilla.org/uriloader/external-protocol-service;1", "nsIExternalProtocolService");
         this.add("favicon",             "@mozilla.org/browser/favicon-service;1",           "nsIFaviconService");
-        this.add("file:",               "@mozilla.org/network/protocol;1?name=file",        "nsIFileProtocolHandler");
+        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", "nsIGlobalHistory3", "nsINavHistoryService", "nsPIPlacesDatabase"]);
+                 ["nsIBrowserHistory", "nsIGlobalHistory2", "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");
         this.add("livemark",            "@mozilla.org/browser/livemark-service;2",          "nsILivemarkService");
+        this.add("messages",            "@mozilla.org/globalmessagemanager;1",              "nsIChromeFrameMessageManager");
         this.add("mime",                "@mozilla.org/mime;1",                              "nsIMIMEService");
         this.add("observer",            "@mozilla.org/observer-service;1",                  "nsIObserverService");
         this.add("pref",                "@mozilla.org/preferences-service;1",               ["nsIPrefBranch2", "nsIPrefService"]);
         this.add("privateBrowsing",     "@mozilla.org/privatebrowsing;1",                   "nsIPrivateBrowsingService");
         this.add("profile",             "@mozilla.org/toolkit/profile-service;1",           "nsIToolkitProfileService");
-        this.add("resource:",           "@mozilla.org/network/protocol;1?name=resource",    ["nsIProtocolHandler", "nsIResProtocolHandler"]);
+        this.add("resource:",           this.PROTOCOL + "resource",                         ["nsIProtocolHandler", "nsIResProtocolHandler"]);
         this.add("runtime",             "@mozilla.org/xre/runtime;1",                       ["nsIXULAppInfo", "nsIXULRuntime"]);
         this.add("rdf",                 "@mozilla.org/rdf/rdf-service;1",                   "nsIRDFService");
+        this.add("security",            "@mozilla.org/scriptsecuritymanager;1",             "nsIScriptSecurityManager");
         this.add("sessionStore",        "@mozilla.org/browser/sessionstore;1",              "nsISessionStore");
+        this.add("spell",               "@mozilla.org/spellchecker/engine;1",               "mozISpellCheckingEngine");
         this.add("stringBundle",        "@mozilla.org/intl/stringbundle;1",                 "nsIStringBundleService");
         this.add("stylesheet",          "@mozilla.org/content/style-sheet-service;1",       "nsIStyleSheetService");
         this.add("subscriptLoader",     "@mozilla.org/moz/jssubscript-loader;1",            "mozIJSSubScriptLoader");
@@ -70,53 +80,61 @@ var Services = Module("Services", {
         this.add("zipReader",           "@mozilla.org/libjar/zip-reader-cache;1",           "nsIZipReaderCache");
 
         this.addClass("CharsetConv",  "@mozilla.org/intl/scriptableunicodeconverter", "nsIScriptableUnicodeConverter", "charset");
+        this.addClass("CharsetStream","@mozilla.org/intl/converter-input-stream;1",   ["nsIConverterInputStream",
+                                                                                       "nsIUnicharLineInputStream"], "init");
+        this.addClass("ConvOutStream","@mozilla.org/intl/converter-output-stream;1", "nsIConverterOutputStream", "init", false);
         this.addClass("File",         "@mozilla.org/file/local;1",                 "nsILocalFile");
+        this.addClass("FileInStream", "@mozilla.org/network/file-input-stream;1",  "nsIFileInputStream", "init", false);
+        this.addClass("FileOutStream","@mozilla.org/network/file-output-stream;1", "nsIFileOutputStream", "init", false);
         this.addClass("Find",         "@mozilla.org/embedcomp/rangefind;1",        "nsIFind");
+        this.addClass("FormData",     "@mozilla.org/files/formdata;1",             "nsIDOMFormData");
         this.addClass("HtmlConverter","@mozilla.org/widget/htmlformatconverter;1", "nsIFormatConverter");
         this.addClass("HtmlEncoder",  "@mozilla.org/layout/htmlCopyEncoder;1",     "nsIDocumentEncoder");
+        this.addClass("InterfacePointer", "@mozilla.org/supports-interface-pointer;1", "nsISupportsInterfacePointer", "data");
         this.addClass("InputStream",  "@mozilla.org/scriptableinputstream;1",      "nsIScriptableInputStream", "init");
         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("StreamChannel","@mozilla.org/network/input-stream-channel;1",
-                      ["nsIChannel", "nsIInputStreamChannel", "nsIRequest"], "setURI");
+                      ["nsIInputStreamChannel", "nsIChannel"], "setURI");
         this.addClass("StreamCopier", "@mozilla.org/network/async-stream-copier;1","nsIAsyncStreamCopier", "init");
         this.addClass("String",       "@mozilla.org/supports-string;1",            "nsISupportsString", "data");
         this.addClass("StringStream", "@mozilla.org/io/string-input-stream;1",     "nsIStringInputStream", "data");
         this.addClass("Transfer",     "@mozilla.org/transfer;1",                   "nsITransfer", "init");
+        this.addClass("Transferable", "@mozilla.org/widget/transferable;1",        "nsITransferable");
         this.addClass("Timer",        "@mozilla.org/timer;1",                      "nsITimer", "initWithCallback");
-        this.addClass("Xmlhttp",      "@mozilla.org/xmlextras/xmlhttprequest;1",   "nsIXMLHttpRequest");
+        this.addClass("URL",          "@mozilla.org/network/standard-url;1",       ["nsIStandardURL", "nsIURL"], "init");
+        this.addClass("Xmlhttp",      "@mozilla.org/xmlextras/xmlhttprequest;1",   "nsIXMLHttpRequest", "open");
         this.addClass("XPathEvaluator", "@mozilla.org/dom/xpath-evaluator;1",      "nsIDOMXPathEvaluator");
         this.addClass("XMLDocument",  "@mozilla.org/xml/xml-document;1",           ["nsIDOMXMLDocument", "nsIDOMNodeSelector"]);
-        this.addClass("ZipReader",    "@mozilla.org/libjar/zip-reader;1",          "nsIZipReader", "open");
-        this.addClass("ZipWriter",    "@mozilla.org/zipwriter;1",                  "nsIZipWriter");
+        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 (classes, ifaces, meth, init, args) {
+    _create: function (name, args) {
         try {
-            let res = Cc[classes][meth || "getService"]();
-            if (!ifaces)
-                return res["wrapped" + "JSObject"]; // Kill stupid validator warning
-            Array.concat(ifaces).forEach(function (iface) res.QueryInterface(Ci[iface]));
-            if (init && args.length) {
-                try {
-                    var isCallable = callable(res[init]);
-                }
-                catch (e) {} // Ugh.
-
-                if (isCallable)
-                    res[init].apply(res, args);
+            var service = this.services[name];
+
+            let res = Cc[service.class][service.method || "getService"]();
+            if (!service.interfaces.length)
+                return res.wrappedJSObject || res;
+
+            service.interfaces.forEach(function (iface) res.QueryInterface(Ci[iface]));
+            if (service.init && args.length) {
+                if (service.callable)
+                    res[service.init].apply(res, args);
                 else
-                    res[init] = args[0];
+                    res[service.init] = args[0];
             }
             return res;
         }
-        catch (e) {
+        catch (e if service.quiet !== false) {
             if (typeof util !== "undefined")
                 util.reportError(e);
             else
-                dump("dactyl: Service creation failed for '" + classes + "': " + e + "\n" + (e.stack || Error(e).stack));
+                dump("dactyl: Service creation failed for '" + service.class + "': " + e + "\n" + (e.stack || Error(e).stack));
             return null;
         }
     },
@@ -133,10 +151,10 @@ var Services = Module("Services", {
      */
     add: function (name, class_, ifaces, meth) {
         const self = this;
-        this.services[name] = { class: class_, interfaces: Array.concat(ifaces || []) };
+        this.services[name] = { method: meth, class: class_, interfaces: Array.concat(ifaces || []) };
         if (name in this && ifaces && !this.__lookupGetter__(name) && !(this[name] instanceof Ci.nsISupports))
             throw TypeError();
-        memoize(this, name, function () self._create(class_, ifaces, meth));
+        memoize(this, name, function () self._create(name));
     },
 
     /**
@@ -144,14 +162,19 @@ var Services = Module("Services", {
      *
      * @param {string} name The class's cache key.
      * @param {string} class_ The class's contract ID.
-     * @param {nsISupports|[nsISupports]} ifaces The interface or array of
+     * @param {string|[string]} ifaces The interface or array of
      *     interfaces implemented by this class.
-     * @param {string} init Name of a property or method used to initialise the
-     *     class. See {@link #_create}.
+     * @param {string} init Name of a property or method used to initialize the
+     *     class.
      */
-    addClass: function (name, class_, ifaces, init) {
+    addClass: function (name, class_, ifaces, init, quiet) {
         const self = this;
-        this[name] = function () self._create(class_, ifaces, "createInstance", init, arguments);
+        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);
         update.apply(null, [this[name]].concat([Ci[i] for each (i in Array.concat(ifaces))]));
         return this[name];
     },
@@ -161,14 +184,14 @@ var Services = Module("Services", {
      *
      * @param {string} name The class's cache key.
      */
-    create: function (name) this[name[0].toUpperCase() + name.substr(1)],
+    create: deprecated("services.*name*()", function create(name) this[util.capitalize(name)]()),
 
     /**
      * Returns the cached service with the specified name.
      *
      * @param {string} name The service's cache key.
      */
-    get: function (name) this[name],
+    get: deprecated("services.*name*", function get(name) this[name]),
 
     /**
      * Returns true if the given service is available.
@@ -177,11 +200,6 @@ var Services = Module("Services", {
      */
     has: function (name) Set.has(this.services, name) && this.services[name].class in Cc &&
         this.services[name].interfaces.every(function (iface) iface in Ci)
-}, {
-}, {
-    javascript: function (dactyl, modules) {
-        modules.JavaScript.setCompleter(this.get, [function () [[k, v] for ([k, v] in Iterator(services)) if (v instanceof Ci.nsISupports)]]);
-    }
 });
 
 endModule();