more code review after testing

This commit is contained in:
gorhill 2014-08-20 19:39:49 -04:00
parent e5573eb985
commit 3f55e5ecc2
4 changed files with 86 additions and 118 deletions

View File

@ -430,8 +430,6 @@ var readLocalFile = function(path, callback) {
cachedAssetsManager.load(path, onCachedContentLoaded, onCachedContentError);
};
exports.getLocal = readLocalFile;
// https://www.youtube.com/watch?v=r9KVpuFPtHc
/******************************************************************************/
@ -450,18 +448,18 @@ var readRepoFile = function(path, callback) {
var onRepoFileLoaded = function() {
this.onload = this.onerror = null;
// console.log('µBlock> readRepoFile() / onRepoFileLoaded()');
console.log('µBlock> readRepoFile("%s") / onRepoFileLoaded()', path);
// https://github.com/gorhill/httpswitchboard/issues/263
if ( this.status === 200 ) {
reportBack(this.responseText);
} else {
reportBack('', 'Error ' + this.statusText);
reportBack('', 'Error: ' + this.statusText);
}
};
var onRepoFileError = function() {
this.onload = this.onerror = null;
console.error('µBlock> readRepoFile() / onRepoFileError("%s")', path);
console.error('µBlock> readRepoFile("%s") / onRepoFileError()', path);
reportBack('', 'Error');
};
@ -475,51 +473,6 @@ var readRepoFile = function(path, callback) {
/******************************************************************************/
var readExternalFile = function(path, callback) {
var reportBack = function(content, err) {
var details = {
'path': path,
'content': content
};
if ( err ) {
details.error = err;
}
callback(details);
};
var onExternalFileCached = function(details) {
this.onload = this.onerror = null;
// console.log('µBlock> onExternalFileCached()');
reportBack(details.content);
};
var onExternalFileLoaded = function() {
this.onload = this.onerror = null;
// console.log('µBlock> onExternalFileLoaded()');
cachedAssetsManager.save(path, this.responseText, onExternalFileCached);
};
var onExternalFileError = function() {
console.error('µBlock> onExternalFileError() / onLocalFileError("%s")', path);
reportBack('', 'Error');
this.onload = this.onerror = null;
};
var onCachedContentLoaded = function(details) {
// console.log('µBlock> readExternalFile() / onCacheFileLoaded()');
reportBack(details.content);
};
var onCachedContentError = function(details) {
// console.error('µBlock> readExternalFile() / onCacheFileError("%s")', path);
getTextFileFromURL(details.path, onExternalFileLoaded, onExternalFileError);
};
cachedAssetsManager.load(path, onCachedContentLoaded, onCachedContentError);
};
/******************************************************************************/
// An asset from an external source with a copy shipped with the extension:
// Path --> starts with 'assets/(thirdparties|ublock)/', with a home URL
// External -->
@ -542,12 +495,10 @@ var readRepoCopyAsset = function(path, callback) {
};
var updateChecksum = function() {
if ( assetEntry !== undefined ) {
if ( assetEntry.repoChecksum !== assetEntry.localChecksum ) {
if ( assetEntry !== undefined && assetEntry.repoChecksum !== assetEntry.localChecksum ) {
assetEntry.localChecksum = assetEntry.repoChecksum;
updateLocalChecksums();
}
}
};
var onInstallFileLoaded = function() {
@ -577,7 +528,7 @@ var readRepoCopyAsset = function(path, callback) {
var onRepoFileLoaded = function() {
this.onload = this.onerror = null;
if ( typeof this.responseText !== 'string' || this.responseText === '' ) {
console.error('µBlock> readRepoCopyAsset("%s") / onRepoFileLoaded("%s"): no response', path, repositoryURL);
console.error('µBlock> readRepoCopyAsset("%s") / onRepoFileLoaded("%s"): error', path, repositoryURL);
cachedAssetsManager.load(path, onCachedContentLoaded, onCachedContentError);
return;
}
@ -621,22 +572,22 @@ var readRepoCopyAsset = function(path, callback) {
};
var onCacheMetaReady = function(entries) {
// Fetch from remote if:
// - Auto-update enabled AND (not in cache OR in cache but obsolete)
var timestamp = entries[path];
// In cache
if ( typeof timestamp === 'number' ) {
// Verify obsolescence
if ( exports.autoUpdate ) {
var obsolete = Date.now() - exports.autoUpdateDelay;
if ( timestamp <= obsolete ) {
console.log('µBlock> readRepoCopyAsset("%s") / onCacheMetaReady(): cache is obsolete', path);
if ( exports.autoUpdate && (typeof timestamp !== 'number' || timestamp <= obsolete) ) {
console.log('µBlock> readRepoCopyAsset("%s") / onCacheMetaReady(): not cached or obsolete', path);
getTextFileFromURL(assetEntry.homeURL, onHomeFileLoaded, onHomeFileError);
return;
}
}
// Not obsolete
// In cache
if ( typeof timestamp === 'number' ) {
cachedAssetsManager.load(path, onCachedContentLoaded, onCachedContentError);
return;
}
// Not in cache
getTextFileFromURL(chrome.runtime.getURL(path), onInstallFileLoaded, onInstallFileError);
};
@ -646,20 +597,18 @@ var readRepoCopyAsset = function(path, callback) {
// Asset doesn't exist
if ( assetEntry === undefined ) {
reportBack('', 'Error: Asset not found');
reportBack('', 'Error: asset not found');
return;
}
// Repo copy changed: fetch from home URL
if ( exports.autoUpdate ) {
if ( assetEntry.localChecksum !== assetEntry.repoChecksum ) {
if ( exports.autoUpdate && assetEntry.localChecksum !== assetEntry.repoChecksum ) {
console.log('µBlock> readRepoCopyAsset("%s") / onRepoMetaReady(): repo has newer version', path);
getTextFileFromURL(assetEntry.homeURL, onHomeFileLoaded, onHomeFileError);
return;
}
}
// No change, we will inspect the cached version for obsolescence
// Load from cache
cachedAssetsManager.entries(onCacheMetaReady);
};
@ -745,20 +694,18 @@ var readRepoOnlyAsset = function(path, callback) {
// Asset doesn't exist
if ( assetEntry === undefined ) {
reportBack('', 'Error: Asset not found');
reportBack('', 'Error: asset not found');
return;
}
// Asset added or changed: load from repo URL and then cache result
if ( exports.autoUpdate ) {
if ( assetEntry.localChecksum !== assetEntry.repoChecksum ) {
if ( exports.autoUpdate && assetEntry.localChecksum !== assetEntry.repoChecksum ) {
console.log('µBlock> readRepoOnlyAsset("%s") / onRepoMetaReady(): repo has newer version', path);
getTextFileFromURL(repositoryURL, onRepoFileLoaded, onRepoFileError);
return;
}
}
// Asset unchanged: load from cache
// Load from cache
cachedAssetsManager.load(path, onCachedContentLoaded, onCachedContentError);
};
@ -813,39 +760,29 @@ var readExternalAsset = function(path, callback) {
reportBack(this.responseText);
};
var onExternalFileError2 = function() {
var onExternalFileError = function() {
this.onload = this.onerror = null;
console.error('µBlock> readExternalAsset("%s") / onExternalFileError2()', path);
reportBack('', 'Error');
};
var onExternalFileError1 = function() {
this.onload = this.onerror = null;
console.error('µBlock> readExternalAsset("%s") / onExternalFileError1()', path);
console.error('µBlock> readExternalAsset("%s") / onExternalFileError()', path);
cachedAssetsManager.load(path, onCachedContentLoaded, onCachedContentError);
};
var entriesReady = function(entries) {
var onCacheMetaReady = function(entries) {
// Fetch from remote if:
// - Not in cache OR
//
// - Auto-update enabled AND in cache but obsolete
var timestamp = entries[path];
// In cache
if ( typeof timestamp === 'number' ) {
// Obsolete?
if ( exports.autoUpdate ) {
var obsolete = Date.now() - exports.autoUpdateDelay;
if ( timestamp <= obsolete ) {
getTextFileFromURL(path, onExternalFileLoaded, onExternalFileError1);
if ( typeof timestamp !== 'number' || (exports.autoUpdate && timestamp <= obsolete) ) {
getTextFileFromURL(path, onExternalFileLoaded, onExternalFileError);
return;
}
}
// Not obsolete
// In cache
cachedAssetsManager.load(path, onCachedContentLoaded, onCachedContentError);
return;
}
// Not in cache
getTextFileFromURL(path, onExternalFileLoaded, onExternalFileError2);
};
cachedAssetsManager.entries(entriesReady);
cachedAssetsManager.entries(onCacheMetaReady);
};
/******************************************************************************/
@ -943,6 +880,10 @@ exports.get = function(path, callback) {
/******************************************************************************/
exports.getLocal = readLocalFile;
/******************************************************************************/
exports.put = function(path, content, callback) {
cachedAssetsManager.save(path, content, callback);
};

View File

@ -46,7 +46,7 @@ return {
allowedRequestCount: 0
},
updateAssetsEvery: 2 * 24 * 60 * 60 * 1000,
updateAssetsEvery: 47 * 60 * 60 * 1000,
projectServerRoot: 'https://raw.githubusercontent.com/gorhill/uBlock/master/',
userFiltersPath: 'assets/user/filters.txt',

View File

@ -32,8 +32,13 @@
// Automatic update of non-user assets
// https://github.com/gorhill/httpswitchboard/issues/334
(function() {
µBlock.updater = (function() {
/******************************************************************************/
var µb = µBlock;
var bufferTime = 0 * 60 * 1000;
var exports = {};
var jobCallback = function() {
if ( µb.userSettings.autoUpdate !== true ) {
@ -43,9 +48,26 @@
µb.loadUpdatableAssets(true);
};
µb.asyncJobs.add('autoUpdateAssets', null, jobCallback, µb.updateAssetsEvery, true);
})();
// https://www.youtube.com/watch?v=cIrGQD84F1g
/******************************************************************************/
exports.restart = function() {
µb.asyncJobs.add(
'autoUpdateAssets',
null,
jobCallback,
µb.updateAssetsEvery - bufferTime,
true
);
};
exports.restart();
/******************************************************************************/
return exports;
})();
/******************************************************************************/

View File

@ -446,6 +446,11 @@
this.assets.autoUpdateDelay = this.updateAssetsEvery;
this.loadPublicSuffixList();
this.loadUbiquitousBlacklists();
// It could be a manual update, so we reset the auto-updater
if ( update ) {
this.updater.restart();
}
};
/******************************************************************************/