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: }; michael@0: michael@0: const { Cc, Ci, Cu } = require("chrome"); michael@0: const base64 = require("../base64"); michael@0: const IOService = Cc["@mozilla.org/network/io-service;1"]. michael@0: getService(Ci.nsIIOService); michael@0: michael@0: const { deprecateFunction } = require('../util/deprecate'); michael@0: const { NetUtil } = Cu.import("resource://gre/modules/NetUtil.jsm"); michael@0: const FaviconService = Cc["@mozilla.org/browser/favicon-service;1"]. michael@0: getService(Ci.nsIFaviconService); michael@0: michael@0: const PNG_B64 = "data:image/png;base64,"; michael@0: const DEF_FAVICON_URI = "chrome://mozapps/skin/places/defaultFavicon.png"; michael@0: let DEF_FAVICON = null; michael@0: michael@0: /** michael@0: * Takes URI of the page and returns associated favicon URI. michael@0: * If page under passed uri has no favicon then base64 encoded data URI of michael@0: * default faveicon is returned. michael@0: * @param {String} uri michael@0: * @returns {String} michael@0: */ michael@0: function getFaviconURIForLocation(uri) { michael@0: let pageURI = NetUtil.newURI(uri); michael@0: try { michael@0: return FaviconService.getFaviconDataAsDataURL( michael@0: FaviconService.getFaviconForPage(pageURI)); michael@0: } michael@0: catch(e) { michael@0: if (!DEF_FAVICON) { michael@0: DEF_FAVICON = PNG_B64 + michael@0: base64.encode(getChromeURIContent(DEF_FAVICON_URI)); michael@0: } michael@0: return DEF_FAVICON; michael@0: } michael@0: } michael@0: exports.getFaviconURIForLocation = getFaviconURIForLocation; michael@0: michael@0: /** michael@0: * Takes chrome URI and returns content under that URI. michael@0: * @param {String} chromeURI michael@0: * @returns {String} michael@0: */ michael@0: function getChromeURIContent(chromeURI) { michael@0: let channel = IOService.newChannel(chromeURI, null, null); michael@0: let input = channel.open(); michael@0: let stream = Cc["@mozilla.org/binaryinputstream;1"]. michael@0: createInstance(Ci.nsIBinaryInputStream); michael@0: stream.setInputStream(input); michael@0: let content = stream.readBytes(input.available()); michael@0: stream.close(); michael@0: input.close(); michael@0: return content; michael@0: } michael@0: exports.getChromeURIContent = deprecateFunction(getChromeURIContent, michael@0: 'getChromeURIContent is deprecated, ' + michael@0: 'please use require("sdk/net/url").readURI instead.' michael@0: ); michael@0: michael@0: /** michael@0: * Creates a base-64 encoded ASCII string from a string of binary data. michael@0: */ michael@0: exports.base64Encode = deprecateFunction(base64.encode, michael@0: 'base64Encode is deprecated, ' + michael@0: 'please use require("sdk/base64").encode instead.' michael@0: ); michael@0: /** michael@0: * Decodes a string of data which has been encoded using base-64 encoding. michael@0: */ michael@0: exports.base64Decode = deprecateFunction(base64.decode, michael@0: 'base64Dencode is deprecated, ' + michael@0: 'please use require("sdk/base64").decode instead.' michael@0: );