1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/lib/sdk/io/data.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,82 @@ 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 +}; 1.13 + 1.14 +const { Cc, Ci, Cu } = require("chrome"); 1.15 +const base64 = require("../base64"); 1.16 +const IOService = Cc["@mozilla.org/network/io-service;1"]. 1.17 + getService(Ci.nsIIOService); 1.18 + 1.19 +const { deprecateFunction } = require('../util/deprecate'); 1.20 +const { NetUtil } = Cu.import("resource://gre/modules/NetUtil.jsm"); 1.21 +const FaviconService = Cc["@mozilla.org/browser/favicon-service;1"]. 1.22 + getService(Ci.nsIFaviconService); 1.23 + 1.24 +const PNG_B64 = "data:image/png;base64,"; 1.25 +const DEF_FAVICON_URI = "chrome://mozapps/skin/places/defaultFavicon.png"; 1.26 +let DEF_FAVICON = null; 1.27 + 1.28 +/** 1.29 + * Takes URI of the page and returns associated favicon URI. 1.30 + * If page under passed uri has no favicon then base64 encoded data URI of 1.31 + * default faveicon is returned. 1.32 + * @param {String} uri 1.33 + * @returns {String} 1.34 + */ 1.35 +function getFaviconURIForLocation(uri) { 1.36 + let pageURI = NetUtil.newURI(uri); 1.37 + try { 1.38 + return FaviconService.getFaviconDataAsDataURL( 1.39 + FaviconService.getFaviconForPage(pageURI)); 1.40 + } 1.41 + catch(e) { 1.42 + if (!DEF_FAVICON) { 1.43 + DEF_FAVICON = PNG_B64 + 1.44 + base64.encode(getChromeURIContent(DEF_FAVICON_URI)); 1.45 + } 1.46 + return DEF_FAVICON; 1.47 + } 1.48 +} 1.49 +exports.getFaviconURIForLocation = getFaviconURIForLocation; 1.50 + 1.51 +/** 1.52 + * Takes chrome URI and returns content under that URI. 1.53 + * @param {String} chromeURI 1.54 + * @returns {String} 1.55 + */ 1.56 +function getChromeURIContent(chromeURI) { 1.57 + let channel = IOService.newChannel(chromeURI, null, null); 1.58 + let input = channel.open(); 1.59 + let stream = Cc["@mozilla.org/binaryinputstream;1"]. 1.60 + createInstance(Ci.nsIBinaryInputStream); 1.61 + stream.setInputStream(input); 1.62 + let content = stream.readBytes(input.available()); 1.63 + stream.close(); 1.64 + input.close(); 1.65 + return content; 1.66 +} 1.67 +exports.getChromeURIContent = deprecateFunction(getChromeURIContent, 1.68 + 'getChromeURIContent is deprecated, ' + 1.69 + 'please use require("sdk/net/url").readURI instead.' 1.70 +); 1.71 + 1.72 +/** 1.73 + * Creates a base-64 encoded ASCII string from a string of binary data. 1.74 + */ 1.75 +exports.base64Encode = deprecateFunction(base64.encode, 1.76 + 'base64Encode is deprecated, ' + 1.77 + 'please use require("sdk/base64").encode instead.' 1.78 +); 1.79 +/** 1.80 + * Decodes a string of data which has been encoded using base-64 encoding. 1.81 + */ 1.82 +exports.base64Decode = deprecateFunction(base64.decode, 1.83 + 'base64Dencode is deprecated, ' + 1.84 + 'please use require("sdk/base64").decode instead.' 1.85 +);