2014-09-30 13:56:56 -06:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
µBlock - a Chromium browser extension to block requests.
|
|
|
|
Copyright (C) 2014 Raymond Hill
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see {http://www.gnu.org/licenses/}.
|
|
|
|
|
|
|
|
Home: https://github.com/gorhill/uBlock
|
|
|
|
*/
|
|
|
|
|
2014-10-02 14:45:26 -06:00
|
|
|
/* jshint bitwise: false */
|
2014-09-30 13:56:56 -06:00
|
|
|
/* global chrome, YaMD5, µBlock */
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// Low-level asset files manager
|
|
|
|
|
|
|
|
µBlock.mirrors = (function() {
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-10-02 14:45:26 -06:00
|
|
|
// To show keys in local storage from console:
|
|
|
|
// chrome.storage.local.get(null, function (data) { console.log(Object.keys(data)) });
|
|
|
|
|
|
|
|
// To cleanup cached items from console:
|
|
|
|
// chrome.storage.local.get(null, function (data) { chrome.storage.local.remove(Object.keys(data).filter(function(a){ return a.indexOf('mirrors_item_') === 0; })); });
|
|
|
|
|
|
|
|
|
2014-09-30 13:56:56 -06:00
|
|
|
var exports = {
|
2014-10-02 14:45:26 -06:00
|
|
|
bytesInUseMax: 5 * 1024 * 1024,
|
|
|
|
ttl: 21 * 24 * 60 * 60 * 1000,
|
2014-09-30 13:56:56 -06:00
|
|
|
bytesInUse: 0,
|
2014-10-06 10:24:18 -06:00
|
|
|
tryCount: 0,
|
2014-09-30 13:56:56 -06:00
|
|
|
hitCount: 0
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var nullFunc = function() {};
|
|
|
|
|
2014-10-02 14:45:26 -06:00
|
|
|
// TODO: need to come up with something better. Key shoud be domain. More
|
|
|
|
// control over what significant part(s) of a URL is to be used as key.
|
2014-10-06 10:24:18 -06:00
|
|
|
var mirrorCandidates = Object.create(null);
|
2014-09-30 13:56:56 -06:00
|
|
|
|
2014-10-11 08:53:09 -06:00
|
|
|
var magicId = 'yawqboypxuhs';
|
2014-09-30 13:56:56 -06:00
|
|
|
var metadataPersistTimer = null;
|
2014-10-02 14:45:26 -06:00
|
|
|
var bytesInUseMercy = 1 * 1024 * 1024;
|
2014-09-30 13:56:56 -06:00
|
|
|
|
|
|
|
var metadata = {
|
|
|
|
magicId: magicId,
|
|
|
|
urlKeyToHashMap: {}
|
|
|
|
};
|
|
|
|
|
2014-10-02 14:45:26 -06:00
|
|
|
var hashToContentMap = {};
|
2014-09-30 13:56:56 -06:00
|
|
|
|
|
|
|
var loaded = false;
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-10-02 14:45:26 -06:00
|
|
|
// Ideally, URL keys and access time would be attached to the data URL entry
|
|
|
|
// itself, but then this would mean the need to persist the whole data URL
|
|
|
|
// every time a new URL key is added or the data URL is accessed, and given the
|
|
|
|
// data URL can be quite large, that would make no sense efficiency-wise to
|
|
|
|
// re-persist the whole thing.
|
|
|
|
// So, ContentEntry persisted once, MetadataEntry persisted often.
|
|
|
|
|
2014-09-30 13:56:56 -06:00
|
|
|
var MetadataEntry = function(hash) {
|
|
|
|
this.accessTime = Date.now();
|
|
|
|
this.hash = hash;
|
|
|
|
};
|
|
|
|
|
|
|
|
var ContentEntry = function(dataURL) {
|
|
|
|
this.createTime = Date.now();
|
|
|
|
this.dataURL = dataURL;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var getTextFileFromURL = function(url, onLoad, onError) {
|
|
|
|
if ( typeof onLoad !== 'function' ) {
|
|
|
|
onLoad = nullFunc;
|
|
|
|
}
|
|
|
|
if ( typeof onError !== 'function' ) {
|
|
|
|
onError = onLoad;
|
|
|
|
}
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.timeout = 10000;
|
|
|
|
xhr.onload = onLoad;
|
|
|
|
xhr.onerror = onError;
|
|
|
|
xhr.ontimeout = onError;
|
|
|
|
xhr.open('get', url, true);
|
2014-10-06 10:24:18 -06:00
|
|
|
xhr.responseType = 'arraybuffer';
|
2014-09-30 13:56:56 -06:00
|
|
|
xhr.send();
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-10-02 14:45:26 -06:00
|
|
|
// Safe binary-to-base64. Because window.btoa doesn't work for binary data...
|
|
|
|
//
|
|
|
|
// This implementation doesn't require the creation of a full-length
|
|
|
|
// intermediate buffer. I expect less short-term memory use will translate in
|
|
|
|
// more efficient conversion. Hopefully I will get time to confirm with
|
|
|
|
// benchmarks in the future.
|
|
|
|
|
|
|
|
var btoaMap = (function(){
|
|
|
|
var out = new Uint8Array(64);
|
|
|
|
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
|
|
var i = chars.length;
|
|
|
|
while ( i-- ) {
|
|
|
|
out[i] = chars.charCodeAt(i);
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
})();
|
|
|
|
|
|
|
|
var btoaSafe = function(input) {
|
|
|
|
var output = [];
|
|
|
|
var bamap = btoaMap;
|
|
|
|
var n = Math.floor(input.length / 3) * 3;
|
|
|
|
var b1, b2, b3;
|
|
|
|
for ( var ii = 0; ii < n; ii += 3 ) {
|
2014-10-06 10:24:18 -06:00
|
|
|
b1 = input[ii ];
|
|
|
|
b2 = input[ii+1];
|
|
|
|
b3 = input[ii+2];
|
2014-10-02 14:45:26 -06:00
|
|
|
output.push(String.fromCharCode(
|
|
|
|
bamap[ b1 >>> 2],
|
|
|
|
bamap[(b1 & 0x03) << 4 | b2 >>> 4],
|
|
|
|
bamap[(b2 & 0x0F) << 2 | b3 >>> 6],
|
|
|
|
bamap[ b3 & 0x3F ]
|
|
|
|
));
|
|
|
|
}
|
|
|
|
// Leftover
|
|
|
|
var m = input.length - n;
|
|
|
|
if ( m > 1 ) {
|
2014-10-06 10:24:18 -06:00
|
|
|
b1 = input[ii ];
|
|
|
|
b2 = input[ii+1];
|
2014-10-02 14:45:26 -06:00
|
|
|
output.push(String.fromCharCode(
|
|
|
|
bamap[ b1 >>> 2],
|
|
|
|
bamap[(b1 & 0x03) << 4 | b2 >>> 4],
|
2014-10-03 10:12:48 -06:00
|
|
|
bamap[(b2 & 0x0F) << 2 ],
|
2014-10-02 14:45:26 -06:00
|
|
|
0x3D
|
|
|
|
));
|
|
|
|
} else if ( m !== 0 ) {
|
2014-10-06 10:24:18 -06:00
|
|
|
b1 = input[ii ];
|
2014-10-02 14:45:26 -06:00
|
|
|
output.push(String.fromCharCode(
|
|
|
|
bamap[ b1 >>>2],
|
|
|
|
bamap[(b1 & 0x03) << 4 ],
|
|
|
|
0x3D,
|
|
|
|
0x3D
|
|
|
|
));
|
|
|
|
}
|
|
|
|
return output.join('');
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-09-30 13:56:56 -06:00
|
|
|
// Extract a `key` from a URL.
|
|
|
|
|
|
|
|
var toUrlKey = function(url) {
|
|
|
|
if ( url.slice(0, 4) !== 'http' ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
var pos = url.indexOf('://');
|
|
|
|
if ( pos === -1 ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
url = url.slice(pos + 3);
|
|
|
|
pos = url.indexOf('/');
|
|
|
|
if ( pos === -1 ) {
|
2014-10-02 14:45:26 -06:00
|
|
|
return '';
|
2014-09-30 13:56:56 -06:00
|
|
|
}
|
2014-10-06 10:24:18 -06:00
|
|
|
var regexes = mirrorCandidates[url.slice(0, pos)];
|
|
|
|
if ( regexes === undefined ) {
|
2014-09-30 13:56:56 -06:00
|
|
|
return '';
|
|
|
|
}
|
2014-10-06 10:24:18 -06:00
|
|
|
var i = regexes.length;
|
|
|
|
var matches;
|
|
|
|
while ( i-- ) {
|
|
|
|
matches = regexes[i].exec(url);
|
|
|
|
if ( matches === null ) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-10-11 08:53:09 -06:00
|
|
|
// https://github.com/gorhill/uBlock/issues/301
|
|
|
|
// Use whole URL as key when no regex capture
|
|
|
|
return matches.length === 1 ? url : matches[1];
|
2014-09-30 13:56:56 -06:00
|
|
|
}
|
2014-10-06 10:24:18 -06:00
|
|
|
return '';
|
2014-09-30 13:56:56 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// Ref: http://www.iana.org/assignments/media-types/media-types.xhtml
|
|
|
|
|
2014-10-06 10:24:18 -06:00
|
|
|
var extractMimeType = function(ctin) {
|
2014-09-30 13:56:56 -06:00
|
|
|
var pos = ctin.indexOf(';');
|
2014-10-06 10:24:18 -06:00
|
|
|
return pos === -1 ? ctin.trim() : ctin.slice(0, pos).trim();
|
2014-09-30 13:56:56 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var metadataExists = function(urlKey) {
|
2014-10-02 14:45:26 -06:00
|
|
|
return typeof urlKey === 'string' &&
|
|
|
|
metadata.urlKeyToHashMap.hasOwnProperty(urlKey);
|
2014-09-30 13:56:56 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var contentExists = function(hash) {
|
2014-10-02 14:45:26 -06:00
|
|
|
return typeof hash === 'string' &&
|
|
|
|
hashToContentMap.hasOwnProperty(hash);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var storageKeyFromHash = function(hash) {
|
|
|
|
return 'mirrors_item_' + hash;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// Given that a single data URL can be shared by many URL keys, pruning is a
|
|
|
|
// bit hairy. So the steps are:
|
|
|
|
// - Collate information about each data URL:
|
|
|
|
// - Last time they were used
|
|
|
|
// - Which URL keys reference them
|
|
|
|
// This will allow us to flush from memory the ones least recently used first.
|
|
|
|
|
|
|
|
var pruneToSize = function(toSize) {
|
|
|
|
if ( exports.bytesInUse < toSize ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var k2hMap = metadata.urlKeyToHashMap;
|
|
|
|
var h2cMap = hashToContentMap;
|
|
|
|
var urlKey, hash;
|
|
|
|
var mdEntry, ctEntry, prEntry;
|
|
|
|
var pruneMap = {};
|
|
|
|
for ( urlKey in k2hMap ) {
|
|
|
|
if ( k2hMap.hasOwnProperty(urlKey) === false ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
mdEntry = k2hMap[urlKey];
|
|
|
|
hash = mdEntry.hash;
|
|
|
|
if ( pruneMap.hasOwnProperty(hash) === false ) {
|
|
|
|
pruneMap[hash] = {
|
|
|
|
urlKeys: [urlKey],
|
|
|
|
accessTime: mdEntry.accessTime
|
|
|
|
};
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
prEntry = pruneMap[hash];
|
|
|
|
prEntry.urlKeys.push(urlKey);
|
|
|
|
prEntry.accessTime = Math.max(prEntry.accessTime, mdEntry.accessTime);
|
|
|
|
}
|
|
|
|
// Least recent at the end of array
|
|
|
|
var compare = function(a, b) {
|
|
|
|
return pruneMap[b].accessTime - pruneMap[a].accessTime;
|
|
|
|
};
|
|
|
|
var hashes = Object.keys(pruneMap).sort(compare);
|
|
|
|
var toRemove = [];
|
|
|
|
var i = hashes.length;
|
|
|
|
while ( i-- ) {
|
|
|
|
hash = hashes[i];
|
|
|
|
prEntry = pruneMap[hash];
|
|
|
|
ctEntry = h2cMap[hash];
|
|
|
|
delete h2cMap[hash];
|
|
|
|
toRemove.push(storageKeyFromHash(hash));
|
|
|
|
exports.bytesInUse -= ctEntry.dataURL.length;
|
|
|
|
while ( urlKey = prEntry.urlKeys.pop() ) {
|
|
|
|
delete k2hMap[urlKey];
|
|
|
|
}
|
|
|
|
if ( exports.bytesInUse < toSize ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( toRemove.length !== 0 ) {
|
|
|
|
//console.debug('mirrors.pruneToSize(%d): removing %o', toSize, toRemove);
|
|
|
|
removeContent(toRemove);
|
|
|
|
updateMetadataNow();
|
|
|
|
}
|
2014-09-30 13:56:56 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var updateMetadata = function() {
|
|
|
|
metadataPersistTimer = null;
|
|
|
|
chrome.storage.local.set({ 'mirrors_metadata': metadata });
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-10-02 14:45:26 -06:00
|
|
|
var updateMetadataNow = function() {
|
2014-09-30 13:56:56 -06:00
|
|
|
if ( metadataPersistTimer !== null ) {
|
|
|
|
clearTimeout(metadataPersistTimer);
|
|
|
|
}
|
|
|
|
updateMetadata();
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-10-02 14:45:26 -06:00
|
|
|
var updateMetadataAsync = function() {
|
|
|
|
if ( metadataPersistTimer === null ) {
|
|
|
|
setTimeout(updateMetadata, 60 * 1000);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var addMetadata = function(urlKey, hash) {
|
|
|
|
metadata.urlKeyToHashMap[urlKey] = new MetadataEntry(hash);
|
|
|
|
updateMetadataNow();
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var removeMetadata = function(urlKey) {
|
|
|
|
delete metadata.urlKeyToHashMap[urlKey];
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var addContent = function(hash, dataURL) {
|
|
|
|
if ( contentExists(hash) ) {
|
2014-09-30 13:56:56 -06:00
|
|
|
return;
|
|
|
|
}
|
2014-10-02 14:45:26 -06:00
|
|
|
var contentEntry = hashToContentMap[hash] = new ContentEntry(dataURL);
|
2014-09-30 13:56:56 -06:00
|
|
|
exports.bytesInUse += dataURL.length;
|
|
|
|
var bin = {};
|
2014-10-02 14:45:26 -06:00
|
|
|
bin[storageKeyFromHash(hash)] = contentEntry;
|
2014-09-30 13:56:56 -06:00
|
|
|
chrome.storage.local.set(bin);
|
2014-10-02 14:45:26 -06:00
|
|
|
if ( exports.bytesInUse >= exports.bytesInUseMax + bytesInUseMercy ) {
|
|
|
|
pruneToSize(exports.bytesInUseMax);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var removeContent = function(what) {
|
|
|
|
chrome.storage.local.remove(what);
|
2014-09-30 13:56:56 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var cacheAsset = function(url) {
|
|
|
|
var urlKey = toUrlKey(url);
|
|
|
|
if ( metadataExists(urlKey) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var onRemoteAssetLoaded = function() {
|
|
|
|
this.onload = this.onerror = null;
|
|
|
|
if ( this.status !== 200 ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-10-06 10:24:18 -06:00
|
|
|
var mimeType = extractMimeType(this.getResponseHeader('Content-Type'));
|
2014-10-11 08:53:09 -06:00
|
|
|
var uint8Buffer = new Uint8Array(this.response);
|
2014-09-30 13:56:56 -06:00
|
|
|
var yamd5 = new YaMD5();
|
2014-10-06 10:24:18 -06:00
|
|
|
yamd5.appendAsciiStr(mimeType);
|
2014-10-11 08:53:09 -06:00
|
|
|
yamd5.appendByteArray(uint8Buffer);
|
2014-09-30 13:56:56 -06:00
|
|
|
var hash = yamd5.end();
|
2014-10-02 14:45:26 -06:00
|
|
|
addMetadata(urlKey, hash);
|
2014-09-30 13:56:56 -06:00
|
|
|
if ( contentExists(hash) ) {
|
|
|
|
//console.debug('mirrors.cacheAsset(): reusing existing content for "%s"', urlKey);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//console.debug('mirrors.cacheAsset(): caching new content for "%s"', urlKey);
|
|
|
|
// Keep original encoding if there was one, otherwise use base64 --
|
|
|
|
// as the result is somewhat more compact I believe
|
2014-10-02 14:45:26 -06:00
|
|
|
var dataUrl = null;
|
|
|
|
try {
|
2014-10-11 08:53:09 -06:00
|
|
|
dataUrl = 'data:' + mimeType + ';base64,' + btoaSafe(uint8Buffer);
|
2014-10-02 14:45:26 -06:00
|
|
|
} catch (e) {
|
2014-10-06 10:24:18 -06:00
|
|
|
//console.debug('"%s":', url, e);
|
2014-10-02 14:45:26 -06:00
|
|
|
}
|
2014-10-06 10:24:18 -06:00
|
|
|
if ( dataUrl !== null ) {
|
|
|
|
addContent(hash, dataUrl);
|
2014-10-02 14:45:26 -06:00
|
|
|
}
|
2014-09-30 13:56:56 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
var onRemoteAssetError = function() {
|
|
|
|
this.onload = this.onerror = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
getTextFileFromURL(
|
|
|
|
url,
|
|
|
|
onRemoteAssetLoaded,
|
|
|
|
onRemoteAssetError
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var toURL = function(url, cache) {
|
2014-10-06 10:24:18 -06:00
|
|
|
exports.tryCount += 1;
|
2014-09-30 13:56:56 -06:00
|
|
|
var urlKey = toUrlKey(url);
|
|
|
|
if ( urlKey === '' ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
if ( metadataExists(urlKey) === false ) {
|
|
|
|
if ( cache === true ) {
|
|
|
|
cacheAsset(url);
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
2014-10-02 14:45:26 -06:00
|
|
|
var dataURL = '';
|
2014-09-30 13:56:56 -06:00
|
|
|
var metadataEntry = metadata.urlKeyToHashMap[urlKey];
|
2014-10-02 14:45:26 -06:00
|
|
|
if ( contentExists(metadataEntry.hash) ) {
|
|
|
|
dataURL = hashToContentMap[metadataEntry.hash].dataURL;
|
|
|
|
metadataEntry.accessTime = Date.now();
|
|
|
|
exports.hitCount += 1;
|
|
|
|
} else {
|
|
|
|
//console.debug('mirrors.toURL(): content not found "%s"', url);
|
|
|
|
delete metadata.urlKeyToHashMap[urlKey];
|
2014-09-30 13:56:56 -06:00
|
|
|
}
|
2014-10-02 14:45:26 -06:00
|
|
|
updateMetadataAsync();
|
|
|
|
return dataURL;
|
2014-09-30 13:56:56 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-10-06 10:24:18 -06:00
|
|
|
var parseMirrorCandidates = function(rawText) {
|
|
|
|
var rawTextEnd = rawText.length;
|
|
|
|
var lineBeg = 0, lineEnd;
|
|
|
|
var line;
|
|
|
|
var key = '', re;
|
|
|
|
while ( lineBeg < rawTextEnd ) {
|
|
|
|
lineEnd = rawText.indexOf('\n', lineBeg);
|
|
|
|
if ( lineEnd === -1 ) {
|
|
|
|
lineEnd = rawText.indexOf('\r', lineBeg);
|
|
|
|
if ( lineEnd === -1 ) {
|
|
|
|
lineEnd = rawTextEnd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
line = rawText.slice(lineBeg, lineEnd);
|
|
|
|
lineBeg = lineEnd + 1;
|
|
|
|
if ( line.charAt(0) === '#' ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( line.charAt(0) !== ' ' ) {
|
|
|
|
key = line.trim();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( key === '' ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
re = new RegExp(line.trim());
|
|
|
|
if ( mirrorCandidates[key] === undefined ) {
|
|
|
|
mirrorCandidates[key] = [];
|
|
|
|
}
|
|
|
|
mirrorCandidates[key].push(re);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-09-30 13:56:56 -06:00
|
|
|
var load = function() {
|
|
|
|
loaded = true;
|
|
|
|
|
2014-10-06 10:24:18 -06:00
|
|
|
var onMirrorCandidatesReady = function(details) {
|
|
|
|
if ( details.content !== '' ) {
|
|
|
|
parseMirrorCandidates(details.content);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-10-02 14:45:26 -06:00
|
|
|
var loadContent = function(urlKey, hash) {
|
|
|
|
var binKey = storageKeyFromHash(hash);
|
2014-09-30 13:56:56 -06:00
|
|
|
var onContentReady = function(bin) {
|
2014-10-02 14:45:26 -06:00
|
|
|
if ( chrome.runtime.lastError || bin.hasOwnProperty(binKey) === false ) {
|
|
|
|
//console.debug('mirrors.load(): failed to load content "%s"', binKey);
|
|
|
|
removeMetadata(urlKey);
|
|
|
|
removeContent(binKey);
|
2014-09-30 13:56:56 -06:00
|
|
|
return;
|
|
|
|
}
|
2014-10-02 14:45:26 -06:00
|
|
|
//console.debug('mirrors.load(): loaded content "%s"', binKey);
|
|
|
|
var ctEntry = hashToContentMap[hash] = bin[binKey];
|
|
|
|
exports.bytesInUse += ctEntry.dataURL.length;
|
2014-09-30 13:56:56 -06:00
|
|
|
};
|
2014-10-02 14:45:26 -06:00
|
|
|
chrome.storage.local.get(binKey, onContentReady);
|
2014-09-30 13:56:56 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
var onMetadataReady = function(bin) {
|
2014-10-02 14:45:26 -06:00
|
|
|
//console.debug('mirrors.load(): loaded metadata');
|
2014-10-11 08:53:09 -06:00
|
|
|
var u2hmap = metadata.urlKeyToHashMap = bin.mirrors_metadata.urlKeyToHashMap;
|
|
|
|
var mustReset = bin.mirrors_metadata.magicId !== magicId;
|
2014-10-02 14:45:26 -06:00
|
|
|
var toRemove = [];
|
|
|
|
var hash;
|
|
|
|
for ( var urlKey in u2hmap ) {
|
|
|
|
if ( u2hmap.hasOwnProperty(urlKey) === false ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
hash = u2hmap[urlKey].hash;
|
2014-10-06 10:24:18 -06:00
|
|
|
if ( mustReset ) {
|
2014-10-02 14:45:26 -06:00
|
|
|
toRemove.push(storageKeyFromHash(hash));
|
|
|
|
removeMetadata(urlKey);
|
2014-09-30 13:56:56 -06:00
|
|
|
continue;
|
|
|
|
}
|
2014-10-02 14:45:26 -06:00
|
|
|
loadContent(urlKey, hash);
|
|
|
|
}
|
|
|
|
if ( toRemove.length !== 0 ) {
|
|
|
|
removeContent(toRemove);
|
|
|
|
updateMetadataNow();
|
2014-09-30 13:56:56 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-10-02 14:45:26 -06:00
|
|
|
chrome.storage.local.get({ 'mirrors_metadata': metadata }, onMetadataReady);
|
2014-10-06 10:24:18 -06:00
|
|
|
µBlock.assets.get('assets/ublock/mirror-candidates.txt', onMirrorCandidatesReady);
|
2014-09-30 13:56:56 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var unload = function() {
|
2014-10-06 10:24:18 -06:00
|
|
|
pruneToSize(0);
|
2014-09-30 13:56:56 -06:00
|
|
|
metadata.urlKeyToHashMap = {};
|
2014-10-02 14:45:26 -06:00
|
|
|
hashToContentMap = {};
|
2014-09-30 13:56:56 -06:00
|
|
|
exports.bytesInUse = 0;
|
|
|
|
exports.hitCount = 0;
|
|
|
|
|
|
|
|
loaded = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
exports.toggle = function(on) {
|
|
|
|
if ( on && loaded !== true ) {
|
|
|
|
load();
|
|
|
|
} else if ( on !== true && loaded ) {
|
|
|
|
unload();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// Export API
|
|
|
|
|
|
|
|
exports.toURL = toURL;
|
2014-10-02 14:45:26 -06:00
|
|
|
exports.pruneToSize = pruneToSize;
|
2014-09-30 13:56:56 -06:00
|
|
|
|
|
|
|
return exports;
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|