Skip to content

Commit 7c5a7db

Browse files
committed
Refactor background to use new per domain isolation structure (stoically#240)
1 parent 027c897 commit 7c5a7db

12 files changed

+97
-59
lines changed

Diff for: .eslintrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
},
88
"extends": "eslint:recommended",
99
"parserOptions": {
10-
"ecmaVersion": 8
10+
"ecmaVersion": 2018
1111
},
1212
"rules": {
1313
"indent": [

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"build-sign": "rm -rf web-ext-artifacts .cache dist && node build-sign.js && npm run lint && npm test && parcel build src/ui/options.html src/ui/popup.html --no-minify --no-source-maps && cp README.md LICENSE dist && web-ext sign --channel unlisted -s dist && git checkout -- src/manifest.json && ncu",
99
"lint": "eslint src/**/*.{js,vue}",
1010
"test": "nyc --reporter=html --reporter=text mocha --reporter=dot ./test/setup.js test/*.test.js --timeout 60000",
11-
"test-watch": "mocha ./test/setup.js test/*.test.js --watch --tmp-debug --inspect",
11+
"test-watch": "mocha ./test/setup.js test/*.test.js -b --watch --tmp-debug --inspect",
1212
"test-functional": "mocha ./test/functional/*.test.js --timeout 10000",
1313
"check-dependencies": "ncu",
1414
"watch": "rm -rf dist && parcel src/ui/options.html src/ui/popup.html --no-hmr"

Diff for: src/background/container.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ class Container {
313313

314314
async tryToRemoveQueue(queue) {
315315
debug('[tryToRemoveQueue] removal queue', queue);
316-
for (let cookieStoreId of queue) {
316+
for (const cookieStoreId of queue) {
317317
if (!this.storage.local.tempContainers[cookieStoreId]) {
318318
debug('[tryToRemoveQueue] unknown container, probably already removed', cookieStoreId);
319319
continue;
@@ -597,7 +597,7 @@ class Container {
597597
const containersOptions = Object.values(this.storage.local.tempContainers);
598598
const assignedColors = {};
599599
let maxColors = 0;
600-
for (let containerOptions of containersOptions) {
600+
for (const containerOptions of containersOptions) {
601601
if (typeof containerOptions !== 'object') {
602602
continue;
603603
}
@@ -610,7 +610,7 @@ class Container {
610610
}
611611
}
612612

613-
for (let color of this.containerColors) {
613+
for (const color of this.containerColors) {
614614
if (!assignedColors[color] || assignedColors[color] < maxColors) {
615615
availableColors.push(color);
616616
}

Diff for: src/background/cookies.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Cookies {
2929
let cookieHeader;
3030
let cookiesHeader = {};
3131
let cookieHeaderChanged = false;
32-
for (let domainPattern in this.storage.local.preferences.cookies.domain) {
32+
for (const domainPattern in this.storage.local.preferences.cookies.domain) {
3333
if (!this.isolation.matchDomainPattern(details.url, domainPattern)) {
3434
continue;
3535
}
@@ -53,7 +53,7 @@ class Cookies {
5353
debug('[maybeAddCookiesToHeader] found temp tab and header', details, cookieHeader, cookiesHeader);
5454
}
5555

56-
for (let cookie of this.storage.local.preferences.cookies.domain[domainPattern]) {
56+
for (const cookie of this.storage.local.preferences.cookies.domain[domainPattern]) {
5757
if (!cookie) {
5858
continue;
5959
}

Diff for: src/background/isolation.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,9 @@ class Isolation {
151151
const parsedURL = new URL(url);
152152
const parsedRequestURL = new URL(request.url);
153153

154-
for (let domainPattern in this.storage.local.preferences.isolation.domain) {
155-
const patternPreferences = this.storage.local.preferences.isolation.domain[domainPattern];
154+
for (const patternPreferences of this.storage.local.preferences.isolation.domain) {
155+
const domainPattern = patternPreferences.pattern;
156+
156157
if (!this.matchDomainPattern(
157158
tab.url === 'about:blank' && openerTab && openerTab.url || tab.url, domainPattern
158159
)) {
@@ -202,8 +203,9 @@ class Isolation {
202203
return false;
203204
}
204205

205-
for (let domainPattern in this.storage.local.preferences.isolation.domain) {
206-
const patternPreferences = this.storage.local.preferences.isolation.domain[domainPattern];
206+
for (const patternPreferences of this.storage.local.preferences.isolation.domain) {
207+
const domainPattern = patternPreferences.pattern;
208+
207209
if (!this.matchDomainPattern(request.url, domainPattern)) {
208210
continue;
209211
}

Diff for: src/background/mouseclick.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,15 @@ class MouseClick {
101101
const parsedClickedURL = new URL(message.href);
102102
debug('[checkClick] checking click', type, message, sender);
103103

104-
for (let domainPattern in this.storage.local.preferences.isolation.domain) {
104+
for (const domainPatternPreferences of this.storage.local.preferences.isolation.domain) {
105+
const domainPattern = domainPatternPreferences.pattern;
105106
if (!this.isolation.matchDomainPattern(sender.tab.url, domainPattern)) {
106107
continue;
107108
}
108-
const domainPatternPreferences = this.storage.local.preferences.isolation.domain[domainPattern].mouseClick;
109-
if (!domainPatternPreferences[type]) {
109+
if (!domainPatternPreferences.mouseClick[type]) {
110110
continue;
111111
}
112-
const preferences = domainPatternPreferences[type];
112+
const preferences = domainPatternPreferences.mouseClick[type];
113113
debug('[checkClick] per website pattern found', );
114114
if (preferences.action === 'global') {
115115
debug('[checkClick] breaking because "global"');

Diff for: src/ui/components/isolation/perdomain.vue

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export default {
118118
this.editing = false;
119119
} else {
120120
this.preferences.isolation.domain.push(JSON.parse(JSON.stringify(this.domain)));
121+
this.reset();
121122
}
122123
}
123124
});

Diff for: test/background.alwaysopenin.test.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@ preferencesTestSet.map(preferences => { describe(`preferences: ${JSON.stringify(
33
describe('Always Open In', () => {
44
beforeEach(async () => {
55
global.background = await loadBackground(preferences);
6-
background.storage.local.preferences.isolation.domain = {
7-
'example.com': {
6+
background.storage.local.preferences.isolation.domain = [
7+
{
8+
pattern: 'example.com',
89
always: {
910
action: 'enabled',
1011
allowedInPermanent: false
1112
}
1213
},
13-
'*.notexample.com': {
14+
{
15+
pattern: '*.notexample.com',
1416
always: {
1517
action: 'enabled',
1618
allowedInPermanent: false
1719
}
1820
}
19-
};
21+
];
2022
});
2123

2224
it('should open in a new temporary container', async () => {
@@ -57,14 +59,15 @@ preferencesTestSet.map(preferences => { describe(`preferences: ${JSON.stringify(
5759
});
5860

5961
it('should not open in a new temporary container if its allowed in permanent container', async () => {
60-
background.storage.local.preferences.isolation.domain = {
61-
'example.com': {
62+
background.storage.local.preferences.isolation.domain = [
63+
{
64+
pattern: 'example.com',
6265
always: {
6366
action: 'enabled',
6467
allowedInPermanent: true
6568
}
6669
}
67-
};
70+
];
6871
await helper.browser.request({
6972
tabId: 2,
7073
tabUrl: 'about:newtab',

Diff for: test/background.isolation.test.js

+41-17
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,13 @@ preferencesTestSet.map(preferences => { describe(`preferences: ${JSON.stringify(
8181

8282
case 'sametab.perdomain':
8383
case 'newtab.perdomain':
84-
background.storage.local.preferences.isolation.domain['example.com'] = defaultIsolationDomainPreferences;
85-
background.storage.local.preferences.isolation.domain['example.com'].navigation.action = 'never';
84+
background.storage.local.preferences.isolation.domain = [{
85+
pattern: 'example.com',
86+
...defaultIsolationDomainPreferences,
87+
navigation: {
88+
action: 'never'
89+
}
90+
}];
8691
break;
8792
}
8893
});
@@ -128,8 +133,13 @@ preferencesTestSet.map(preferences => { describe(`preferences: ${JSON.stringify(
128133

129134
case 'sametab.perdomain':
130135
case 'newtab.perdomain':
131-
background.storage.local.preferences.isolation.domain['example.com'] = defaultIsolationDomainPreferences;
132-
background.storage.local.preferences.isolation.domain['example.com'].navigation.action = 'always';
136+
background.storage.local.preferences.isolation.domain = [{
137+
pattern: 'example.com',
138+
...defaultIsolationDomainPreferences,
139+
navigation: {
140+
action: 'always'
141+
}
142+
}];
133143
break;
134144
}
135145
});
@@ -174,8 +184,13 @@ preferencesTestSet.map(preferences => { describe(`preferences: ${JSON.stringify(
174184

175185
case 'sametab.perdomain':
176186
case 'newtab.perdomain':
177-
background.storage.local.preferences.isolation.domain['example.com'] = defaultIsolationDomainPreferences;
178-
background.storage.local.preferences.isolation.domain['example.com'].excluded['excluded.com'] = {};
187+
background.storage.local.preferences.isolation.domain = [{
188+
pattern: 'example.com',
189+
...defaultIsolationDomainPreferences,
190+
excluded: {
191+
'excluded.com': {}
192+
}
193+
}];
179194
break;
180195
}
181196

@@ -198,8 +213,13 @@ preferencesTestSet.map(preferences => { describe(`preferences: ${JSON.stringify(
198213

199214
case 'sametab.perdomain':
200215
case 'newtab.perdomain':
201-
background.storage.local.preferences.isolation.domain['example.com'] = defaultIsolationDomainPreferences;
202-
background.storage.local.preferences.isolation.domain['example.com'].navigation.action = 'notsamedomain';
216+
background.storage.local.preferences.isolation.domain = [{
217+
pattern: 'example.com',
218+
...defaultIsolationDomainPreferences,
219+
navigation: {
220+
action: 'notsamedomain'
221+
}
222+
}];
203223
break;
204224
}
205225
});
@@ -258,8 +278,13 @@ preferencesTestSet.map(preferences => { describe(`preferences: ${JSON.stringify(
258278

259279
case 'sametab.perdomain':
260280
case 'newtab.perdomain':
261-
background.storage.local.preferences.isolation.domain['example.com'] = defaultIsolationDomainPreferences;
262-
background.storage.local.preferences.isolation.domain['example.com'].navigation.action = 'notsamedomainexact';
281+
background.storage.local.preferences.isolation.domain = [{
282+
pattern: 'example.com',
283+
...defaultIsolationDomainPreferences,
284+
navigation: {
285+
action: 'notsamedomainexact'
286+
}
287+
}];
263288
break;
264289
}
265290
});
@@ -399,14 +424,13 @@ preferencesTestSet.map(preferences => { describe(`preferences: ${JSON.stringify(
399424
});
400425

401426
it('should not open in a new temporary container if the opener tab url belonging to the request matches the pattern', async () => {
402-
background.storage.local.preferences.isolation.domain = {
403-
'example.com': {
404-
always: {
405-
action: 'enabled',
406-
allowedInPermanent: false
407-
}
427+
background.storage.local.preferences.isolation.domain = [{
428+
pattern: 'example.com',
429+
always: {
430+
action: 'enabled',
431+
allowedInPermanent: false
408432
}
409-
};
433+
}];
410434

411435
await browser.tabs._create({
412436
url: 'https://example.com',

0 commit comments

Comments
 (0)