2013-04-24 08:43:01 +00:00
|
|
|
path = require 'path'
|
2015-11-12 12:30:40 +00:00
|
|
|
|
|
|
|
electron = require 'electron'
|
|
|
|
{ipcMain} = electron
|
2015-11-12 10:28:04 +00:00
|
|
|
objectsRegistry = require './objects-registry'
|
|
|
|
|
2013-05-30 12:16:54 +00:00
|
|
|
v8Util = process.atomBinding 'v8_util'
|
2015-11-12 10:28:04 +00:00
|
|
|
{IDWeakMap} = process.atomBinding 'id_weak_map'
|
2013-05-04 09:01:28 +00:00
|
|
|
|
2013-05-04 13:38:25 +00:00
|
|
|
# Convert a real value into meta data.
|
2015-08-27 07:23:23 +00:00
|
|
|
valueToMeta = (sender, value, optimizeSimpleObject=false) ->
|
2013-05-04 13:38:25 +00:00
|
|
|
meta = type: typeof value
|
|
|
|
|
2015-06-09 09:46:33 +00:00
|
|
|
meta.type = 'buffer' if Buffer.isBuffer value
|
2013-05-04 13:38:25 +00:00
|
|
|
meta.type = 'value' if value is null
|
|
|
|
meta.type = 'array' if Array.isArray value
|
2015-10-23 06:23:05 +00:00
|
|
|
meta.type = 'error' if value instanceof Error
|
2015-10-23 06:51:25 +00:00
|
|
|
meta.type = 'date' if value instanceof Date
|
2015-11-12 12:30:40 +00:00
|
|
|
meta.type = 'promise' if value?.constructor.name is 'Promise'
|
|
|
|
|
2015-08-27 07:23:23 +00:00
|
|
|
# Treat simple objects as value.
|
|
|
|
if optimizeSimpleObject and meta.type is 'object' and v8Util.getHiddenValue value, 'simple'
|
|
|
|
meta.type = 'value'
|
|
|
|
|
2013-05-04 13:38:25 +00:00
|
|
|
# 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.
|
2015-08-27 11:01:34 +00:00
|
|
|
meta.id = objectsRegistry.add sender.getId(), value
|
2013-05-04 13:38:25 +00:00
|
|
|
|
2015-11-02 12:28:01 +00:00
|
|
|
meta.members = ({name, type: typeof field} for name, field of value)
|
2015-06-09 09:46:33 +00:00
|
|
|
else if meta.type is 'buffer'
|
|
|
|
meta.value = Array::slice.call value, 0
|
2015-07-31 05:26:57 +00:00
|
|
|
else if meta.type is 'promise'
|
2015-11-02 12:28:01 +00:00
|
|
|
meta.then = valueToMeta sender, value.then.bind(value)
|
2015-10-23 06:23:05 +00:00
|
|
|
else if meta.type is 'error'
|
2015-11-02 12:28:01 +00:00
|
|
|
meta.members = plainObjectToMeta value
|
2015-10-23 06:51:25 +00:00
|
|
|
else if meta.type is 'date'
|
|
|
|
meta.value = value.getTime()
|
2013-05-04 13:38:25 +00:00
|
|
|
else
|
|
|
|
meta.type = 'value'
|
|
|
|
meta.value = value
|
|
|
|
|
|
|
|
meta
|
2013-04-24 08:43:01 +00:00
|
|
|
|
2015-11-02 12:28:01 +00:00
|
|
|
# Convert object to meta by value.
|
|
|
|
plainObjectToMeta = (obj) ->
|
|
|
|
Object.getOwnPropertyNames(obj).map (name) -> {name, value: obj[name]}
|
2015-10-31 23:20:54 +00:00
|
|
|
|
2013-05-14 12:00:44 +00:00
|
|
|
# Convert Error into meta data.
|
2015-10-23 06:23:05 +00:00
|
|
|
exceptionToMeta = (error) ->
|
|
|
|
type: 'exception', message: error.message, stack: (error.stack || error)
|
2013-05-14 12:00:44 +00:00
|
|
|
|
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
|
2015-06-09 09:46:33 +00:00
|
|
|
when 'buffer' then new Buffer(meta.value)
|
2015-07-31 05:45:22 +00:00
|
|
|
when 'promise' then Promise.resolve(then: metaToValue(meta.then))
|
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'
|
2015-10-31 07:00:06 +00:00
|
|
|
# Cache the callbacks in renderer.
|
|
|
|
unless sender.callbacks
|
|
|
|
sender.callbacks = new IDWeakMap
|
|
|
|
sender.on 'render-view-deleted', ->
|
|
|
|
sender.callbacks.clear()
|
|
|
|
return sender.callbacks.get meta.id if sender.callbacks.has meta.id
|
2015-10-29 10:57:30 +00:00
|
|
|
|
2013-10-05 12:53:31 +00:00
|
|
|
rendererReleased = false
|
2015-10-31 07:00:06 +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 = ->
|
2015-10-16 01:36:02 +00:00
|
|
|
if rendererReleased
|
|
|
|
throw new Error("Attempting to call a function in a renderer window
|
2015-10-28 15:59:46 +00:00
|
|
|
that has been closed or released. Function provided here: #{meta.location}.")
|
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
|
2015-10-31 07:00:06 +00:00
|
|
|
sender.callbacks.remove meta.id
|
2014-04-25 08:13:16 +00:00
|
|
|
sender.send 'ATOM_RENDERER_RELEASE_CALLBACK', meta.id
|
2015-10-31 07:00:06 +00:00
|
|
|
sender.callbacks.set meta.id, ret
|
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) ->
|
2015-11-18 01:36:37 +00:00
|
|
|
funcMarkedAsync = v8Util.getHiddenValue(func, 'asynchronous')
|
2015-11-24 22:49:52 +00:00
|
|
|
funcPassedCallback = typeof args[args.length - 1] is 'function'
|
2015-11-18 01:36:37 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
if funcMarkedAsync and not funcPassedCallback
|
|
|
|
args.push (ret) ->
|
|
|
|
event.returnValue = valueToMeta event.sender, ret, true
|
|
|
|
func.apply caller, args
|
|
|
|
else
|
|
|
|
ret = func.apply caller, args
|
2015-08-27 07:23:23 +00:00
|
|
|
event.returnValue = valueToMeta event.sender, ret, true
|
2015-11-18 01:36:37 +00:00
|
|
|
catch e
|
|
|
|
# Catch functions thrown further down in function invocation and wrap
|
|
|
|
# them with the function name so it's easier to trace things like
|
|
|
|
# `Error processing argument -1.`
|
|
|
|
funcName = func.name ? "anonymous"
|
|
|
|
throw new Error("Could not call remote function `#{funcName}`.
|
|
|
|
Check that the function signature is correct.
|
|
|
|
Underlying error: #{e.message}")
|
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
|
|
|
|
2015-11-12 10:28:04 +00:00
|
|
|
ipcMain.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
|
2015-10-23 06:23:05 +00:00
|
|
|
event.returnValue = exceptionToMeta e
|
2013-04-24 08:43:01 +00:00
|
|
|
|
2015-11-12 12:30:40 +00:00
|
|
|
ipcMain.on 'ATOM_BROWSER_GET_BUILTIN', (event, module) ->
|
|
|
|
try
|
|
|
|
event.returnValue = valueToMeta event.sender, electron[module]
|
|
|
|
catch e
|
|
|
|
event.returnValue = exceptionToMeta e
|
|
|
|
|
2015-11-12 10:28:04 +00:00
|
|
|
ipcMain.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
|
2015-10-23 06:23:05 +00:00
|
|
|
event.returnValue = exceptionToMeta e
|
2013-04-30 08:27:14 +00:00
|
|
|
|
2015-11-12 10:28:04 +00:00
|
|
|
ipcMain.on 'ATOM_BROWSER_CURRENT_WINDOW', (event) ->
|
2013-04-27 11:06:41 +00:00
|
|
|
try
|
2015-10-01 06:08:33 +00:00
|
|
|
event.returnValue = valueToMeta event.sender, event.sender.getOwnerBrowserWindow()
|
2013-04-27 11:06:41 +00:00
|
|
|
catch e
|
2015-10-23 06:23:05 +00:00
|
|
|
event.returnValue = exceptionToMeta e
|
2013-04-27 11:06:41 +00:00
|
|
|
|
2015-11-12 10:28:04 +00:00
|
|
|
ipcMain.on 'ATOM_BROWSER_CURRENT_WEB_CONTENTS', (event) ->
|
2015-05-11 06:05:20 +00:00
|
|
|
event.returnValue = valueToMeta event.sender, event.sender
|
|
|
|
|
2015-11-12 10:28:04 +00:00
|
|
|
ipcMain.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
|
2015-10-23 06:23:05 +00:00
|
|
|
event.returnValue = exceptionToMeta e
|
2013-04-24 08:43:01 +00:00
|
|
|
|
2015-11-12 10:28:04 +00:00
|
|
|
ipcMain.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
|
2015-10-23 06:23:05 +00:00
|
|
|
event.returnValue = exceptionToMeta e
|
2013-04-24 08:43:01 +00:00
|
|
|
|
2015-11-12 10:28:04 +00:00
|
|
|
ipcMain.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
|
2015-10-23 06:23:05 +00:00
|
|
|
event.returnValue = exceptionToMeta e
|
2013-08-25 12:44:52 +00:00
|
|
|
|
2015-11-12 10:28:04 +00:00
|
|
|
ipcMain.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
|
2015-10-23 06:23:05 +00:00
|
|
|
event.returnValue = exceptionToMeta e
|
2013-04-24 08:43:01 +00:00
|
|
|
|
2015-11-12 10:28:04 +00:00
|
|
|
ipcMain.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
|
2015-10-23 06:23:05 +00:00
|
|
|
event.returnValue = exceptionToMeta e
|
2013-04-24 08:43:01 +00:00
|
|
|
|
2015-11-12 10:28:04 +00:00
|
|
|
ipcMain.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
|
2015-10-23 06:23:05 +00:00
|
|
|
event.returnValue = exceptionToMeta e
|
2013-04-25 11:26:31 +00:00
|
|
|
|
2015-11-12 10:28:04 +00:00
|
|
|
ipcMain.on 'ATOM_BROWSER_DEREFERENCE', (event, id) ->
|
2015-08-27 11:01:34 +00:00
|
|
|
objectsRegistry.remove event.sender.getId(), id
|
2014-10-24 11:57:29 +00:00
|
|
|
|
2015-11-12 10:28:04 +00:00
|
|
|
ipcMain.on 'ATOM_BROWSER_GUEST_WEB_CONTENTS', (event, guestInstanceId) ->
|
2014-10-24 11:57:29 +00:00
|
|
|
try
|
|
|
|
guestViewManager = require './guest-view-manager'
|
|
|
|
event.returnValue = valueToMeta event.sender, guestViewManager.getGuest(guestInstanceId)
|
|
|
|
catch e
|
2015-10-23 06:23:05 +00:00
|
|
|
event.returnValue = exceptionToMeta e
|
2015-11-13 14:15:16 +00:00
|
|
|
|
|
|
|
ipcMain.on 'ATOM_BROWSER_LIST_MODULES', (event) ->
|
|
|
|
event.returnValue = (name for name of electron)
|
2015-11-25 23:54:30 +00:00
|
|
|
|
|
|
|
ipcMain.on 'ATOM_BROWSER_ASYNC_CALL_TO_GUEST_VIEW', (event, guestInstanceId, method, args...) ->
|
|
|
|
try
|
|
|
|
guestViewManager = require './guest-view-manager'
|
|
|
|
guest = guestViewManager.getGuest(guestInstanceId)
|
|
|
|
guest[method].apply(guest, args)
|
|
|
|
catch e
|
|
|
|
event.returnValue = exceptionToMeta e
|