Separate renderer functions by renderer process ID

Close #4324.
This commit is contained in:
Cheng Zhao 2016-02-22 14:36:50 +08:00
parent d4c954870a
commit 593a79ce20

View file

@ -11,6 +11,10 @@ const FUNCTION_PROPERTIES = [
'length', 'name', 'arguments', 'caller', 'prototype', 'length', 'name', 'arguments', 'caller', 'prototype',
]; ];
// The remote functions in renderer processes.
// (webContentsId) => {id: Function}
let rendererFunctions = {};
// Return the description of object's members: // Return the description of object's members:
let getObjectMemebers = function(object) { let getObjectMemebers = function(object) {
let names = Object.getOwnPropertyNames(object); let names = Object.getOwnPropertyNames(object);
@ -177,32 +181,30 @@ var unwrapArgs = function(sender, args) {
}; };
case 'function': { case 'function': {
// Cache the callbacks in renderer. // Cache the callbacks in renderer.
if (!sender.callbacks) { let webContentsId = sender.getId();
sender.callbacks = new IDWeakMap; let callbacks = rendererFunctions[webContentsId];
sender.on('render-view-deleted', function() { if (!callbacks) {
return this.callbacks.clear(); callbacks = rendererFunctions[webContentsId] = new IDWeakMap;
sender.once('render-view-deleted', function(event, id) {
callbacks.clear();
delete rendererFunctions[id];
}); });
} }
if (sender.callbacks.has(meta.id)) if (callbacks.has(meta.id))
return sender.callbacks.get(meta.id); return callbacks.get(meta.id);
// Prevent the callback from being called when its page is gone.
rendererReleased = false;
sender.once('render-view-deleted', function() {
rendererReleased = true;
});
let callIntoRenderer = function(...args) { let callIntoRenderer = function(...args) {
if (rendererReleased || sender.isDestroyed()) if ((webContentsId in rendererFunctions) && !sender.isDestroyed())
sender.send('ATOM_RENDERER_CALLBACK', meta.id, valueToMeta(sender, args));
else
throw new Error(`Attempting to call a function in a renderer window that has been closed or released. Function provided here: ${meta.location}.`); throw new Error(`Attempting to call a function in a renderer window that has been closed or released. Function provided here: ${meta.location}.`);
sender.send('ATOM_RENDERER_CALLBACK', meta.id, valueToMeta(sender, args));
}; };
v8Util.setDestructor(callIntoRenderer, function() { v8Util.setDestructor(callIntoRenderer, function() {
if (!rendererReleased && !sender.isDestroyed()) if ((webContentsId in rendererFunctions) && !sender.isDestroyed())
sender.send('ATOM_RENDERER_RELEASE_CALLBACK', meta.id); sender.send('ATOM_RENDERER_RELEASE_CALLBACK', meta.id);
}); });
sender.callbacks.set(meta.id, callIntoRenderer); callbacks.set(meta.id, callIntoRenderer);
return callIntoRenderer; return callIntoRenderer;
} }
default: default: