-
Notifications
You must be signed in to change notification settings - Fork 30.9k
/
Copy pathnativeHostMainService.ts
983 lines (786 loc) · 37 KB
/
nativeHostMainService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as fs from 'fs';
import { exec } from 'child_process';
import { app, BrowserWindow, clipboard, Display, Menu, MessageBoxOptions, MessageBoxReturnValue, OpenDevToolsOptions, OpenDialogOptions, OpenDialogReturnValue, powerMonitor, SaveDialogOptions, SaveDialogReturnValue, screen, shell, webContents } from 'electron';
import { arch, cpus, freemem, loadavg, platform, release, totalmem, type } from 'os';
import { promisify } from 'util';
import { memoize } from '../../../base/common/decorators.js';
import { Emitter, Event } from '../../../base/common/event.js';
import { Disposable } from '../../../base/common/lifecycle.js';
import { matchesSomeScheme, Schemas } from '../../../base/common/network.js';
import { dirname, join, posix, resolve, win32 } from '../../../base/common/path.js';
import { isLinux, isMacintosh, isWindows } from '../../../base/common/platform.js';
import { AddFirstParameterToFunctions } from '../../../base/common/types.js';
import { URI } from '../../../base/common/uri.js';
import { realpath } from '../../../base/node/extpath.js';
import { virtualMachineHint } from '../../../base/node/id.js';
import { Promises, SymlinkSupport } from '../../../base/node/pfs.js';
import { findFreePort } from '../../../base/node/ports.js';
import { localize } from '../../../nls.js';
import { ISerializableCommandAction } from '../../action/common/action.js';
import { INativeOpenDialogOptions } from '../../dialogs/common/dialogs.js';
import { IDialogMainService } from '../../dialogs/electron-main/dialogMainService.js';
import { IEnvironmentMainService } from '../../environment/electron-main/environmentMainService.js';
import { createDecorator, IInstantiationService } from '../../instantiation/common/instantiation.js';
import { ILifecycleMainService, IRelaunchOptions } from '../../lifecycle/electron-main/lifecycleMainService.js';
import { ILogService } from '../../log/common/log.js';
import { ICommonNativeHostService, INativeHostOptions, IOSProperties, IOSStatistics } from '../common/native.js';
import { IProductService } from '../../product/common/productService.js';
import { IPartsSplash } from '../../theme/common/themeService.js';
import { IThemeMainService } from '../../theme/electron-main/themeMainService.js';
import { defaultWindowState, ICodeWindow } from '../../window/electron-main/window.js';
import { IColorScheme, IOpenedAuxiliaryWindow, IOpenedMainWindow, IOpenEmptyWindowOptions, IOpenWindowOptions, IPoint, IRectangle, IWindowOpenable } from '../../window/common/window.js';
import { defaultBrowserWindowOptions, IWindowsMainService, OpenContext } from '../../windows/electron-main/windows.js';
import { isWorkspaceIdentifier, toWorkspaceIdentifier } from '../../workspace/common/workspace.js';
import { IWorkspacesManagementMainService } from '../../workspaces/electron-main/workspacesManagementMainService.js';
import { VSBuffer } from '../../../base/common/buffer.js';
import { hasWSLFeatureInstalled } from '../../remote/node/wsl.js';
import { WindowProfiler } from '../../profiling/electron-main/windowProfiling.js';
import { IV8Profile } from '../../profiling/common/profiling.js';
import { IAuxiliaryWindowsMainService } from '../../auxiliaryWindow/electron-main/auxiliaryWindows.js';
import { IAuxiliaryWindow } from '../../auxiliaryWindow/electron-main/auxiliaryWindow.js';
import { CancellationError } from '../../../base/common/errors.js';
import { IConfigurationService } from '../../configuration/common/configuration.js';
import { IProxyAuthService } from './auth.js';
import { AuthInfo, Credentials, IRequestService } from '../../request/common/request.js';
import { randomPath } from '../../../base/common/extpath.js';
export interface INativeHostMainService extends AddFirstParameterToFunctions<ICommonNativeHostService, Promise<unknown> /* only methods, not events */, number | undefined /* window ID */> { }
export const INativeHostMainService = createDecorator<INativeHostMainService>('nativeHostMainService');
export class NativeHostMainService extends Disposable implements INativeHostMainService {
declare readonly _serviceBrand: undefined;
constructor(
@IWindowsMainService private readonly windowsMainService: IWindowsMainService,
@IAuxiliaryWindowsMainService private readonly auxiliaryWindowsMainService: IAuxiliaryWindowsMainService,
@IDialogMainService private readonly dialogMainService: IDialogMainService,
@ILifecycleMainService private readonly lifecycleMainService: ILifecycleMainService,
@IEnvironmentMainService private readonly environmentMainService: IEnvironmentMainService,
@ILogService private readonly logService: ILogService,
@IProductService private readonly productService: IProductService,
@IThemeMainService private readonly themeMainService: IThemeMainService,
@IWorkspacesManagementMainService private readonly workspacesManagementMainService: IWorkspacesManagementMainService,
@IConfigurationService private readonly configurationService: IConfigurationService,
@IRequestService private readonly requestService: IRequestService,
@IProxyAuthService private readonly proxyAuthService: IProxyAuthService,
@IInstantiationService private readonly instantiationService: IInstantiationService
) {
super();
}
//#region Properties
get windowId(): never { throw new Error('Not implemented in electron-main'); }
//#endregion
//#region Events
readonly onDidOpenMainWindow = Event.map(this.windowsMainService.onDidOpenWindow, window => window.id);
readonly onDidTriggerWindowSystemContextMenu = Event.any(
Event.map(this.windowsMainService.onDidTriggerSystemContextMenu, ({ window, x, y }) => ({ windowId: window.id, x, y })),
Event.map(this.auxiliaryWindowsMainService.onDidTriggerSystemContextMenu, ({ window, x, y }) => ({ windowId: window.id, x, y }))
);
readonly onDidMaximizeWindow = Event.any(
Event.map(this.windowsMainService.onDidMaximizeWindow, window => window.id),
Event.map(this.auxiliaryWindowsMainService.onDidMaximizeWindow, window => window.id)
);
readonly onDidUnmaximizeWindow = Event.any(
Event.map(this.windowsMainService.onDidUnmaximizeWindow, window => window.id),
Event.map(this.auxiliaryWindowsMainService.onDidUnmaximizeWindow, window => window.id)
);
readonly onDidChangeWindowFullScreen = Event.any(
Event.map(this.windowsMainService.onDidChangeFullScreen, e => ({ windowId: e.window.id, fullscreen: e.fullscreen })),
Event.map(this.auxiliaryWindowsMainService.onDidChangeFullScreen, e => ({ windowId: e.window.id, fullscreen: e.fullscreen }))
);
readonly onDidBlurMainWindow = Event.filter(Event.fromNodeEventEmitter(app, 'browser-window-blur', (event, window: BrowserWindow) => window.id), windowId => !!this.windowsMainService.getWindowById(windowId));
readonly onDidFocusMainWindow = Event.any(
Event.map(Event.filter(Event.map(this.windowsMainService.onDidChangeWindowsCount, () => this.windowsMainService.getLastActiveWindow()), window => !!window), window => window!.id),
Event.filter(Event.fromNodeEventEmitter(app, 'browser-window-focus', (event, window: BrowserWindow) => window.id), windowId => !!this.windowsMainService.getWindowById(windowId))
);
readonly onDidBlurMainOrAuxiliaryWindow = Event.any(
this.onDidBlurMainWindow,
Event.map(Event.filter(Event.fromNodeEventEmitter(app, 'browser-window-blur', (event, window: BrowserWindow) => this.auxiliaryWindowsMainService.getWindowByWebContents(window.webContents)), window => !!window), window => window!.id)
);
readonly onDidFocusMainOrAuxiliaryWindow = Event.any(
this.onDidFocusMainWindow,
Event.map(Event.filter(Event.fromNodeEventEmitter(app, 'browser-window-focus', (event, window: BrowserWindow) => this.auxiliaryWindowsMainService.getWindowByWebContents(window.webContents)), window => !!window), window => window!.id)
);
readonly onDidResumeOS = Event.fromNodeEventEmitter(powerMonitor, 'resume');
readonly onDidChangeColorScheme = this.themeMainService.onDidChangeColorScheme;
private readonly _onDidChangePassword = this._register(new Emitter<{ account: string; service: string }>());
readonly onDidChangePassword = this._onDidChangePassword.event;
readonly onDidChangeDisplay = Event.debounce(Event.any(
Event.filter(Event.fromNodeEventEmitter(screen, 'display-metrics-changed', (event: Electron.Event, display: Display, changedMetrics?: string[]) => changedMetrics), changedMetrics => {
// Electron will emit 'display-metrics-changed' events even when actually
// going fullscreen, because the dock hides. However, we do not want to
// react on this event as there is no change in display bounds.
return !(Array.isArray(changedMetrics) && changedMetrics.length === 1 && changedMetrics[0] === 'workArea');
}),
Event.fromNodeEventEmitter(screen, 'display-added'),
Event.fromNodeEventEmitter(screen, 'display-removed')
), () => { }, 100);
//#endregion
//#region Window
getWindows(windowId: number | undefined, options: { includeAuxiliaryWindows: true }): Promise<Array<IOpenedMainWindow | IOpenedAuxiliaryWindow>>;
getWindows(windowId: number | undefined, options: { includeAuxiliaryWindows: false }): Promise<Array<IOpenedMainWindow>>;
async getWindows(windowId: number | undefined, options: { includeAuxiliaryWindows: boolean }): Promise<Array<IOpenedMainWindow | IOpenedAuxiliaryWindow>> {
const mainWindows = this.windowsMainService.getWindows().map(window => ({
id: window.id,
workspace: window.openedWorkspace ?? toWorkspaceIdentifier(window.backupPath, window.isExtensionDevelopmentHost),
title: window.win?.getTitle() ?? '',
filename: window.getRepresentedFilename(),
dirty: window.isDocumentEdited()
}));
const auxiliaryWindows = [];
if (options.includeAuxiliaryWindows) {
auxiliaryWindows.push(...this.auxiliaryWindowsMainService.getWindows().map(window => ({
id: window.id,
parentId: window.parentId,
title: window.win?.getTitle() ?? '',
filename: window.getRepresentedFilename()
})));
}
return [...mainWindows, ...auxiliaryWindows];
}
async getWindowCount(windowId: number | undefined): Promise<number> {
return this.windowsMainService.getWindowCount();
}
async getActiveWindowId(windowId: number | undefined): Promise<number | undefined> {
const activeWindow = this.windowsMainService.getFocusedWindow() || this.windowsMainService.getLastActiveWindow();
if (activeWindow) {
return activeWindow.id;
}
return undefined;
}
async getActiveWindowPosition(): Promise<IRectangle | undefined> {
const activeWindow = this.windowsMainService.getFocusedWindow() || this.windowsMainService.getLastActiveWindow();
if (activeWindow) {
return activeWindow.getBounds();
}
return undefined;
}
async getNativeWindowHandle(fallbackWindowId: number | undefined, windowId: number): Promise<VSBuffer | undefined> {
const window = this.windowById(windowId, fallbackWindowId);
if (window?.win) {
return VSBuffer.wrap(window.win.getNativeWindowHandle());
}
return undefined;
}
openWindow(windowId: number | undefined, options?: IOpenEmptyWindowOptions): Promise<void>;
openWindow(windowId: number | undefined, toOpen: IWindowOpenable[], options?: IOpenWindowOptions): Promise<void>;
openWindow(windowId: number | undefined, arg1?: IOpenEmptyWindowOptions | IWindowOpenable[], arg2?: IOpenWindowOptions): Promise<void> {
if (Array.isArray(arg1)) {
return this.doOpenWindow(windowId, arg1, arg2);
}
return this.doOpenEmptyWindow(windowId, arg1);
}
private async doOpenWindow(windowId: number | undefined, toOpen: IWindowOpenable[], options: IOpenWindowOptions = Object.create(null)): Promise<void> {
if (toOpen.length > 0) {
await this.windowsMainService.open({
context: OpenContext.API,
contextWindowId: windowId,
urisToOpen: toOpen,
cli: this.environmentMainService.args,
forceNewWindow: options.forceNewWindow,
forceReuseWindow: options.forceReuseWindow,
preferNewWindow: options.preferNewWindow,
diffMode: options.diffMode,
mergeMode: options.mergeMode,
addMode: options.addMode,
removeMode: options.removeMode,
gotoLineMode: options.gotoLineMode,
noRecentEntry: options.noRecentEntry,
waitMarkerFileURI: options.waitMarkerFileURI,
remoteAuthority: options.remoteAuthority || undefined,
forceProfile: options.forceProfile,
forceTempProfile: options.forceTempProfile,
});
}
}
private async doOpenEmptyWindow(windowId: number | undefined, options?: IOpenEmptyWindowOptions): Promise<void> {
await this.windowsMainService.openEmptyWindow({
context: OpenContext.API,
contextWindowId: windowId
}, options);
}
async isFullScreen(windowId: number | undefined, options?: INativeHostOptions): Promise<boolean> {
const window = this.windowById(options?.targetWindowId, windowId);
return window?.isFullScreen ?? false;
}
async toggleFullScreen(windowId: number | undefined, options?: INativeHostOptions): Promise<void> {
const window = this.windowById(options?.targetWindowId, windowId);
window?.toggleFullScreen();
}
async getCursorScreenPoint(windowId: number | undefined): Promise<{ readonly point: IPoint; readonly display: IRectangle }> {
const point = screen.getCursorScreenPoint();
const display = screen.getDisplayNearestPoint(point);
return { point, display: display.bounds };
}
async isMaximized(windowId: number | undefined, options?: INativeHostOptions): Promise<boolean> {
const window = this.windowById(options?.targetWindowId, windowId);
return window?.win?.isMaximized() ?? false;
}
async moveWindowTop(windowId: number | undefined, options?: INativeHostOptions): Promise<void> {
const window = this.windowById(options?.targetWindowId, windowId);
window?.win?.moveTop();
}
async positionWindow(windowId: number | undefined, position: IRectangle, options?: INativeHostOptions): Promise<void> {
const window = this.windowById(options?.targetWindowId, windowId);
if (window?.win) {
if (window.win.isFullScreen()) {
const fullscreenLeftFuture = Event.toPromise(Event.once(Event.fromNodeEventEmitter(window.win, 'leave-full-screen')));
window.win.setFullScreen(false);
await fullscreenLeftFuture;
}
window.win.setBounds(position);
}
}
async updateWindowControls(windowId: number | undefined, options: INativeHostOptions & { height?: number; backgroundColor?: string; foregroundColor?: string }): Promise<void> {
const window = this.windowById(options?.targetWindowId, windowId);
window?.updateWindowControls(options);
}
async focusWindow(windowId: number | undefined, options?: INativeHostOptions & { force?: boolean }): Promise<void> {
const window = this.windowById(options?.targetWindowId, windowId);
window?.focus({ force: options?.force ?? false });
}
async setMinimumSize(windowId: number | undefined, width: number | undefined, height: number | undefined): Promise<void> {
const window = this.codeWindowById(windowId);
if (window?.win) {
const [windowWidth, windowHeight] = window.win.getSize();
const [minWindowWidth, minWindowHeight] = window.win.getMinimumSize();
const [newMinWindowWidth, newMinWindowHeight] = [width ?? minWindowWidth, height ?? minWindowHeight];
const [newWindowWidth, newWindowHeight] = [Math.max(windowWidth, newMinWindowWidth), Math.max(windowHeight, newMinWindowHeight)];
if (minWindowWidth !== newMinWindowWidth || minWindowHeight !== newMinWindowHeight) {
window.win.setMinimumSize(newMinWindowWidth, newMinWindowHeight);
}
if (windowWidth !== newWindowWidth || windowHeight !== newWindowHeight) {
window.win.setSize(newWindowWidth, newWindowHeight);
}
}
}
async saveWindowSplash(windowId: number | undefined, splash: IPartsSplash): Promise<void> {
const window = this.codeWindowById(windowId);
this.themeMainService.saveWindowSplash(windowId, window?.openedWorkspace, splash);
}
//#endregion
//#region macOS Shell Command
async installShellCommand(windowId: number | undefined): Promise<void> {
const { source, target } = await this.getShellCommandLink();
// Only install unless already existing
try {
const { symbolicLink } = await SymlinkSupport.stat(source);
if (symbolicLink && !symbolicLink.dangling) {
const linkTargetRealPath = await realpath(source);
if (target === linkTargetRealPath) {
return;
}
}
// Different source, delete it first
await fs.promises.unlink(source);
} catch (error) {
if (error.code !== 'ENOENT') {
throw error; // throw on any error but file not found
}
}
try {
await fs.promises.symlink(target, source);
} catch (error) {
if (error.code !== 'EACCES' && error.code !== 'ENOENT') {
throw error;
}
const { response } = await this.showMessageBox(windowId, {
type: 'info',
message: localize('warnEscalation', "{0} will now prompt with 'osascript' for Administrator privileges to install the shell command.", this.productService.nameShort),
buttons: [
localize({ key: 'ok', comment: ['&& denotes a mnemonic'] }, "&&OK"),
localize('cancel', "Cancel")
]
});
if (response === 1 /* Cancel */) {
throw new CancellationError();
}
try {
const command = `osascript -e "do shell script \\"mkdir -p /usr/local/bin && ln -sf \'${target}\' \'${source}\'\\" with administrator privileges"`;
await promisify(exec)(command);
} catch (error) {
throw new Error(localize('cantCreateBinFolder', "Unable to install the shell command '{0}'.", source));
}
}
}
async uninstallShellCommand(windowId: number | undefined): Promise<void> {
const { source } = await this.getShellCommandLink();
try {
await fs.promises.unlink(source);
} catch (error) {
switch (error.code) {
case 'EACCES': {
const { response } = await this.showMessageBox(windowId, {
type: 'info',
message: localize('warnEscalationUninstall', "{0} will now prompt with 'osascript' for Administrator privileges to uninstall the shell command.", this.productService.nameShort),
buttons: [
localize({ key: 'ok', comment: ['&& denotes a mnemonic'] }, "&&OK"),
localize('cancel', "Cancel")
]
});
if (response === 1 /* Cancel */) {
throw new CancellationError();
}
try {
const command = `osascript -e "do shell script \\"rm \'${source}\'\\" with administrator privileges"`;
await promisify(exec)(command);
} catch (error) {
throw new Error(localize('cantUninstall', "Unable to uninstall the shell command '{0}'.", source));
}
break;
}
case 'ENOENT':
break; // ignore file not found
default:
throw error;
}
}
}
private async getShellCommandLink(): Promise<{ readonly source: string; readonly target: string }> {
const target = resolve(this.environmentMainService.appRoot, 'bin', 'code');
const source = `/usr/local/bin/${this.productService.applicationName}`;
// Ensure source exists
const sourceExists = await Promises.exists(target);
if (!sourceExists) {
throw new Error(localize('sourceMissing', "Unable to find shell script in '{0}'", target));
}
return { source, target };
}
//#endregion
//#region Dialog
async showMessageBox(windowId: number | undefined, options: MessageBoxOptions & INativeHostOptions): Promise<MessageBoxReturnValue> {
const window = this.windowById(options?.targetWindowId, windowId);
return this.dialogMainService.showMessageBox(options, window?.win ?? undefined);
}
async showSaveDialog(windowId: number | undefined, options: SaveDialogOptions & INativeHostOptions): Promise<SaveDialogReturnValue> {
const window = this.windowById(options?.targetWindowId, windowId);
return this.dialogMainService.showSaveDialog(options, window?.win ?? undefined);
}
async showOpenDialog(windowId: number | undefined, options: OpenDialogOptions & INativeHostOptions): Promise<OpenDialogReturnValue> {
const window = this.windowById(options?.targetWindowId, windowId);
return this.dialogMainService.showOpenDialog(options, window?.win ?? undefined);
}
async pickFileFolderAndOpen(windowId: number | undefined, options: INativeOpenDialogOptions): Promise<void> {
const paths = await this.dialogMainService.pickFileFolder(options);
if (paths) {
await this.doOpenPicked(await Promise.all(paths.map(async path => (await SymlinkSupport.existsDirectory(path)) ? { folderUri: URI.file(path) } : { fileUri: URI.file(path) })), options, windowId);
}
}
async pickFolderAndOpen(windowId: number | undefined, options: INativeOpenDialogOptions): Promise<void> {
const paths = await this.dialogMainService.pickFolder(options);
if (paths) {
await this.doOpenPicked(paths.map(path => ({ folderUri: URI.file(path) })), options, windowId);
}
}
async pickFileAndOpen(windowId: number | undefined, options: INativeOpenDialogOptions): Promise<void> {
const paths = await this.dialogMainService.pickFile(options);
if (paths) {
await this.doOpenPicked(paths.map(path => ({ fileUri: URI.file(path) })), options, windowId);
}
}
async pickWorkspaceAndOpen(windowId: number | undefined, options: INativeOpenDialogOptions): Promise<void> {
const paths = await this.dialogMainService.pickWorkspace(options);
if (paths) {
await this.doOpenPicked(paths.map(path => ({ workspaceUri: URI.file(path) })), options, windowId);
}
}
private async doOpenPicked(openable: IWindowOpenable[], options: INativeOpenDialogOptions, windowId: number | undefined): Promise<void> {
await this.windowsMainService.open({
context: OpenContext.DIALOG,
contextWindowId: windowId,
cli: this.environmentMainService.args,
urisToOpen: openable,
forceNewWindow: options.forceNewWindow,
/* remoteAuthority will be determined based on openable */
});
}
//#endregion
//#region OS
async showItemInFolder(windowId: number | undefined, path: string): Promise<void> {
shell.showItemInFolder(path);
}
async setRepresentedFilename(windowId: number | undefined, path: string, options?: INativeHostOptions): Promise<void> {
const window = this.windowById(options?.targetWindowId, windowId);
window?.setRepresentedFilename(path);
}
async setDocumentEdited(windowId: number | undefined, edited: boolean, options?: INativeHostOptions): Promise<void> {
const window = this.windowById(options?.targetWindowId, windowId);
window?.setDocumentEdited(edited);
}
async openExternal(windowId: number | undefined, url: string, defaultApplication?: string): Promise<boolean> {
this.environmentMainService.unsetSnapExportedVariables();
try {
if (matchesSomeScheme(url, Schemas.http, Schemas.https)) {
this.openExternalBrowser(url, defaultApplication);
} else {
shell.openExternal(url);
}
} finally {
this.environmentMainService.restoreSnapExportedVariables();
}
return true;
}
private async openExternalBrowser(url: string, defaultApplication?: string): Promise<void> {
const configuredBrowser = defaultApplication ?? this.configurationService.getValue<string>('workbench.externalBrowser');
if (!configuredBrowser) {
return shell.openExternal(url);
}
if (configuredBrowser.includes(posix.sep) || configuredBrowser.includes(win32.sep)) {
const browserPathExists = await Promises.exists(configuredBrowser);
if (!browserPathExists) {
this.logService.error(`Configured external browser path does not exist: ${configuredBrowser}`);
return shell.openExternal(url);
}
}
try {
const { default: open } = await import('open');
const res = await open(url, {
app: {
// Use `open.apps` helper to allow cross-platform browser
// aliases to be looked up properly. Fallback to the
// configured value if not found.
name: Object.hasOwn(open.apps, configuredBrowser) ? open.apps[(configuredBrowser as keyof typeof open['apps'])] : configuredBrowser
}
});
if (!isWindows) {
// On Linux/macOS, listen to stderr and treat that as failure
// for opening the browser to fallback to the default.
// On Windows, unfortunately PowerShell seems to always write
// to stderr so we cannot use it there
// (see also https://github.com/microsoft/vscode/issues/230636)
res.stderr?.once('data', (data: Buffer) => {
this.logService.error(`Error openening external URL '${url}' using browser '${configuredBrowser}': ${data.toString()}`);
return shell.openExternal(url);
});
}
} catch (error) {
this.logService.error(`Unable to open external URL '${url}' using browser '${configuredBrowser}' due to ${error}.`);
return shell.openExternal(url);
}
}
moveItemToTrash(windowId: number | undefined, fullPath: string): Promise<void> {
return shell.trashItem(fullPath);
}
async isAdmin(): Promise<boolean> {
let isAdmin: boolean;
if (isWindows) {
isAdmin = (await import('native-is-elevated')).default();
} else {
isAdmin = process.getuid?.() === 0;
}
return isAdmin;
}
async writeElevated(windowId: number | undefined, source: URI, target: URI, options?: { unlock?: boolean }): Promise<void> {
const sudoPrompt = await import('@vscode/sudo-prompt');
const argsFile = randomPath(this.environmentMainService.userDataPath, 'code-elevated');
await Promises.writeFile(argsFile, JSON.stringify({ source: source.fsPath, target: target.fsPath }));
try {
await new Promise<void>((resolve, reject) => {
const sudoCommand: string[] = [`"${this.cliPath}"`];
if (options?.unlock) {
sudoCommand.push('--file-chmod');
}
sudoCommand.push('--file-write', `"${argsFile}"`);
const promptOptions = {
name: this.productService.nameLong.replace('-', ''),
icns: (isMacintosh && this.environmentMainService.isBuilt) ? join(dirname(this.environmentMainService.appRoot), `${this.productService.nameShort}.icns`) : undefined
};
this.logService.trace(`[sudo-prompt] running command: ${sudoCommand.join(' ')}`);
sudoPrompt.exec(sudoCommand.join(' '), promptOptions, (error?, stdout?, stderr?) => {
if (stdout) {
this.logService.trace(`[sudo-prompt] received stdout: ${stdout}`);
}
if (stderr) {
this.logService.error(`[sudo-prompt] received stderr: ${stderr}`);
}
if (error) {
reject(error);
} else {
resolve(undefined);
}
});
});
} finally {
await fs.promises.unlink(argsFile);
}
}
async isRunningUnderARM64Translation(): Promise<boolean> {
if (isLinux || isWindows) {
return false;
}
return app.runningUnderARM64Translation;
}
@memoize
private get cliPath(): string {
// Windows
if (isWindows) {
if (this.environmentMainService.isBuilt) {
return join(dirname(process.execPath), 'bin', `${this.productService.applicationName}.cmd`);
}
return join(this.environmentMainService.appRoot, 'scripts', 'code-cli.bat');
}
// Linux
if (isLinux) {
if (this.environmentMainService.isBuilt) {
return join(dirname(process.execPath), 'bin', `${this.productService.applicationName}`);
}
return join(this.environmentMainService.appRoot, 'scripts', 'code-cli.sh');
}
// macOS
if (this.environmentMainService.isBuilt) {
return join(this.environmentMainService.appRoot, 'bin', 'code');
}
return join(this.environmentMainService.appRoot, 'scripts', 'code-cli.sh');
}
async getOSStatistics(): Promise<IOSStatistics> {
return {
totalmem: totalmem(),
freemem: freemem(),
loadavg: loadavg()
};
}
async getOSProperties(): Promise<IOSProperties> {
return {
arch: arch(),
platform: platform(),
release: release(),
type: type(),
cpus: cpus()
};
}
async getOSVirtualMachineHint(): Promise<number> {
return virtualMachineHint.value();
}
async getOSColorScheme(): Promise<IColorScheme> {
return this.themeMainService.getColorScheme();
}
// WSL
async hasWSLFeatureInstalled(): Promise<boolean> {
return isWindows && hasWSLFeatureInstalled();
}
//#endregion
//#region Screenshots
async getScreenshot(windowId: number | undefined, options?: INativeHostOptions): Promise<ArrayBufferLike | undefined> {
const window = this.windowById(options?.targetWindowId, windowId);
const captured = await window?.win?.webContents.capturePage();
return captured?.toJPEG(95);
}
//#endregion
//#region Process
async getProcessId(windowId: number | undefined): Promise<number | undefined> {
const window = this.windowById(undefined, windowId);
return window?.win?.webContents.getOSProcessId();
}
async killProcess(windowId: number | undefined, pid: number, code: string): Promise<void> {
process.kill(pid, code);
}
//#endregion
//#region Clipboard
async readClipboardText(windowId: number | undefined, type?: 'selection' | 'clipboard'): Promise<string> {
return clipboard.readText(type);
}
async readImage(): Promise<Uint8Array> {
return clipboard.readImage().toPNG();
}
async writeClipboardText(windowId: number | undefined, text: string, type?: 'selection' | 'clipboard'): Promise<void> {
return clipboard.writeText(text, type);
}
async readClipboardFindText(windowId: number | undefined,): Promise<string> {
return clipboard.readFindText();
}
async writeClipboardFindText(windowId: number | undefined, text: string): Promise<void> {
return clipboard.writeFindText(text);
}
async writeClipboardBuffer(windowId: number | undefined, format: string, buffer: VSBuffer, type?: 'selection' | 'clipboard'): Promise<void> {
return clipboard.writeBuffer(format, Buffer.from(buffer.buffer), type);
}
async readClipboardBuffer(windowId: number | undefined, format: string): Promise<VSBuffer> {
return VSBuffer.wrap(clipboard.readBuffer(format));
}
async hasClipboard(windowId: number | undefined, format: string, type?: 'selection' | 'clipboard'): Promise<boolean> {
return clipboard.has(format, type);
}
//#endregion
//#region macOS Touchbar
async newWindowTab(): Promise<void> {
await this.windowsMainService.open({
context: OpenContext.API,
cli: this.environmentMainService.args,
forceNewTabbedWindow: true,
forceEmpty: true,
remoteAuthority: this.environmentMainService.args.remote || undefined
});
}
async showPreviousWindowTab(): Promise<void> {
Menu.sendActionToFirstResponder('selectPreviousTab:');
}
async showNextWindowTab(): Promise<void> {
Menu.sendActionToFirstResponder('selectNextTab:');
}
async moveWindowTabToNewWindow(): Promise<void> {
Menu.sendActionToFirstResponder('moveTabToNewWindow:');
}
async mergeAllWindowTabs(): Promise<void> {
Menu.sendActionToFirstResponder('mergeAllWindows:');
}
async toggleWindowTabsBar(): Promise<void> {
Menu.sendActionToFirstResponder('toggleTabBar:');
}
async updateTouchBar(windowId: number | undefined, items: ISerializableCommandAction[][]): Promise<void> {
const window = this.codeWindowById(windowId);
window?.updateTouchBar(items);
}
//#endregion
//#region Lifecycle
async notifyReady(windowId: number | undefined): Promise<void> {
const window = this.codeWindowById(windowId);
window?.setReady();
}
async relaunch(windowId: number | undefined, options?: IRelaunchOptions): Promise<void> {
return this.lifecycleMainService.relaunch(options);
}
async reload(windowId: number | undefined, options?: { disableExtensions?: boolean }): Promise<void> {
const window = this.codeWindowById(windowId);
if (window) {
// Special case: support `transient` workspaces by preventing
// the reload and rather go back to an empty window. Transient
// workspaces should never restore, even when the user wants
// to reload.
// For: https://github.com/microsoft/vscode/issues/119695
if (isWorkspaceIdentifier(window.openedWorkspace)) {
const configPath = window.openedWorkspace.configPath;
if (configPath.scheme === Schemas.file) {
const workspace = await this.workspacesManagementMainService.resolveLocalWorkspace(configPath);
if (workspace?.transient) {
return this.openWindow(window.id, { forceReuseWindow: true });
}
}
}
// Proceed normally to reload the window
return this.lifecycleMainService.reload(window, options?.disableExtensions !== undefined ? { _: [], 'disable-extensions': options.disableExtensions } : undefined);
}
}
async closeWindow(windowId: number | undefined, options?: INativeHostOptions): Promise<void> {
const window = this.windowById(options?.targetWindowId, windowId);
return window?.win?.close();
}
async quit(windowId: number | undefined): Promise<void> {
// If the user selected to exit from an extension development host window, do not quit, but just
// close the window unless this is the last window that is opened.
const window = this.windowsMainService.getLastActiveWindow();
if (window?.isExtensionDevelopmentHost && this.windowsMainService.getWindowCount() > 1 && window.win) {
window.win.close();
}
// Otherwise: normal quit
else {
this.lifecycleMainService.quit();
}
}
async exit(windowId: number | undefined, code: number): Promise<void> {
await this.lifecycleMainService.kill(code);
}
//#endregion
//#region Connectivity
async resolveProxy(windowId: number | undefined, url: string): Promise<string | undefined> {
const window = this.codeWindowById(windowId);
const session = window?.win?.webContents?.session;
return session?.resolveProxy(url);
}
async lookupAuthorization(_windowId: number | undefined, authInfo: AuthInfo): Promise<Credentials | undefined> {
return this.proxyAuthService.lookupAuthorization(authInfo);
}
async lookupKerberosAuthorization(_windowId: number | undefined, url: string): Promise<string | undefined> {
return this.requestService.lookupKerberosAuthorization(url);
}
async loadCertificates(_windowId: number | undefined): Promise<string[]> {
return this.requestService.loadCertificates();
}
findFreePort(windowId: number | undefined, startPort: number, giveUpAfter: number, timeout: number, stride = 1): Promise<number> {
return findFreePort(startPort, giveUpAfter, timeout, stride);
}
//#endregion
//#region Development
private gpuInfoWindowId: number | undefined;
async openDevTools(windowId: number | undefined, options?: Partial<OpenDevToolsOptions> & INativeHostOptions): Promise<void> {
const window = this.windowById(options?.targetWindowId, windowId);
window?.win?.webContents.openDevTools(options?.mode ? { mode: options.mode, activate: options.activate } : undefined);
}
async toggleDevTools(windowId: number | undefined, options?: INativeHostOptions): Promise<void> {
const window = this.windowById(options?.targetWindowId, windowId);
window?.win?.webContents.toggleDevTools();
}
async openGPUInfoWindow(windowId: number | undefined): Promise<void> {
const parentWindow = this.codeWindowById(windowId);
if (!parentWindow) {
return;
}
if (typeof this.gpuInfoWindowId !== 'number') {
const options = this.instantiationService.invokeFunction(defaultBrowserWindowOptions, defaultWindowState(), { forceNativeTitlebar: true });
options.backgroundColor = undefined;
const gpuInfoWindow = new BrowserWindow(options);
gpuInfoWindow.setMenuBarVisibility(false);
gpuInfoWindow.loadURL('chrome://gpu');
gpuInfoWindow.once('ready-to-show', () => gpuInfoWindow.show());
gpuInfoWindow.once('close', () => this.gpuInfoWindowId = undefined);
parentWindow.win?.on('close', () => {
if (this.gpuInfoWindowId) {
BrowserWindow.fromId(this.gpuInfoWindowId)?.close();
this.gpuInfoWindowId = undefined;
}
});
this.gpuInfoWindowId = gpuInfoWindow.id;
}
if (typeof this.gpuInfoWindowId === 'number') {
const window = BrowserWindow.fromId(this.gpuInfoWindowId);
if (window?.isMinimized()) {
window?.restore();
}
window?.focus();
}
}
//#endregion
// #region Performance
async profileRenderer(windowId: number | undefined, session: string, duration: number): Promise<IV8Profile> {
const window = this.codeWindowById(windowId);
if (!window || !window.win) {
throw new Error();
}
const profiler = new WindowProfiler(window.win, session, this.logService);
const result = await profiler.inspect(duration);
return result;
}
// #endregion
//#region Registry (windows)
async windowsGetStringRegKey(windowId: number | undefined, hive: 'HKEY_CURRENT_USER' | 'HKEY_LOCAL_MACHINE' | 'HKEY_CLASSES_ROOT' | 'HKEY_USERS' | 'HKEY_CURRENT_CONFIG', path: string, name: string): Promise<string | undefined> {
if (!isWindows) {
return undefined;
}
const Registry = await import('@vscode/windows-registry');
try {
return Registry.GetStringRegKey(hive, path, name);
} catch {
return undefined;
}
}
//#endregion
private windowById(windowId: number | undefined, fallbackCodeWindowId?: number): ICodeWindow | IAuxiliaryWindow | undefined {
return this.codeWindowById(windowId) ?? this.auxiliaryWindowById(windowId) ?? this.codeWindowById(fallbackCodeWindowId);
}
private codeWindowById(windowId: number | undefined): ICodeWindow | undefined {
if (typeof windowId !== 'number') {
return undefined;
}
return this.windowsMainService.getWindowById(windowId);
}
private auxiliaryWindowById(windowId: number | undefined): IAuxiliaryWindow | undefined {
if (typeof windowId !== 'number') {
return undefined;
}
const contents = webContents.fromId(windowId);
if (!contents) {
return undefined;
}
return this.auxiliaryWindowsMainService.getWindowByWebContents(contents);
}
}