this fixes #990 + code review for #978

This commit is contained in:
gorhill 2015-03-11 11:05:13 -04:00
parent 415846e326
commit 145e5da7ce
3 changed files with 104 additions and 71 deletions

View File

@ -1144,17 +1144,17 @@ var exports = {
/******************************************************************************/ /******************************************************************************/
var onAssetUpdated = function(details) { var onOneUpdated = function(details) {
// Resource fetched, we can safely restart the daemon. // Resource fetched, we can safely restart the daemon.
scheduleUpdateDaemon(); scheduleUpdateDaemon();
var path = details.path; var path = details.path;
if ( details.error ) { if ( details.error ) {
//console.debug('µBlock.assetUpdater/onAssetUpdated: "%s" failed', path); //console.debug('µBlock.assetUpdater/onOneUpdated: "%s" failed', path);
return; return;
} }
//console.debug('µBlock.assetUpdater/onAssetUpdated: "%s"', path); //console.debug('µBlock.assetUpdater/onOneUpdated: "%s"', path);
updated[path] = true; updated[path] = true;
updatedCount += 1; updatedCount += 1;
@ -1168,6 +1168,9 @@ var onAssetUpdated = function(details) {
/******************************************************************************/ /******************************************************************************/
var updateOne = function() { var updateOne = function() {
// Because this can be called from outside the daemon's main loop
µb.assets.autoUpdate = µb.userSettings.autoUpdate || exports.manualUpdate;
var metaEntry; var metaEntry;
var updatingCount = 0; var updatingCount = 0;
var updatingText = null; var updatingText = null;
@ -1196,7 +1199,7 @@ var updateOne = function() {
suspendUpdateDaemon(); suspendUpdateDaemon();
//console.debug('µBlock.assetUpdater/updateOne: assets.get("%s")', path); //console.debug('µBlock.assetUpdater/updateOne: assets.get("%s")', path);
µb.assets.get(path, onAssetUpdated); µb.assets.get(path, onOneUpdated);
updatingCount = 1; updatingCount = 1;
updatingText = metaEntry.homeURL || path; updatingText = metaEntry.homeURL || path;
break; break;
@ -1211,9 +1214,58 @@ var updateOne = function() {
/******************************************************************************/ /******************************************************************************/
var onMetadataReady = function(response) { // Update one asset, fetch metadata if not done yet.
metadata = response;
updateOne(); var safeUpdateOne = function() {
if ( metadata !== null ) {
updateOne();
return;
}
// Because this can be called from outside the daemon's main loop
µb.assets.autoUpdate = µb.userSettings.autoUpdate || exports.manualUpdate;
var onMetadataReady = function(response) {
scheduleUpdateDaemon();
metadata = response;
updateOne();
};
suspendUpdateDaemon();
µb.assets.metadata(onMetadataReady);
};
/******************************************************************************/
var safeStartListener = function(callback) {
// Because this can be called from outside the daemon's main loop
µb.assets.autoUpdate = µb.userSettings.autoUpdate || exports.manualUpdate;
var onStartListenerDone = function(assets) {
scheduleUpdateDaemon();
assets = assets || {};
for ( var path in assets ) {
if ( assets.hasOwnProperty(path) === false ) {
continue;
}
if ( toUpdate.hasOwnProperty(path) ) {
continue;
}
//console.debug('assets.js > µBlock.assetUpdater/safeStartListener: "%s"', path);
toUpdate[path] = true;
toUpdateCount += 1;
}
if ( typeof callback === 'function' ) {
callback();
}
};
if ( typeof onStartListener === 'function' ) {
suspendUpdateDaemon();
onStartListener(onStartListenerDone);
} else {
onStartListenerDone(null);
}
}; };
/******************************************************************************/ /******************************************************************************/
@ -1233,20 +1285,14 @@ var updateDaemon = function() {
if ( Date.now() >= updateCycleTime ) { if ( Date.now() >= updateCycleTime ) {
//console.debug('µBlock.assetUpdater/updateDaemon: update cycle started'); //console.debug('µBlock.assetUpdater/updateDaemon: update cycle started');
reset(); reset();
if ( typeof onStartListener === 'function' ) { safeStartListener();
onStartListener();
}
} }
return; return;
} }
// Any asset to update? // Any asset to update?
if ( toUpdateCount !== 0 ) { if ( toUpdateCount !== 0 ) {
if ( metadata === null ) { safeUpdateOne();
µb.assets.metadata(onMetadataReady);
} else {
updateOne();
}
return; return;
} }
// Nothing left to update // Nothing left to update
@ -1307,37 +1353,6 @@ var reset = function() {
/******************************************************************************/ /******************************************************************************/
// Manual update: just a matter of forcing the update daemon to work on a
// tighter schedule.
exports.force = function() {
if ( exports.manualUpdate ) {
return;
}
suspendUpdateDaemon();
reset();
if ( typeof onStartListener === 'function' ) {
onStartListener();
}
// This must be done here
exports.manualUpdate = true;
if ( toUpdateCount === 0 ) {
updateCycleTime = Date.now() + updateCycleNextPeriod;
scheduleUpdateDaemon();
manualUpdateNotify(true, 1);
return;
}
scheduleUpdateDaemon();
manualUpdateNotify(false, 0);
};
/******************************************************************************/
var manualUpdateNotify = function(done, value, text) { var manualUpdateNotify = function(done, value, text) {
if ( exports.manualUpdate === false ) { if ( exports.manualUpdate === false ) {
return; return;
@ -1365,6 +1380,33 @@ var manualUpdateNotify = function(done, value, text) {
/******************************************************************************/ /******************************************************************************/
// Manual update: just a matter of forcing the update daemon to work on a
// tighter schedule.
exports.force = function() {
if ( exports.manualUpdate ) {
return;
}
reset();
exports.manualUpdate = true;
var onStartListenerDone = function() {
if ( toUpdateCount === 0 ) {
updateCycleTime = Date.now() + updateCycleNextPeriod;
manualUpdateNotify(true, 1);
} else {
manualUpdateNotify(false, 0);
safeUpdateOne();
}
};
safeStartListener(onStartListenerDone);
};
/******************************************************************************/
exports.onStart = { exports.onStart = {
addEventListener: function(callback) { addEventListener: function(callback) {
onStartListener = callback || null; onStartListener = callback || null;
@ -1392,19 +1434,6 @@ exports.onCompleted = {
/******************************************************************************/ /******************************************************************************/
exports.add = function(path) {
if ( toUpdate.hasOwnProperty(path) ) {
return;
}
//console.debug('assets.js > µBlock.assetUpdater.add("%s")', path);
toUpdate[path] = true;
toUpdateCount += 1;
};
/******************************************************************************/
// Typically called when an update has been forced. // Typically called when an update has been forced.
exports.restart = function() { exports.restart = function() {

View File

@ -1021,7 +1021,7 @@ var backupUserData = function(callback) {
var restoreUserData = function(request) { var restoreUserData = function(request) {
var userData = request.userData; var userData = request.userData;
var countdown = 6; var countdown = 5;
var onCountdown = function() { var onCountdown = function() {
countdown -= 1; countdown -= 1;
if ( countdown === 0 ) { if ( countdown === 0 ) {
@ -1032,7 +1032,7 @@ var restoreUserData = function(request) {
var onAllRemoved = function() { var onAllRemoved = function() {
// Be sure to adjust `countdown` if adding/removing anything below // Be sure to adjust `countdown` if adding/removing anything below
µb.XAL.keyvalSetOne('version', userData.version); µb.XAL.keyvalSetOne('version', userData.version);
µBlock.saveLocalSettings(onCountdown); µBlock.saveLocalSettings(true);
µb.XAL.keyvalSetMany(userData.userSettings, onCountdown); µb.XAL.keyvalSetMany(userData.userSettings, onCountdown);
µb.XAL.keyvalSetOne('remoteBlacklists', userData.filterLists, onCountdown); µb.XAL.keyvalSetOne('remoteBlacklists', userData.filterLists, onCountdown);
µb.XAL.keyvalSetOne('netWhitelist', userData.netWhitelist, onCountdown); µb.XAL.keyvalSetOne('netWhitelist', userData.netWhitelist, onCountdown);
@ -1055,8 +1055,10 @@ var restoreUserData = function(request) {
var resetUserData = function() { var resetUserData = function() {
µb.XAL.keyvalRemoveAll(); µb.XAL.keyvalRemoveAll();
// Keep global counts, people can become quite attached to numbers // Keep global counts, people can become quite attached to numbers
µb.saveLocalSettings(); µb.saveLocalSettings(true);
vAPI.app.restart(); vAPI.app.restart();
}; };

View File

@ -38,16 +38,15 @@
/******************************************************************************/ /******************************************************************************/
µBlock.saveLocalSettings = function(callback) { µBlock.saveLocalSettings = function(force) {
if ( typeof callback !== 'function' ) { if ( force ) {
callback = this.noopFunc; this.localSettingsModifyTime = Date.now();
} }
if ( this.localSettingsModifyTime <= this.localSettingsSaveTime ) { if ( this.localSettingsModifyTime <= this.localSettingsSaveTime ) {
callback();
return; return;
} }
this.localSettingsSaveTime = Date.now(); this.localSettingsSaveTime = Date.now();
vAPI.storage.set(this.localSettings, callback); vAPI.storage.set(this.localSettings);
}; };
/******************************************************************************/ /******************************************************************************/
@ -638,9 +637,10 @@
/******************************************************************************/ /******************************************************************************/
µBlock.updateStartHandler = function() { µBlock.updateStartHandler = function(callback) {
var µb = this; var µb = this;
var onListsReady = function(lists) { var onListsReady = function(lists) {
var assets = {};
for ( var location in lists ) { for ( var location in lists ) {
if ( lists.hasOwnProperty(location) === false ) { if ( lists.hasOwnProperty(location) === false ) {
continue; continue;
@ -648,12 +648,14 @@
if ( lists[location].off ) { if ( lists[location].off ) {
continue; continue;
} }
µb.assetUpdater.add(location); assets[location] = true;
} }
assets[µb.pslPath] = true;
assets['assets/ublock/mirror-candidates.txt'] = true;
callback(assets);
}; };
this.getAvailableLists(onListsReady); this.getAvailableLists(onListsReady);
this.assetUpdater.add(this.pslPath);
this.assetUpdater.add('assets/ublock/mirror-candidates.txt');
}; };
/******************************************************************************/ /******************************************************************************/