|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 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 { createDecorator } from 'vs/platform/instantiation/common/instantiation'; |
| 7 | +import { Disposable } from 'vs/base/common/lifecycle'; |
| 8 | +import { getServiceMachineId } from 'vs/platform/serviceMachineId/common/serviceMachineId'; |
| 9 | +import { IEnvironmentService } from 'vs/platform/environment/common/environment'; |
| 10 | +import { IFileService } from 'vs/platform/files/common/files'; |
| 11 | +import { IStorageService } from 'vs/platform/storage/common/storage'; |
| 12 | +import { IUserDataSyncStoreService, IUserData, IUserDataSyncLogService } from 'vs/platform/userDataSync/common/userDataSync'; |
| 13 | +import { localize } from 'vs/nls'; |
| 14 | +import { IProductService } from 'vs/platform/product/common/productService'; |
| 15 | + |
| 16 | +interface IMachineData { |
| 17 | + id: string; |
| 18 | + name: string; |
| 19 | + disabled?: boolean; |
| 20 | +} |
| 21 | + |
| 22 | +interface IMachinesData { |
| 23 | + version: number; |
| 24 | + machines: IMachineData[]; |
| 25 | +} |
| 26 | + |
| 27 | +export type IUserDataSyncMachine = Readonly<IMachineData> & { readonly isCurrent: boolean }; |
| 28 | + |
| 29 | + |
| 30 | +export const IUserDataSyncMachinesService = createDecorator<IUserDataSyncMachinesService>('IUserDataSyncMachinesService'); |
| 31 | +export interface IUserDataSyncMachinesService { |
| 32 | + _serviceBrand: any; |
| 33 | + |
| 34 | + getMachines(): Promise<IUserDataSyncMachine[]>; |
| 35 | + updateName(name: string): Promise<void>; |
| 36 | + unset(): Promise<void>; |
| 37 | + |
| 38 | + disable(id: string): Promise<void> |
| 39 | +} |
| 40 | + |
| 41 | +export class UserDataSyncMachinesService extends Disposable implements IUserDataSyncMachinesService { |
| 42 | + |
| 43 | + private static readonly VERSION = 1; |
| 44 | + private static readonly RESOURCE = 'machines'; |
| 45 | + |
| 46 | + _serviceBrand: any; |
| 47 | + |
| 48 | + private readonly currentMachineIdPromise: Promise<string>; |
| 49 | + private userData: IUserData | null = null; |
| 50 | + |
| 51 | + constructor( |
| 52 | + @IEnvironmentService environmentService: IEnvironmentService, |
| 53 | + @IFileService fileService: IFileService, |
| 54 | + @IStorageService storageService: IStorageService, |
| 55 | + @IUserDataSyncStoreService private readonly userDataSyncStoreService: IUserDataSyncStoreService, |
| 56 | + @IUserDataSyncLogService private readonly logService: IUserDataSyncLogService, |
| 57 | + @IProductService private readonly productService: IProductService, |
| 58 | + ) { |
| 59 | + super(); |
| 60 | + this.currentMachineIdPromise = getServiceMachineId(environmentService, fileService, storageService); |
| 61 | + } |
| 62 | + |
| 63 | + async getMachines(): Promise<IUserDataSyncMachine[]> { |
| 64 | + const currentMachineId = await this.currentMachineIdPromise; |
| 65 | + const machineData = await this.readMachinesData(); |
| 66 | + return machineData.machines.map<IUserDataSyncMachine>(machine => ({ ...machine, ...{ isCurrent: machine.id === currentMachineId } })); |
| 67 | + } |
| 68 | + |
| 69 | + async updateName(name: string): Promise<void> { |
| 70 | + const currentMachineId = await this.currentMachineIdPromise; |
| 71 | + const machineData = await this.readMachinesData(); |
| 72 | + let currentMachine = machineData.machines.find(({ id }) => id === currentMachineId); |
| 73 | + if (currentMachine) { |
| 74 | + currentMachine.name = name; |
| 75 | + } else { |
| 76 | + machineData.machines.push({ id: currentMachineId, name }); |
| 77 | + } |
| 78 | + await this.writeMachinesData(machineData); |
| 79 | + } |
| 80 | + |
| 81 | + async unset(): Promise<void> { |
| 82 | + const currentMachineId = await this.currentMachineIdPromise; |
| 83 | + const machineData = await this.readMachinesData(); |
| 84 | + const updatedMachines = machineData.machines.filter(({ id }) => id !== currentMachineId); |
| 85 | + if (updatedMachines.length !== machineData.machines.length) { |
| 86 | + machineData.machines = updatedMachines; |
| 87 | + await this.writeMachinesData(machineData); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + async disable(machineId: string): Promise<void> { |
| 92 | + const machineData = await this.readMachinesData(); |
| 93 | + const machine = machineData.machines.find(({ id }) => id === machineId); |
| 94 | + if (machine) { |
| 95 | + machine.disabled = true; |
| 96 | + await this.writeMachinesData(machineData); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private async readMachinesData(): Promise<IMachinesData> { |
| 101 | + this.userData = await this.userDataSyncStoreService.read(UserDataSyncMachinesService.RESOURCE, this.userData); |
| 102 | + const machinesData = this.parse(this.userData); |
| 103 | + if (machinesData.version !== UserDataSyncMachinesService.VERSION) { |
| 104 | + throw new Error(localize('error incompatible', "Cannot read machines data as the current version is incompatible. Please update {0} and try again.", this.productService.nameLong)); |
| 105 | + } |
| 106 | + return machinesData; |
| 107 | + } |
| 108 | + |
| 109 | + private async writeMachinesData(machinesData: IMachinesData): Promise<void> { |
| 110 | + const content = JSON.stringify(machinesData); |
| 111 | + const ref = await this.userDataSyncStoreService.write(UserDataSyncMachinesService.RESOURCE, content, this.userData?.ref || null); |
| 112 | + this.userData = { ref, content }; |
| 113 | + } |
| 114 | + |
| 115 | + private parse(userData: IUserData): IMachinesData { |
| 116 | + if (userData.content !== null) { |
| 117 | + try { |
| 118 | + return JSON.parse(userData.content); |
| 119 | + } catch (e) { |
| 120 | + this.logService.error(e); |
| 121 | + } |
| 122 | + } |
| 123 | + return { |
| 124 | + version: UserDataSyncMachinesService.VERSION, |
| 125 | + machines: [] |
| 126 | + }; |
| 127 | + } |
| 128 | +} |
0 commit comments