2016-01-15 22:15:42 +00:00
|
|
|
'use strict';
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-15 22:29:36 +00:00
|
|
|
const v8Util = process.atomBinding('v8_util');
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-02-22 06:10:25 +00:00
|
|
|
class ObjectsRegistry {
|
2016-01-15 22:15:42 +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}
|
|
|
|
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.
|
2016-02-22 06:03:31 +00:00
|
|
|
// (webContentsId) => [id]
|
2016-01-15 22:15:42 +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.
|
2016-02-22 06:54:55 +00:00
|
|
|
add(webContents, obj) {
|
2016-02-22 06:03:31 +00:00
|
|
|
// Get or assign an ID to the object.
|
|
|
|
let 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.
|
2016-02-22 06:54:55 +00:00
|
|
|
let webContentsId = webContents.getId();
|
2016-02-22 06:03:31 +00:00
|
|
|
let owner = this.owners[webContentsId];
|
2016-02-22 06:54:55 +00:00
|
|
|
if (!owner) {
|
2016-02-22 06:03:31 +00:00
|
|
|
owner = this.owners[webContentsId] = new Set();
|
2016-02-22 06:54:55 +00:00
|
|
|
// Clear the storage when webContents is reloaded/navigated.
|
|
|
|
webContents.once('render-view-deleted', (event, id) => {
|
|
|
|
this.clear(id);
|
|
|
|
});
|
|
|
|
}
|
2016-02-22 06:03:31 +00:00
|
|
|
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 17:57:36 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 22:15:42 +00:00
|
|
|
// Get an object according to its ID.
|
|
|
|
get(id) {
|
2016-02-22 06:03:31 +00:00
|
|
|
return this.storage[id].object;
|
2016-01-15 17:57:36 +00:00
|
|
|
}
|
2016-01-15 22:15:42 +00:00
|
|
|
|
|
|
|
// Dereference an object according to its ID.
|
|
|
|
remove(webContentsId, id) {
|
2016-02-22 06:03:31 +00:00
|
|
|
// Dereference from the storage.
|
|
|
|
this.dereference(id);
|
2016-01-15 22:15:42 +00:00
|
|
|
|
2016-02-22 06:03:31 +00:00
|
|
|
// Also remove the reference in owner.
|
|
|
|
this.owners[webContentsId].delete(id);
|
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.
|
|
|
|
clear(webContentsId) {
|
2016-02-22 06:03:31 +00:00
|
|
|
let owner = this.owners[webContentsId];
|
|
|
|
if (!owner)
|
2016-01-15 22:15:42 +00:00
|
|
|
return;
|
2016-02-22 06:03:31 +00:00
|
|
|
for (let id of owner)
|
|
|
|
this.dereference(id);
|
|
|
|
delete this.owners[webContentsId];
|
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.
|
|
|
|
saveToStorage(object) {
|
2016-02-22 06:03:31 +00:00
|
|
|
let id = v8Util.getHiddenValue(object, 'atomId');
|
2016-01-15 22:15:42 +00:00
|
|
|
if (!id) {
|
|
|
|
id = ++this.nextId;
|
|
|
|
this.storage[id] = {
|
|
|
|
count: 0,
|
|
|
|
object: object
|
|
|
|
};
|
|
|
|
v8Util.setHiddenValue(object, 'atomId', id);
|
|
|
|
}
|
|
|
|
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-02-22 06:03:31 +00:00
|
|
|
dereference(id) {
|
|
|
|
let pointer = this.storage[id];
|
2016-01-15 22:15:42 +00:00
|
|
|
if (pointer == null) {
|
|
|
|
return;
|
|
|
|
}
|
2016-02-22 06:03:31 +00:00
|
|
|
pointer.count -= 1;
|
2016-01-15 22:15:42 +00:00
|
|
|
if (pointer.count === 0) {
|
|
|
|
v8Util.deleteHiddenValue(pointer.object, 'atomId');
|
|
|
|
return delete this.storage[id];
|
|
|
|
}
|
2016-01-15 17:57:36 +00:00
|
|
|
}
|
2016-01-15 22:15:42 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
|
|
|
module.exports = new ObjectsRegistry;
|