tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

FaviconUtils.sys.mjs (1678B)


      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 
      5 const TYPE_ICO = "image/x-icon";
      6 const TYPE_SVG = "image/svg+xml";
      7 
      8 export { TYPE_ICO, TYPE_SVG };
      9 
     10 // SVG images are send as raw data URLs from the content process to the parent.
     11 // The raw data: URL should NOT be used directly when displaying, but instead wrapped with a moz-remote-image: for safe re-encoding!
     12 export const SVG_DATA_URI_PREFIX = `data:${TYPE_SVG};base64,`;
     13 
     14 // URL schemes that we don't want to load and convert to data URLs.
     15 export const TRUSTED_FAVICON_SCHEMES = Object.freeze([
     16  "chrome",
     17  "about",
     18  "resource",
     19 ]);
     20 
     21 // Creates a moz-remote-image: URL wrapping the specified URL.
     22 function getMozRemoteImageURL(imageUrl, size) {
     23  let params = new URLSearchParams({
     24    url: imageUrl,
     25    width: size,
     26    height: size,
     27  });
     28  return "moz-remote-image://?" + params;
     29 }
     30 
     31 export { getMozRemoteImageURL };
     32 
     33 /**
     34 * Converts a Blob into a data: URL.
     35 *
     36 * @param {Blob} blob The Blob to be converted.
     37 * @returns {Promise<string>} Returns a promise that resolves with the data: URL
     38 *                            or rejects with an error.
     39 */
     40 export function blobAsDataURL(blob) {
     41  return new Promise((resolve, reject) => {
     42    let reader = new FileReader();
     43    reader.addEventListener("load", () => resolve(reader.result));
     44    reader.addEventListener("error", reject);
     45    reader.readAsDataURL(blob);
     46  });
     47 }
     48 
     49 // Shim for tabbrowser.js that uses `defineESModuleGetters`.
     50 export let FaviconUtils = {
     51  SVG_DATA_URI_PREFIX,
     52  getMozRemoteImageURL,
     53 };