browser/components/tabview/favicons.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 let FavIcons = {
michael@0 6 // Pref that controls whether to display site icons.
michael@0 7 PREF_CHROME_SITE_ICONS: "browser.chrome.site_icons",
michael@0 8
michael@0 9 // Pref that controls whether to display fav icons.
michael@0 10 PREF_CHROME_FAVICONS: "browser.chrome.favicons",
michael@0 11
michael@0 12 // Lazy getter for pref browser.chrome.site_icons.
michael@0 13 get _prefSiteIcons() {
michael@0 14 delete this._prefSiteIcons;
michael@0 15 this._prefSiteIcons = Services.prefs.getBoolPref(this.PREF_CHROME_SITE_ICONS);
michael@0 16 },
michael@0 17
michael@0 18 // Lazy getter for pref browser.chrome.favicons.
michael@0 19 get _prefFavicons() {
michael@0 20 delete this._prefFavicons;
michael@0 21 this._prefFavicons = Services.prefs.getBoolPref(this.PREF_CHROME_FAVICONS);
michael@0 22 },
michael@0 23
michael@0 24 get defaultFavicon() this._favIconService.defaultFavicon.spec,
michael@0 25
michael@0 26 init: function FavIcons_init() {
michael@0 27 XPCOMUtils.defineLazyServiceGetter(this, "_favIconService",
michael@0 28 "@mozilla.org/browser/favicon-service;1", "nsIFaviconService");
michael@0 29
michael@0 30 Services.prefs.addObserver(this.PREF_CHROME_SITE_ICONS, this, false);
michael@0 31 Services.prefs.addObserver(this.PREF_CHROME_FAVICONS, this, false);
michael@0 32 },
michael@0 33
michael@0 34 uninit: function FavIcons_uninit() {
michael@0 35 Services.prefs.removeObserver(this.PREF_CHROME_SITE_ICONS, this);
michael@0 36 Services.prefs.removeObserver(this.PREF_CHROME_FAVICONS, this);
michael@0 37 },
michael@0 38
michael@0 39 observe: function FavIcons_observe(subject, topic, data) {
michael@0 40 let value = Services.prefs.getBoolPref(data);
michael@0 41
michael@0 42 if (data == this.PREF_CHROME_SITE_ICONS)
michael@0 43 this._prefSiteIcons = value;
michael@0 44 else if (data == this.PREF_CHROME_FAVICONS)
michael@0 45 this._prefFavicons = value;
michael@0 46 },
michael@0 47
michael@0 48 // ----------
michael@0 49 // Function: getFavIconUrlForTab
michael@0 50 // Gets the "favicon link URI" for the given xul:tab, or null if unavailable.
michael@0 51 getFavIconUrlForTab: function FavIcons_getFavIconUrlForTab(tab, callback) {
michael@0 52 this._isImageDocument(tab, function (isImageDoc) {
michael@0 53 if (isImageDoc) {
michael@0 54 callback(tab.pinned ? tab.image : null);
michael@0 55 } else {
michael@0 56 this._getFavIconForNonImageDocument(tab, callback);
michael@0 57 }
michael@0 58 }.bind(this));
michael@0 59 },
michael@0 60
michael@0 61 // ----------
michael@0 62 // Function: _getFavIconForNonImageDocument
michael@0 63 // Retrieves the favicon for a tab containing a non-image document.
michael@0 64 _getFavIconForNonImageDocument:
michael@0 65 function FavIcons_getFavIconForNonImageDocument(tab, callback) {
michael@0 66
michael@0 67 if (tab.image)
michael@0 68 this._getFavIconFromTabImage(tab, callback);
michael@0 69 else if (this._shouldLoadFavIcon(tab))
michael@0 70 this._getFavIconForHttpDocument(tab, callback);
michael@0 71 else
michael@0 72 callback(null);
michael@0 73 },
michael@0 74
michael@0 75 // ----------
michael@0 76 // Function: _getFavIconFromTabImage
michael@0 77 // Retrieves the favicon for tab with a tab image.
michael@0 78 _getFavIconFromTabImage:
michael@0 79 function FavIcons_getFavIconFromTabImage(tab, callback) {
michael@0 80
michael@0 81 let tabImage = gBrowser.getIcon(tab);
michael@0 82
michael@0 83 // If the tab image's url starts with http(s), fetch icon from favicon
michael@0 84 // service via the moz-anno protocol.
michael@0 85 if (/^https?:/.test(tabImage)) {
michael@0 86 let tabImageURI = gWindow.makeURI(tabImage);
michael@0 87 tabImage = this._favIconService.getFaviconLinkForIcon(tabImageURI).spec;
michael@0 88 }
michael@0 89
michael@0 90 callback(tabImage);
michael@0 91 },
michael@0 92
michael@0 93 // ----------
michael@0 94 // Function: _getFavIconForHttpDocument
michael@0 95 // Retrieves the favicon for tab containg a http(s) document.
michael@0 96 _getFavIconForHttpDocument:
michael@0 97 function FavIcons_getFavIconForHttpDocument(tab, callback) {
michael@0 98
michael@0 99 let {currentURI} = tab.linkedBrowser;
michael@0 100 this._favIconService.getFaviconURLForPage(currentURI, function (uri) {
michael@0 101 if (uri) {
michael@0 102 callback(this._favIconService.getFaviconLinkForIcon(uri).spec);
michael@0 103 } else {
michael@0 104 callback(this.defaultFavicon);
michael@0 105 }
michael@0 106 }.bind(this));
michael@0 107 },
michael@0 108
michael@0 109 // ----------
michael@0 110 // Function: _isImageDocument
michael@0 111 // Checks whether an image is loaded into the given tab.
michael@0 112 _isImageDocument: function UI__isImageDocument(tab, callback) {
michael@0 113 let mm = tab.linkedBrowser.messageManager;
michael@0 114 let message = "Panorama:isImageDocument";
michael@0 115
michael@0 116 mm.addMessageListener(message, function onMessage(cx) {
michael@0 117 mm.removeMessageListener(cx.name, onMessage);
michael@0 118 callback(cx.json.isImageDocument);
michael@0 119 });
michael@0 120
michael@0 121 mm.sendAsyncMessage(message);
michael@0 122 },
michael@0 123
michael@0 124 // ----------
michael@0 125 // Function: _shouldLoadFavIcon
michael@0 126 // Checks whether fav icon should be loaded for a given tab.
michael@0 127 _shouldLoadFavIcon: function FavIcons_shouldLoadFavIcon(tab) {
michael@0 128 // No need to load a favicon if the user doesn't want site or favicons.
michael@0 129 if (!this._prefSiteIcons || !this._prefFavicons)
michael@0 130 return false;
michael@0 131
michael@0 132 let uri = tab.linkedBrowser.currentURI;
michael@0 133
michael@0 134 // Stop here if we don't have a valid nsIURI.
michael@0 135 if (!uri || !(uri instanceof Ci.nsIURI))
michael@0 136 return false;
michael@0 137
michael@0 138 // Load favicons for http(s) pages only.
michael@0 139 return uri.schemeIs("http") || uri.schemeIs("https");
michael@0 140 }
michael@0 141 };

mercurial