Skip to content
This repository was archived by the owner on Dec 11, 2019. It is now read-only.

Commit cd5ec5a

Browse files
committed
put private tabs in different sessions
partial fix for #6684 and #8963 TODO: Either the private tab sessions should be cleared and revert back to private session 1 when the last private tab is closed, or we need a private tab session manager menu. Currently the session will keep incrementing until the browser restarts. Test Plan: 1. open private tab 2. in the private tab, go to a site that requires login 3. login, then open a link from that site in a new private tab 4. you should also be logged in in the second private tab 5. open a new private tab 6. go to the same site. you should not be logged in.
1 parent 81b7b7f commit cd5ec5a

File tree

6 files changed

+107
-41
lines changed

6 files changed

+107
-41
lines changed

app/browser/tabs.js

+17-16
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const {app, BrowserWindow, extensions, session, ipcMain} = require('electron')
1313
const {makeImmutable, makeJS} = require('../common/state/immutableUtil')
1414
const {getTargetAboutUrl, getSourceAboutUrl, isSourceAboutUrl, newFrameUrl, isTargetAboutUrl, isIntermediateAboutPage, isTargetMagnetUrl, getSourceMagnetUrl} = require('../../js/lib/appUrlUtil')
1515
const {isURL, getUrlFromInput, toPDFJSLocation, getDefaultFaviconUrl, isHttpOrHttps, getLocationIfPDF} = require('../../js/lib/urlutil')
16-
const {isSessionPartition} = require('../../js/state/frameStateUtil')
16+
const {isSessionPartition, getPartitionNumber, getPartitionFromNumber} = require('../../js/state/frameStateUtil')
1717
const {getOrigin} = require('../../js/lib/urlutil')
1818
const {getSetting} = require('../../js/settings')
1919
const settings = require('../../js/constants/settings')
@@ -39,7 +39,10 @@ const ledgerState = require('../common/state/ledgerState')
3939
const {getWindow} = require('./windows')
4040

4141
let currentPartitionNumber = 0
42-
const incrementPartitionNumber = () => ++currentPartitionNumber
42+
let currentPrivatePartitionNumber = 0
43+
const incrementPartitionNumber = (isPrivate) => isPrivate
44+
? ++currentPrivatePartitionNumber
45+
: ++currentPartitionNumber
4346

4447
const normalizeUrl = function (url) {
4548
if (isSourceAboutUrl(url)) {
@@ -74,28 +77,26 @@ const updateTab = (tabId, changeInfo = {}) => {
7477
}
7578
}
7679

77-
const getPartitionNumber = (partition) => {
78-
return Number(partition.split('persist:partition-')[1] || 0)
79-
}
80-
8180
/**
8281
* Obtains the current partition.
8382
* Warning: This function has global side effects in that it increments the
8483
* global next partition number if isPartitioned is passed into the create options.
8584
*/
8685
const getPartition = (createProperties) => {
87-
let partition = session.defaultSession.partition
8886
if (createProperties.partition) {
89-
partition = createProperties.partition
90-
} else if (createProperties.isPrivate) {
91-
partition = 'default'
92-
} else if (createProperties.isPartitioned) {
93-
partition = `persist:partition-${incrementPartitionNumber()}`
94-
} else if (createProperties.partitionNumber) {
95-
partition = `persist:partition-${createProperties.partitionNumber}`
87+
return createProperties.partition
9688
}
97-
98-
return partition
89+
if (createProperties.partitionNumber) {
90+
return getPartitionFromNumber(createProperties.partitionNumber,
91+
createProperties.isPrivate)
92+
}
93+
if (createProperties.isPrivate) {
94+
return getPartitionFromNumber(incrementPartitionNumber(true), true)
95+
}
96+
if (createProperties.isPartitioned) {
97+
return getPartitionFromNumber(incrementPartitionNumber(false), false)
98+
}
99+
return session.defaultSession.partition
99100
}
100101

101102
const needsPartitionAssigned = (createProperties) => {

app/common/state/tabContentState/partitionState.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,7 @@ const frameStateUtil = require('../../../../js/state/frameStateUtil')
1010
const {tabs} = require('../../../../js/constants/config')
1111

1212
module.exports.isPartitionTab = (state, frameKey) => {
13-
const frame = frameStateUtil.getFrameByKey(state, frameKey)
14-
15-
if (frame == null) {
16-
return false
17-
}
18-
19-
return !!frame.get('partitionNumber')
13+
return module.exports.getPartitionNumber(state, frameKey) > 0
2014
}
2115

2216
module.exports.getPartitionNumber = (state, frameKey) => {
@@ -28,9 +22,9 @@ module.exports.getPartitionNumber = (state, frameKey) => {
2822

2923
const partitionNumber = frame.get('partitionNumber')
3024
if (typeof partitionNumber === 'string') {
31-
return partitionNumber.replace(/^partition-/i, '')
25+
return Number(partitionNumber.replace(/^partition-/i, ''))
3226
}
33-
return partitionNumber
27+
return Number(partitionNumber)
3428
}
3529

3630
module.exports.getMaxAllowedPartitionNumber = (state, frameKey) => {

app/renderer/components/main/siteInfo.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class SiteInfo extends React.Component {
105105
}
106106

107107
get partitionInfo () {
108-
if (this.props.partitionNumber) {
108+
if (this.props.partitionNumber > 0) {
109109
// Figure out the partition info display
110110
let l10nArgs = {
111111
partitionNumber: this.props.partitionNumber

js/state/frameStateUtil.js

+21-10
Original file line numberDiff line numberDiff line change
@@ -289,31 +289,42 @@ function isAncestorFrameKey (state, frame, parentFrameKey) {
289289
return isAncestorFrameKey(state, parentFrame, parentFrameKey)
290290
}
291291

292+
/**
293+
* @param {string} partition - the name of the partition
294+
* @returns {number}
295+
*/
292296
function getPartitionNumber (partition) {
293-
const regex = /(?:persist:)?partition-(\d+)/
297+
const regex = /^(?:persist:)?partition-(\d+)$/
294298
const matches = regex.exec(partition)
295-
return Number((matches && matches[1]) || 0)
299+
const partitionNumber = Number((matches && matches[1]) || 0)
300+
// Return negative numbers for private sessions to avoid conflicts with
301+
// regular sessions
302+
return isPrivatePartition(partition) ? -partitionNumber : partitionNumber
296303
}
297304

298305
function isPrivatePartition (partition) {
299-
return partition && !partition.startsWith('persist:')
306+
if (partition === '') {
307+
return true
308+
}
309+
if (partition) {
310+
return !partition.startsWith('persist:')
311+
}
312+
return false
300313
}
301314

302315
function isSessionPartition (partition) {
303-
return partition && partition.startsWith('persist:partition-')
316+
return partition &&
317+
(partition.startsWith('persist:partition-') || partition.startsWith('partition-'))
304318
}
305319

306320
function getPartition (frameOpts) {
307321
return getPartitionFromNumber(frameOpts.get('partitionNumber'), frameOpts.get('isPrivate'))
308322
}
309323

310324
function getPartitionFromNumber (partitionNumber, incognito) {
311-
if (!partitionNumber && !incognito) {
312-
return 'persist:default'
313-
} else if (incognito) {
314-
return 'default'
315-
}
316-
return `persist:partition-${partitionNumber}`
325+
const partition = partitionNumber ? `partition-${Math.abs(partitionNumber)}` : 'default'
326+
const prefix = incognito ? '' : 'persist:'
327+
return `${prefix}${partition}`
317328
}
318329

319330
const frameOptsFromFrame = (frame) => {

test/unit/app/common/state/tabContentStateTest/partitionStateTest.js

+29-5
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ describe('partitionState unit tests', function () {
5252
assert.equal(partitionState.isPartitionTab(), false)
5353
})
5454

55-
it('returns true if partition number is defined', function * () {
55+
it('returns true if partition number is positive', function * () {
5656
const partitionNumber = 1337
5757
const state = defaultState
5858
.setIn(['frames', index, 'partitionNumber'], partitionNumber)
@@ -65,15 +65,33 @@ describe('partitionState unit tests', function () {
6565
const result = partitionState.isPartitionTab(state, frameKey)
6666
assert.equal(result, false)
6767
})
68+
69+
it('returns false if partition number is 0', function * () {
70+
const partitionNumber = 0
71+
const state = defaultState
72+
.setIn(['frames', index, 'partitionNumber'], partitionNumber)
73+
const result = partitionState.isPartitionTab(state, frameKey)
74+
assert.equal(result, false)
75+
})
76+
77+
it('returns false if partition number is negative', function * () {
78+
const partitionNumber = -10
79+
const state = defaultState
80+
.setIn(['frames', index, 'partitionNumber'], partitionNumber)
81+
const result = partitionState.isPartitionTab(state, frameKey)
82+
assert.equal(result, false)
83+
})
6884
})
6985

7086
describe('getPartitionNumber', function () {
7187
it('returns zero if frame is null/undefined', function * () {
7288
assert.equal(partitionState.getPartitionNumber(), 0)
7389
})
7490

75-
it('returns zero if frame is null/undefined', function * () {
76-
assert.equal(partitionState.getPartitionNumber(), 0)
91+
it('returns zero if partition number is null/undefined', function * () {
92+
const state = defaultState
93+
.setIn(['frames', index, 'partitionNumber'], null)
94+
assert.equal(partitionState.getPartitionNumber(state, frameKey), 0)
7795
})
7896

7997
it('can remove _partition_ string and keep the partition number', function * () {
@@ -95,8 +113,14 @@ describe('partitionState unit tests', function () {
95113
})
96114

97115
describe('getMaxAllowedPartitionNumber', function () {
98-
it('returns false if frame is null/undefined', function () {
99-
assert.equal(partitionState.getMaxAllowedPartitionNumber(), false)
116+
it('returns zero if frame is null/undefined', function () {
117+
assert.equal(partitionState.getMaxAllowedPartitionNumber(), 0)
118+
})
119+
120+
it('returns zero if partition number is null/undefined', function * () {
121+
const state = defaultState
122+
.setIn(['frames', index, 'partitionNumber'], null)
123+
assert.equal(partitionState.getMaxAllowedPartitionNumber(state, frameKey), 0)
100124
})
101125

102126
it('returns partition number', function * () {

test/unit/state/frameStateUtilTest.js

+36
Original file line numberDiff line numberDiff line change
@@ -444,4 +444,40 @@ describe('frameStateUtil', function () {
444444
assert.equal(result, true)
445445
})
446446
})
447+
448+
describe('partition utils', function () {
449+
it('getPartitionNumber', function () {
450+
const getPartitionNumber = frameStateUtil.getPartitionNumber
451+
assert.equal(getPartitionNumber(''), 0)
452+
assert.equal(getPartitionNumber('1'), 0)
453+
assert.equal(getPartitionNumber('partition-1'), -1)
454+
assert.equal(getPartitionNumber('partition-222'), -222)
455+
assert.equal(getPartitionNumber('partition-222.1'), 0)
456+
assert.equal(getPartitionNumber('persist:partition-1'), 1)
457+
assert.equal(getPartitionNumber('unpersist:partition-1'), 0)
458+
assert.equal(getPartitionNumber('persist:partition-10'), 10)
459+
assert.equal(getPartitionNumber('persist:partition'), 0)
460+
})
461+
it('isPrivatePartition', function () {
462+
const isPrivatePartition = frameStateUtil.isPrivatePartition
463+
assert.equal(isPrivatePartition(''), true)
464+
assert.equal(isPrivatePartition('partition-1'), true)
465+
assert.equal(isPrivatePartition('persist:partition-1'), false)
466+
})
467+
it('isSessionPartition', function () {
468+
const isSessionPartition = frameStateUtil.isSessionPartition
469+
assert.equal(isSessionPartition(''), false)
470+
assert.equal(isSessionPartition('default'), false)
471+
assert.equal(isSessionPartition('partition-1'), true)
472+
assert.equal(isSessionPartition('persist:partition-1'), true)
473+
})
474+
it('getPartitionFromNumber', function () {
475+
const getPartitionFromNumber = frameStateUtil.getPartitionFromNumber
476+
assert.equal(getPartitionFromNumber(0, true), 'default')
477+
assert.equal(getPartitionFromNumber(0, false), 'persist:default')
478+
assert.equal(getPartitionFromNumber(1, true), 'partition-1')
479+
assert.equal(getPartitionFromNumber(-10, true), 'partition-10')
480+
assert.equal(getPartitionFromNumber(1, false), 'persist:partition-1')
481+
})
482+
})
447483
})

0 commit comments

Comments
 (0)