Skip to content

Commit

Permalink
feat: add process.contextId used by @electron/remote (#28007)
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak committed Mar 17, 2021
1 parent 54bc219 commit 485fa5b
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 1 deletion.
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.
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 @@ -2490,6 +2490,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');
});
});
});

0 comments on commit 485fa5b

Please sign in to comment.