From 56eca91b26ff4e25f548aae1969990cfa6716544 Mon Sep 17 00:00:00 2001 From: Joao Moreno Date: Wed, 18 May 2022 14:13:58 +0200 Subject: [PATCH] add vscode-policy-watcher dependency, support registerPolicyDefinitions --- package.json | 2 +- src/vs/code/electron-main/main.ts | 4 +- src/vs/code/node/cliProcessMain.ts | 6 +- .../policy/node/nativePolicyService.ts | 68 ++++++++++++++++++ .../policy/node/windowsPolicyService.ts | 70 ------------------- yarn.lock | 8 +-- 6 files changed, 79 insertions(+), 79 deletions(-) create mode 100644 src/vs/platform/policy/node/nativePolicyService.ts delete mode 100644 src/vs/platform/policy/node/windowsPolicyService.ts diff --git a/package.json b/package.json index ed114a6ceac..412567abe45 100644 --- a/package.json +++ b/package.json @@ -81,6 +81,7 @@ "tas-client-umd": "0.1.5", "v8-inspect-profiler": "^0.1.0", "vscode-oniguruma": "1.6.1", + "vscode-policy-watcher": "^1.1.0", "vscode-proxy-agent": "^0.12.0", "vscode-regexpp": "^3.1.0", "vscode-textmate": "7.0.1", @@ -221,7 +222,6 @@ }, "optionalDependencies": { "@vscode/windows-registry": "1.0.6", - "vscode-policy-watcher": "^1.0.0", "windows-foreground-love": "0.4.0", "windows-mutex": "0.4.1", "windows-process-tree": "0.3.3" diff --git a/src/vs/code/electron-main/main.ts b/src/vs/code/electron-main/main.ts index d1563aebe63..cb41f40555e 100644 --- a/src/vs/code/electron-main/main.ts +++ b/src/vs/code/electron-main/main.ts @@ -63,7 +63,7 @@ import { StateMainService } from 'vs/platform/state/electron-main/stateMainServi import { NullTelemetryService } from 'vs/platform/telemetry/common/telemetryUtils'; import { IThemeMainService, ThemeMainService } from 'vs/platform/theme/electron-main/themeMainService'; import { IPolicyService, NullPolicyService } from 'vs/platform/policy/common/policy'; -import { WindowsPolicyService } from 'vs/platform/policy/node/windowsPolicyService'; +import { NativePolicyService } from 'vs/platform/policy/node/nativePolicyService'; import { FilePolicyService } from 'vs/platform/policy/common/filePolicyService'; /** @@ -170,7 +170,7 @@ class CodeMain { services.set(ILoggerService, new LoggerService(logService, fileService)); // Policy - const policyService = isWindows && productService.win32RegValueName ? new WindowsPolicyService(productService.win32RegValueName) + const policyService = isWindows && productService.win32RegValueName ? new NativePolicyService(productService.win32RegValueName) : environmentMainService.policyFile ? new FilePolicyService(environmentMainService.policyFile, fileService, logService) : new NullPolicyService(); services.set(IPolicyService, policyService); diff --git a/src/vs/code/node/cliProcessMain.ts b/src/vs/code/node/cliProcessMain.ts index f0151458057..bc630b7cae8 100644 --- a/src/vs/code/node/cliProcessMain.ts +++ b/src/vs/code/node/cliProcessMain.ts @@ -42,7 +42,7 @@ import { ConsoleLogger, getLogLevel, ILogger, ILogService, LogLevel, MultiplexLo import { SpdLogLogger } from 'vs/platform/log/node/spdlogLog'; import { FilePolicyService } from 'vs/platform/policy/common/filePolicyService'; import { IPolicyService, NullPolicyService } from 'vs/platform/policy/common/policy'; -import { WindowsPolicyService } from 'vs/platform/policy/node/windowsPolicyService'; +import { NativePolicyService } from 'vs/platform/policy/node/nativePolicyService'; import product from 'vs/platform/product/common/product'; import { IProductService } from 'vs/platform/product/common/productService'; import { IRequestService } from 'vs/platform/request/common/request'; @@ -133,7 +133,9 @@ class CliMain extends Disposable { fileService.registerProvider(Schemas.file, diskFileSystemProvider); // Policy - const policyService = isWindows && productService.win32RegValueName ? new WindowsPolicyService(productService.win32RegValueName) : environmentService.policyFile ? new FilePolicyService(environmentService.policyFile, fileService, logService) : new NullPolicyService(); + const policyService = isWindows && productService.win32RegValueName ? new NativePolicyService(productService.win32RegValueName) + : environmentService.policyFile ? new FilePolicyService(environmentService.policyFile, fileService, logService) + : new NullPolicyService(); services.set(IPolicyService, policyService); // Configuration diff --git a/src/vs/platform/policy/node/nativePolicyService.ts b/src/vs/platform/policy/node/nativePolicyService.ts new file mode 100644 index 00000000000..10cd2ee5446 --- /dev/null +++ b/src/vs/platform/policy/node/nativePolicyService.ts @@ -0,0 +1,68 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import { Emitter } from 'vs/base/common/event'; +import { IPolicyService, PolicyDefinition, PolicyName, PolicyValue } from 'vs/platform/policy/common/policy'; +import { IStringDictionary } from 'vs/base/common/collections'; +import { Iterable } from 'vs/base/common/iterator'; +import { Throttler } from 'vs/base/common/async'; +import type { Watcher } from 'vscode-policy-watcher'; + +export class NativePolicyService implements IPolicyService { + + readonly _serviceBrand: undefined; + + private policyDefinitions: IStringDictionary = {}; + private readonly policies = new Map(); + + private readonly _onDidChange = new Emitter(); + readonly onDidChange = this._onDidChange.event; + + private throttler = new Throttler(); + private watcher: Watcher | undefined; + + constructor(private readonly productName: string) { } + + async registerPolicyDefinitions(policyDefinitions: IStringDictionary): Promise> { + const size = Object.keys(this.policyDefinitions).length; + this.policyDefinitions = { ...policyDefinitions, ...this.policyDefinitions }; + + if (size !== Object.keys(this.policyDefinitions).length) { + await this.throttler.queue(async () => { + this.watcher?.dispose(); + + const { createWatcher } = await import('vscode-policy-watcher'); + + await new Promise(c => { + this.watcher = createWatcher(this.productName, policyDefinitions, update => { + for (const key in update) { + const value = update[key] as any; + + if (value === undefined) { + this.policies.delete(key); + } else { + this.policies.set(key, value); + } + } + + this._onDidChange.fire(Object.keys(update)); + c(); + }); + }); + }); + } + + return Iterable.reduce(this.policies.entries(), (r, [name, value]) => ({ ...r, [name]: value }), {}); + } + + getPolicyValue(name: PolicyName): PolicyValue | undefined { + return this.policies.get(name); + } + + dispose(): void { + this._onDidChange.dispose(); + this.watcher?.dispose(); + } +} diff --git a/src/vs/platform/policy/node/windowsPolicyService.ts b/src/vs/platform/policy/node/windowsPolicyService.ts deleted file mode 100644 index 23439893dc8..00000000000 --- a/src/vs/platform/policy/node/windowsPolicyService.ts +++ /dev/null @@ -1,70 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See License.txt in the project root for license information. - *--------------------------------------------------------------------------------------------*/ - -import { Event, Emitter } from 'vs/base/common/event'; -import { Disposable } from 'vs/base/common/lifecycle'; -import { IPolicyService, PolicyDefinition, PolicyName, PolicyValue } from 'vs/platform/policy/common/policy'; -import { createWatcher, Watcher } from 'vscode-policy-watcher'; -import { IStringDictionary } from 'vs/base/common/collections'; -import { Iterable } from 'vs/base/common/iterator'; - -export class WindowsPolicyService extends Disposable implements IPolicyService { - - readonly _serviceBrand: undefined; - - private readonly policies = new Map(); - private init: Promise | undefined; - - private readonly _onDidChange = new Emitter(); - readonly onDidChange = this._onDidChange.event; - - constructor(private readonly productName: string) { - super(); - } - - async registerPolicyDefinitions(policies: IStringDictionary): Promise> { - if (!this.init) { - this.init = new Promise(c => { - let first = true; - - const watcher = createWatcher(this.productName, policies, update => { - for (const key in update) { - const value = update[key] as any; - - if (value === undefined) { - this.policies.delete(key); - } else { - this.policies.set(key, value); - } - } - - if (first) { - first = false; - c(watcher); - } else { - this._onDidChange.fire(Object.keys(update)); - } - }); - - this._register(watcher); - }); - - await this.init; - } else { - const watcher = await this.init; - const promise = Event.toPromise(this.onDidChange); - watcher.addPolicies(policies); - await promise; - } - - // TODO@joao: heavy cleanup - - return Iterable.reduce(this.policies.entries(), (r, [name, value]) => ({ ...r, [name]: value }), {}); - } - - getPolicyValue(name: PolicyName): PolicyValue | undefined { - return this.policies.get(name); - } -} diff --git a/yarn.lock b/yarn.lock index ac4091e65aa..36cf507aa65 100644 --- a/yarn.lock +++ b/yarn.lock @@ -11796,10 +11796,10 @@ vscode-oniguruma@1.6.1: resolved "https://registry.yarnpkg.com/vscode-oniguruma/-/vscode-oniguruma-1.6.1.tgz#2bf4dfcfe3dd2e56eb549a3068c8ee39e6c30ce5" integrity sha512-vc4WhSIaVpgJ0jJIejjYxPvURJavX6QG41vu0mGhqywMkQqulezEqEQ3cO3gc8GvcOpX6ycmKGqRoROEMBNXTQ== -vscode-policy-watcher@^1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/vscode-policy-watcher/-/vscode-policy-watcher-1.0.0.tgz#08138a5ab15a1d6c021df51716a4daa97d74b471" - integrity sha512-j9nWBInu9wPW+3Uw/flYXP0kTewTqTfM6ADC24stqxURCAGOWxYz3ArBo21o+3zOrxpfCjc5mCbUNfLk+6IHuA== +vscode-policy-watcher@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/vscode-policy-watcher/-/vscode-policy-watcher-1.1.0.tgz#2921353c5080b3452929f1e350b9fab9ff852cc9" + integrity sha512-yPvy3Or66H0l8/FyWbJeGxpWW3GDZf65EIT7fqp1Ethdz4ecEnHThuHti7SlfdRJTf5qnifrX7af02INiHqDMA== dependencies: bindings "^1.5.0" node-addon-api "*"