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:
parent
188bb5e225
commit
36c260f4d5
2 changed files with 43 additions and 16 deletions
|
@ -1,15 +1,22 @@
|
||||||
ipc = require 'ipc'
|
ipc = require 'ipc'
|
||||||
path = require 'path'
|
path = require 'path'
|
||||||
objectsRegistry = require './objects_registry.js'
|
objectsRegistry = require './objects_registry.js'
|
||||||
|
v8_util = process.atomBinding 'v8_util'
|
||||||
|
|
||||||
metaToArgs = (metas) ->
|
unwrapArgs = (processId, routingId, args) ->
|
||||||
metas.map (meta) ->
|
args.map (arg) ->
|
||||||
if meta.type is 'value'
|
if arg.type is 'value'
|
||||||
meta.value
|
arg.value
|
||||||
else if meta.type is 'remoteObject'
|
else if arg.type is 'remoteObject'
|
||||||
objectsRegistry.get meta.id
|
objectsRegistry.get arg.id
|
||||||
|
else if arg.type is 'function'
|
||||||
|
ret = ->
|
||||||
|
ipc.sendChannel processId, routingId, 'ATOM_RENDERER_CALLBACK', arg.id, new Meta(processId, routingId, arguments)
|
||||||
|
v8_util.setDestructor ret, ->
|
||||||
|
ipc.sendChannel processId, routingId, 'ATOM_RENDERER_RELEASE_CALLBACK', arg.id
|
||||||
|
ret
|
||||||
else
|
else
|
||||||
throw new TypeError("Unknown type: #{meta.type}")
|
throw new TypeError("Unknown type: #{arg.type}")
|
||||||
|
|
||||||
# Convert a real value into a POD structure which carries information of this
|
# Convert a real value into a POD structure which carries information of this
|
||||||
# value.
|
# value.
|
||||||
|
@ -67,7 +74,7 @@ ipc.on 'ATOM_BROWSER_CURRENT_WINDOW', (event, processId, routingId) ->
|
||||||
|
|
||||||
ipc.on 'ATOM_BROWSER_CONSTRUCTOR', (event, processId, routingId, id, args) ->
|
ipc.on 'ATOM_BROWSER_CONSTRUCTOR', (event, processId, routingId, id, args) ->
|
||||||
try
|
try
|
||||||
args = metaToArgs args
|
args = unwrapArgs processId, routingId, args
|
||||||
constructor = objectsRegistry.get id
|
constructor = objectsRegistry.get id
|
||||||
# Call new with array of arguments.
|
# Call new with array of arguments.
|
||||||
# http://stackoverflow.com/questions/1606797/use-of-apply-with-new-operator-is-this-possible
|
# http://stackoverflow.com/questions/1606797/use-of-apply-with-new-operator-is-this-possible
|
||||||
|
@ -78,7 +85,7 @@ ipc.on 'ATOM_BROWSER_CONSTRUCTOR', (event, processId, routingId, id, args) ->
|
||||||
|
|
||||||
ipc.on 'ATOM_BROWSER_FUNCTION_CALL', (event, processId, routingId, id, args) ->
|
ipc.on 'ATOM_BROWSER_FUNCTION_CALL', (event, processId, routingId, id, args) ->
|
||||||
try
|
try
|
||||||
args = metaToArgs args
|
args = unwrapArgs processId, routingId, args
|
||||||
func = objectsRegistry.get id
|
func = objectsRegistry.get id
|
||||||
ret = func.apply global, args
|
ret = func.apply global, args
|
||||||
event.result = new Meta(processId, routingId, ret)
|
event.result = new Meta(processId, routingId, ret)
|
||||||
|
@ -87,7 +94,7 @@ ipc.on 'ATOM_BROWSER_FUNCTION_CALL', (event, processId, routingId, id, args) ->
|
||||||
|
|
||||||
ipc.on 'ATOM_BROWSER_MEMBER_CALL', (event, processId, routingId, id, method, args) ->
|
ipc.on 'ATOM_BROWSER_MEMBER_CALL', (event, processId, routingId, id, method, args) ->
|
||||||
try
|
try
|
||||||
args = metaToArgs args
|
args = unwrapArgs processId, routingId, args
|
||||||
obj = objectsRegistry.get id
|
obj = objectsRegistry.get id
|
||||||
ret = obj[method].apply(obj, args)
|
ret = obj[method].apply(obj, args)
|
||||||
event.result = new Meta(processId, routingId, ret)
|
event.result = new Meta(processId, routingId, ret)
|
||||||
|
|
|
@ -1,16 +1,30 @@
|
||||||
ipc = require 'ipc'
|
ipc = require 'ipc'
|
||||||
v8_util = process.atomBinding 'v8_util'
|
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.
|
# Transform the arguments passed to browser into list of descriptions.
|
||||||
#
|
#
|
||||||
# This function assumes an array is passed, and it only converts remote objects
|
# This function assumes an array is passed, and it only converts remote objects
|
||||||
# and functions, other types of values are passed as it is.
|
# and functions, other types of values are passed as it is.
|
||||||
argsToMeta = (args) ->
|
wrapArgs = (args) ->
|
||||||
args.map (value) ->
|
Array::slice.call(args).map (value) ->
|
||||||
if typeof value is 'object' and v8_util.getHiddenValue value, 'isRemoteObject'
|
if typeof value is 'object' and v8_util.getHiddenValue value, 'isRemoteObject'
|
||||||
type: 'remoteObject', id: value.id
|
type: 'remoteObject', id: value.id
|
||||||
else if typeof value is 'function'
|
else if typeof value is 'function'
|
||||||
throw new TypeError('functions is not supported yet')
|
type: 'function', id: storeCallback(value)
|
||||||
else
|
else
|
||||||
type: 'value', value: value
|
type: 'value', value: value
|
||||||
|
|
||||||
|
@ -28,7 +42,7 @@ metaToValue = (meta) ->
|
||||||
constructor: ->
|
constructor: ->
|
||||||
if @constructor == RemoteFunction
|
if @constructor == RemoteFunction
|
||||||
# Constructor call.
|
# 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
|
# Returning object in constructor will replace constructed object
|
||||||
# with the returned object.
|
# with the returned object.
|
||||||
|
@ -36,7 +50,7 @@ metaToValue = (meta) ->
|
||||||
return metaToValue obj
|
return metaToValue obj
|
||||||
else
|
else
|
||||||
# Function call.
|
# 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
|
return metaToValue ret
|
||||||
else
|
else
|
||||||
ret = v8_util.createObjectWithName meta.name
|
ret = v8_util.createObjectWithName meta.name
|
||||||
|
@ -47,7 +61,7 @@ metaToValue = (meta) ->
|
||||||
if member.type is 'function'
|
if member.type is 'function'
|
||||||
ret[member.name] = ->
|
ret[member.name] = ->
|
||||||
# Call member function.
|
# 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
|
metaToValue ret
|
||||||
else
|
else
|
||||||
ret.__defineSetter__ member.name, (value) ->
|
ret.__defineSetter__ member.name, (value) ->
|
||||||
|
@ -69,6 +83,12 @@ metaToValue = (meta) ->
|
||||||
|
|
||||||
ret
|
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.
|
# Release all resources of current render view when it's going to be unloaded.
|
||||||
window.addEventListener 'unload', (event) ->
|
window.addEventListener 'unload', (event) ->
|
||||||
ipc.sendChannelSync 'ATOM_BROWSER_RELEASE_RENDER_VIEW'
|
ipc.sendChannelSync 'ATOM_BROWSER_RELEASE_RENDER_VIEW'
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue