electron/atom/browser/lib/rpc-server.js

347 lines
11 KiB
JavaScript
Raw Normal View History

'use strict';
2016-01-14 21:21:11 +00:00
const electron = require('electron');
const ipcMain = electron.ipcMain;
const objectsRegistry = require('./objects-registry');
const v8Util = process.atomBinding('v8_util');
const IDWeakMap = process.atomBinding('id_weak_map').IDWeakMap;
2016-01-12 02:40:23 +00:00
2016-01-14 21:21:11 +00:00
var slice = [].slice;
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Convert a real value into meta data.
2016-01-14 21:21:11 +00:00
var valueToMeta = function(sender, value, optimizeSimpleObject) {
2016-01-12 02:40:23 +00:00
var el, field, i, len, meta, name;
if (optimizeSimpleObject == null) {
optimizeSimpleObject = false;
}
meta = {
type: typeof value
};
if (Buffer.isBuffer(value)) {
meta.type = 'buffer';
}
if (value === null) {
meta.type = 'value';
}
if (Array.isArray(value)) {
meta.type = 'array';
}
if (value instanceof Error) {
meta.type = 'error';
}
if (value instanceof Date) {
meta.type = 'date';
}
if ((value != null ? value.constructor.name : void 0) === 'Promise') {
meta.type = 'promise';
}
2016-01-14 18:35:29 +00:00
// Treat simple objects as value.
2016-01-12 02:40:23 +00:00
if (optimizeSimpleObject && meta.type === 'object' && v8Util.getHiddenValue(value, 'simple')) {
meta.type = 'value';
}
2016-01-14 18:35:29 +00:00
// Treat the arguments object as array.
if (meta.type === 'object' && (value.hasOwnProperty('callee')) && (value.length != null)) {
2016-01-12 02:40:23 +00:00
meta.type = 'array';
}
if (meta.type === 'array') {
meta.members = [];
for (i = 0, len = value.length; i < len; i++) {
el = value[i];
meta.members.push(valueToMeta(sender, el));
}
} else if (meta.type === 'object' || meta.type === 'function') {
meta.name = value.constructor.name;
2016-01-14 18:44:21 +00:00
// 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.
2016-01-12 02:40:23 +00:00
meta.id = objectsRegistry.add(sender.getId(), value);
meta.members = (function() {
var results;
results = [];
for (name in value) {
field = value[name];
results.push({
name: name,
type: typeof field
});
}
return results;
})();
} else if (meta.type === 'buffer') {
meta.value = Array.prototype.slice.call(value, 0);
} else if (meta.type === 'promise') {
meta.then = valueToMeta(sender, value.then.bind(value));
} else if (meta.type === 'error') {
meta.members = plainObjectToMeta(value);
2016-01-14 18:35:29 +00:00
// Error.name is not part of own properties.
2016-01-12 02:40:23 +00:00
meta.members.push({
name: 'name',
value: value.name
});
} else if (meta.type === 'date') {
meta.value = value.getTime();
} else {
meta.type = 'value';
meta.value = value;
}
return meta;
};
2016-01-14 18:35:29 +00:00
// Convert object to meta by value.
2016-01-14 21:21:11 +00:00
var plainObjectToMeta = function(obj) {
2016-01-12 02:40:23 +00:00
return Object.getOwnPropertyNames(obj).map(function(name) {
return {
name: name,
value: obj[name]
};
});
};
2016-01-14 18:35:29 +00:00
// Convert Error into meta data.
2016-01-14 21:21:11 +00:00
var exceptionToMeta = function(error) {
2016-01-12 02:40:23 +00:00
return {
type: 'exception',
message: error.message,
stack: error.stack || error
};
};
2016-01-14 18:35:29 +00:00
// Convert array of meta data from renderer into array of real values.
2016-01-14 21:21:11 +00:00
var unwrapArgs = function(sender, args) {
2016-01-12 02:40:23 +00:00
var metaToValue;
metaToValue = function(meta) {
var i, len, member, ref, rendererReleased, returnValue;
2016-01-12 02:40:23 +00:00
switch (meta.type) {
case 'value':
return meta.value;
case 'remote-object':
return objectsRegistry.get(meta.id);
case 'array':
return unwrapArgs(sender, meta.value);
case 'buffer':
return new Buffer(meta.value);
case 'date':
return new Date(meta.value);
case 'promise':
return Promise.resolve({
then: metaToValue(meta.then)
});
case 'object': {
let ret = v8Util.createObjectWithName(meta.name);
2016-01-12 02:40:23 +00:00
ref = meta.members;
for (i = 0, len = ref.length; i < len; i++) {
member = ref[i];
ret[member.name] = metaToValue(member.value);
}
return ret;
}
2016-01-12 02:40:23 +00:00
case 'function-with-return-value':
returnValue = metaToValue(meta.value);
return function() {
return returnValue;
};
case 'function': {
2016-01-14 18:35:29 +00:00
// Cache the callbacks in renderer.
2016-01-12 02:40:23 +00:00
if (!sender.callbacks) {
sender.callbacks = new IDWeakMap;
sender.on('render-view-deleted', function() {
return this.callbacks.clear();
2016-01-12 02:40:23 +00:00
});
}
if (sender.callbacks.has(meta.id))
2016-01-12 02:40:23 +00:00
return sender.callbacks.get(meta.id);
// Prevent the callback from being called when its page is gone.
2016-01-12 02:40:23 +00:00
rendererReleased = false;
sender.once('render-view-deleted', function() {
rendererReleased = true;
2016-01-12 02:40:23 +00:00
});
let callIntoRenderer = function(...args) {
if (rendererReleased || sender.isDestroyed())
throw new Error(`Attempting to call a function in a renderer window that has been closed or released. Function provided here: ${meta.location}.`);
sender.send('ATOM_RENDERER_CALLBACK', meta.id, valueToMeta(sender, args));
2016-01-12 02:40:23 +00:00
};
v8Util.setDestructor(callIntoRenderer, function() {
if (!rendererReleased && !sender.isDestroyed())
sender.send('ATOM_RENDERER_RELEASE_CALLBACK', meta.id);
2016-01-12 02:40:23 +00:00
});
sender.callbacks.set(meta.id, callIntoRenderer);
return callIntoRenderer;
}
2016-01-12 02:40:23 +00:00
default:
throw new TypeError("Unknown type: " + meta.type);
}
};
return args.map(metaToValue);
};
2016-01-14 18:44:21 +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.
2016-01-14 21:21:11 +00:00
var callFunction = function(event, func, caller, args) {
2016-01-19 22:49:40 +00:00
var funcMarkedAsync, funcName, funcPassedCallback, ref, ret;
2016-01-12 02:40:23 +00:00
funcMarkedAsync = v8Util.getHiddenValue(func, 'asynchronous');
funcPassedCallback = typeof args[args.length - 1] === 'function';
try {
if (funcMarkedAsync && !funcPassedCallback) {
args.push(function(ret) {
return event.returnValue = valueToMeta(event.sender, ret, true);
});
return func.apply(caller, args);
} else {
ret = func.apply(caller, args);
return event.returnValue = valueToMeta(event.sender, ret, true);
}
2016-01-19 22:49:40 +00:00
} catch (error) {
2016-01-14 18:44:21 +00:00
// 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.`
2016-01-12 02:40:23 +00:00
funcName = (ref = func.name) != null ? ref : "anonymous";
2016-01-19 23:11:55 +00:00
throw new Error("Could not call remote function `" + funcName + "`. Check that the function signature is correct. Underlying error: " + error.message);
2016-01-12 02:40:23 +00:00
}
};
2016-01-14 18:35:29 +00:00
// Send by BrowserWindow when its render view is deleted.
2016-01-12 02:40:23 +00:00
process.on('ATOM_BROWSER_RELEASE_RENDER_VIEW', function(id) {
return objectsRegistry.clear(id);
});
ipcMain.on('ATOM_BROWSER_REQUIRE', function(event, module) {
try {
return event.returnValue = valueToMeta(event.sender, process.mainModule.require(module));
2016-01-19 22:49:40 +00:00
} catch (error) {
return event.returnValue = exceptionToMeta(error);
2016-01-12 02:40:23 +00:00
}
});
ipcMain.on('ATOM_BROWSER_GET_BUILTIN', function(event, module) {
try {
return event.returnValue = valueToMeta(event.sender, electron[module]);
2016-01-19 22:49:40 +00:00
} catch (error) {
return event.returnValue = exceptionToMeta(error);
2016-01-12 02:40:23 +00:00
}
});
ipcMain.on('ATOM_BROWSER_GLOBAL', function(event, name) {
try {
return event.returnValue = valueToMeta(event.sender, global[name]);
2016-01-19 22:49:40 +00:00
} catch (error) {
return event.returnValue = exceptionToMeta(error);
2016-01-12 02:40:23 +00:00
}
});
ipcMain.on('ATOM_BROWSER_CURRENT_WINDOW', function(event) {
try {
return event.returnValue = valueToMeta(event.sender, event.sender.getOwnerBrowserWindow());
2016-01-19 22:49:40 +00:00
} catch (error) {
return event.returnValue = exceptionToMeta(error);
2016-01-12 02:40:23 +00:00
}
});
ipcMain.on('ATOM_BROWSER_CURRENT_WEB_CONTENTS', function(event) {
return event.returnValue = valueToMeta(event.sender, event.sender);
});
ipcMain.on('ATOM_BROWSER_CONSTRUCTOR', function(event, id, args) {
2016-01-19 22:49:40 +00:00
var constructor, obj;
2016-01-12 02:40:23 +00:00
try {
args = unwrapArgs(event.sender, args);
constructor = objectsRegistry.get(id);
2016-01-14 18:44:21 +00:00
// Call new with array of arguments.
// http://stackoverflow.com/questions/1606797/use-of-apply-with-new-operator-is-this-possible
2016-01-12 02:40:23 +00:00
obj = new (Function.prototype.bind.apply(constructor, [null].concat(args)));
return event.returnValue = valueToMeta(event.sender, obj);
2016-01-19 22:49:40 +00:00
} catch (error) {
return event.returnValue = exceptionToMeta(error);
2016-01-12 02:40:23 +00:00
}
});
ipcMain.on('ATOM_BROWSER_FUNCTION_CALL', function(event, id, args) {
2016-01-19 22:49:40 +00:00
var func;
2016-01-12 02:40:23 +00:00
try {
args = unwrapArgs(event.sender, args);
func = objectsRegistry.get(id);
return callFunction(event, func, global, args);
2016-01-19 22:49:40 +00:00
} catch (error) {
return event.returnValue = exceptionToMeta(error);
2016-01-12 02:40:23 +00:00
}
});
ipcMain.on('ATOM_BROWSER_MEMBER_CONSTRUCTOR', function(event, id, method, args) {
2016-01-19 22:49:40 +00:00
var constructor, obj;
2016-01-12 02:40:23 +00:00
try {
args = unwrapArgs(event.sender, args);
constructor = objectsRegistry.get(id)[method];
2016-01-14 18:35:29 +00:00
// Call new with array of arguments.
2016-01-12 02:40:23 +00:00
obj = new (Function.prototype.bind.apply(constructor, [null].concat(args)));
return event.returnValue = valueToMeta(event.sender, obj);
2016-01-19 22:49:40 +00:00
} catch (error) {
return event.returnValue = exceptionToMeta(error);
2016-01-12 02:40:23 +00:00
}
});
ipcMain.on('ATOM_BROWSER_MEMBER_CALL', function(event, id, method, args) {
2016-01-19 22:49:40 +00:00
var obj;
2016-01-12 02:40:23 +00:00
try {
args = unwrapArgs(event.sender, args);
obj = objectsRegistry.get(id);
return callFunction(event, obj[method], obj, args);
2016-01-19 22:49:40 +00:00
} catch (error) {
return event.returnValue = exceptionToMeta(error);
2016-01-12 02:40:23 +00:00
}
});
ipcMain.on('ATOM_BROWSER_MEMBER_SET', function(event, id, name, value) {
2016-01-19 22:49:40 +00:00
var obj;
2016-01-12 02:40:23 +00:00
try {
obj = objectsRegistry.get(id);
obj[name] = value;
return event.returnValue = null;
2016-01-19 22:49:40 +00:00
} catch (error) {
return event.returnValue = exceptionToMeta(error);
2016-01-12 02:40:23 +00:00
}
});
ipcMain.on('ATOM_BROWSER_MEMBER_GET', function(event, id, name) {
2016-01-19 22:49:40 +00:00
var obj;
2016-01-12 02:40:23 +00:00
try {
obj = objectsRegistry.get(id);
return event.returnValue = valueToMeta(event.sender, obj[name]);
2016-01-19 22:49:40 +00:00
} catch (error) {
return event.returnValue = exceptionToMeta(error);
2016-01-12 02:40:23 +00:00
}
});
ipcMain.on('ATOM_BROWSER_DEREFERENCE', function(event, id) {
return objectsRegistry.remove(event.sender.getId(), id);
});
ipcMain.on('ATOM_BROWSER_GUEST_WEB_CONTENTS', function(event, guestInstanceId) {
2016-01-19 22:49:40 +00:00
var guestViewManager;
2016-01-12 02:40:23 +00:00
try {
guestViewManager = require('./guest-view-manager');
return event.returnValue = valueToMeta(event.sender, guestViewManager.getGuest(guestInstanceId));
2016-01-19 22:49:40 +00:00
} catch (error) {
return event.returnValue = exceptionToMeta(error);
2016-01-12 02:40:23 +00:00
}
});
ipcMain.on('ATOM_BROWSER_ASYNC_CALL_TO_GUEST_VIEW', function() {
2016-01-19 22:49:40 +00:00
var args, event, guest, guestInstanceId, guestViewManager, method;
2016-01-12 02:40:23 +00:00
event = arguments[0], guestInstanceId = arguments[1], method = arguments[2], args = 4 <= arguments.length ? slice.call(arguments, 3) : [];
try {
guestViewManager = require('./guest-view-manager');
guest = guestViewManager.getGuest(guestInstanceId);
return guest[method].apply(guest, args);
2016-01-19 22:49:40 +00:00
} catch (error) {
return event.returnValue = exceptionToMeta(error);
2016-01-12 02:40:23 +00:00
}
});