favicon.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. module.metadata = {
  6. "stability": "unstable",
  7. "engines": {
  8. "Firefox": "*"
  9. }
  10. };
  11. const { Cc, Ci, Cu } = require("chrome");
  12. const { defer, reject } = require("../core/promise");
  13. const FaviconService = Cc["@mozilla.org/browser/favicon-service;1"].
  14. getService(Ci.nsIFaviconService);
  15. const AsyncFavicons = FaviconService.QueryInterface(Ci.mozIAsyncFavicons);
  16. const { isValidURI } = require("../url");
  17. const { newURI, getURL } = require("../url/utils");
  18. /**
  19. * Takes an object of several possible types and
  20. * returns a promise that resolves to the page's favicon URI.
  21. * @param {String|Tab} object
  22. * @param {Function} (callback)
  23. * @returns {Promise}
  24. */
  25. function getFavicon (object, callback) {
  26. let url = getURL(object);
  27. let deferred = defer();
  28. if (url && isValidURI(url)) {
  29. AsyncFavicons.getFaviconURLForPage(newURI(url), function (aURI) {
  30. if (aURI && aURI.spec)
  31. deferred.resolve(aURI.spec.toString());
  32. else
  33. deferred.reject(null);
  34. });
  35. } else {
  36. deferred.reject(null);
  37. }
  38. if (callback) deferred.promise.then(callback, callback);
  39. return deferred.promise;
  40. }
  41. exports.getFavicon = getFavicon;