From 2e89c2d4ba5659c77ccde02605b87181658f8137 Mon Sep 17 00:00:00 2001 From: Rachel Macfarlane Date: Fri, 15 Jan 2021 17:08:09 -0800 Subject: [PATCH] Add 'key' to onDidChange of secrets API, #112249 --- src/vs/platform/native/common/native.ts | 2 +- .../native/electron-main/nativeHostMainService.ts | 6 +++--- src/vs/vscode.proposed.d.ts | 12 +++++++++++- .../workbench/api/browser/mainThreadSecretState.ts | 5 +++-- src/vs/workbench/api/common/exHostSecretState.ts | 10 +++++----- src/vs/workbench/api/common/extHost.protocol.ts | 2 +- src/vs/workbench/api/common/extHostSecrets.ts | 10 +++++++--- .../credentials/browser/credentialsService.ts | 8 ++++---- .../services/credentials/common/credentials.ts | 7 ++++++- .../electron-sandbox/credentialsService.ts | 4 ++-- 10 files changed, 43 insertions(+), 23 deletions(-) diff --git a/src/vs/platform/native/common/native.ts b/src/vs/platform/native/common/native.ts index 60baf2da4ef..e51b3ac0e22 100644 --- a/src/vs/platform/native/common/native.ts +++ b/src/vs/platform/native/common/native.ts @@ -49,7 +49,7 @@ export interface ICommonNativeHostService { readonly onDidChangeColorScheme: Event; - readonly onDidChangePassword: Event; + readonly onDidChangePassword: Event<{ service: string, account: string }>; // Window getWindows(): Promise; diff --git a/src/vs/platform/native/electron-main/nativeHostMainService.ts b/src/vs/platform/native/electron-main/nativeHostMainService.ts index 2ce235e5328..58318253150 100644 --- a/src/vs/platform/native/electron-main/nativeHostMainService.ts +++ b/src/vs/platform/native/electron-main/nativeHostMainService.ts @@ -90,7 +90,7 @@ export class NativeHostMainService extends Disposable implements INativeHostMain private readonly _onDidChangeColorScheme = this._register(new Emitter()); readonly onDidChangeColorScheme = this._onDidChangeColorScheme.event; - private readonly _onDidChangePassword = this._register(new Emitter()); + private readonly _onDidChangePassword = this._register(new Emitter<{ account: string, service: string }>()); readonly onDidChangePassword = this._onDidChangePassword.event; //#endregion @@ -705,7 +705,7 @@ export class NativeHostMainService extends Disposable implements INativeHostMain await keytar.setPassword(service, account, password); } - this._onDidChangePassword.fire(); + this._onDidChangePassword.fire({ service, account }); } async deletePassword(windowId: number | undefined, service: string, account: string): Promise { @@ -713,7 +713,7 @@ export class NativeHostMainService extends Disposable implements INativeHostMain const didDelete = await keytar.deletePassword(service, account); if (didDelete) { - this._onDidChangePassword.fire(); + this._onDidChangePassword.fire({ service, account }); } return didDelete; diff --git a/src/vs/vscode.proposed.d.ts b/src/vs/vscode.proposed.d.ts index 95f1ede0e40..0432e5972a2 100644 --- a/src/vs/vscode.proposed.d.ts +++ b/src/vs/vscode.proposed.d.ts @@ -2424,6 +2424,16 @@ declare module 'vscode' { //#region https://github.com/microsoft/vscode/issues/112249 + /** + * The event data that is fired when a secret is added or removed. + */ + export interface SecretStorageChangeEvent { + /** + * The key of the secret that has changed. + */ + key: string; + } + /** * Represents a storage utility for secrets, information that is * sensitive. @@ -2453,7 +2463,7 @@ declare module 'vscode' { /** * Fires when a secret is set or deleted. */ - onDidChange: Event; + onDidChange: Event; } export interface ExtensionContext { diff --git a/src/vs/workbench/api/browser/mainThreadSecretState.ts b/src/vs/workbench/api/browser/mainThreadSecretState.ts index 92805a0a12a..4067acedb8b 100644 --- a/src/vs/workbench/api/browser/mainThreadSecretState.ts +++ b/src/vs/workbench/api/browser/mainThreadSecretState.ts @@ -23,8 +23,9 @@ export class MainThreadSecretState extends Disposable implements MainThreadSecre super(); this._proxy = extHostContext.getProxy(ExtHostContext.ExtHostSecretState); - this._register(this.credentialsService.onDidChangePassword(_ => { - this._proxy.$onDidChangePassword(); + this._register(this.credentialsService.onDidChangePassword(e => { + const extensionId = e.service.substring(this.productService.urlProtocol.length); + this._proxy.$onDidChangePassword({ extensionId, key: e.account }); })); } diff --git a/src/vs/workbench/api/common/exHostSecretState.ts b/src/vs/workbench/api/common/exHostSecretState.ts index 35b5d57a61f..2715b881f75 100644 --- a/src/vs/workbench/api/common/exHostSecretState.ts +++ b/src/vs/workbench/api/common/exHostSecretState.ts @@ -4,21 +4,21 @@ *--------------------------------------------------------------------------------------------*/ import { ExtHostSecretStateShape, MainContext, MainThreadSecretStateShape } from 'vs/workbench/api/common/extHost.protocol'; -import { Emitter, Event } from 'vs/base/common/event'; +import { Emitter } from 'vs/base/common/event'; import { IExtHostRpcService } from 'vs/workbench/api/common/extHostRpcService'; import { createDecorator } from 'vs/platform/instantiation/common/instantiation'; export class ExtHostSecretState implements ExtHostSecretStateShape { private _proxy: MainThreadSecretStateShape; - private _onDidChangePassword = new Emitter(); - readonly onDidChangePassword: Event = this._onDidChangePassword.event; + private _onDidChangePassword = new Emitter<{ extensionId: string, key: string }>(); + readonly onDidChangePassword = this._onDidChangePassword.event; constructor(mainContext: IExtHostRpcService) { this._proxy = mainContext.getProxy(MainContext.MainThreadSecretState); } - async $onDidChangePassword(): Promise { - this._onDidChangePassword.fire(); + async $onDidChangePassword(e: { extensionId: string, key: string }): Promise { + this._onDidChangePassword.fire(e); } get(extensionId: string, key: string): Promise { diff --git a/src/vs/workbench/api/common/extHost.protocol.ts b/src/vs/workbench/api/common/extHost.protocol.ts index b2a4e235b00..4320674124c 100644 --- a/src/vs/workbench/api/common/extHost.protocol.ts +++ b/src/vs/workbench/api/common/extHost.protocol.ts @@ -1130,7 +1130,7 @@ export interface ExtHostAuthenticationShape { } export interface ExtHostSecretStateShape { - $onDidChangePassword(): Promise; + $onDidChangePassword(e: { extensionId: string, key: string }): Promise; } export interface ExtHostSearchShape { diff --git a/src/vs/workbench/api/common/extHostSecrets.ts b/src/vs/workbench/api/common/extHostSecrets.ts index d67ec72bcf3..837b36f514f 100644 --- a/src/vs/workbench/api/common/extHostSecrets.ts +++ b/src/vs/workbench/api/common/extHostSecrets.ts @@ -14,15 +14,19 @@ export class ExtensionSecrets implements vscode.SecretStorage { protected readonly _id: string; protected readonly _secretState: ExtHostSecretState; - private _onDidChange = new Emitter(); - readonly onDidChange: Event = this._onDidChange.event; + private _onDidChange = new Emitter(); + readonly onDidChange: Event = this._onDidChange.event; constructor(extensionDescription: IExtensionDescription, secretState: ExtHostSecretState) { this._id = ExtensionIdentifier.toKey(extensionDescription.identifier); this._secretState = secretState; - this._secretState.onDidChangePassword(_ => this._onDidChange.fire()); + this._secretState.onDidChangePassword(e => { + if (e.extensionId === this._id) { + this._onDidChange.fire({ key: e.key }); + } + }); } get(key: string): Promise { diff --git a/src/vs/workbench/services/credentials/browser/credentialsService.ts b/src/vs/workbench/services/credentials/browser/credentialsService.ts index a0d03ec8417..8dd3d74ee49 100644 --- a/src/vs/workbench/services/credentials/browser/credentialsService.ts +++ b/src/vs/workbench/services/credentials/browser/credentialsService.ts @@ -3,7 +3,7 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { ICredentialsService, ICredentialsProvider } from 'vs/workbench/services/credentials/common/credentials'; +import { ICredentialsService, ICredentialsProvider, ICredentialsChangeEvent } from 'vs/workbench/services/credentials/common/credentials'; import { registerSingleton } from 'vs/platform/instantiation/common/extensions'; import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService'; import { Emitter } from 'vs/base/common/event'; @@ -13,7 +13,7 @@ export class BrowserCredentialsService extends Disposable implements ICredential declare readonly _serviceBrand: undefined; - private _onDidChangePassword = this._register(new Emitter()); + private _onDidChangePassword = this._register(new Emitter()); readonly onDidChangePassword = this._onDidChangePassword.event; private credentialsProvider: ICredentialsProvider; @@ -35,13 +35,13 @@ export class BrowserCredentialsService extends Disposable implements ICredential async setPassword(service: string, account: string, password: string): Promise { await this.credentialsProvider.setPassword(service, account, password); - this._onDidChangePassword.fire(); + this._onDidChangePassword.fire({ service, account }); } deletePassword(service: string, account: string): Promise { const didDelete = this.credentialsProvider.deletePassword(service, account); if (didDelete) { - this._onDidChangePassword.fire(); + this._onDidChangePassword.fire({ service, account }); } return didDelete; diff --git a/src/vs/workbench/services/credentials/common/credentials.ts b/src/vs/workbench/services/credentials/common/credentials.ts index b1280cd463a..73b10f0c971 100644 --- a/src/vs/workbench/services/credentials/common/credentials.ts +++ b/src/vs/workbench/services/credentials/common/credentials.ts @@ -16,7 +16,12 @@ export interface ICredentialsProvider { findCredentials(service: string): Promise>; } +export interface ICredentialsChangeEvent { + service: string + account: string; +} + export interface ICredentialsService extends ICredentialsProvider { readonly _serviceBrand: undefined; - readonly onDidChangePassword: Event; + readonly onDidChangePassword: Event; } diff --git a/src/vs/workbench/services/credentials/electron-sandbox/credentialsService.ts b/src/vs/workbench/services/credentials/electron-sandbox/credentialsService.ts index 18ad290ad9e..ba59fbff0c5 100644 --- a/src/vs/workbench/services/credentials/electron-sandbox/credentialsService.ts +++ b/src/vs/workbench/services/credentials/electron-sandbox/credentialsService.ts @@ -3,7 +3,7 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { ICredentialsService } from 'vs/workbench/services/credentials/common/credentials'; +import { ICredentialsChangeEvent, ICredentialsService } from 'vs/workbench/services/credentials/common/credentials'; import { INativeHostService } from 'vs/platform/native/electron-sandbox/native'; import { registerSingleton } from 'vs/platform/instantiation/common/extensions'; import { Emitter } from 'vs/base/common/event'; @@ -13,7 +13,7 @@ export class KeytarCredentialsService extends Disposable implements ICredentials declare readonly _serviceBrand: undefined; - private _onDidChangePassword: Emitter = this._register(new Emitter()); + private _onDidChangePassword: Emitter = this._register(new Emitter()); readonly onDidChangePassword = this._onDidChangePassword.event; constructor(@INativeHostService private readonly nativeHostService: INativeHostService) { -- GitLab