src/vs/platform/policy/common/policy.ts
99 LOC · 90 covered · 9 uncovered · 14 ranges · 3213 concepts · 5 introducers · 1619 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
policy.ts ×8
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { IStringDictionary } from '../../../base/common/collections.js';
import { IPolicyData } from '../../../base/common/defaultAccount.js';
import { Emitter, Event } from '../../../base/common/event.js';
import { Iterable } from '../../../base/common/iterator.js';
import { Disposable } from '../../../base/common/lifecycle.js';
import { IManagedSettingsPolicyDefinitions, PolicyName } from '../../../base/common/policy.js';
import { createDecorator } from '../../instantiation/common/instantiation.js';
export type PolicyValue = string | number | boolean;
export type PolicyDefinition = {
type: 'string' | 'number' | 'boolean';
value?: (policyData: IPolicyData) => string | number | boolean | undefined;
managedSettings?: IManagedSettingsPolicyDefinitions;
restrictedValue?: PolicyValue;
};
/** Returns a structured-clone-safe copy of `definition`, dropping the non-cloneable `value` callback. */
export function toSerializablePolicyDefinition(definition: PolicyDefinition): PolicyDefinition {
return { type: definition.type, managedSettings: definition.managedSettings, restrictedValue: definition.restrictedValue };
policy.ts ×2
}
/**
* Returns the value to apply for `definition` when the account-policy gate is active
* but not satisfied. Uses `definition.restrictedValue` when specified, otherwise falls
* back to a type-driven safe default.
*/
export function getRestrictedPolicyValue(definition: PolicyDefinition): PolicyValue {
if (definition.restrictedValue !== undefined) {
return definition.restrictedValue;
}
switch (definition.type) {
case 'boolean': return false;
case 'number': return 0;
case 'string': return '';
}
}
export const IPolicyService = createDecorator<IPolicyService>('policy');
export interface IPolicyService {
readonly _serviceBrand: undefined;
readonly onDidChange: Event<readonly PolicyName[]>;
updatePolicyDefinitions(policyDefinitions: IStringDictionary<PolicyDefinition>): Promise<IStringDictionary<PolicyValue>>;
getPolicyValue(name: PolicyName): PolicyValue | undefined;
serialize(): IStringDictionary<{ definition: PolicyDefinition; value: PolicyValue }> | undefined;
readonly policyDefinitions: IStringDictionary<PolicyDefinition>;
}
export abstract class AbstractPolicyService extends Disposable implements IPolicyService {
public policyDefinitions: IStringDictionary<PolicyDefinition> = {};
protected policies = new Map<PolicyName, PolicyValue>();
protected readonly _onDidChange = this._register(new Emitter<readonly PolicyName[]>());
readonly onDidChange = this._onDidChange.event;
async updatePolicyDefinitions(policyDefinitions: IStringDictionary<PolicyDefinition>): Promise<IStringDictionary<PolicyValue>> {
// Replace existing definitions; identity comparison avoids redundant watcher churn.
policy.ts ×2
let changed = false;
for (const name of Object.keys(policyDefinitions)) {
if (this.policyDefinitions[name] !== policyDefinitions[name]) {
this.policyDefinitions[name] = policyDefinitions[name];
changed = true;
}
}
if (changed) {
await this._updatePolicyDefinitions(this.policyDefinitions);
}
return Iterable.reduce(this.policies.entries(), (r, [name, value]) => ({ ...r, [name]: value }), {});
}
getPolicyValue(name: PolicyName): PolicyValue | undefined {
}
serialize(): IStringDictionary<{ definition: PolicyDefinition; value: PolicyValue }> {
return Iterable.reduce<[PolicyName, PolicyDefinition], IStringDictionary<{ definition: PolicyDefinition; value: PolicyValue }>>(Object.entries(this.policyDefinitions), (r, [name, definition]) => ({ ...r, [name]: { definition: toSerializablePolicyDefinition(definition), value: this.policies.get(name)! } }), {});
policy.ts ×2
}
protected abstract _updatePolicyDefinitions(policyDefinitions: IStringDictionary<PolicyDefinition>): Promise<void>;
}
export class NullPolicyService implements IPolicyService {
readonly onDidChange = Event.None;
async updatePolicyDefinitions() { return {}; }
getPolicyValue() { return undefined; }
serialize() { return undefined; }
policyDefinitions: IStringDictionary<PolicyDefinition> = {};