Ensure scriptlet cache is reset when filtering profile changes

Related issue:
https://github.com/uBlockOrigin/uBlock-issues/issues/2896

TODO: Eventually, distinguish between filtering profile increasing
or decreasing so as to avoid flushing caches when increasing
filtering, which should not affect the scriptlets cache.
This commit is contained in:
Raymond Hill 2023-10-21 14:25:26 -04:00
parent d6bd14d708
commit e5b438257f
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
5 changed files with 35 additions and 20 deletions

View File

@ -66,6 +66,10 @@ const scriptletFilteringEngine = {
const contentScriptRegisterer = new (class {
constructor() {
this.hostnameToDetails = new Map();
if ( browser.contentScripts === undefined ) { return; }
µb.onEvent('filteringBehaviorChanged', ( ) => {
this.reset();
});
}
register(hostname, code) {
if ( browser.contentScripts === undefined ) { return false; }

View File

@ -453,7 +453,7 @@ if ( selfieIsValid !== true ) {
// Flush memory cache -- unsure whether the browser does this internally
// when loading a new extension.
vAPI.net.handlerBehaviorChanged();
µb.filteringBehaviorChanged();
// Final initialization steps after all needed assets are in memory.

View File

@ -243,7 +243,7 @@ import {
if ( typeof hs[key] !== typeof hsDefault[key] ) { continue; }
this.hiddenSettings[key] = hs[key];
}
this.fireDOMEvent('hiddenSettingsChanged');
this.fireEvent('hiddenSettingsChanged');
};
// Note: Save only the settings which values differ from the default ones.
@ -259,7 +259,7 @@ import {
});
};
self.addEventListener('hiddenSettingsChanged', ( ) => {
µb.onEvent('hiddenSettingsChanged', ( ) => {
const µbhs = µb.hiddenSettings;
ubologSet(µbhs.consoleLogLevel === 'info');
vAPI.net.setOptions({
@ -364,6 +364,7 @@ self.addEventListener('hiddenSettingsChanged', ( ) => {
netWhitelist: this.arrayFromWhitelist(this.netWhitelist)
});
this.netWhitelistModifyTime = Date.now();
µb.filteringBehaviorChanged();
};
/******************************************************************************/
@ -593,7 +594,7 @@ self.addEventListener('hiddenSettingsChanged', ( ) => {
// https://www.reddit.com/r/uBlockOrigin/comments/cj7g7m/
// https://www.reddit.com/r/uBlockOrigin/comments/cnq0bi/
vAPI.net.handlerBehaviorChanged();
µb.filteringBehaviorChanged();
vAPI.messaging.broadcast({ what: 'userFiltersUpdated' });
};
@ -830,7 +831,7 @@ self.addEventListener('hiddenSettingsChanged', ( ) => {
staticExtFilteringEngine.freeze();
redirectEngine.freeze();
vAPI.net.unsuspend();
vAPI.net.handlerBehaviorChanged();
µb.filteringBehaviorChanged();
vAPI.storage.set({ 'availableFilterLists': µb.availableFilterLists });

View File

@ -187,10 +187,6 @@ const matchBucket = function(url, hostname, bucket, start) {
}
}
this.saveWhitelist();
// Flush memory cache
vAPI.net.handlerBehaviorChanged();
return true;
};
@ -425,7 +421,7 @@ const matchBucket = function(url, hostname, bucket, start) {
redirectEngine.invalidateResourcesSelfie(io);
this.loadRedirectResources();
}
this.fireDOMEvent('hiddenSettingsChanged');
this.fireEvent('hiddenSettingsChanged');
};
/******************************************************************************/
@ -526,9 +522,7 @@ const matchBucket = function(url, hostname, bucket, start) {
cosmeticFilteringEngine.removeFromSelectorCache(srcHostname, 'net');
// Flush memory cache
if ( action === 1 ) {
vAPI.net.handlerBehaviorChanged();
}
µb.filteringBehaviorChanged();
if ( details.tabId === undefined ) { return; }
@ -614,7 +608,7 @@ const matchBucket = function(url, hostname, bucket, start) {
switch ( details.name ) {
case 'no-scripting':
case 'no-remote-fonts':
vAPI.net.handlerBehaviorChanged();
µb.filteringBehaviorChanged();
break;
default:
break;
@ -675,7 +669,7 @@ const matchBucket = function(url, hostname, bucket, start) {
parse();
self.addEventListener('hiddenSettingsChanged', ( ) => { parse(); });
µb.onEvent('hiddenSettingsChanged', ( ) => { parse(); });
}
/******************************************************************************/

View File

@ -134,16 +134,32 @@ import µb from './background.js';
/******************************************************************************/
µb.fireDOMEvent = function(name) {
µb.fireEvent = function(name) {
if (
window instanceof Object &&
window.dispatchEvent instanceof Function &&
window.CustomEvent instanceof Function
self instanceof Object &&
self.dispatchEvent instanceof Function &&
self.CustomEvent instanceof Function
) {
window.dispatchEvent(new CustomEvent(name));
self.dispatchEvent(new CustomEvent(name));
}
};
µb.onEvent = function(name, fn) {
if (
self instanceof Object &&
self.addEventListener instanceof Function
) {
self.addEventListener(name, fn);
}
};
/******************************************************************************/
µb.filteringBehaviorChanged = function() {
vAPI.net.handlerBehaviorChanged();
this.fireEvent('filteringBehaviorChanged');
};
/******************************************************************************/
// TODO: properly compare arrays