Skip to content

Commit 8eef842

Browse files
author
Miguel Solorio
committed
Merge branch 'master' of github.com:Microsoft/vscode
2 parents 261c5b2 + 5684336 commit 8eef842

5 files changed

Lines changed: 101 additions & 21 deletions

File tree

src/vs/code/electron-main/app.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ import { INativeEnvironmentService } from 'vs/platform/environment/node/environm
8080
import { mnemonicButtonLabel, getPathLabel } from 'vs/base/common/labels';
8181
import { IFileService } from 'vs/platform/files/common/files';
8282
import { WebviewProtocolProvider } from 'vs/platform/webview/electron-main/webviewProtocolProvider';
83+
import { WebviewChannel } from 'vs/platform/webview/electron-main/webviewIpcs';
84+
import { WebviewMainService } from 'vs/platform/webview/electron-main/webviewMainService';
85+
import { IWebviewManagerService } from 'vs/platform/webview/common/webviewManagerService';
8386

8487
export class CodeApplication extends Disposable {
8588
private windowsMainService: IWindowsMainService | undefined;
@@ -471,6 +474,7 @@ export class CodeApplication extends Disposable {
471474

472475
services.set(IIssueMainService, new SyncDescriptor(IssueMainService, [machineId, this.userEnv]));
473476
services.set(IElectronMainService, new SyncDescriptor(ElectronMainService));
477+
services.set(IWebviewManagerService, new SyncDescriptor(WebviewMainService));
474478
services.set(IWorkspacesService, new SyncDescriptor(WorkspacesService));
475479
services.set(IMenubarMainService, new SyncDescriptor(MenubarMainService));
476480

@@ -578,6 +582,10 @@ export class CodeApplication extends Disposable {
578582
const urlChannel = createChannelReceiver(urlService);
579583
electronIpcServer.registerChannel('url', urlChannel);
580584

585+
const webviewManagerService = accessor.get(IWebviewManagerService);
586+
const webviewChannel = new WebviewChannel(webviewManagerService);
587+
electronIpcServer.registerChannel('webview', webviewChannel);
588+
581589
const storageMainService = accessor.get(IStorageMainService);
582590
const storageChannel = this._register(new GlobalStorageDatabaseChannel(this.logService, storageMainService));
583591
electronIpcServer.registerChannel('storage', storageChannel);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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 { createDecorator } from 'vs/platform/instantiation/common/instantiation';
7+
8+
export const IWebviewManagerService = createDecorator<IWebviewManagerService>('webviewManagerService');
9+
10+
export interface IWebviewManagerService {
11+
_serviceBrand: unknown;
12+
13+
setIgnoreMenuShortcuts(webContentsId: number, enabled: boolean): void;
14+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 { IServerChannel } from 'vs/base/parts/ipc/common/ipc';
8+
import { IWebviewManagerService } from 'vs/platform/webview/common/webviewManagerService';
9+
10+
export class WebviewChannel implements IServerChannel {
11+
12+
constructor(
13+
@IWebviewManagerService private readonly webviewMainService: IWebviewManagerService,
14+
) { }
15+
16+
listen(_: unknown, event: string): Event<any> {
17+
throw new Error(`Event not found: ${event}`);
18+
}
19+
20+
async call(_: unknown, command: string, arg?: any): Promise<any> {
21+
switch (command) {
22+
case 'setIgnoreMenuShortcuts': this.webviewMainService.setIgnoreMenuShortcuts(arg[0], arg[1]); return;
23+
}
24+
25+
throw new Error(`Call not found: ${command}`);
26+
}
27+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 { webContents } from 'electron';
7+
import { IWebviewManagerService } from 'vs/platform/webview/common/webviewManagerService';
8+
9+
export class WebviewMainService implements IWebviewManagerService {
10+
11+
_serviceBrand: undefined;
12+
13+
public setIgnoreMenuShortcuts(webContentsId: number, enabled: boolean): void {
14+
const contents = webContents.fromId(webContentsId);
15+
if (!contents) {
16+
throw new Error(`Invalid webContentsId: ${webContentsId}`);
17+
}
18+
if (!contents.isDestroyed()) {
19+
contents.setIgnoreMenuShortcuts(enabled);
20+
}
21+
}
22+
}

src/vs/workbench/contrib/webview/electron-browser/webviewElement.ts

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import { FindInPageOptions, OnBeforeRequestListenerDetails, OnHeadersReceivedListenerDetails, Response, WebContents, WebviewTag } from 'electron';
77
import { ipcRenderer } from 'vs/base/parts/sandbox/electron-sandbox/globals';
88
import { addDisposableListener } from 'vs/base/browser/dom';
9+
import { equals } from 'vs/base/common/arrays';
910
import { ThrottledDelayer } from 'vs/base/common/async';
1011
import { Emitter, Event } from 'vs/base/common/event';
1112
import { once } from 'vs/base/common/functional';
@@ -26,8 +27,9 @@ import { WebviewPortMappingManager } from 'vs/workbench/contrib/webview/common/p
2627
import { WebviewThemeDataProvider } from 'vs/workbench/contrib/webview/common/themeing';
2728
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
2829
import { WebviewFindDelegate, WebviewFindWidget } from '../browser/webviewFindWidget';
29-
import { equals } from 'vs/base/common/arrays';
30-
30+
import { IWebviewManagerService } from 'vs/platform/webview/common/webviewManagerService';
31+
import { IMainProcessService } from 'vs/platform/ipc/electron-sandbox/mainProcessService';
32+
import { createChannelSender } from 'vs/base/parts/ipc/common/ipc';
3133

3234
class WebviewTagHandle extends Disposable {
3335

@@ -185,26 +187,30 @@ class WebviewPortMappingProvider extends Disposable {
185187

186188
class WebviewKeyboardHandler {
187189

188-
private readonly _webviews = new Set<WebviewTagHandle>();
190+
private readonly _webviews = new Set<WebviewTag>();
189191
private readonly _isUsingNativeTitleBars: boolean;
190192

191-
constructor(configurationService: IConfigurationService) {
193+
private readonly webviewMainService: IWebviewManagerService;
194+
195+
constructor(
196+
configurationService: IConfigurationService,
197+
mainProcessService: IMainProcessService,
198+
) {
192199
this._isUsingNativeTitleBars = configurationService.getValue<string>('window.titleBarStyle') === 'native';
200+
201+
this.webviewMainService = createChannelSender<IWebviewManagerService>(mainProcessService.getChannel('webview'));
193202
}
194203

195-
public add(
196-
webviewHandle: WebviewTagHandle,
197-
): IDisposable {
198-
this._webviews.add(webviewHandle);
204+
public add(webview: WebviewTag): IDisposable {
205+
this._webviews.add(webview);
199206

200207
const disposables = new DisposableStore();
208+
201209
if (this.shouldToggleMenuShortcutsEnablement) {
202-
disposables.add(webviewHandle.onFirstLoad(() => {
203-
this.setIgnoreMenuShortcutsForWebview(webviewHandle, true);
204-
}));
210+
this.setIgnoreMenuShortcutsForWebview(webview, true);
205211
}
206212

207-
disposables.add(addDisposableListener(webviewHandle.webview, 'ipc-message', (event) => {
213+
disposables.add(addDisposableListener(webview, 'ipc-message', (event) => {
208214
switch (event.channel) {
209215
case 'did-focus':
210216
this.setIgnoreMenuShortcuts(true);
@@ -218,7 +224,7 @@ class WebviewKeyboardHandler {
218224

219225
return toDisposable(() => {
220226
disposables.dispose();
221-
this._webviews.delete(webviewHandle);
227+
this._webviews.delete(webview);
222228
});
223229
}
224230

@@ -232,12 +238,9 @@ class WebviewKeyboardHandler {
232238
}
233239
}
234240

235-
private setIgnoreMenuShortcutsForWebview(webview: WebviewTagHandle, value: boolean) {
241+
private setIgnoreMenuShortcutsForWebview(webview: WebviewTag, value: boolean) {
236242
if (this.shouldToggleMenuShortcutsEnablement) {
237-
const contents = webview.webContents;
238-
if (!contents?.isDestroyed()) {
239-
contents?.setIgnoreMenuShortcuts(value);
240-
}
243+
this.webviewMainService.setIgnoreMenuShortcuts(webview.getWebContentsId(), value);
241244
}
242245
}
243246
}
@@ -246,9 +249,12 @@ export class ElectronWebviewBasedWebview extends BaseWebview<WebviewTag> impleme
246249

247250
private static _webviewKeyboardHandler: WebviewKeyboardHandler | undefined;
248251

249-
private static getWebviewKeyboardHandler(configService: IConfigurationService) {
252+
private static getWebviewKeyboardHandler(
253+
configService: IConfigurationService,
254+
mainProcessService: IMainProcessService,
255+
) {
250256
if (!this._webviewKeyboardHandler) {
251-
this._webviewKeyboardHandler = new WebviewKeyboardHandler(configService);
257+
this._webviewKeyboardHandler = new WebviewKeyboardHandler(configService, mainProcessService);
252258
}
253259
return this._webviewKeyboardHandler;
254260
}
@@ -276,6 +282,7 @@ export class ElectronWebviewBasedWebview extends BaseWebview<WebviewTag> impleme
276282
@IEnvironmentService environementService: IEnvironmentService,
277283
@IWorkbenchEnvironmentService workbenchEnvironmentService: IWorkbenchEnvironmentService,
278284
@IConfigurationService configurationService: IConfigurationService,
285+
@IMainProcessService mainProcessService: IMainProcessService,
279286
) {
280287
super(id, options, contentOptions, extension, _webviewThemeDataProvider, telemetryService, environementService, workbenchEnvironmentService);
281288

@@ -291,7 +298,9 @@ export class ElectronWebviewBasedWebview extends BaseWebview<WebviewTag> impleme
291298
tunnelService,
292299
));
293300

294-
this._register(ElectronWebviewBasedWebview.getWebviewKeyboardHandler(configurationService).add(webviewAndContents));
301+
this._register(addDisposableListener(this.element!, 'did-start-loading', once(() => {
302+
this._register(ElectronWebviewBasedWebview.getWebviewKeyboardHandler(configurationService, mainProcessService).add(this.element!));
303+
})));
295304

296305
this._domReady = new Promise(resolve => {
297306
const subscription = this._register(this.on(WebviewMessageChannels.webviewReady, () => {

0 commit comments

Comments
 (0)