Bug 512784: add Services.jsm module to allow easy global access to common services, r=dao, r=Mossop

--HG--
extra : rebase_source : 1827809b6e8ce51e4fb238e962f36aaf30b6ad15
This commit is contained in:
Gavin Sharp 2010-01-28 13:31:45 -05:00
parent 1042977486
commit eaf57306e9
7 changed files with 288 additions and 233 deletions

View File

@ -49,6 +49,7 @@
# Nils Maier <maierman@web.de>
# Rob Arnold <robarnold@cmu.edu>
# Dietrich Ayala <dietrich@mozilla.com>
# Gavin Sharp <gavin@gavinsharp.com>
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
@ -66,6 +67,7 @@
let Ci = Components.interfaces;
let Cu = Components.utils;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
const nsIWebNavigation = Ci.nsIWebNavigation;
@ -106,9 +108,7 @@ var gEditUIVisible = true;
__defineGetter__("gPrefService", function() {
delete this.gPrefService;
return this.gPrefService = Cc["@mozilla.org/preferences-service;1"]
.getService(Ci.nsIPrefBranch2)
.QueryInterface(Ci.nsIPrefService);
return this.gPrefService = Services.prefs;
});
__defineGetter__("PluralForm", function() {
@ -326,9 +326,6 @@ function findChildShell(aDocument, aDocShell, aSoughtURI) {
const gPopupBlockerObserver = {
_reportButton: null,
get _pm ()
Cc["@mozilla.org/permissionmanager;1"].getService(Ci.nsIPermissionManager),
onUpdatePageReport: function (aEvent)
{
if (aEvent.originalTarget != gBrowser.selectedBrowser)
@ -395,7 +392,7 @@ const gPopupBlockerObserver = {
toggleAllowPopupsForSite: function (aEvent)
{
var pm = this._pm;
var pm = Services.pm;
var shouldBlock = aEvent.target.getAttribute("block") == "true";
var perm = shouldBlock ? pm.DENY_ACTION : pm.ALLOW_ACTION;
pm.add(gBrowser.currentURI, "popup", perm);
@ -419,7 +416,7 @@ const gPopupBlockerObserver = {
try {
blockedPopupAllowSite.removeAttribute("hidden");
var pm = this._pm;
var pm = Services.pm;
if (pm.testPermission(uri, "popup") == pm.ALLOW_ACTION) {
// Offer an item to block popups for this site, if a whitelist entry exists
// already for it.
@ -536,9 +533,7 @@ const gPopupBlockerObserver = {
permissionType : "popup",
windowTitle : bundlePreferences.getString("popuppermissionstitle"),
introText : bundlePreferences.getString("popuppermissionstext") };
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator);
var existingWindow = wm.getMostRecentWindow("Browser:Permissions");
var existingWindow = Services.wm.getMostRecentWindow("Browser:Permissions");
if (existingWindow) {
existingWindow.initWithParams(params);
existingWindow.focus();
@ -1029,9 +1024,7 @@ function BrowserStartup() {
// Certain kinds of automigration rely on this notification to complete their
// tasks BEFORE the browser window is shown.
Cc["@mozilla.org/observer-service;1"]
.getService(Ci.nsIObserverService)
.notifyObservers(null, "browser-window-before-show", "");
Services.obs.notifyObservers(null, "browser-window-before-show", "");
// Set a sane starting width/height for all resolutions on new profiles.
if (!document.documentElement.hasAttribute("width")) {
@ -1101,9 +1094,8 @@ function HandleAppCommandEvent(evt) {
}
function prepareForStartup() {
var os = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
gBrowser.addEventListener("DOMUpdatePageReport", gPopupBlockerObserver.onUpdatePageReport, false);
// Note: we need to listen to untrusted events, because the pluginfinder XBL
// binding can't fire trusted ones (runs with page privileges).
gBrowser.addEventListener("PluginNotFound", gMissingPluginInstaller.newMissingPlugin, true, true);
@ -1112,7 +1104,9 @@ function prepareForStartup() {
gBrowser.addEventListener("PluginOutdated", gMissingPluginInstaller.newMissingPlugin, true, true);
gBrowser.addEventListener("PluginDisabled", gMissingPluginInstaller.newDisabledPlugin, true, true);
gBrowser.addEventListener("NewPluginInstalled", gMissingPluginInstaller.refreshBrowser, false);
os.addObserver(gMissingPluginInstaller.pluginCrashed, "plugin-crashed", false);
Services.obs.addObserver(gMissingPluginInstaller.pluginCrashed, "plugin-crashed", false);
window.addEventListener("AppCommand", HandleAppCommandEvent, true);
var webNavigation;
@ -1156,7 +1150,7 @@ function prepareForStartup() {
// progress notifications for back/forward button updating
webNavigation.sessionHistory = Components.classes["@mozilla.org/browser/shistory;1"]
.createInstance(Components.interfaces.nsISHistory);
os.addObserver(gBrowser.browsers[0], "browser:purge-session-history", false);
Services.obs.addObserver(gBrowser.browsers[0], "browser:purge-session-history", false);
// remove the disablehistory attribute so the browser cleans up, as
// though it had done this work itself
@ -1185,9 +1179,8 @@ function prepareForStartup() {
}
function delayedStartup(isLoadingBlank, mustLoadSidebar) {
var os = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
os.addObserver(gSessionHistoryObserver, "browser:purge-session-history", false);
os.addObserver(gXPInstallObserver, "xpinstall-install-blocked", false);
Services.obs.addObserver(gSessionHistoryObserver, "browser:purge-session-history", false);
Services.obs.addObserver(gXPInstallObserver, "xpinstall-install-blocked", false);
BrowserOffline.init();
OfflineApps.init();
@ -1256,12 +1249,10 @@ function delayedStartup(isLoadingBlank, mustLoadSidebar) {
[brandShortName]);
var checkboxLabel = shellBundle.getFormattedString("setDefaultBrowserDontAsk",
[brandShortName]);
const IPS = Components.interfaces.nsIPromptService;
var ps = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
.getService(IPS);
var checkEveryTime = { value: shouldCheck };
var ps = Services.prompt;
var rv = ps.confirmEx(window, promptTitle, promptMessage,
IPS.STD_YES_NO_BUTTONS,
ps.STD_YES_NO_BUTTONS,
null, null, null, checkboxLabel, checkEveryTime);
if (rv == 0)
shell.setDefaultBrowser(true, false);
@ -1401,11 +1392,9 @@ function BrowserShutdown()
Components.utils.reportError(ex);
}
var os = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
os.removeObserver(gSessionHistoryObserver, "browser:purge-session-history");
os.removeObserver(gXPInstallObserver, "xpinstall-install-blocked");
os.removeObserver(gMissingPluginInstaller.pluginCrashed, "plugin-crashed");
Services.obs.removeObserver(gSessionHistoryObserver, "browser:purge-session-history");
Services.obs.removeObserver(gXPInstallObserver, "xpinstall-install-blocked");
Services.obs.removeObserver(gMissingPluginInstaller.pluginCrashed, "plugin-crashed");
try {
gBrowser.removeProgressListener(window.XULBrowserWindow);
@ -1428,9 +1417,7 @@ function BrowserShutdown()
DownloadMonitorPanel.uninit();
gPrivateBrowsingUI.uninit();
var windowManager = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService();
var windowManagerInterface = windowManager.QueryInterface(Components.interfaces.nsIWindowMediator);
var enumerator = windowManagerInterface.getEnumerator(null);
var enumerator = Services.wm.getEnumerator(null);
enumerator.getNext();
if (!enumerator.hasMoreElements()) {
document.persist("sidebar-box", "sidebarcommand");
@ -1949,8 +1936,6 @@ function getShortcutOrURI(aURL, aPostDataRef) {
var shortcutURL = null;
var keyword = aURL;
var param = "";
var searchService = Cc["@mozilla.org/browser/search-service;1"].
getService(Ci.nsIBrowserSearchService);
var offset = aURL.indexOf(" ");
if (offset > 0) {
@ -1961,7 +1946,7 @@ function getShortcutOrURI(aURL, aPostDataRef) {
if (!aPostDataRef)
aPostDataRef = {};
var engine = searchService.getEngineByAlias(keyword);
var engine = Services.search.getEngineByAlias(keyword);
if (engine) {
var submission = engine.getSubmission(param, null);
aPostDataRef.value = submission.postData;
@ -2815,11 +2800,10 @@ var homeButtonObserver = {
function openHomeDialog(aURL)
{
var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);
var promptTitle = gNavigatorBundle.getString("droponhometitle");
var promptMsg = gNavigatorBundle.getString("droponhomemsg");
var pressedVal = promptService.confirmEx(window, promptTitle, promptMsg,
promptService.STD_YES_NO_BUTTONS,
var pressedVal = Services.prompt.confirmEx(window, promptTitle, promptMsg,
Services.prompt.STD_YES_NO_BUTTONS,
null, null, null, null, {value:0});
if (pressedVal == 0) {
@ -3072,9 +3056,7 @@ const BrowserSearch = {
// to the list of hidden engines rather than to the main list.
// XXX This will need to be changed when engines are identified by URL;
// see bug 335102.
var searchService = Cc["@mozilla.org/browser/search-service;1"].
getService(Ci.nsIBrowserSearchService);
if (searchService.getEngineByName(engine.title))
if (Services.search.getEngineByName(engine.title))
hidden = true;
var engines = (hidden ? browser.hiddenEngines : browser.engines) || [];
@ -3150,10 +3132,7 @@ const BrowserSearch = {
searchBar.select();
searchBar.focus();
} else {
var ss = Cc["@mozilla.org/browser/search-service;1"].
getService(Ci.nsIBrowserSearchService);
var searchForm = ss.defaultEngine.searchForm;
openUILinkIn(searchForm, "current");
openUILinkIn(Services.search.defaultEngine.searchForm, "current");
}
},
@ -3169,16 +3148,14 @@ const BrowserSearch = {
* tab.
*/
loadSearch: function BrowserSearch_search(searchText, useNewTab) {
var ss = Cc["@mozilla.org/browser/search-service;1"].
getService(Ci.nsIBrowserSearchService);
var engine;
// If the search bar is visible, use the current engine, otherwise, fall
// back to the default engine.
if (isElementVisible(this.searchBar))
engine = ss.currentEngine;
engine = Services.search.currentEngine;
else
engine = ss.defaultEngine;
engine = Services.search.defaultEngine;
var submission = engine.getSubmission(searchText, null); // HTML response
@ -3311,9 +3288,7 @@ function BrowserDownloadsUI()
function toOpenWindowByType(inType, uri, features)
{
var windowManager = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService();
var windowManagerInterface = windowManager.QueryInterface(Components.interfaces.nsIWindowMediator);
var topWindow = windowManagerInterface.getMostRecentWindow(inType);
var topWindow = Services.wm.getMostRecentWindow(inType);
if (topWindow)
topWindow.focus();
@ -4327,8 +4302,6 @@ var XULBrowserWindow = {
gBrowser.selectedBrowser.engines = null;
var uri = aRequest.QueryInterface(Ci.nsIChannel).URI;
var observerService = Cc["@mozilla.org/observer-service;1"]
.getService(Ci.nsIObserverService);
if (gURLBar &&
gURLBar.value == "" &&
@ -4336,7 +4309,7 @@ var XULBrowserWindow = {
URLBarSetURI(uri);
try {
observerService.notifyObservers(content, "StartDocumentLoad", uri.spec);
Services.obs.notifyObservers(content, "StartDocumentLoad", uri.spec);
} catch (e) {
}
},
@ -4344,12 +4317,9 @@ var XULBrowserWindow = {
endDocumentLoad: function (aRequest, aStatus) {
var urlStr = aRequest.QueryInterface(Ci.nsIChannel).originalURI.spec;
var observerService = Cc["@mozilla.org/observer-service;1"]
.getService(Ci.nsIObserverService);
var notification = Components.isSuccessCode(aStatus) ? "EndDocumentLoad" : "FailDocumentLoad";
try {
observerService.notifyObservers(content, notification, urlStr);
Services.obs.notifyObservers(content, notification, urlStr);
} catch (e) {
}
}
@ -5171,15 +5141,13 @@ function SelectDetector(event, doReload)
}
try {
var pref = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
var str = Components.classes["@mozilla.org/supports-string;1"]
.createInstance(Components.interfaces.nsISupportsString);
var str = Cc["@mozilla.org/supports-string;1"].
createInstance(Ci.nsISupportsString);
str.data = prefvalue;
pref.setComplexValue("intl.charset.detector",
Components.interfaces.nsISupportsString, str);
if (doReload) window.content.location.reload();
gPrefService.setComplexValue("intl.charset.detector", Ci.nsISupportsString, str);
if (doReload)
window.content.location.reload();
}
catch (ex) {
dump("Failed to set the intl.charset.detector preference.\n");
@ -5231,59 +5199,45 @@ function UpdateCurrentCharset()
}
}
function UpdateCharsetDetector()
{
var prefvalue;
function UpdateCharsetDetector() {
var prefvalue = "off";
try {
var pref = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
prefvalue = pref.getComplexValue("intl.charset.detector",
Components.interfaces.nsIPrefLocalizedString).data;
}
catch (ex) {
prefvalue = "";
}
try {
prefvalue = gPrefService.getComplexValue("intl.charset.detector", Ci.nsIPrefLocalizedString).data;
}
catch (ex) {}
if (prefvalue == "") prefvalue = "off";
dump("intl.charset.detector = "+ prefvalue + "\n");
prefvalue = "chardet." + prefvalue;
prefvalue = 'chardet.' + prefvalue;
var menuitem = document.getElementById(prefvalue);
if (menuitem) {
menuitem.setAttribute('checked', 'true');
}
var menuitem = document.getElementById(prefvalue);
if (menuitem)
menuitem.setAttribute("checked", "true");
}
function UpdateMenus(event)
{
// use setTimeout workaround to delay checkmark the menu
// when onmenucomplete is ready then use it instead of oncreate
// see bug 78290 for the detail
UpdateCurrentCharset();
setTimeout(UpdateCurrentCharset, 0);
UpdateCharsetDetector();
setTimeout(UpdateCharsetDetector, 0);
function UpdateMenus(event) {
// use setTimeout workaround to delay checkmark the menu
// when onmenucomplete is ready then use it instead of oncreate
// see bug 78290 for the detail
UpdateCurrentCharset();
setTimeout(UpdateCurrentCharset, 0);
UpdateCharsetDetector();
setTimeout(UpdateCharsetDetector, 0);
}
function CreateMenu(node)
{
var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
observerService.notifyObservers(null, "charsetmenu-selected", node);
function CreateMenu(node) {
Services.obs.notifyObservers(null, "charsetmenu-selected", node);
}
function charsetLoadListener (event)
{
var charset = window.content.document.characterSet;
function charsetLoadListener(event) {
var charset = window.content.document.characterSet;
if (charset.length > 0 && (charset != gLastBrowserCharset)) {
if (!gCharsetMenu)
gCharsetMenu = Components.classes['@mozilla.org/rdf/datasource;1?name=charset-menu'].getService().QueryInterface(Components.interfaces.nsICurrentCharsetListener);
gCharsetMenu.SetCurrentCharset(charset);
gPrevCharset = gLastBrowserCharset;
gLastBrowserCharset = charset;
}
if (charset.length > 0 && (charset != gLastBrowserCharset)) {
if (!gCharsetMenu)
gCharsetMenu = Cc['@mozilla.org/rdf/datasource;1?name=charset-menu'].getService(Ci.nsICurrentCharsetListener);
gCharsetMenu.SetCurrentCharset(charset);
gPrevCharset = gLastBrowserCharset;
gLastBrowserCharset = charset;
}
}
/* Begin Page Style Functions */
@ -5393,28 +5347,22 @@ var BrowserOffline = {
if (!this._uiElement)
this._uiElement = document.getElementById("goOfflineMenuitem");
var os = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
os.addObserver(this, "network:offline-status-changed", false);
Services.obs.addObserver(this, "network:offline-status-changed", false);
var ioService = Components.classes["@mozilla.org/network/io-service;1"].
getService(Components.interfaces.nsIIOService2);
this._updateOfflineUI(ioService.offline);
this._updateOfflineUI(Services.io.offline);
},
uninit: function ()
{
try {
var os = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
os.removeObserver(this, "network:offline-status-changed");
Services.obs.removeObserver(this, "network:offline-status-changed");
} catch (ex) {
}
},
toggleOfflineStatus: function ()
{
var ioService = Components.classes["@mozilla.org/network/io-service;1"].
getService(Components.interfaces.nsIIOService2);
var ioService = Services.io;
// Stop automatic management of the offline status
try {
@ -5448,19 +5396,17 @@ var BrowserOffline = {
// BrowserOffline Implementation Methods
_canGoOffline: function ()
{
var os = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
if (os) {
try {
var cancelGoOffline = Components.classes["@mozilla.org/supports-PRBool;1"].createInstance(Components.interfaces.nsISupportsPRBool);
os.notifyObservers(cancelGoOffline, "offline-requested", null);
try {
var cancelGoOffline = Cc["@mozilla.org/supports-PRBool;1"].createInstance(Ci.nsISupportsPRBool);
Services.obs.notifyObservers(cancelGoOffline, "offline-requested", null);
// Something aborted the quit process.
if (cancelGoOffline.data)
return false;
}
catch (ex) {
}
// Something aborted the quit process.
if (cancelGoOffline.data)
return false;
}
catch (ex) {
}
return true;
},
@ -5480,18 +5426,14 @@ var OfflineApps = {
// OfflineApps Public Methods
init: function ()
{
var obs = Cc["@mozilla.org/observer-service;1"].
getService(Ci.nsIObserverService);
obs.addObserver(this, "dom-storage-warn-quota-exceeded", false);
obs.addObserver(this, "offline-cache-update-completed", false);
Services.obs.addObserver(this, "dom-storage-warn-quota-exceeded", false);
Services.obs.addObserver(this, "offline-cache-update-completed", false);
},
uninit: function ()
{
var obs = Cc["@mozilla.org/observer-service;1"].
getService(Ci.nsIObserverService);
obs.removeObserver(this, "dom-storage-warn-quota-exceeded");
obs.removeObserver(this, "offline-cache-update-completed");
Services.obs.removeObserver(this, "dom-storage-warn-quota-exceeded");
Services.obs.removeObserver(this, "offline-cache-update-completed");
},
handleEvent: function(event) {
@ -5588,45 +5530,37 @@ var OfflineApps = {
// Now that we've warned once, prevent the warning from showing up
// again.
var pm = Cc["@mozilla.org/permissionmanager;1"].
getService(Ci.nsIPermissionManager);
pm.add(aURI, "offline-app",
Ci.nsIOfflineCacheUpdateService.ALLOW_NO_WARN);
Services.pm.add(aURI, "offline-app",
Ci.nsIOfflineCacheUpdateService.ALLOW_NO_WARN);
},
// XXX: duplicated in preferences/advanced.js
_getOfflineAppUsage: function (host, groups)
{
var cacheService = Components.classes["@mozilla.org/network/application-cache-service;1"].
getService(Components.interfaces.nsIApplicationCacheService);
var cacheService = Cc["@mozilla.org/network/application-cache-service;1"].
getService(Ci.nsIApplicationCacheService);
if (!groups)
groups = cacheService.getGroups();
var ios = Components.classes["@mozilla.org/network/io-service;1"].
getService(Components.interfaces.nsIIOService);
var usage = 0;
for (var i = 0; i < groups.length; i++) {
var uri = ios.newURI(groups[i], null, null);
var uri = Services.io.newURI(groups[i], null, null);
if (uri.asciiHost == host) {
var cache = cacheService.getActiveCache(groups[i]);
usage += cache.usage;
}
}
var storageManager = Components.classes["@mozilla.org/dom/storagemanager;1"].
getService(Components.interfaces.nsIDOMStorageManager);
var storageManager = Cc["@mozilla.org/dom/storagemanager;1"].
getService(Ci.nsIDOMStorageManager);
usage += storageManager.getUsage(host);
return usage;
},
_checkUsage: function(aURI) {
var pm = Cc["@mozilla.org/permissionmanager;1"].
getService(Ci.nsIPermissionManager);
// if the user has already allowed excessive usage, don't bother checking
if (pm.testExactPermission(aURI, "offline-app") !=
if (Services.pm.testExactPermission(aURI, "offline-app") !=
Ci.nsIOfflineCacheUpdateService.ALLOW_NO_WARN) {
var usage = this._getOfflineAppUsage(aURI.asciiHost);
var warnQuota = gPrefService.getIntPref("offline-apps.quota.warn");
@ -5648,12 +5582,9 @@ var OfflineApps = {
aContentWindow);
var currentURI = aContentWindow.document.documentURIObject;
var pm = Cc["@mozilla.org/permissionmanager;1"].
getService(Ci.nsIPermissionManager);
// don't bother showing UI if the user has already made a decision
if (pm.testExactPermission(currentURI, "offline-app") !=
Ci.nsIPermissionManager.UNKNOWN_ACTION)
if (Services.pm.testExactPermission(currentURI, "offline-app") != Services.pm.UNKNOWN_ACTION)
return;
try {
@ -5707,10 +5638,7 @@ var OfflineApps = {
},
allowSite: function(aDocument) {
var pm = Cc["@mozilla.org/permissionmanager;1"].
getService(Ci.nsIPermissionManager);
pm.add(aDocument.documentURIObject, "offline-app",
Ci.nsIPermissionManager.ALLOW_ACTION);
Services.pm.add(aDocument.documentURIObject, "offline-app", Services.pm.ALLOW_ACTION);
// When a site is enabled while loading, manifest resources will
// start fetching immediately. This one time we need to do it
@ -5719,10 +5647,7 @@ var OfflineApps = {
},
disallowSite: function(aDocument) {
var pm = Cc["@mozilla.org/permissionmanager;1"].
getService(Ci.nsIPermissionManager);
pm.add(aDocument.documentURIObject, "offline-app",
Ci.nsIPermissionManager.DENY_ACTION);
Services.pm.add(aDocument.documentURIObject, "offline-app", Services.pm.DENY_ACTION);
},
manage: function() {
@ -5804,8 +5729,7 @@ function warnAboutClosingWindow() {
// Figure out if there's at least one other browser window around.
let foundOtherBrowserWindow = false;
let wm = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
let e = wm.getEnumerator("navigator:browser");
let e = Services.wm.getEnumerator("navigator:browser");
while (e.hasMoreElements() && !foundOtherBrowserWindow) {
let win = e.getNext();
if (win != window && win.toolbar.visible)
@ -5814,12 +5738,12 @@ function warnAboutClosingWindow() {
if (foundOtherBrowserWindow)
return gBrowser.warnAboutClosingTabs(true);
let os = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
let os = Services.obs;
let closingCanceled = Cc["@mozilla.org/supports-PRBool;1"].
createInstance(Ci.nsISupportsPRBool);
os.notifyObservers(closingCanceled,
"browser-lastwindow-close-requested", null);
"browser-lastwindow-close-requested", null);
if (closingCanceled.data)
return false;
@ -5860,19 +5784,16 @@ var MailIntegration = {
// aURL --> a nsIURI which represents the url to launch
_launchExternalUrl: function (aURL) {
var extProtocolSvc =
Components.classes["@mozilla.org/uriloader/external-protocol-service;1"]
.getService(Components.interfaces.nsIExternalProtocolService);
Cc["@mozilla.org/uriloader/external-protocol-service;1"]
.getService(Ci.nsIExternalProtocolService);
if (extProtocolSvc)
extProtocolSvc.loadUrl(aURL);
}
};
function BrowserOpenAddonsMgr(aPane)
{
function BrowserOpenAddonsMgr(aPane) {
const EMTYPE = "Extension:Manager";
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator);
var theEM = wm.getMostRecentWindow(EMTYPE);
var theEM = Services.wm.getMostRecentWindow(EMTYPE);
if (theEM) {
theEM.focus();
if (aPane)
@ -5888,8 +5809,7 @@ function BrowserOpenAddonsMgr(aPane)
window.openDialog(EMURL, "", EMFEATURES);
}
function AddKeywordForSearchField()
{
function AddKeywordForSearchField() {
var node = document.popupNode;
var charset = node.ownerDocument.characterSet;
@ -7156,16 +7076,13 @@ function getBrowser() gBrowser;
function getNavToolbox() gNavToolbox;
let gPrivateBrowsingUI = {
_observerService: null,
_privateBrowsingService: null,
_searchBarValue: null,
_findBarValue: null,
init: function PBUI_init() {
this._observerService = Cc["@mozilla.org/observer-service;1"].
getService(Ci.nsIObserverService);
this._observerService.addObserver(this, "private-browsing", false);
this._observerService.addObserver(this, "private-browsing-transition-complete", false);
Services.obs.addObserver(this, "private-browsing", false);
Services.obs.addObserver(this, "private-browsing-transition-complete", false);
this._privateBrowsingService = Cc["@mozilla.org/privatebrowsing;1"].
getService(Ci.nsIPrivateBrowsingService);
@ -7175,8 +7092,8 @@ let gPrivateBrowsingUI = {
},
uninit: function PBUI_unint() {
this._observerService.removeObserver(this, "private-browsing");
this._observerService.removeObserver(this, "private-browsing-transition-complete");
Services.obs.removeObserver(this, "private-browsing");
Services.obs.removeObserver(this, "private-browsing-transition-complete");
},
get _disableUIOnToggle PBUI__disableUIOnTogle() {
@ -7235,12 +7152,11 @@ let gPrivateBrowsingUI = {
#endif
var message = pbBundle.formatStringFromName("privateBrowsingMessage", [appName], 1);
var promptService = Cc["@mozilla.org/embedcomp/prompt-service;1"].
getService(Ci.nsIPromptService);
var ps = Services.prompt;
var flags = promptService.BUTTON_TITLE_IS_STRING * promptService.BUTTON_POS_0 +
promptService.BUTTON_TITLE_IS_STRING * promptService.BUTTON_POS_1 +
promptService.BUTTON_POS_0_DEFAULT;
var flags = ps.BUTTON_TITLE_IS_STRING * ps.BUTTON_POS_0 +
ps.BUTTON_TITLE_IS_STRING * ps.BUTTON_POS_1 +
ps.BUTTON_POS_0_DEFAULT;
var neverAsk = {value:false};
var button0Title = pbBundle.GetStringFromName("privateBrowsingYesTitle");
@ -7248,9 +7164,9 @@ let gPrivateBrowsingUI = {
var neverAskText = pbBundle.GetStringFromName("privateBrowsingNeverAsk");
var result;
var choice = promptService.confirmEx(null, dialogTitle, header + message,
flags, button0Title, button1Title, null,
neverAskText, neverAsk);
var choice = ps.confirmEx(null, dialogTitle, header + message,
flags, button0Title, button1Title, null,
neverAskText, neverAsk);
switch (choice) {
case 0: // Start Private Browsing
@ -7535,7 +7451,7 @@ var LightWeightThemeWebInstaller = {
},
_isAllowed: function (node) {
var pm = Cc["@mozilla.org/permissionmanager;1"].getService(Ci.nsIPermissionManager);
var pm = Services.pm;
var prefs = [["xpinstall.whitelist.add", pm.ALLOW_ACTION],
["xpinstall.whitelist.add.36", pm.ALLOW_ACTION],

View File

@ -0,0 +1,35 @@
var gTestTab;
function test() {
waitForExplicitFinish();
is(getTopWin(), window, "got top window");
is(getBoolPref("general.startup.browser", false), true, "getBoolPref");
is(getBoolPref("this.pref.doesnt.exist", true), true, "getBoolPref fallback");
is(getBoolPref("this.pref.doesnt.exist", false), false, "getBoolPref fallback #2");
gTestTab = openNewTabWith("http://example.com");
gBrowser.selectedTab = gTestTab;
gTestTab.linkedBrowser.addEventListener("load", function () {
gTestTab.linkedBrowser.removeEventListener("load", arguments.callee, true);
is(gTestTab.linkedBrowser.currentURI.spec, "http://example.com/", "example.com loaded");
test_openUILink();
}, true);
}
function test_openUILink() {
gTestTab.linkedBrowser.addEventListener("load", function () {
gTestTab.linkedBrowser.removeEventListener("load", arguments.callee, true);
is(gTestTab.linkedBrowser.currentURI.spec, "http://example.org/", "example.org loaded");
gBrowser.removeTab(gTestTab);
finish();
}, true);
//openUILink(url, e, ignoreButton, ignoreAlt, allowKeywordFixup, postData, referrerUrl);
openUILink("http://example.org"); // defaults to "current"
}

View File

@ -22,6 +22,7 @@
# Contributor(s):
# Alec Flett <alecf@netscape.com>
# Ehsan Akhgari <ehsan.akhgari@gmail.com>
# Gavin Sharp <gavin@gavinsharp.com>
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
@ -37,10 +38,8 @@
#
# ***** END LICENSE BLOCK *****
/**
* Communicator Shared Utility Library
* for shared application glue for the Communicator suite of applications
**/
// Services = object with smart getters for common XPCOM services
Components.utils.import("resource://gre/modules/Services.jsm");
var TAB_DROP_TYPE = "application/x-moz-tabbrowser-tab";
@ -69,9 +68,7 @@ function goToggleToolbar( id, elementID )
function getTopWin()
{
var windowManager = Components.classes['@mozilla.org/appshell/window-mediator;1']
.getService(Components.interfaces.nsIWindowMediator);
return windowManager.getMostRecentWindow("navigator:browser");
return Services.wm.getMostRecentWindow("navigator:browser");
}
function openTopWin( url )
@ -79,12 +76,10 @@ function openTopWin( url )
openUILink(url, {})
}
function getBoolPref ( prefname, def )
function getBoolPref(prefname, def)
{
try {
var pref = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
return pref.getBoolPref(prefname);
try {
return Services.prefs.getBoolPref(prefname);
}
catch(er) {
return def;
@ -231,7 +226,7 @@ function openUILinkIn(url, where, aAllowThirdPartyFixup, aPostData, aReferrerURI
return;
}
var loadInBackground = getBoolPref("browser.tabs.loadBookmarksInBackground", false);
var loadInBackground = getBoolPref("browser.tabs.loadBookmarksInBackground");
switch (where) {
case "current":
@ -375,9 +370,7 @@ function isBidiEnabled() {
case "ur-":
case "syr":
rv = true;
var pref = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
pref.setBoolPref("bidi.browser.ui", true);
Services.prefs.setBoolPref("bidi.browser.ui", true);
}
} catch (e) {}
@ -387,9 +380,7 @@ function isBidiEnabled() {
function openAboutDialog()
{
#ifdef XP_MACOSX
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator);
var win = wm.getMostRecentWindow("Browser:About");
var win = Services.wm.getMostRecentWindow("Browser:About");
if (win)
win.focus();
else {
@ -406,9 +397,7 @@ function openPreferences(paneID, extraArgs)
var instantApply = getBoolPref("browser.preferences.instantApply", false);
var features = "chrome,titlebar,toolbar,centerscreen" + (instantApply ? ",dialog=no" : ",modal");
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator);
var win = wm.getMostRecentWindow("Browser:Preferences");
var win = Services.wm.getMostRecentWindow("Browser:Preferences");
if (win) {
win.focus();
if (paneID) {
@ -587,17 +576,7 @@ function openNewTabWith(aURL, aDocument, aPostData, aEvent,
if (aDocument)
urlSecurityCheck(aURL, aDocument.nodePrincipal);
var prefSvc = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService);
prefSvc = prefSvc.getBranch(null);
// should we open it in a new tab?
var loadInBackground = true;
try {
loadInBackground = prefSvc.getBoolPref("browser.tabs.loadInBackground");
}
catch(ex) {
}
var loadInBackground = getBoolPref("browser.tabs.loadInBackground");
if (aEvent && aEvent.shiftKey)
loadInBackground = !loadInBackground;
@ -688,12 +667,9 @@ function openHelpLink(aHelpTopic, aCalledFromModal) {
}
function openPrefsHelp() {
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch2);
// non-instant apply prefwindows are usually modal, so we can't open in the topmost window,
// since its probably behind the window.
var instantApply = prefs.getBoolPref("browser.preferences.instantApply");
var instantApply = getBoolPref("browser.preferences.instantApply");
var helpTopic = document.getElementsByTagName("prefwindow")[0].currentPane.helpTopic;
openHelpLink(helpTopic, !instantApply);

View File

@ -79,6 +79,7 @@ DIRS += tests
endif
EXTRA_JS_MODULES = \
Services.jsm \
InlineSpellChecker.jsm \
WindowDraggingUtils.jsm \
$(NULL)

View File

@ -0,0 +1,74 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Gavin Sharp <gavin@gavinsharp.com> (original author)
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
let EXPORTED_SYMBOLS = ["Services"];
const Ci = Components.interfaces;
const Cc = Components.classes;
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
let Services = {};
XPCOMUtils.defineLazyGetter(Services, "prefs", function () {
return Cc["@mozilla.org/preferences-service;1"]
.getService(Ci.nsIPrefService)
.QueryInterface(Ci.nsIPrefBranch2);
});
XPCOMUtils.defineLazyServiceGetter(Services, "wm",
"@mozilla.org/appshell/window-mediator;1",
"nsIWindowMediator");
XPCOMUtils.defineLazyServiceGetter(Services, "obs",
"@mozilla.org/observer-service;1",
"nsIObserverService");
XPCOMUtils.defineLazyServiceGetter(Services, "pm",
"@mozilla.org/permissionmanager;1",
"nsIPermissionManager");
XPCOMUtils.defineLazyServiceGetter(Services, "io",
"@mozilla.org/network/io-service;1",
"nsIIOService2");
XPCOMUtils.defineLazyServiceGetter(Services, "prompt",
"@mozilla.org/embedcomp/prompt-service;1",
"nsIPromptService");
XPCOMUtils.defineLazyServiceGetter(Services, "search",
"@mozilla.org/browser/search-service;1",
"nsIBrowserSearchService");

View File

@ -54,6 +54,7 @@ _BROWSER_TEST_FILES = \
browser_bug471962.js \
browser_keyevents_during_autoscrolling.js \
browser_bug295977_autoscroll_overflow.js \
browser_Services.js \
$(NULL)
libs:: $(_BROWSER_TEST_FILES)

View File

@ -0,0 +1,52 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Gavin Sharp <gavin@gavinsharp.com> (original author)
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
function test() {
/** Tests for Services.jsm (Bug 512784) **/
ok(Services, "Services object exists");
checkServices();
}
function checkServices() {
ok(Services.prefs instanceof Ci.nsIPrefBranch2, "Services.prefs is an nsIPrefBranch2");
ok(Services.prefs instanceof Ci.nsIPrefService, "Services.prefs is an nsIPrefService");
ok(Services.wm instanceof Ci.nsIWindowMediator, "Services.wm is an nsIWindowMediator");
ok(Services.pm instanceof Ci.nsIPermissionManager, "Services.pm is an nsIPermissionManager");
ok(Services.io instanceof Ci.nsIIOService, "Services.io is an nsIIOService");
ok(Services.io instanceof Ci.nsIIOService2, "Services.io is an nsIIOService2");
ok(Services.prompt instanceof Ci.nsIPromptService, "Services.prompt is an nsIPromptService");
ok(Services.search instanceof Ci.nsIBrowserSearchService, "Services.search is an nsIBrowserSearchService");
}