This commit is contained in:
gorhill 2015-10-14 10:28:37 -04:00
parent 8106604a2a
commit 4fcdac821d
1 changed files with 64 additions and 11 deletions

View File

@ -276,8 +276,14 @@ var cachedAssetsManager = (function() {
exports.remove(/./); exports.remove(/./);
}; };
exports.exists = function(path) {
return entries !== null && entries.hasOwnProperty(path);
};
exports.onRemovedListener = null; exports.onRemovedListener = null;
getEntries(function(){});
return exports; return exports;
})(); })();
@ -286,6 +292,10 @@ var cachedAssetsManager = (function() {
var getTextFileFromURL = function(url, onLoad, onError) { var getTextFileFromURL = function(url, onLoad, onError) {
// console.log('µBlock.assets/getTextFileFromURL("%s"):', url); // console.log('µBlock.assets/getTextFileFromURL("%s"):', url);
if ( typeof onError !== 'function' ) {
onError = onLoad;
}
// https://github.com/gorhill/uMatrix/issues/15 // https://github.com/gorhill/uMatrix/issues/15
var onResponseReceived = function() { var onResponseReceived = function() {
this.onload = this.onerror = this.ontimeout = null; this.onload = this.onerror = this.ontimeout = null;
@ -377,11 +387,16 @@ var getRepoMetadata = function(callback) {
lastRepoMetaTimestamp = Date.now(); lastRepoMetaTimestamp = Date.now();
lastRepoMetaIsRemote = exports.remoteFetchBarrier === 0; lastRepoMetaIsRemote = exports.remoteFetchBarrier === 0;
var defaultChecksums;
var localChecksums; var localChecksums;
var repoChecksums; var repoChecksums;
var checksumsReceived = function() { var checksumsReceived = function() {
if ( localChecksums === undefined || repoChecksums === undefined ) { if (
defaultChecksums === undefined ||
localChecksums === undefined ||
repoChecksums === undefined
) {
return; return;
} }
// Remove from cache assets which no longer exist in the repo // Remove from cache assets which no longer exist in the repo
@ -393,6 +408,15 @@ var getRepoMetadata = function(callback) {
continue; continue;
} }
entry = entries[path]; entry = entries[path];
// https://github.com/gorhill/uBlock/issues/760
// If the resource does not have a cached instance, we must reset
// the checksum to its value at install time.
if (
cachedAssetsManager.exists(path) === false &&
stringIsNotEmpty(defaultChecksums[path])
) {
entry.localChecksum = defaultChecksums[path];
}
// If repo checksums could not be fetched, assume no change. // If repo checksums could not be fetched, assume no change.
// https://github.com/gorhill/uBlock/issues/602 // https://github.com/gorhill/uBlock/issues/602
// Added: if repo checksum is that of the empty string, // Added: if repo checksum is that of the empty string,
@ -451,41 +475,64 @@ var getRepoMetadata = function(callback) {
return out.join('\n'); return out.join('\n');
}; };
var parseChecksums = function(text, which) { var parseChecksums = function(text, eachFn) {
var entries = repoMetadata.entries;
var lines = text.split(/\n+/); var lines = text.split(/\n+/);
var i = lines.length; var i = lines.length;
var fields, assetPath; var fields;
while ( i-- ) { while ( i-- ) {
fields = lines[i].trim().split(/\s+/); fields = lines[i].trim().split(/\s+/);
if ( fields.length !== 2 ) { if ( fields.length !== 2 ) {
continue; continue;
} }
assetPath = fields[1]; eachFn(fields[1], fields[0]);
if ( entries[assetPath] === undefined ) {
entries[assetPath] = new AssetEntry();
}
entries[assetPath][which + 'Checksum'] = fields[0];
} }
}; };
var onLocalChecksumsLoaded = function(details) { var onLocalChecksumsLoaded = function(details) {
var entries = repoMetadata.entries;
var processChecksum = function(path, checksum) {
if ( entries.hasOwnProperty(path) === false ) {
entries[path] = new AssetEntry();
}
entries[path].localChecksum = checksum;
};
if ( (localChecksums = validateChecksums(details)) ) { if ( (localChecksums = validateChecksums(details)) ) {
parseChecksums(localChecksums, 'local'); parseChecksums(localChecksums, processChecksum);
} }
checksumsReceived(); checksumsReceived();
}; };
var onRepoChecksumsLoaded = function(details) { var onRepoChecksumsLoaded = function(details) {
if ( (repoChecksums = validateChecksums(details)) ) { var entries = repoMetadata.entries;
parseChecksums(repoChecksums, 'repo'); var processChecksum = function(path, checksum) {
if ( entries.hasOwnProperty(path) === false ) {
entries[path] = new AssetEntry();
} }
entries[path].repoChecksum = checksum;
};
if ( (repoChecksums = validateChecksums(details)) ) {
parseChecksums(repoChecksums, processChecksum);
}
checksumsReceived();
};
// https://github.com/gorhill/uBlock/issues/760
// We need the checksum values at install time, because some resources
// may have been purged, in which case the checksum must be reset to the
// value at install time.
var onDefaultChecksumsLoaded = function() {
defaultChecksums = Object.create(null);
var processChecksum = function(path, checksum) {
defaultChecksums[path] = checksum;
};
parseChecksums(this.responseText || '', processChecksum);
checksumsReceived(); checksumsReceived();
}; };
repoMetadata = new RepoMetadata(); repoMetadata = new RepoMetadata();
repoMetadata.waiting.push(callback); repoMetadata.waiting.push(callback);
readRepoFile('assets/checksums.txt', onRepoChecksumsLoaded); readRepoFile('assets/checksums.txt', onRepoChecksumsLoaded);
getTextFileFromURL(vAPI.getURL('assets/checksums.txt'), onDefaultChecksumsLoaded);
readLocalFile('assets/checksums.txt', onLocalChecksumsLoaded); readLocalFile('assets/checksums.txt', onLocalChecksumsLoaded);
}; };
@ -1164,10 +1211,16 @@ exports.metadata = function(callback) {
/******************************************************************************/ /******************************************************************************/
exports.purge = function(pattern, before) { exports.purge = function(pattern, before) {
// Purging means we should mark resources current metadata as obsolete.
lastRepoMetaTimestamp = 0;
cachedAssetsManager.remove(pattern, before); cachedAssetsManager.remove(pattern, before);
}; };
exports.purgeAll = function(callback) { exports.purgeAll = function(callback) {
// Purging means we should mark resources current metadata as obsolete.
lastRepoMetaTimestamp = 0;
cachedAssetsManager.removeAll(callback); cachedAssetsManager.removeAll(callback);
}; };