Skip to content

Commit 46e8f20

Browse files
committed
sandbox - make acquiring message port from sandbox reusable
1 parent 067261c commit 46e8f20

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import { Event } from 'vs/base/common/event';
7+
import { generateUuid } from 'vs/base/common/uuid';
8+
import { ipcMessagePort, ipcRenderer } from 'vs/base/parts/sandbox/electron-sandbox/globals';
9+
10+
interface IMessageChannelResult {
11+
nonce: string;
12+
port: MessagePort;
13+
source: unknown;
14+
}
15+
16+
export async function acquirePort(requestChannel: string, responseChannel: string): Promise<MessagePort> {
17+
18+
// Ask to create message channel inside the window
19+
// and send over a UUID to correlate the response
20+
const nonce = generateUuid();
21+
ipcMessagePort.acquire(responseChannel, nonce);
22+
ipcRenderer.send(requestChannel, nonce);
23+
24+
// Wait until the main side has returned the `MessagePort`
25+
// We need to filter by the `nonce` to ensure we listen
26+
// to the right response.
27+
const onMessageChannelResult = Event.fromDOMEventEmitter<IMessageChannelResult>(window, 'message', (e: MessageEvent) => ({ nonce: e.data, port: e.ports[0], source: e.source }));
28+
const { port } = await Event.toPromise(Event.once(Event.filter(onMessageChannelResult, e => e.nonce === nonce && e.source === window)));
29+
30+
return port;
31+
}

src/vs/workbench/services/ipc/electron-sandbox/sharedProcessService.ts

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { Event } from 'vs/base/common/event';
7-
import { ipcMessagePort, ipcRenderer } from 'vs/base/parts/sandbox/electron-sandbox/globals';
86
import { Client as MessagePortClient } from 'vs/base/parts/ipc/common/ipc.mp';
97
import { IChannel, IServerChannel, getDelayedChannel } from 'vs/base/parts/ipc/common/ipc';
10-
import { generateUuid } from 'vs/base/common/uuid';
118
import { ILogService } from 'vs/platform/log/common/log';
129
import { Disposable } from 'vs/base/common/lifecycle';
1310
import { ISharedProcessService } from 'vs/platform/ipc/electron-sandbox/services';
1411
import { mark } from 'vs/base/common/performance';
1512
import { Barrier, timeout } from 'vs/base/common/async';
13+
import { acquirePort } from 'vs/base/parts/ipc/electron-sandbox/ipc.mp';
1614

1715
export class SharedProcessService extends Disposable implements ISharedProcessService {
1816

@@ -44,20 +42,9 @@ export class SharedProcessService extends Disposable implements ISharedProcessSe
4442
// is more cruicial.
4543
await Promise.race([this.restoredBarrier.wait(), timeout(2000)]);
4644

45+
// Acquire a message port connected to the shared process
4746
mark('code/willConnectSharedProcess');
48-
49-
// Ask to create message channel inside the window
50-
// and send over a UUID to correlate the response
51-
const nonce = generateUuid();
52-
ipcMessagePort.acquire('vscode:createSharedProcessMessageChannelResult', nonce);
53-
ipcRenderer.send('vscode:createSharedProcessMessageChannel', nonce);
54-
55-
// Wait until the main side has returned the `MessagePort`
56-
// We need to filter by the `nonce` to ensure we listen
57-
// to the right response.
58-
const onMessageChannelResult = Event.fromDOMEventEmitter<{ nonce: string, port: MessagePort, source: unknown }>(window, 'message', (e: MessageEvent) => ({ nonce: e.data, port: e.ports[0], source: e.source }));
59-
const { port } = await Event.toPromise(Event.once(Event.filter(onMessageChannelResult, e => e.nonce === nonce && e.source === window)));
60-
47+
const port = await acquirePort('vscode:createSharedProcessMessageChannel', 'vscode:createSharedProcessMessageChannelResult');
6148
mark('code/didConnectSharedProcess');
6249
this.logService.trace('Renderer->SharedProcess#connect: connection established');
6350

src/vs/workbench/services/textmodelResolver/common/textModelResolverService.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,15 @@ class ResourceModelCollection extends ReferenceCollection<Promise<ITextEditorMod
8989
}
9090

9191
// Track as being disposed before waiting for model to load
92-
// to handle the case that the reference is aquired again
92+
// to handle the case that the reference is acquired again
9393
this.modelsToDispose.add(key);
9494

9595
(async () => {
9696
try {
9797
const model = await modelPromise;
9898

9999
if (!this.modelsToDispose.has(key)) {
100-
// return if model has been aquired again meanwhile
100+
// return if model has been acquired again meanwhile
101101
return;
102102
}
103103

@@ -108,7 +108,7 @@ class ResourceModelCollection extends ReferenceCollection<Promise<ITextEditorMod
108108
}
109109

110110
if (!this.modelsToDispose.has(key)) {
111-
// return if model has been aquired again meanwhile
111+
// return if model has been acquired again meanwhile
112112
return;
113113
}
114114

0 commit comments

Comments
 (0)