Skip to content

Commit

Permalink
Ensure the "Filter lists" pane is in sync with update status
Browse files Browse the repository at this point in the history
Related issue:
- #2394

Additionally, I added a new advanced setting to control
how long after launch an auto-update session should be
started -- value is in seconds:

    autoUpdateDelayAfterLaunch 180
  • Loading branch information
gorhill committed May 19, 2019
1 parent a0ac1b7 commit 72d9758
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 45 deletions.
4 changes: 4 additions & 0 deletions src/js/3p-filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@ const renderFilterLists = function(soft) {
if ( !soft ) {
filteringSettingsHash = hashFromCurrentFromSettings();
}

// https://github.com/gorhill/uBlock/issues/2394
document.body.classList.toggle('updating', listDetails.isUpdating);

renderWidgets();
};

Expand Down
62 changes: 35 additions & 27 deletions src/js/assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -916,15 +916,16 @@ api.rmrf = function() {
/******************************************************************************/

// Asset updater area.
var updaterStatus,
const updaterAssetDelayDefault = 120000;
const updaterUpdated = [];
const updaterFetched = new Set();

let updaterStatus,
updaterTimer,
updaterAssetDelayDefault = 120000,
updaterAssetDelay = updaterAssetDelayDefault,
updaterUpdated = [],
updaterFetched = new Set(),
noRemoteResources;

var updateFirst = function() {
const updateFirst = function() {
// https://github.com/gorhill/uBlock/commit/126110c9a0a0630cd556f5cb215422296a961029
// Firefox extension reviewers do not want uBO/webext to fetch its own
// scriptlets/resources asset from the project's own repo (github.com).
Expand All @@ -939,31 +940,33 @@ var updateFirst = function() {
}
updaterStatus = 'updating';
updaterFetched.clear();
updaterUpdated = [];
updaterUpdated.length = 0;
fireNotification('before-assets-updated');
updateNext();
};

var updateNext = function() {
var assetDict, cacheDict;
const updateNext = function() {
let assetDict, cacheDict;

// This will remove a cached asset when it's no longer in use.
var garbageCollectOne = function(assetKey) {
var cacheEntry = cacheDict[assetKey];
const garbageCollectOne = function(assetKey) {
const cacheEntry = cacheDict[assetKey];
if ( cacheEntry && cacheEntry.readTime < assetCacheRegistryStartTime ) {
assetCacheRemove(assetKey);
}
};

var findOne = function() {
var now = Date.now(),
assetEntry, cacheEntry;
for ( var assetKey in assetDict ) {
assetEntry = assetDict[assetKey];
const findOne = function() {
const now = Date.now();
for ( const assetKey in assetDict ) {
const assetEntry = assetDict[assetKey];
if ( assetEntry.hasRemoteURL !== true ) { continue; }
if ( updaterFetched.has(assetKey) ) { continue; }
cacheEntry = cacheDict[assetKey];
if ( cacheEntry && (cacheEntry.writeTime + assetEntry.updateAfter * 86400000) > now ) {
const cacheEntry = cacheDict[assetKey];
if (
cacheEntry &&
(cacheEntry.writeTime + assetEntry.updateAfter * 86400000) > now
) {
continue;
}
// Update of user scripts/resources forbidden?
Expand All @@ -982,7 +985,7 @@ var updateNext = function() {
}
};

var updatedOne = function(details) {
const updatedOne = function(details) {
if ( details.content !== '' ) {
updaterUpdated.push(details.assetKey);
if ( details.assetKey === 'assets.json' ) {
Expand All @@ -998,8 +1001,8 @@ var updateNext = function() {
}
};

var updateOne = function() {
var assetKey = findOne();
const updateOne = function() {
const assetKey = findOne();
if ( assetKey === undefined ) {
return updateDone();
}
Expand All @@ -1020,20 +1023,20 @@ var updateNext = function() {
});
};

var updateDone = function() {
var assetKeys = updaterUpdated.slice(0);
const updateDone = function() {
const assetKeys = updaterUpdated.slice(0);
updaterFetched.clear();
updaterUpdated = [];
updaterUpdated.length = 0;
updaterStatus = undefined;
updaterAssetDelay = updaterAssetDelayDefault;
fireNotification('after-assets-updated', { assetKeys: assetKeys });
};

api.updateStart = function(details) {
var oldUpdateDelay = updaterAssetDelay,
newUpdateDelay = typeof details.delay === 'number' ?
details.delay :
updaterAssetDelayDefault;
const oldUpdateDelay = updaterAssetDelay;
const newUpdateDelay = typeof details.delay === 'number' ?
details.delay :
updaterAssetDelayDefault;
updaterAssetDelay = Math.min(oldUpdateDelay, newUpdateDelay);
if ( updaterStatus !== undefined ) {
if ( newUpdateDelay < oldUpdateDelay ) {
Expand All @@ -1055,6 +1058,11 @@ api.updateStop = function() {
}
};

api.isUpdating = function() {
return updaterStatus === 'updating' &&
updaterAssetDelay <= µBlock.hiddenSettings.manualUpdateAssetFetchPeriod;
};

/******************************************************************************/

return api;
Expand Down
1 change: 1 addition & 0 deletions src/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const µBlock = (function() { // jshint ignore:line
assetFetchTimeout: 30,
autoCommentFilterTemplate: '{{date}} {{origin}}',
autoUpdateAssetFetchPeriod: 120,
autoUpdateDelayAfterLaunch: 180,
autoUpdatePeriod: 7,
cacheStorageAPI: 'unset',
cacheStorageCompression: true,
Expand Down
32 changes: 16 additions & 16 deletions src/js/messaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -862,41 +862,41 @@ const resetUserData = function() {

// 3rd-party filters

var prepListEntries = function(entries) {
var µburi = µb.URI;
var entry, hn;
for ( var k in entries ) {
const prepListEntries = function(entries) {
const µburi = µb.URI;
for ( const k in entries ) {
if ( entries.hasOwnProperty(k) === false ) { continue; }
entry = entries[k];
const entry = entries[k];
if ( typeof entry.supportURL === 'string' && entry.supportURL !== '' ) {
entry.supportName = µburi.hostnameFromURI(entry.supportURL);
} else if ( typeof entry.homeURL === 'string' && entry.homeURL !== '' ) {
hn = µburi.hostnameFromURI(entry.homeURL);
entry.supportURL = 'http://' + hn + '/';
const hn = µburi.hostnameFromURI(entry.homeURL);
entry.supportURL = `http://${hn}/`;
entry.supportName = µburi.domainFromHostname(hn);
}
}
};

var getLists = function(callback) {
var r = {
const getLists = function(callback) {
const r = {
autoUpdate: µb.userSettings.autoUpdate,
available: null,
cache: null,
cosmeticFilterCount: µb.cosmeticFilteringEngine.getFilterCount(),
current: µb.availableFilterLists,
externalLists: µb.userSettings.externalLists,
ignoreGenericCosmeticFilters: µb.userSettings.ignoreGenericCosmeticFilters,
isUpdating: µb.assets.isUpdating(),
netFilterCount: µb.staticNetFilteringEngine.getFilterCount(),
parseCosmeticFilters: µb.userSettings.parseAllABPHideFilters,
userFiltersPath: µb.userFiltersPath
};
var onMetadataReady = function(entries) {
const onMetadataReady = function(entries) {
r.cache = entries;
prepListEntries(r.cache);
callback(r);
};
var onLists = function(lists) {
const onLists = function(lists) {
r.available = lists;
prepListEntries(r.available);
µb.assets.metadata(onMetadataReady);
Expand All @@ -908,7 +908,7 @@ var getLists = function(callback) {

// My rules

var getRules = function() {
const getRules = function() {
return {
permanentRules:
µb.permanentFirewall.toArray().concat(
Expand All @@ -923,7 +923,7 @@ var getRules = function() {
};
};

var modifyRuleset = function(details) {
const modifyRuleset = function(details) {
let swRuleset, hnRuleset, urlRuleset;
if ( details.permanent ) {
swRuleset = µb.permanentSwitches;
Expand Down Expand Up @@ -974,7 +974,7 @@ var modifyRuleset = function(details) {

// Shortcuts pane

let getShortcuts = function(callback) {
const getShortcuts = function(callback) {
if ( µb.canUseShortcuts === false ) {
return callback([]);
}
Expand All @@ -995,7 +995,7 @@ let getShortcuts = function(callback) {
});
};

let setShortcut = function(details) {
const setShortcut = function(details) {
if ( µb.canUpdateShortcuts === false ) { return; }
if ( details.shortcut === undefined ) {
vAPI.commands.reset(details.name);
Expand All @@ -1009,7 +1009,7 @@ let setShortcut = function(details) {

/******************************************************************************/

var onMessage = function(request, sender, callback) {
const onMessage = function(request, sender, callback) {
// Async
switch ( request.what ) {
case 'backupUserData':
Expand Down
8 changes: 6 additions & 2 deletions src/js/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,13 @@ var onAllReady = function() {
initializeTabs();

// https://github.com/chrisaljoudi/uBlock/issues/184
// Check for updates not too far in the future.
// Check for updates not too far in the future.
µb.assets.addObserver(µb.assetObserver.bind(µb));
µb.scheduleAssetUpdater(µb.userSettings.autoUpdate ? 5 * 60 * 1000 : 0);
µb.scheduleAssetUpdater(
µb.userSettings.autoUpdate
? µb.hiddenSettings.autoUpdateDelayAfterLaunch * 1000
: 0
);

// vAPI.cloud is optional.
if ( µb.cloudStorageSupported ) {
Expand Down

2 comments on commit 72d9758

@mikhaelkh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wiki becomes more and more outdated.
New options allowGenericProceduralFilters false, autoUpdateDelayAfterLaunch 180, default changed selfieAfter 3. Did I miss anything?

@gwarser
Copy link
Contributor

@gwarser gwarser commented on 72d9758 May 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.