addon-sdk/source/lib/sdk/places/favicon.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/lib/sdk/places/favicon.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,48 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +module.metadata = {
    1.11 +  "stability": "unstable",
    1.12 +  "engines": {
    1.13 +    "Firefox": "*"
    1.14 +  }
    1.15 +};
    1.16 +
    1.17 +const { Cc, Ci, Cu } = require("chrome");
    1.18 +const { defer, reject } = require("../core/promise");
    1.19 +const FaviconService = Cc["@mozilla.org/browser/favicon-service;1"].
    1.20 +                          getService(Ci.nsIFaviconService);
    1.21 +const AsyncFavicons = FaviconService.QueryInterface(Ci.mozIAsyncFavicons);
    1.22 +const { isValidURI } = require("../url");
    1.23 +const { newURI, getURL } = require("../url/utils");
    1.24 +
    1.25 +/**
    1.26 + * Takes an object of several possible types and
    1.27 + * returns a promise that resolves to the page's favicon URI.
    1.28 + * @param {String|Tab} object
    1.29 + * @param {Function} (callback)
    1.30 + * @returns {Promise}
    1.31 + */
    1.32 +
    1.33 +function getFavicon (object, callback) {
    1.34 +  let url = getURL(object);
    1.35 +  let deferred = defer();
    1.36 +
    1.37 +  if (url && isValidURI(url)) {
    1.38 +    AsyncFavicons.getFaviconURLForPage(newURI(url), function (aURI) {
    1.39 +      if (aURI && aURI.spec)
    1.40 +        deferred.resolve(aURI.spec.toString());
    1.41 +      else
    1.42 +        deferred.reject(null);
    1.43 +    });
    1.44 +  } else {
    1.45 +    deferred.reject(null);
    1.46 +  }
    1.47 +
    1.48 +  if (callback) deferred.promise.then(callback, callback);
    1.49 +  return deferred.promise;
    1.50 +}
    1.51 +exports.getFavicon = getFavicon;

mercurial