2019-07-10 15:03:37 +03:00
|
|
|
/* 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/. */
|
|
|
|
|
2017-06-05 17:04:06 +03:00
|
|
|
const THUMBNAIL_DIRECTORY = "thumbnails";
|
|
|
|
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm", this);
|
|
|
|
ChromeUtils.import("resource://gre/modules/osfile.jsm", this);
|
2017-06-05 17:04:06 +03:00
|
|
|
|
|
|
|
XPCOMUtils.defineLazyGetter(this, "gCryptoHash", function() {
|
|
|
|
return Cc["@mozilla.org/security/hash;1"].createInstance(Ci.nsICryptoHash);
|
|
|
|
});
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyGetter(this, "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}"),
|
2018-04-23 06:55:06 +03:00
|
|
|
QueryInterface: ChromeUtils.generateQI([Ci.nsIPageThumbsStorageService]),
|
2017-06-05 17:04:06 +03:00
|
|
|
// The path for the storage
|
|
|
|
_path: null,
|
|
|
|
get path() {
|
|
|
|
if (!this._path) {
|
|
|
|
this._path = OS.Path.join(
|
|
|
|
OS.Constants.Path.localProfileDir,
|
|
|
|
THUMBNAIL_DIRECTORY
|
|
|
|
);
|
2019-07-05 12:14:05 +03:00
|
|
|
}
|
2017-06-05 17:04:06 +03:00
|
|
|
return this._path;
|
|
|
|
},
|
|
|
|
|
|
|
|
getLeafNameForURL(aURL) {
|
|
|
|
if (typeof aURL != "string") {
|
|
|
|
throw new TypeError("Expecting a string");
|
2019-07-05 12:14:05 +03:00
|
|
|
}
|
2017-06-05 17:04:06 +03:00
|
|
|
let hash = this._calculateMD5Hash(aURL);
|
|
|
|
return hash + ".png";
|
2019-07-05 12:14:05 +03:00
|
|
|
},
|
|
|
|
|
2017-06-05 17:04:06 +03:00
|
|
|
getFilePathForURL(aURL) {
|
|
|
|
return OS.Path.join(this.path, this.getLeafNameForURL(aURL));
|
2019-07-05 12:14:05 +03:00
|
|
|
},
|
|
|
|
|
2017-06-05 17:04:06 +03:00
|
|
|
_calculateMD5Hash(aValue) {
|
|
|
|
let hash = gCryptoHash;
|
|
|
|
let value = gUnicodeConverter.convertToByteArray(aValue);
|
2019-07-05 12:14:05 +03:00
|
|
|
|
2017-06-05 17:04:06 +03:00
|
|
|
hash.init(hash.MD5);
|
|
|
|
hash.update(value, value.length);
|
|
|
|
return this._convertToHexString(hash.finish(false));
|
2019-07-05 12:14:05 +03:00
|
|
|
},
|
|
|
|
|
2017-06-05 17:04:06 +03:00
|
|
|
_convertToHexString(aData) {
|
|
|
|
let hex = "";
|
|
|
|
for (let i = 0; i < aData.length; i++) {
|
|
|
|
hex += ("0" + aData.charCodeAt(i).toString(16)).slice(-2);
|
2019-07-05 12:14:05 +03:00
|
|
|
}
|
2017-06-05 17:04:06 +03:00
|
|
|
return hex;
|
2019-07-05 12:14:05 +03:00
|
|
|
},
|
2017-06-05 17:04:06 +03:00
|
|
|
};
|
|
|
|
|
2019-01-30 08:23:45 +03:00
|
|
|
var EXPORTED_SYMBOLS = ["PageThumbsStorageService"];
|