diff options
Diffstat (limited to 'src/js/assets.js')
-rw-r--r-- | src/js/assets.js | 80 |
1 files changed, 68 insertions, 12 deletions
diff --git a/src/js/assets.js b/src/js/assets.js index 8549cd8..08616c5 100644 --- a/src/js/assets.js +++ b/src/js/assets.js @@ -61,6 +61,7 @@ var lastRepoMetaTimestamp = 0; var lastRepoMetaIsRemote = false; var refreshRepoMetaPeriod = 5 * oneHour; var errorCantConnectTo = vAPI.i18n('errorCantConnectTo'); +var xhrTimeout = vAPI.localStorage.getItem('xhrTimeout') || 30000; var exports = { autoUpdate: true, @@ -275,8 +276,14 @@ var cachedAssetsManager = (function() { exports.remove(/./); }; + exports.exists = function(path) { + return entries !== null && entries.hasOwnProperty(path); + }; + exports.onRemovedListener = null; + getEntries(function(){}); + return exports; })(); @@ -285,6 +292,10 @@ var cachedAssetsManager = (function() { var getTextFileFromURL = function(url, onLoad, onError) { // console.log('µBlock.assets/getTextFileFromURL("%s"):', url); + if ( typeof onError !== 'function' ) { + onError = onLoad; + } + // https://github.com/gorhill/uMatrix/issues/15 var onResponseReceived = function() { this.onload = this.onerror = this.ontimeout = null; @@ -318,7 +329,7 @@ var getTextFileFromURL = function(url, onLoad, onError) { var xhr = new XMLHttpRequest(); try { xhr.open('get', url, true); - xhr.timeout = 30000; + xhr.timeout = xhrTimeout; xhr.onload = onResponseReceived; xhr.onerror = onErrorReceived; xhr.ontimeout = onErrorReceived; @@ -376,11 +387,16 @@ var getRepoMetadata = function(callback) { lastRepoMetaTimestamp = Date.now(); lastRepoMetaIsRemote = exports.remoteFetchBarrier === 0; + var defaultChecksums; var localChecksums; var repoChecksums; var checksumsReceived = function() { - if ( localChecksums === undefined || repoChecksums === undefined ) { + if ( + defaultChecksums === undefined || + localChecksums === undefined || + repoChecksums === undefined + ) { return; } // Remove from cache assets which no longer exist in the repo @@ -392,6 +408,17 @@ var getRepoMetadata = function(callback) { continue; } 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 ( + stringIsNotEmpty(defaultChecksums[path]) && + entry.localChecksum !== defaultChecksums[path] && + cachedAssetsManager.exists(path) === false + ) { + entry.localChecksum = defaultChecksums[path]; + checksumsChanged = true; + } // If repo checksums could not be fetched, assume no change. // https://github.com/gorhill/uBlock/issues/602 // Added: if repo checksum is that of the empty string, @@ -450,41 +477,64 @@ var getRepoMetadata = function(callback) { return out.join('\n'); }; - var parseChecksums = function(text, which) { - var entries = repoMetadata.entries; + var parseChecksums = function(text, eachFn) { var lines = text.split(/\n+/); var i = lines.length; - var fields, assetPath; + var fields; while ( i-- ) { fields = lines[i].trim().split(/\s+/); if ( fields.length !== 2 ) { continue; } - assetPath = fields[1]; - if ( entries[assetPath] === undefined ) { - entries[assetPath] = new AssetEntry(); - } - entries[assetPath][which + 'Checksum'] = fields[0]; + eachFn(fields[1], fields[0]); } }; 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)) ) { - parseChecksums(localChecksums, 'local'); + parseChecksums(localChecksums, processChecksum); } checksumsReceived(); }; var onRepoChecksumsLoaded = function(details) { + var entries = repoMetadata.entries; + var processChecksum = function(path, checksum) { + if ( entries.hasOwnProperty(path) === false ) { + entries[path] = new AssetEntry(); + } + entries[path].repoChecksum = checksum; + }; if ( (repoChecksums = validateChecksums(details)) ) { - parseChecksums(repoChecksums, 'repo'); + 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(); + }; + repoMetadata = new RepoMetadata(); repoMetadata.waiting.push(callback); readRepoFile('assets/checksums.txt', onRepoChecksumsLoaded); + getTextFileFromURL(vAPI.getURL('assets/checksums.txt'), onDefaultChecksumsLoaded); readLocalFile('assets/checksums.txt', onLocalChecksumsLoaded); }; @@ -1166,8 +1216,14 @@ exports.purge = function(pattern, before) { cachedAssetsManager.remove(pattern, before); }; +exports.purgeCacheableAsset = function(pattern, before) { + cachedAssetsManager.remove(pattern, before); + lastRepoMetaTimestamp = 0; +}; + exports.purgeAll = function(callback) { cachedAssetsManager.removeAll(callback); + lastRepoMetaTimestamp = 0; }; /******************************************************************************/ |