Skip to content

Commit 85dd951

Browse files
committed
Don't use storage.session for background cache #3434
1 parent ec34d05 commit 85dd951

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

webextensions/background/background-cache.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ function log(...args) {
3131
const kCONTENTS_VERSION = 5;
3232

3333
let mActivated = false;
34+
const mCaches = {};
3435

3536
export function activate() {
3637
mActivated = true;
3738
configs.$addObserver(onConfigChange);
3839

39-
if (!configs.persistCachedTree &&
40-
browser.storage.session) {
40+
if (!configs.persistCachedTree) {
4141
browser.windows.getAll().then(windows => {
4242
for (const win of windows) {
4343
browser.sessions.removeWindowValue(win.id, Constants.kWINDOW_STATE_CACHED_TABS).catch(ApiTabs.createErrorSuppressor());
@@ -299,17 +299,13 @@ async function updateWindowCache(owner, key, value) {
299299
if (!owner)
300300
return;
301301

302-
if (!configs.persistCachedTree &&
303-
browser.storage.session) {
302+
if (!configs.persistCachedTree) {
304303
const storageKey = `backgroundCache-${await UniqueId.ensureWindowId(owner.windowId)}-${key}`;
305-
if (value) {
306-
const data = {};
307-
data[storageKey] = value;
308-
return browser.storage.session.set(data);
309-
}
310-
else {
311-
return browser.storage.session.remove(storageKey);
312-
}
304+
if (value)
305+
mCaches[storageKey] = value;
306+
else
307+
delete mCaches[storageKey];
308+
return;
313309
}
314310

315311
if (value === undefined) {
@@ -345,11 +341,7 @@ export function markWindowCacheDirtyFromTab(tab, akey) {
345341
async function getWindowCache(owner, key) {
346342
if (!configs.persistCachedTree) {
347343
const storageKey = `backgroundCache-${await UniqueId.ensureWindowId(owner.windowId)}-${key}`;
348-
const defaultData = {};
349-
defaultData[storageKey] = undefined;
350-
return browser.storage.session.get(defaultData).then(data => {
351-
return data[storageKey];
352-
});
344+
return mCaches[storageKey];
353345
}
354346
return browser.sessions.getWindowValue(owner.windowId, key).catch(ApiTabs.createErrorHandler());
355347
}
@@ -525,6 +517,14 @@ Tab.onHidden.addListener(tab => {
525517
reserveToCacheTree(tab.windowId);
526518
});
527519

520+
browser.windows.onRemoved.addListener(async windowId => {
521+
const storageKeyPrefix = `backgroundCache-${await UniqueId.ensureWindowId(windowId)}-`;
522+
for (const key in mCaches) {
523+
if (key.startsWith(storageKeyPrefix))
524+
delete mCaches[key];
525+
}
526+
});
527+
528528
function onConfigChange(key) {
529529
switch (key) {
530530
case 'useCachedTree':

0 commit comments

Comments
 (0)