mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-01-15 09:35:37 -05:00
* Initial VS Code 1.21 file copy with patches * A few more merges * Post npm install * Fix batch of build breaks * Fix more build breaks * Fix more build errors * Fix more build breaks * Runtime fixes 1 * Get connection dialog working with some todos * Fix a few packaging issues * Copy several node_modules to package build to fix loader issues * Fix breaks from master * A few more fixes * Make tests pass * First pass of license header updates * Second pass of license header updates * Fix restore dialog issues * Remove add additional themes menu items * fix select box issues where the list doesn't show up * formatting * Fix editor dispose issue * Copy over node modules to correct location on all platforms
147 lines
5.6 KiB
TypeScript
147 lines
5.6 KiB
TypeScript
/*---------------------------------------------------------------------------------------------
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
|
*--------------------------------------------------------------------------------------------*/
|
|
'use strict';
|
|
|
|
import { TPromise } from 'vs/base/common/winjs.base';
|
|
import { IMainContext } from 'vs/workbench/api/node/extHost.protocol';
|
|
import { SqlMainContext, MainThreadCredentialManagementShape, ExtHostCredentialManagementShape } from 'sql/workbench/api/node/sqlExtHost.protocol';
|
|
import * as vscode from 'vscode';
|
|
import * as sqlops from 'sqlops';
|
|
import { Disposable } from 'vs/workbench/api/node/extHostTypes';
|
|
|
|
class CredentialAdapter {
|
|
public provider: sqlops.CredentialProvider;
|
|
|
|
constructor(provider: sqlops.CredentialProvider) {
|
|
this.provider = provider;
|
|
}
|
|
|
|
public saveCredential(credentialId: string, password: string): Thenable<boolean> {
|
|
return this.provider.saveCredential(credentialId, password);
|
|
}
|
|
|
|
public readCredential(credentialId: string): Thenable<sqlops.Credential> {
|
|
return this.provider.readCredential(credentialId);
|
|
}
|
|
|
|
public deleteCredential(credentialId: string): Thenable<boolean> {
|
|
return this.provider.deleteCredential(credentialId);
|
|
}
|
|
}
|
|
|
|
type Adapter = CredentialAdapter;
|
|
|
|
export class ExtHostCredentialManagement extends ExtHostCredentialManagementShape {
|
|
// MEMBER VARIABLES ////////////////////////////////////////////////////
|
|
private _adapter: { [handle: number]: Adapter } = Object.create(null);
|
|
private _handlePool: number = 0;
|
|
private _proxy: MainThreadCredentialManagementShape;
|
|
private _registrationPromise: Promise<void>;
|
|
private _registrationPromiseResolve;
|
|
|
|
constructor(mainContext: IMainContext) {
|
|
super();
|
|
|
|
let self = this;
|
|
|
|
this._proxy = mainContext.getProxy(SqlMainContext.MainThreadCredentialManagement);
|
|
|
|
// Create a promise to resolve when a credential provider has been registered.
|
|
// HACK: this gives us a deferred promise
|
|
this._registrationPromise = new Promise((resolve) => { self._registrationPromiseResolve = resolve; });
|
|
}
|
|
|
|
// PUBLIC METHODS //////////////////////////////////////////////////////
|
|
public $registerCredentialProvider(provider: sqlops.CredentialProvider): vscode.Disposable {
|
|
// Store the credential provider
|
|
provider.handle = this._nextHandle();
|
|
this._adapter[provider.handle] = new CredentialAdapter(provider);
|
|
|
|
// Register the credential provider with the main thread
|
|
this._proxy.$registerCredentialProvider(provider.handle);
|
|
|
|
// Resolve the credential provider registration promise
|
|
this._registrationPromiseResolve();
|
|
return this._createDisposable(provider.handle);
|
|
}
|
|
|
|
public $getCredentialProvider(namespaceId: string): Thenable<sqlops.CredentialProvider> {
|
|
let self = this;
|
|
|
|
if (!namespaceId) {
|
|
return TPromise.wrapError(new Error('A namespace must be provided when retrieving a credential provider'));
|
|
}
|
|
|
|
// When the registration promise has finished successfully,
|
|
return this._registrationPromise.then(() =>
|
|
self._withAdapter(0, CredentialAdapter, adapter => self._createNamespacedCredentialProvider(namespaceId, adapter))
|
|
);
|
|
}
|
|
|
|
public $saveCredential(credentialId: string, password: string): Thenable<boolean> {
|
|
return this._withAdapter(0, CredentialAdapter, adapter => adapter.saveCredential(credentialId, password));
|
|
}
|
|
|
|
public $readCredential(credentialId: string): Thenable<sqlops.Credential> {
|
|
return this._withAdapter(0, CredentialAdapter, adapter => adapter.readCredential(credentialId));
|
|
}
|
|
|
|
public $deleteCredential(credentialId: string): Thenable<boolean> {
|
|
return this._withAdapter(0, CredentialAdapter, adapter => adapter.deleteCredential(credentialId));
|
|
}
|
|
|
|
/**
|
|
* Helper method for tests. Not exposed via shape.
|
|
* @return {number} Number of providers registered
|
|
*/
|
|
public getProviderCount(): number {
|
|
return Object.keys(this._adapter).length;
|
|
}
|
|
|
|
// PRIVATE HELPERS /////////////////////////////////////////////////////
|
|
private static _getNamespacedCredentialId(namespaceId: string, credentialId: string) {
|
|
return `${namespaceId}|${credentialId}`;
|
|
}
|
|
|
|
private _createNamespacedCredentialProvider(namespaceId: string, adapter: CredentialAdapter): Thenable<sqlops.CredentialProvider> {
|
|
// Create a provider that wraps the methods in a namespace
|
|
let provider: sqlops.CredentialProvider = {
|
|
handle: adapter.provider.handle,
|
|
deleteCredential: (credentialId: string) => {
|
|
let namespacedId = ExtHostCredentialManagement._getNamespacedCredentialId(namespaceId, credentialId);
|
|
return adapter.provider.deleteCredential(namespacedId);
|
|
},
|
|
readCredential: (credentialId: string) => {
|
|
let namespacedId = ExtHostCredentialManagement._getNamespacedCredentialId(namespaceId, credentialId);
|
|
return adapter.provider.readCredential(namespacedId);
|
|
},
|
|
saveCredential: (credentialId: string, credential: string) => {
|
|
let namespacedId = ExtHostCredentialManagement._getNamespacedCredentialId(namespaceId, credentialId);
|
|
return adapter.provider.saveCredential(namespacedId, credential);
|
|
}
|
|
};
|
|
return Promise.resolve(provider);
|
|
}
|
|
|
|
private _createDisposable(handle: number): Disposable {
|
|
return new Disposable(() => {
|
|
delete this._adapter[handle];
|
|
this._proxy.$unregisterCredentialProvider(handle);
|
|
});
|
|
}
|
|
|
|
private _nextHandle(): number {
|
|
return this._handlePool++;
|
|
}
|
|
|
|
private _withAdapter<A, R>(handle: number, ctor: { new(...args: any[]): A }, callback: (adapter: A) => Thenable<R>): Thenable<R> {
|
|
let adapter = this._adapter[handle];
|
|
if (!(adapter instanceof ctor)) {
|
|
return TPromise.wrapError(new Error('no adapter found'));
|
|
}
|
|
return callback(<any>adapter);
|
|
}
|
|
}
|