main.ts 11.1 KB
Newer Older
E
Erich Gamma 已提交
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 nls = require('vs/nls');
J
Johannes Rieken 已提交
9 10 11
import { TPromise } from 'vs/base/common/winjs.base';
import { WorkbenchShell } from 'vs/workbench/electron-browser/shell';
import { IOptions } from 'vs/workbench/common/options';
12
import * as browser from 'vs/base/browser/browser';
J
Johannes Rieken 已提交
13
import { domContentLoaded } from 'vs/base/browser/dom';
E
Erich Gamma 已提交
14
import errors = require('vs/base/common/errors');
15
import comparer = require('vs/base/common/comparers');
E
Erich Gamma 已提交
16 17 18 19
import platform = require('vs/base/common/platform');
import paths = require('vs/base/common/paths');
import uri from 'vs/base/common/uri';
import strings = require('vs/base/common/strings');
J
Johannes Rieken 已提交
20
import { IResourceInput } from 'vs/platform/editor/common/editor';
21 22
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { EmptyWorkspaceServiceImpl, WorkspaceServiceImpl, WorkspaceService } from 'vs/workbench/services/configuration/node/configuration';
23 24
import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors';
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
25
import { realpath } from 'vs/base/node/pfs';
J
Johannes Rieken 已提交
26
import { EnvironmentService } from 'vs/platform/environment/node/environmentService';
E
Erich Gamma 已提交
27 28
import path = require('path');
import gracefulFs = require('graceful-fs');
B
Benjamin Pasero 已提交
29 30
import { IInitData } from 'vs/workbench/services/timer/common/timerService';
import { TimerService } from 'vs/workbench/services/timer/node/timerService';
A
Alex Dima 已提交
31
import { KeyboardMapperFactory } from "vs/workbench/services/keybinding/electron-browser/keybindingService";
32 33
import { IWindowConfiguration, IPath, IWindowsService } from 'vs/platform/windows/common/windows';
import { WindowsChannelClient } from 'vs/platform/windows/common/windowsIpc';
B
Benjamin Pasero 已提交
34 35 36
import { IStorageService } from 'vs/platform/storage/common/storage';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { StorageService, inMemoryLocalStorageInstance } from 'vs/platform/storage/common/storageService';
37 38 39 40 41 42 43 44
import { Client as ElectronIPCClient } from 'vs/base/parts/ipc/electron-browser/ipc.electron-browser';
import { webFrame, remote } from 'electron';
import { UpdateChannelClient } from 'vs/platform/update/common/updateIpc';
import { IUpdateService } from 'vs/platform/update/common/update';
import { URLChannelClient } from 'vs/platform/url/common/urlIpc';
import { IURLService } from 'vs/platform/url/common/url';
import { WorkspacesChannelClient } from 'vs/platform/workspaces/common/workspacesIpc';
import { IWorkspacesService } from 'vs/platform/workspaces/common/workspaces';
45

B
Benjamin Pasero 已提交
46
import fs = require('fs');
B
Benjamin Pasero 已提交
47
gracefulFs.gracefulify(fs); // enable gracefulFs
E
Erich Gamma 已提交
48

49 50
const currentWindowId = remote.getCurrentWindow().id;

51
export function startup(configuration: IWindowConfiguration): TPromise<void> {
52

53 54
	// Ensure others can listen to zoom level changes
	browser.setZoomFactor(webFrame.getZoomFactor());
55

56 57
	// See https://github.com/Microsoft/vscode/issues/26151
	// Can be trusted because we are not setting it ourselves.
58
	browser.setZoomLevel(webFrame.getZoomLevel(), true /* isTrusted */);
59

60 61
	browser.setFullscreen(!!configuration.fullscreen);

A
Alex Dima 已提交
62 63
	KeyboardMapperFactory.INSTANCE._onKeyboardLayoutChanged(configuration.isISOKeyboard);

64
	browser.setAccessibilitySupport(configuration.accessibilitySupport ? platform.AccessibilitySupport.Enabled : platform.AccessibilitySupport.Disabled);
65

66 67 68
	// Setup Intl
	comparer.setFileNameComparer(new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }));

E
Erich Gamma 已提交
69
	// Shell Options
B
polish  
Benjamin Pasero 已提交
70 71 72
	const filesToOpen = configuration.filesToOpen && configuration.filesToOpen.length ? toInputs(configuration.filesToOpen) : null;
	const filesToCreate = configuration.filesToCreate && configuration.filesToCreate.length ? toInputs(configuration.filesToCreate) : null;
	const filesToDiff = configuration.filesToDiff && configuration.filesToDiff.length ? toInputs(configuration.filesToDiff) : null;
B
Benjamin Pasero 已提交
73
	const shellOptions: IOptions = {
74 75
		filesToOpen,
		filesToCreate,
B
Benjamin Pasero 已提交
76
		filesToDiff
E
Erich Gamma 已提交
77 78
	};

79 80
	// Open workbench
	return openWorkbench(configuration, shellOptions);
E
Erich Gamma 已提交
81 82
}

83
function toInputs(paths: IPath[], isUntitledFile?: boolean): IResourceInput[] {
E
Erich Gamma 已提交
84
	return paths.map(p => {
85 86 87 88 89 90 91
		const input = <IResourceInput>{};

		if (isUntitledFile) {
			input.resource = uri.from({ scheme: 'untitled', path: p.filePath });
		} else {
			input.resource = uri.file(p.filePath);
		}
E
Erich Gamma 已提交
92

93 94 95 96
		input.options = {
			pinned: true // opening on startup is always pinned and not preview
		};

E
Erich Gamma 已提交
97
		if (p.lineNumber) {
98 99 100
			input.options.selection = {
				startLineNumber: p.lineNumber,
				startColumn: p.columnNumber
E
Erich Gamma 已提交
101 102 103 104 105 106 107
			};
		}

		return input;
	});
}

108
function openWorkbench(configuration: IWindowConfiguration, options: IOptions): TPromise<void> {
109 110
	const mainProcessClient = new ElectronIPCClient(String(`window${currentWindowId}`));
	const mainServices = createMainProcessServices(mainProcessClient);
111

112 113 114 115
	const environmentService = new EnvironmentService(configuration, configuration.execPath);

	// Since the configuration service is one of the core services that is used in so many places, we initialize it
	// right before startup of the workbench shell to have its data ready for consumers
116
	return createAndInitializeWorkspaceService(configuration, environmentService, <IWorkspacesService>mainServices.get(IWorkspacesService)).then(workspaceService => {
117 118
		const timerService = new TimerService((<any>window).MonacoEnvironment.timers as IInitData, !workspaceService.hasWorkspace());
		const storageService = createStorageService(configuration, workspaceService, environmentService);
119 120 121 122 123 124 125 126 127 128 129 130 131 132

		timerService.beforeDOMContentLoaded = Date.now();

		return domContentLoaded().then(() => {
			timerService.afterDOMContentLoaded = Date.now();

			// Open Shell
			timerService.beforeWorkbenchOpen = Date.now();
			const shell = new WorkbenchShell(document.body, {
				contextService: workspaceService,
				configurationService: workspaceService,
				environmentService,
				timerService,
				storageService
133
			}, mainServices, configuration, options);
134 135 136 137 138 139 140
			shell.open();

			// Inform user about loading issues from the loader
			(<any>self).require.config({
				onError: (err: any) => {
					if (err.errorCode === 'load') {
						shell.onUnexpectedError(loaderError(err));
141
					}
142
				}
143 144 145 146 147
			});
		});
	});
}

148
function createAndInitializeWorkspaceService(configuration: IWindowConfiguration, environmentService: EnvironmentService, workspacesService: IWorkspacesService): TPromise<WorkspaceService> {
149
	return validateWorkspacePath(configuration).then(() => {
150 151 152
		const workspaceConfigPath = configuration.workspace ? uri.file(configuration.workspace.configPath) : null;
		const folderPath = configuration.folderPath ? uri.file(configuration.folderPath) : null;
		const workspaceService = (workspaceConfigPath || configuration.folderPath) ? new WorkspaceServiceImpl(workspaceConfigPath, folderPath, environmentService, workspacesService) : new EmptyWorkspaceServiceImpl(environmentService);
B
Benjamin Pasero 已提交
153

154 155
		return workspaceService.initialize().then(() => workspaceService, error => new EmptyWorkspaceServiceImpl(environmentService));
	});
156 157
}

158 159 160
function validateWorkspacePath(configuration: IWindowConfiguration): TPromise<void> {
	if (!configuration.folderPath) {
		return TPromise.as(null);
E
Erich Gamma 已提交
161 162
	}

163
	return realpath(configuration.folderPath).then(realFolderPath => {
164

E
Erich Gamma 已提交
165 166 167 168
		// for some weird reason, node adds a trailing slash to UNC paths
		// we never ever want trailing slashes as our workspace path unless
		// someone opens root ("/").
		// See also https://github.com/nodejs/io.js/issues/1765
169 170
		if (paths.isUNC(realFolderPath) && strings.endsWith(realFolderPath, paths.nativeSep)) {
			realFolderPath = strings.rtrim(realFolderPath, paths.nativeSep);
171
		}
E
Erich Gamma 已提交
172

173
		// update config
174
		configuration.folderPath = realFolderPath;
175
	}, error => {
176 177
		errors.onUnexpectedError(error);

178
		return null; // treat invalid paths as empty workspace
179
	});
E
Erich Gamma 已提交
180 181
}

182 183
function createStorageService(configuration: IWindowConfiguration, workspaceService: IWorkspaceContextService, environmentService: IEnvironmentService): IStorageService {
	const workspace = workspaceService.getWorkspace();
184 185 186 187

	let workspaceId: string;
	let secondaryWorkspaceId: number;

188 189 190 191
	// in multi root workspace mode we use the provided ID as key for workspace storage
	if (workspaceService.hasMultiFolderWorkspace()) {
		workspaceId = uri.from({ path: workspace.id, scheme: 'root' }).toString();
	}
192

193 194 195 196 197 198
	// in single folder mode we use the path of the opened folder as key for workspace storage
	// the ctime is used as secondary workspace id to clean up stale UI state if necessary
	else if (workspaceService.hasFolderWorkspace()) {
		const legacyWorkspace = workspaceService.getLegacyWorkspace();
		workspaceId = legacyWorkspace.resource.toString();
		secondaryWorkspaceId = legacyWorkspace.ctime;
199 200 201 202 203 204 205 206 207 208
	}

	// finaly, if we do not have a workspace open, we need to find another identifier for the window to store
	// workspace UI state. if we have a backup path in the configuration we can use that because this
	// will be a unique identifier per window that is stable between restarts as long as there are
	// dirty files in the workspace.
	// We use basename() to produce a short identifier, we do not need the full path. We use a custom
	// scheme so that we can later distinguish these identifiers from the workspace one.
	else if (configuration.backupPath) {
		workspaceId = uri.from({ path: path.basename(configuration.backupPath), scheme: 'empty' }).toString();
209 210
	}

211 212
	const disableStorage = !!environmentService.extensionTestsPath; // never keep any state when running extension tests!
	const storage = disableStorage ? inMemoryLocalStorageInstance : window.localStorage;
213

214
	return new StorageService(storage, storage, workspaceId, secondaryWorkspaceId);
215 216
}

217
function createMainProcessServices(mainProcessClient: ElectronIPCClient): ServiceCollection {
218 219 220 221 222 223 224 225 226
	const serviceCollection = new ServiceCollection();

	const windowsChannel = mainProcessClient.getChannel('windows');
	serviceCollection.set(IWindowsService, new WindowsChannelClient(windowsChannel));

	const updateChannel = mainProcessClient.getChannel('update');
	serviceCollection.set(IUpdateService, new SyncDescriptor(UpdateChannelClient, updateChannel));

	const urlChannel = mainProcessClient.getChannel('url');
227
	serviceCollection.set(IURLService, new SyncDescriptor(URLChannelClient, urlChannel, currentWindowId));
228 229

	const workspacesChannel = mainProcessClient.getChannel('workspaces');
230
	serviceCollection.set(IWorkspacesService, new WorkspacesChannelClient(workspacesChannel));
231 232 233 234

	return serviceCollection;
}

235 236 237 238 239 240
function loaderError(err: Error): Error {
	if (platform.isWeb) {
		return new Error(nls.localize('loaderError', "Failed to load a required file. Either you are no longer connected to the internet or the server you are connected to is offline. Please refresh the browser to try again."));
	}

	return new Error(nls.localize('loaderErrorNative', "Failed to load a required file. Please restart the application to try again. Details: {0}", JSON.stringify(err)));
241
}