1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-21 03:47:58 +01:00
Files
pentadactyl-pm/common/content/services.js
Kris Maglione 7a9889b18c Add IO#File.
New review:
owner: dougkearns
I like this for the most part, except that it has to go to lengths
to wrap the original nsIFile correctly, an that it can't be passed
directly to other XPCOM components. It makes most operations on
files a lot cleaner, though.
2009-10-31 17:40:16 -04:00

127 lines
5.7 KiB
JavaScript

// Copyright (c) 2008-2009 by 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.
/** @scope modules */
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
const Cu = Components.utils;
/**
* Cached XPCOM services and classes.
*
* @constructor
*/
function Services()
{
const classes = {};
const services = {};
function create(classes, ifaces, meth)
{
try
{
let res = Cc[classes][meth || "getService"]();
if (!ifaces)
return res.wrappedJSObject;
ifaces = Array.concat(ifaces);
ifaces.forEach(function (iface) res.QueryInterface(iface));
return res;
}
catch (e)
{
// liberator.log() is not defined at this time, so just dump any error
dump("Service creation failed for '" + classes + "': " + e + "\n");
}
}
const self = {
/* @property {Object} A map of all cached services. */
get services() services,
/* @property {Object} A map of all cached classes. */
get classes() classes,
/**
* Adds a new XPCOM service to the cache.
*
* @param {string} name The service's cache key.
* @param {string} class The class's contract ID.
* @param {nsISupports|nsISupports[]} ifaces The interface or array of
* interfaces implemented by this service.
* @param {string} meth The name of the function used to instanciate
* the service.
*/
add: function (name, class, ifaces, meth)
{
return services[name] = create(class, ifaces, meth);
},
/**
* Adds a new XPCOM class to the cache.
*
* @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
* interfaces implemented by this class.
*/
addClass: function (name, class, ifaces)
{
return classes[name] = function () create(class, ifaces, "createInstance");
},
/**
* Returns the cached service with the specified name.
*
* @param {string} name The service's cache key.
*/
get: function (name) services[name],
/**
* Returns a new instance of the cached class with the specified name.
*
* @param {string} name The class's cache key.
*/
create: function (name) classes[name]()
};
self.add("appStartup", "@mozilla.org/toolkit/app-startup;1", Ci.nsIAppStartup);
self.add("autoCompleteSearch", "@mozilla.org/autocomplete/search;1?name=history", Ci.nsIAutoCompleteSearch);
self.add("bookmarks", "@mozilla.org/browser/nav-bookmarks-service;1", Ci.nsINavBookmarksService);
self.add("browserSearch", "@mozilla.org/browser/search-service;1", Ci.nsIBrowserSearchService);
self.add("cache", "@mozilla.org/network/cache-service;1", Ci.nsICacheService);
self.add("console", "@mozilla.org/consoleservice;1", Ci.nsIConsoleService);
self.add("directory", "@mozilla.org/file/directory_service;1", Ci.nsIProperties);
self.add("environment", "@mozilla.org/process/environment;1", Ci.nsIEnvironment);
self.add("extensionManager", "@mozilla.org/extensions/manager;1", Ci.nsIExtensionManager);
self.add("favicon", "@mozilla.org/browser/favicon-service;1", Ci.nsIFaviconService);
self.add("json", "@mozilla.org/dom/json;1", Ci.nsIJSON, "createInstance");
self.add("liberator:", "@mozilla.org/network/protocol;1?name=liberator");
self.add("livemark", "@mozilla.org/browser/livemark-service;2", Ci.nsILivemarkService);
self.add("observer", "@mozilla.org/observer-service;1", Ci.nsIObserverService);
self.add("io", "@mozilla.org/network/io-service;1", Ci.nsIIOService);
self.add("pref", "@mozilla.org/preferences-service;1", [Ci.nsIPrefService, Ci.nsIPrefBranch, Ci.nsIPrefBranch2]);
self.add("profile", "@mozilla.org/toolkit/profile-service;1", Ci.nsIToolkitProfileService);
self.add("rdf", "@mozilla.org/rdf/rdf-service;1", Ci.nsIRDFService);
self.add("sessionStore", "@mozilla.org/browser/sessionstore;1", Ci.nsISessionStore);
self.add("subscriptLoader", "@mozilla.org/moz/jssubscript-loader;1", Ci.mozIJSSubScriptLoader);
self.add("threadManager", "@mozilla.org/thread-manager;1", Ci.nsIThreadManager);
self.add("windowMediator", "@mozilla.org/appshell/window-mediator;1", Ci.nsIWindowMediator);
self.add("windowWatcher", "@mozilla.org/embedcomp/window-watcher;1", Ci.nsIWindowWatcher);
self.add("xulAppInfo", "@mozilla.org/xre/app-info;1", Ci.nsIXULAppInfo);
self.addClass("file", "@mozilla.org/file/local;1", Ci.nsILocalFile);
self.addClass("file:", "@mozilla.org/network/protocol;1?name=file", Ci.nsIFileProtocolHandler);
self.addClass("find", "@mozilla.org/embedcomp/rangefind;1", Ci.nsIFind);
self.addClass("process", "@mozilla.org/process/util;1", Ci.nsIProcess);
return self;
}
var services = Services();
// vim: set fdm=marker sw=4 ts=4 et: