michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: let FavIcons = { michael@0: // Pref that controls whether to display site icons. michael@0: PREF_CHROME_SITE_ICONS: "browser.chrome.site_icons", michael@0: michael@0: // Pref that controls whether to display fav icons. michael@0: PREF_CHROME_FAVICONS: "browser.chrome.favicons", michael@0: michael@0: // Lazy getter for pref browser.chrome.site_icons. michael@0: get _prefSiteIcons() { michael@0: delete this._prefSiteIcons; michael@0: this._prefSiteIcons = Services.prefs.getBoolPref(this.PREF_CHROME_SITE_ICONS); michael@0: }, michael@0: michael@0: // Lazy getter for pref browser.chrome.favicons. michael@0: get _prefFavicons() { michael@0: delete this._prefFavicons; michael@0: this._prefFavicons = Services.prefs.getBoolPref(this.PREF_CHROME_FAVICONS); michael@0: }, michael@0: michael@0: get defaultFavicon() this._favIconService.defaultFavicon.spec, michael@0: michael@0: init: function FavIcons_init() { michael@0: XPCOMUtils.defineLazyServiceGetter(this, "_favIconService", michael@0: "@mozilla.org/browser/favicon-service;1", "nsIFaviconService"); michael@0: michael@0: Services.prefs.addObserver(this.PREF_CHROME_SITE_ICONS, this, false); michael@0: Services.prefs.addObserver(this.PREF_CHROME_FAVICONS, this, false); michael@0: }, michael@0: michael@0: uninit: function FavIcons_uninit() { michael@0: Services.prefs.removeObserver(this.PREF_CHROME_SITE_ICONS, this); michael@0: Services.prefs.removeObserver(this.PREF_CHROME_FAVICONS, this); michael@0: }, michael@0: michael@0: observe: function FavIcons_observe(subject, topic, data) { michael@0: let value = Services.prefs.getBoolPref(data); michael@0: michael@0: if (data == this.PREF_CHROME_SITE_ICONS) michael@0: this._prefSiteIcons = value; michael@0: else if (data == this.PREF_CHROME_FAVICONS) michael@0: this._prefFavicons = value; michael@0: }, michael@0: michael@0: // ---------- michael@0: // Function: getFavIconUrlForTab michael@0: // Gets the "favicon link URI" for the given xul:tab, or null if unavailable. michael@0: getFavIconUrlForTab: function FavIcons_getFavIconUrlForTab(tab, callback) { michael@0: this._isImageDocument(tab, function (isImageDoc) { michael@0: if (isImageDoc) { michael@0: callback(tab.pinned ? tab.image : null); michael@0: } else { michael@0: this._getFavIconForNonImageDocument(tab, callback); michael@0: } michael@0: }.bind(this)); michael@0: }, michael@0: michael@0: // ---------- michael@0: // Function: _getFavIconForNonImageDocument michael@0: // Retrieves the favicon for a tab containing a non-image document. michael@0: _getFavIconForNonImageDocument: michael@0: function FavIcons_getFavIconForNonImageDocument(tab, callback) { michael@0: michael@0: if (tab.image) michael@0: this._getFavIconFromTabImage(tab, callback); michael@0: else if (this._shouldLoadFavIcon(tab)) michael@0: this._getFavIconForHttpDocument(tab, callback); michael@0: else michael@0: callback(null); michael@0: }, michael@0: michael@0: // ---------- michael@0: // Function: _getFavIconFromTabImage michael@0: // Retrieves the favicon for tab with a tab image. michael@0: _getFavIconFromTabImage: michael@0: function FavIcons_getFavIconFromTabImage(tab, callback) { michael@0: michael@0: let tabImage = gBrowser.getIcon(tab); michael@0: michael@0: // If the tab image's url starts with http(s), fetch icon from favicon michael@0: // service via the moz-anno protocol. michael@0: if (/^https?:/.test(tabImage)) { michael@0: let tabImageURI = gWindow.makeURI(tabImage); michael@0: tabImage = this._favIconService.getFaviconLinkForIcon(tabImageURI).spec; michael@0: } michael@0: michael@0: callback(tabImage); michael@0: }, michael@0: michael@0: // ---------- michael@0: // Function: _getFavIconForHttpDocument michael@0: // Retrieves the favicon for tab containg a http(s) document. michael@0: _getFavIconForHttpDocument: michael@0: function FavIcons_getFavIconForHttpDocument(tab, callback) { michael@0: michael@0: let {currentURI} = tab.linkedBrowser; michael@0: this._favIconService.getFaviconURLForPage(currentURI, function (uri) { michael@0: if (uri) { michael@0: callback(this._favIconService.getFaviconLinkForIcon(uri).spec); michael@0: } else { michael@0: callback(this.defaultFavicon); michael@0: } michael@0: }.bind(this)); michael@0: }, michael@0: michael@0: // ---------- michael@0: // Function: _isImageDocument michael@0: // Checks whether an image is loaded into the given tab. michael@0: _isImageDocument: function UI__isImageDocument(tab, callback) { michael@0: let mm = tab.linkedBrowser.messageManager; michael@0: let message = "Panorama:isImageDocument"; michael@0: michael@0: mm.addMessageListener(message, function onMessage(cx) { michael@0: mm.removeMessageListener(cx.name, onMessage); michael@0: callback(cx.json.isImageDocument); michael@0: }); michael@0: michael@0: mm.sendAsyncMessage(message); michael@0: }, michael@0: michael@0: // ---------- michael@0: // Function: _shouldLoadFavIcon michael@0: // Checks whether fav icon should be loaded for a given tab. michael@0: _shouldLoadFavIcon: function FavIcons_shouldLoadFavIcon(tab) { michael@0: // No need to load a favicon if the user doesn't want site or favicons. michael@0: if (!this._prefSiteIcons || !this._prefFavicons) michael@0: return false; michael@0: michael@0: let uri = tab.linkedBrowser.currentURI; michael@0: michael@0: // Stop here if we don't have a valid nsIURI. michael@0: if (!uri || !(uri instanceof Ci.nsIURI)) michael@0: return false; michael@0: michael@0: // Load favicons for http(s) pages only. michael@0: return uri.schemeIs("http") || uri.schemeIs("https"); michael@0: } michael@0: };