2016-03-24 20:15:04 +00:00
|
|
|
'use strict'
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
const v8Util = process.atomBinding('v8_util')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2018-07-24 07:21:38 +00:00
|
|
|
const getOwnerKey = (webContents, contextId) => {
|
|
|
|
return `${webContents.id}-${contextId}`
|
|
|
|
}
|
|
|
|
|
2016-02-22 06:10:25 +00:00
|
|
|
class ObjectsRegistry {
|
2016-03-24 20:15:04 +00:00
|
|
|
constructor () {
|
|
|
|
this.nextId = 0
|
2016-01-15 17:57:36 +00:00
|
|
|
|
2016-01-15 22:15:42 +00:00
|
|
|
// Stores all objects by ref-counting.
|
|
|
|
// (id) => {object, count}
|
2016-03-24 20:15:04 +00:00
|
|
|
this.storage = {}
|
2016-01-15 17:57:36 +00:00
|
|
|
|
2016-01-15 22:15:42 +00:00
|
|
|
// Stores the IDs of objects referenced by WebContents.
|
2018-07-24 07:21:38 +00:00
|
|
|
// (ownerKey) => [id]
|
2016-03-24 20:15:04 +00:00
|
|
|
this.owners = {}
|
2016-01-15 17:57:36 +00:00
|
|
|
}
|
|
|
|
|
2016-02-22 06:03:31 +00:00
|
|
|
// Register a new object and return its assigned ID. If the object is already
|
|
|
|
// registered then the already assigned ID would be returned.
|
2018-07-10 08:15:40 +00:00
|
|
|
add (webContents, contextId, obj) {
|
2016-02-22 06:03:31 +00:00
|
|
|
// Get or assign an ID to the object.
|
2016-12-01 00:07:02 +00:00
|
|
|
const id = this.saveToStorage(obj)
|
2016-01-15 22:15:42 +00:00
|
|
|
|
2016-02-22 06:03:31 +00:00
|
|
|
// Add object to the set of referenced objects.
|
2018-07-24 07:21:38 +00:00
|
|
|
const ownerKey = getOwnerKey(webContents, contextId)
|
|
|
|
let owner = this.owners[ownerKey]
|
2016-02-22 06:54:55 +00:00
|
|
|
if (!owner) {
|
2018-07-24 07:21:38 +00:00
|
|
|
owner = this.owners[ownerKey] = new Set()
|
2018-07-10 08:15:40 +00:00
|
|
|
this.registerDeleteListener(webContents, contextId)
|
2016-02-22 06:54:55 +00:00
|
|
|
}
|
2016-02-22 06:03:31 +00:00
|
|
|
if (!owner.has(id)) {
|
2016-03-24 20:15:04 +00:00
|
|
|
owner.add(id)
|
2016-02-22 06:03:31 +00:00
|
|
|
// Increase reference count if not referenced before.
|
2016-03-24 20:15:04 +00:00
|
|
|
this.storage[id].count++
|
2016-01-15 22:15:42 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
return id
|
2016-01-15 17:57:36 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 22:15:42 +00:00
|
|
|
// Get an object according to its ID.
|
2016-03-24 20:15:04 +00:00
|
|
|
get (id) {
|
2016-11-17 17:55:13 +00:00
|
|
|
const pointer = this.storage[id]
|
|
|
|
if (pointer != null) return pointer.object
|
2016-01-15 17:57:36 +00:00
|
|
|
}
|
2016-01-15 22:15:42 +00:00
|
|
|
|
|
|
|
// Dereference an object according to its ID.
|
2018-07-10 08:15:40 +00:00
|
|
|
// Note that an object may be double-freed (cleared when page is reloaded, and
|
|
|
|
// then garbage collected in old page).
|
2018-07-24 07:21:38 +00:00
|
|
|
remove (webContents, contextId, id) {
|
|
|
|
const ownerKey = getOwnerKey(webContents, contextId)
|
|
|
|
let owner = this.owners[ownerKey]
|
2016-04-21 02:17:12 +00:00
|
|
|
if (owner) {
|
2018-07-10 08:15:40 +00:00
|
|
|
// Remove the reference in owner.
|
2016-04-21 02:17:12 +00:00
|
|
|
owner.delete(id)
|
2018-07-10 08:15:40 +00:00
|
|
|
// Dereference from the storage.
|
|
|
|
this.dereference(id)
|
2016-04-21 02:17:12 +00:00
|
|
|
}
|
2016-01-15 17:57:36 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 22:15:42 +00:00
|
|
|
// Clear all references to objects refrenced by the WebContents.
|
2018-07-24 07:21:38 +00:00
|
|
|
clear (webContents, contextId) {
|
|
|
|
const ownerKey = getOwnerKey(webContents, contextId)
|
|
|
|
let owner = this.owners[ownerKey]
|
2016-03-24 20:15:04 +00:00
|
|
|
if (!owner) return
|
|
|
|
|
|
|
|
for (let id of owner) this.dereference(id)
|
|
|
|
|
2018-07-24 07:21:38 +00:00
|
|
|
delete this.owners[ownerKey]
|
2016-01-15 17:57:36 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 22:15:42 +00:00
|
|
|
// Private: Saves the object into storage and assigns an ID for it.
|
2016-03-24 20:15:04 +00:00
|
|
|
saveToStorage (object) {
|
|
|
|
let id = v8Util.getHiddenValue(object, 'atomId')
|
2016-01-15 22:15:42 +00:00
|
|
|
if (!id) {
|
2016-03-24 20:15:04 +00:00
|
|
|
id = ++this.nextId
|
2016-01-15 22:15:42 +00:00
|
|
|
this.storage[id] = {
|
|
|
|
count: 0,
|
|
|
|
object: object
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
|
|
|
v8Util.setHiddenValue(object, 'atomId', id)
|
2016-01-15 22:15:42 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
return id
|
2016-01-15 17:57:36 +00:00
|
|
|
}
|
2016-01-15 22:15:42 +00:00
|
|
|
|
|
|
|
// Private: Dereference the object from store.
|
2016-03-24 20:15:04 +00:00
|
|
|
dereference (id) {
|
|
|
|
let pointer = this.storage[id]
|
2016-01-15 22:15:42 +00:00
|
|
|
if (pointer == null) {
|
2016-03-24 20:15:04 +00:00
|
|
|
return
|
2016-01-15 22:15:42 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
pointer.count -= 1
|
2016-01-15 22:15:42 +00:00
|
|
|
if (pointer.count === 0) {
|
2016-03-24 20:15:04 +00:00
|
|
|
v8Util.deleteHiddenValue(pointer.object, 'atomId')
|
2016-12-01 00:07:02 +00:00
|
|
|
delete this.storage[id]
|
2016-01-15 22:15:42 +00:00
|
|
|
}
|
2016-01-15 17:57:36 +00:00
|
|
|
}
|
2016-12-01 00:59:59 +00:00
|
|
|
|
2018-07-24 07:21:38 +00:00
|
|
|
// Private: Clear the storage when renderer process is destroyed.
|
2018-07-10 08:15:40 +00:00
|
|
|
registerDeleteListener (webContents, contextId) {
|
2018-09-11 18:18:10 +00:00
|
|
|
// contextId => ${processHostId}-${contextCount}
|
|
|
|
const processHostId = contextId.split('-')[0]
|
|
|
|
const listener = (event, deletedProcessHostId) => {
|
|
|
|
if (deletedProcessHostId &&
|
|
|
|
deletedProcessHostId.toString() === processHostId) {
|
2016-12-01 00:59:59 +00:00
|
|
|
webContents.removeListener('render-view-deleted', listener)
|
2018-07-24 07:21:38 +00:00
|
|
|
this.clear(webContents, contextId)
|
2016-12-01 00:59:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
webContents.on('render-view-deleted', listener)
|
|
|
|
}
|
2016-01-15 22:15:42 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
module.exports = new ObjectsRegistry()
|