Skip to content

Commit

Permalink
Properly manage cache storage regarding managed user filters
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Mar 23, 2024
1 parent 11a4856 commit 90ab1a7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 56 deletions.
14 changes: 5 additions & 9 deletions src/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,18 @@
Home: https://github.com/gorhill/uBlock
*/

/* globals browser */

'use strict';

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

import logger from './logger.js';
import { FilteringContext } from './filtering-context.js';
import { ubologSet } from './console.js';

import {
domainFromHostname,
hostnameFromURI,
originFromURI,
} from './uri-utils.js';

import { FilteringContext } from './filtering-context.js';
import logger from './logger.js';
import { ubologSet } from './console.js';

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

// Not all platforms may have properly declared vAPI.webextFlavor.
Expand All @@ -54,7 +50,6 @@ const hiddenSettingsDefault = {
autoUpdatePeriod: 1,
benchmarkDatasetURL: 'unset',
blockingProfiles: '11111/#F00 11010/#C0F 11001/#00F 00001',
cacheStorageAPI: 'unset',
cacheStorageCompression: true,
cacheStorageCompressionThreshold: 65536,
cacheStorageMultithread: 2,
Expand Down Expand Up @@ -97,6 +92,7 @@ const hiddenSettingsDefault = {

if ( vAPI.webextFlavor.soup.has('devbuild') ) {
hiddenSettingsDefault.consoleLogLevel = 'info';
hiddenSettingsDefault.cacheStorageAPI = 'unset';
ubologSet(true);
}

Expand Down
97 changes: 50 additions & 47 deletions src/js/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,39 @@
Home: https://github.com/gorhill/uBlock
*/

'use strict';

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

import publicSuffixList from '../lib/publicsuffixlist/publicsuffixlist.js';
import punycode from '../lib/punycode.js';
import * as sfp from './static-filtering-parser.js';

import io from './assets.js';
import { CompiledListReader, CompiledListWriter } from './static-filtering-io.js';
import { LineIterator, orphanizeString } from './text-utils.js';
import { broadcast, filteringBehaviorChanged, onBroadcast } from './broadcast.js';
import { i18n, i18n$ } from './i18n.js';
import {
permanentFirewall,
permanentSwitches,
permanentURLFiltering,
} from './filtering-engines.js';
import { ubolog, ubologSet } from './console.js';

import cosmeticFilteringEngine from './cosmetic-filtering.js';
import { hostnameFromURI } from './uri-utils.js';
import io from './assets.js';
import logger from './logger.js';
import lz4Codec from './lz4.js';
import publicSuffixList from '../lib/publicsuffixlist/publicsuffixlist.js';
import punycode from '../lib/punycode.js';
import { redirectEngine } from './redirect-engine.js';
import staticExtFilteringEngine from './static-ext-filtering.js';
import staticFilteringReverseLookup from './reverselookup.js';
import staticNetFilteringEngine from './static-net-filtering.js';
import µb from './background.js';
import { hostnameFromURI } from './uri-utils.js';
import { i18n, i18n$ } from './i18n.js';
import { redirectEngine } from './redirect-engine.js';
import { ubolog, ubologSet } from './console.js';
import * as sfp from './static-filtering-parser.js';

import {
permanentFirewall,
permanentSwitches,
permanentURLFiltering,
} from './filtering-engines.js';

import {
CompiledListReader,
CompiledListWriter,
} from './static-filtering-io.js';
/******************************************************************************/

import {
LineIterator,
orphanizeString,
} from './text-utils.js';
// https://eslint.org/docs/latest/rules/no-prototype-builtins
const hasOwnProperty = (o, p) =>
Object.prototype.hasOwnProperty.call(o, p);

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

Expand Down Expand Up @@ -191,7 +187,7 @@ import {
for ( const entry of adminSettings ) {
if ( entry.length < 1 ) { continue; }
const name = entry[0];
if ( usDefault.hasOwnProperty(name) === false ) { continue; }
if ( hasOwnProperty(usDefault, name) === false ) { continue; }
const value = entry.length < 2
? usDefault[name]
: this.settingValueFromString(usDefault, name, entry[1]);
Expand Down Expand Up @@ -220,8 +216,8 @@ import {

const toRemove = [];
for ( const key in this.userSettings ) {
if ( this.userSettings.hasOwnProperty(key) === false ) { continue; }
if ( toSave.hasOwnProperty(key) ) { continue; }
if ( hasOwnProperty(this.userSettings, key) === false ) { continue; }
if ( hasOwnProperty(toSave, key) ) { continue; }
toRemove.push(key);
}
if ( toRemove.length !== 0 ) {
Expand Down Expand Up @@ -258,7 +254,7 @@ import {
for ( const entry of advancedSettings ) {
if ( entry.length < 1 ) { continue; }
const name = entry[0];
if ( hsDefault.hasOwnProperty(name) === false ) { continue; }
if ( hasOwnProperty(hsDefault, name) === false ) { continue; }
const value = entry.length < 2
? hsDefault[name]
: this.hiddenSettingValueFromString(name, entry[1]);
Expand Down Expand Up @@ -292,8 +288,8 @@ import {
}

for ( const key in hsDefault ) {
if ( hsDefault.hasOwnProperty(key) === false ) { continue; }
if ( hsAdmin.hasOwnProperty(name) ) { continue; }
if ( hasOwnProperty(hsDefault, key) === false ) { continue; }
if ( hasOwnProperty(hsAdmin, name) ) { continue; }
if ( typeof hs[key] !== typeof hsDefault[key] ) { continue; }
this.hiddenSettings[key] = hs[key];
}
Expand Down Expand Up @@ -338,8 +334,8 @@ onBroadcast(msg => {
const matches = /^\s*(\S+)\s+(.+)$/.exec(line);
if ( matches === null || matches.length !== 3 ) { continue; }
const name = matches[1];
if ( out.hasOwnProperty(name) === false ) { continue; }
if ( this.hiddenSettingsAdmin.hasOwnProperty(name) ) { continue; }
if ( hasOwnProperty(out, name) === false ) { continue; }
if ( hasOwnProperty(this.hiddenSettingsAdmin, name) ) { continue; }
const value = this.hiddenSettingValueFromString(name, matches[2]);
if ( value !== undefined ) {
out[name] = value;
Expand All @@ -351,7 +347,7 @@ onBroadcast(msg => {
µb.hiddenSettingValueFromString = function(name, value) {
if ( typeof name !== 'string' || typeof value !== 'string' ) { return; }
const hsDefault = this.hiddenSettingsDefault;
if ( hsDefault.hasOwnProperty(name) === false ) { return; }
if ( hasOwnProperty(hsDefault, name) === false ) { return; }
let r;
switch ( typeof hsDefault[name] ) {
case 'boolean':
Expand Down Expand Up @@ -588,7 +584,6 @@ onBroadcast(msg => {
// https://github.com/gorhill/uBlock/issues/1022
// Be sure to end with an empty line.
content = content.trim();
if ( content !== '' ) { content += '\n'; }
this.removeCompiledFilterList(this.userFiltersPath);
return io.put(this.userFiltersPath, content);
};
Expand Down Expand Up @@ -696,7 +691,7 @@ onBroadcast(msg => {
µb.autoSelectRegionalFilterLists = function(lists) {
const selectedListKeys = [ this.userFiltersPath ];
for ( const key in lists ) {
if ( lists.hasOwnProperty(key) === false ) { continue; }
if ( hasOwnProperty(lists, key) === false ) { continue; }
const list = lists[key];
if ( list.content !== 'filters' ) { continue; }
if ( list.off !== true ) {
Expand Down Expand Up @@ -950,7 +945,7 @@ onBroadcast(msg => {
let acceptedCount = snfe.acceptedCount + sxfe.acceptedCount;
let discardedCount = snfe.discardedCount + sxfe.discardedCount;
µb.applyCompiledFilters(compiled, assetKey === µb.userFiltersPath);
if ( µb.availableFilterLists.hasOwnProperty(assetKey) ) {
if ( hasOwnProperty(µb.availableFilterLists, assetKey) ) {
const entry = µb.availableFilterLists[assetKey];
entry.entryCount = snfe.acceptedCount + sxfe.acceptedCount -
acceptedCount;
Expand Down Expand Up @@ -986,7 +981,7 @@ onBroadcast(msg => {
// content.
const toLoad = [];
for ( const assetKey in lists ) {
if ( lists.hasOwnProperty(assetKey) === false ) { continue; }
if ( hasOwnProperty(lists, assetKey) === false ) { continue; }
if ( lists[assetKey].off ) { continue; }
toLoad.push(
µb.getCompiledFilterList(assetKey).then(details => {
Expand Down Expand Up @@ -1438,8 +1433,8 @@ onBroadcast(msg => {
const µbus = this.userSettings;
const adminus = data.userSettings;
for ( const name in µbus ) {
if ( µbus.hasOwnProperty(name) === false ) { continue; }
if ( adminus.hasOwnProperty(name) === false ) { continue; }
if ( hasOwnProperty(µbus, name) === false ) { continue; }
if ( hasOwnProperty(adminus, name) === false ) { continue; }
bin[name] = adminus[name];
binNotEmpty = true;
}
Expand Down Expand Up @@ -1502,13 +1497,21 @@ onBroadcast(msg => {
vAPI.storage.set(bin);
}

if (
Array.isArray(toOverwrite.filters) &&
toOverwrite.filters.length !== 0
) {
this.saveUserFilters(toOverwrite.filters.join('\n'));
let userFiltersAfter;
if ( Array.isArray(toOverwrite.filters) ) {
userFiltersAfter = toOverwrite.filters.join('\n').trim();
} else if ( typeof data.userFilters === 'string' ) {
this.saveUserFilters(data.userFilters);
userFiltersAfter = data.userFilters.trim();
}
if ( typeof userFiltersAfter === 'string' ) {
const bin = await vAPI.storage.get(this.userFiltersPath);
const userFiltersBefore = bin && bin[this.userFiltersPath] || '';
if ( userFiltersAfter !== userFiltersBefore ) {
await Promise.all([
this.saveUserFilters(userFiltersAfter),
this.selfieManager.destroy(),
]);
}
}
};

Expand Down Expand Up @@ -1602,7 +1605,7 @@ onBroadcast(msg => {
if ( topic === 'before-asset-updated' ) {
if ( details.type === 'filters' ) {
if (
this.availableFilterLists.hasOwnProperty(details.assetKey) === false ||
hasOwnProperty(this.availableFilterLists, details.assetKey) === false ||
this.selectedFilterLists.indexOf(details.assetKey) === -1 ||
this.badLists.get(details.assetKey)
) {
Expand All @@ -1617,7 +1620,7 @@ onBroadcast(msg => {
// Skip selfie-related content.
if ( details.assetKey.startsWith('selfie/') ) { return; }
const cached = typeof details.content === 'string' && details.content !== '';
if ( this.availableFilterLists.hasOwnProperty(details.assetKey) ) {
if ( hasOwnProperty(this.availableFilterLists, details.assetKey) ) {
if ( cached ) {
if ( this.selectedFilterLists.indexOf(details.assetKey) !== -1 ) {
this.extractFilterListMetadata(
Expand Down

0 comments on commit 90ab1a7

Please sign in to comment.