Skip to content

Commit 23a25a1

Browse files
committed
Use storage.session (on-memory storage area) instead of sessions (persisted to the disk storage) by default #3388
1 parent 16e89e8 commit 23a25a1

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

webextensions/background/background-cache.js

+33
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ let mActivated = false;
3434
export function activate() {
3535
mActivated = true;
3636
configs.$addObserver(onConfigChange);
37+
38+
if (!configs.persistCachedTree &&
39+
browser.storage.session) {
40+
browser.windows.getAll().then(windows => {
41+
for (const win of windows) {
42+
browser.sessions.removeWindowValue(win.id, Constants.kWINDOW_STATE_CACHED_TABS).catch(ApiTabs.createErrorSuppressor());
43+
browser.sessions.removeWindowValue(win.id, Constants.kWINDOW_STATE_CACHED_SIDEBAR_TABS_DIRTY).catch(ApiTabs.createErrorSuppressor());
44+
}
45+
});
46+
}
3747
}
3848

3949

@@ -287,6 +297,20 @@ function fixupTabRestoredFromCachePostProcess(tab) {
287297
async function updateWindowCache(owner, key, value) {
288298
if (!owner)
289299
return;
300+
301+
if (!configs.persistCachedTree &&
302+
browser.storage.session) {
303+
const storagKey = `backgroundCache-window${owner.windowId}-${key}`;
304+
if (value) {
305+
const data = {};
306+
data[storagKey] = value;
307+
return browser.storage.session.set(data);
308+
}
309+
else {
310+
return browser.storage.session.remove(storagKey);
311+
}
312+
}
313+
290314
if (value === undefined) {
291315
try {
292316
return browser.sessions.removeWindowValue(owner.windowId, key).catch(ApiTabs.createErrorSuppressor());
@@ -318,6 +342,14 @@ export function markWindowCacheDirtyFromTab(tab, akey) {
318342
}
319343

320344
async function getWindowCache(owner, key) {
345+
if (!configs.persistCachedTree) {
346+
const storageKey = `backgroundCache-window${owner.windowId}-${key}`;
347+
const defaultData = {};
348+
defaultData[storageKey] = undefined;
349+
return browser.storage.session.get(defaultData).then(data => {
350+
return data[storageKey];
351+
});
352+
}
321353
return browser.sessions.getWindowValue(owner.windowId, key).catch(ApiTabs.createErrorHandler());
322354
}
323355

@@ -495,6 +527,7 @@ Tab.onHidden.addListener(tab => {
495527
function onConfigChange(key) {
496528
switch (key) {
497529
case 'useCachedTree':
530+
case 'persistCachedTree':
498531
browser.windows.getAll({
499532
populate: true,
500533
windowTypes: ['normal']

webextensions/common/common.js

+1
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ export const configs = new Configs({
352352
notifiedFeaturesVersion: 0,
353353

354354
useCachedTree: true,
355+
persistCachedTree: false,
355356

356357
// This should be removed after https://bugzilla.mozilla.org/show_bug.cgi?id=1388193
357358
// or https://bugzilla.mozilla.org/show_bug.cgi?id=1421329 become fixed.

webextensions/sidebar/sidebar-cache.js

+39
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ let mTabBar;
5353
export function init() {
5454
mTargetWindow = TabsStore.getCurrentWindowId();
5555
mTabBar = document.querySelector('#tabbar');
56+
57+
if (!configs.persistCachedTree)
58+
clearPersistentWindowCache();
5659
}
5760

5861
export function startTracking() {
@@ -433,6 +436,20 @@ async function fixupTabsRestoredFromCache(tabElements, tabs, options = {}) {
433436
// ===================================================================
434437

435438
function updateWindowCache(key, value) {
439+
if (!configs.persistCachedTree &&
440+
browser.storage.session) {
441+
const storagKey = `sidebarCache-window${mTargetWindow}-${key}`;
442+
if (value) {
443+
const data = {};
444+
data[storagKey] = value;
445+
browser.storage.session.set(data);
446+
}
447+
else {
448+
browser.storage.session.remove(storagKey);
449+
}
450+
return;
451+
}
452+
436453
if (!mLastWindowCacheOwner ||
437454
!Tab.get(mLastWindowCacheOwner.id))
438455
return;
@@ -448,11 +465,23 @@ function updateWindowCache(key, value) {
448465

449466
function clearWindowCache() {
450467
log('clearWindowCache ', { stack: configs.debug && new Error().stack });
468+
if (!configs.persistCachedTree)
469+
clearPersistentWindowCache();
451470
updateWindowCache(Constants.kWINDOW_STATE_CACHED_SIDEBAR);
452471
updateWindowCache(Constants.kWINDOW_STATE_CACHED_SIDEBAR_TABS_DIRTY);
453472
updateWindowCache(Constants.kWINDOW_STATE_CACHED_SIDEBAR_COLLAPSED_DIRTY);
454473
}
455474

475+
function clearPersistentWindowCache() {
476+
if (!mLastWindowCacheOwner ||
477+
!Tab.get(mLastWindowCacheOwner.id))
478+
return;
479+
log('clearPersistentWindowCache ', { stack: configs.debug && new Error().stack });
480+
browser.sessions.removeWindowValue(mLastWindowCacheOwner.windowId, Constants.kWINDOW_STATE_CACHED_SIDEBAR).catch(ApiTabs.createErrorSuppressor());
481+
browser.sessions.removeWindowValue(mLastWindowCacheOwner.windowId, Constants.kWINDOW_STATE_CACHED_SIDEBAR_TABS_DIRTY).catch(ApiTabs.createErrorSuppressor());
482+
browser.sessions.removeWindowValue(mLastWindowCacheOwner.windowId, Constants.kWINDOW_STATE_CACHED_SIDEBAR_COLLAPSED_DIRTY).catch(ApiTabs.createErrorSuppressor());
483+
}
484+
456485
export function markWindowCacheDirty(key) {
457486
if (markWindowCacheDirty.timeout)
458487
clearTimeout(markWindowCacheDirty.timeout);
@@ -463,6 +492,15 @@ export function markWindowCacheDirty(key) {
463492
}
464493

465494
async function getWindowCache(key) {
495+
if (!configs.persistCachedTree) {
496+
const storageKey = `sidebarCache-window${mTargetWindow}-${key}`;
497+
const defaultData = {};
498+
defaultData[storageKey] = undefined;
499+
return browser.storage.session.get(defaultData).then(data => {
500+
return data[storageKey];
501+
});
502+
}
503+
466504
if (!mLastWindowCacheOwner)
467505
return null;
468506
return browser.sessions.getWindowValue(mLastWindowCacheOwner.windowId, key).catch(ApiTabs.createErrorHandler());
@@ -527,6 +565,7 @@ async function updateCachedTabbar() {
527565
function onConfigChange(changedKey) {
528566
switch (changedKey) {
529567
case 'useCachedTree':
568+
case 'persistCachedTree':
530569
if (configs[changedKey]) {
531570
reserveToUpdateCachedTabbar();
532571
}

0 commit comments

Comments
 (0)