src/vs/platform/userDataProfile/common/userDataProfileStorageService.ts

215 LOC · 175 covered · 40 uncovered · 35 ranges · 602 concepts · 9 introducers · 353 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.

1 > /*--------------------------------------------------------------------------------------------- storageIpc.ts ×22
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import { Disposable, DisposableMap, MutableDisposable, isDisposable, toDisposable } from '../../../base/common/lifecycle.js';
7 > import { IStorage, IStorageDatabase, Storage } from '../../../base/parts/storage/common/storage.js';
8 > import { createDecorator } from '../../instantiation/common/instantiation.js';
9 > import { AbstractStorageService, IStorageService, IStorageValueChangeEvent, StorageScope, StorageTarget, isProfileUsingDefaultStorage } from '../../storage/common/storage.js';
10 > import { Emitter, Event } from '../../../base/common/event.js';
11 > import { IRemoteService } from '../../ipc/common/services.js';
12 > import { ILogService } from '../../log/common/log.js';
13 > import { ApplicationStorageDatabaseClient, ProfileStorageDatabaseClient } from '../../storage/common/storageIpc.js';
14 > import { IUserDataProfile, IUserDataProfilesService, reviveProfile } from './userDataProfile.js';
15 >
16 > export interface IProfileStorageValueChanges {
17 > readonly profile: IUserDataProfile;
18 > readonly changes: IStorageValueChangeEvent[];
19 > }
20 >
21 > export interface IProfileStorageChanges {
22 > readonly targetChanges: IUserDataProfile[];
23 > readonly valueChanges: IProfileStorageValueChanges[];
24 > }
25 >
26 > export interface IStorageValue {
27 > readonly value: string | undefined;
28 > readonly target: StorageTarget;
29 > readonly scope?: StorageScope;
30 > }
31 >
32 > export const IUserDataProfileStorageService = createDecorator<IUserDataProfileStorageService>('IUserDataProfileStorageService');
33 > export interface IUserDataProfileStorageService {
34 > readonly _serviceBrand: undefined;
35 >
36 > /**
37 > * Emitted whenever data is updated or deleted in a profile storage or target of a profile storage entry changes
38 > */
39 > readonly onDidChange: Event<IProfileStorageChanges>;
40 >
41 > /**
42 > * Return the requested profile storage data
43 > * @param profile The profile from which the data has to be read from
44 > */
45 > readStorageData(profile: IUserDataProfile): Promise<Map<string, IStorageValue>>;
46 >
47 > /**
48 > * Update the given profile storage data in the profile storage
49 > * @param profile The profile to which the data has to be written to
50 > * @param data Data that has to be updated
51 > * @param target Storage target of the data
52 > * @param scope Storage scope of the data (defaults to PROFILE)
53 > */
54 > updateStorageData(profile: IUserDataProfile, data: Map<string, string | undefined | null>, target: StorageTarget, scope?: StorageScope): Promise<void>;
55 >
56 > /**
57 > * Calls a function with a storage service scoped to given profile.
58 > */
59 > withProfileScopedStorageService<T>(profile: IUserDataProfile, fn: (storageService: IStorageService) => Promise<T>): Promise<T>;
60 > }
61 >
62 > export abstract class AbstractUserDataProfileStorageService extends Disposable implements IUserDataProfileStorageService {
63 >
64 > _serviceBrand: undefined;
65 >
66 > readonly abstract onDidChange: Event<IProfileStorageChanges>;
67 >
68 > private readonly storageServicesMap: DisposableMap<string, StorageService> | undefined;
69 >
70 > constructor(
71 > persistStorages: boolean, userDataProfileStorageService.ts ×2
72 > @IStorageService protected readonly storageService: IStorageService
73 > ) {
74 > super();
75 > if (persistStorages) {
76 this.storageServicesMap = this._register(new DisposableMap<string, StorageService>());
77 }
80 > async readStorageData(profile: IUserDataProfile): Promise<Map<string, IStorageValue>> {
81 > return this.withProfileScopedStorageService(profile, async storageService => this.getItems(storageService, profile)); userDataProfileStorageService.ts ×4
82 > }
84 > async updateStorageData(profile: IUserDataProfile, data: Map<string, string | undefined | null>, target: StorageTarget, scope = StorageScope.PROFILE): Promise<void> {
85 > return this.withProfileScopedStorageService(profile, async storageService => this.writeItems(storageService, data, target, scope)); userDataProfileStorageService.ts ×2
86 > }
88 > async withProfileScopedStorageService<T>(profile: IUserDataProfile, fn: (storageService: IStorageService) => Promise<T>): Promise<T> {
89 > if (this.storageService.hasScope(profile)) { userDataProfileStorageService.ts ×3
90 > return fn(this.storageService); globalStateSync.ts ×15
91 > }
93 > let storageService = this.storageServicesMap?.get(profile.id); userDataProfileStorageService.ts ×3
94 > if (!storageService) {
95 > storageService = new StorageService(this.createStorageDatabase(profile)); userDataProfileStorageService.ts ×8
96 > this.storageServicesMap?.set(profile.id, storageService);
97 >
98 > try {
99 > await storageService.initialize();
100 > } catch (error) {
101 if (this.storageServicesMap?.has(profile.id)) {
102 this.storageServicesMap.deleteAndDispose(profile.id);
103 } else {
104 storageService.dispose();
105 }
106 throw error;
107 }
109 > try {
110 > const result = await fn(storageService);
111 > await storageService.flush();
112 > return result;
113 > } finally {
114 > if (!this.storageServicesMap?.has(profile.id)) {
115 > storageService.dispose();
116 > }
117 > }
120 > private getItems(storageService: IStorageService, profile: IUserDataProfile): Map<string, IStorageValue> {
121 > const result = new Map<string, IStorageValue>(); userDataProfileStorageService.ts ×4
122 > const populate = (scope: StorageScope, target: StorageTarget) => {
123 > for (const key of storageService.keys(scope, target)) {
124 > result.set(key, { value: storageService.get(key, scope), target, scope }); userDataProfileStorageService.ts ×1
125 > }
127 > populate(StorageScope.PROFILE, StorageTarget.USER);
128 > populate(StorageScope.PROFILE, StorageTarget.MACHINE);
129 > if (profile.isDefault) {
130 > populate(StorageScope.APPLICATION_SHARED, StorageTarget.USER); globalStateSync.ts ×15
131 > populate(StorageScope.APPLICATION_SHARED, StorageTarget.MACHINE);
132 > }
134 > }
136 > private writeItems(storageService: IStorageService, items: Map<string, string | undefined | null>, target: StorageTarget, scope = StorageScope.PROFILE): void {
137 > storageService.storeAll(Array.from(items.entries()).map(([key, value]) => ({ key, value, scope, target })), true); userDataProfileStorageService.ts ×2
138 > }
140 > protected abstract createStorageDatabase(profile: IUserDataProfile): Promise<IStorageDatabase>;
141 > }
142 >
143 > export class RemoteUserDataProfileStorageService extends AbstractUserDataProfileStorageService implements IUserDataProfileStorageService {
144 >
145 > private readonly _onDidChange: Emitter<IProfileStorageChanges>;
146 > readonly onDidChange: Event<IProfileStorageChanges>;
147 >
148 > constructor(
149 persistStorages: boolean,
150 private readonly remoteService: IRemoteService,
151 userDataProfilesService: IUserDataProfilesService,
152 storageService: IStorageService,
153 logService: ILogService,
154 ) {
155 super(persistStorages, storageService);
156
157 const channel = remoteService.getChannel('profileStorageListener');
158 const disposable = this._register(new MutableDisposable());
159 this._onDidChange = this._register(new Emitter<IProfileStorageChanges>({
160 // Start listening to profile storage changes only when someone is listening
161 onWillAddFirstListener: () => {
162 disposable.value = channel.listen<IProfileStorageChanges>('onDidChange')(e => {
163 logService.trace('profile storage changes', e);
164 this._onDidChange.fire({
165 targetChanges: e.targetChanges.map(profile => reviveProfile(profile, userDataProfilesService.profilesHome.scheme)),
166 valueChanges: e.valueChanges.map(e => ({ ...e, profile: reviveProfile(e.profile, userDataProfilesService.profilesHome.scheme) }))
167 });
168 });
169 },
170 // Stop listening to profile storage changes when no one is listening
171 onDidRemoveLastListener: () => disposable.value = undefined
172 }));
173 this.onDidChange = this._onDidChange.event;
174 }
176 > protected async createStorageDatabase(profile: IUserDataProfile): Promise<IStorageDatabase> {
177 const storageChannel = this.remoteService.getChannel('storage');
178 return isProfileUsingDefaultStorage(profile) ? new ApplicationStorageDatabaseClient(storageChannel) : new ProfileStorageDatabaseClient(storageChannel, profile);
179 }
181 >
182 > class StorageService extends AbstractStorageService {
183 >
184 > private profileStorage: IStorage | undefined;
185 >
186 > constructor(private readonly profileStorageDatabase: Promise<IStorageDatabase>) {
187 > super({ flushInterval: 100 }); userDataProfileStorageService.ts ×8
188 > }
190 > protected async doInitialize(): Promise<void> {
191 > const profileStorageDatabase = await this.profileStorageDatabase; userDataProfileStorageService.ts ×8
192 > const profileStorage = new Storage(profileStorageDatabase);
193 > this._register(profileStorage.onDidChangeStorage(e => {
194 > this.emitDidChangeValue(StorageScope.PROFILE, e); storage.ts ×2
196 > this._register(toDisposable(() => {
197 > profileStorage.close();
198 > profileStorage.dispose();
199 > if (isDisposable(profileStorageDatabase)) {
200 profileStorageDatabase.dispose();
201 }
203 > this.profileStorage = profileStorage;
204 > return this.profileStorage.init();
205 > }
207 > protected getStorage(scope: StorageScope): IStorage | undefined {
208 > return scope === StorageScope.PROFILE ? this.profileStorage : undefined; userDataProfileStorageService.ts ×8
209 > }
211 > protected getLogDetails(): string | undefined { return undefined; }
212 > protected async switchToProfile(): Promise<void> { }
213 > protected async switchToWorkspace(): Promise<void> { }
214 > hasScope() { return false; }
215 > }