|
6 | 6 | import { AbstractPolicyService, IPolicyService, PolicyDefinition } from 'vs/platform/policy/common/policy'; |
7 | 7 | import { IStringDictionary } from 'vs/base/common/collections'; |
8 | 8 | import { Throttler } from 'vs/base/common/async'; |
9 | | -import { createWatcher, Watcher } from 'vscode-policy-watcher'; |
| 9 | +import { createWatcher, PolicyUpdate, Watcher } from 'vscode-policy-watcher'; |
10 | 10 | import { MutableDisposable } from 'vs/base/common/lifecycle'; |
| 11 | +import { ILogService } from 'vs/platform/log/common/log'; |
11 | 12 |
|
12 | 13 | export class NativePolicyService extends AbstractPolicyService implements IPolicyService { |
13 | 14 |
|
14 | 15 | private throttler = new Throttler(); |
15 | 16 | private watcher = this._register(new MutableDisposable<Watcher>()); |
16 | 17 |
|
17 | | - constructor(private readonly productName: string) { |
| 18 | + constructor( |
| 19 | + @ILogService private readonly logService: ILogService, |
| 20 | + private readonly productName: string |
| 21 | + ) { |
18 | 22 | super(); |
19 | 23 | } |
20 | 24 |
|
21 | | - protected async initializePolicies(policyDefinitions: IStringDictionary<PolicyDefinition>): Promise<void> { |
| 25 | + protected async _updatePolicyDefinitions(policyDefinitions: IStringDictionary<PolicyDefinition>): Promise<void> { |
| 26 | + this.logService.trace(`NativePolicyService#_updatePolicyDefinitions - Found ${policyDefinitions.length} policy definitions`); |
| 27 | + |
22 | 28 | await this.throttler.queue(() => new Promise<void>((c, e) => { |
23 | 29 | try { |
24 | 30 | this.watcher.value = createWatcher(this.productName, policyDefinitions, update => { |
25 | | - for (const key in update) { |
26 | | - const value = update[key] as any; |
27 | | - |
28 | | - if (value === undefined) { |
29 | | - this.policies.delete(key); |
30 | | - } else { |
31 | | - this.policies.set(key, value); |
32 | | - } |
33 | | - } |
34 | | - |
35 | | - this._onDidChange.fire(Object.keys(update)); |
| 31 | + this._onDidPolicyChange(update); |
36 | 32 | c(); |
37 | 33 | }); |
38 | 34 | } catch (err) { |
| 35 | + this.logService.error(`NativePolicyService#_updatePolicyDefinitions - Error creating watcher:`, err); |
39 | 36 | e(err); |
40 | 37 | } |
41 | 38 | })); |
42 | 39 | } |
43 | 40 |
|
| 41 | + private _onDidPolicyChange(update: PolicyUpdate<IStringDictionary<PolicyDefinition>>): void { |
| 42 | + this.logService.trace(`NativePolicyService#_onDidPolicyChange - Updated policy values: ${Object.keys(update).join(', ')}`); |
| 43 | + |
| 44 | + for (const key in update) { |
| 45 | + const value = update[key] as any; |
| 46 | + |
| 47 | + if (value === undefined) { |
| 48 | + this.policies.delete(key); |
| 49 | + } else { |
| 50 | + this.policies.set(key, value); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + this._onDidChange.fire(Object.keys(update)); |
| 55 | + } |
44 | 56 | } |
0 commit comments