Skip to content

Commit c6ef239

Browse files
committed
Use IndexedDB to store caches #3434
1 parent 504cd51 commit c6ef239

File tree

9 files changed

+327
-122
lines changed

9 files changed

+327
-122
lines changed

webextensions/_locales/en/messages.json

-2
Original file line numberDiff line numberDiff line change
@@ -787,8 +787,6 @@
787787
"config_staticARIALabel_description": { "message": "*Please try checking this if your speech recognition system misses tab elements after some tab operations." },
788788
"config_useCachedTree_label": { "message": "Optimize tree restoration with cache" },
789789
"config_useCachedTree_description": { "message": "*Please try unchecking and re-checking this to refresh the cache, when the behavior around tree is unstable." },
790-
"config_persistCachedTree_label": { "message": "Persist tree cache" },
791-
"config_persistCachedTree_description": { "message": "*Initialization process at the browser startup may be optimized, but it will broat the size of browser's session file and increase disk I/O." },
792790
"config_acceleratedTabCreation_label": { "message": "Accelerate operations around newly opened tabs (*NOTE: You'll see unstable behavior around tabs.)" },
793791
"config_maximumAcceptableDelayForTabDuplication_before": { "message": "Abort tab duplication when it takes" },
794792
"config_maximumAcceptableDelayForTabDuplication_after": { "message": "milliseconds or more." },

webextensions/_locales/ja/messages.json

-2
Original file line numberDiff line numberDiff line change
@@ -784,8 +784,6 @@
784784
"config_staticARIALabel_description": { "message": "※何らかのタブ操作のあとで音声認識システムがタブ要素を見失ってしまう場合、このチェックをONにしてください。" },
785785
"config_useCachedTree_label": { "message": "キャッシュを使ってツリーの初期化を高速化する" },
786786
"config_useCachedTree_description": { "message": "※キャッシュの不整合で動作が不安定になっている場合、このチェックをOFFにして再度ONにすることでキャッシュが刷新され状況が改善するかもしれません。" },
787-
"config_persistCachedTree_label": { "message": "キャッシュを永続化する" },
788-
"config_persistCachedTree_description": { "message": "※ブラウザー起動時やアドオン更新時などの初期化処理を短縮できますが、ブラウザーのセッションファイルが肥大化しディスクI/Oが増加します" },
789787
"config_acceleratedTabCreation_label": { "message": "新しいタブが開かれた時の処理を高速化する(※動作が不安定になります)" },
790788
"config_maximumAcceptableDelayForTabDuplication_before": { "message": "\u200b" },
791789
"config_maximumAcceptableDelayForTabDuplication_after": { "message": "ミリ秒以内にタブの複製を完了できなかった場合は処理を中断する" },

webextensions/background/background-cache.js

+27-63
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ import {
1313
configs
1414
} from '/common/common.js';
1515
import * as ApiTabs from '/common/api-tabs.js';
16+
import * as CacheStorage from '/common/cache-storage.js';
1617
import * as Constants from '/common/constants.js';
1718
import * as MetricsData from '/common/metrics-data.js';
1819
import * as TabsInternalOperation from '/common/tabs-internal-operation.js';
1920
import * as TabsStore from '/common/tabs-store.js';
2021
import * as TabsUpdate from '/common/tabs-update.js';
21-
import * as UniqueId from '/common/unique-id.js';
2222

2323
import Tab from '/common/Tab.js';
2424

@@ -36,14 +36,13 @@ export function activate() {
3636
mActivated = true;
3737
configs.$addObserver(onConfigChange);
3838

39-
if (!configs.persistCachedTree) {
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-
}
39+
// clear obsolete cache
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+
});
4746
}
4847

4948

@@ -298,31 +297,19 @@ async function updateWindowCache(owner, key, value) {
298297
if (!owner)
299298
return;
300299

301-
if (!configs.persistCachedTree) {
302-
const storageKey = `backgroundCache-${await UniqueId.ensureWindowId(owner.windowId)}-${key}`;
303-
if (value)
304-
sessionStorage.setItem(storageKey, value);
305-
else
306-
sessionStorage.removeItem(storageKey);
307-
return;
308-
}
309-
310-
if (value === undefined) {
311-
try {
312-
return browser.sessions.removeWindowValue(owner.windowId, key).catch(ApiTabs.createErrorSuppressor());
313-
}
314-
catch(e) {
315-
console.log(new Error('fatal error: failed to delete window cache'), e, owner, key, value);
316-
}
317-
}
318-
else {
319-
try {
320-
return browser.sessions.setWindowValue(owner.windowId, key, value).catch(ApiTabs.createErrorSuppressor());
321-
}
322-
catch(e) {
323-
console.log(new Error('fatal error: failed to update window cache'), e, owner, key, value);
324-
}
325-
}
300+
if (value)
301+
CacheStorage.setValue({
302+
windowId: owner.windowId,
303+
key,
304+
value,
305+
store: CacheStorage.STORE_BACKGROUND_CACHES,
306+
});
307+
else
308+
CacheStorage.deleteValue({
309+
windowId: owner.windowId,
310+
key,
311+
store: CacheStorage.STORE_BACKGROUND_CACHES,
312+
});
326313
}
327314

328315
export function markWindowCacheDirtyFromTab(tab, akey) {
@@ -338,11 +325,11 @@ export function markWindowCacheDirtyFromTab(tab, akey) {
338325
}
339326

340327
async function getWindowCache(owner, key) {
341-
if (!configs.persistCachedTree) {
342-
const storageKey = `backgroundCache-${await UniqueId.ensureWindowId(owner.windowId)}-${key}`;
343-
return sessionStorage.getItem(storageKey);
344-
}
345-
return browser.sessions.getWindowValue(owner.windowId, key).catch(ApiTabs.createErrorHandler());
328+
return CacheStorage.getValue({
329+
windowId: owner.windowId,
330+
key,
331+
store: CacheStorage.STORE_BACKGROUND_CACHES,
332+
});
346333
}
347334

348335
function getWindowCacheOwner(windowId) {
@@ -516,36 +503,13 @@ Tab.onHidden.addListener(tab => {
516503
reserveToCacheTree(tab.windowId);
517504
});
518505

519-
browser.runtime.onMessage.addListener((message, _sender) => {
520-
switch (message && message.type) {
521-
case Constants.kCOMMAND_GET_ON_MEMORY_CACHE:
522-
return sessionStorage.getItem(message.key);
523-
524-
case Constants.kCOMMAND_SET_ON_MEMORY_CACHE:
525-
if (message.value)
526-
sessionStorage.setItem(message.key, message.value);
527-
else
528-
sessionStorage.removeItem(message.key);
529-
return;
530-
531-
default:
532-
return;
533-
}
534-
});
535-
536506
browser.windows.onRemoved.addListener(async windowId => {
537-
const storageKeyPart = `Cache-${await UniqueId.ensureWindowId(windowId)}-`;
538-
for (let i = sessionStorage.length; i > -1; i--) {
539-
const key = sessionStorage.key(i);
540-
if (key.includes(storageKeyPart))
541-
sessionStorage.removeItem(key);
542-
}
507+
CacheStorage.clearForWindow(windowId);
543508
});
544509

545510
function onConfigChange(key) {
546511
switch (key) {
547512
case 'useCachedTree':
548-
case 'persistCachedTree':
549513
browser.windows.getAll({
550514
populate: true,
551515
windowTypes: ['normal']

webextensions/background/browser-action-menu.js

-5
Original file line numberDiff line numberDiff line change
@@ -1911,11 +1911,6 @@ const mItems = [
19111911
key: 'useCachedTree',
19121912
type: 'checkbox'
19131913
},
1914-
{
1915-
title: indent(2) + browser.i18n.getMessage('config_persistCachedTree_label'),
1916-
key: 'persistCachedTree',
1917-
type: 'checkbox'
1918-
},
19191914
{
19201915
title: indent() + browser.i18n.getMessage('config_supportTabsMultiselect_label'),
19211916
key: 'supportTabsMultiselect',

0 commit comments

Comments
 (0)