Path: blob/main/src/vs/sessions/contrib/policyBlocked/browser/policyBlocked.contribution.ts
13401 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { Disposable, MutableDisposable } from '../../../../base/common/lifecycle.js';6import { IWorkbenchContribution, registerWorkbenchContribution2, WorkbenchPhase } from '../../../../workbench/common/contributions.js';7import { IWorkbenchLayoutService } from '../../../../workbench/services/layout/browser/layoutService.js';8import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';9import { IConfigurationService } from '../../../../platform/configuration/common/configuration.js';10import { IDefaultAccountService } from '../../../../platform/defaultAccount/common/defaultAccount.js';11import { ChatConfiguration } from '../../../../workbench/contrib/chat/common/constants.js';12import { ISessionsBlockedOverlayOptions, SessionsBlockedReason, SessionsPolicyBlockedOverlay } from './sessionsPolicyBlocked.js';13import { AccountPolicyGateState, AccountPolicyGateUnsatisfiedReason, IAccountPolicyGateService } from '../../../../workbench/services/policies/common/accountPolicyService.js';1415export class SessionsPolicyBlockedContribution extends Disposable implements IWorkbenchContribution {1617static readonly ID = 'workbench.contrib.sessionsPolicyBlocked';1819private readonly overlayRef = this._register(new MutableDisposable());20private currentReason: SessionsBlockedReason | undefined;2122constructor(23@IConfigurationService private readonly configurationService: IConfigurationService,24@IWorkbenchLayoutService private readonly layoutService: IWorkbenchLayoutService,25@IInstantiationService private readonly instantiationService: IInstantiationService,26@IAccountPolicyGateService private readonly gateService: IAccountPolicyGateService,27@IDefaultAccountService private readonly defaultAccountService: IDefaultAccountService,28) {29super();3031this.update();3233this._register(this.configurationService.onDidChangeConfiguration(e => {34if (e.affectsConfiguration(ChatConfiguration.AgentEnabled)) {35this.update();36}37}));3839this._register(this.gateService.onDidChangeGateInfo(() => this.update()));40}4142private update(): void {43const gateInfo = this.gateService.gateInfo;4445// The gate forces chat.agent.enabled = false via restrictedValue when stably46// Restricted. Suppress AgentDisabled in that case so users see the gate-specific47// overlay (or the welcome screen for noAccount/wrongProvider) instead.48const gateForcesAgentDisabled = gateInfo.state === AccountPolicyGateState.Restricted49&& gateInfo.reason !== AccountPolicyGateUnsatisfiedReason.PolicyNotResolved;5051const agentEnabled = this.configurationService.getValue<boolean>(ChatConfiguration.AgentEnabled);52if (agentEnabled === false && !gateForcesAgentDisabled) {53this.showOverlay({ reason: SessionsBlockedReason.AgentDisabled });54return;55}5657if (gateInfo.state === AccountPolicyGateState.Restricted) {58// Defer to the sessions welcome/walkthrough so the user signs in via the standard flow.59if (gateInfo.reason === AccountPolicyGateUnsatisfiedReason.NoAccount60|| gateInfo.reason === AccountPolicyGateUnsatisfiedReason.WrongProvider) {61this.overlayRef.clear();62this.currentReason = undefined;63return;64}6566if (gateInfo.reason === AccountPolicyGateUnsatisfiedReason.PolicyNotResolved) {67this.showOverlay({ reason: SessionsBlockedReason.Loading });68} else {69const accountName = this.defaultAccountService.currentDefaultAccount?.accountName;70this.showOverlay({71reason: SessionsBlockedReason.AccountPolicyGate,72approvedOrganizations: gateInfo.approvedOrganizations,73accountName,74});75}76return;77}7879this.overlayRef.clear();80this.currentReason = undefined;81}8283private showOverlay(options: ISessionsBlockedOverlayOptions): void {84// AccountPolicyGate may need re-render when the account name changes.85if (this.currentReason === options.reason && options.reason !== SessionsBlockedReason.AccountPolicyGate) {86return;87}88this.overlayRef.clear();89this.currentReason = options.reason;9091this.overlayRef.value = this.instantiationService.createInstance(92SessionsPolicyBlockedOverlay,93this.layoutService.mainContainer,94options,95);96}97}9899registerWorkbenchContribution2(SessionsPolicyBlockedContribution.ID, SessionsPolicyBlockedContribution, WorkbenchPhase.BlockRestore);100101102