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