Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add process.contextId used by @electron/remote #28007

Merged
merged 2 commits into from Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/api/process.md
Expand Up @@ -35,6 +35,7 @@ In sandboxed renderers the `process` object contains only a subset of the APIs:
* `versions`
* `mas`
* `windowsStore`
* `contextId`

## Events

Expand Down Expand Up @@ -133,6 +134,13 @@ A `String` representing Electron's version string.
A `Boolean`. If the app is running as a Windows Store app (appx), this property is `true`,
for otherwise it is `undefined`.

### `process.contextId` _Readonly_

A `String` (optional) representing a globally unique ID of the current JavaScript context.
miniak marked this conversation as resolved.
Show resolved Hide resolved
Each frame has its own JavaScript context. When contextIsolation is enabled, the isolated
world also has a separate JavaScript context.
This property is only available in the renderer process.

## Methods

The `process` object has the following methods:
Expand Down
4 changes: 4 additions & 0 deletions lib/renderer/init.ts
Expand Up @@ -39,6 +39,10 @@ require('@electron/internal/common/init');
// The global variable will be used by ipc for event dispatching
const v8Util = process._linkedBinding('electron_common_v8_util');

// Expose process.contextId
const contextId = v8Util.getHiddenValue<string>(global, 'contextId');
Object.defineProperty(process, 'contextId', { enumerable: true, value: contextId });

const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal');
const ipcRenderer = require('@electron/internal/renderer/api/ipc-renderer').default;

Expand Down
4 changes: 4 additions & 0 deletions lib/sandboxed_renderer/init.ts
Expand Up @@ -89,6 +89,10 @@ Object.defineProperty(preloadProcess, 'noDeprecation', {
}
});

// Expose process.contextId
const contextId = v8Util.getHiddenValue<string>(global, 'contextId');
Object.defineProperty(preloadProcess, 'contextId', { enumerable: true, value: contextId });

process.on('loaded', () => (preloadProcess as events.EventEmitter).emit('loaded'));
process.on('exit', () => (preloadProcess as events.EventEmitter).emit('exit'));
(process as events.EventEmitter).on('document-start', () => (preloadProcess as events.EventEmitter).emit('document-start'));
Expand Down
1 change: 1 addition & 0 deletions spec-main/api-browser-window-spec.ts
Expand Up @@ -2623,6 +2623,7 @@ describe('BrowserWindow module', () => {
expect(test.type).to.equal('renderer');
expect(test.version).to.equal(process.version);
expect(test.versions).to.deep.equal(process.versions);
expect(test.contextId).to.be.a('string');

if (process.platform === 'linux' && test.osSandbox) {
expect(test.creationTime).to.be.null('creation time');
Expand Down
3 changes: 2 additions & 1 deletion spec-main/fixtures/module/preload-sandbox.js
Expand Up @@ -42,7 +42,8 @@
sandboxed: process.sandboxed,
type: process.type,
version: process.version,
versions: process.versions
versions: process.versions,
contextId: process.contextId
};
}
} else if (location.href !== 'about:blank') {
Expand Down
6 changes: 6 additions & 0 deletions spec/api-process-spec.js
Expand Up @@ -115,4 +115,10 @@ describe('process module', () => {
expect(success).to.be.false();
});
});

describe('process.contextId', () => {
it('is a string', () => {
expect(process.contextId).to.be.a('string');
});
});
});