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

'use strict';

8
import { app, ipcMain as ipc } from 'electron';
9
import * as platform from 'vs/base/common/platform';
10
import { WindowsManager } from 'vs/code/electron-main/windows';
J
Joao Moreno 已提交
11
import { IWindowsService, OpenContext, ActiveWindowManager } from 'vs/platform/windows/common/windows';
12 13
import { WindowsChannel } from 'vs/platform/windows/common/windowsIpc';
import { WindowsService } from 'vs/platform/windows/electron-main/windowsService';
14
import { ILifecycleService } from 'vs/platform/lifecycle/electron-main/lifecycleMain';
B
Benjamin Pasero 已提交
15
import { CodeMenu } from 'vs/code/electron-main/menus';
B
Benjamin Pasero 已提交
16
import { getShellEnvironment } from 'vs/code/node/shellEnv';
17 18 19 20 21 22 23 24 25 26
import { IUpdateService } from 'vs/platform/update/common/update';
import { UpdateChannel } from 'vs/platform/update/common/updateIpc';
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';
import { LaunchService, LaunchChannel, ILaunchService } from './launch';
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';
27
import { ILogService } from 'vs/platform/log/common/log';
B
Benjamin Pasero 已提交
28
import { IStateService } from 'vs/platform/state/common/state';
29 30 31
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 已提交
32
import { URLHandlerChannelClient, URLServiceChannel } from 'vs/platform/url/common/urlIpc';
33 34 35 36
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { NullTelemetryService } from 'vs/platform/telemetry/common/telemetryUtils';
import { ITelemetryAppenderChannel, TelemetryAppenderClient } from 'vs/platform/telemetry/common/telemetryIpc';
import { TelemetryService, ITelemetryServiceConfig } from 'vs/platform/telemetry/common/telemetryService';
37
import { resolveCommonProperties } from 'vs/platform/telemetry/node/commonProperties';
38 39 40
import { getDelayedChannel } from 'vs/base/parts/ipc/common/ipc';
import product from 'vs/platform/node/product';
import pkg from 'vs/platform/node/package';
J
Joao Moreno 已提交
41
import { ProxyAuthHandler } from './auth';
B
Benjamin Pasero 已提交
42 43
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { ConfigurationService } from 'vs/platform/configuration/node/configurationService';
44 45
import { TPromise } from 'vs/base/common/winjs.base';
import { IWindowsMainService } from 'vs/platform/windows/electron-main/windows';
B
Benjamin Pasero 已提交
46
import { IHistoryMainService } from 'vs/platform/history/common/history';
B
Benjamin Pasero 已提交
47
import { isUndefinedOrNull } from 'vs/base/common/types';
48 49
import { CodeWindow } from 'vs/code/electron-main/window';
import { KeyboardLayoutMonitor } from 'vs/code/electron-main/keyboard';
M
Matt Bierner 已提交
50
import URI from 'vs/base/common/uri';
B
Benjamin Pasero 已提交
51 52
import { WorkspacesChannel } from 'vs/platform/workspaces/common/workspacesIpc';
import { IWorkspacesMainService } from 'vs/platform/workspaces/common/workspaces';
53
import { getMachineId } from 'vs/base/node/id';
54 55 56
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';
57 58
import { IIssueService } from 'vs/platform/issue/common/issue';
import { IssueChannel } from 'vs/platform/issue/common/issueIpc';
P
Pine Wu 已提交
59
import { IssueService } from 'vs/platform/issue/electron-main/issueService';
60
import { LogLevelSetterChannel } from 'vs/platform/log/common/logIpc';
B
Benjamin Pasero 已提交
61
import { setUnexpectedErrorHandler } from 'vs/base/common/errors';
J
Joao Moreno 已提交
62
import { ElectronURLListener } from 'vs/platform/url/electron-main/electronUrlListener';
J
Joao Moreno 已提交
63
import { serve as serveDriver } from 'vs/platform/driver/electron-main/driver';
64

B
Benjamin Pasero 已提交
65
export class CodeApplication {
B
Benjamin Pasero 已提交
66

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

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
	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,
B
Benjamin Pasero 已提交
84
		@IConfigurationService configurationService: ConfigurationService,
B
Benjamin Pasero 已提交
85
		@IStateService private stateService: IStateService,
B
Benjamin Pasero 已提交
86
		@IHistoryMainService private historyMainService: IHistoryMainService
87 88 89 90 91 92 93 94 95
	) {
		this.toDispose = [mainIpcServer, configurationService];

		this.registerListeners();
	}

	private registerListeners(): void {

		// We handle uncaught exceptions here to prevent electron from opening a dialog to the user
B
Benjamin Pasero 已提交
96 97
		setUnexpectedErrorHandler(err => this.onUnexpectedError(err));
		process.on('uncaughtException', err => this.onUnexpectedError(err));
98

99
		app.on('will-quit', () => {
J
Joao Moreno 已提交
100
			this.logService.trace('App#will-quit: disposing resources');
101 102 103 104

			this.dispose();
		});

105 106 107 108 109 110 111
		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 已提交
112
			this.logService.trace('App#activate');
113 114 115 116 117 118 119

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

120 121 122 123 124 125 126 127 128 129
		const 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;
			}
			const srcUri: any = URI.parse(source.toLowerCase()).toString();
			return srcUri.startsWith(URI.file(this.environmentService.appRoot.toLowerCase()).toString());
		};
M
Matt Bierner 已提交
130

131
		app.on('web-contents-created', (event: any, contents) => {
132
			contents.on('will-attach-webview', (event: Electron.Event, webPreferences, params) => {
M
Matt Bierner 已提交
133 134 135 136 137 138 139
				delete webPreferences.preload;
				webPreferences.nodeIntegration = false;

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

M
Matt Bierner 已提交
141
				// Otherwise prevent loading
B
Benjamin Pasero 已提交
142
				this.logService.error('webContents#web-contents-created: Prevented webview attach');
M
Matt Bierner 已提交
143 144 145 146
				event.preventDefault();
			});

			contents.on('will-navigate', event => {
B
Benjamin Pasero 已提交
147
				this.logService.error('webContents#will-navigate: Prevented webcontent navigation');
M
Matt Bierner 已提交
148 149 150 151
				event.preventDefault();
			});
		});

152 153 154
		let macOpenFiles: string[] = [];
		let runningTimeout: number = null;
		app.on('open-file', (event: Event, path: string) => {
J
Joao Moreno 已提交
155
			this.logService.trace('App#open-file: ', path);
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
			event.preventDefault();

			// Keep in array because more might come!
			macOpenFiles.push(path);

			// 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,
						pathsToOpen: macOpenFiles,
						preferNewWindow: true /* dropping on the dock or opening from finder prefers to open in a new window */
					});
					macOpenFiles = [];
					runningTimeout = null;
				}
			}, 100);
		});

182 183 184 185
		app.on('new-window-for-tab', () => {
			this.windowsMainService.openNewWindow(OpenContext.DESKTOP); //macOS native tab "+" button
		});

186
		ipc.on('vscode:exit', (event: any, code: number) => {
J
Joao Moreno 已提交
187
			this.logService.trace('IPC#vscode:exit', code);
188 189 190 191 192

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

193 194
		ipc.on('vscode:fetchShellEnv', event => {
			const webContents = event.sender.webContents;
195
			getShellEnvironment().then(shellEnv => {
J
Johannes Rieken 已提交
196 197 198
				if (!webContents.isDestroyed()) {
					webContents.send('vscode:acceptShellEnv', shellEnv);
				}
199
			}, err => {
J
Johannes Rieken 已提交
200 201 202
				if (!webContents.isDestroyed()) {
					webContents.send('vscode:acceptShellEnv', {});
				}
B
Benjamin Pasero 已提交
203 204

				this.logService.error('Error fetching shell env', err);
205 206
			});
		});
207

208
		ipc.on('vscode:broadcast', (event: any, windowId: number, broadcast: { channel: string; payload: any; }) => {
209
			if (this.windowsMainService && broadcast.channel && !isUndefinedOrNull(broadcast.payload)) {
J
Joao Moreno 已提交
210
				this.logService.trace('IPC#vscode:broadcast', broadcast.channel, broadcast.payload);
211 212 213 214

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

215 216
				// Send to all windows (except sender window)
				this.windowsMainService.sendToAll('vscode:broadcast', broadcast, [windowId]);
217 218 219 220
			}
		});

		// Keyboard layout changes
221
		KeyboardLayoutMonitor.INSTANCE.onDidChangeKeyboardLayout(() => {
222
			if (this.windowsMainService) {
223
				this.windowsMainService.sendToAll('vscode:keyboardLayoutChanged', false);
224 225 226 227
			}
		});
	}

B
Benjamin Pasero 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
	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);
		}
	}

249 250 251 252 253 254
	private onBroadcast(event: string, payload: any): void {

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

B
Benjamin Pasero 已提交
255 256
			this.stateService.setItem(CodeWindow.themeStorageKey, data.id);
			this.stateService.setItem(CodeWindow.themeBackgroundStorageKey, data.background);
257
		}
258 259
	}

260
	public startup(): TPromise<void> {
J
Joao Moreno 已提交
261 262 263
		this.logService.debug('Starting VS Code');
		this.logService.debug(`from: ${this.environmentService.appRoot}`);
		this.logService.debug('args:', this.environmentService.args);
264

265 266 267 268 269 270 271
		// 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);
		}
272

273 274
		// Create Electron IPC Server
		this.electronIpcServer = new ElectronIPCServer();
275

276 277 278 279
		// Resolve unique machine ID
		this.logService.trace('Resolving machine identifier...');
		return this.resolveMachineId().then(machineId => {
			this.logService.trace(`Resolved machine identifier: ${machineId}`);
280

281 282 283
			// Spawn shared process
			this.sharedProcess = new SharedProcess(this.environmentService, this.lifecycleService, this.logService, machineId, this.userEnv);
			this.sharedProcessClient = this.sharedProcess.whenReady().then(() => connect(this.environmentService.sharedIPCHandle, 'main'));
284

285 286
			// Services
			const appInstantiationService = this.initServices(machineId);
287

288
			let promise: TPromise<any> = TPromise.as(null);
289

290 291 292 293 294 295 296
			// Create driver
			if (this.environmentService.driverHandle) {
				serveDriver(this.electronIpcServer, this.environmentService.driverHandle, appInstantiationService).then(server => {
					this.logService.info('Driver started at:', this.environmentService.driverHandle);
					this.toDispose.push(server);
				});
			}
J
Joao Moreno 已提交
297

298 299 300 301
			return promise.then(() => {
				// Setup Auth Handler
				const authHandler = appInstantiationService.createInstance(ProxyAuthHandler);
				this.toDispose.push(authHandler);
302

303 304
				// Open Windows
				appInstantiationService.invokeFunction(accessor => this.openFirstWindow(accessor));
305

306 307
				// Post Open Windows Tasks
				appInstantiationService.invokeFunction(accessor => this.afterWindowOpen(accessor));
308
			});
309
		});
310 311
	}

312
	private resolveMachineId(): TPromise<string> {
B
Benjamin Pasero 已提交
313
		const machineId = this.stateService.getItem<string>(CodeApplication.MACHINE_ID_KEY);
314 315 316 317 318 319 320
		if (machineId) {
			return TPromise.wrap(machineId);
		}

		return getMachineId().then(machineId => {

			// Remember in global storage
B
Benjamin Pasero 已提交
321
			this.stateService.setItem(CodeApplication.MACHINE_ID_KEY, machineId);
322 323 324 325 326 327

			return machineId;
		});
	}

	private initServices(machineId: string): IInstantiationService {
328 329
		const services = new ServiceCollection();

330 331 332 333 334 335 336 337
		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));
		}

338
		services.set(IWindowsMainService, new SyncDescriptor(WindowsManager, machineId));
339 340
		services.set(IWindowsService, new SyncDescriptor(WindowsService, this.sharedProcess));
		services.set(ILaunchService, new SyncDescriptor(LaunchService));
341
		services.set(IIssueService, new SyncDescriptor(IssueService, machineId, this.userEnv));
342 343

		// Telemtry
344
		if (this.environmentService.isBuilt && !this.environmentService.isExtensionDevelopment && !this.environmentService.args['disable-telemetry'] && !!product.enableTelemetry) {
345 346
			const channel = getDelayedChannel<ITelemetryAppenderChannel>(this.sharedProcessClient.then(c => c.getChannel('telemetryAppender')));
			const appender = new TelemetryAppenderClient(channel);
347
			const commonProperties = resolveCommonProperties(product.commit, pkg.version, machineId, this.environmentService.installSourcePath);
348 349
			const piiPaths = [this.environmentService.appRoot, this.environmentService.extensionsPath];
			const config: ITelemetryServiceConfig = { appender, commonProperties, piiPaths };
350

351 352 353 354 355 356 357 358
			services.set(ITelemetryService, new SyncDescriptor(TelemetryService, config));
		} else {
			services.set(ITelemetryService, NullTelemetryService);
		}

		return this.instantiationService.createChild(services);
	}

359
	private openFirstWindow(accessor: ServicesAccessor): void {
360 361 362 363 364 365 366 367 368 369 370 371
		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);

372 373 374 375
		const issueService = accessor.get(IIssueService);
		const issueChannel = new IssueChannel(issueService);
		this.electronIpcServer.registerChannel('issue', issueChannel);

B
Benjamin Pasero 已提交
376 377 378 379
		const workspacesService = accessor.get(IWorkspacesMainService);
		const workspacesChannel = appInstantiationService.createInstance(WorkspacesChannel, workspacesService);
		this.electronIpcServer.registerChannel('workspaces', workspacesChannel);

380 381 382 383 384
		const windowsService = accessor.get(IWindowsService);
		const windowsChannel = new WindowsChannel(windowsService);
		this.electronIpcServer.registerChannel('windows', windowsChannel);
		this.sharedProcessClient.done(client => client.registerChannel('windows', windowsChannel));

J
Joao Moreno 已提交
385 386 387 388
		const urlService = accessor.get(IURLService);
		const urlChannel = new URLServiceChannel(urlService);
		this.electronIpcServer.registerChannel('url', urlChannel);

S
Sandeep Somavarapu 已提交
389
		// Log level management
390
		const logLevelChannel = new LogLevelSetterChannel(accessor.get(ILogService));
S
Sandeep Somavarapu 已提交
391 392 393
		this.electronIpcServer.registerChannel('loglevel', logLevelChannel);
		this.sharedProcessClient.done(client => client.registerChannel('loglevel', logLevelChannel));

394 395 396 397
		// Lifecycle
		this.lifecycleService.ready();

		// Propagate to clients
398
		this.windowsMainService = accessor.get(IWindowsMainService); // TODO@Joao: unfold this
J
Joao Moreno 已提交
399 400 401

		const args = this.environmentService.args;

J
Joao Moreno 已提交
402
		// Create a URL handler which forwards to the last active window
J
Joao Moreno 已提交
403 404 405
		const activeWindowManager = new ActiveWindowManager(windowsService);
		const urlHandlerChannel = this.electronIpcServer.getChannel('urlHandler', { route: () => activeWindowManager.activeClientId });
		const multiplexURLHandler = new URLHandlerChannelClient(urlHandlerChannel);
J
Joao Moreno 已提交
406 407

		// Register the multiple URL handker
J
Joao Moreno 已提交
408 409
		urlService.registerHandler(multiplexURLHandler);

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

415 416 417
		this.windowsMainService.ready(this.userEnv);

		// Open our first window
418
		const macOpenFiles = (<any>global).macOpenFiles as string[];
419 420 421
		const context = !!process.env['VSCODE_CLI'] ? OpenContext.CLI : OpenContext.DESKTOP;
		if (args['new-window'] && args._.length === 0) {
			this.windowsMainService.open({ context, cli: args, forceNewWindow: true, forceEmpty: true, initialStartup: true }); // new window if "-n" was used without paths
422 423
		} else if (macOpenFiles && macOpenFiles.length && (!args._ || !args._.length)) {
			this.windowsMainService.open({ context: OpenContext.DOCK, cli: args, pathsToOpen: macOpenFiles, initialStartup: true }); // mac: open-file event received on startup
424 425 426
		} else {
			this.windowsMainService.open({ context, cli: args, forceNewWindow: args['new-window'] || (!args._.length && args['unity-launch']), diffMode: args.diff, initialStartup: true }); // default: read paths from cli
		}
427 428 429 430
	}

	private afterWindowOpen(accessor: ServicesAccessor): void {
		const appInstantiationService = accessor.get(IInstantiationService);
431
		const windowsMainService = accessor.get(IWindowsMainService);
432 433 434

		let windowsMutex: Mutex = null;
		if (platform.isWindows) {
435 436

			// Setup Windows mutex
437 438 439 440 441
			try {
				const Mutex = (require.__$__nodeRequire('windows-mutex') as any).Mutex;
				windowsMutex = new Mutex(product.win32MutexName);
				this.toDispose.push({ dispose: () => windowsMutex.release() });
			} catch (e) {
442
				if (!this.environmentService.isBuilt) {
443
					windowsMainService.showMessageBox({
444 445 446 447 448
						title: product.nameLong,
						type: 'warning',
						message: 'Failed to load windows-mutex!',
						detail: e.toString(),
						noLink: true
449 450
					});
				}
451
			}
452

453
			// Ensure Windows foreground love module
454
			try {
455
				// tslint:disable-next-line:no-unused-expression
456 457 458
				<any>require.__$__nodeRequire('windows-foreground-love');
			} catch (e) {
				if (!this.environmentService.isBuilt) {
459
					windowsMainService.showMessageBox({
460 461 462 463 464
						title: product.nameLong,
						type: 'warning',
						message: 'Failed to load windows-foreground-love!',
						detail: e.toString(),
						noLink: true
465 466 467
					});
				}
			}
468
		}
469 470

		// Install Menu
B
Benjamin Pasero 已提交
471
		appInstantiationService.createInstance(CodeMenu);
472 473

		// Jump List
B
Benjamin Pasero 已提交
474 475
		this.historyMainService.updateWindowsJumpList();
		this.historyMainService.onRecentlyOpenedChange(() => this.historyMainService.updateWindowsJumpList());
476 477 478 479 480 481 482 483

		// Start shared process here
		this.sharedProcess.spawn();
	}

	private dispose(): void {
		this.toDispose = dispose(this.toDispose);
	}
J
Johannes Rieken 已提交
484
}