aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorgorhill <rhill@raymondhill.net>2014-08-20 19:39:49 -0400
committergorhill <rhill@raymondhill.net>2014-08-20 19:39:49 -0400
commit3f55e5ecc25367a946ccf9257dcaa71a513d00f4 (patch)
tree9c9c2b75d969d33ce581f3cb4cb05e909254238e
parente5573eb9857d6439607d41feedaac93fd4c1cc4e (diff)
downloaduBlock-3f55e5ecc25367a946ccf9257dcaa71a513d00f4.zip
uBlock-3f55e5ecc25367a946ccf9257dcaa71a513d00f4.tar.gz
uBlock-3f55e5ecc25367a946ccf9257dcaa71a513d00f4.tar.bz2
more code review after testing
-rw-r--r--js/assets.js153
-rw-r--r--js/background.js2
-rw-r--r--js/start.js48
-rw-r--r--js/storage.js5
4 files changed, 88 insertions, 120 deletions
diff --git a/js/assets.js b/js/assets.js
index ae43ab6..bf62d64 100644
--- a/js/assets.js
+++ b/js/assets.js
@@ -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,11 +495,9 @@ var readRepoCopyAsset = function(path, callback) {
};
var updateChecksum = function() {
- if ( assetEntry !== undefined ) {
- if ( assetEntry.repoChecksum !== assetEntry.localChecksum ) {
- assetEntry.localChecksum = assetEntry.repoChecksum;
- updateLocalChecksums();
- }
+ if ( assetEntry !== undefined && assetEntry.repoChecksum !== assetEntry.localChecksum ) {
+ assetEntry.localChecksum = assetEntry.repoChecksum;
+ updateLocalChecksums();
}
};
@@ -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];
+ var obsolete = Date.now() - exports.autoUpdateDelay;
+ 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;
+ }
+
// 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);
- getTextFileFromURL(assetEntry.homeURL, onHomeFileLoaded, onHomeFileError);
- return;
- }
- }
- // Not obsolete
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 ) {
- console.log('µBlock> readRepoCopyAsset("%s") / onRepoMetaReady(): repo has newer version', path);
- getTextFileFromURL(assetEntry.homeURL, onHomeFileLoaded, onHomeFileError);
- return;
- }
+ 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 ) {
- console.log('µBlock> readRepoOnlyAsset("%s") / onRepoMetaReady(): repo has newer version', path);
- getTextFileFromURL(repositoryURL, onRepoFileLoaded, onRepoFileError);
- return;
- }
+ 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() {
- this.onload = this.onerror = null;
- console.error('µBlock> readExternalAsset("%s") / onExternalFileError2()', path);
- reportBack('', 'Error');
- };
-
- var onExternalFileError1 = function() {
+ var onExternalFileError = 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);
- return;
- }
- }
- // Not obsolete
- cachedAssetsManager.load(path, onCachedContentLoaded, onCachedContentError);
+ var obsolete = Date.now() - exports.autoUpdateDelay;
+ if ( typeof timestamp !== 'number' || (exports.autoUpdate && timestamp <= obsolete) ) {
+ getTextFileFromURL(path, onExternalFileLoaded, onExternalFileError);
return;
}
- // Not in cache
- getTextFileFromURL(path, onExternalFileLoaded, onExternalFileError2);
+
+ // In cache
+ cachedAssetsManager.load(path, onCachedContentLoaded, onCachedContentError);
};
- 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);
};
diff --git a/js/background.js b/js/background.js
index ff72b98..e1bc77f 100644
--- a/js/background.js
+++ b/js/background.js
@@ -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',
diff --git a/js/start.js b/js/start.js
index 8f140ab..3ed3d80 100644
--- a/js/start.js
+++ b/js/start.js
@@ -32,20 +32,42 @@
// Automatic update of non-user assets
// https://github.com/gorhill/httpswitchboard/issues/334
-(function() {
- var µb = µBlock;
-
- var jobCallback = function() {
- if ( µb.userSettings.autoUpdate !== true ) {
- return;
- }
- // TODO: need smarter update, currently blindly reloading all.
- µb.loadUpdatableAssets(true);
- };
-
- µb.asyncJobs.add('autoUpdateAssets', null, jobCallback, µb.updateAssetsEvery, true);
-})();
+µBlock.updater = (function() {
+
+/******************************************************************************/
+
+var µb = µBlock;
+var bufferTime = 0 * 60 * 1000;
+var exports = {};
+
+var jobCallback = function() {
+ if ( µb.userSettings.autoUpdate !== true ) {
+ return;
+ }
+ // TODO: need smarter update, currently blindly reloading all.
+ µb.loadUpdatableAssets(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;
+
+})();
+
+/******************************************************************************/
diff --git a/js/storage.js b/js/storage.js
index 083c6dc..dd2f6cf 100644
--- a/js/storage.js
+++ b/js/storage.js
@@ -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();
+ }
};
/******************************************************************************/