diff --git a/src/sql/azdata.proposed.d.ts b/src/sql/azdata.proposed.d.ts index 09dab7e515..c957885590 100644 --- a/src/sql/azdata.proposed.d.ts +++ b/src/sql/azdata.proposed.d.ts @@ -8,4 +8,22 @@ import * as vscode from 'vscode'; declare module 'azdata' { + /** + * Namespace for connection management + */ + export namespace connection { + export type ConnectionEventType = + | 'onConnect' + | 'onDisconnect' + | 'onConnectionChanged'; + + export interface ConnectionEventListener { + onConnectionEvent(type: ConnectionEventType, ownerUri: string, args: IConnectionProfile): void; + } + + /** + * Register a connection event listener + */ + export function registerConnectionEventListener(listener: connection.ConnectionEventListener): void; + } } diff --git a/src/sql/workbench/api/browser/mainThreadConnectionManagement.ts b/src/sql/workbench/api/browser/mainThreadConnectionManagement.ts index 3c47655bf7..4e1e7d29dd 100644 --- a/src/sql/workbench/api/browser/mainThreadConnectionManagement.ts +++ b/src/sql/workbench/api/browser/mainThreadConnectionManagement.ts @@ -7,7 +7,7 @@ import { SqlExtHostContext, SqlMainContext, ExtHostConnectionManagementShape, Ma import * as azdata from 'azdata'; import { IExtHostContext } from 'vs/workbench/api/common/extHost.protocol'; import { extHostNamedCustomer } from 'vs/workbench/api/common/extHostCustomers'; -import { IConnectionManagementService, ConnectionType } from 'sql/platform/connection/common/connectionManagement'; +import { IConnectionManagementService, ConnectionType, IConnectionParams } from 'sql/platform/connection/common/connectionManagement'; import { IObjectExplorerService } from 'sql/workbench/services/objectExplorer/common/objectExplorerService'; import { IEditorService } from 'vs/workbench/services/editor/common/editorService'; import * as TaskUtilities from 'sql/workbench/browser/taskUtilities'; @@ -44,6 +44,45 @@ export class MainThreadConnectionManagement implements MainThreadConnectionManag this._toDispose = dispose(this._toDispose); } + public $registerConnectionEventListener(handle: number, providerId: string): void { + + let stripProfile = (inputProfile: azdata.IConnectionProfile) => { + if (!inputProfile) { + return inputProfile; + } + + let outputProfile: azdata.IConnectionProfile = { + connectionName: inputProfile.connectionName, + serverName: inputProfile.serverName, + databaseName: inputProfile.databaseName, + userName: inputProfile.userName, + password: inputProfile.password, + authenticationType: inputProfile.authenticationType, + savePassword: inputProfile.savePassword, + groupFullName: inputProfile.groupFullName, + groupId: inputProfile.groupId, + providerName: inputProfile.providerName, + saveProfile: inputProfile.saveProfile, + id: inputProfile.id, + azureTenantId: inputProfile.azureTenantId, + options: inputProfile.options + }; + return outputProfile; + }; + + this._connectionManagementService.onConnect((params: IConnectionParams) => { + this._proxy.$onConnectionEvent(handle, 'onConnect', params.connectionUri, stripProfile(params.connectionProfile)); + }); + + this._connectionManagementService.onConnectionChanged((params: IConnectionParams) => { + this._proxy.$onConnectionEvent(handle, 'onConnectionChanged', params.connectionUri, stripProfile(params.connectionProfile)); + }); + + this._connectionManagementService.onDisconnect((params: IConnectionParams) => { + this._proxy.$onConnectionEvent(handle, 'onDisconnect', params.connectionUri, stripProfile(params.connectionProfile)); + }); + } + public $getConnections(activeConnectionsOnly?: boolean): Thenable { return Promise.resolve(this._connectionManagementService.getConnections(activeConnectionsOnly).map(profile => this.convertToConnectionProfile(profile))); } diff --git a/src/sql/workbench/api/common/extHostConnectionManagement.ts b/src/sql/workbench/api/common/extHostConnectionManagement.ts index 55b04658de..708e6a0c03 100644 --- a/src/sql/workbench/api/common/extHostConnectionManagement.ts +++ b/src/sql/workbench/api/common/extHostConnectionManagement.ts @@ -10,6 +10,8 @@ import * as azdata from 'azdata'; export class ExtHostConnectionManagement extends ExtHostConnectionManagementShape { private _proxy: MainThreadConnectionManagementShape; + private _nextListenerHandle: number = 0; + private _connectionListeners = new Map(); constructor( mainContext: IMainContext @@ -22,6 +24,19 @@ export class ExtHostConnectionManagement extends ExtHostConnectionManagementShap return this._proxy.$getCurrentConnectionProfile(); } + public $onConnectionEvent(handle: number, type: azdata.connection.ConnectionEventType, ownerUri: string, profile: azdata.IConnectionProfile): void { + let listener = this._connectionListeners[handle]; + if (listener) { + listener.onConnectionEvent(type, ownerUri, profile); + } + } + + public $registerConnectionEventListener(providerId: string, listener: azdata.connection.ConnectionEventListener): void { + this._connectionListeners[this._nextListenerHandle] = listener; + this._proxy.$registerConnectionEventListener(this._nextListenerHandle, providerId); + this._nextListenerHandle++; + } + public $getConnections(activeConnectionsOnly?: boolean): Thenable { return this._proxy.$getConnections(activeConnectionsOnly); } diff --git a/src/sql/workbench/api/common/sqlExtHost.protocol.ts b/src/sql/workbench/api/common/sqlExtHost.protocol.ts index 674d690acf..9c41c3c5ec 100644 --- a/src/sql/workbench/api/common/sqlExtHost.protocol.ts +++ b/src/sql/workbench/api/common/sqlExtHost.protocol.ts @@ -38,7 +38,7 @@ export abstract class ExtHostAccountManagementShape { } export abstract class ExtHostConnectionManagementShape { - $onConnectionOpened(handleId: string, connection: azdata.connection.Connection): void { throw ni; } + $onConnectionEvent(handle: number, type: azdata.connection.ConnectionEventType, ownerUri: string, profile: azdata.IConnectionProfile): void { throw ni(); } } export abstract class ExtHostDataProtocolShape { @@ -606,6 +606,7 @@ export interface MainThreadDataProtocolShape extends IDisposable { } export interface MainThreadConnectionManagementShape extends IDisposable { + $registerConnectionEventListener(handle: number, providerId: string): void; $getConnections(activeConnectionsOnly?: boolean): Thenable; $getActiveConnections(): Thenable; $getCurrentConnection(): Thenable; diff --git a/src/sql/workbench/api/node/sqlExtHost.api.impl.ts b/src/sql/workbench/api/node/sqlExtHost.api.impl.ts index 6459e51135..36ba46efab 100644 --- a/src/sql/workbench/api/node/sqlExtHost.api.impl.ts +++ b/src/sql/workbench/api/node/sqlExtHost.api.impl.ts @@ -101,6 +101,9 @@ export function createApiFactory( getConnections(activeConnectionsOnly?: boolean): Thenable { return extHostConnectionManagement.$getConnections(activeConnectionsOnly); }, + registerConnectionEventListener(listener: azdata.connection.ConnectionEventListener): void { + return extHostConnectionManagement.$registerConnectionEventListener(mssqlProviderName, listener); + }, // "sqlops" back-compat APIs getActiveConnections(): Thenable {