Skip to content

Commit

Permalink
Use storage.session (on-memory storage area) instead of sessions (per…
Browse files Browse the repository at this point in the history
…sisted to the disk storage) by default #3388
  • Loading branch information
piroor committed Nov 22, 2023
1 parent 16e89e8 commit 23a25a1
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
33 changes: 33 additions & 0 deletions webextensions/background/background-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ let mActivated = false;
export function activate() {
mActivated = true;
configs.$addObserver(onConfigChange);

if (!configs.persistCachedTree &&
browser.storage.session) {
browser.windows.getAll().then(windows => {
for (const win of windows) {
browser.sessions.removeWindowValue(win.id, Constants.kWINDOW_STATE_CACHED_TABS).catch(ApiTabs.createErrorSuppressor());
browser.sessions.removeWindowValue(win.id, Constants.kWINDOW_STATE_CACHED_SIDEBAR_TABS_DIRTY).catch(ApiTabs.createErrorSuppressor());
}
});
}
}


Expand Down Expand Up @@ -287,6 +297,20 @@ function fixupTabRestoredFromCachePostProcess(tab) {
async function updateWindowCache(owner, key, value) {
if (!owner)
return;

if (!configs.persistCachedTree &&
browser.storage.session) {
const storagKey = `backgroundCache-window${owner.windowId}-${key}`;
if (value) {
const data = {};
data[storagKey] = value;
return browser.storage.session.set(data);
}
else {
return browser.storage.session.remove(storagKey);
}
}

if (value === undefined) {
try {
return browser.sessions.removeWindowValue(owner.windowId, key).catch(ApiTabs.createErrorSuppressor());
Expand Down Expand Up @@ -318,6 +342,14 @@ export function markWindowCacheDirtyFromTab(tab, akey) {
}

async function getWindowCache(owner, key) {
if (!configs.persistCachedTree) {
const storageKey = `backgroundCache-window${owner.windowId}-${key}`;
const defaultData = {};
defaultData[storageKey] = undefined;
return browser.storage.session.get(defaultData).then(data => {
return data[storageKey];
});
}
return browser.sessions.getWindowValue(owner.windowId, key).catch(ApiTabs.createErrorHandler());
}

Expand Down Expand Up @@ -495,6 +527,7 @@ Tab.onHidden.addListener(tab => {
function onConfigChange(key) {
switch (key) {
case 'useCachedTree':
case 'persistCachedTree':
browser.windows.getAll({
populate: true,
windowTypes: ['normal']
Expand Down
1 change: 1 addition & 0 deletions webextensions/common/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ export const configs = new Configs({
notifiedFeaturesVersion: 0,

useCachedTree: true,
persistCachedTree: false,

// This should be removed after https://bugzilla.mozilla.org/show_bug.cgi?id=1388193
// or https://bugzilla.mozilla.org/show_bug.cgi?id=1421329 become fixed.
Expand Down
39 changes: 39 additions & 0 deletions webextensions/sidebar/sidebar-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ let mTabBar;
export function init() {
mTargetWindow = TabsStore.getCurrentWindowId();
mTabBar = document.querySelector('#tabbar');

if (!configs.persistCachedTree)
clearPersistentWindowCache();
}

export function startTracking() {
Expand Down Expand Up @@ -433,6 +436,20 @@ async function fixupTabsRestoredFromCache(tabElements, tabs, options = {}) {
// ===================================================================

function updateWindowCache(key, value) {
if (!configs.persistCachedTree &&
browser.storage.session) {
const storagKey = `sidebarCache-window${mTargetWindow}-${key}`;
if (value) {
const data = {};
data[storagKey] = value;
browser.storage.session.set(data);
}
else {
browser.storage.session.remove(storagKey);
}
return;
}

if (!mLastWindowCacheOwner ||
!Tab.get(mLastWindowCacheOwner.id))
return;
Expand All @@ -448,11 +465,23 @@ function updateWindowCache(key, value) {

function clearWindowCache() {
log('clearWindowCache ', { stack: configs.debug && new Error().stack });
if (!configs.persistCachedTree)
clearPersistentWindowCache();
updateWindowCache(Constants.kWINDOW_STATE_CACHED_SIDEBAR);
updateWindowCache(Constants.kWINDOW_STATE_CACHED_SIDEBAR_TABS_DIRTY);
updateWindowCache(Constants.kWINDOW_STATE_CACHED_SIDEBAR_COLLAPSED_DIRTY);
}

function clearPersistentWindowCache() {
if (!mLastWindowCacheOwner ||
!Tab.get(mLastWindowCacheOwner.id))
return;
log('clearPersistentWindowCache ', { stack: configs.debug && new Error().stack });
browser.sessions.removeWindowValue(mLastWindowCacheOwner.windowId, Constants.kWINDOW_STATE_CACHED_SIDEBAR).catch(ApiTabs.createErrorSuppressor());
browser.sessions.removeWindowValue(mLastWindowCacheOwner.windowId, Constants.kWINDOW_STATE_CACHED_SIDEBAR_TABS_DIRTY).catch(ApiTabs.createErrorSuppressor());
browser.sessions.removeWindowValue(mLastWindowCacheOwner.windowId, Constants.kWINDOW_STATE_CACHED_SIDEBAR_COLLAPSED_DIRTY).catch(ApiTabs.createErrorSuppressor());
}

export function markWindowCacheDirty(key) {
if (markWindowCacheDirty.timeout)
clearTimeout(markWindowCacheDirty.timeout);
Expand All @@ -463,6 +492,15 @@ export function markWindowCacheDirty(key) {
}

async function getWindowCache(key) {
if (!configs.persistCachedTree) {
const storageKey = `sidebarCache-window${mTargetWindow}-${key}`;
const defaultData = {};
defaultData[storageKey] = undefined;
return browser.storage.session.get(defaultData).then(data => {
return data[storageKey];
});
}

if (!mLastWindowCacheOwner)
return null;
return browser.sessions.getWindowValue(mLastWindowCacheOwner.windowId, key).catch(ApiTabs.createErrorHandler());
Expand Down Expand Up @@ -527,6 +565,7 @@ async function updateCachedTabbar() {
function onConfigChange(changedKey) {
switch (changedKey) {
case 'useCachedTree':
case 'persistCachedTree':
if (configs[changedKey]) {
reserveToUpdateCachedTabbar();
}
Expand Down

0 comments on commit 23a25a1

Please sign in to comment.