electron/atom/renderer/api/lib/remote.js

347 lines
9.7 KiB
JavaScript
Raw Normal View History

2016-01-14 22:20:06 +00:00
const ipcRenderer = require('electron').ipcRenderer;
const CallbacksRegistry = require('electron').CallbacksRegistry;
const v8Util = process.atomBinding('v8_util');
2016-01-12 02:40:23 +00:00
2016-01-14 22:20:06 +00:00
const callbacksRegistry = new CallbacksRegistry;
2016-01-12 02:40:23 +00:00
2016-01-15 17:24:53 +00:00
var indexOf = [].indexOf;
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Check for circular reference.
2016-01-14 22:20:06 +00:00
var isCircular = function(field, visited) {
2016-01-12 02:40:23 +00:00
if (typeof field === 'object') {
if (indexOf.call(visited, field) >= 0) {
return true;
}
visited.push(field);
}
return false;
};
2016-01-14 18:35:29 +00:00
// Convert the arguments object into an array of meta data.
2016-01-14 22:20:06 +00:00
var wrapArgs = function(args, visited) {
2016-01-12 02:40:23 +00:00
var valueToMeta;
if (visited == null) {
visited = [];
}
valueToMeta = function(value) {
var field, prop, ret;
if (Array.isArray(value)) {
return {
type: 'array',
value: wrapArgs(value, visited)
};
} else if (Buffer.isBuffer(value)) {
return {
type: 'buffer',
value: Array.prototype.slice.call(value, 0)
};
} else if (value instanceof Date) {
return {
type: 'date',
value: value.getTime()
};
} else if ((value != null ? value.constructor.name : void 0) === 'Promise') {
return {
type: 'promise',
then: valueToMeta(value.then.bind(value))
};
} else if ((value != null) && typeof value === 'object' && v8Util.getHiddenValue(value, 'atomId')) {
return {
type: 'remote-object',
id: v8Util.getHiddenValue(value, 'atomId')
};
} else if ((value != null) && typeof value === 'object') {
ret = {
type: 'object',
name: value.constructor.name,
members: []
};
for (prop in value) {
field = value[prop];
ret.members.push({
name: prop,
value: valueToMeta(isCircular(field, visited) ? null : field)
});
}
return ret;
} else if (typeof value === 'function' && v8Util.getHiddenValue(value, 'returnValue')) {
return {
type: 'function-with-return-value',
value: valueToMeta(value())
};
} else if (typeof value === 'function') {
return {
type: 'function',
id: callbacksRegistry.add(value),
location: v8Util.getHiddenValue(value, 'location')
};
} else {
return {
type: 'value',
value: value
};
}
};
return Array.prototype.slice.call(args).map(valueToMeta);
};
2016-01-14 18:35:29 +00:00
// Convert meta data from browser into real value.
2016-01-14 22:20:06 +00:00
var metaToValue = function(meta) {
2016-01-12 02:40:23 +00:00
var RemoteFunction, el, i, j, len, len1, member, ref1, ref2, results, ret;
switch (meta.type) {
case 'value':
return meta.value;
case 'array':
ref1 = meta.members;
results = [];
for (i = 0, len = ref1.length; i < len; i++) {
el = ref1[i];
results.push(metaToValue(el));
}
return results;
case 'buffer':
return new Buffer(meta.value);
case 'promise':
return Promise.resolve({
then: metaToValue(meta.then)
});
case 'error':
return metaToPlainObject(meta);
case 'date':
return new Date(meta.value);
case 'exception':
throw new Error(meta.message + "\n" + meta.stack);
break;
default:
if (meta.type === 'function') {
2016-01-14 18:35:29 +00:00
// A shadow class to represent the remote function object.
2016-01-12 02:40:23 +00:00
ret = RemoteFunction = (function() {
function RemoteFunction() {
var obj;
if (this.constructor === RemoteFunction) {
2016-01-14 18:35:29 +00:00
// Constructor call.
2016-01-12 02:40:23 +00:00
obj = ipcRenderer.sendSync('ATOM_BROWSER_CONSTRUCTOR', meta.id, wrapArgs(arguments));
/*
Returning object in constructor will replace constructed object
with the returned object.
http://stackoverflow.com/questions/1978049/what-values-can-a-constructor-return-to-avoid-returning-this
*/
return metaToValue(obj);
} else {
2016-01-14 18:35:29 +00:00
// Function call.
2016-01-12 02:40:23 +00:00
obj = ipcRenderer.sendSync('ATOM_BROWSER_FUNCTION_CALL', meta.id, wrapArgs(arguments));
return metaToValue(obj);
}
}
return RemoteFunction;
})();
} else {
ret = v8Util.createObjectWithName(meta.name);
}
2016-01-14 18:35:29 +00:00
// Polulate delegate members.
2016-01-12 02:40:23 +00:00
ref2 = meta.members;
for (j = 0, len1 = ref2.length; j < len1; j++) {
member = ref2[j];
if (member.type === 'function') {
ret[member.name] = createRemoteMemberFunction(meta.id, member.name);
} else {
Object.defineProperty(ret, member.name, createRemoteMemberProperty(meta.id, member.name));
}
}
2016-01-14 19:10:12 +00:00
// Track delegate object's life time, and tell the browser to clean up
// when the object is GCed.
2016-01-12 02:40:23 +00:00
v8Util.setDestructor(ret, function() {
return ipcRenderer.send('ATOM_BROWSER_DEREFERENCE', meta.id);
});
2016-01-14 18:35:29 +00:00
// Remember object's id.
2016-01-12 02:40:23 +00:00
v8Util.setHiddenValue(ret, 'atomId', meta.id);
return ret;
}
};
2016-01-14 18:35:29 +00:00
// Construct a plain object from the meta.
2016-01-14 22:20:06 +00:00
var metaToPlainObject = function(meta) {
2016-01-12 02:40:23 +00:00
var i, len, name, obj, ref1, ref2, value;
obj = (function() {
switch (meta.type) {
case 'error':
return new Error;
default:
return {};
}
})();
ref1 = meta.members;
for (i = 0, len = ref1.length; i < len; i++) {
ref2 = ref1[i], name = ref2.name, value = ref2.value;
obj[name] = value;
}
return obj;
};
2016-01-14 19:10:12 +00:00
// Create a RemoteMemberFunction instance.
// This function's content should not be inlined into metaToValue, otherwise V8
// may consider it circular reference.
2016-01-14 22:20:06 +00:00
var createRemoteMemberFunction = function(metaId, name) {
2016-01-12 02:40:23 +00:00
var RemoteMemberFunction;
return RemoteMemberFunction = (function() {
function RemoteMemberFunction() {
var ret;
if (this.constructor === RemoteMemberFunction) {
2016-01-14 18:35:29 +00:00
// Constructor call.
2016-01-12 02:40:23 +00:00
ret = ipcRenderer.sendSync('ATOM_BROWSER_MEMBER_CONSTRUCTOR', metaId, name, wrapArgs(arguments));
return metaToValue(ret);
} else {
2016-01-14 18:35:29 +00:00
// Call member function.
2016-01-12 02:40:23 +00:00
ret = ipcRenderer.sendSync('ATOM_BROWSER_MEMBER_CALL', metaId, name, wrapArgs(arguments));
return metaToValue(ret);
}
}
return RemoteMemberFunction;
})();
};
2016-01-14 19:10:12 +00:00
// Create configuration for defineProperty.
// This function's content should not be inlined into metaToValue, otherwise V8
// may consider it circular reference.
2016-01-14 22:20:06 +00:00
var createRemoteMemberProperty = function(metaId, name) {
2016-01-12 02:40:23 +00:00
return {
enumerable: true,
configurable: false,
set: function(value) {
2016-01-14 18:35:29 +00:00
// Set member data.
2016-01-12 02:40:23 +00:00
ipcRenderer.sendSync('ATOM_BROWSER_MEMBER_SET', metaId, name, value);
return value;
},
get: function() {
2016-01-14 18:35:29 +00:00
// Get member data.
2016-01-12 02:40:23 +00:00
return metaToValue(ipcRenderer.sendSync('ATOM_BROWSER_MEMBER_GET', metaId, name));
}
};
};
2016-01-14 18:35:29 +00:00
// Browser calls a callback in renderer.
2016-01-12 02:40:23 +00:00
ipcRenderer.on('ATOM_RENDERER_CALLBACK', function(event, id, args) {
return callbacksRegistry.apply(id, metaToValue(args));
});
2016-01-14 18:35:29 +00:00
// A callback in browser is released.
2016-01-12 02:40:23 +00:00
ipcRenderer.on('ATOM_RENDERER_RELEASE_CALLBACK', function(event, id) {
return callbacksRegistry.remove(id);
});
2016-01-14 18:35:29 +00:00
// List all built-in modules in browser process.
2016-01-14 22:20:06 +00:00
const browserModules = require('../../../browser/api/lib/exports/electron');
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// And add a helper receiver for each one.
2016-01-14 22:20:06 +00:00
var fn = function(name) {
2016-01-12 02:40:23 +00:00
return Object.defineProperty(exports, name, {
get: function() {
return exports.getBuiltin(name);
}
});
};
2016-01-14 22:20:06 +00:00
for (var name in browserModules) {
2016-01-12 02:40:23 +00:00
fn(name);
}
2016-01-14 19:10:12 +00:00
// Get remote module.
// (Just like node's require, the modules are cached permanently, note that this
// is safe leak since the object is not expected to get freed in browser)
2016-01-14 22:20:06 +00:00
var moduleCache = {};
2016-01-12 02:40:23 +00:00
exports.require = function(module) {
var meta;
if (moduleCache[module] != null) {
return moduleCache[module];
}
meta = ipcRenderer.sendSync('ATOM_BROWSER_REQUIRE', module);
return moduleCache[module] = metaToValue(meta);
};
2016-01-14 18:35:29 +00:00
// Optimize require('electron').
2016-01-12 02:40:23 +00:00
moduleCache.electron = exports;
2016-01-14 18:35:29 +00:00
// Alias to remote.require('electron').xxx.
2016-01-14 22:20:06 +00:00
var builtinCache = {};
2016-01-12 02:40:23 +00:00
exports.getBuiltin = function(module) {
var meta;
if (builtinCache[module] != null) {
return builtinCache[module];
}
meta = ipcRenderer.sendSync('ATOM_BROWSER_GET_BUILTIN', module);
return builtinCache[module] = metaToValue(meta);
};
2016-01-14 18:35:29 +00:00
// Get current BrowserWindow object.
2016-01-14 22:20:06 +00:00
var windowCache = null;
2016-01-12 02:40:23 +00:00
exports.getCurrentWindow = function() {
var meta;
if (windowCache != null) {
return windowCache;
}
meta = ipcRenderer.sendSync('ATOM_BROWSER_CURRENT_WINDOW');
return windowCache = metaToValue(meta);
};
2016-01-14 18:35:29 +00:00
// Get current WebContents object.
2016-01-14 22:20:06 +00:00
var webContentsCache = null;
2016-01-12 02:40:23 +00:00
exports.getCurrentWebContents = function() {
var meta;
if (webContentsCache != null) {
return webContentsCache;
}
meta = ipcRenderer.sendSync('ATOM_BROWSER_CURRENT_WEB_CONTENTS');
return webContentsCache = metaToValue(meta);
};
2016-01-14 18:35:29 +00:00
// Get a global object in browser.
2016-01-12 02:40:23 +00:00
exports.getGlobal = function(name) {
var meta;
meta = ipcRenderer.sendSync('ATOM_BROWSER_GLOBAL', name);
return metaToValue(meta);
};
2016-01-14 18:35:29 +00:00
// Get the process object in browser.
2016-01-14 22:20:06 +00:00
var processCache = null;
2016-01-12 02:40:23 +00:00
exports.__defineGetter__('process', function() {
if (processCache == null) {
processCache = exports.getGlobal('process');
}
return processCache;
});
2016-01-14 18:35:29 +00:00
// Create a funtion that will return the specifed value when called in browser.
2016-01-12 02:40:23 +00:00
exports.createFunctionWithReturnValue = function(returnValue) {
var func;
func = function() {
return returnValue;
};
v8Util.setHiddenValue(func, 'returnValue', true);
return func;
};
2016-01-14 18:35:29 +00:00
// Get the guest WebContents from guestInstanceId.
2016-01-12 02:40:23 +00:00
exports.getGuestWebContents = function(guestInstanceId) {
var meta;
meta = ipcRenderer.sendSync('ATOM_BROWSER_GUEST_WEB_CONTENTS', guestInstanceId);
return metaToValue(meta);
};