Skip to content

Commit

Permalink
fix #172367
Browse files Browse the repository at this point in the history
  • Loading branch information
sandy081 committed Jan 26, 2023
1 parent 45db0a2 commit 9f9cdee
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 15 deletions.
9 changes: 8 additions & 1 deletion src/vs/platform/userDataProfile/browser/userDataProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,17 @@ export class BrowserUserDataProfilesService extends UserDataProfilesService impl
}

protected override getStoredProfileAssociations(): StoredProfileAssociations {
const migrateKey = 'profileAssociationsMigration';
try {
const value = window.localStorage.getItem(UserDataProfilesService.PROFILE_ASSOCIATIONS_KEY);
if (value) {
return revive(JSON.parse(value));
let associations: StoredProfileAssociations = JSON.parse(value);
if (!window.localStorage.getItem(migrateKey)) {
associations = this.migrateStoredProfileAssociations(associations);
this.saveStoredProfileAssociations(associations);
window.localStorage.setItem(migrateKey, 'true');
}
return associations;
}
} catch (error) {
/* ignore */
Expand Down
40 changes: 31 additions & 9 deletions src/vs/platform/userDataProfile/common/userDataProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,24 +242,22 @@ export class UserDataProfilesService extends Disposable implements IUserDataProf
}
const workspaces = new ResourceMap<IUserDataProfile>();
const emptyWindows = new Map<string, IUserDataProfile>();
const defaultProfile = toUserDataProfile(hash(this.environmentService.userRoamingDataHome.path).toString(16), localize('defaultProfile', "Default"), this.environmentService.userRoamingDataHome);
const defaultProfile = this.createDefaultProfile();
profiles.unshift({ ...defaultProfile, extensionsResource: this.getDefaultProfileExtensionsLocation() ?? defaultProfile.extensionsResource, isDefault: true });
if (profiles.length) {
const profileAssociaitions = this.getStoredProfileAssociations();
if (profileAssociaitions.workspaces) {
for (const [workspacePath, profilePath] of Object.entries(profileAssociaitions.workspaces)) {
for (const [workspacePath, profileId] of Object.entries(profileAssociaitions.workspaces)) {
const workspace = URI.parse(workspacePath);
const profileLocation = URI.parse(profilePath);
const profile = profiles.find(p => this.uriIdentityService.extUri.isEqual(p.location, profileLocation));
const profile = profiles.find(p => p.id === profileId);
if (profile) {
workspaces.set(workspace, profile);
}
}
}
if (profileAssociaitions.emptyWindows) {
for (const [windowId, profilePath] of Object.entries(profileAssociaitions.emptyWindows)) {
const profileLocation = URI.parse(profilePath);
const profile = profiles.find(p => this.uriIdentityService.extUri.isEqual(p.location, profileLocation));
for (const [windowId, profileId] of Object.entries(profileAssociaitions.emptyWindows)) {
const profile = profiles.find(p => p.id === profileId);
if (profile) {
emptyWindows.set(windowId, profile);
}
Expand All @@ -271,6 +269,10 @@ export class UserDataProfilesService extends Disposable implements IUserDataProf
return this._profilesObject;
}

private createDefaultProfile() {
return toUserDataProfile('__default__profile__', localize('defaultProfile', "Default"), this.environmentService.userRoamingDataHome);
}

async createTransientProfile(workspaceIdentifier?: IAnyWorkspaceIdentifier): Promise<IUserDataProfile> {
const namePrefix = `Temp`;
const nameRegEx = new RegExp(`${escapeRegExpCharacters(namePrefix)}\\s(\\d+)`);
Expand Down Expand Up @@ -548,16 +550,36 @@ export class UserDataProfilesService extends Disposable implements IUserDataProf
private updateStoredProfileAssociations() {
const workspaces: IStringDictionary<string> = {};
for (const [workspace, profile] of this.profilesObject.workspaces.entries()) {
workspaces[workspace.toString()] = profile.location.toString();
workspaces[workspace.toString()] = profile.id;
}
const emptyWindows: IStringDictionary<string> = {};
for (const [windowId, profile] of this.profilesObject.emptyWindows.entries()) {
emptyWindows[windowId.toString()] = profile.location.toString();
emptyWindows[windowId.toString()] = profile.id;
}
this.saveStoredProfileAssociations({ workspaces, emptyWindows });
this._profilesObject = undefined;
}

// TODO: @sandy081 Remove migration after couple of releases
protected migrateStoredProfileAssociations(storedProfileAssociations: StoredProfileAssociations): StoredProfileAssociations {
const workspaces: IStringDictionary<string> = {};
const defaultProfile = this.createDefaultProfile();
if (storedProfileAssociations.workspaces) {
for (const [workspace, location] of Object.entries(storedProfileAssociations.workspaces)) {
const uri = URI.parse(location);
workspaces[workspace] = this.uriIdentityService.extUri.isEqual(uri, defaultProfile.location) ? defaultProfile.id : this.uriIdentityService.extUri.basename(uri);
}
}
const emptyWindows: IStringDictionary<string> = {};
if (storedProfileAssociations.emptyWindows) {
for (const [workspace, location] of Object.entries(storedProfileAssociations.emptyWindows)) {
const uri = URI.parse(location);
emptyWindows[workspace] = this.uriIdentityService.extUri.isEqual(uri, defaultProfile.location) ? defaultProfile.id : this.uriIdentityService.extUri.basename(uri);
}
}
return { workspaces, emptyWindows };
}

protected getStoredProfiles(): StoredUserDataProfile[] { return []; }
protected saveStoredProfiles(storedProfiles: StoredUserDataProfile[]): void { throw new Error('not implemented'); }

Expand Down
18 changes: 16 additions & 2 deletions src/vs/platform/userDataProfile/electron-main/userDataProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,21 @@ export class UserDataProfilesMainService extends UserDataProfilesService impleme

protected override saveStoredProfiles(storedProfiles: StoredUserDataProfile[]): void {
if (storedProfiles.length) {
this.stateMainService.setItem(UserDataProfilesMainService.PROFILES_KEY, storedProfiles);
this.stateMainService.setItem(UserDataProfilesMainService.PROFILES_KEY, storedProfiles.map(profile => ({ ...profile, location: this.uriIdentityService.extUri.basename(profile.location) })));
} else {
this.stateMainService.removeItem(UserDataProfilesMainService.PROFILES_KEY);
}
}

protected override getStoredProfiles(): StoredUserDataProfile[] {
const storedProfiles = super.getStoredProfiles();
if (!this.stateMainService.getItem<boolean>('userDataProfilesMigration', false)) {
this.saveStoredProfiles(storedProfiles);
this.stateMainService.setItem('userDataProfilesMigration', true);
}
return storedProfiles;
}

protected override saveStoredProfileAssociations(storedProfileAssociations: StoredProfileAssociations): void {
if (storedProfileAssociations.emptyWindows || storedProfileAssociations.workspaces) {
this.stateMainService.setItem(UserDataProfilesMainService.PROFILE_ASSOCIATIONS_KEY, storedProfileAssociations);
Expand All @@ -72,7 +81,12 @@ export class UserDataProfilesMainService extends UserDataProfilesService impleme
}, {});
this.stateMainService.setItem(UserDataProfilesMainService.PROFILE_ASSOCIATIONS_KEY, <StoredProfileAssociations>{ workspaces });
}
return super.getStoredProfileAssociations();
const associations = super.getStoredProfileAssociations();
if (!this.stateMainService.getItem<boolean>(UserDataProfilesService.PROFILE_ASSOCIATIONS_MIGRATION_KEY, false)) {
this.saveStoredProfileAssociations(associations);
this.stateMainService.setItem(UserDataProfilesService.PROFILE_ASSOCIATIONS_MIGRATION_KEY, true);
}
return associations;
}

}
12 changes: 9 additions & 3 deletions src/vs/platform/userDataProfile/node/userDataProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { revive } from 'vs/base/common/marshalling';
import { isString } from 'vs/base/common/types';
import { URI, UriDto } from 'vs/base/common/uri';
import { INativeEnvironmentService } from 'vs/platform/environment/common/environment';
import { IFileService } from 'vs/platform/files/common/files';
Expand All @@ -29,9 +29,12 @@ export class ServerUserDataProfilesService extends BaseUserDataProfilesService i

}

type StoredUserDataProfileState = StoredUserDataProfile & { location: URI | string };

export class UserDataProfilesService extends ServerUserDataProfilesService implements IUserDataProfilesService {

protected static readonly PROFILE_ASSOCIATIONS_MIGRATION_KEY = 'profileAssociationsMigration';

constructor(
@IStateService private readonly stateService: IStateService,
@IUriIdentityService uriIdentityService: IUriIdentityService,
Expand All @@ -43,11 +46,14 @@ export class UserDataProfilesService extends ServerUserDataProfilesService imple
}

protected override getStoredProfiles(): StoredUserDataProfile[] {
return revive(this.stateService.getItem<UriDto<StoredUserDataProfile>[]>(UserDataProfilesService.PROFILES_KEY, []));
const storedProfilesState = this.stateService.getItem<UriDto<StoredUserDataProfileState>[]>(UserDataProfilesService.PROFILES_KEY, []);
return storedProfilesState.map(p => ({ ...p, location: isString(p.location) ? this.uriIdentityService.extUri.joinPath(this.profilesHome, p.location) : URI.revive(p.location) }));
}

protected override getStoredProfileAssociations(): StoredProfileAssociations {
return revive(this.stateService.getItem<UriDto<StoredProfileAssociations>>(UserDataProfilesService.PROFILE_ASSOCIATIONS_KEY, {}));
const associations = this.stateService.getItem<StoredProfileAssociations>(UserDataProfilesService.PROFILE_ASSOCIATIONS_KEY, {});
const migrated = this.stateService.getItem<boolean>(UserDataProfilesService.PROFILE_ASSOCIATIONS_MIGRATION_KEY, false);
return migrated ? associations : this.migrateStoredProfileAssociations(associations);
}

protected override getDefaultProfileExtensionsLocation(): URI {
Expand Down

0 comments on commit 9f9cdee

Please sign in to comment.