2013-04-24 08:43:01 +00:00
|
|
|
ipc = require 'ipc'
|
|
|
|
path = require 'path'
|
2013-05-30 12:16:54 +00:00
|
|
|
objectsRegistry = require './objects-registry.js'
|
|
|
|
v8Util = process.atomBinding 'v8_util'
|
2013-05-04 09:01:28 +00:00
|
|
|
|
2013-05-04 13:38:25 +00:00
|
|
|
# Convert a real value into meta data.
|
2014-04-25 08:13:16 +00:00
|
|
|
valueToMeta = (sender, value) ->
|
2013-05-04 13:38:25 +00:00
|
|
|
meta = type: typeof value
|
|
|
|
|
|
|
|
meta.type = 'value' if value is null
|
|
|
|
meta.type = 'array' if Array.isArray value
|
|
|
|
|
|
|
|
# Treat the arguments object as array.
|
|
|
|
meta.type = 'array' if meta.type is 'object' and value.callee? and value.length?
|
|
|
|
|
|
|
|
if meta.type is 'array'
|
|
|
|
meta.members = []
|
2014-04-25 08:13:16 +00:00
|
|
|
meta.members.push valueToMeta(sender, el) for el in value
|
2013-05-04 13:38:25 +00:00
|
|
|
else if meta.type is 'object' or meta.type is 'function'
|
|
|
|
meta.name = value.constructor.name
|
|
|
|
|
|
|
|
# Reference the original value if it's an object, because when it's
|
|
|
|
# passed to renderer we would assume the renderer keeps a reference of
|
|
|
|
# it.
|
2014-04-25 08:45:14 +00:00
|
|
|
[meta.id, meta.storeId] = objectsRegistry.add sender.getId(), value
|
2013-05-04 13:38:25 +00:00
|
|
|
|
|
|
|
meta.members = []
|
|
|
|
meta.members.push {name: prop, type: typeof field} for prop, field of value
|
|
|
|
else
|
|
|
|
meta.type = 'value'
|
|
|
|
meta.value = value
|
|
|
|
|
|
|
|
meta
|
2013-04-24 08:43:01 +00:00
|
|
|
|
2013-05-14 12:00:44 +00:00
|
|
|
# Convert Error into meta data.
|
|
|
|
errorToMeta = (error) ->
|
|
|
|
type: 'error', message: error.message, stack: (error.stack || error)
|
|
|
|
|
2013-05-04 14:59:32 +00:00
|
|
|
# Convert array of meta data from renderer into array of real values.
|
2014-04-25 08:13:16 +00:00
|
|
|
unwrapArgs = (sender, args) ->
|
2013-08-14 13:51:51 +00:00
|
|
|
metaToValue = (meta) ->
|
2013-05-04 14:59:32 +00:00
|
|
|
switch meta.type
|
|
|
|
when 'value' then meta.value
|
2013-08-14 13:51:51 +00:00
|
|
|
when 'remote-object' then objectsRegistry.get meta.id
|
2014-04-25 08:13:16 +00:00
|
|
|
when 'array' then unwrapArgs sender, meta.value
|
2013-08-14 13:51:51 +00:00
|
|
|
when 'object'
|
2013-08-25 09:22:36 +00:00
|
|
|
ret = v8Util.createObjectWithName meta.name
|
2013-08-14 13:51:51 +00:00
|
|
|
for member in meta.members
|
|
|
|
ret[member.name] = metaToValue(member.value)
|
|
|
|
ret
|
2013-08-25 09:22:36 +00:00
|
|
|
when 'function-with-return-value'
|
|
|
|
returnValue = metaToValue meta.value
|
|
|
|
-> returnValue
|
2013-05-04 14:59:32 +00:00
|
|
|
when 'function'
|
2013-10-05 12:53:31 +00:00
|
|
|
rendererReleased = false
|
2014-04-25 08:45:14 +00:00
|
|
|
objectsRegistry.once "clear-#{sender.getId()}", ->
|
2013-10-05 12:53:31 +00:00
|
|
|
rendererReleased = true
|
|
|
|
|
2013-05-04 14:59:32 +00:00
|
|
|
ret = ->
|
2013-10-05 12:53:31 +00:00
|
|
|
throw new Error('Calling a callback of released renderer view') if rendererReleased
|
2014-04-25 08:13:16 +00:00
|
|
|
sender.send 'ATOM_RENDERER_CALLBACK', meta.id, valueToMeta(sender, arguments)
|
2013-05-30 12:16:54 +00:00
|
|
|
v8Util.setDestructor ret, ->
|
2013-10-05 12:53:31 +00:00
|
|
|
return if rendererReleased
|
2014-04-25 08:13:16 +00:00
|
|
|
sender.send 'ATOM_RENDERER_RELEASE_CALLBACK', meta.id
|
2013-05-04 14:59:32 +00:00
|
|
|
ret
|
|
|
|
else throw new TypeError("Unknown type: #{meta.type}")
|
|
|
|
|
2013-08-14 13:51:51 +00:00
|
|
|
args.map metaToValue
|
|
|
|
|
2013-09-24 10:01:12 +00:00
|
|
|
# Call a function and send reply asynchronously if it's a an asynchronous
|
|
|
|
# style function and the caller didn't pass a callback.
|
2014-04-25 08:13:16 +00:00
|
|
|
callFunction = (event, func, caller, args) ->
|
2013-09-24 10:01:12 +00:00
|
|
|
if v8Util.getHiddenValue(func, 'asynchronous') and typeof args[args.length - 1] isnt 'function'
|
|
|
|
args.push (ret) ->
|
2014-04-25 08:13:16 +00:00
|
|
|
event.returnValue = valueToMeta event.sender, ret
|
2013-09-24 10:01:12 +00:00
|
|
|
func.apply caller, args
|
|
|
|
else
|
|
|
|
ret = func.apply caller, args
|
2014-04-25 08:13:16 +00:00
|
|
|
event.returnValue = valueToMeta event.sender, ret
|
2013-09-24 10:01:12 +00:00
|
|
|
|
2013-12-06 06:44:25 +00:00
|
|
|
# Send by BrowserWindow when its render view is deleted.
|
2014-04-25 08:45:14 +00:00
|
|
|
process.on 'ATOM_BROWSER_RELEASE_RENDER_VIEW', (id) ->
|
|
|
|
objectsRegistry.clear id
|
2013-12-06 06:44:25 +00:00
|
|
|
|
2014-04-25 08:13:16 +00:00
|
|
|
ipc.on 'ATOM_BROWSER_REQUIRE', (event, module) ->
|
2013-04-26 14:25:30 +00:00
|
|
|
try
|
2014-06-22 06:56:30 +00:00
|
|
|
event.returnValue = valueToMeta event.sender, process.mainModule.require(module)
|
2013-04-26 14:25:30 +00:00
|
|
|
catch e
|
2013-09-20 13:37:47 +00:00
|
|
|
event.returnValue = errorToMeta e
|
2013-04-24 08:43:01 +00:00
|
|
|
|
2014-04-25 08:13:16 +00:00
|
|
|
ipc.on 'ATOM_BROWSER_GLOBAL', (event, name) ->
|
2013-04-30 08:27:14 +00:00
|
|
|
try
|
2014-04-25 08:13:16 +00:00
|
|
|
event.returnValue = valueToMeta event.sender, global[name]
|
2013-04-30 08:27:14 +00:00
|
|
|
catch e
|
2013-09-20 13:37:47 +00:00
|
|
|
event.returnValue = errorToMeta e
|
2013-04-30 08:27:14 +00:00
|
|
|
|
2014-04-25 08:13:16 +00:00
|
|
|
ipc.on 'ATOM_BROWSER_CURRENT_WINDOW', (event) ->
|
2013-04-27 11:06:41 +00:00
|
|
|
try
|
2013-05-30 12:16:54 +00:00
|
|
|
BrowserWindow = require 'browser-window'
|
2014-04-25 08:45:14 +00:00
|
|
|
window = BrowserWindow.fromWebContents event.sender
|
|
|
|
window = BrowserWindow.fromDevToolsWebContents event.sender unless window?
|
2014-04-25 08:13:16 +00:00
|
|
|
event.returnValue = valueToMeta event.sender, window
|
2013-04-27 11:06:41 +00:00
|
|
|
catch e
|
2013-09-20 13:37:47 +00:00
|
|
|
event.returnValue = errorToMeta e
|
2013-04-27 11:06:41 +00:00
|
|
|
|
2014-04-25 08:13:16 +00:00
|
|
|
ipc.on 'ATOM_BROWSER_CONSTRUCTOR', (event, id, args) ->
|
2013-04-24 08:43:01 +00:00
|
|
|
try
|
2014-04-25 08:13:16 +00:00
|
|
|
args = unwrapArgs event.sender, args
|
2013-04-26 14:25:30 +00:00
|
|
|
constructor = objectsRegistry.get id
|
2013-04-25 07:12:56 +00:00
|
|
|
# Call new with array of arguments.
|
|
|
|
# http://stackoverflow.com/questions/1606797/use-of-apply-with-new-operator-is-this-possible
|
2013-04-24 08:43:01 +00:00
|
|
|
obj = new (Function::bind.apply(constructor, [null].concat(args)))
|
2014-04-25 08:13:16 +00:00
|
|
|
event.returnValue = valueToMeta event.sender, obj
|
2013-04-24 08:43:01 +00:00
|
|
|
catch e
|
2013-09-20 13:37:47 +00:00
|
|
|
event.returnValue = errorToMeta e
|
2013-04-24 08:43:01 +00:00
|
|
|
|
2014-04-25 08:13:16 +00:00
|
|
|
ipc.on 'ATOM_BROWSER_FUNCTION_CALL', (event, id, args) ->
|
2013-04-24 08:43:01 +00:00
|
|
|
try
|
2014-04-25 08:13:16 +00:00
|
|
|
args = unwrapArgs event.sender, args
|
2013-04-26 14:25:30 +00:00
|
|
|
func = objectsRegistry.get id
|
2014-04-25 08:13:16 +00:00
|
|
|
callFunction event, func, global, args
|
2013-04-24 08:43:01 +00:00
|
|
|
catch e
|
2013-09-20 13:37:47 +00:00
|
|
|
event.returnValue = errorToMeta e
|
2013-04-24 08:43:01 +00:00
|
|
|
|
2014-04-25 08:13:16 +00:00
|
|
|
ipc.on 'ATOM_BROWSER_MEMBER_CONSTRUCTOR', (event, id, method, args) ->
|
2013-08-25 12:44:52 +00:00
|
|
|
try
|
2014-04-25 08:13:16 +00:00
|
|
|
args = unwrapArgs event.sender, args
|
2013-08-25 12:44:52 +00:00
|
|
|
constructor = objectsRegistry.get(id)[method]
|
|
|
|
# Call new with array of arguments.
|
|
|
|
obj = new (Function::bind.apply(constructor, [null].concat(args)))
|
2014-04-25 08:13:16 +00:00
|
|
|
event.returnValue = valueToMeta event.sender, obj
|
2013-08-25 12:44:52 +00:00
|
|
|
catch e
|
2013-09-20 13:37:47 +00:00
|
|
|
event.returnValue = errorToMeta e
|
2013-08-25 12:44:52 +00:00
|
|
|
|
2014-04-25 08:13:16 +00:00
|
|
|
ipc.on 'ATOM_BROWSER_MEMBER_CALL', (event, id, method, args) ->
|
2013-04-24 08:43:01 +00:00
|
|
|
try
|
2014-04-25 08:13:16 +00:00
|
|
|
args = unwrapArgs event.sender, args
|
2013-04-26 14:25:30 +00:00
|
|
|
obj = objectsRegistry.get id
|
2014-04-25 08:13:16 +00:00
|
|
|
callFunction event, obj[method], obj, args
|
2013-04-24 08:43:01 +00:00
|
|
|
catch e
|
2013-09-20 13:37:47 +00:00
|
|
|
event.returnValue = errorToMeta e
|
2013-04-24 08:43:01 +00:00
|
|
|
|
2014-04-25 08:13:16 +00:00
|
|
|
ipc.on 'ATOM_BROWSER_MEMBER_SET', (event, id, name, value) ->
|
2013-04-24 08:43:01 +00:00
|
|
|
try
|
2013-04-26 14:25:30 +00:00
|
|
|
obj = objectsRegistry.get id
|
2013-04-26 13:14:29 +00:00
|
|
|
obj[name] = value
|
2013-09-20 13:39:07 +00:00
|
|
|
event.returnValue = null
|
2013-04-24 08:43:01 +00:00
|
|
|
catch e
|
2013-09-20 13:37:47 +00:00
|
|
|
event.returnValue = errorToMeta e
|
2013-04-24 08:43:01 +00:00
|
|
|
|
2014-04-25 08:13:16 +00:00
|
|
|
ipc.on 'ATOM_BROWSER_MEMBER_GET', (event, id, name) ->
|
2013-04-24 08:43:01 +00:00
|
|
|
try
|
2013-04-26 14:25:30 +00:00
|
|
|
obj = objectsRegistry.get id
|
2014-04-25 08:13:16 +00:00
|
|
|
event.returnValue = valueToMeta event.sender, obj[name]
|
2013-04-24 08:43:01 +00:00
|
|
|
catch e
|
2013-09-20 13:37:47 +00:00
|
|
|
event.returnValue = errorToMeta e
|
2013-04-25 11:26:31 +00:00
|
|
|
|
2014-04-25 08:13:16 +00:00
|
|
|
ipc.on 'ATOM_BROWSER_DEREFERENCE', (event, storeId) ->
|
2014-04-25 08:45:14 +00:00
|
|
|
objectsRegistry.remove event.sender.getId(), storeId
|