app.ts 24.9 KB
Newer Older
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

B
Benjamin Pasero 已提交
6
import { app, ipcMain as ipc, systemPreferences, shell, Event, contentTracing } from 'electron';
7
import * as platform from 'vs/base/common/platform';
8
import { WindowsManager } from 'vs/code/electron-main/windows';
J
Joao Moreno 已提交
9
import { IWindowsService, OpenContext, ActiveWindowManager } from 'vs/platform/windows/common/windows';
J
Joao Moreno 已提交
10
import { WindowsChannel } from 'vs/platform/windows/node/windowsIpc';
11
import { WindowsService } from 'vs/platform/windows/electron-main/windowsService';
12
import { ILifecycleService } from 'vs/platform/lifecycle/electron-main/lifecycleMain';
B
Benjamin Pasero 已提交
13
import { getShellEnvironment } from 'vs/code/node/shellEnv';
14
import { IUpdateService } from 'vs/platform/update/common/update';
J
Joao Moreno 已提交
15
import { UpdateChannel } from 'vs/platform/update/node/updateIpc';
16 17 18 19
import { Server as ElectronIPCServer } from 'vs/base/parts/ipc/electron-main/ipc.electron-main';
import { Server, connect, Client } from 'vs/base/parts/ipc/node/ipc.net';
import { SharedProcess } from 'vs/code/electron-main/sharedProcess';
import { Mutex } from 'windows-mutex';
B
Benjamin Pasero 已提交
20
import { LaunchService, LaunchChannel, ILaunchService } from 'vs/platform/launch/electron-main/launchService';
21 22 23
import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors';
24
import { ILogService } from 'vs/platform/log/common/log';
B
Benjamin Pasero 已提交
25
import { IStateService } from 'vs/platform/state/common/state';
26 27 28
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IURLService } from 'vs/platform/url/common/url';
J
Joao Moreno 已提交
29
import { URLHandlerChannelClient, URLServiceChannel } from 'vs/platform/url/node/urlIpc';
30
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
31
import { NullTelemetryService, combinedAppender, LogAppender } from 'vs/platform/telemetry/common/telemetryUtils';
J
Joao Moreno 已提交
32
import { ITelemetryAppenderChannel, TelemetryAppenderClient } from 'vs/platform/telemetry/node/telemetryIpc';
33
import { TelemetryService, ITelemetryServiceConfig } from 'vs/platform/telemetry/common/telemetryService';
34
import { resolveCommonProperties } from 'vs/platform/telemetry/node/commonProperties';
J
Joao Moreno 已提交
35
import { getDelayedChannel } from 'vs/base/parts/ipc/node/ipc';
36 37
import product from 'vs/platform/node/product';
import pkg from 'vs/platform/node/package';
38
import { ProxyAuthHandler } from 'vs/code/electron-main/auth';
B
Benjamin Pasero 已提交
39 40
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { ConfigurationService } from 'vs/platform/configuration/node/configurationService';
41
import { TPromise } from 'vs/base/common/winjs.base';
B
Benjamin Pasero 已提交
42
import { IWindowsMainService, ICodeWindow } from 'vs/platform/windows/electron-main/windows';
B
Benjamin Pasero 已提交
43
import { IHistoryMainService } from 'vs/platform/history/common/history';
B
Benjamin Pasero 已提交
44
import { isUndefinedOrNull } from 'vs/base/common/types';
45
import { KeyboardLayoutMonitor } from 'vs/code/electron-main/keyboard';
46
import { URI } from 'vs/base/common/uri';
J
Joao Moreno 已提交
47
import { WorkspacesChannel } from 'vs/platform/workspaces/node/workspacesIpc';
B
Benjamin Pasero 已提交
48
import { IWorkspacesMainService } from 'vs/platform/workspaces/common/workspaces';
49
import { getMachineId } from 'vs/base/node/id';
50 51 52
import { Win32UpdateService } from 'vs/platform/update/electron-main/updateService.win32';
import { LinuxUpdateService } from 'vs/platform/update/electron-main/updateService.linux';
import { DarwinUpdateService } from 'vs/platform/update/electron-main/updateService.darwin';
53
import { IIssueService } from 'vs/platform/issue/common/issue';
J
Joao Moreno 已提交
54
import { IssueChannel } from 'vs/platform/issue/node/issueIpc';
P
Pine Wu 已提交
55
import { IssueService } from 'vs/platform/issue/electron-main/issueService';
J
Joao Moreno 已提交
56
import { LogLevelSetterChannel } from 'vs/platform/log/node/logIpc';
A
Alex Dima 已提交
57
import * as errors from 'vs/base/common/errors';
J
Joao Moreno 已提交
58
import { ElectronURLListener } from 'vs/platform/url/electron-main/electronUrlListener';
J
Joao Moreno 已提交
59
import { serve as serveDriver } from 'vs/platform/driver/electron-main/driver';
60 61
import { IMenubarService } from 'vs/platform/menubar/common/menubar';
import { MenubarService } from 'vs/platform/menubar/electron-main/menubarService';
J
Joao Moreno 已提交
62
import { MenubarChannel } from 'vs/platform/menubar/node/menubarIpc';
63
import { ILabelService, RegisterFormatterEvent } from 'vs/platform/label/common/label';
M
Martin Aeschlimann 已提交
64
import { hasArgs } from 'vs/platform/environment/node/argv';
65
import { RunOnceScheduler } from 'vs/base/common/async';
66
import { registerContextMenuListener } from 'vs/base/parts/contextmenu/electron-main/contextmenu';
67
import { THEME_STORAGE_KEY, THEME_BG_STORAGE_KEY } from 'vs/code/electron-main/theme';
B
Benjamin Pasero 已提交
68 69 70
import { nativeSep, join } from 'vs/base/common/paths';
import { homedir } from 'os';
import { localize } from 'vs/nls';
71

B
Benjamin Pasero 已提交
72
export class CodeApplication {
B
Benjamin Pasero 已提交
73

74
	private static readonly MACHINE_ID_KEY = 'telemetry.machineId';
B
Benjamin Pasero 已提交
75

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
	private toDispose: IDisposable[];
	private windowsMainService: IWindowsMainService;

	private electronIpcServer: ElectronIPCServer;

	private sharedProcess: SharedProcess;
	private sharedProcessClient: TPromise<Client>;

	constructor(
		private mainIpcServer: Server,
		private userEnv: platform.IProcessEnvironment,
		@IInstantiationService private instantiationService: IInstantiationService,
		@ILogService private logService: ILogService,
		@IEnvironmentService private environmentService: IEnvironmentService,
		@ILifecycleService private lifecycleService: ILifecycleService,
91
		@IConfigurationService private configurationService: ConfigurationService,
B
Benjamin Pasero 已提交
92
		@IStateService private stateService: IStateService,
93
		@IHistoryMainService private historyMainService: IHistoryMainService,
I
isidor 已提交
94
		@ILabelService private labelService: ILabelService
95 96 97 98 99 100 101 102 103
	) {
		this.toDispose = [mainIpcServer, configurationService];

		this.registerListeners();
	}

	private registerListeners(): void {

		// We handle uncaught exceptions here to prevent electron from opening a dialog to the user
A
Alex Dima 已提交
104
		errors.setUnexpectedErrorHandler(err => this.onUnexpectedError(err));
B
Benjamin Pasero 已提交
105
		process.on('uncaughtException', err => this.onUnexpectedError(err));
106
		process.on('unhandledRejection', (reason: any, promise: Promise<any>) => errors.onUnexpectedError(reason));
107

108 109 110
		// Contextmenu via IPC support
		registerContextMenuListener();

111
		app.on('will-quit', () => {
J
Joao Moreno 已提交
112
			this.logService.trace('App#will-quit: disposing resources');
113 114 115 116

			this.dispose();
		});

117 118 119 120 121 122 123
		app.on('accessibility-support-changed', (event: Event, accessibilitySupportEnabled: boolean) => {
			if (this.windowsMainService) {
				this.windowsMainService.sendToAll('vscode:accessibilitySupportChanged', accessibilitySupportEnabled);
			}
		});

		app.on('activate', (event: Event, hasVisibleWindows: boolean) => {
J
Joao Moreno 已提交
124
			this.logService.trace('App#activate');
125 126 127 128 129 130 131

			// Mac only event: open new window when we get activated
			if (!hasVisibleWindows && this.windowsMainService) {
				this.windowsMainService.openNewWindow(OpenContext.DOCK);
			}
		});

132 133
		// Security related measures (https://electronjs.org/docs/tutorial/security)
		// DO NOT CHANGE without consulting the documentation
134
		app.on('web-contents-created', (event: any, contents) => {
135
			contents.on('will-attach-webview', (event: Electron.Event, webPreferences, params) => {
136 137

				// Ensure defaults
M
Matt Bierner 已提交
138 139 140 141
				delete webPreferences.preload;
				webPreferences.nodeIntegration = false;

				// Verify URLs being loaded
142
				if (this.isValidWebviewSource(params.src) && this.isValidWebviewSource(webPreferences.preloadURL)) {
M
Matt Bierner 已提交
143 144
					return;
				}
M
Matt Bierner 已提交
145

146 147
				delete webPreferences.preloadUrl;

M
Matt Bierner 已提交
148
				// Otherwise prevent loading
B
Benjamin Pasero 已提交
149
				this.logService.error('webContents#web-contents-created: Prevented webview attach');
150

M
Matt Bierner 已提交
151 152 153 154
				event.preventDefault();
			});

			contents.on('will-navigate', event => {
B
Benjamin Pasero 已提交
155
				this.logService.error('webContents#will-navigate: Prevented webcontent navigation');
156

M
Matt Bierner 已提交
157 158
				event.preventDefault();
			});
159 160 161 162 163 164

			contents.on('new-window', (event: Event, url: string) => {
				event.preventDefault(); // prevent code that wants to open links

				shell.openExternal(url);
			});
M
Matt Bierner 已提交
165 166
		});

167
		let macOpenFileURIs: URI[] = [];
168
		let runningTimeout: any = null;
169
		app.on('open-file', (event: Event, path: string) => {
J
Joao Moreno 已提交
170
			this.logService.trace('App#open-file: ', path);
171 172 173
			event.preventDefault();

			// Keep in array because more might come!
174
			macOpenFileURIs.push(URI.file(path));
175 176 177 178 179 180 181 182 183 184 185 186 187

			// Clear previous handler if any
			if (runningTimeout !== null) {
				clearTimeout(runningTimeout);
				runningTimeout = null;
			}

			// Handle paths delayed in case more are coming!
			runningTimeout = setTimeout(() => {
				if (this.windowsMainService) {
					this.windowsMainService.open({
						context: OpenContext.DOCK /* can also be opening from finder while app is running */,
						cli: this.environmentService.args,
S
Sandeep Somavarapu 已提交
188
						urisToOpen: macOpenFileURIs,
189 190
						preferNewWindow: true /* dropping on the dock or opening from finder prefers to open in a new window */
					});
191
					macOpenFileURIs = [];
192 193 194 195 196
					runningTimeout = null;
				}
			}, 100);
		});

197 198 199 200
		app.on('new-window-for-tab', () => {
			this.windowsMainService.openNewWindow(OpenContext.DESKTOP); //macOS native tab "+" button
		});

201
		ipc.on('vscode:exit', (event: Event, code: number) => {
J
Joao Moreno 已提交
202
			this.logService.trace('IPC#vscode:exit', code);
203 204 205 206 207

			this.dispose();
			this.lifecycleService.kill(code);
		});

208 209
		ipc.on('vscode:fetchShellEnv', (event: Event) => {
			const webContents = event.sender;
210
			getShellEnvironment().then(shellEnv => {
J
Johannes Rieken 已提交
211 212 213
				if (!webContents.isDestroyed()) {
					webContents.send('vscode:acceptShellEnv', shellEnv);
				}
214
			}, err => {
J
Johannes Rieken 已提交
215 216 217
				if (!webContents.isDestroyed()) {
					webContents.send('vscode:acceptShellEnv', {});
				}
B
Benjamin Pasero 已提交
218 219

				this.logService.error('Error fetching shell env', err);
220 221
			});
		});
222

223
		ipc.on('vscode:broadcast', (event: Event, windowId: number, broadcast: { channel: string; payload: any; }) => {
224
			if (this.windowsMainService && broadcast.channel && !isUndefinedOrNull(broadcast.payload)) {
J
Joao Moreno 已提交
225
				this.logService.trace('IPC#vscode:broadcast', broadcast.channel, broadcast.payload);
226 227 228 229

				// Handle specific events on main side
				this.onBroadcast(broadcast.channel, broadcast.payload);

230 231
				// Send to all windows (except sender window)
				this.windowsMainService.sendToAll('vscode:broadcast', broadcast, [windowId]);
232 233 234
			}
		});

235
		ipc.on('vscode:labelRegisterFormatter', (event: any, data: RegisterFormatterEvent) => {
236
			this.labelService.registerFormatter(data.selector, data.formatter);
237 238
		});

239 240 241 242 243 244 245 246 247 248 249 250
		ipc.on('vscode:toggleDevTools', (event: Event) => {
			event.sender.toggleDevTools();
		});

		ipc.on('vscode:openDevTools', (event: Event) => {
			event.sender.openDevTools();
		});

		ipc.on('vscode:reloadWindow', (event: Event) => {
			event.sender.reload();
		});

251
		// Keyboard layout changes
252
		KeyboardLayoutMonitor.INSTANCE.onDidChangeKeyboardLayout(() => {
253
			if (this.windowsMainService) {
254
				this.windowsMainService.sendToAll('vscode:keyboardLayoutChanged', false);
255 256 257 258
			}
		});
	}

259 260 261 262 263 264 265 266 267
	private isValidWebviewSource(source: string): boolean {
		if (!source) {
			return false;
		}

		if (source === 'data:text/html;charset=utf-8,%3C%21DOCTYPE%20html%3E%0D%0A%3Chtml%20lang%3D%22en%22%20style%3D%22width%3A%20100%25%3B%20height%3A%20100%25%22%3E%0D%0A%3Chead%3E%0D%0A%09%3Ctitle%3EVirtual%20Document%3C%2Ftitle%3E%0D%0A%3C%2Fhead%3E%0D%0A%3Cbody%20style%3D%22margin%3A%200%3B%20overflow%3A%20hidden%3B%20width%3A%20100%25%3B%20height%3A%20100%25%22%3E%0D%0A%3C%2Fbody%3E%0D%0A%3C%2Fhtml%3E') {
			return true;
		}

268 269
		const srcUri: any = URI.parse(source).fsPath.toLowerCase();
		const rootUri = URI.file(this.environmentService.appRoot).fsPath.toLowerCase();
270
		return srcUri.startsWith(rootUri + nativeSep);
271 272
	}

B
Benjamin Pasero 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
	private onUnexpectedError(err: Error): void {
		if (err) {

			// take only the message and stack property
			const friendlyError = {
				message: err.message,
				stack: err.stack
			};

			// handle on client side
			if (this.windowsMainService) {
				this.windowsMainService.sendToFocused('vscode:reportError', JSON.stringify(friendlyError));
			}
		}

		this.logService.error(`[uncaught exception in main]: ${err}`);
		if (err.stack) {
			this.logService.error(err.stack);
		}
	}

294 295 296 297 298 299
	private onBroadcast(event: string, payload: any): void {

		// Theme changes
		if (event === 'vscode:changeColorTheme' && typeof payload === 'string') {
			let data = JSON.parse(payload);

300 301
			this.stateService.setItem(THEME_STORAGE_KEY, data.baseTheme);
			this.stateService.setItem(THEME_BG_STORAGE_KEY, data.background);
302
		}
303 304
	}

B
Benjamin Pasero 已提交
305
	startup(): TPromise<void> {
J
Joao Moreno 已提交
306 307 308
		this.logService.debug('Starting VS Code');
		this.logService.debug(`from: ${this.environmentService.appRoot}`);
		this.logService.debug('args:', this.environmentService.args);
309

310 311 312 313 314 315 316
		// Make sure we associate the program with the app user model id
		// This will help Windows to associate the running program with
		// any shortcut that is pinned to the taskbar and prevent showing
		// two icons in the taskbar for the same app.
		if (platform.isWindows && product.win32AppUserModelId) {
			app.setAppUserModelId(product.win32AppUserModelId);
		}
317

318 319 320 321 322 323 324 325
		// Fix native tabs on macOS 10.13
		// macOS enables a compatibility patch for any bundle ID beginning with
		// "com.microsoft.", which breaks native tabs for VS Code when using this
		// identifier (from the official build).
		// Explicitly opt out of the patch here before creating any windows.
		// See: https://github.com/Microsoft/vscode/issues/35361#issuecomment-399794085
		try {
			if (platform.isMacintosh && this.configurationService.getValue<boolean>('window.nativeTabs') === true && !systemPreferences.getUserDefault('NSUseImprovedLayoutPass', 'boolean')) {
326
				systemPreferences.setUserDefault('NSUseImprovedLayoutPass', 'boolean', true as any);
327 328 329 330 331
			}
		} catch (error) {
			this.logService.error(error);
		}

332 333
		// Create Electron IPC Server
		this.electronIpcServer = new ElectronIPCServer();
334

335 336 337 338
		// Resolve unique machine ID
		this.logService.trace('Resolving machine identifier...');
		return this.resolveMachineId().then(machineId => {
			this.logService.trace(`Resolved machine identifier: ${machineId}`);
339

340
			// Spawn shared process
341
			this.sharedProcess = this.instantiationService.createInstance(SharedProcess, machineId, this.userEnv);
342
			this.sharedProcessClient = this.sharedProcess.whenReady().then(() => connect(this.environmentService.sharedIPCHandle, 'main'));
343

344 345
			// Services
			const appInstantiationService = this.initServices(machineId);
J
Joao Moreno 已提交
346

347
			let promise: TPromise<any> = TPromise.as(null);
348

349 350
			// Create driver
			if (this.environmentService.driverHandle) {
351
				serveDriver(this.electronIpcServer, this.environmentService.driverHandle, this.environmentService, appInstantiationService).then(server => {
352 353 354 355
					this.logService.info('Driver started at:', this.environmentService.driverHandle);
					this.toDispose.push(server);
				});
			}
356

357
			return promise.then(() => {
358

359 360 361
				// Setup Auth Handler
				const authHandler = appInstantiationService.createInstance(ProxyAuthHandler);
				this.toDispose.push(authHandler);
362

363
				// Open Windows
B
Benjamin Pasero 已提交
364
				const windows = appInstantiationService.invokeFunction(accessor => this.openFirstWindow(accessor));
B
Benjamin Pasero 已提交
365

366 367
				// Post Open Windows Tasks
				appInstantiationService.invokeFunction(accessor => this.afterWindowOpen(accessor));
B
Benjamin Pasero 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380

				// Tracing: Stop tracing after windows are ready if enabled
				if (this.environmentService.args.trace) {
					this.logService.info(`Tracing: waiting for windows to get ready...`);

					let recordingStopped = false;
					const stopRecording = (timeout) => {
						if (recordingStopped) {
							return;
						}

						recordingStopped = true; // only once

381
						contentTracing.stopRecording(join(homedir(), `${product.applicationName}-${Math.random().toString(16).slice(-4)}.trace.txt`), path => {
B
Benjamin Pasero 已提交
382 383 384 385 386 387 388
							if (!timeout) {
								this.windowsMainService.showMessageBox({
									type: 'info',
									message: localize('trace.message', "Successfully created trace."),
									detail: localize('trace.detail', "Please create an issue and manually attach the following file:\n{0}", path),
									buttons: [localize('trace.ok', "Ok")]
								}, this.windowsMainService.getLastActiveWindow());
B
Benjamin Pasero 已提交
389
							} else {
B
Benjamin Pasero 已提交
390
								this.logService.info(`Tracing: data recorded (after 30s timeout) to ${path}`);
B
Benjamin Pasero 已提交
391 392 393 394 395 396 397 398 399 400 401 402 403
							}
						});
					};

					// Wait up to 30s before creating the trace anyways
					const timeoutHandle = setTimeout(() => stopRecording(true), 30000);

					// Wait for all windows to get ready and stop tracing then
					TPromise.join(windows.map(window => window.ready())).then(() => {
						clearTimeout(timeoutHandle);
						stopRecording(false);
					});
				}
404
			});
405
		});
406 407
	}

408
	private resolveMachineId(): TPromise<string> {
B
Benjamin Pasero 已提交
409
		const machineId = this.stateService.getItem<string>(CodeApplication.MACHINE_ID_KEY);
410 411 412 413 414 415 416
		if (machineId) {
			return TPromise.wrap(machineId);
		}

		return getMachineId().then(machineId => {

			// Remember in global storage
B
Benjamin Pasero 已提交
417
			this.stateService.setItem(CodeApplication.MACHINE_ID_KEY, machineId);
418 419 420 421 422 423

			return machineId;
		});
	}

	private initServices(machineId: string): IInstantiationService {
424 425
		const services = new ServiceCollection();

426 427 428 429 430 431 432 433
		if (process.platform === 'win32') {
			services.set(IUpdateService, new SyncDescriptor(Win32UpdateService));
		} else if (process.platform === 'linux') {
			services.set(IUpdateService, new SyncDescriptor(LinuxUpdateService));
		} else if (process.platform === 'darwin') {
			services.set(IUpdateService, new SyncDescriptor(DarwinUpdateService));
		}

434
		services.set(IWindowsMainService, new SyncDescriptor(WindowsManager, machineId));
435 436
		services.set(IWindowsService, new SyncDescriptor(WindowsService, this.sharedProcess));
		services.set(ILaunchService, new SyncDescriptor(LaunchService));
437
		services.set(IIssueService, new SyncDescriptor(IssueService, machineId, this.userEnv));
438
		services.set(IMenubarService, new SyncDescriptor(MenubarService));
439 440

		// Telemtry
441
		if (!this.environmentService.isExtensionDevelopment && !this.environmentService.args['disable-telemetry'] && !!product.enableTelemetry) {
442
			const channel = getDelayedChannel<ITelemetryAppenderChannel>(this.sharedProcessClient.then(c => c.getChannel('telemetryAppender')));
443
			const appender = combinedAppender(new TelemetryAppenderClient(channel), new LogAppender(this.logService));
444
			const commonProperties = resolveCommonProperties(product.commit, pkg.version, machineId, this.environmentService.installSourcePath);
445 446
			const piiPaths = [this.environmentService.appRoot, this.environmentService.extensionsPath];
			const config: ITelemetryServiceConfig = { appender, commonProperties, piiPaths };
447

448 449 450 451 452 453 454 455
			services.set(ITelemetryService, new SyncDescriptor(TelemetryService, config));
		} else {
			services.set(ITelemetryService, NullTelemetryService);
		}

		return this.instantiationService.createChild(services);
	}

B
Benjamin Pasero 已提交
456
	private openFirstWindow(accessor: ServicesAccessor): ICodeWindow[] {
457 458 459 460 461 462 463 464 465 466 467 468
		const appInstantiationService = accessor.get(IInstantiationService);

		// Register more Main IPC services
		const launchService = accessor.get(ILaunchService);
		const launchChannel = new LaunchChannel(launchService);
		this.mainIpcServer.registerChannel('launch', launchChannel);

		// Register more Electron IPC services
		const updateService = accessor.get(IUpdateService);
		const updateChannel = new UpdateChannel(updateService);
		this.electronIpcServer.registerChannel('update', updateChannel);

469 470 471 472
		const issueService = accessor.get(IIssueService);
		const issueChannel = new IssueChannel(issueService);
		this.electronIpcServer.registerChannel('issue', issueChannel);

B
Benjamin Pasero 已提交
473 474 475 476
		const workspacesService = accessor.get(IWorkspacesMainService);
		const workspacesChannel = appInstantiationService.createInstance(WorkspacesChannel, workspacesService);
		this.electronIpcServer.registerChannel('workspaces', workspacesChannel);

477 478 479
		const windowsService = accessor.get(IWindowsService);
		const windowsChannel = new WindowsChannel(windowsService);
		this.electronIpcServer.registerChannel('windows', windowsChannel);
480
		this.sharedProcessClient.then(client => client.registerChannel('windows', windowsChannel));
481

482 483 484 485
		const menubarService = accessor.get(IMenubarService);
		const menubarChannel = new MenubarChannel(menubarService);
		this.electronIpcServer.registerChannel('menubar', menubarChannel);

J
Joao Moreno 已提交
486 487 488 489
		const urlService = accessor.get(IURLService);
		const urlChannel = new URLServiceChannel(urlService);
		this.electronIpcServer.registerChannel('url', urlChannel);

S
Sandeep Somavarapu 已提交
490
		// Log level management
491
		const logLevelChannel = new LogLevelSetterChannel(accessor.get(ILogService));
S
Sandeep Somavarapu 已提交
492
		this.electronIpcServer.registerChannel('loglevel', logLevelChannel);
493
		this.sharedProcessClient.then(client => client.registerChannel('loglevel', logLevelChannel));
S
Sandeep Somavarapu 已提交
494

495 496 497 498
		// Lifecycle
		this.lifecycleService.ready();

		// Propagate to clients
499
		const windowsMainService = this.windowsMainService = accessor.get(IWindowsMainService); // TODO@Joao: unfold this
J
Joao Moreno 已提交
500 501 502

		const args = this.environmentService.args;

J
Joao Moreno 已提交
503
		// Create a URL handler which forwards to the last active window
J
Joao Moreno 已提交
504
		const activeWindowManager = new ActiveWindowManager(windowsService);
J
Joao Moreno 已提交
505
		const route = () => activeWindowManager.getActiveClientId();
J
Joao Moreno 已提交
506
		const urlHandlerChannel = this.electronIpcServer.getChannel('urlHandler', { routeCall: route, routeEvent: route });
J
Joao Moreno 已提交
507
		const multiplexURLHandler = new URLHandlerChannelClient(urlHandlerChannel);
J
Joao Moreno 已提交
508

509 510 511 512 513 514
		// On Mac, Code can be running without any open windows, so we must create a window to handle urls,
		// if there is none
		if (platform.isMacintosh) {
			const environmentService = accessor.get(IEnvironmentService);

			urlService.registerHandler({
J
Joao Moreno 已提交
515
				handleURL(uri: URI): TPromise<boolean> {
516 517 518 519 520 521 522
					if (windowsMainService.getWindowCount() === 0) {
						const cli = { ...environmentService.args, goto: true };
						const [window] = windowsMainService.open({ context: OpenContext.API, cli, forceEmpty: true });

						return window.ready().then(() => urlService.open(uri));
					}

J
Joao Moreno 已提交
523
					return TPromise.as(false);
524 525 526 527
				}
			});
		}

J
Joao Moreno 已提交
528
		// Register the multiple URL handker
J
Joao Moreno 已提交
529 530
		urlService.registerHandler(multiplexURLHandler);

J
Joao Moreno 已提交
531
		// Watch Electron URLs and forward them to the UrlService
J
Joao Moreno 已提交
532 533 534 535
		const urls = args['open-url'] ? args._urls : [];
		const urlListener = new ElectronURLListener(urls, urlService, this.windowsMainService);
		this.toDispose.push(urlListener);

536 537 538
		this.windowsMainService.ready(this.userEnv);

		// Open our first window
539
		const macOpenFiles = (<any>global).macOpenFiles as string[];
540
		const context = !!process.env['VSCODE_CLI'] ? OpenContext.CLI : OpenContext.DESKTOP;
M
Martin Aeschlimann 已提交
541 542 543
		const hasCliArgs = hasArgs(args._);
		const hasFolderURIs = hasArgs(args['folder-uri']);
		const hasFileURIs = hasArgs(args['file-uri']);
544

M
Martin Aeschlimann 已提交
545
		if (args['new-window'] && !hasCliArgs && !hasFolderURIs && !hasFileURIs) {
B
Benjamin Pasero 已提交
546
			return this.windowsMainService.open({ context, cli: args, forceNewWindow: true, forceEmpty: true, initialStartup: true }); // new window if "-n" was used without paths
547
		}
B
Benjamin Pasero 已提交
548 549 550 551 552 553

		if (macOpenFiles && macOpenFiles.length && !hasCliArgs && !hasFolderURIs && !hasFileURIs) {
			return this.windowsMainService.open({ context: OpenContext.DOCK, cli: args, urisToOpen: macOpenFiles.map(file => URI.file(file)), initialStartup: true }); // mac: open-file event received on startup
		}

		return this.windowsMainService.open({ context, cli: args, forceNewWindow: args['new-window'] || (!hasCliArgs && args['unity-launch']), diffMode: args.diff, initialStartup: true }); // default: read paths from cli
554 555 556
	}

	private afterWindowOpen(accessor: ServicesAccessor): void {
557
		const windowsMainService = accessor.get(IWindowsMainService);
558 559 560

		let windowsMutex: Mutex = null;
		if (platform.isWindows) {
561 562

			// Setup Windows mutex
563 564 565 566 567
			try {
				const Mutex = (require.__$__nodeRequire('windows-mutex') as any).Mutex;
				windowsMutex = new Mutex(product.win32MutexName);
				this.toDispose.push({ dispose: () => windowsMutex.release() });
			} catch (e) {
568
				if (!this.environmentService.isBuilt) {
569
					windowsMainService.showMessageBox({
570 571 572 573 574
						title: product.nameLong,
						type: 'warning',
						message: 'Failed to load windows-mutex!',
						detail: e.toString(),
						noLink: true
575 576
					});
				}
577
			}
578

579
			// Ensure Windows foreground love module
580
			try {
581
				// tslint:disable-next-line:no-unused-expression
582 583 584
				<any>require.__$__nodeRequire('windows-foreground-love');
			} catch (e) {
				if (!this.environmentService.isBuilt) {
585
					windowsMainService.showMessageBox({
586 587 588 589 590
						title: product.nameLong,
						type: 'warning',
						message: 'Failed to load windows-foreground-love!',
						detail: e.toString(),
						noLink: true
591 592 593
					});
				}
			}
594
		}
595 596

		// Jump List
B
Benjamin Pasero 已提交
597 598
		this.historyMainService.updateWindowsJumpList();
		this.historyMainService.onRecentlyOpenedChange(() => this.historyMainService.updateWindowsJumpList());
599

600
		// Start shared process after a while
601 602 603
		const sharedProcess = new RunOnceScheduler(() => this.sharedProcess.spawn(), 3000);
		sharedProcess.schedule();
		this.toDispose.push(sharedProcess);
604 605 606 607 608
	}

	private dispose(): void {
		this.toDispose = dispose(this.toDispose);
	}
609
}