src/vs/platform/policy/node/nativeManagedSettingsService.ts
141 LOC · 133 covered · 8 uncovered · 26 ranges · 12 concepts · 6 introducers · 7 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.
/*---------------------------------------------------------------------------------------------
nativeManagedSettingsIpc.ts ×7
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Throttler } from '../../../base/common/async.js';
import { IStringDictionary } from '../../../base/common/collections.js';
import { Emitter } from '../../../base/common/event.js';
import { Disposable, MutableDisposable } from '../../../base/common/lifecycle.js';
import { equals } from '../../../base/common/objects.js';
import { IManagedSettingsPolicyDefinitions, ManagedSettingsData } from '../../../base/common/policy.js';
import { ILogService } from '../../log/common/log.js';
import { collectManagedSettingsDefinitions, INativeManagedSettingsService } from '../common/copilotManagedSettings.js';
import { PolicyDefinition, PolicyValue } from '../common/policy.js';
import type { Watcher } from '@vscode/policy-watcher';
export interface INativePolicyWatcherOptions {
readonly registryPath?: string;
}
export type NativePolicyWatcherFactory = (
productName: string,
policies: Record<string, { type: 'string' | 'number' | 'boolean' }>,
onDidChange: (update: Record<string, PolicyValue | undefined>) => void,
options?: INativePolicyWatcherOptions,
) => Watcher;
export class NativeManagedSettingsService extends Disposable implements INativeManagedSettingsService {
readonly _serviceBrand: undefined;
private readonly throttler = this._register(new Throttler());
private readonly watcher = this._register(new MutableDisposable<Watcher>());
private readonly managedSettingsValues = new Map<string, PolicyValue>();
private watchedSettings: IManagedSettingsPolicyDefinitions = {};
private readonly _onDidChangeManagedSettings = this._register(new Emitter<ManagedSettingsData>());
readonly onDidChangeManagedSettings = this._onDidChangeManagedSettings.event;
get managedSettings(): ManagedSettingsData {
return Object.fromEntries(this.managedSettingsValues);
}
constructor(
private readonly productName: string,
private readonly watcherOptions?: INativePolicyWatcherOptions,
private readonly watcherFactory?: NativePolicyWatcherFactory,
) {
super();
}
async updatePolicyDefinitions(policyDefinitions: IStringDictionary<PolicyDefinition>): Promise<ManagedSettingsData> {
const managedSettings = collectManagedSettingsDefinitions(policyDefinitions);
nativeManagedSettingsService.ts ×3
if (equals(this.watchedSettings, managedSettings)) {
}
this.watchedSettings = managedSettings;
const changed = this.pruneManagedSettingsValues();
await this.updateWatcher();
if (changed) {
this._onDidChangeManagedSettings.fire(this.managedSettings);
nativeManagedSettingsService.ts ×2
}
private pruneManagedSettingsValues(): boolean {
for (const key of this.managedSettingsValues.keys()) {
this.managedSettingsValues.delete(key);
changed = true;
}
}
}
private async updateWatcher(): Promise<void> {
const managedSettingDefinitions = this.getManagedSettingDefinitions();
nativeManagedSettingsService.ts ×11
this.logService.trace(`NativeManagedSettingsService#updateWatcher - Found ${Object.keys(managedSettingDefinitions).length} managed-settings definitions`);
if (Object.keys(managedSettingDefinitions).length === 0) {
const hadManagedSettings = this.managedSettingsValues.size > 0;
this.managedSettingsValues.clear();
if (hadManagedSettings) {
this._onDidChangeManagedSettings.fire(this.managedSettings);
}
}
const { createWatcher } = this.watcherFactory ? { createWatcher: this.watcherFactory } : (await import('@vscode/policy-watcher') as { createWatcher: NativePolicyWatcherFactory });
await this.throttler.queue(() => new Promise<void>((c, e) => {
this.logService.trace(`Creating native managed-settings watcher for productName ${this.productName}`);
this.watcher.value = createWatcher(this.productName, managedSettingDefinitions, update => {
this._onDidManagedSettingsChange(update as Record<string, PolicyValue | undefined>);
c();
}, this.watcherOptions);
} catch (err) {
this.logService.error(`NativeManagedSettingsService#updateWatcher - Error creating watcher:`, err);
e(err);
}
}));
/**
* Project the internal {@link IManagedSettingsPolicyDefinitions} (readonly, and free to grow
* extra fields) down to the minimal `{ type }` payload the external `@vscode/policy-watcher`
* native module expects. Deliberately a fresh, narrowly-typed copy rather than handing the
* watcher our internal state: it decouples the two shapes so a future field on
* `IManagedSettingPolicyDefinition` cannot silently leak across the native boundary.
*/
private getManagedSettingDefinitions(): Record<string, { type: 'string' | 'number' | 'boolean' }> {
const definitions: Record<string, { type: 'string' | 'number' | 'boolean' }> = {};
nativeManagedSettingsService.ts ×11
for (const key in this.watchedSettings) {
definitions[key] = { type: this.watchedSettings[key].type };
}
return definitions;
}
private _onDidManagedSettingsChange(update: Record<string, PolicyValue | undefined>): void {
this.logService.trace(`NativeManagedSettingsService#_onDidManagedSettingsChange - Updated managed-settings values: ${JSON.stringify(update)}`);
nativeManagedSettingsService.ts ×11
let changed = false;
for (const [key, value] of Object.entries(update)) {
if (value === undefined) {
changed = this.managedSettingsValues.delete(key) || changed;
if (this.managedSettingsValues.get(key) !== value) {
this.managedSettingsValues.set(key, value);
changed = true;
}
}
}
if (changed) {
this._onDidChangeManagedSettings.fire(this.managedSettings);
}
}