summaryrefslogtreecommitdiff
path: root/common/content/services.js
blob: fa0d8ff06e3e0d55c86433bb4f43635f1d278e55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/***** BEGIN LICENSE BLOCK ***** {{{
 Copyright ©2008-2009 by Kris Maglione <maglione.k at Gmail>

 Permission is hereby granted, free of charge, to any person obtaining a
 copy of this software and associated documentation files (the "Software"),
 to deal in the Software without restriction, including without limitation
 the rights to use, copy, modify, merge, publish, distribute, sublicense,
 and/or sell copies of the Software, and to permit persons to whom the 
 Software is furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in 
 all copies or substantial portions of the Software. 
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL 
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 DEALINGS IN THE SOFTWARE.
}}} ***** END LICENSE BLOCK *****/

/** @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)
    {
        ifaces = Array.concat(ifaces);
        try
        {
            let res = Cc[classes][meth || "getService"](ifaces.shift());
            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);
        }
    }

    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("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("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: