fix: use render client id to track deleted render process hosts (backport: 3-0-x) (#14557)

* fix: use render client id to track deleted render process hosts

* fix: use webContentsId with contextId together (#13749)
This commit is contained in:
Robo 2018-09-12 08:26:20 +05:30 committed by Shelley Vohr
parent 5e81d8dad9
commit 8d27657fa5
8 changed files with 41 additions and 47 deletions

View file

@ -2,6 +2,10 @@
const v8Util = process.atomBinding('v8_util')
const getOwnerKey = (webContents, contextId) => {
return `${webContents.id}-${contextId}`
}
class ObjectsRegistry {
constructor () {
this.nextId = 0
@ -11,7 +15,7 @@ class ObjectsRegistry {
this.storage = {}
// Stores the IDs of objects referenced by WebContents.
// (webContentsContextId) => [id]
// (ownerKey) => [id]
this.owners = {}
}
@ -22,10 +26,10 @@ class ObjectsRegistry {
const id = this.saveToStorage(obj)
// Add object to the set of referenced objects.
const webContentsContextId = `${webContents.id}-${contextId}`
let owner = this.owners[webContentsContextId]
const ownerKey = getOwnerKey(webContents, contextId)
let owner = this.owners[ownerKey]
if (!owner) {
owner = this.owners[webContentsContextId] = new Set()
owner = this.owners[ownerKey] = new Set()
this.registerDeleteListener(webContents, contextId)
}
if (!owner.has(id)) {
@ -46,8 +50,8 @@ class ObjectsRegistry {
// Note that an object may be double-freed (cleared when page is reloaded, and
// then garbage collected in old page).
remove (webContents, contextId, id) {
const webContentsContextId = `${webContents.id}-${contextId}`
let owner = this.owners[webContentsContextId]
const ownerKey = getOwnerKey(webContents, contextId)
let owner = this.owners[ownerKey]
if (owner) {
// Remove the reference in owner.
owner.delete(id)
@ -58,13 +62,13 @@ class ObjectsRegistry {
// Clear all references to objects refrenced by the WebContents.
clear (webContents, contextId) {
const webContentsContextId = `${webContents.id}-${contextId}`
let owner = this.owners[webContentsContextId]
const ownerKey = getOwnerKey(webContents, contextId)
let owner = this.owners[ownerKey]
if (!owner) return
for (let id of owner) this.dereference(id)
delete this.owners[webContentsContextId]
delete this.owners[ownerKey]
}
// Private: Saves the object into storage and assigns an ID for it.
@ -83,8 +87,6 @@ class ObjectsRegistry {
// Private: Dereference the object from store.
dereference (id) {
if (process.env.ELECTRON_DISABLE_REMOTE_DEREFERENCING) return
let pointer = this.storage[id]
if (pointer == null) {
return
@ -96,12 +98,13 @@ class ObjectsRegistry {
}
}
// Private: Clear the storage when renderer process is destoryed.
// Private: Clear the storage when renderer process is destroyed.
registerDeleteListener (webContents, contextId) {
// contextId => ${OSProcessId}-${contextCount}
const OSProcessId = contextId.split('-')[0]
const listener = (event, deletedProcessId, deletedOSProcessId) => {
if (deletedOSProcessId && deletedOSProcessId.toString() === OSProcessId) {
// contextId => ${processHostId}-${contextCount}
const processHostId = contextId.split('-')[0]
const listener = (event, deletedProcessHostId) => {
if (deletedProcessHostId &&
deletedProcessHostId.toString() === processHostId) {
webContents.removeListener('render-view-deleted', listener)
this.clear(webContents, contextId)
}