mozilla

Revision 236773 of Services.jsm

  • Revision slug: JavaScript_code_modules/Services.jsm
  • Revision title: Services.jsm
  • Revision id: 236773
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment one or more formatting changes

Revision Content

{{ gecko_minversion_header("2") }}

The Services.jsm JavaScript code module offers a wide assortment of lazy getters that simplify the process of obtaining references to commonly used services.

To use it, you first need to import the code module into your JavaScript scope:

Components.utils.import("resource://gre/modules/Services.jsm");

Then you can obtain references to services by simply accessing them from the Services object exported by the code module. For example, to obtain a reference to the preferences service:

var prefsService = Services.prefs;

Provided service getters

Service accessor Service interface Service name
appinfo {{ interface("nsIXULAppInfo") }}
{{ interface("nsIXULRuntime") }}
Application information service
console {{ interface("nsIConsoleService") }} Error console service
contentPrefs {{ interface("nsIContentPrefService") }}

Content Preferences service

cookies {{ interface("nsICookieManager2") }}

Cookie Manager 2 service

dirsvc {{ interface("nsIDirectoryService") }}
{{ interface("nsIProperties") }}
Directory service
droppedLinkHandler {{ interface("nsIDroppedLinkHandler") }} Dropped link handler service
eTLD {{ interface("nsIEffectiveTLDService") }}

EffectiveTLD service

io {{ interface("nsIIOService") }}
{{ interface("nsIIOService2") }}
I/O Service
locale {{ interface("nsILocaleService") }} Locale service
logins {{ interface("nsILoginManager") }}

Password Manager service

obs {{ interface("nsIObserverService") }} Observer service
perms {{ interface("nsIPermissionManager") }} Permission manager service
prefs {{ interface("nsIPrefBranch") }}
{{ interface("nsIPrefBranch2") }}
{{ interface("nsIPrefService") }}
Preferences service
prompt {{ interface("nsIPromptService") }} Prompt service
scriptloader {{ interface("mozIJSSubScriptLoader") }} JavaScript subscript loader service
search {{ interface("nsIBrowserSearchService") }} Browser search service¹
startup {{ interface("nsIAppStartup") }} Application startup service
storage {{ interface("mozIStorageService") }} Storage API service
strings {{ interface("nsIStringBundleService") }} String bundle service
sysinfo {{ interface("nsIPropertyBag2") }} System info service
telemetry {{ interface("nsITelemetry") }} Telemetry service
tm {{ interface("nsIThreadManager") }} Thread Manager service
urlFormatter {{ interface("nsIURLFormatter") }}

URL Formatter service

vc {{ interface("nsIVersionComparator") }} Version comparator service
wm {{ interface("nsIWindowMediator") }} Window mediator service
ww {{ interface("nsIWindowWatcher") }} Window watcher service
DOMRequest {{ interface("nsIDOMRequestService") }} DOMRequest service
cpmm {{ interface("nsIFrameMessageManager") }} Child Process Message Manager
ppmm {{ interface("nsIFrameMessageManager") }} Parent Process Message Manager

¹Not present in Thunderbird or SeaMonkey.

See also

{{ languages( { "zh-cn": "zh-cn/JavaScript_code_modules/Services.jsm" } ) }}

Revision Source

<p>{{ gecko_minversion_header("2") }}</p>
<p>The <code>Services.jsm</code> JavaScript code module offers a wide assortment of lazy getters that simplify the process of obtaining references to commonly used services.</p>
<p>To use it, you first need to import the code module into your JavaScript scope:</p>
<pre class="eval"><span class="nowiki">Components.utils.import("resource://gre/modules/Services.jsm");</span>
</pre>
<p>Then you can obtain references to services by simply accessing them from the <code>Services</code> object exported by the code module. For example, to obtain a reference to the preferences service:</p>
<pre class="brush: js">var prefsService = Services.prefs;
</pre>
<h2>Provided service getters</h2>
<table class="standard-table" style="width: auto;"> <tbody> <tr> <td class="header">Service accessor</td> <td class="header">Service interface</td> <td class="header">Service name</td> </tr> <tr> <td><code>appinfo</code></td> <td>{{ interface("nsIXULAppInfo") }}<br> {{ interface("nsIXULRuntime") }}</td> <td>Application information service</td> </tr>
<!--		<tr>
			<td><code>bookmarks</code></td>
			<td>{{interface("nsINavBookmarksService")}}</td>
			<td>Bookmarks service</td>
		</tr> --> <tr> <td><code>console</code></td> <td>{{ interface("nsIConsoleService") }}</td> <td>Error console service</td> </tr> <tr> <td><code>contentPrefs</code></td> <td>{{ interface("nsIContentPrefService") }}</td> <td> <p>Content Preferences service</p> </td> </tr> <tr> <td><code>cookies</code></td> <td>{{ interface("nsICookieManager2") }}</td> <td> <p>Cookie Manager 2 service</p> </td> </tr> <tr> <td><code>dirsvc</code></td> <td>{{ interface("nsIDirectoryService") }}<br> {{ interface("nsIProperties") }}</td> <td>Directory service</td> </tr> <tr> <td><code>droppedLinkHandler</code></td> <td>{{ interface("nsIDroppedLinkHandler") }}</td> <td>Dropped link handler service</td> </tr> <tr> <td><code>eTLD</code></td> <td>{{ interface("nsIEffectiveTLDService") }}</td> <td> <p>EffectiveTLD service</p> </td> </tr> <tr> <td><code>io</code></td> <td>{{ interface("nsIIOService") }}<br> {{ interface("nsIIOService2") }}</td> <td>I/O Service</td> </tr> <tr> <td><code>locale</code></td> <td>{{ interface("nsILocaleService") }}</td> <td>Locale service</td> </tr> <tr> <td><code>logins</code></td> <td>{{ interface("nsILoginManager") }}</td> <td> <p>Password Manager service</p> </td> </tr> <tr> <td><code>obs</code></td> <td>{{ interface("nsIObserverService") }}</td> <td>Observer service</td> </tr> <tr> <td><code>perms</code></td> <td>{{ interface("nsIPermissionManager") }}</td> <td>Permission manager service</td> </tr> <tr> <td><code>prefs</code></td> <td>{{ interface("nsIPrefBranch") }}<br> {{ interface("nsIPrefBranch2") }}<br> {{ interface("nsIPrefService") }}</td> <td>Preferences service</td> </tr> <tr> <td><code>prompt</code></td> <td>{{ interface("nsIPromptService") }}</td> <td>Prompt service</td> </tr> <tr> <td><code>scriptloader</code></td> <td>{{ interface("mozIJSSubScriptLoader") }}</td> <td>JavaScript subscript loader service</td> </tr> <tr> <td><code>search</code></td> <td>{{ interface("nsIBrowserSearchService") }}</td> <td>Browser search service¹</td> </tr> <tr> <td><code>startup</code></td> <td>{{ interface("nsIAppStartup") }}</td> <td>Application startup service</td> </tr> <tr> <td><code>storage</code></td> <td>{{ interface("mozIStorageService") }}</td> <td><a href="/en/Storage" title="en/Storage">Storage API</a> service</td> </tr> <tr> <td><code>strings</code></td> <td>{{ interface("nsIStringBundleService") }}</td> <td>String bundle service</td> </tr> <tr> <td><code>sysinfo</code></td> <td>{{ interface("nsIPropertyBag2") }}</td> <td>System info service</td> </tr> <tr> <td><code>telemetry</code></td> <td>{{ interface("nsITelemetry") }}</td> <td>Telemetry service</td> </tr> <tr> <td><code>tm</code></td> <td>{{ interface("nsIThreadManager") }}</td> <td><a href="/en/The_Thread_Manager" title="en/The Thread Manager">Thread Manager</a> service</td> </tr> <tr> <td><code>urlFormatter</code></td> <td>{{ interface("nsIURLFormatter") }}</td> <td> <p>URL Formatter service</p> </td> </tr> <tr> <td><code>vc</code></td> <td>{{ interface("nsIVersionComparator") }}</td> <td>Version comparator service</td> </tr> <tr> <td><code>wm</code></td> <td>{{ interface("nsIWindowMediator") }}</td> <td>Window mediator service</td> </tr> <tr> <td><code>ww</code></td> <td>{{ interface("nsIWindowWatcher") }}</td> <td>Window watcher service</td> </tr> <tr> <td><code>DOMRequest</code></td> <td>{{ interface("nsIDOMRequestService") }}</td> <td>DOMRequest service</td> </tr> <tr> <td><code>cpmm</code></td> <td>{{ interface("nsIFrameMessageManager") }}</td> <td>Child Process Message Manager</td> </tr> <tr> <td><code>ppmm</code></td> <td>{{ interface("nsIFrameMessageManager") }}</td> <td>Parent Process Message Manager</td> </tr> </tbody>
</table>
<p>¹Not present in Thunderbird or SeaMonkey.</p>
<h2>See also</h2>
<ul> <li><a class="internal" href="/en/JavaScript_code_modules/Using" title="en/JavaScript code modules/Using
    JavaScript code
    modules">Using JavaScript code modules</a></li> <li><a class="internal" href="/en/JavaScript_code_modules" title="en/JavaScript code
    modules">JavaScript code modules</a></li> <li><a class="internal" href="/en/XPCOM/mozilla::services_namespace" title="mozilla::services C++ namespace">mozilla::services C++ namespace</a></li>
</ul>
<p>{{ languages( { "zh-cn": "zh-cn/JavaScript_code_modules/Services.jsm" } ) }}</p>
Revert to this revision