electron/lib/browser/objects-registry.js

117 lines
3.2 KiB
JavaScript
Raw Normal View History

'use strict'
2016-01-12 02:40:23 +00:00
const v8Util = process.atomBinding('v8_util')
2016-01-12 02:40:23 +00:00
const getOwnerKey = (webContents, contextId) => {
return `${webContents.id}-${contextId}`
}
class ObjectsRegistry {
constructor () {
this.nextId = 0
2016-01-15 22:15:42 +00:00
// Stores all objects by ref-counting.
// (id) => {object, count}
this.storage = {}
2016-01-15 22:15:42 +00:00
// Stores the IDs of objects referenced by WebContents.
// (ownerKey) => [id]
this.owners = {}
}
// Register a new object and return its assigned ID. If the object is already
// registered then the already assigned ID would be returned.
add (webContents, contextId, obj) {
// Get or assign an ID to the object.
const id = this.saveToStorage(obj)
2016-01-15 22:15:42 +00:00
// Add object to the set of referenced objects.
const ownerKey = getOwnerKey(webContents, contextId)
let owner = this.owners[ownerKey]
if (!owner) {
owner = this.owners[ownerKey] = new Set()
this.registerDeleteListener(webContents, contextId)
}
if (!owner.has(id)) {
owner.add(id)
// Increase reference count if not referenced before.
this.storage[id].count++
2016-01-15 22:15:42 +00:00
}
return id
}
2016-01-15 22:15:42 +00:00
// Get an object according to its ID.
get (id) {
const pointer = this.storage[id]
if (pointer != null) return pointer.object
}
2016-01-15 22:15:42 +00:00
// Dereference an object according to its ID.
// 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 ownerKey = getOwnerKey(webContents, contextId)
let owner = this.owners[ownerKey]
if (owner) {
// Remove the reference in owner.
owner.delete(id)
// Dereference from the storage.
this.dereference(id)
}
}
2016-01-15 22:15:42 +00:00
// Clear all references to objects refrenced by the WebContents.
clear (webContents, contextId) {
const ownerKey = getOwnerKey(webContents, contextId)
let owner = this.owners[ownerKey]
if (!owner) return
for (let id of owner) this.dereference(id)
delete this.owners[ownerKey]
}
2016-01-15 22:15:42 +00:00
// Private: Saves the object into storage and assigns an ID for it.
saveToStorage (object) {
let id = v8Util.getHiddenValue(object, 'atomId')
2016-01-15 22:15:42 +00:00
if (!id) {
id = ++this.nextId
2016-01-15 22:15:42 +00:00
this.storage[id] = {
count: 0,
object: object
}
v8Util.setHiddenValue(object, 'atomId', id)
2016-01-15 22:15:42 +00:00
}
return id
}
2016-01-15 22:15:42 +00:00
// Private: Dereference the object from store.
dereference (id) {
let pointer = this.storage[id]
2016-01-15 22:15:42 +00:00
if (pointer == null) {
return
2016-01-15 22:15:42 +00:00
}
pointer.count -= 1
2016-01-15 22:15:42 +00:00
if (pointer.count === 0) {
v8Util.deleteHiddenValue(pointer.object, 'atomId')
delete this.storage[id]
2016-01-15 22:15:42 +00:00
}
}
2016-12-01 00:59:59 +00:00
// Private: Clear the storage when renderer process is destroyed.
registerDeleteListener (webContents, contextId) {
// 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)
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
module.exports = new ObjectsRegistry()