toolkit/components/thumbnails/PageThumbsStorageService.jsm
author Mozilla Releng Treescript <release+treescript@mozilla.org>
Sat, 25 Jun 2022 19:02:59 +0000
changeset 622090 c9b65d44600ac04887d0dcf6242c5d98fe7bd242
parent 619910 2bec86a7db24f8c3d66413a137592c113f05671c
permissions -rw-r--r--
no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD el -> 58cf40fbc7269bbf9fada1253ec8f011507a992f it -> 31a83f189f50d61cf1cf21a2215cd158c27cdc9c ro -> de1113d8613b112c14d8e75a3e1e0ce7b1a6236e ru -> ebb41fc34812cd2b60b9d71d4131620e5be1cbd3 sl -> f13ec224df213c09e026461144f137d231bd6fd0 tg -> 8584c83ca1fe45406ba32353ae7e76c98d026363 zh-CN -> 09b635a976c6192dccb652bb137867db8b16f722

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

const THUMBNAIL_DIRECTORY = "thumbnails";

const { XPCOMUtils } = ChromeUtils.import(
  "resource://gre/modules/XPCOMUtils.jsm"
);
const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm");

const lazy = {};

XPCOMUtils.defineLazyGetter(lazy, "gCryptoHash", function() {
  return Cc["@mozilla.org/security/hash;1"].createInstance(Ci.nsICryptoHash);
});

XPCOMUtils.defineLazyGetter(lazy, "gUnicodeConverter", function() {
  let converter = Cc[
    "@mozilla.org/intl/scriptableunicodeconverter"
  ].createInstance(Ci.nsIScriptableUnicodeConverter);
  converter.charset = "utf8";
  return converter;
});
function PageThumbsStorageService() {}

PageThumbsStorageService.prototype = {
  classID: Components.ID("{97943eec-0e48-49ef-b7b7-cf4aa0109bb6}"),
  QueryInterface: ChromeUtils.generateQI(["nsIPageThumbsStorageService"]),
  // The path for the storage
  _path: null,
  get path() {
    if (!this._path) {
      this._path = OS.Path.join(
        OS.Constants.Path.localProfileDir,
        THUMBNAIL_DIRECTORY
      );
    }
    return this._path;
  },

  getLeafNameForURL(aURL) {
    if (typeof aURL != "string") {
      throw new TypeError("Expecting a string");
    }
    let hash = this._calculateMD5Hash(aURL);
    return hash + ".png";
  },

  getFilePathForURL(aURL) {
    return OS.Path.join(this.path, this.getLeafNameForURL(aURL));
  },

  _calculateMD5Hash(aValue) {
    let hash = lazy.gCryptoHash;
    let value = lazy.gUnicodeConverter.convertToByteArray(aValue);

    hash.init(hash.MD5);
    hash.update(value, value.length);
    return this._convertToHexString(hash.finish(false));
  },

  _convertToHexString(aData) {
    let hex = "";
    for (let i = 0; i < aData.length; i++) {
      hex += ("0" + aData.charCodeAt(i).toString(16)).slice(-2);
    }
    return hex;
  },
};

var EXPORTED_SYMBOLS = ["PageThumbsStorageService"];