src/vs/workbench/services/userDataProfile/common/remoteUserDataProfiles.ts

180 LOC · 59 covered · 121 uncovered · 11 ranges · 556 concepts · 1 introducers · 293 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 > /*--------------------------------------------------------------------------------------------- mcpTypes.ts ×31
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 } from '../../../../base/common/lifecycle.js';
7 > import { InstantiationType, registerSingleton } from '../../../../platform/instantiation/common/extensions.js';
8 > import { createDecorator } from '../../../../platform/instantiation/common/instantiation.js';
9 > import { DidChangeProfilesEvent, IUserDataProfile, IUserDataProfilesService } from '../../../../platform/userDataProfile/common/userDataProfile.js';
10 > import { IRemoteAgentService } from '../../remote/common/remoteAgentService.js';
11 > import { IStorageService, StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js';
12 > import { IStringDictionary } from '../../../../base/common/collections.js';
13 > import { ILogService } from '../../../../platform/log/common/log.js';
14 > import { IUserDataProfileService } from './userDataProfile.js';
15 > import { distinct } from '../../../../base/common/arrays.js';
16 > import { IWorkbenchEnvironmentService } from '../../environment/common/environmentService.js';
17 > import { UserDataProfilesService } from '../../../../platform/userDataProfile/common/userDataProfileIpc.js';
18 > import { ErrorNoTelemetry } from '../../../../base/common/errors.js';
19 >
20 > const associatedRemoteProfilesKey = 'associatedRemoteProfiles';
21 >
22 > export const IRemoteUserDataProfilesService = createDecorator<IRemoteUserDataProfilesService>('IRemoteUserDataProfilesService');
23 > export interface IRemoteUserDataProfilesService {
24 > readonly _serviceBrand: undefined;
25 > getRemoteProfiles(): Promise<readonly IUserDataProfile[]>;
26 > getRemoteProfile(localProfile: IUserDataProfile): Promise<IUserDataProfile>;
27 > }
28 >
29 > class RemoteUserDataProfilesService extends Disposable implements IRemoteUserDataProfilesService {
30 >
31 > readonly _serviceBrand: undefined;
32 >
33 > private readonly initPromise: Promise<void>;
34 >
35 > private remoteUserDataProfilesService: IUserDataProfilesService | undefined;
36 >
37 > constructor(
38 @IWorkbenchEnvironmentService private readonly environmentService: IWorkbenchEnvironmentService,
39 @IRemoteAgentService private readonly remoteAgentService: IRemoteAgentService,
40 @IUserDataProfilesService private readonly userDataProfilesService: IUserDataProfilesService,
41 @IUserDataProfileService private readonly userDataProfileService: IUserDataProfileService,
42 @IStorageService private readonly storageService: IStorageService,
43 @ILogService private readonly logService: ILogService,
44 ) {
45 super();
46 this.initPromise = this.init();
47 }
49 > private async init(): Promise<void> {
50 const connection = this.remoteAgentService.getConnection();
51 if (!connection) {
52 return;
53 }
54
55 const environment = await this.remoteAgentService.getEnvironment();
56 if (!environment) {
57 return;
58 }
59
60 this.remoteUserDataProfilesService = new UserDataProfilesService(environment.profiles.all, environment.profiles.home, connection.getChannel('userDataProfiles'));
61 this._register(this.userDataProfilesService.onDidChangeProfiles(e => this.onDidChangeLocalProfiles(e)));
62
63 // Associate current local profile with remote profile
64 const remoteProfile = await this.getAssociatedRemoteProfile(this.userDataProfileService.currentProfile, this.remoteUserDataProfilesService);
65 if (!remoteProfile.isDefault) {
66 this.setAssociatedRemoteProfiles([...this.getAssociatedRemoteProfiles(), remoteProfile.id]);
67 }
68
69 this.cleanUp();
70 }
72 > private async onDidChangeLocalProfiles(e: DidChangeProfilesEvent): Promise<void> {
73 for (const profile of e.removed) {
74 const remoteProfile = this.remoteUserDataProfilesService?.profiles.find(p => p.id === profile.id);
75 if (remoteProfile) {
76 await this.remoteUserDataProfilesService?.removeProfile(remoteProfile);
77 }
78 }
79 }
81 > async getRemoteProfiles(): Promise<readonly IUserDataProfile[]> {
82 await this.initPromise;
83
84 if (!this.remoteUserDataProfilesService) {
85 throw new ErrorNoTelemetry('Remote profiles service not available in the current window');
86 }
87
88 return this.remoteUserDataProfilesService.profiles;
89 }
91 > async getRemoteProfile(localProfile: IUserDataProfile): Promise<IUserDataProfile> {
92 await this.initPromise;
93
94 if (!this.remoteUserDataProfilesService) {
95 throw new ErrorNoTelemetry('Remote profiles service not available in the current window');
96 }
97
98 return this.getAssociatedRemoteProfile(localProfile, this.remoteUserDataProfilesService);
99 }
101 > private async getAssociatedRemoteProfile(localProfile: IUserDataProfile, remoteUserDataProfilesService: IUserDataProfilesService): Promise<IUserDataProfile> {
102 // If the local profile is the default profile, return the remote default profile
103 if (localProfile.isDefault) {
104 return remoteUserDataProfilesService.defaultProfile;
105 }
106
107 let profile = remoteUserDataProfilesService.profiles.find(p => p.id === localProfile.id);
108 if (!profile) {
109 profile = await remoteUserDataProfilesService.createProfile(localProfile.id, localProfile.name, {
110 transient: localProfile.isTransient,
111 useDefaultFlags: localProfile.useDefaultFlags,
112 });
113 this.setAssociatedRemoteProfiles([...this.getAssociatedRemoteProfiles(), this.userDataProfileService.currentProfile.id]);
114 }
115 return profile;
116 }
118 > private getAssociatedRemoteProfiles(): string[] {
119 if (this.environmentService.remoteAuthority) {
120 const remotes = this.parseAssociatedRemoteProfiles();
121 return remotes[this.environmentService.remoteAuthority] ?? [];
122 }
123 return [];
124 }
126 > private setAssociatedRemoteProfiles(profiles: string[]): void {
127 if (this.environmentService.remoteAuthority) {
128 const remotes = this.parseAssociatedRemoteProfiles();
129 profiles = distinct(profiles);
130 if (profiles.length) {
131 remotes[this.environmentService.remoteAuthority] = profiles;
132 } else {
133 delete remotes[this.environmentService.remoteAuthority];
134 }
135 if (Object.keys(remotes).length) {
136 this.storageService.store(associatedRemoteProfilesKey, JSON.stringify(remotes), StorageScope.APPLICATION, StorageTarget.MACHINE);
137 } else {
138 this.storageService.remove(associatedRemoteProfilesKey, StorageScope.APPLICATION);
139 }
140 }
141 }
143 > private parseAssociatedRemoteProfiles(): IStringDictionary<string[]> {
144 if (this.environmentService.remoteAuthority) {
145 const value = this.storageService.get(associatedRemoteProfilesKey, StorageScope.APPLICATION);
146 try {
147 return value ? JSON.parse(value) : {};
148 } catch (error) {
149 this.logService.error(error);
150 }
151 }
152 return {};
153 }
155 > private async cleanUp(): Promise<void> {
156 const associatedRemoteProfiles: string[] = [];
157 for (const profileId of this.getAssociatedRemoteProfiles()) {
158 const remoteProfile = this.remoteUserDataProfilesService?.profiles.find(p => p.id === profileId);
159 if (!remoteProfile) {
160 continue;
161 }
162 const localProfile = this.userDataProfilesService.profiles.find(p => p.id === profileId);
163 if (localProfile) {
164 if (localProfile.name !== remoteProfile.name) {
165 await this.remoteUserDataProfilesService?.updateProfile(remoteProfile, { name: localProfile.name });
166 }
167 associatedRemoteProfiles.push(profileId);
168 continue;
169 }
170 if (remoteProfile) {
171 // Cleanup remote profiles those are not available locally
172 await this.remoteUserDataProfilesService?.removeProfile(remoteProfile);
173 }
174 }
175 this.setAssociatedRemoteProfiles(associatedRemoteProfiles);
176 }
178 > }
179 >
180 > registerSingleton(IRemoteUserDataProfilesService, RemoteUserDataProfilesService, InstantiationType.Delayed);