/*--------------------------------------------------------------------------------------------- * 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 { TPromise } from 'vs/base/common/winjs.base'; import Event, { Emitter } from 'vs/base/common/event'; import { MainContext, MainThreadDebugServiceShape, ExtHostDebugServiceShape, DebugSessionUUID, IMainContext } from 'vs/workbench/api/node/extHost.protocol'; import * as vscode from 'vscode'; import URI from 'vs/base/common/uri'; export class ExtHostDebugService implements ExtHostDebugServiceShape { private _debugServiceProxy: MainThreadDebugServiceShape; private _debugSessions: Map = new Map(); private _onDidStartDebugSession: Emitter; get onDidStartDebugSession(): Event { return this._onDidStartDebugSession.event; } private _onDidTerminateDebugSession: Emitter; get onDidTerminateDebugSession(): Event { return this._onDidTerminateDebugSession.event; } private _onDidChangeActiveDebugSession: Emitter; get onDidChangeActiveDebugSession(): Event { return this._onDidChangeActiveDebugSession.event; } private _activeDebugSession: ExtHostDebugSession | undefined; get activeDebugSession(): ExtHostDebugSession | undefined { return this._activeDebugSession; } private _onDidReceiveDebugSessionCustomEvent: Emitter; get onDidReceiveDebugSessionCustomEvent(): Event { return this._onDidReceiveDebugSessionCustomEvent.event; } constructor(mainContext: IMainContext) { this._onDidStartDebugSession = new Emitter(); this._onDidTerminateDebugSession = new Emitter(); this._onDidChangeActiveDebugSession = new Emitter(); this._onDidReceiveDebugSessionCustomEvent = new Emitter(); this._debugServiceProxy = mainContext.get(MainContext.MainThreadDebugService); } public startDebugging(folder: vscode.WorkspaceFolder | undefined, nameOrConfig: string | vscode.DebugConfiguration): TPromise { return this._debugServiceProxy.$startDebugging(folder ? folder.uri : undefined, nameOrConfig); } public startDebugSession(folder: vscode.WorkspaceFolder | undefined, config: vscode.DebugConfiguration): TPromise { return this._debugServiceProxy.$startDebugSession(folder ? folder.uri : undefined, config).then((id: DebugSessionUUID) => { const debugSession = new ExtHostDebugSession(this._debugServiceProxy, id, config.type, config.name); this._debugSessions.set(id, debugSession); return debugSession; }); } public $acceptDebugSessionStarted(id: DebugSessionUUID, type: string, name: string): void { let debugSession = this._debugSessions.get(id); if (!debugSession) { debugSession = new ExtHostDebugSession(this._debugServiceProxy, id, type, name); this._debugSessions.set(id, debugSession); } this._onDidStartDebugSession.fire(debugSession); } public $acceptDebugSessionTerminated(id: DebugSessionUUID, type: string, name: string): void { let debugSession = this._debugSessions.get(id); if (!debugSession) { debugSession = new ExtHostDebugSession(this._debugServiceProxy, id, type, name); this._debugSessions.set(id, debugSession); } this._onDidTerminateDebugSession.fire(debugSession); this._debugSessions.delete(id); } public $acceptDebugSessionActiveChanged(id: DebugSessionUUID | undefined, type?: string, name?: string): void { if (id) { this._activeDebugSession = this._debugSessions.get(id); if (!this._activeDebugSession) { this._activeDebugSession = new ExtHostDebugSession(this._debugServiceProxy, id, type, name); this._debugSessions.set(id, this._activeDebugSession); } } else { this._activeDebugSession = undefined; } this._onDidChangeActiveDebugSession.fire(this._activeDebugSession); } public $acceptDebugSessionCustomEvent(id: DebugSessionUUID, type: string, name: string, event: any): void { let debugSession = this._debugSessions.get(id); if (!debugSession) { debugSession = new ExtHostDebugSession(this._debugServiceProxy, id, type, name); this._debugSessions.set(id, debugSession); } const ee: vscode.DebugSessionCustomEvent = { session: debugSession, event: event.event, body: event.body }; this._onDidReceiveDebugSessionCustomEvent.fire(ee); } } export class ExtHostDebugSession implements vscode.DebugSession { private _debugServiceProxy: MainThreadDebugServiceShape; private _id: DebugSessionUUID; private _type: string; private _name: string; constructor(proxy: MainThreadDebugServiceShape, id: DebugSessionUUID, type: string, name: string) { this._debugServiceProxy = proxy; this._id = id; this._type = type; this._name = name; }; public get id(): string { return this._id; } public get type(): string { return this._type; } public get name(): string { return this._name; } public customRequest(command: string, args: any): Thenable { return this._debugServiceProxy.$customDebugAdapterRequest(this._id, command, args); } }