data.js 2.6 KB

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