shell.ts 21.5 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

import 'vs/css!./media/shell';

10
import * as nls from 'vs/nls';
11
import {TPromise} from 'vs/base/common/winjs.base';
J
Joao Moreno 已提交
12
import * as platform from 'vs/base/common/platform';
E
Erich Gamma 已提交
13
import {Dimension, Builder, $} from 'vs/base/browser/builder';
J
Johannes Rieken 已提交
14
import {escapeRegExpCharacters} from 'vs/base/common/strings';
E
Erich Gamma 已提交
15
import dom = require('vs/base/browser/dom');
16
import aria = require('vs/base/browser/ui/aria/aria');
J
Joao Moreno 已提交
17
import {dispose, IDisposable} from 'vs/base/common/lifecycle';
E
Erich Gamma 已提交
18 19 20 21 22
import errors = require('vs/base/common/errors');
import {ContextViewService} from 'vs/platform/contextview/browser/contextViewService';
import {ContextMenuService} from 'vs/workbench/services/contextview/electron-browser/contextmenuService';
import timer = require('vs/base/common/timer');
import {Workbench} from 'vs/workbench/browser/workbench';
23
import {Storage, inMemoryLocalStorageInstance} from 'vs/workbench/common/storage';
24
import {ITelemetryService, NullTelemetryService} from 'vs/platform/telemetry/common/telemetry';
E
Erich Gamma 已提交
25 26 27
import {ElectronTelemetryService} from  'vs/platform/telemetry/electron-browser/electronTelemetryService';
import {ElectronIntegration} from 'vs/workbench/electron-browser/integration';
import {Update} from 'vs/workbench/electron-browser/update';
B
Benjamin Pasero 已提交
28
import {WorkspaceStats} from 'vs/platform/telemetry/common/workspaceStats';
E
Erich Gamma 已提交
29 30 31 32 33 34 35
import {IWindowService, WindowService} from 'vs/workbench/services/window/electron-browser/windowService';
import {MessageService} from 'vs/workbench/services/message/electron-browser/messageService';
import {RequestService} from 'vs/workbench/services/request/node/requestService';
import {IConfigurationService} from 'vs/platform/configuration/common/configuration';
import {FileService} from 'vs/workbench/services/files/electron-browser/fileService';
import {SearchService} from 'vs/workbench/services/search/node/searchService';
import {LifecycleService} from 'vs/workbench/services/lifecycle/electron-browser/lifecycleService';
36
import {WorkbenchKeybindingService} from 'vs/workbench/services/keybinding/electron-browser/keybindingService';
E
Erich Gamma 已提交
37
import {MainThreadService} from 'vs/workbench/services/thread/electron-browser/threadService';
38
import {MainProcessMarkerService} from 'vs/platform/markers/common/markerService';
E
Erich Gamma 已提交
39 40 41 42 43 44
import {IActionsService} from 'vs/platform/actions/common/actions';
import ActionsService from 'vs/platform/actions/common/actionsService';
import {IModelService} from 'vs/editor/common/services/modelService';
import {ModelServiceImpl} from 'vs/editor/common/services/modelServiceImpl';
import {CodeEditorServiceImpl} from 'vs/editor/browser/services/codeEditorServiceImpl';
import {ICodeEditorService} from 'vs/editor/common/services/codeEditorService';
45 46
import {EditorWorkerServiceImpl} from 'vs/editor/common/services/editorWorkerServiceImpl';
import {IEditorWorkerService} from 'vs/editor/common/services/editorWorkerService';
J
Johannes Rieken 已提交
47
import {MainProcessVSCodeAPIHelper} from 'vs/workbench/api/node/extHost.api.impl';
48
import {MainProcessExtensionService} from 'vs/platform/extensions/common/nativeExtensionService';
J
Johannes Rieken 已提交
49
import {MainThreadDocuments} from 'vs/workbench/api/node/extHostDocuments';
E
Erich Gamma 已提交
50 51
import {MainProcessTextMateSyntax} from 'vs/editor/node/textMate/TMSyntax';
import {MainProcessTextMateSnippet} from 'vs/editor/node/textMate/TMSnippets';
M
Martin Aeschlimann 已提交
52
import {JSONValidationExtensionPoint} from 'vs/platform/jsonschemas/common/jsonValidationExtensionPoint';
E
Erich Gamma 已提交
53
import {LanguageConfigurationFileHandler} from 'vs/editor/node/languageConfiguration';
J
Johannes Rieken 已提交
54 55 56 57
import {MainThreadFileSystemEventService} from 'vs/workbench/api/node/extHostFileSystemEventService';
import {MainThreadQuickOpen} from 'vs/workbench/api/node/extHostQuickOpen';
import {MainThreadStatusBar} from 'vs/workbench/api/node/extHostStatusBar';
import {MainThreadCommands} from 'vs/workbench/api/node/extHostCommands';
58
import {RemoteTelemetryServiceHelper} from 'vs/platform/telemetry/common/remoteTelemetryService';
J
Johannes Rieken 已提交
59 60 61 62 63 64 65 66
import {MainThreadDiagnostics} from 'vs/workbench/api/node/extHostDiagnostics';
import {MainThreadOutputService} from 'vs/workbench/api/node/extHostOutputService';
import {MainThreadMessageService} from 'vs/workbench/api/node/extHostMessageService';
import {MainThreadLanguages} from 'vs/workbench/api/node/extHostLanguages';
import {MainThreadEditors} from 'vs/workbench/api/node/extHostEditors';
import {MainThreadWorkspace} from 'vs/workbench/api/node/extHostWorkspace';
import {MainThreadConfiguration} from 'vs/workbench/api/node/extHostConfiguration';
import {MainThreadLanguageFeatures} from 'vs/workbench/api/node/extHostLanguageFeatures';
E
Erich Gamma 已提交
67
import {IOptions} from 'vs/workbench/common/options';
M
Martin Aeschlimann 已提交
68
import {IStorageService} from 'vs/platform/storage/common/storage';
E
Erich Gamma 已提交
69
import {MainThreadStorage} from 'vs/platform/storage/common/remotable.storage';
70
import {ServiceCollection} from 'vs/platform/instantiation/common/serviceCollection';
71
import {InstantiationService} from 'vs/platform/instantiation/common/instantiationService';
E
Erich Gamma 已提交
72 73 74
import {IContextViewService, IContextMenuService} from 'vs/platform/contextview/browser/contextView';
import {IEventService} from 'vs/platform/event/common/event';
import {IFileService} from 'vs/platform/files/common/files';
75
import {IKeybindingService, IKeybindingContextKey} from 'vs/platform/keybinding/common/keybindingService';
E
Erich Gamma 已提交
76
import {ILifecycleService} from 'vs/platform/lifecycle/common/lifecycle';
B
Benjamin Pasero 已提交
77
import {IMarkerService} from 'vs/platform/markers/common/markers';
E
Erich Gamma 已提交
78 79 80 81 82
import {IMessageService, Severity} from 'vs/platform/message/common/message';
import {IRequestService} from 'vs/platform/request/common/request';
import {ISearchService} from 'vs/platform/search/common/search';
import {IThreadService} from 'vs/platform/thread/common/thread';
import {IWorkspaceContextService, IConfiguration, IWorkspace} from 'vs/platform/workspace/common/workspace';
A
Alex Dima 已提交
83
import {IExtensionService} from 'vs/platform/extensions/common/extensions';
E
Erich Gamma 已提交
84 85
import {MainThreadModeServiceImpl} from 'vs/editor/common/services/modeServiceImpl';
import {IModeService} from 'vs/editor/common/services/modeService';
86
import {IUntitledEditorService, UntitledEditorService} from 'vs/workbench/services/untitled/common/untitledEditorService';
E
Erich Gamma 已提交
87
import {CrashReporter} from 'vs/workbench/electron-browser/crashReporter';
M
Martin Aeschlimann 已提交
88 89
import {IThemeService} from 'vs/workbench/services/themes/common/themeService';
import {ThemeService} from 'vs/workbench/services/themes/electron-browser/themeService';
J
Joao Moreno 已提交
90
import {getDelayedChannel} from 'vs/base/parts/ipc/common/ipc';
J
Joao Moreno 已提交
91
import {connect} from 'vs/base/parts/ipc/node/ipc.net';
92
import {IExtensionsChannel, ExtensionsChannelClient} from 'vs/workbench/parts/extensions/common/extensionsIpc';
93 94
import {IExtensionsService} from 'vs/workbench/parts/extensions/common/extensions';
import {ReloadWindowAction} from 'vs/workbench/electron-browser/actions';
E
Erich Gamma 已提交
95

96 97 98
// self registering service
import 'vs/platform/opener/electron-browser/opener.contribution';

99 100 101 102 103 104 105 106 107
/**
 * Services that we require for the Shell
 */
export interface ICoreServices {
	contextService: IWorkspaceContextService;
	eventService: IEventService;
	configurationService: IConfigurationService;
}

E
Erich Gamma 已提交
108 109 110 111 112
/**
 * The Monaco Workbench Shell contains the Monaco workbench with a rich header containing navigation and the activity bar.
 * With the Shell being the top level element in the page, it is also responsible for driving the layouting.
 */
export class WorkbenchShell {
B
Benjamin Pasero 已提交
113
	private storageService: IStorageService;
114
	private messageService: MessageService;
115
	private eventService: IEventService;
B
Benjamin Pasero 已提交
116 117 118
	private contextViewService: ContextViewService;
	private windowService: IWindowService;
	private threadService: MainThreadService;
119
	private configurationService: IConfigurationService;
M
Martin Aeschlimann 已提交
120
	private themeService: ThemeService;
121
	private contextService: IWorkspaceContextService;
122
	private telemetryService: ITelemetryService;
123
	private keybindingService: WorkbenchKeybindingService;
E
Erich Gamma 已提交
124 125

	private container: HTMLElement;
126
	private toUnbind: IDisposable[];
E
Erich Gamma 已提交
127 128 129 130 131 132 133 134 135 136 137
	private previousErrorValue: string;
	private previousErrorTime: number;
	private content: HTMLElement;
	private contentsContainer: Builder;
	private currentTheme: string;

	private configuration: IConfiguration;
	private workspace: IWorkspace;
	private options: IOptions;
	private workbench: Workbench;

138 139
	private messagesShowingContextKey: IKeybindingContextKey<boolean>;

140
	constructor(container: HTMLElement, workspace: IWorkspace, services: ICoreServices, configuration: IConfiguration, options: IOptions) {
E
Erich Gamma 已提交
141 142 143 144
		this.container = container;

		this.workspace = workspace;
		this.configuration = configuration;
145 146 147 148 149
		this.options = options;

		this.contextService = services.contextService;
		this.eventService = services.eventService;
		this.configurationService = services.configurationService;
E
Erich Gamma 已提交
150 151 152 153 154 155 156

		this.toUnbind = [];
		this.previousErrorTime = 0;
	}

	private createContents(parent: Builder): Builder {

157
		// ARIA
B
Benjamin Pasero 已提交
158
		aria.setARIAContainer(document.body);
159

E
Erich Gamma 已提交
160 161 162 163
		// Workbench Container
		let workbenchContainer = $(parent).div();

		// Instantiation service with services
164 165
		let serviceCollection = this.initServiceCollection();
		let instantiationService = new InstantiationService(serviceCollection);
E
Erich Gamma 已提交
166 167 168

		//crash reporting
		if (!!this.configuration.env.crashReporter) {
169
			let crashReporter = instantiationService.createInstance(CrashReporter, this.configuration.env.version, this.configuration.env.commitHash);
E
Erich Gamma 已提交
170 171 172 173
			crashReporter.start(this.configuration.env.crashReporter);
		}

		const sharedProcessClientPromise = connect(process.env['VSCODE_SHARED_IPC_HOOK']);
174

175 176 177
		sharedProcessClientPromise.done(service => {
			service.onClose(() => {
				this.messageService.show(Severity.Error, {
178
					message: nls.localize('sharedProcessCrashed', "The shared process terminated unexpectedly. Please reload the window to recover."),
179 180 181 182 183
					actions: [instantiationService.createInstance(ReloadWindowAction, ReloadWindowAction.ID, ReloadWindowAction.LABEL)]
				});
			});
		}, errors.onUnexpectedError);

184 185 186 187 188 189 190
		const extensionsChannelPromise = sharedProcessClientPromise
			.then(client => client.getChannel<IExtensionsChannel>('extensions'));

		const channel = getDelayedChannel<IExtensionsChannel>(extensionsChannelPromise);
		const extensionsService = new ExtensionsChannelClient(channel);

		serviceCollection.set(IExtensionsService, extensionsService);
E
Erich Gamma 已提交
191 192

		// Workbench
193
		this.workbench = instantiationService.createInstance(Workbench, workbenchContainer.getHTMLElement(), this.workspace, this.configuration, this.options, serviceCollection);
E
Erich Gamma 已提交
194 195
		this.workbench.startup({
			onServicesCreated: () => {
A
Alex Dima 已提交
196
				this.initExtensionSystem();
B
Benjamin Pasero 已提交
197 198 199
			},
			onWorkbenchStarted: () => {
				this.onWorkbenchStarted();
E
Erich Gamma 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
			}
		});

		// Electron integration
		this.workbench.getInstantiationService().createInstance(ElectronIntegration).integrate(this.container);

		// Update
		this.workbench.getInstantiationService().createInstance(Update);

		// Handle case where workbench is not starting up properly
		let timeoutHandle = setTimeout(() => {
			console.warn('Workbench did not finish loading in 10 seconds, that might be a problem that should be reported.');
		}, 10000);

		this.workbench.joinCreation().then(() => {
			clearTimeout(timeoutHandle);
		});

		return workbenchContainer;
	}

B
Benjamin Pasero 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
	private onWorkbenchStarted(): void {

		// Log to telemetry service
		let windowSize = {
			innerHeight: window.innerHeight,
			innerWidth: window.innerWidth,
			outerHeight: window.outerHeight,
			outerWidth: window.outerWidth
		};

		this.telemetryService.publicLog('workspaceLoad',
			{
				userAgent: navigator.userAgent,
				windowSize: windowSize,
				emptyWorkbench: !this.contextService.getWorkspace(),
				customKeybindingsCount: this.keybindingService.customKeybindingsCount(),
				theme: this.currentTheme
			});

		let workspaceStats: WorkspaceStats = <WorkspaceStats>this.workbench.getInstantiationService().createInstance(WorkspaceStats);
		workspaceStats.reportWorkspaceTags();
J
Joao Moreno 已提交
242 243 244 245

		if ((platform.isLinux || platform.isMacintosh) && process.getuid() === 0) {
			this.messageService.show(Severity.Warning, nls.localize('runningAsRoot', "It is recommended not to run Code as 'root'."));
		}
B
Benjamin Pasero 已提交
246 247
	}

248
	private initServiceCollection(): ServiceCollection {
B
Benjamin Pasero 已提交
249
		this.windowService = new WindowService();
250

A
Alex Dima 已提交
251
		let disableWorkspaceStorage = this.configuration.env.extensionTestsPath || (!this.workspace && !this.configuration.env.extensionDevelopmentPath); // without workspace or in any extension test, we use inMemory storage unless we develop an extension where we want to preserve state
252
		this.storageService = new Storage(this.contextService, window.localStorage, disableWorkspaceStorage ? inMemoryLocalStorageInstance : window.localStorage);
E
Erich Gamma 已提交
253

254
		if (this.configuration.env.isBuilt && !this.configuration.env.extensionDevelopmentPath && !!this.configuration.env.enableTelemetry) {
255
			this.telemetryService = new ElectronTelemetryService(this.configurationService, this.storageService, {
256
				cleanupPatterns: [
257 258
					[new RegExp(escapeRegExpCharacters(this.configuration.env.appRoot), 'gi'), ''],
					[new RegExp(escapeRegExpCharacters(this.configuration.env.userExtensionsHome), 'gi'), '']
259
				],
260 261 262 263
				version: this.configuration.env.version,
				commitHash: this.configuration.env.commitHash
			});
		} else {
264
			this.telemetryService = NullTelemetryService;
265
		}
E
Erich Gamma 已提交
266

267
		this.messageService = new MessageService(this.contextService, this.windowService, this.telemetryService);
E
Erich Gamma 已提交
268

B
Benjamin Pasero 已提交
269
		let fileService = new FileService(
270 271
			this.configurationService,
			this.eventService,
272 273
			this.contextService,
			this.messageService
E
Erich Gamma 已提交
274 275
		);

B
Benjamin Pasero 已提交
276
		let lifecycleService = new LifecycleService(this.messageService, this.windowService);
277
		lifecycleService.onShutdown(() => fileService.dispose());
E
Erich Gamma 已提交
278

279
		this.threadService = new MainThreadService(this.contextService, this.messageService, this.windowService, lifecycleService);
E
Erich Gamma 已提交
280

281 282 283 284 285 286 287 288 289
		let extensionService = new MainProcessExtensionService(this.contextService, this.threadService, this.messageService, this.telemetryService);

		this.keybindingService = new WorkbenchKeybindingService(this.configurationService, this.contextService, this.eventService, this.telemetryService, this.messageService, extensionService, <any>window);
		this.messagesShowingContextKey = this.keybindingService.createKey('globalMessageVisible', false);
		this.messageService.onMessagesShowing(() => this.messagesShowingContextKey.set(true));
		this.messageService.onMessagesCleared(() => this.messagesShowingContextKey.reset());

		this.contextViewService = new ContextViewService(this.container, this.telemetryService, this.messageService);

B
Benjamin Pasero 已提交
290 291
		let requestService = new RequestService(
			this.contextService,
292
			this.configurationService,
B
Benjamin Pasero 已提交
293
			this.telemetryService
E
Erich Gamma 已提交
294
		);
295
		lifecycleService.onShutdown(() => requestService.dispose());
E
Erich Gamma 已提交
296

297
		let markerService = new MainProcessMarkerService(this.threadService);
E
Erich Gamma 已提交
298

299 300
		let modeService = new MainThreadModeServiceImpl(this.threadService, extensionService, this.configurationService);
		let modelService = new ModelServiceImpl(this.threadService, markerService, modeService, this.configurationService, this.messageService);
301
		let editorWorkerService = new EditorWorkerServiceImpl(modelService);
E
Erich Gamma 已提交
302 303

		let untitledEditorService = new UntitledEditorService();
M
Martin Aeschlimann 已提交
304
		this.themeService = new ThemeService(extensionService, this.windowService, this.storageService);
E
Erich Gamma 已提交
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
		let result = new ServiceCollection();
		result.set(ITelemetryService, this.telemetryService);
		result.set(IEventService, this.eventService);
		result.set(IRequestService, requestService);
		result.set(IWorkspaceContextService, this.contextService);
		result.set(IContextViewService, this.contextViewService);
		result.set(IContextMenuService, new ContextMenuService(this.messageService, this.telemetryService, this.keybindingService));
		result.set(IMessageService, this.messageService);
		result.set(IStorageService, this.storageService);
		result.set(ILifecycleService, lifecycleService);
		result.set(IThreadService, this.threadService);
		result.set(IExtensionService, extensionService);
		result.set(IModeService, modeService);
		result.set(IFileService, fileService);
		result.set(IUntitledEditorService, untitledEditorService);
		result.set(ISearchService, new SearchService(modelService, untitledEditorService, this.contextService, this.configurationService));
		result.set(IWindowService, this.windowService);
		result.set(IConfigurationService, this.configurationService);
		result.set(IKeybindingService, this.keybindingService);
		result.set(IMarkerService, markerService);
		result.set(IModelService, modelService);
		result.set(ICodeEditorService, new CodeEditorServiceImpl());
		result.set(IEditorWorkerService, editorWorkerService);
		result.set(IThemeService, this.themeService);
		result.set(IActionsService, new ActionsService(extensionService, this.keybindingService));
E
Erich Gamma 已提交
331 332 333 334 335

		return result;
	}

	// TODO@Alex, TODO@Joh move this out of here?
A
Alex Dima 已提交
336
	private initExtensionSystem(): void {
B
Benjamin Pasero 已提交
337 338 339
		this.threadService.getRemotable(MainProcessVSCodeAPIHelper);
		this.threadService.getRemotable(MainThreadDocuments);
		this.threadService.getRemotable(RemoteTelemetryServiceHelper);
E
Erich Gamma 已提交
340 341
		this.workbench.getInstantiationService().createInstance(MainProcessTextMateSyntax);
		this.workbench.getInstantiationService().createInstance(MainProcessTextMateSnippet);
M
Martin Aeschlimann 已提交
342
		this.workbench.getInstantiationService().createInstance(JSONValidationExtensionPoint);
E
Erich Gamma 已提交
343
		this.workbench.getInstantiationService().createInstance(LanguageConfigurationFileHandler);
B
Benjamin Pasero 已提交
344 345 346
		this.threadService.getRemotable(MainThreadConfiguration);
		this.threadService.getRemotable(MainThreadQuickOpen);
		this.threadService.getRemotable(MainThreadStatusBar);
E
Erich Gamma 已提交
347
		this.workbench.getInstantiationService().createInstance(MainThreadFileSystemEventService);
B
Benjamin Pasero 已提交
348 349 350 351 352 353 354 355
		this.threadService.getRemotable(MainThreadCommands);
		this.threadService.getRemotable(MainThreadOutputService);
		this.threadService.getRemotable(MainThreadDiagnostics);
		this.threadService.getRemotable(MainThreadMessageService);
		this.threadService.getRemotable(MainThreadLanguages);
		this.threadService.getRemotable(MainThreadWorkspace);
		this.threadService.getRemotable(MainThreadEditors);
		this.threadService.getRemotable(MainThreadStorage);
J
Johannes Rieken 已提交
356
		this.threadService.getRemotable(MainThreadLanguageFeatures);
E
Erich Gamma 已提交
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
	}

	public open(): void {

		// Listen on unexpected errors
		errors.setUnexpectedErrorHandler((error: any) => {
			this.onUnexpectedError(error);
		});

		// Shell Class for CSS Scoping
		$(this.container).addClass('monaco-shell');

		// Controls
		this.content = $('.monaco-shell-content').appendTo(this.container).getHTMLElement();

		// Handle Load Performance Timers
		this.writeTimers();

		// Create Contents
		this.contentsContainer = this.createContents($(this.content));

		// Layout
		this.layout();

		// Listeners
		this.registerListeners();

		// Enable theme support
M
Martin Aeschlimann 已提交
385
		this.themeService.initialize(this.container).then(null, error => {
386 387
			errors.onUnexpectedError(error);
		});
E
Erich Gamma 已提交
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
	}

	private registerListeners(): void {

		// Resize
		$(window).on(dom.EventType.RESIZE, () => this.layout(), this.toUnbind);
	}

	private writeTimers(): void {
		let timers = (<any>window).MonacoEnvironment.timers;
		if (timers) {
			let events: timer.IExistingTimerEvent[] = [];

			// Program
			if (timers.beforeProgram) {
				events.push({
					startTime: timers.beforeProgram,
					stopTime: timers.afterProgram,
					topic: 'Startup',
					name: 'Program Start',
					description: 'Time it takes to pass control to VSCodes main method'
				});
			}

			// Window
			if (timers.vscodeStart) {
				events.push({
					startTime: timers.vscodeStart,
					stopTime: timers.beforeLoad,
					topic: 'Startup',
					name: 'VSCode Startup',
					description: 'Time it takes to create a window and startup VSCode'
				});
			}

			// Load
			events.push({
				startTime: timers.beforeLoad,
				stopTime: timers.afterLoad,
				topic: 'Startup',
				name: 'Load Modules',
				description: 'Time it takes to load VSCodes main modules'
			});

			// Ready
			events.push({
				startTime: timers.beforeReady,
				stopTime: timers.afterReady,
				topic: 'Startup',
				name: 'Event DOMContentLoaded',
				description: 'Time it takes for the DOM to emit DOMContentLoaded event'
			});

			// Write to Timer
			timer.getTimeKeeper().setInitialCollectedEvents(events, timers.start);
		}
	}

	public onUnexpectedError(error: any): void {
		let errorMsg = errors.toErrorMessage(error, true);
		if (!errorMsg) {
			return;
		}

		let now = new Date().getTime();
		if (errorMsg === this.previousErrorValue && now - this.previousErrorTime <= 1000) {
			return; // Return if error message identical to previous and shorter than 1 second
		}

		this.previousErrorTime = now;
		this.previousErrorValue = errorMsg;

		// Log to console
		console.error(errorMsg);

		// Show to user if friendly message provided
464
		if (error && error.friendlyMessage && this.messageService) {
B
Benjamin Pasero 已提交
465
			this.messageService.show(Severity.Error, error.friendlyMessage);
E
Erich Gamma 已提交
466 467 468 469 470 471 472 473 474
		}
	}

	public layout(): void {
		let clArea = $(this.container).getClientArea();

		let contentsSize = new Dimension(clArea.width, clArea.height);
		this.contentsContainer.size(contentsSize.width, contentsSize.height);

B
Benjamin Pasero 已提交
475
		this.contextViewService.layout();
E
Erich Gamma 已提交
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
		this.workbench.layout();
	}

	public joinCreation(): TPromise<boolean> {
		return this.workbench.joinCreation();
	}

	public dispose(force?: boolean): void {

		// Workbench
		if (this.workbench) {
			let veto = this.workbench.shutdown(force);

			// If Workbench vetos dispose, return early
			if (veto) {
				return;
			}
		}

B
Benjamin Pasero 已提交
495 496
		this.contextViewService.dispose();
		this.storageService.dispose();
E
Erich Gamma 已提交
497 498

		// Listeners
J
Joao Moreno 已提交
499
		this.toUnbind = dispose(this.toUnbind);
E
Erich Gamma 已提交
500 501 502 503 504

		// Container
		$(this.container).empty();
	}
}