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

Commit a417d65

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 59d1627 commit a417d65

File tree

5 files changed

+72
-28
lines changed

5 files changed

+72
-28
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} = 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')
@@ -35,7 +35,10 @@ const historyState = require('../common/state/historyState')
3535
const bookmarkOrderCache = require('../common/cache/bookmarkOrderCache')
3636

3737
let currentPartitionNumber = 0
38-
const incrementPartitionNumber = () => ++currentPartitionNumber
38+
let currentPrivatePartitionNumber = 0
39+
const incrementPartitionNumber = (isPrivate) => isPrivate
40+
? ++currentPrivatePartitionNumber
41+
: ++currentPartitionNumber
3942

4043
const normalizeUrl = function (url) {
4144
if (isSourceAboutUrl(url)) {
@@ -70,28 +73,26 @@ const updateTab = (tabId, changeInfo = {}) => {
7073
}
7174
}
7275

73-
const getPartitionNumber = (partition) => {
74-
return Number(partition.split('persist:partition-')[1] || 0)
75-
}
76-
7776
/**
7877
* Obtains the current partition.
7978
* Warning: This function has global side effects in that it increments the
8079
* global next partition number if isPartitioned is passed into the create options.
8180
*/
8281
const getPartition = (createProperties) => {
83-
let partition = session.defaultSession.partition
8482
if (createProperties.partition) {
85-
partition = createProperties.partition
86-
} else if (createProperties.isPrivate) {
87-
partition = 'default'
88-
} else if (createProperties.isPartitioned) {
89-
partition = `persist:partition-${incrementPartitionNumber()}`
90-
} else if (createProperties.partitionNumber) {
91-
partition = `persist:partition-${createProperties.partitionNumber}`
83+
return createProperties.partition
9284
}
93-
94-
return partition
85+
if (createProperties.partitionNumber) {
86+
return getPartitionFromNumber(createProperties.partitionNumber,
87+
createProperties.isPrivate)
88+
}
89+
if (createProperties.isPrivate) {
90+
return getPartitionFromNumber(incrementPartitionNumber(true), true)
91+
}
92+
if (createProperties.isPartitioned) {
93+
return getPartitionFromNumber(incrementPartitionNumber(false), false)
94+
}
95+
return session.defaultSession.partition
9596
}
9697

9798
const needsPartitionAssigned = (createProperties) => {

app/renderer/components/main/siteInfo.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class SiteInfo extends React.Component {
109109
}
110110

111111
get partitionInfo () {
112-
if (this.props.partitionNumber) {
112+
if (this.props.partitionNumber > 0) {
113113
// Figure out the partition info display
114114
let l10nArgs = {
115115
partitionNumber: this.props.partitionNumber

app/renderer/components/tabs/tab.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ class Tab extends React.Component {
257257
props.isNarrowestView = tabContentState.isNarrowestView(currentWindow, props.frameKey)
258258
props.isPlayIndicatorBreakpoint = tabContentState.isMediumView(currentWindow, props.frameKey) || props.isNarrowView
259259
props.title = frame.get('title')
260-
props.showSessionIcon = partition && hasSeconardImage
260+
props.showSessionIcon = partition > 0 && hasSeconardImage
261261
props.showPrivateIcon = props.isPrivateTab && hasSeconardImage
262262
props.showFavIcon = !((hasBreakpoint(breakpoint, 'extraSmall') && props.isActive) || frame.get('location') === 'about:newtab')
263263
props.showAudioIcon = breakpoint === 'default' && !!frame.get('audioPlaybackActive')

js/state/frameStateUtil.js

+17-10
Original file line numberDiff line numberDiff line change
@@ -278,30 +278,37 @@ function isAncestorFrameKey (state, frame, parentFrameKey) {
278278
}
279279

280280
function getPartitionNumber (partition) {
281-
const regex = /(?:persist:)?partition-(\d+)/
281+
const regex = /^(?:persist:)?partition-(\d+)$/
282282
const matches = regex.exec(partition)
283-
return Number((matches && matches[1]) || 0)
283+
const partitionNumber = Number((matches && matches[1]) || 0)
284+
// Return negative numbers for private sessions to avoid conflicts with
285+
// regular sessions
286+
return isPrivatePartition(partition) ? -partitionNumber : partitionNumber
284287
}
285288

286289
function isPrivatePartition (partition) {
287-
return partition && !partition.startsWith('persist:')
290+
if (partition === '') {
291+
return true
292+
}
293+
if (partition) {
294+
return !partition.startsWith('persist:')
295+
}
296+
return false
288297
}
289298

290299
function isSessionPartition (partition) {
291-
return partition && partition.startsWith('persist:partition-')
300+
return partition &&
301+
(partition.startsWith('persist:partition-') || partition.startsWith('partition-'))
292302
}
293303

294304
function getPartition (frameOpts) {
295305
return getPartitionFromNumber(frameOpts.get('partitionNumber'), frameOpts.get('isPrivate'))
296306
}
297307

298308
function getPartitionFromNumber (partitionNumber, incognito) {
299-
if (!partitionNumber && !incognito) {
300-
return 'persist:default'
301-
} else if (incognito) {
302-
return 'default'
303-
}
304-
return `persist:partition-${partitionNumber}`
309+
const partition = partitionNumber ? `partition-${Math.abs(partitionNumber)}` : 'default'
310+
const prefix = incognito ? '' : 'persist:'
311+
return `${prefix}${partition}`
305312
}
306313

307314
const frameOptsFromFrame = (frame) => {

test/unit/state/frameStateUtilTest.js

+36
Original file line numberDiff line numberDiff line change
@@ -411,4 +411,40 @@ describe('frameStateUtil', function () {
411411
assert.equal(result, 1)
412412
})
413413
})
414+
415+
describe('partition utils', function () {
416+
it('getPartitionNumber', function () {
417+
const getPartitionNumber = frameStateUtil.getPartitionNumber
418+
assert.equal(getPartitionNumber(''), 0)
419+
assert.equal(getPartitionNumber('1'), 0)
420+
assert.equal(getPartitionNumber('partition-1'), -1)
421+
assert.equal(getPartitionNumber('partition-222'), -222)
422+
assert.equal(getPartitionNumber('partition-222.1'), 0)
423+
assert.equal(getPartitionNumber('persist:partition-1'), 1)
424+
assert.equal(getPartitionNumber('unpersist:partition-1'), 0)
425+
assert.equal(getPartitionNumber('persist:partition-10'), 10)
426+
assert.equal(getPartitionNumber('persist:partition'), 0)
427+
})
428+
it('isPrivatePartition', function () {
429+
const isPrivatePartition = frameStateUtil.isPrivatePartition
430+
assert.equal(isPrivatePartition(''), true)
431+
assert.equal(isPrivatePartition('partition-1'), true)
432+
assert.equal(isPrivatePartition('persist:partition-1'), false)
433+
})
434+
it('isSessionPartition', function () {
435+
const isSessionPartition = frameStateUtil.isSessionPartition
436+
assert.equal(isSessionPartition(''), false)
437+
assert.equal(isSessionPartition('default'), false)
438+
assert.equal(isSessionPartition('partition-1'), true)
439+
assert.equal(isSessionPartition('persist:partition-1'), true)
440+
})
441+
it('getPartitionFromNumber', function () {
442+
const getPartitionFromNumber = frameStateUtil.getPartitionFromNumber
443+
assert.equal(getPartitionFromNumber(0, true), 'default')
444+
assert.equal(getPartitionFromNumber(0, false), 'persist:default')
445+
assert.equal(getPartitionFromNumber(1, true), 'partition-1')
446+
assert.equal(getPartitionFromNumber(-10, true), 'partition-10')
447+
assert.equal(getPartitionFromNumber(1, false), 'persist:partition-1')
448+
})
449+
})
414450
})

0 commit comments

Comments
 (0)