]> khamidou — GitHub mirrors - electron.git/commitdiff
Pass remote objects from renderer to browser by ID.
authorCheng Zhao <zcbenz@gmail.com>
Fri, 3 May 2013 13:50:36 +0000 (21:50 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Fri, 3 May 2013 13:51:06 +0000 (21:51 +0800)
browser/atom/rpc_server.coffee
renderer/api/lib/remote.coffee

index c1fd0d434795540878b2a579e16fed24019aecff..aa492e7c9d4bf7a96ed966ae46bf1812f413f560 100644 (file)
@@ -2,6 +2,15 @@ ipc = require 'ipc'
 path = require 'path'
 objectsRegistry = require './objects_registry.js'
 
+metaToArgs = (metas) ->
+  metas.map (meta) ->
+    if meta.type is 'value'
+      meta.value
+    else if meta.type is 'remoteObject'
+      objectsRegistry.get meta.id
+    else
+      throw new TypeError("Unknown type: #{meta.type}")
+
 # Convert a real value into a POD structure which carries information of this
 # value.
 class Meta
@@ -58,6 +67,7 @@ ipc.on 'ATOM_BROWSER_CURRENT_WINDOW', (event, processId, routingId) ->
 
 ipc.on 'ATOM_BROWSER_CONSTRUCTOR', (event, processId, routingId, id, args) ->
   try
+    args = metaToArgs args
     constructor = objectsRegistry.get id
     # Call new with array of arguments.
     # http://stackoverflow.com/questions/1606797/use-of-apply-with-new-operator-is-this-possible
@@ -68,6 +78,7 @@ ipc.on 'ATOM_BROWSER_CONSTRUCTOR', (event, processId, routingId, id, args) ->
 
 ipc.on 'ATOM_BROWSER_FUNCTION_CALL', (event, processId, routingId, id, args) ->
   try
+    args = metaToArgs args
     func = objectsRegistry.get id
     ret = func.apply global, args
     event.result = new Meta(processId, routingId, ret)
@@ -76,6 +87,7 @@ ipc.on 'ATOM_BROWSER_FUNCTION_CALL', (event, processId, routingId, id, args) ->
 
 ipc.on 'ATOM_BROWSER_MEMBER_CALL', (event, processId, routingId, id, method, args) ->
   try
+    args = metaToArgs args
     obj = objectsRegistry.get id
     ret = obj[method].apply(obj, args)
     event.result = new Meta(processId, routingId, ret)
index 3cdcb3f3d72eba06e9868cd45930b278e247b9c3..8fbccc4128a3060d97ee2b1daa40d6a517692b3d 100644 (file)
@@ -1,6 +1,19 @@
 ipc = require 'ipc'
 v8_util = process.atomBinding 'v8_util'
 
+# Transform the arguments passed to browser into list of descriptions.
+#
+# This function assumes an array is passed, and it only converts remote objects
+# and functions, other types of values are passed as it is.
+argsToMeta = (args) ->
+  args.map (value) ->
+    if typeof value is 'object' and v8_util.getHiddenValue value, 'isRemoteObject'
+      type: 'remoteObject', id: value.id
+    else if typeof value is 'function'
+      throw new TypeError('functions is not supported yet')
+    else
+      type: 'value', value: value
+
 # Transform the description of value into a value or delegate object.
 metaToValue = (meta) ->
   switch meta.type
@@ -51,6 +64,9 @@ metaToValue = (meta) ->
       v8_util.setDestructor ret, ->
         ipc.sendChannel 'ATOM_BROWSER_DEREFERENCE', meta.storeId
 
+      # Mark this is a remote object.
+      v8_util.setHiddenValue ret, 'isRemoteObject', true
+
       ret
 
 # Release all resources of current render view when it's going to be unloaded.