From 18daad32ff1654b7356d99091a1e5eb4c04dc2a0 Mon Sep 17 00:00:00 2001 From: Sandeep Somavarapu Date: Fri, 14 Jul 2017 19:23:53 +0200 Subject: [PATCH] Create electron main process services in workbech main --- src/vs/workbench/electron-browser/main.ts | 40 +++++++++++++++++++--- src/vs/workbench/electron-browser/shell.ts | 36 +++++-------------- 2 files changed, 45 insertions(+), 31 deletions(-) diff --git a/src/vs/workbench/electron-browser/main.ts b/src/vs/workbench/electron-browser/main.ts index b2af47dd283..587449fda05 100644 --- a/src/vs/workbench/electron-browser/main.ts +++ b/src/vs/workbench/electron-browser/main.ts @@ -20,6 +20,8 @@ import strings = require('vs/base/common/strings'); import { IResourceInput } from 'vs/platform/editor/common/editor'; import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace'; import { EmptyWorkspaceServiceImpl, WorkspaceServiceImpl, WorkspaceService } from 'vs/workbench/services/configuration/node/configuration'; +import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors'; +import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection'; import { realpath } from 'vs/base/node/pfs'; import { EnvironmentService } from 'vs/platform/environment/node/environmentService'; import path = require('path'); @@ -27,12 +29,19 @@ import gracefulFs = require('graceful-fs'); import { IInitData } from 'vs/workbench/services/timer/common/timerService'; import { TimerService } from 'vs/workbench/services/timer/node/timerService'; import { KeyboardMapperFactory } from "vs/workbench/services/keybinding/electron-browser/keybindingService"; -import { IWindowConfiguration, IPath } from 'vs/platform/windows/common/windows'; +import { IWindowConfiguration, IPath, IWindowsService } from 'vs/platform/windows/common/windows'; +import { WindowsChannelClient } from 'vs/platform/windows/common/windowsIpc'; 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'; - -import { webFrame } from 'electron'; +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'; import fs = require('fs'); gracefulFs.gracefulify(fs); // enable gracefulFs @@ -95,6 +104,11 @@ function toInputs(paths: IPath[], isUntitledFile?: boolean): IResourceInput[] { } function openWorkbench(configuration: IWindowConfiguration, options: IOptions): TPromise { + + const currentWindow = remote.getCurrentWindow(); + const mainProcessClient = new ElectronIPCClient(String(`window${currentWindow.id}`)); + const mainServices = createMainProcessServices(mainProcessClient, currentWindow); + 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 @@ -116,7 +130,7 @@ function openWorkbench(configuration: IWindowConfiguration, options: IOptions): environmentService, timerService, storageService - }, configuration, options); + }, mainServices, configuration, options); shell.open(); // Inform user about loading issues from the loader @@ -199,6 +213,24 @@ function createStorageService(configuration: IWindowConfiguration, workspaceServ return new StorageService(storage, storage, workspaceId, secondaryWorkspaceId); } +function createMainProcessServices(mainProcessClient: ElectronIPCClient, currentWindow: Electron.BrowserWindow): ServiceCollection { + 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'); + serviceCollection.set(IURLService, new SyncDescriptor(URLChannelClient, urlChannel, currentWindow.id)); + + const workspacesChannel = mainProcessClient.getChannel('workspaces'); + serviceCollection.set(IWorkspacesService, new SyncDescriptor(WorkspacesChannelClient, workspacesChannel)); + + return serviceCollection; +} + 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.")); diff --git a/src/vs/workbench/electron-browser/shell.ts b/src/vs/workbench/electron-browser/shell.ts index 77a24f8ecfd..7a5981df770 100644 --- a/src/vs/workbench/electron-browser/shell.ts +++ b/src/vs/workbench/electron-browser/shell.ts @@ -33,7 +33,6 @@ import { resolveWorkbenchCommonProperties, getOrCreateMachineId } from 'vs/platf import { machineIdIpcChannel } from 'vs/platform/telemetry/node/commonProperties'; import { WorkspaceStats } from 'vs/workbench/services/telemetry/common/workspaceStats'; import { IWindowsService, IWindowService, IWindowConfiguration } from 'vs/platform/windows/common/windows'; -import { WindowsChannelClient } from 'vs/platform/windows/common/windowsIpc'; import { WindowService } from 'vs/platform/windows/electron-browser/windowService'; import { MessageService } from 'vs/workbench/services/message/electron-browser/messageService'; import { IRequestService } from 'vs/platform/request/node/request'; @@ -52,8 +51,6 @@ import { IIntegrityService } from 'vs/platform/integrity/common/integrity'; import { EditorWorkerServiceImpl } from 'vs/editor/common/services/editorWorkerServiceImpl'; import { IEditorWorkerService } from 'vs/editor/common/services/editorWorkerService'; import { MainProcessExtensionService } from 'vs/workbench/api/electron-browser/mainThreadExtensionService'; -import { IWorkspacesService } from 'vs/platform/workspaces/common/workspaces'; -import { WorkspacesChannelClient } from 'vs/platform/workspaces/common/workspacesIpc'; import { IOptions } from 'vs/workbench/common/options'; import { IStorageService } from 'vs/platform/storage/common/storage'; import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; @@ -79,14 +76,9 @@ import { CrashReporterService } from 'vs/workbench/services/crashReporter/electr import { NodeCachedDataManager } from 'vs/workbench/electron-browser/nodeCachedDataManager'; import { getDelayedChannel } from 'vs/base/parts/ipc/common/ipc'; import { connect as connectNet } from 'vs/base/parts/ipc/node/ipc.net'; -import { Client as ElectronIPCClient } from 'vs/base/parts/ipc/electron-browser/ipc.electron-browser'; import { IExtensionManagementChannel, ExtensionManagementChannelClient } from 'vs/platform/extensionManagement/common/extensionManagementIpc'; import { IExtensionManagementService, IExtensionEnablementService } from 'vs/platform/extensionManagement/common/extensionManagement'; import { ExtensionEnablementService } from 'vs/platform/extensionManagement/common/extensionEnablementService'; -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 { ExtensionHostProcessWorker } from 'vs/workbench/electron-browser/extensionHost'; import { ITimerService } from 'vs/workbench/services/timer/common/timerService'; import { remote, ipcRenderer as ipc } from 'electron'; @@ -131,12 +123,13 @@ export class WorkbenchShell { private contextService: IWorkspaceContextService; private telemetryService: ITelemetryService; private extensionService: MainProcessExtensionService; - private windowsService: IWindowsService; private broadcastService: IBroadcastService; private timerService: ITimerService; private themeService: WorkbenchThemeService; private lifecycleService: LifecycleService; + private mainProcessServices: ServiceCollection; + private container: HTMLElement; private toUnbind: IDisposable[]; private previousErrorValue: string; @@ -148,7 +141,7 @@ export class WorkbenchShell { private options: IOptions; private workbench: Workbench; - constructor(container: HTMLElement, services: ICoreServices, configuration: IWindowConfiguration, options: IOptions) { + constructor(container: HTMLElement, services: ICoreServices, mainProcessServices: ServiceCollection, configuration: IWindowConfiguration, options: IOptions) { this.container = container; this.configuration = configuration; @@ -160,6 +153,8 @@ export class WorkbenchShell { this.timerService = services.timerService; this.storageService = services.storageService; + this.mainProcessServices = mainProcessServices; + this.toUnbind = []; this.previousErrorTime = 0; } @@ -255,22 +250,18 @@ export class WorkbenchShell { serviceCollection.set(IEnvironmentService, this.environmentService); serviceCollection.set(ITimerService, this.timerService); serviceCollection.set(IStorageService, this.storageService); + this.mainProcessServices.forEach((serviceIdentifier, serviceInstance) => { + serviceCollection.set(serviceIdentifier, serviceInstance); + }); const instantiationService: IInstantiationService = new InstantiationService(serviceCollection, true); this.broadcastService = new BroadcastService(currentWindow.id); serviceCollection.set(IBroadcastService, this.broadcastService); - const mainProcessClient = new ElectronIPCClient(String(`window${currentWindow.id}`)); - disposables.push(mainProcessClient); - - const windowsChannel = mainProcessClient.getChannel('windows'); - this.windowsService = new WindowsChannelClient(windowsChannel); - serviceCollection.set(IWindowsService, this.windowsService); - serviceCollection.set(IWindowService, new SyncDescriptor(WindowService, currentWindow.id)); - const sharedProcess = this.windowsService.whenSharedProcessReady() + const sharedProcess = (serviceCollection.get(IWindowsService)).whenSharedProcessReady() .then(() => connectNet(this.environmentService.sharedIPCHandle, `window:${currentWindow.id}`)); sharedProcess @@ -380,15 +371,6 @@ export class WorkbenchShell { serviceCollection.set(IIntegrityService, new SyncDescriptor(IntegrityServiceImpl)); - const updateChannel = mainProcessClient.getChannel('update'); - serviceCollection.set(IUpdateService, new SyncDescriptor(UpdateChannelClient, updateChannel)); - - const urlChannel = mainProcessClient.getChannel('url'); - serviceCollection.set(IURLService, new SyncDescriptor(URLChannelClient, urlChannel, currentWindow.id)); - - const workspacesChannel = mainProcessClient.getChannel('workspaces'); - serviceCollection.set(IWorkspacesService, new SyncDescriptor(WorkspacesChannelClient, workspacesChannel)); - return [instantiationService, serviceCollection]; } -- GitLab