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 michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: module.metadata = { michael@0: "stability": "unstable", michael@0: "engines": { michael@0: "Firefox": "*" michael@0: } michael@0: }; michael@0: michael@0: const { Cc, Ci, Cu } = require("chrome"); michael@0: const { defer, reject } = require("../core/promise"); michael@0: const FaviconService = Cc["@mozilla.org/browser/favicon-service;1"]. michael@0: getService(Ci.nsIFaviconService); michael@0: const AsyncFavicons = FaviconService.QueryInterface(Ci.mozIAsyncFavicons); michael@0: const { isValidURI } = require("../url"); michael@0: const { newURI, getURL } = require("../url/utils"); michael@0: michael@0: /** michael@0: * Takes an object of several possible types and michael@0: * returns a promise that resolves to the page's favicon URI. michael@0: * @param {String|Tab} object michael@0: * @param {Function} (callback) michael@0: * @returns {Promise} michael@0: */ michael@0: michael@0: function getFavicon (object, callback) { michael@0: let url = getURL(object); michael@0: let deferred = defer(); michael@0: michael@0: if (url && isValidURI(url)) { michael@0: AsyncFavicons.getFaviconURLForPage(newURI(url), function (aURI) { michael@0: if (aURI && aURI.spec) michael@0: deferred.resolve(aURI.spec.toString()); michael@0: else michael@0: deferred.reject(null); michael@0: }); michael@0: } else { michael@0: deferred.reject(null); michael@0: } michael@0: michael@0: if (callback) deferred.promise.then(callback, callback); michael@0: return deferred.promise; michael@0: } michael@0: exports.getFavicon = getFavicon;