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
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { BrowserWindow, BrowserWindowConstructorOptions, Display, IpcMainEvent, screen } from 'electron';
import { validatedIpcMain } from 'vs/base/parts/ipc/electron-main/ipcMain';
import { arch, release, type } from 'os';
import { mnemonicButtonLabel } from 'vs/base/common/labels';
import { DisposableStore } from 'vs/base/common/lifecycle';
import { FileAccess } from 'vs/base/common/network';
import { IProcessEnvironment, isMacintosh } from 'vs/base/common/platform';
import { listProcesses } from 'vs/base/node/ps';
import { localize } from 'vs/nls';
import { IDiagnosticsService, isRemoteDiagnosticError, PerformanceInfo } from 'vs/platform/diagnostics/common/diagnostics';
import { IDiagnosticsMainService } from 'vs/platform/diagnostics/electron-main/diagnosticsMainService';
import { IDialogMainService } from 'vs/platform/dialogs/electron-main/dialogMainService';
import { IEnvironmentMainService } from 'vs/platform/environment/electron-main/environmentMainService';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { ICommonIssueService, IssueReporterData, IssueReporterWindowConfiguration, ProcessExplorerData, ProcessExplorerWindowConfiguration } from 'vs/platform/issue/common/issue';
import { ILaunchMainService } from 'vs/platform/launch/electron-main/launchMainService';
import { ILogService } from 'vs/platform/log/common/log';
import { INativeHostMainService } from 'vs/platform/native/electron-main/nativeHostMainService';
import product from 'vs/platform/product/common/product';
import { IProductService } from 'vs/platform/product/common/productService';
import { IIPCObjectUrl, IProtocolMainService } from 'vs/platform/protocol/electron-main/protocol';
import { zoomLevelToZoomFactor } from 'vs/platform/window/common/window';
import { IWindowState } from 'vs/platform/window/electron-main/window';
export const IIssueMainService = createDecorator<IIssueMainService>('issueMainService');
interface IBrowserWindowOptions {
backgroundColor: string | undefined;
title: string;
zoomLevel: number;
alwaysOnTop: boolean;
}
export interface IIssueMainService extends ICommonIssueService { }
export class IssueMainService implements ICommonIssueService {
declare readonly _serviceBrand: undefined;
private static readonly DEFAULT_BACKGROUND_COLOR = '#1E1E1E';
private issueReporterWindow: BrowserWindow | null = null;
private issueReporterParentWindow: BrowserWindow | null = null;
private processExplorerWindow: BrowserWindow | null = null;
private processExplorerParentWindow: BrowserWindow | null = null;
constructor(
private userEnv: IProcessEnvironment,
@IEnvironmentMainService private readonly environmentMainService: IEnvironmentMainService,
@ILaunchMainService private readonly launchMainService: ILaunchMainService,
@ILogService private readonly logService: ILogService,
@IDiagnosticsService private readonly diagnosticsService: IDiagnosticsService,
@IDiagnosticsMainService private readonly diagnosticsMainService: IDiagnosticsMainService,
@IDialogMainService private readonly dialogMainService: IDialogMainService,
@INativeHostMainService private readonly nativeHostMainService: INativeHostMainService,
@IProtocolMainService private readonly protocolMainService: IProtocolMainService,
@IProductService private readonly productService: IProductService
) {
this.registerListeners();
}
private registerListeners(): void {
validatedIpcMain.on('vscode:issueSystemInfoRequest', async event => {
const [info, remoteData] = await Promise.all([this.launchMainService.getMainProcessInfo(), this.diagnosticsMainService.getRemoteDiagnostics({ includeProcesses: false, includeWorkspaceMetadata: false })]);
const msg = await this.diagnosticsService.getSystemInfo(info, remoteData);
this.safeSend(event, 'vscode:issueSystemInfoResponse', msg);
});
validatedIpcMain.on('vscode:listProcesses', async event => {
const processes = [];
try {
const mainPid = await this.launchMainService.getMainProcessId();
processes.push({ name: localize('local', "Local"), rootProcess: await listProcesses(mainPid) });
const remoteDiagnostics = await this.diagnosticsMainService.getRemoteDiagnostics({ includeProcesses: true });
remoteDiagnostics.forEach(data => {
if (isRemoteDiagnosticError(data)) {
processes.push({
name: data.hostName,
rootProcess: data
});
} else {
if (data.processes) {
processes.push({
name: data.hostName,
rootProcess: data.processes
});
}
}
});
} catch (e) {
this.logService.error(`Listing processes failed: ${e}`);
}
this.safeSend(event, 'vscode:listProcessesResponse', processes);
});
validatedIpcMain.on('vscode:issueReporterClipboard', async event => {
const messageOptions = {
title: this.productService.nameLong,
message: localize('issueReporterWriteToClipboard', "There is too much data to send to GitHub directly. The data will be copied to the clipboard, please paste it into the GitHub issue page that is opened."),
type: 'warning',
buttons: [
mnemonicButtonLabel(localize({ key: 'ok', comment: ['&& denotes a mnemonic'] }, "&&OK")),
mnemonicButtonLabel(localize({ key: 'cancel', comment: ['&& denotes a mnemonic'] }, "&&Cancel")),
],
defaultId: 0,
cancelId: 1,
noLink: true
};
if (this.issueReporterWindow) {
const result = await this.dialogMainService.showMessageBox(messageOptions, this.issueReporterWindow);
this.safeSend(event, 'vscode:issueReporterClipboardResponse', result.response === 0);
}
});
validatedIpcMain.on('vscode:issuePerformanceInfoRequest', async event => {
const performanceInfo = await this.getPerformanceInfo();
this.safeSend(event, 'vscode:issuePerformanceInfoResponse', performanceInfo);
});
validatedIpcMain.on('vscode:issueReporterConfirmClose', async () => {
const messageOptions = {
title: this.productService.nameLong,
message: localize('confirmCloseIssueReporter', "Your input will not be saved. Are you sure you want to close this window?"),
type: 'warning',
buttons: [
mnemonicButtonLabel(localize({ key: 'yes', comment: ['&& denotes a mnemonic'] }, "&&Yes")),
mnemonicButtonLabel(localize({ key: 'cancel', comment: ['&& denotes a mnemonic'] }, "&&Cancel")),
],
defaultId: 0,
cancelId: 1,
noLink: true
};
if (this.issueReporterWindow) {
const result = await this.dialogMainService.showMessageBox(messageOptions, this.issueReporterWindow);
if (result.response === 0) {
if (this.issueReporterWindow) {
this.issueReporterWindow.destroy();
this.issueReporterWindow = null;
}
}
}
});
validatedIpcMain.on('vscode:workbenchCommand', (_: unknown, commandInfo: { id: any; from: any; args: any }) => {
const { id, from, args } = commandInfo;
let parentWindow: BrowserWindow | null;
switch (from) {
case 'issueReporter':
parentWindow = this.issueReporterParentWindow;
break;
case 'processExplorer':
parentWindow = this.processExplorerParentWindow;
break;
default:
throw new Error(`Unexpected command source: ${from}`);
}
if (parentWindow) {
parentWindow.webContents.send('vscode:runAction', { id, from, args });
}
});
validatedIpcMain.on('vscode:openExternal', (_: unknown, arg: string) => {
this.nativeHostMainService.openExternal(undefined, arg);
});
validatedIpcMain.on('vscode:closeIssueReporter', event => {
if (this.issueReporterWindow) {
this.issueReporterWindow.close();
}
});
validatedIpcMain.on('vscode:closeProcessExplorer', event => {
if (this.processExplorerWindow) {
this.processExplorerWindow.close();
}
});
validatedIpcMain.on('vscode:windowsInfoRequest', async event => {
const mainProcessInfo = await this.launchMainService.getMainProcessInfo();
this.safeSend(event, 'vscode:windowsInfoResponse', mainProcessInfo.windows);
});
}
private safeSend(event: IpcMainEvent, channel: string, ...args: unknown[]): void {
if (!event.sender.isDestroyed()) {
event.sender.send(channel, ...args);
}
}
async openReporter(data: IssueReporterData): Promise<void> {
if (!this.issueReporterWindow) {
this.issueReporterParentWindow = BrowserWindow.getFocusedWindow();
if (this.issueReporterParentWindow) {
const issueReporterDisposables = new DisposableStore();
const issueReporterWindowConfigUrl = issueReporterDisposables.add(this.protocolMainService.createIPCObjectUrl<IssueReporterWindowConfiguration>());
const position = this.getWindowPosition(this.issueReporterParentWindow, 700, 800);
this.issueReporterWindow = this.createBrowserWindow(position, issueReporterWindowConfigUrl, {
backgroundColor: data.styles.backgroundColor,
title: localize('issueReporter', "Issue Reporter"),
zoomLevel: data.zoomLevel,
alwaysOnTop: false
}, 'issue-reporter');
// Store into config object URL
issueReporterWindowConfigUrl.update({
appRoot: this.environmentMainService.appRoot,
windowId: this.issueReporterWindow.id,
userEnv: this.userEnv,
data,
disableExtensions: !!this.environmentMainService.disableExtensions,
os: {
type: type(),
arch: arch(),
release: release(),
},
product
});
this.issueReporterWindow.loadURL(
FileAccess.asBrowserUri('vs/code/electron-sandbox/issue/issueReporter.html', require).toString(true)
);
this.issueReporterWindow.on('close', () => {
this.issueReporterWindow = null;
issueReporterDisposables.dispose();
});
this.issueReporterParentWindow.on('closed', () => {
if (this.issueReporterWindow) {
this.issueReporterWindow.close();
this.issueReporterWindow = null;
issueReporterDisposables.dispose();
}
});
}
}
if (this.issueReporterWindow) {
this.focusWindow(this.issueReporterWindow);
}
}
async openProcessExplorer(data: ProcessExplorerData): Promise<void> {
if (!this.processExplorerWindow) {
this.processExplorerParentWindow = BrowserWindow.getFocusedWindow();
if (this.processExplorerParentWindow) {
const processExplorerDisposables = new DisposableStore();
const processExplorerWindowConfigUrl = processExplorerDisposables.add(this.protocolMainService.createIPCObjectUrl<ProcessExplorerWindowConfiguration>());
const position = this.getWindowPosition(this.processExplorerParentWindow, 800, 500);
this.processExplorerWindow = this.createBrowserWindow(position, processExplorerWindowConfigUrl, {
backgroundColor: data.styles.backgroundColor,
title: localize('processExplorer', "Process Explorer"),
zoomLevel: data.zoomLevel,
alwaysOnTop: true
}, 'process-explorer');
// Store into config object URL
processExplorerWindowConfigUrl.update({
appRoot: this.environmentMainService.appRoot,
windowId: this.processExplorerWindow.id,
userEnv: this.userEnv,
data,
product
});
this.processExplorerWindow.loadURL(
FileAccess.asBrowserUri('vs/code/electron-sandbox/processExplorer/processExplorer.html', require).toString(true)
);
this.processExplorerWindow.on('close', () => {
this.processExplorerWindow = null;
processExplorerDisposables.dispose();
});
this.processExplorerParentWindow.on('close', () => {
if (this.processExplorerWindow) {
this.processExplorerWindow.close();
this.processExplorerWindow = null;
processExplorerDisposables.dispose();
}
});
}
}
if (this.processExplorerWindow) {
this.focusWindow(this.processExplorerWindow);
}
}
private focusWindow(window: BrowserWindow): void {
if (window.isMinimized()) {
window.restore();
}
window.focus();
}
private createBrowserWindow<T>(position: IWindowState, ipcObjectUrl: IIPCObjectUrl<T>, options: IBrowserWindowOptions, windowKind: string): BrowserWindow {
const window = new BrowserWindow({
fullscreen: false,
skipTaskbar: true,
resizable: true,
width: position.width,
height: position.height,
minWidth: 300,
minHeight: 200,
x: position.x,
y: position.y,
title: options.title,
backgroundColor: options.backgroundColor || IssueMainService.DEFAULT_BACKGROUND_COLOR,
webPreferences: {
preload: FileAccess.asFileUri('vs/base/parts/sandbox/electron-browser/preload.js', require).fsPath,
additionalArguments: [`--vscode-window-config=${ipcObjectUrl.resource.toString()}`, `--vscode-window-kind=${windowKind}`],
v8CacheOptions: this.environmentMainService.useCodeCache ? 'bypassHeatCheck' : 'none',
enableWebSQL: false,
spellcheck: false,
nativeWindowOpen: true,
zoomFactor: zoomLevelToZoomFactor(options.zoomLevel),
sandbox: true,
contextIsolation: true
},
alwaysOnTop: options.alwaysOnTop,
experimentalDarkMode: true
} as BrowserWindowConstructorOptions & { experimentalDarkMode: boolean });
window.setMenuBarVisibility(false);
return window;
}
async getSystemStatus(): Promise<string> {
const [info, remoteData] = await Promise.all([this.launchMainService.getMainProcessInfo(), this.diagnosticsMainService.getRemoteDiagnostics({ includeProcesses: false, includeWorkspaceMetadata: false })]);
return this.diagnosticsService.getDiagnostics(info, remoteData);
}
private getWindowPosition(parentWindow: BrowserWindow, defaultWidth: number, defaultHeight: number): IWindowState {
// We want the new window to open on the same display that the parent is in
let displayToUse: Display | undefined;
const displays = screen.getAllDisplays();
// Single Display
if (displays.length === 1) {
displayToUse = displays[0];
}
// Multi Display
else {
// on mac there is 1 menu per window so we need to use the monitor where the cursor currently is
if (isMacintosh) {
const cursorPoint = screen.getCursorScreenPoint();
displayToUse = screen.getDisplayNearestPoint(cursorPoint);
}
// if we have a last active window, use that display for the new window
if (!displayToUse && parentWindow) {
displayToUse = screen.getDisplayMatching(parentWindow.getBounds());
}
// fallback to primary display or first display
if (!displayToUse) {
displayToUse = screen.getPrimaryDisplay() || displays[0];
}
}
const state: IWindowState = {
width: defaultWidth,
height: defaultHeight
};
const displayBounds = displayToUse.bounds;
state.x = displayBounds.x + (displayBounds.width / 2) - (state.width! / 2);
state.y = displayBounds.y + (displayBounds.height / 2) - (state.height! / 2);
if (displayBounds.width > 0 && displayBounds.height > 0 /* Linux X11 sessions sometimes report wrong display bounds */) {
if (state.x < displayBounds.x) {
state.x = displayBounds.x; // prevent window from falling out of the screen to the left
}
if (state.y < displayBounds.y) {
state.y = displayBounds.y; // prevent window from falling out of the screen to the top
}
if (state.x > (displayBounds.x + displayBounds.width)) {
state.x = displayBounds.x; // prevent window from falling out of the screen to the right
}
if (state.y > (displayBounds.y + displayBounds.height)) {
state.y = displayBounds.y; // prevent window from falling out of the screen to the bottom
}
if (state.width! > displayBounds.width) {
state.width = displayBounds.width; // prevent window from exceeding display bounds width
}
if (state.height! > displayBounds.height) {
state.height = displayBounds.height; // prevent window from exceeding display bounds height
}
}
return state;
}
private async getPerformanceInfo(): Promise<PerformanceInfo> {
try {
const [info, remoteData] = await Promise.all([this.launchMainService.getMainProcessInfo(), this.diagnosticsMainService.getRemoteDiagnostics({ includeProcesses: true, includeWorkspaceMetadata: true })]);
return await this.diagnosticsService.getPerformanceInfo(info, remoteData);
} catch (error) {
this.logService.warn('issueService#getPerformanceInfo ', error.message);
throw error;
}
}
}
|