addon-sdk/source/lib/sdk/io/data.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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/. */
     5 "use strict";
     7 module.metadata = {
     8   "stability": "unstable"
     9 };
    11 const { Cc, Ci, Cu } = require("chrome");
    12 const base64 = require("../base64");
    13 const IOService = Cc["@mozilla.org/network/io-service;1"].
    14   getService(Ci.nsIIOService);
    16 const { deprecateFunction } = require('../util/deprecate');
    17 const { NetUtil } = Cu.import("resource://gre/modules/NetUtil.jsm");
    18 const FaviconService = Cc["@mozilla.org/browser/favicon-service;1"].
    19                           getService(Ci.nsIFaviconService);
    21 const PNG_B64 = "data:image/png;base64,";
    22 const DEF_FAVICON_URI = "chrome://mozapps/skin/places/defaultFavicon.png";
    23 let   DEF_FAVICON = null;
    25 /**
    26  * Takes URI of the page and returns associated favicon URI.
    27  * If page under passed uri has no favicon then base64 encoded data URI of
    28  * default faveicon is returned.
    29  * @param {String} uri
    30  * @returns {String}
    31  */
    32 function getFaviconURIForLocation(uri) {
    33   let pageURI = NetUtil.newURI(uri);
    34   try {
    35     return FaviconService.getFaviconDataAsDataURL(
    36                   FaviconService.getFaviconForPage(pageURI));
    37   }
    38   catch(e) {
    39     if (!DEF_FAVICON) {
    40       DEF_FAVICON = PNG_B64 +
    41                     base64.encode(getChromeURIContent(DEF_FAVICON_URI));
    42     }
    43     return DEF_FAVICON;
    44   }
    45 }
    46 exports.getFaviconURIForLocation = getFaviconURIForLocation;
    48 /**
    49  * Takes chrome URI and returns content under that URI.
    50  * @param {String} chromeURI
    51  * @returns {String}
    52  */
    53 function getChromeURIContent(chromeURI) {
    54   let channel = IOService.newChannel(chromeURI, null, null);
    55   let input = channel.open();
    56   let stream = Cc["@mozilla.org/binaryinputstream;1"].
    57                 createInstance(Ci.nsIBinaryInputStream);
    58   stream.setInputStream(input);
    59   let content = stream.readBytes(input.available());
    60   stream.close();
    61   input.close();
    62   return content;
    63 }
    64 exports.getChromeURIContent = deprecateFunction(getChromeURIContent,
    65   'getChromeURIContent is deprecated, ' +
    66   'please use require("sdk/net/url").readURI instead.'
    67 );
    69 /**
    70  * Creates a base-64 encoded ASCII string from a string of binary data.
    71  */
    72 exports.base64Encode = deprecateFunction(base64.encode,
    73   'base64Encode is deprecated, ' +
    74   'please use require("sdk/base64").encode instead.'
    75 );
    76 /**
    77  * Decodes a string of data which has been encoded using base-64 encoding.
    78  */
    79 exports.base64Decode = deprecateFunction(base64.decode,
    80   'base64Dencode is deprecated, ' +
    81   'please use require("sdk/base64").decode instead.'
    82 );

mercurial