From 0d77fd4a2c7af3ac05d7c4a023e28c8420e6c20c Mon Sep 17 00:00:00 2001 From: Robo Date: Wed, 17 Feb 2016 13:27:46 +0530 Subject: [PATCH] remote: return webcontents instance from cache --- atom/renderer/api/lib/remote.js | 7 +++++++ spec/api-ipc-spec.js | 8 ++++++++ 2 files changed, 15 insertions(+) diff --git a/atom/renderer/api/lib/remote.js b/atom/renderer/api/lib/remote.js index ad01e77ac5..d8a5508621 100644 --- a/atom/renderer/api/lib/remote.js +++ b/atom/renderer/api/lib/remote.js @@ -1,11 +1,14 @@ const ipcRenderer = require('electron').ipcRenderer; const CallbacksRegistry = require('electron').CallbacksRegistry; const v8Util = process.atomBinding('v8_util'); +const IDWeakMap = process.atomBinding('id_weak_map').IDWeakMap; const callbacksRegistry = new CallbacksRegistry; var includes = [].includes; +var remoteObjectCache = new IDWeakMap; + // Check for circular reference. var isCircular = function(field, visited) { if (typeof field === 'object') { @@ -154,6 +157,9 @@ var metaToValue = function(meta) { } } + if (remoteObjectCache.has(meta.id)) + return remoteObjectCache.get(meta.id); + // Track delegate object's life time, and tell the browser to clean up // when the object is GCed. v8Util.setDestructor(ret, function() { @@ -162,6 +168,7 @@ var metaToValue = function(meta) { // Remember object's id. v8Util.setHiddenValue(ret, 'atomId', meta.id); + remoteObjectCache.set(meta.id, ret); return ret; } }; diff --git a/spec/api-ipc-spec.js b/spec/api-ipc-spec.js index 2c8bc28f15..9cf741412b 100644 --- a/spec/api-ipc-spec.js +++ b/spec/api-ipc-spec.js @@ -90,6 +90,14 @@ describe('ipc module', function() { }); }); + describe('remote webContents', function() { + it('can return same object with different getters', function() { + var contents1 = remote.getCurrentWindow().webContents; + var contents2 = remote.getCurrentWebContents(); + assert(contents1 == contents2); + }); + }); + describe('ipc.sender.send', function() { it('should work when sending an object containing id property', function(done) { var obj = {