2020-06-11 18:36:03 +00:00
|
|
|
import { CallbacksRegistry } from '../remote/callbacks-registry';
|
|
|
|
import { isPromise, isSerializableObject, serialize, deserialize } from '../../common/type-utils';
|
|
|
|
import { MetaTypeFromRenderer, ObjectMember, ObjProtoDescriptor, MetaType } from '../../common/remote/types';
|
|
|
|
import { ipcRendererInternal } from '../ipc-renderer-internal';
|
|
|
|
import type { BrowserWindow, WebContents } from 'electron/main';
|
|
|
|
import { browserModuleNames } from '@electron/internal/browser/api/module-names';
|
|
|
|
import { commonModuleList } from '@electron/internal/common/api/module-list';
|
2016-02-22 02:52:21 +00:00
|
|
|
|
2020-06-23 03:32:45 +00:00
|
|
|
const v8Util = process._linkedBinding('electron_common_v8_util');
|
|
|
|
const { hasSwitch } = process._linkedBinding('electron_common_command_line');
|
|
|
|
const { NativeImage } = process._linkedBinding('electron_common_native_image');
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const callbacksRegistry = new CallbacksRegistry();
|
2020-06-12 22:50:03 +00:00
|
|
|
const remoteObjectCache = new Map();
|
|
|
|
const finalizationRegistry = new (window as any).FinalizationRegistry((id: number) => {
|
|
|
|
const ref = remoteObjectCache.get(id);
|
|
|
|
if (ref !== undefined && ref.deref() === undefined) {
|
|
|
|
remoteObjectCache.delete(id);
|
|
|
|
ipcRendererInternal.send('ELECTRON_BROWSER_DEREFERENCE', contextId, id, 0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function getCachedRemoteObject (id: number) {
|
|
|
|
const ref = remoteObjectCache.get(id);
|
|
|
|
if (ref !== undefined) {
|
|
|
|
const deref = ref.deref();
|
|
|
|
if (deref !== undefined) return deref;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function setCachedRemoteObject (id: number, value: any) {
|
|
|
|
const wr = new (window as any).WeakRef(value);
|
|
|
|
remoteObjectCache.set(id, wr);
|
|
|
|
finalizationRegistry.register(value, id);
|
|
|
|
return value;
|
|
|
|
}
|
2016-02-17 07:57:46 +00:00
|
|
|
|
2018-07-10 08:15:40 +00:00
|
|
|
// An unique ID that can represent current context.
|
2020-06-11 18:36:03 +00:00
|
|
|
const contextId = v8Util.getHiddenValue<string>(global, 'contextId');
|
2018-07-10 08:15:40 +00:00
|
|
|
|
|
|
|
// Notify the main process when current context is going to be released.
|
|
|
|
// Note that when the renderer process is destroyed, the message may not be
|
|
|
|
// sent, we also listen to the "render-view-deleted" event in the main process
|
|
|
|
// to guard that situation.
|
|
|
|
process.on('exit', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const command = 'ELECTRON_BROWSER_CONTEXT_RELEASE';
|
|
|
|
ipcRendererInternal.send(command, contextId);
|
|
|
|
});
|
2018-07-10 08:15:40 +00:00
|
|
|
|
2020-03-25 20:13:10 +00:00
|
|
|
const IS_REMOTE_PROXY = Symbol('is-remote-proxy');
|
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Convert the arguments object into an array of meta data.
|
2020-06-11 18:36:03 +00:00
|
|
|
function wrapArgs (args: any[], visited = new Set()): any {
|
|
|
|
const valueToMeta = (value: any): any => {
|
2016-07-11 17:39:21 +00:00
|
|
|
// Check for circular reference.
|
|
|
|
if (visited.has(value)) {
|
2016-01-12 02:40:23 +00:00
|
|
|
return {
|
2016-07-11 17:28:01 +00:00
|
|
|
type: 'value',
|
|
|
|
value: null
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2016-07-11 17:28:01 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 16:29:24 +00:00
|
|
|
if (value instanceof NativeImage) {
|
2020-05-22 15:56:57 +00:00
|
|
|
return { type: 'nativeimage', value: serialize(value) };
|
2020-05-18 16:29:24 +00:00
|
|
|
} else if (Array.isArray(value)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
visited.add(value);
|
2018-10-02 01:56:31 +00:00
|
|
|
const meta = {
|
2016-01-12 02:40:23 +00:00
|
|
|
type: 'array',
|
2016-07-11 17:28:01 +00:00
|
|
|
value: wrapArgs(value, visited)
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
|
|
|
visited.delete(value);
|
|
|
|
return meta;
|
2019-10-10 13:59:08 +00:00
|
|
|
} else if (value instanceof Buffer) {
|
2016-01-12 02:40:23 +00:00
|
|
|
return {
|
|
|
|
type: 'buffer',
|
2019-10-10 13:59:08 +00:00
|
|
|
value
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2019-10-10 13:59:08 +00:00
|
|
|
} else if (isSerializableObject(value)) {
|
2016-01-12 02:40:23 +00:00
|
|
|
return {
|
2019-10-10 13:59:08 +00:00
|
|
|
type: 'value',
|
|
|
|
value
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2019-10-10 13:59:08 +00:00
|
|
|
} else if (typeof value === 'object') {
|
2016-05-25 05:38:35 +00:00
|
|
|
if (isPromise(value)) {
|
2016-04-20 05:26:49 +00:00
|
|
|
return {
|
|
|
|
type: 'promise',
|
2020-06-11 18:36:03 +00:00
|
|
|
then: valueToMeta(function (onFulfilled: Function, onRejected: Function) {
|
2020-03-20 20:28:31 +00:00
|
|
|
value.then(onFulfilled, onRejected);
|
2016-05-23 22:07:01 +00:00
|
|
|
})
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2020-04-13 21:32:29 +00:00
|
|
|
} else if (v8Util.getHiddenValue(value, 'electronId')) {
|
2016-04-20 05:26:49 +00:00
|
|
|
return {
|
|
|
|
type: 'remote-object',
|
2020-04-13 21:32:29 +00:00
|
|
|
id: v8Util.getHiddenValue(value, 'electronId')
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2016-04-20 05:26:49 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 18:36:03 +00:00
|
|
|
const meta: MetaTypeFromRenderer = {
|
2016-01-12 02:40:23 +00:00
|
|
|
type: 'object',
|
2017-10-26 03:41:11 +00:00
|
|
|
name: value.constructor ? value.constructor.name : '',
|
2016-01-12 02:40:23 +00:00
|
|
|
members: []
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
|
|
|
visited.add(value);
|
2020-03-17 20:17:55 +00:00
|
|
|
for (const prop in value) { // eslint-disable-line guard-for-in
|
2016-07-11 17:28:01 +00:00
|
|
|
meta.members.push({
|
2016-01-12 02:40:23 +00:00
|
|
|
name: prop,
|
2016-07-11 17:28:01 +00:00
|
|
|
value: valueToMeta(value[prop])
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
visited.delete(value);
|
|
|
|
return meta;
|
2016-01-12 02:40:23 +00:00
|
|
|
} else if (typeof value === 'function' && v8Util.getHiddenValue(value, 'returnValue')) {
|
|
|
|
return {
|
|
|
|
type: 'function-with-return-value',
|
|
|
|
value: valueToMeta(value())
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2016-01-12 02:40:23 +00:00
|
|
|
} else if (typeof value === 'function') {
|
|
|
|
return {
|
|
|
|
type: 'function',
|
|
|
|
id: callbacksRegistry.add(value),
|
2016-11-09 13:05:46 +00:00
|
|
|
location: v8Util.getHiddenValue(value, 'location'),
|
|
|
|
length: value.length
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2016-01-12 02:40:23 +00:00
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
type: 'value',
|
2019-10-10 13:59:08 +00:00
|
|
|
value
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
|
|
|
return args.map(valueToMeta);
|
2016-03-25 19:57:17 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-02-22 02:52:21 +00:00
|
|
|
// Populate object's members from descriptors.
|
2016-04-01 06:45:36 +00:00
|
|
|
// The |ref| will be kept referenced by |members|.
|
2016-02-22 02:52:21 +00:00
|
|
|
// This matches |getObjectMemebers| in rpc-server.
|
2020-06-11 18:36:03 +00:00
|
|
|
function setObjectMembers (ref: any, object: any, metaId: number, members: ObjectMember[]) {
|
2020-03-20 20:28:31 +00:00
|
|
|
if (!Array.isArray(members)) return;
|
2016-11-15 17:44:41 +00:00
|
|
|
|
2018-10-02 01:56:31 +00:00
|
|
|
for (const member of members) {
|
2020-03-20 20:28:31 +00:00
|
|
|
if (Object.prototype.hasOwnProperty.call(object, member.name)) continue;
|
2016-02-22 02:52:21 +00:00
|
|
|
|
2020-06-11 18:36:03 +00:00
|
|
|
const descriptor: PropertyDescriptor = { enumerable: member.enumerable };
|
2016-02-22 02:52:21 +00:00
|
|
|
if (member.type === 'method') {
|
2020-06-11 18:36:03 +00:00
|
|
|
const remoteMemberFunction = function (this: any, ...args: any[]) {
|
2020-03-20 20:28:31 +00:00
|
|
|
let command;
|
2016-02-22 02:52:21 +00:00
|
|
|
if (this && this.constructor === remoteMemberFunction) {
|
2020-03-20 20:28:31 +00:00
|
|
|
command = 'ELECTRON_BROWSER_MEMBER_CONSTRUCTOR';
|
2016-02-22 02:52:21 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
command = 'ELECTRON_BROWSER_MEMBER_CALL';
|
2016-02-22 02:52:21 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
const ret = ipcRendererInternal.sendSync(command, contextId, metaId, member.name, wrapArgs(args));
|
|
|
|
return metaToValue(ret);
|
|
|
|
};
|
2016-08-16 20:54:21 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
let descriptorFunction = proxyFunctionProperties(remoteMemberFunction, metaId, member.name);
|
2016-08-16 20:54:21 +00:00
|
|
|
|
2017-11-03 01:07:40 +00:00
|
|
|
descriptor.get = () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
descriptorFunction.ref = ref; // The member should reference its object.
|
|
|
|
return descriptorFunction;
|
|
|
|
};
|
2016-04-01 06:26:30 +00:00
|
|
|
// Enable monkey-patch the method
|
2017-11-03 01:07:40 +00:00
|
|
|
descriptor.set = (value) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
descriptorFunction = value;
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
descriptor.configurable = true;
|
2016-02-22 02:52:21 +00:00
|
|
|
} else if (member.type === 'get') {
|
2017-11-03 01:07:40 +00:00
|
|
|
descriptor.get = () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const command = 'ELECTRON_BROWSER_MEMBER_GET';
|
|
|
|
const meta = ipcRendererInternal.sendSync(command, contextId, metaId, member.name);
|
|
|
|
return metaToValue(meta);
|
|
|
|
};
|
2016-02-22 02:52:21 +00:00
|
|
|
|
|
|
|
if (member.writable) {
|
2017-11-03 01:07:40 +00:00
|
|
|
descriptor.set = (value) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const args = wrapArgs([value]);
|
|
|
|
const command = 'ELECTRON_BROWSER_MEMBER_SET';
|
|
|
|
const meta = ipcRendererInternal.sendSync(command, contextId, metaId, member.name, args);
|
|
|
|
if (meta != null) metaToValue(meta);
|
|
|
|
return value;
|
|
|
|
};
|
2016-02-22 02:52:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
Object.defineProperty(object, member.name, descriptor);
|
2016-02-22 02:52:21 +00:00
|
|
|
}
|
2016-03-25 19:57:17 +00:00
|
|
|
}
|
2016-02-22 02:52:21 +00:00
|
|
|
|
|
|
|
// Populate object's prototype from descriptor.
|
|
|
|
// This matches |getObjectPrototype| in rpc-server.
|
2020-06-11 18:36:03 +00:00
|
|
|
function setObjectPrototype (ref: any, object: any, metaId: number, descriptor: ObjProtoDescriptor) {
|
2020-03-20 20:28:31 +00:00
|
|
|
if (descriptor === null) return;
|
|
|
|
const proto = {};
|
|
|
|
setObjectMembers(ref, proto, metaId, descriptor.members);
|
|
|
|
setObjectPrototype(ref, proto, metaId, descriptor.proto);
|
|
|
|
Object.setPrototypeOf(object, proto);
|
2016-03-25 19:57:17 +00:00
|
|
|
}
|
2016-02-22 02:52:21 +00:00
|
|
|
|
2016-08-17 20:58:48 +00:00
|
|
|
// Wrap function in Proxy for accessing remote properties
|
2020-06-11 18:36:03 +00:00
|
|
|
function proxyFunctionProperties (remoteMemberFunction: Function, metaId: number, name: string) {
|
2020-03-20 20:28:31 +00:00
|
|
|
let loaded = false;
|
2016-08-17 21:21:50 +00:00
|
|
|
|
|
|
|
// Lazily load function properties
|
2016-08-17 20:58:48 +00:00
|
|
|
const loadRemoteProperties = () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
if (loaded) return;
|
|
|
|
loaded = true;
|
|
|
|
const command = 'ELECTRON_BROWSER_MEMBER_GET';
|
|
|
|
const meta = ipcRendererInternal.sendSync(command, contextId, metaId, name);
|
|
|
|
setObjectMembers(remoteMemberFunction, remoteMemberFunction, meta.id, meta.members);
|
|
|
|
};
|
2016-08-17 20:58:48 +00:00
|
|
|
|
2020-06-11 18:36:03 +00:00
|
|
|
return new Proxy(remoteMemberFunction as any, {
|
|
|
|
set: (target, property, value) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
if (property !== 'ref') loadRemoteProperties();
|
|
|
|
target[property] = value;
|
|
|
|
return true;
|
2016-09-14 20:21:44 +00:00
|
|
|
},
|
2020-06-11 18:36:03 +00:00
|
|
|
get: (target, property) => {
|
2020-03-25 20:13:10 +00:00
|
|
|
if (property === IS_REMOTE_PROXY) return true;
|
2020-03-20 20:28:31 +00:00
|
|
|
if (!Object.prototype.hasOwnProperty.call(target, property)) loadRemoteProperties();
|
|
|
|
const value = target[property];
|
2017-03-17 17:29:07 +00:00
|
|
|
if (property === 'toString' && typeof value === 'function') {
|
2020-03-20 20:28:31 +00:00
|
|
|
return value.bind(target);
|
2017-03-17 17:29:07 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
return value;
|
2016-08-17 20:58:48 +00:00
|
|
|
},
|
|
|
|
ownKeys: (target) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
loadRemoteProperties();
|
|
|
|
return Object.getOwnPropertyNames(target);
|
2016-08-17 20:58:48 +00:00
|
|
|
},
|
|
|
|
getOwnPropertyDescriptor: (target, property) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const descriptor = Object.getOwnPropertyDescriptor(target, property);
|
|
|
|
if (descriptor) return descriptor;
|
|
|
|
loadRemoteProperties();
|
|
|
|
return Object.getOwnPropertyDescriptor(target, property);
|
2016-08-17 20:58:48 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2016-08-17 20:58:48 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Convert meta data from browser into real value.
|
2020-06-11 18:36:03 +00:00
|
|
|
function metaToValue (meta: MetaType): any {
|
|
|
|
if (meta.type === 'value') {
|
|
|
|
return meta.value;
|
|
|
|
} else if (meta.type === 'array') {
|
|
|
|
return meta.members.map((member) => metaToValue(member));
|
|
|
|
} else if (meta.type === 'nativeimage') {
|
|
|
|
return deserialize(meta.value);
|
|
|
|
} else if (meta.type === 'buffer') {
|
|
|
|
return Buffer.from(meta.value.buffer, meta.value.byteOffset, meta.value.byteLength);
|
|
|
|
} else if (meta.type === 'promise') {
|
|
|
|
return Promise.resolve({ then: metaToValue(meta.then) });
|
|
|
|
} else if (meta.type === 'error') {
|
|
|
|
return metaToError(meta);
|
|
|
|
} else if (meta.type === 'exception') {
|
|
|
|
if (meta.value.type === 'error') { throw metaToError(meta.value); } else { throw new Error(`Unexpected value type in exception: ${meta.value.type}`); }
|
2017-10-25 13:51:21 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
let ret;
|
2020-06-12 22:50:03 +00:00
|
|
|
if ('id' in meta) {
|
|
|
|
const cached = getCachedRemoteObject(meta.id);
|
|
|
|
if (cached !== undefined) { return cached; }
|
2017-11-03 01:07:40 +00:00
|
|
|
}
|
2017-10-25 13:51:21 +00:00
|
|
|
|
|
|
|
// A shadow class to represent the remote function object.
|
|
|
|
if (meta.type === 'function') {
|
2020-06-11 18:36:03 +00:00
|
|
|
const remoteFunction = function (this: any, ...args: any[]) {
|
2020-03-20 20:28:31 +00:00
|
|
|
let command;
|
2017-10-25 13:51:21 +00:00
|
|
|
if (this && this.constructor === remoteFunction) {
|
2020-03-20 20:28:31 +00:00
|
|
|
command = 'ELECTRON_BROWSER_CONSTRUCTOR';
|
2017-10-25 13:51:21 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
command = 'ELECTRON_BROWSER_FUNCTION_CALL';
|
2016-03-25 19:57:17 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
const obj = ipcRendererInternal.sendSync(command, contextId, meta.id, wrapArgs(args));
|
|
|
|
return metaToValue(obj);
|
|
|
|
};
|
|
|
|
ret = remoteFunction;
|
2017-10-25 13:51:21 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
ret = {};
|
2017-10-25 13:51:21 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
setObjectMembers(ret, ret, meta.id, meta.members);
|
|
|
|
setObjectPrototype(ret, ret, meta.id, meta.proto);
|
2020-06-11 18:36:03 +00:00
|
|
|
if (ret.constructor && (ret.constructor as any)[IS_REMOTE_PROXY]) {
|
2020-03-25 20:13:10 +00:00
|
|
|
Object.defineProperty(ret.constructor, 'name', { value: meta.name });
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2017-10-25 13:51:21 +00:00
|
|
|
// Track delegate obj's lifetime & tell browser to clean up when object is GCed.
|
2020-04-13 21:32:29 +00:00
|
|
|
v8Util.setHiddenValue(ret, 'electronId', meta.id);
|
2020-06-12 22:50:03 +00:00
|
|
|
setCachedRemoteObject(meta.id, ret);
|
2020-03-20 20:28:31 +00:00
|
|
|
return ret;
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-25 19:57:17 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2020-06-11 18:36:03 +00:00
|
|
|
function metaToError (meta: { type: 'error', value: any, members: ObjectMember[] }) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const obj = meta.value;
|
2019-10-10 13:59:08 +00:00
|
|
|
for (const { name, value } of meta.members) {
|
2020-03-20 20:28:31 +00:00
|
|
|
obj[name] = metaToValue(value);
|
2017-11-03 01:29:17 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
return obj;
|
2016-03-25 19:57:17 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2020-06-11 18:36:03 +00:00
|
|
|
function handleMessage (channel: string, handler: Function) {
|
2019-02-15 01:24:25 +00:00
|
|
|
ipcRendererInternal.on(channel, (event, passedContextId, id, ...args) => {
|
2018-08-23 23:27:52 +00:00
|
|
|
if (passedContextId === contextId) {
|
2020-03-20 20:28:31 +00:00
|
|
|
handler(id, ...args);
|
2018-10-31 15:26:57 +00:00
|
|
|
} else {
|
|
|
|
// Message sent to an un-exist context, notify the error to main process.
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcRendererInternal.send('ELECTRON_BROWSER_WRONG_CONTEXT_ERROR', contextId, passedContextId, id);
|
2018-08-23 23:27:52 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2018-08-23 23:27:52 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const enableStacks = hasSwitch('enable-api-filtering-logging');
|
2019-10-04 17:49:09 +00:00
|
|
|
|
2020-06-11 18:36:03 +00:00
|
|
|
function getCurrentStack (): string | undefined {
|
|
|
|
const target = { stack: undefined as string | undefined };
|
2019-10-04 17:49:09 +00:00
|
|
|
if (enableStacks) {
|
2020-03-20 20:28:31 +00:00
|
|
|
Error.captureStackTrace(target, getCurrentStack);
|
2019-10-04 17:49:09 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
return target.stack;
|
2019-10-04 17:49:09 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Browser calls a callback in renderer.
|
2020-06-11 18:36:03 +00:00
|
|
|
handleMessage('ELECTRON_RENDERER_CALLBACK', (id: number, args: any) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
callbacksRegistry.apply(id, metaToValue(args));
|
|
|
|
});
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// A callback in browser is released.
|
2020-06-11 18:36:03 +00:00
|
|
|
handleMessage('ELECTRON_RENDERER_RELEASE_CALLBACK', (id: number) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
callbacksRegistry.remove(id);
|
|
|
|
});
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2020-06-11 18:36:03 +00:00
|
|
|
exports.require = (module: string) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const command = 'ELECTRON_BROWSER_REQUIRE';
|
|
|
|
const meta = ipcRendererInternal.sendSync(command, contextId, module, getCurrentStack());
|
|
|
|
return metaToValue(meta);
|
|
|
|
};
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Alias to remote.require('electron').xxx.
|
2020-06-11 18:36:03 +00:00
|
|
|
export function getBuiltin (module: string) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const command = 'ELECTRON_BROWSER_GET_BUILTIN';
|
|
|
|
const meta = ipcRendererInternal.sendSync(command, contextId, module, getCurrentStack());
|
|
|
|
return metaToValue(meta);
|
2020-06-11 18:36:03 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2020-06-11 18:36:03 +00:00
|
|
|
export function getCurrentWindow (): BrowserWindow {
|
2020-03-20 20:28:31 +00:00
|
|
|
const command = 'ELECTRON_BROWSER_CURRENT_WINDOW';
|
|
|
|
const meta = ipcRendererInternal.sendSync(command, contextId, getCurrentStack());
|
|
|
|
return metaToValue(meta);
|
2020-06-11 18:36:03 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Get current WebContents object.
|
2020-06-11 18:36:03 +00:00
|
|
|
export function getCurrentWebContents (): WebContents {
|
2020-03-20 20:28:31 +00:00
|
|
|
const command = 'ELECTRON_BROWSER_CURRENT_WEB_CONTENTS';
|
|
|
|
const meta = ipcRendererInternal.sendSync(command, contextId, getCurrentStack());
|
|
|
|
return metaToValue(meta);
|
2020-06-11 18:36:03 +00:00
|
|
|
}
|
2018-03-20 04:54:47 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Get a global object in browser.
|
2020-06-11 18:36:03 +00:00
|
|
|
export function getGlobal<T = any> (name: string): T {
|
2020-03-20 20:28:31 +00:00
|
|
|
const command = 'ELECTRON_BROWSER_GLOBAL';
|
|
|
|
const meta = ipcRendererInternal.sendSync(command, contextId, name, getCurrentStack());
|
|
|
|
return metaToValue(meta);
|
2020-06-11 18:36:03 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Get the process object in browser.
|
2019-07-09 11:44:46 +00:00
|
|
|
Object.defineProperty(exports, 'process', {
|
|
|
|
get: () => exports.getGlobal('process')
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2017-10-26 03:41:11 +00:00
|
|
|
// Create a function that will return the specified value when called in browser.
|
2020-06-11 18:36:03 +00:00
|
|
|
export function createFunctionWithReturnValue<T> (returnValue: T): () => T {
|
2020-03-20 20:28:31 +00:00
|
|
|
const func = () => returnValue;
|
|
|
|
v8Util.setHiddenValue(func, 'returnValue', true);
|
|
|
|
return func;
|
2020-06-11 18:36:03 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2020-06-11 18:36:03 +00:00
|
|
|
const addBuiltinProperty = (name: string) => {
|
2017-02-23 18:01:27 +00:00
|
|
|
Object.defineProperty(exports, name, {
|
2017-11-03 01:07:40 +00:00
|
|
|
get: () => exports.getBuiltin(name)
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
};
|
2017-02-23 18:01:27 +00:00
|
|
|
|
2020-06-11 18:36:03 +00:00
|
|
|
const browserModules = commonModuleList.concat(browserModuleNames.map(name => ({ name, loader: () => {} })));
|
2017-03-06 14:58:10 +00:00
|
|
|
|
|
|
|
// And add a helper receiver for each one.
|
|
|
|
browserModules
|
|
|
|
.filter((m) => !m.private)
|
|
|
|
.map((m) => m.name)
|
2020-03-20 20:28:31 +00:00
|
|
|
.forEach(addBuiltinProperty);
|