index.ts 2.2 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/*---------------------------------------------------------------------------------------------
 *  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 env = require('vs/workbench/electron-main/env');
import events = require('vs/base/common/eventEmitter');
import platform = require('vs/base/common/platform');

13
import { ipcMain as ipc, BrowserWindow } from 'electron';
E
Erich Gamma 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

interface ICredentialsContext {
	id: number;
	host: string;
	command: string;
}

interface ICredentials {
	username: string;
	password: string;
}

interface ICredentialsResult {
	id: number;
	credentials: ICredentials;
}

interface IContext {
	credentials: ICredentials;
33
	window: Electron.BrowserWindow;
E
Erich Gamma 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
}

export function configure(bus: events.EventEmitter): void {
	var cache: { [id: string]: IContext } = Object.create(null);

	ipc.on('git:askpass', (event, result: ICredentialsResult) => {
		cache[result.id].credentials = result.credentials;
	});

	bus.addListener('git:askpass', (context: ICredentialsContext) => {
		var cachedResult = cache[context.id];

		if (typeof cachedResult !== 'undefined') {
			bus.emit('git:askpass:' + context.id, cachedResult.credentials);
			return;
		}

		if (context.command === 'fetch') {
			bus.emit('git:askpass:' + context.id, { id: context.id, credentials: { username: '', password: '' }});
			return;
		}

		var win = new BrowserWindow({
57 58
			alwaysOnTop: true,
			skipTaskbar: true,
E
Erich Gamma 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72
			resizable: false,
			width: 450,
			height: platform.isWindows ? 280 : 260,
			show: true,
			title: env.product.nameLong
		});

		win.setMenuBarVisibility(false);

		cache[context.id] = {
			window: win,
			credentials: null
		};

B
Benjamin Pasero 已提交
73
		win.loadURL(require.toUrl('vs/workbench/parts/git/electron-main/index.html'));
E
Erich Gamma 已提交
74 75 76 77 78 79 80 81 82 83 84
		win.webContents.executeJavaScript('init(' + JSON.stringify(context) + ')');

		win.once('closed', () => {
			bus.emit('git:askpass:' + context.id, cache[context.id].credentials);

			setTimeout(function () {
				delete cache[context.id];
			}, 1000 * 10);
		});
	});
}