src/vs/platform/policy/common/fileManagedSettingsService.ts
92 LOC · 92 covered · 0 uncovered · 18 ranges · 28 concepts · 9 introducers · 16 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.
/*---------------------------------------------------------------------------------------------
fileManagedSettingsIpc.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 { ThrottledDelayer } from '../../../base/common/async.js';
import { Emitter, Event } from '../../../base/common/event.js';
import { Disposable } from '../../../base/common/lifecycle.js';
import { equals } from '../../../base/common/objects.js';
import { ManagedSettingsData } from '../../../base/common/policy.js';
import { isObject } from '../../../base/common/types.js';
import { URI } from '../../../base/common/uri.js';
import { FileOperationError, FileOperationResult, IFileService } from '../../files/common/files.js';
import { ILogService } from '../../log/common/log.js';
import { IFileManagedSettingsService, normalizeManagedSettings, RawManagedSettingsData } from './copilotManagedSettings.js';
/**
* Upper bound on the size of `managed-settings.json` we will read into the main process. A
* legitimate settings file is a few KB; capping the read keeps a malformed or hostile file on the
* (externally writable, privileged) well-known path from OOM-ing the main process. `readFile`
* throws `FILE_TOO_LARGE` past this, which `refresh()` treats like any other read failure.
*/
const MANAGED_SETTINGS_MAX_FILE_SIZE = 1024 * 1024; // 1 MB
export class FileManagedSettingsService extends Disposable implements IFileManagedSettingsService {
readonly _serviceBrand: undefined;
private _rawManagedSettings: RawManagedSettingsData = {};
get rawManagedSettings(): RawManagedSettingsData { return this._rawManagedSettings; }
private _managedSettings: ManagedSettingsData = {};
get managedSettings(): ManagedSettingsData { return this._managedSettings; }
private readonly _onDidChangeRawManagedSettings = this._register(new Emitter<RawManagedSettingsData>());
readonly onDidChangeRawManagedSettings = this._onDidChangeRawManagedSettings.event;
private readonly _onDidChangeManagedSettings = this._register(new Emitter<ManagedSettingsData>());
readonly onDidChangeManagedSettings = this._onDidChangeManagedSettings.event;
private readonly throttledDelayer = this._register(new ThrottledDelayer(500));
constructor(
@IFileService private readonly fileService: IFileService,
@ILogService private readonly logService: ILogService,
) {
super();
const onDidChangeFile = Event.filter(fileService.onDidFilesChange, e => e.affects(file));
this._register(fileService.watch(file));
this._register(onDidChangeFile(() => this.throttledDelayer.trigger(() => this.refresh())));
// Initial read — routed through the same delayer (with no delay) so it is serialized
// against change-triggered refreshes and can't be clobbered by a racing read. Non-blocking;
// IPC clients handle eventual data arrival.
this.throttledDelayer.trigger(() => this.refresh(), 0);
}
private async refresh(): Promise<void> {
const previous = this._managedSettings;
try {
const content = await this.fileService.readFile(this.file, { limits: { size: MANAGED_SETTINGS_MAX_FILE_SIZE } });
if (isObject(parsed)) {
this._managedSettings = normalizeManagedSettings(parsed as Record<string, unknown>,
msg => this.logService.warn(`[FileManagedSettingsService] ${msg}`));
this.logService.warn('[FileManagedSettingsService] managed-settings.json is not a JSON object');
fileManagedSettingsService.ts ×1
this._rawManagedSettings = {};
this._managedSettings = {};
}
if ((<FileOperationError>error).fileOperationResult !== FileOperationResult.FILE_NOT_FOUND) {
fileManagedSettingsService.ts ×2
this.logService.error('[FileManagedSettingsService] Failed to read managed-settings.json', error);
fileManagedSettingsService.ts ×1
}
this._managedSettings = {};
}
if (!equals(previousRaw, this._rawManagedSettings)) {
this._onDidChangeRawManagedSettings.fire(this._rawManagedSettings);
fileManagedSettingsService.ts ×2
}
}