mainThreadDebugService.ts 3.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*---------------------------------------------------------------------------------------------
 *  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 { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { IDebugService, IProcess, IConfig } from 'vs/workbench/parts/debug/common/debug';
import { IThreadService } from 'vs/workbench/services/thread/common/threadService';
import { TPromise } from 'vs/base/common/winjs.base';
import { ExtHostContext, ExtHostDebugServiceShape, MainThreadDebugServiceShape, DebugSessionUUID } from '../node/extHost.protocol';

export class MainThreadDebugService extends MainThreadDebugServiceShape {

	private _proxy: ExtHostDebugServiceShape;
	private _toDispose: IDisposable[];

	constructor(
		@IThreadService threadService: IThreadService,
		@IDebugService private debugService: IDebugService
	) {
		super();
23

24 25 26
		this._proxy = threadService.get(ExtHostContext.ExtHostDebugService);
		this._toDispose = [];
		this._toDispose.push(debugService.onDidEndProcess(proc => this._proxy.$acceptDebugSessionTerminated(<DebugSessionUUID>proc.getId(), proc.configuration.type, proc.name)));
27 28 29 30 31 32 33
		this._toDispose.push(debugService.getViewModel().onDidFocusProcess(proc => {
			if (proc) {
				this._proxy.$acceptDebugSessionActiveChanged(<DebugSessionUUID>proc.getId(), proc.configuration.type, proc.name);
			} else {
				this._proxy.$acceptDebugSessionActiveChanged(undefined);
			}
		}));
34 35 36 37 38 39 40 41
	}

	public dispose(): void {
		this._toDispose = dispose(this._toDispose);
	}

	public $createDebugSession(configuration: IConfig): TPromise<DebugSessionUUID> {
		if (configuration.request !== 'launch' && configuration.request !== 'attach') {
42
			return TPromise.wrapError(new Error(`only 'launch' or 'attach' allowed for 'request' attribute`));
43 44 45 46 47
		}
		return this.debugService.createProcess(configuration).then(process => {
			if (process) {
				return <DebugSessionUUID>process.getId();
			}
48
			return TPromise.wrapError(new Error('cannot create debug session'));
49 50 51 52 53 54 55 56 57 58 59 60
		}, err => {
			return TPromise.wrapError(err && err.message ? err.message : 'cannot create debug session');
		});
	}

	public $customDebugAdapterRequest(sessionId: DebugSessionUUID, request: string, args: any): TPromise<any> {
		const process = this._findProcessByUUID(sessionId);
		if (process) {
			return process.session.custom(request, args).then(response => {
				if (response.success) {
					return response.body;
				} else {
61
					return TPromise.wrapError(new Error(response.message));
62 63 64
				}
			});
		}
65
		return TPromise.wrapError(new Error('debug session not found'));
66 67 68 69 70 71 72 73 74 75 76
	}

	private _findProcessByUUID(processId: DebugSessionUUID): IProcess | null {
		const processes = this.debugService.getModel().getProcesses();
		const result = processes.filter(process => process.getId() === processId);
		if (result.length > 0) {
			return processes[0];	// there can only be one
		}
		return null;
	}
}