Skip to content

Commit 8f7b7a7

Browse files
committed
cachestorage.js: Refuse backend changes after initialization
Enforce the existing cache storage implementation requirement that the backend not change at runtime: - Warn if `select()` is not called prior to first use. - Refuse changes after a backend has been chosen (via `select()` or by default on first use).
1 parent 0a7e2d2 commit 8f7b7a7

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

src/js/cachestorage.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import * as s14e from './s14e-serializer.js';
3535

3636
const STORAGE_NAME = 'uBlock0CacheStorage';
3737
const extensionStorage = webext.storage.local;
38+
const defaultFastCache = 'indexedDB';
3839

3940
const keysFromGetArg = arg => {
4041
if ( arg === null || arg === undefined ) { return []; }
@@ -45,8 +46,6 @@ const keysFromGetArg = arg => {
4546
return Object.keys(arg);
4647
};
4748

48-
let fastCache = 'indexedDB';
49-
5049
/*******************************************************************************
5150
*
5251
* Extension storage
@@ -56,6 +55,15 @@ let fastCache = 'indexedDB';
5655
* */
5756

5857
const cacheStorage = (( ) => {
58+
let fastCache;
59+
60+
const getFastCacheStorage = ( ) => {
61+
if ( fastCache === undefined ) {
62+
ubolog(`Cache storage backend not selected, using default`);
63+
fastCache = defaultFastCache;
64+
}
65+
return cacheAPIs[fastCache];
66+
};
5967

6068
const exGet = (api, wanted, outbin) => {
6169
return api.get(wanted).then(inbin => {
@@ -96,7 +104,7 @@ const cacheStorage = (( ) => {
96104
get(argbin) {
97105
const outbin = {};
98106
return exGet(
99-
cacheAPIs[fastCache],
107+
getFastCacheStorage(),
100108
keysFromGetArg(argbin),
101109
outbin
102110
).then(wanted => {
@@ -123,7 +131,7 @@ const cacheStorage = (( ) => {
123131

124132
async keys(regex) {
125133
const results = await Promise.all([
126-
cacheAPIs[fastCache].keys(regex),
134+
getFastCacheStorage().keys(regex),
127135
extensionStorage.get(null).catch(( ) => {}),
128136
]);
129137
const keys = new Set(results[0]);
@@ -144,28 +152,38 @@ const cacheStorage = (( ) => {
144152
promises.push(compress(serializedbin, key, rawbin[key]));
145153
}
146154
await Promise.all(promises);
147-
cacheAPIs[fastCache].set(rawbin, serializedbin);
155+
getFastCacheStorage().set(rawbin, serializedbin);
148156
return extensionStorage.set(serializedbin).catch(reason => {
149157
ubolog(reason);
150158
});
151159
},
152160

153161
remove(...args) {
154-
cacheAPIs[fastCache].remove(...args);
162+
getFastCacheStorage().remove(...args);
155163
return extensionStorage.remove(...args).catch(reason => {
156164
ubolog(reason);
157165
});
158166
},
159167

160168
clear(...args) {
161-
cacheAPIs[fastCache].clear(...args);
169+
getFastCacheStorage().clear(...args);
162170
return extensionStorage.clear(...args).catch(reason => {
163171
ubolog(reason);
164172
});
165173
},
166174

167175
select(api) {
168-
if ( cacheAPIs.hasOwnProperty(api) === false ) { return fastCache; }
176+
if ( fastCache !== undefined ) {
177+
ubolog(`Refusing cache storage backend change`);
178+
return fastCache;
179+
}
180+
if ( cacheAPIs.hasOwnProperty(api) === false ) {
181+
if ( api !== undefined && api !== 'unset' ) {
182+
ubolog(`Ignoring unimplemented cache storage backend`);
183+
}
184+
fastCache = defaultFastCache;
185+
return fastCache;
186+
}
169187
fastCache = api;
170188
for ( const k of Object.keys(cacheAPIs) ) {
171189
if ( k === api ) { continue; }

0 commit comments

Comments
 (0)