1 // Copyright (c) 2008-2011 by 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.
10 Components.utils.import("resource://dactyl/bootstrap.jsm");
11 defineModule("services", {
12 exports: ["services"],
17 * A lazily-instantiated XPCOM class and service cache.
19 var Services = Module("Services", {
23 this.add("annotation", "@mozilla.org/browser/annotation-service;1", "nsIAnnotationService");
24 this.add("appShell", "@mozilla.org/appshell/appShellService;1", "nsIAppShellService");
25 this.add("appStartup", "@mozilla.org/toolkit/app-startup;1", "nsIAppStartup");
26 this.add("autoCompleteSearch", "@mozilla.org/autocomplete/search;1?name=history", "nsIAutoCompleteSearch");
27 this.add("bookmarks", "@mozilla.org/browser/nav-bookmarks-service;1", "nsINavBookmarksService");
28 this.add("bootstrap", "@dactyl.googlecode.com/base/bootstrap");
29 this.add("browserSearch", "@mozilla.org/browser/search-service;1", "nsIBrowserSearchService");
30 this.add("cache", "@mozilla.org/network/cache-service;1", "nsICacheService");
31 this.add("charset", "@mozilla.org/charset-converter-manager;1", "nsICharsetConverterManager");
32 this.add("chromeRegistry", "@mozilla.org/chrome/chrome-registry;1", "nsIXULChromeRegistry");
33 this.add("commandLineHandler", "@mozilla.org/commandlinehandler/general-startup;1?type=dactyl");
34 this.add("console", "@mozilla.org/consoleservice;1", "nsIConsoleService");
35 this.add("dactyl:", "@mozilla.org/network/protocol;1?name=dactyl");
36 this.add("debugger", "@mozilla.org/js/jsd/debugger-service;1", "jsdIDebuggerService");
37 this.add("directory", "@mozilla.org/file/directory_service;1", "nsIProperties");
38 this.add("downloadManager", "@mozilla.org/download-manager;1", "nsIDownloadManager");
39 this.add("environment", "@mozilla.org/process/environment;1", "nsIEnvironment");
40 this.add("extensionManager", "@mozilla.org/extensions/manager;1", "nsIExtensionManager");
41 this.add("externalProtocol", "@mozilla.org/uriloader/external-protocol-service;1", "nsIExternalProtocolService");
42 this.add("favicon", "@mozilla.org/browser/favicon-service;1", "nsIFaviconService");
43 this.add("file:", "@mozilla.org/network/protocol;1?name=file", "nsIFileProtocolHandler");
44 this.add("focus", "@mozilla.org/focus-manager;1", "nsIFocusManager");
45 this.add("history", "@mozilla.org/browser/global-history;2",
46 ["nsIBrowserHistory", "nsIGlobalHistory3", "nsINavHistoryService", "nsPIPlacesDatabase"]);
47 this.add("io", "@mozilla.org/network/io-service;1", "nsIIOService");
48 this.add("json", "@mozilla.org/dom/json;1", "nsIJSON", "createInstance");
49 this.add("listeners", "@mozilla.org/eventlistenerservice;1", "nsIEventListenerService");
50 this.add("livemark", "@mozilla.org/browser/livemark-service;2", "nsILivemarkService");
51 this.add("mime", "@mozilla.org/mime;1", "nsIMIMEService");
52 this.add("observer", "@mozilla.org/observer-service;1", "nsIObserverService");
53 this.add("pref", "@mozilla.org/preferences-service;1", ["nsIPrefBranch2", "nsIPrefService"]);
54 this.add("privateBrowsing", "@mozilla.org/privatebrowsing;1", "nsIPrivateBrowsingService");
55 this.add("profile", "@mozilla.org/toolkit/profile-service;1", "nsIToolkitProfileService");
56 this.add("resource:", "@mozilla.org/network/protocol;1?name=resource", ["nsIProtocolHandler", "nsIResProtocolHandler"]);
57 this.add("runtime", "@mozilla.org/xre/runtime;1", ["nsIXULAppInfo", "nsIXULRuntime"]);
58 this.add("rdf", "@mozilla.org/rdf/rdf-service;1", "nsIRDFService");
59 this.add("sessionStore", "@mozilla.org/browser/sessionstore;1", "nsISessionStore");
60 this.add("stringBundle", "@mozilla.org/intl/stringbundle;1", "nsIStringBundleService");
61 this.add("stylesheet", "@mozilla.org/content/style-sheet-service;1", "nsIStyleSheetService");
62 this.add("subscriptLoader", "@mozilla.org/moz/jssubscript-loader;1", "mozIJSSubScriptLoader");
63 this.add("tagging", "@mozilla.org/browser/tagging-service;1", "nsITaggingService");
64 this.add("tld", "@mozilla.org/network/effective-tld-service;1", "nsIEffectiveTLDService");
65 this.add("threading", "@mozilla.org/thread-manager;1", "nsIThreadManager");
66 this.add("urifixup", "@mozilla.org/docshell/urifixup;1", "nsIURIFixup");
67 this.add("versionCompare", "@mozilla.org/xpcom/version-comparator;1", "nsIVersionComparator");
68 this.add("windowMediator", "@mozilla.org/appshell/window-mediator;1", "nsIWindowMediator");
69 this.add("windowWatcher", "@mozilla.org/embedcomp/window-watcher;1", "nsIWindowWatcher");
70 this.add("zipReader", "@mozilla.org/libjar/zip-reader-cache;1", "nsIZipReaderCache");
72 this.addClass("CharsetConv", "@mozilla.org/intl/scriptableunicodeconverter", "nsIScriptableUnicodeConverter", "charset");
73 this.addClass("File", "@mozilla.org/file/local;1", "nsILocalFile");
74 this.addClass("Find", "@mozilla.org/embedcomp/rangefind;1", "nsIFind");
75 this.addClass("HtmlConverter","@mozilla.org/widget/htmlformatconverter;1", "nsIFormatConverter");
76 this.addClass("HtmlEncoder", "@mozilla.org/layout/htmlCopyEncoder;1", "nsIDocumentEncoder");
77 this.addClass("InputStream", "@mozilla.org/scriptableinputstream;1", "nsIScriptableInputStream", "init");
78 this.addClass("Persist", "@mozilla.org/embedding/browser/nsWebBrowserPersist;1", "nsIWebBrowserPersist");
79 this.addClass("Pipe", "@mozilla.org/pipe;1", "nsIPipe", "init");
80 this.addClass("Process", "@mozilla.org/process/util;1", "nsIProcess", "init");
81 this.addClass("StreamChannel","@mozilla.org/network/input-stream-channel;1",
82 ["nsIChannel", "nsIInputStreamChannel", "nsIRequest"], "setURI");
83 this.addClass("StreamCopier", "@mozilla.org/network/async-stream-copier;1","nsIAsyncStreamCopier", "init");
84 this.addClass("String", "@mozilla.org/supports-string;1", "nsISupportsString", "data");
85 this.addClass("StringStream", "@mozilla.org/io/string-input-stream;1", "nsIStringInputStream", "data");
86 this.addClass("Transfer", "@mozilla.org/transfer;1", "nsITransfer", "init");
87 this.addClass("Timer", "@mozilla.org/timer;1", "nsITimer", "initWithCallback");
88 this.addClass("Xmlhttp", "@mozilla.org/xmlextras/xmlhttprequest;1", "nsIXMLHttpRequest");
89 this.addClass("XPathEvaluator", "@mozilla.org/dom/xpath-evaluator;1", "nsIDOMXPathEvaluator");
90 this.addClass("XMLDocument", "@mozilla.org/xml/xml-document;1", ["nsIDOMXMLDocument", "nsIDOMNodeSelector"]);
91 this.addClass("ZipReader", "@mozilla.org/libjar/zip-reader;1", "nsIZipReader", "open");
92 this.addClass("ZipWriter", "@mozilla.org/zipwriter;1", "nsIZipWriter");
94 reinit: function () {},
96 _create: function (classes, ifaces, meth, init, args) {
98 let res = Cc[classes][meth || "getService"]();
100 return res["wrapped" + "JSObject"]; // Kill stupid validator warning
101 Array.concat(ifaces).forEach(function (iface) res.QueryInterface(Ci[iface]));
102 if (init && args.length) {
104 var isCallable = callable(res[init]);
109 res[init].apply(res, args);
116 if (typeof util !== "undefined")
119 dump("dactyl: Service creation failed for '" + classes + "': " + e + "\n" + (e.stack || Error(e).stack));
125 * Adds a new XPCOM service to the cache.
127 * @param {string} name The service's cache key.
128 * @param {string} class The class's contract ID.
129 * @param {string|[string]} ifaces The interface or array of
130 * interfaces implemented by this service.
131 * @param {string} meth The name of the function used to instantiate
134 add: function (name, class_, ifaces, meth) {
136 this.services[name] = { class: class_, interfaces: Array.concat(ifaces || []) };
137 if (name in this && ifaces && !this.__lookupGetter__(name) && !(this[name] instanceof Ci.nsISupports))
139 memoize(this, name, function () self._create(class_, ifaces, meth));
143 * Adds a new XPCOM class to the cache.
145 * @param {string} name The class's cache key.
146 * @param {string} class_ The class's contract ID.
147 * @param {nsISupports|[nsISupports]} ifaces The interface or array of
148 * interfaces implemented by this class.
149 * @param {string} init Name of a property or method used to initialise the
150 * class. See {@link #_create}.
152 addClass: function (name, class_, ifaces, init) {
154 this[name] = function () self._create(class_, ifaces, "createInstance", init, arguments);
155 update.apply(null, [this[name]].concat([Ci[i] for each (i in Array.concat(ifaces))]));
160 * Returns a new instance of the cached class with the specified name.
162 * @param {string} name The class's cache key.
164 create: function (name) this[name[0].toUpperCase() + name.substr(1)],
167 * Returns the cached service with the specified name.
169 * @param {string} name The service's cache key.
171 get: function (name) this[name],
174 * Returns true if the given service is available.
176 * @param {string} name The service's cache key.
178 has: function (name) Set.has(this.services, name) && this.services[name].class in Cc &&
179 this.services[name].interfaces.every(function (iface) iface in Ci)
182 javascript: function (dactyl, modules) {
183 modules.JavaScript.setCompleter(this.get, [function () [[k, v] for ([k, v] in Iterator(services)) if (v instanceof Ci.nsISupports)]]);
189 } catch(e){dump(e.fileName+":"+e.lineNumber+": "+e+"\n" + e.stack);}
191 // vim: set fdm=marker sw=4 sts=4 et ft=javascript: