Resupport cross-process callback.

It seems that it's avoidable to make callback cross-process when we
start to emitting events for GUI elements, without this feature our
implementation will be much more complicated. I will find a way to warn
about resources leak in browser.
This commit is contained in:
Cheng Zhao 2013-05-04 17:01:28 +08:00
parent 188bb5e225
commit 36c260f4d5
2 changed files with 43 additions and 16 deletions

View file

@ -1,16 +1,30 @@
ipc = require 'ipc'
v8_util = process.atomBinding 'v8_util'
nextCallbackId = 0
storedCallbacks = {}
storeCallback = (callback) ->
++nextCallbackId
storedCallbacks[nextCallbackId] = callback
nextCallbackId
makeCallback = (id, args) ->
storedCallbacks[id].apply global, args
releaseCallback = (id) ->
delete storedCallbacks[id]
# 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) ->
wrapArgs = (args) ->
Array::slice.call(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')
type: 'function', id: storeCallback(value)
else
type: 'value', value: value
@ -28,7 +42,7 @@ metaToValue = (meta) ->
constructor: ->
if @constructor == RemoteFunction
# Constructor call.
obj = ipc.sendChannelSync 'ATOM_BROWSER_CONSTRUCTOR', meta.id, Array::slice.call(arguments)
obj = ipc.sendChannelSync 'ATOM_BROWSER_CONSTRUCTOR', meta.id, wrapArgs(arguments)
# Returning object in constructor will replace constructed object
# with the returned object.
@ -36,7 +50,7 @@ metaToValue = (meta) ->
return metaToValue obj
else
# Function call.
ret = ipc.sendChannelSync 'ATOM_BROWSER_FUNCTION_CALL', meta.id, Array::slice.call(arguments)
ret = ipc.sendChannelSync 'ATOM_BROWSER_FUNCTION_CALL', meta.id, wrapArgs(arguments)
return metaToValue ret
else
ret = v8_util.createObjectWithName meta.name
@ -47,7 +61,7 @@ metaToValue = (meta) ->
if member.type is 'function'
ret[member.name] = ->
# Call member function.
ret = ipc.sendChannelSync 'ATOM_BROWSER_MEMBER_CALL', meta.id, member.name, Array::slice.call(arguments)
ret = ipc.sendChannelSync 'ATOM_BROWSER_MEMBER_CALL', meta.id, member.name, wrapArgs(arguments)
metaToValue ret
else
ret.__defineSetter__ member.name, (value) ->
@ -69,6 +83,12 @@ metaToValue = (meta) ->
ret
ipc.on 'ATOM_RENDERER_CALLBACK', (id, args) ->
makeCallback id, metaToValue(args)
ipc.on 'ATOM_RENDERER_RELEASE_CALLBACK', (id) ->
releaseCallback id
# Release all resources of current render view when it's going to be unloaded.
window.addEventListener 'unload', (event) ->
ipc.sendChannelSync 'ATOM_BROWSER_RELEASE_RENDER_VIEW'