-
Notifications
You must be signed in to change notification settings - Fork 27.9k
/
userDataProfile.ts
92 lines (72 loc) · 4.54 KB
/
userDataProfile.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Emitter, Event } from 'vs/base/common/event';
import { Disposable } from 'vs/base/common/lifecycle';
import { joinPath } from 'vs/base/common/resources';
import { URI, UriDto } from 'vs/base/common/uri';
import { IChannel } from 'vs/base/parts/ipc/common/ipc';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { IMainProcessService } from 'vs/platform/ipc/electron-sandbox/services';
import { DidChangeProfilesEvent, IUserDataProfile, IUserDataProfilesService, reviveProfile, UseDefaultProfileFlags, WorkspaceIdentifier } from 'vs/platform/userDataProfile/common/userDataProfile';
export class UserDataProfilesNativeService extends Disposable implements IUserDataProfilesService {
readonly _serviceBrand: undefined;
private readonly channel: IChannel;
readonly profilesHome: URI;
get defaultProfile(): IUserDataProfile { return this.profiles[0]; }
private _profiles: IUserDataProfile[] = [];
get profiles(): IUserDataProfile[] { return this._profiles; }
private readonly _onDidChangeProfiles = this._register(new Emitter<DidChangeProfilesEvent>());
readonly onDidChangeProfiles = this._onDidChangeProfiles.event;
readonly onDidResetWorkspaces: Event<void>;
constructor(
profiles: readonly UriDto<IUserDataProfile>[],
@IMainProcessService mainProcessService: IMainProcessService,
@IEnvironmentService environmentService: IEnvironmentService,
) {
super();
this.channel = mainProcessService.getChannel('userDataProfiles');
this.profilesHome = joinPath(environmentService.userRoamingDataHome, 'profiles');
this._profiles = profiles.map(profile => reviveProfile(profile, this.profilesHome.scheme));
this._register(this.channel.listen<DidChangeProfilesEvent>('onDidChangeProfiles')(e => {
const added = e.added.map(profile => reviveProfile(profile, this.profilesHome.scheme));
const removed = e.removed.map(profile => reviveProfile(profile, this.profilesHome.scheme));
const updated = e.updated.map(profile => reviveProfile(profile, this.profilesHome.scheme));
this._profiles = e.all.map(profile => reviveProfile(profile, this.profilesHome.scheme));
this._onDidChangeProfiles.fire({ added, removed, updated, all: this.profiles });
}));
this.onDidResetWorkspaces = this.channel.listen<void>('onDidResetWorkspaces');
}
async createNamedProfile(name: string, useDefaultFlags?: UseDefaultProfileFlags, workspaceIdentifier?: WorkspaceIdentifier): Promise<IUserDataProfile> {
const result = await this.channel.call<UriDto<IUserDataProfile>>('createNamedProfile', [name, useDefaultFlags, workspaceIdentifier]);
return reviveProfile(result, this.profilesHome.scheme);
}
async createProfile(id: string, name: string, useDefaultFlags?: UseDefaultProfileFlags, transient?: boolean, workspaceIdentifier?: WorkspaceIdentifier): Promise<IUserDataProfile> {
const result = await this.channel.call<UriDto<IUserDataProfile>>('createProfile', [id, name, useDefaultFlags, transient, workspaceIdentifier]);
return reviveProfile(result, this.profilesHome.scheme);
}
async createTransientProfile(workspaceIdentifier?: WorkspaceIdentifier): Promise<IUserDataProfile> {
const result = await this.channel.call<UriDto<IUserDataProfile>>('createTransientProfile', [workspaceIdentifier]);
return reviveProfile(result, this.profilesHome.scheme);
}
async setProfileForWorkspace(workspaceIdentifier: WorkspaceIdentifier, profile: IUserDataProfile): Promise<void> {
await this.channel.call<UriDto<IUserDataProfile>>('setProfileForWorkspace', [workspaceIdentifier, profile]);
}
removeProfile(profile: IUserDataProfile): Promise<void> {
return this.channel.call('removeProfile', [profile]);
}
async updateProfile(profile: IUserDataProfile, name: string, useDefaultFlags?: UseDefaultProfileFlags, transient?: boolean): Promise<IUserDataProfile> {
const result = await this.channel.call<UriDto<IUserDataProfile>>('updateProfile', [profile, name, useDefaultFlags, transient]);
return reviveProfile(result, this.profilesHome.scheme);
}
resetWorkspaces(): Promise<void> {
return this.channel.call('resetWorkspaces');
}
cleanUp(): Promise<void> {
return this.channel.call('cleanUp');
}
cleanUpTransientProfiles(): Promise<void> {
return this.channel.call('cleanUpTransientProfiles');
}
}