No longer need to count the references in webContents

All remote objects are now cached in renderer process, so there is
always only one reference to one remote object for each webContents.
This commit is contained in:
Cheng Zhao 2016-02-22 14:03:31 +08:00
parent fe7462b352
commit 2b04af4349
2 changed files with 30 additions and 45 deletions

View file

@ -15,70 +15,57 @@ class ObjectsRegistry extends EventEmitter {
this.storage = {}; this.storage = {};
// Stores the IDs of objects referenced by WebContents. // Stores the IDs of objects referenced by WebContents.
// (webContentsId) => {(id) => (count)} // (webContentsId) => [id]
this.owners = {}; this.owners = {};
} }
// Register a new object, the object would be kept referenced until you release // Register a new object and return its assigned ID. If the object is already
// it explicitly. // registered then the already assigned ID would be returned.
add(webContentsId, obj) { add(webContentsId, obj) {
var base, base1, id; // Get or assign an ID to the object.
id = this.saveToStorage(obj); let id = this.saveToStorage(obj);
// Remember the owner. // Add object to the set of referenced objects.
if ((base = this.owners)[webContentsId] == null) { let owner = this.owners[webContentsId];
base[webContentsId] = {}; if (!owner)
owner = this.owners[webContentsId] = new Set();
if (!owner.has(id)) {
owner.add(id);
// Increase reference count if not referenced before.
this.storage[id].count++;
} }
if ((base1 = this.owners[webContentsId])[id] == null) {
base1[id] = 0;
}
this.owners[webContentsId][id]++;
// Returns object's id
return id; return id;
} }
// Get an object according to its ID. // Get an object according to its ID.
get(id) { get(id) {
var ref; return this.storage[id].object;
return (ref = this.storage[id]) != null ? ref.object : void 0;
} }
// Dereference an object according to its ID. // Dereference an object according to its ID.
remove(webContentsId, id) { remove(webContentsId, id) {
var pointer; // Dereference from the storage.
this.dereference(id, 1); this.dereference(id);
// Also reduce the count in owner. // Also remove the reference in owner.
pointer = this.owners[webContentsId]; this.owners[webContentsId].delete(id);
if (pointer == null) {
return;
}
--pointer[id];
if (pointer[id] === 0) {
return delete pointer[id];
}
} }
// Clear all references to objects refrenced by the WebContents. // Clear all references to objects refrenced by the WebContents.
clear(webContentsId) { clear(webContentsId) {
var count, id, ref; this.emit(`clear-${webContentsId}`);
this.emit("clear-" + webContentsId);
if (this.owners[webContentsId] == null) { let owner = this.owners[webContentsId];
if (!owner)
return; return;
} for (let id of owner)
ref = this.owners[webContentsId]; this.dereference(id);
for (id in ref) { delete this.owners[webContentsId];
count = ref[id];
this.dereference(id, count);
}
return delete this.owners[webContentsId];
} }
// Private: Saves the object into storage and assigns an ID for it. // Private: Saves the object into storage and assigns an ID for it.
saveToStorage(object) { saveToStorage(object) {
var id; let id = v8Util.getHiddenValue(object, 'atomId');
id = v8Util.getHiddenValue(object, 'atomId');
if (!id) { if (!id) {
id = ++this.nextId; id = ++this.nextId;
this.storage[id] = { this.storage[id] = {
@ -87,18 +74,16 @@ class ObjectsRegistry extends EventEmitter {
}; };
v8Util.setHiddenValue(object, 'atomId', id); v8Util.setHiddenValue(object, 'atomId', id);
} }
++this.storage[id].count;
return id; return id;
} }
// Private: Dereference the object from store. // Private: Dereference the object from store.
dereference(id, count) { dereference(id) {
var pointer; let pointer = this.storage[id];
pointer = this.storage[id];
if (pointer == null) { if (pointer == null) {
return; return;
} }
pointer.count -= count; pointer.count -= 1;
if (pointer.count === 0) { if (pointer.count === 0) {
v8Util.deleteHiddenValue(pointer.object, 'atomId'); v8Util.deleteHiddenValue(pointer.object, 'atomId');
return delete this.storage[id]; return delete this.storage[id];

View file

@ -145,7 +145,7 @@ var exceptionToMeta = function(error) {
var unwrapArgs = function(sender, args) { var unwrapArgs = function(sender, args) {
var metaToValue; var metaToValue;
metaToValue = function(meta) { metaToValue = function(meta) {
var i, len, member, ref, rendererReleased, returnValue; var i, len, member, ref, returnValue;
switch (meta.type) { switch (meta.type) {
case 'value': case 'value':
return meta.value; return meta.value;