@@ -17,22 +17,6 @@ import { CommandsRegistry } from 'vs/platform/commands/common/commands';
1717import { IQuickInputService } from 'vs/platform/quickinput/common/quickInput' ;
1818import { INotificationService } from 'vs/platform/notification/common/notification' ;
1919
20- interface AuthDependent {
21- providerId : string ;
22- label : string ;
23- scopes : string [ ] ;
24- scopeDescriptions ?: string ;
25- }
26-
27- const BUILT_IN_AUTH_DEPENDENTS : AuthDependent [ ] = [
28- {
29- providerId : 'microsoft' ,
30- label : 'Settings sync' ,
31- scopes : [ 'https://management.core.windows.net/.default' , 'offline_access' ] ,
32- scopeDescriptions : 'Read user email'
33- }
34- ] ;
35-
3620interface AllowedExtension {
3721 id : string ;
3822 name : string ;
@@ -74,12 +58,13 @@ export class MainThreadAuthenticationProvider extends Disposable {
7458 private _accounts = new Map < string , string [ ] > ( ) ; // Map account name to session ids
7559 private _sessions = new Map < string , string > ( ) ; // Map account id to name
7660 private _signInMenuItem : IMenuItem | undefined ;
61+ private _signInMenuDisposables : IDisposable [ ] = [ ] ;
7762
7863 constructor (
7964 private readonly _proxy : ExtHostAuthenticationShape ,
8065 public readonly id : string ,
8166 public readonly displayName : string ,
82- public readonly dependents : AuthDependent [ ] ,
67+ private readonly supportsMultipleAccounts : boolean ,
8368 private readonly notificationService : INotificationService
8469 ) {
8570 super ( ) ;
@@ -135,29 +120,33 @@ export class MainThreadAuthenticationProvider extends Disposable {
135120 quickPick . show ( ) ;
136121 }
137122
138- private async registerCommandsAndContextMenuItems ( ) : Promise < void > {
139- const sessions = await this . _proxy . $getSessions ( this . id ) ;
123+ private createSignInMenu ( hasSessions : boolean ) : void {
124+ this . _signInMenuDisposables . push ( CommandsRegistry . registerCommand ( {
125+ id : `signIn${ this . id } ` ,
126+ handler : ( accessor , args ) => {
127+ this . login ( ) ;
128+ } ,
129+ } ) ) ;
140130
141- if ( this . dependents . length ) {
142- this . _register ( CommandsRegistry . registerCommand ( {
131+ this . _signInMenuItem = {
132+ group : '2_providers' ,
133+ command : {
143134 id : `signIn${ this . id } ` ,
144- handler : ( accessor , args ) => {
145- this . login ( this . dependents . reduce ( ( previous : string [ ] , current ) => previous . concat ( current . scopes ) , [ ] ) ) ;
146- } ,
147- } ) ) ;
148-
149- this . _signInMenuItem = {
150- group : '2_providers' ,
151- command : {
152- id : `signIn${ this . id } ` ,
153- title : sessions . length
154- ? nls . localize ( 'addAnotherAccount' , "Sign in to another {0} account" , this . displayName )
155- : nls . localize ( 'addAccount' , "Sign in to {0}" , this . displayName )
156- } ,
157- order : 3
158- } ;
135+ title : hasSessions
136+ ? nls . localize ( 'addAnotherAccount' , "Sign in to another {0} account" , this . displayName )
137+ : nls . localize ( 'addAccount' , "Sign in to {0}" , this . displayName )
138+ } ,
139+ order : 3
140+ } ;
159141
160- this . _register ( MenuRegistry . appendMenuItem ( MenuId . AccountsContext , this . _signInMenuItem ) ) ;
142+ this . _signInMenuDisposables . push ( MenuRegistry . appendMenuItem ( MenuId . AccountsContext , this . _signInMenuItem ) ) ;
143+ }
144+
145+ private async registerCommandsAndContextMenuItems ( ) : Promise < void > {
146+ const sessions = await this . _proxy . $getSessions ( this . id ) ;
147+
148+ if ( ! sessions . length || ( sessions . length && this . supportsMultipleAccounts ) ) {
149+ this . createSignInMenu ( ! ! sessions . length ) ;
161150 }
162151
163152 sessions . forEach ( session => this . registerSession ( session ) ) ;
@@ -261,6 +250,8 @@ export class MainThreadAuthenticationProvider extends Disposable {
261250
262251 if ( this . _signInMenuItem ) {
263252 this . _signInMenuItem . command . title = nls . localize ( 'addAccount' , "Sign in to {0}" , this . displayName ) ;
253+ } else {
254+ this . createSignInMenu ( false ) ;
264255 }
265256 }
266257 }
@@ -269,11 +260,16 @@ export class MainThreadAuthenticationProvider extends Disposable {
269260 addedSessions . forEach ( session => this . registerSession ( session ) ) ;
270261
271262 if ( addedSessions . length && this . _signInMenuItem ) {
272- this . _signInMenuItem . command . title = nls . localize ( 'addAnotherAccount' , "Sign in to another {0} account" , this . displayName ) ;
263+ if ( this . supportsMultipleAccounts ) {
264+ this . _signInMenuItem . command . title = nls . localize ( 'addAnotherAccount' , "Sign in to another {0} account" , this . displayName ) ;
265+ } else {
266+ this . _signInMenuDisposables . forEach ( item => item . dispose ( ) ) ;
267+ this . _signInMenuItem = undefined ;
268+ }
273269 }
274270 }
275271
276- login ( scopes : string [ ] ) : Promise < modes . AuthenticationSession > {
272+ login ( scopes ? : string [ ] ) : Promise < modes . AuthenticationSession > {
277273 return this . _proxy . $login ( this . id , scopes ) . then ( session => {
278274 return {
279275 id : session . id ,
@@ -292,6 +288,7 @@ export class MainThreadAuthenticationProvider extends Disposable {
292288 super . dispose ( ) ;
293289 this . _sessionMenuItems . forEach ( item => item . forEach ( d => d . dispose ( ) ) ) ;
294290 this . _sessionMenuItems . clear ( ) ;
291+ this . _signInMenuDisposables . forEach ( item => item . dispose ( ) ) ;
295292 }
296293}
297294
@@ -310,10 +307,8 @@ export class MainThreadAuthentication extends Disposable implements MainThreadAu
310307 this . _proxy = extHostContext . getProxy ( ExtHostContext . ExtHostAuthentication ) ;
311308 }
312309
313- async $registerAuthenticationProvider ( id : string , displayName : string ) : Promise < void > {
314- const dependentBuiltIns = BUILT_IN_AUTH_DEPENDENTS . filter ( dependency => dependency . providerId === id ) ;
315-
316- const provider = new MainThreadAuthenticationProvider ( this . _proxy , id , displayName , dependentBuiltIns , this . notificationService ) ;
310+ async $registerAuthenticationProvider ( id : string , displayName : string , supportsMultipleAccounts : boolean ) : Promise < void > {
311+ const provider = new MainThreadAuthenticationProvider ( this . _proxy , id , displayName , supportsMultipleAccounts , this . notificationService ) ;
317312 this . authenticationService . registerAuthenticationProvider ( id , provider ) ;
318313 }
319314
0 commit comments