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.

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

mercurial