Browse Source

Merge pull request #9101 from electron/wrap-remote-set-as-args

Support calling remote setters with remote objects
Kevin Sawicki 8 years ago
parent
commit
3c1a372157

+ 3 - 2
lib/browser/rpc-server.js

@@ -360,15 +360,16 @@ ipcMain.on('ELECTRON_BROWSER_MEMBER_CALL', function (event, id, method, args) {
   }
 })
 
-ipcMain.on('ELECTRON_BROWSER_MEMBER_SET', function (event, id, name, value) {
+ipcMain.on('ELECTRON_BROWSER_MEMBER_SET', function (event, id, name, args) {
   try {
+    args = unwrapArgs(event.sender, args)
     let obj = objectsRegistry.get(id)
 
     if (obj == null) {
       throwRPCError(`Cannot set property '${name}' on missing remote object ${id}`)
     }
 
-    obj[name] = value
+    obj[name] = args[0]
     event.returnValue = null
   } catch (error) {
     event.returnValue = exceptionToMeta(error)

+ 2 - 1
lib/renderer/api/remote.js

@@ -139,7 +139,8 @@ const setObjectMembers = function (ref, object, metaId, members) {
       // Only set setter when it is writable.
       if (member.writable) {
         descriptor.set = function (value) {
-          const meta = ipcRenderer.sendSync('ELECTRON_BROWSER_MEMBER_SET', metaId, member.name, value)
+          const args = wrapArgs([value])
+          const meta = ipcRenderer.sendSync('ELECTRON_BROWSER_MEMBER_SET', metaId, member.name, args)
           // Meta will be non-null when a setter error occurred so parse it
           // to a value so it gets re-thrown.
           if (meta != null) {

+ 14 - 0
spec/api-ipc-spec.js

@@ -175,8 +175,14 @@ describe('ipc module', function () {
     it('can change its properties', function () {
       var property = remote.require(path.join(fixtures, 'module', 'property.js'))
       assert.equal(property.property, 1127)
+
+      property.property = null
+      assert.equal(property.property, null)
+      property.property = undefined
+      assert.equal(property.property, undefined)
       property.property = 1007
       assert.equal(property.property, 1007)
+
       assert.equal(property.getFunctionProperty(), 'foo-browser')
       property.func.property = 'bar'
       assert.equal(property.getFunctionProperty(), 'bar-browser')
@@ -199,6 +205,14 @@ describe('ipc module', function () {
       }, /setting error/)
     })
 
+    it('can set a remote property with a remote object', function () {
+      const foo = remote.require(path.join(fixtures, 'module', 'remote-object-set.js'))
+
+      assert.doesNotThrow(function () {
+        foo.bar = remote.getCurrentWindow()
+      })
+    })
+
     it('can construct an object from its member', function () {
       var call = remote.require(path.join(fixtures, 'module', 'call.js'))
       var obj = new call.constructor()

+ 11 - 0
spec/fixtures/module/remote-object-set.js

@@ -0,0 +1,11 @@
+const {BrowserWindow} = require('electron')
+
+class Foo {
+  set bar (value) {
+    if (!(value instanceof BrowserWindow)) {
+      throw new Error('setting error')
+    }
+  }
+}
+
+module.exports = new Foo()