Skip to content

Commit 3a2d9ba

Browse files
committed
disable style cache in db
1 parent 8366089 commit 3a2d9ba

File tree

4 files changed

+13
-118
lines changed

4 files changed

+13
-118
lines changed

src/background/style-manager/cache.js

Lines changed: 4 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import {bgBusy} from '../common';
2-
import {cacheDB, db as styleDB} from '../db';
3-
import {bgMortal, bgMortalChanged} from '../tab-manager';
42

53
let onDeleted;
64
let timer;
@@ -12,73 +10,20 @@ const toWrite = new Set();
1210

1311
export default cache;
1412

15-
if (__.MV3) {
16-
bgMortalChanged.add(val => {
17-
if (val) cacheDB.putMany(Object.values(cache));
18-
else cacheDB.clear();
19-
});
20-
}
21-
2213
/** @param {MatchCache.Entry} val
2314
* @return {void} */
2415
export function add(val) {
25-
cache.set(val.url, bgMortal ? hit(val) : val);
16+
cache.set(val.url, hit(val));
2617
if (cache.size >= MAX) prune();
2718
}
2819

2920
/** @return {void} */
3021
export function clear() {
3122
if (onDeleted) cache.forEach(onDeleted);
32-
if (__.MV3 && bgMortal) cacheDB.clear();
3323
if (timer) timer = clearTimeout(timer);
3424
cache.clear();
3525
}
3626

37-
/** @param {string} url
38-
* @return {Promise<?MatchCache.Entry>} */
39-
export async function loadOne(url) {
40-
const val = await cacheDB.get(url);
41-
if (val) {
42-
cache.set(url, hit(val));
43-
const styleIds = Object.keys(val.sections ??= {}).map(Number);
44-
const styles = styleIds.length ? await styleDB.getMany(styleIds) : [];
45-
for (const style of styles) {
46-
if (!style || !make(val, style)) {
47-
del([val]);
48-
return;
49-
}
50-
}
51-
}
52-
return val;
53-
}
54-
55-
/** @return {Promise<void>} */
56-
export async function loadAll() {
57-
for (const val of await cacheDB.getAll()) {
58-
if (!cache.has(val.url)) {
59-
cache.set(val.url, val);
60-
}
61-
}
62-
}
63-
64-
/** @param {StyleDataMap} dataMap
65-
* @return {void} */
66-
export function hydrate(dataMap) {
67-
const toDel = [];
68-
for (const val of cache.values()) {
69-
for (const id in (val.sections ??= {})) {
70-
const data = dataMap.get(+id);
71-
if (!data || !make(val, data.style)) {
72-
toDel.push(val);
73-
break;
74-
} else {
75-
data.appliesTo.add(val.url);
76-
}
77-
}
78-
}
79-
if (toDel[0]) del(toDel);
80-
}
81-
8227
/**
8328
* @param {MatchCache.DbEntry} entry
8429
* @param {StyleObj} style
@@ -122,39 +67,12 @@ function del(items) {
12267
cache.delete(items[i] = val.url);
12368
onDeleted(val);
12469
}
125-
if (__.MV3 && bgMortal) cacheDB.deleteMany(items);
12670
}
12771

12872
/** @return {void} */
12973
function flush() {
130-
const bare = [];
131-
let toDel;
132-
nextEntry:
133-
for (const val of toWrite) {
134-
const {d, url, sections} = val;
135-
/** @type {MatchCache.IndexMap} */
136-
const indexes = {};
137-
/** @type {MatchCache.DbEntry} */
138-
const res = {};
139-
let styleId;
140-
for (styleId in sections) {
141-
/** @type {Injection.Sections | MatchCache.Index} */
142-
const sec = sections[styleId];
143-
const idx = sec && (Array.isArray(sec) ? sec : sec.idx);
144-
if (!idx) {
145-
(toDel ??= []).push(val);
146-
continue nextEntry;
147-
}
148-
indexes[styleId] = idx;
149-
}
150-
// Adding the meaningful props first to ensure their visibility in devtools DB viewer
151-
if (styleId) res.sections = indexes;
152-
res.d = [d?.[1] || 0, new Date()];
153-
res.url = url;
154-
bare.push(res);
155-
}
156-
if (toDel) del(toDel);
157-
if (__.MV3) cacheDB.putMany(bare);
74+
for (const val of toWrite)
75+
val.d = [val.d?.[1] || 0, new Date()];
15876
toWrite.clear();
15977
timer = null;
16078
}
@@ -170,7 +88,7 @@ async function flushLater() {
17088
* @param {T} val
17189
* @return {T} */
17290
export function hit(val) {
173-
if (val && bgMortal) {
91+
if (val) {
17492
toWrite.add(val);
17593
if (!timer) flushLater();
17694
}

src/background/style-manager/init.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {STORAGE_KEY} from '@/js/prefs';
44
import * as colorScheme from '../color-scheme';
55
import {bgBusy, bgInit, onSchemeChange} from '../common';
66
import {db, draftsDB, execMirror, prefsDB} from '../db';
7-
import {bgMortal} from '../tab-manager';
87
import * as styleCache from './cache';
98
import './init';
109
import {fixKnownProblems} from './fixer';
@@ -16,15 +15,13 @@ bgInit.push(async () => {
1615
let [orderFromDb, styles] = await Promise.all([
1716
prefsDB.get(kInjectionOrder),
1817
db.getAll(),
19-
__.MV3 && bgMortal ? styleCache.loadAll() : [],
2018
]);
2119
if (!orderFromDb)
2220
orderFromDb = await execMirror(STORAGE_KEY, 'get', kInjectionOrder);
2321
if (!styles[0])
2422
styles = mirrored = await execMirror(DB, 'getAll');
2523
setOrderImpl(orderFromDb, {store: false});
2624
initStyleMap(styles, mirrored);
27-
styleCache.hydrate(dataMap);
2825
__.DEBUGLOG('styleMan init done');
2926
});
3027

src/background/style-via-webrequest.js

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ import * as prefs from '@/js/prefs';
55
import {CHROME, FIREFOX} from '@/js/ua';
66
import {actionPopupUrl, ownRoot} from '@/js/urls';
77
import {deepEqual, isEmptyObj} from '@/js/util';
8-
import {ignoreChromeError, ownId, toggleListener} from '@/js/util-webext';
8+
import {ownId, toggleListener} from '@/js/util-webext';
99
import * as colorScheme from './color-scheme';
1010
import {bgBusy, bgPreInit, clientDataJobs, dataHub, onUnload} from './common';
1111
import {stateDB} from './db';
1212
import {webNavigation} from './navigation-manager';
1313
import offscreen from './offscreen';
1414
import makePopupData from './popup-data';
1515
import {getSectionsByUrl} from './style-manager';
16-
import * as styleCache from './style-manager/cache';
1716
import tabCache, * as tabMan from './tab-manager';
1817

1918
const idCSP = 'patchCsp';
@@ -136,32 +135,14 @@ function toggle(prefKey) {
136135
async function prepareStyles(req) {
137136
const {tabId, frameId, url} = req; if (tabId < 0) return;
138137
const key = tabId + ':' + frameId;
139-
const bgPreInitLen = __.MV3 && bgPreInit.length;
140-
const isDark = curXHR
141-
&& colorScheme.isSystem()
142-
&& !tabMan.someInjectable()
143-
&& colorScheme.refreshSystemDark();
144-
__.DEBUGLOG('prepareStyles', key, req, {isDark});
145-
if (!curXHR && !curCSP && !bgBusy) return;
146-
let cached, unlock;
147-
let lock = __.MV3 && bgPreInitLen && new Promise(resolve => (unlock = resolve));
148-
if (__.MV3 && bgPreInitLen) { // bgPreInit in progress, let's join it
149-
bgPreInit.push(
150-
styleCache.loadOne(url),
151-
frameId ? browser.tabs.get(tabId).catch(ignoreChromeError) : undefined,
152-
isDark,
153-
);
154-
const all = Promise.all(bgPreInit);
155-
if (lock) bgPreInit.push(lock); // keeps bgBusy from resolving until we're done here
156-
[cached, req.tab] = (await all).slice(bgPreInitLen);
157-
__.DEBUGLOG('prepareStyles cache', key, cached);
158-
}
159-
if (!cached && bgBusy) {
160-
if (lock) lock = unlock(); // set to undefined
138+
const isInit = bgBusy;
139+
__.DEBUGLOG('prepareStyles', key, req);
140+
if (!curXHR && !curCSP && !bgBusy)
141+
return;
142+
if (bgBusy)
161143
await bgBusy;
162-
} else if (!bgPreInitLen && isDark && __.MV3) {
163-
await isDark;
164-
}
144+
if (curXHR && colorScheme.isSystem() && (isInit || !tabMan.someInjectable()))
145+
await colorScheme.refreshSystemDark();
165146
const oldData = toSend[key];
166147
const data = oldData || {};
167148
const payload = data.payload = getSectionsByUrl.call({sender: req}, url, null, kStyleViaXhr);
@@ -172,7 +153,6 @@ async function prepareStyles(req) {
172153
await prepareStylesMV3(tabId, frameId, url, data, key, payload);
173154
}
174155
toSend[key] = data;
175-
if (lock) setTimeout(unlock);
176156
__.DEBUGLOG('prepareStyles done', key, data);
177157
}
178158

src/background/tab-manager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const putObject = obj => stateDB.putMany(
6060
);
6161

6262
export const bgMortalChanged = __.MV3 && new Set();
63-
export let bgMortal;
63+
let bgMortal;
6464

6565
bgInit.push(async () => {
6666
const [saved, tabs] = await Promise.all([

0 commit comments

Comments
 (0)