userDataSyncIpc.ts 9.9 KB
Newer Older
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6
import { IServerChannel, IChannel, IPCServer } from 'vs/base/parts/ipc/common/ipc';
S
Sandeep Somavarapu 已提交
7
import { Event, Emitter } from 'vs/base/common/event';
8
import { IUserDataSyncService, IUserDataSyncUtilService, IUserDataAutoSyncService, IManualSyncTask, IUserDataManifest } from 'vs/platform/userDataSync/common/userDataSync';
S
Sandeep Somavarapu 已提交
9 10 11
import { URI } from 'vs/base/common/uri';
import { IStringDictionary } from 'vs/base/common/collections';
import { FormattingOptions } from 'vs/base/common/jsonFormatter';
12 13
import { IStorageKeysSyncRegistryService, IStorageKey } from 'vs/platform/userDataSync/common/storageKeys';
import { Disposable } from 'vs/base/common/lifecycle';
S
Sandeep Somavarapu 已提交
14
import { ILogService } from 'vs/platform/log/common/log';
S
Sandeep Somavarapu 已提交
15
import { IUserDataSyncMachinesService } from 'vs/platform/userDataSync/common/userDataSyncMachines';
16
import { IUserDataSyncAccountService } from 'vs/platform/userDataSync/common/userDataSyncAccount';
17 18 19

export class UserDataSyncChannel implements IServerChannel {

20
	constructor(private server: IPCServer, private readonly service: IUserDataSyncService, private readonly logService: ILogService) { }
21 22 23 24

	listen(_: unknown, event: string): Event<any> {
		switch (event) {
			case 'onDidChangeStatus': return this.service.onDidChangeStatus;
S
Sandeep Somavarapu 已提交
25
			case 'onSynchronizeResource': return this.service.onSynchronizeResource;
26
			case 'onDidChangeConflicts': return this.service.onDidChangeConflicts;
27
			case 'onDidChangeLocal': return this.service.onDidChangeLocal;
28
			case 'onDidChangeLastSyncTime': return this.service.onDidChangeLastSyncTime;
S
Sandeep Somavarapu 已提交
29
			case 'onSyncErrors': return this.service.onSyncErrors;
30 31 32 33
		}
		throw new Error(`Event not found: ${event}`);
	}

S
Sandeep Somavarapu 已提交
34 35 36 37 38 39
	async call(context: any, command: string, args?: any): Promise<any> {
		try {
			const result = await this._call(context, command, args);
			return result;
		} catch (e) {
			this.logService.error(e);
40
			throw e;
S
Sandeep Somavarapu 已提交
41 42 43 44
		}
	}

	private _call(context: any, command: string, args?: any): Promise<any> {
45
		switch (command) {
46
			case '_getInitialData': return Promise.resolve([this.service.status, this.service.conflicts, this.service.lastSyncTime]);
47 48 49

			case 'createManualSyncTask': return this.createManualSyncTask();

50
			case 'pull': return this.service.pull();
51
			case 'replace': return this.service.replace(URI.revive(args[0]));
S
Sandeep Somavarapu 已提交
52 53
			case 'reset': return this.service.reset();
			case 'resetLocal': return this.service.resetLocal();
54
			case 'hasPreviouslySynced': return this.service.hasPreviouslySynced();
55
			case 'hasLocalData': return this.service.hasLocalData();
S
Sandeep Somavarapu 已提交
56
			case 'isFirstTimeSyncingWithAnotherMachine': return this.service.isFirstTimeSyncingWithAnotherMachine();
57
			case 'acceptPreviewContent': return this.service.acceptPreviewContent(args[0], URI.revive(args[1]), args[2]);
58 59 60 61
			case 'resolveContent': return this.service.resolveContent(URI.revive(args[0]));
			case 'getLocalSyncResourceHandles': return this.service.getLocalSyncResourceHandles(args[0]);
			case 'getRemoteSyncResourceHandles': return this.service.getRemoteSyncResourceHandles(args[0]);
			case 'getAssociatedResources': return this.service.getAssociatedResources(args[0], { created: args[1].created, uri: URI.revive(args[1].uri) });
62
			case 'getMachineId': return this.service.getMachineId(args[0], { created: args[1].created, uri: URI.revive(args[1].uri) });
63 64 65
		}
		throw new Error('Invalid call');
	}
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 92 93 94 95 96

	private taskCounter = 1;
	private async createManualSyncTask(): Promise<{ initialData: { manifest: IUserDataManifest | null }, channelName: string }> {
		const manualSyncTask = await this.service.createManualSyncTask();
		const manualSyncTaskChannel = new ManualSyncTaskChannel(manualSyncTask);
		const channelName = `manualSyncTask-${this.taskCounter++}`;
		this.server.registerChannel(channelName, manualSyncTaskChannel);
		return { initialData: { manifest: manualSyncTask.manifest }, channelName };
	}
}

class ManualSyncTaskChannel implements IServerChannel {

	constructor(private readonly manualSyncTask: IManualSyncTask) { }

	listen(_: unknown, event: string): Event<any> {
		throw new Error(`Event not found: ${event}`);
	}

	async call(context: any, command: string, args?: any): Promise<any> {
		switch (command) {
			case 'preview': return this.manualSyncTask.preview();
			case 'accept': return this.manualSyncTask.accept(URI.revive(args[0]), args[1]);
			case 'merge': return this.manualSyncTask.merge();
			case 'pull': return this.manualSyncTask.pull();
			case 'push': return this.manualSyncTask.push();
			case 'stop': return this.manualSyncTask.stop();
		}
		throw new Error('Invalid call');
	}

97
}
S
Sandeep Somavarapu 已提交
98

S
Sandeep Somavarapu 已提交
99 100 101 102 103
export class UserDataAutoSyncChannel implements IServerChannel {

	constructor(private readonly service: IUserDataAutoSyncService) { }

	listen(_: unknown, event: string): Event<any> {
S
Sandeep Somavarapu 已提交
104
		switch (event) {
S
Sandeep Somavarapu 已提交
105 106
			case 'onTurnOnSync': return this.service.onTurnOnSync;
			case 'onDidTurnOnSync': return this.service.onDidTurnOnSync;
S
Sandeep Somavarapu 已提交
107 108
			case 'onError': return this.service.onError;
		}
S
Sandeep Somavarapu 已提交
109 110 111 112 113
		throw new Error(`Event not found: ${event}`);
	}

	call(context: any, command: string, args?: any): Promise<any> {
		switch (command) {
S
Sandeep Somavarapu 已提交
114 115
			case 'triggerSync': return this.service.triggerSync(args[0], args[1]);
			case 'turnOn': return this.service.turnOn(args[0]);
S
Sandeep Somavarapu 已提交
116
			case 'turnOff': return this.service.turnOff(args[0]);
S
Sandeep Somavarapu 已提交
117 118 119 120 121
		}
		throw new Error('Invalid call');
	}
}

S
Sandeep Somavarapu 已提交
122 123 124 125 126 127 128 129 130 131
export class UserDataSycnUtilServiceChannel implements IServerChannel {

	constructor(private readonly service: IUserDataSyncUtilService) { }

	listen(_: unknown, event: string): Event<any> {
		throw new Error(`Event not found: ${event}`);
	}

	call(context: any, command: string, args?: any): Promise<any> {
		switch (command) {
S
Sandeep Somavarapu 已提交
132
			case 'resolveDefaultIgnoredSettings': return this.service.resolveDefaultIgnoredSettings();
S
Sandeep Somavarapu 已提交
133 134 135 136 137 138 139 140 141
			case 'resolveUserKeybindings': return this.service.resolveUserBindings(args[0]);
			case 'resolveFormattingOptions': return this.service.resolveFormattingOptions(URI.revive(args[0]));
		}
		throw new Error('Invalid call');
	}
}

export class UserDataSyncUtilServiceClient implements IUserDataSyncUtilService {

142
	declare readonly _serviceBrand: undefined;
S
Sandeep Somavarapu 已提交
143 144 145 146

	constructor(private readonly channel: IChannel) {
	}

S
Sandeep Somavarapu 已提交
147 148 149 150
	async resolveDefaultIgnoredSettings(): Promise<string[]> {
		return this.channel.call('resolveDefaultIgnoredSettings');
	}

S
Sandeep Somavarapu 已提交
151 152 153 154 155 156 157 158 159 160
	async resolveUserBindings(userbindings: string[]): Promise<IStringDictionary<string>> {
		return this.channel.call('resolveUserKeybindings', [userbindings]);
	}

	async resolveFormattingOptions(file: URI): Promise<FormattingOptions> {
		return this.channel.call('resolveFormattingOptions', [file]);
	}

}

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
export class StorageKeysSyncRegistryChannel implements IServerChannel {

	constructor(private readonly service: IStorageKeysSyncRegistryService) { }

	listen(_: unknown, event: string): Event<any> {
		switch (event) {
			case 'onDidChangeStorageKeys': return this.service.onDidChangeStorageKeys;
		}
		throw new Error(`Event not found: ${event}`);
	}

	call(context: any, command: string, args?: any): Promise<any> {
		switch (command) {
			case '_getInitialData': return Promise.resolve(this.service.storageKeys);
			case 'registerStorageKey': return Promise.resolve(this.service.registerStorageKey(args[0]));
		}
		throw new Error('Invalid call');
	}
}

export class StorageKeysSyncRegistryChannelClient extends Disposable implements IStorageKeysSyncRegistryService {

183
	declare readonly _serviceBrand: undefined;
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207

	private _storageKeys: ReadonlyArray<IStorageKey> = [];
	get storageKeys(): ReadonlyArray<IStorageKey> { return this._storageKeys; }
	private readonly _onDidChangeStorageKeys: Emitter<ReadonlyArray<IStorageKey>> = this._register(new Emitter<ReadonlyArray<IStorageKey>>());
	readonly onDidChangeStorageKeys = this._onDidChangeStorageKeys.event;

	constructor(private readonly channel: IChannel) {
		super();
		this.channel.call<IStorageKey[]>('_getInitialData').then(storageKeys => {
			this.updateStorageKeys(storageKeys);
			this._register(this.channel.listen<ReadonlyArray<IStorageKey>>('onDidChangeStorageKeys')(storageKeys => this.updateStorageKeys(storageKeys)));
		});
	}

	private async updateStorageKeys(storageKeys: ReadonlyArray<IStorageKey>): Promise<void> {
		this._storageKeys = storageKeys;
		this._onDidChangeStorageKeys.fire(this.storageKeys);
	}

	registerStorageKey(storageKey: IStorageKey): void {
		this.channel.call('registerStorageKey', [storageKey]);
	}

}
S
Sandeep Somavarapu 已提交
208 209 210 211 212 213 214 215 216 217 218 219

export class UserDataSyncMachinesServiceChannel implements IServerChannel {

	constructor(private readonly service: IUserDataSyncMachinesService) { }

	listen(_: unknown, event: string): Event<any> {
		throw new Error(`Event not found: ${event}`);
	}

	async call(context: any, command: string, args?: any): Promise<any> {
		switch (command) {
			case 'getMachines': return this.service.getMachines();
220
			case 'addCurrentMachine': return this.service.addCurrentMachine();
S
Sandeep Somavarapu 已提交
221 222
			case 'removeCurrentMachine': return this.service.removeCurrentMachine();
			case 'renameMachine': return this.service.renameMachine(args[0], args[1]);
223
			case 'setEnablement': return this.service.setEnablement(args[0], args[1]);
S
Sandeep Somavarapu 已提交
224 225 226 227 228
		}
		throw new Error('Invalid call');
	}

}
229 230 231 232 233 234

export class UserDataSyncAccountServiceChannel implements IServerChannel {
	constructor(private readonly service: IUserDataSyncAccountService) { }

	listen(_: unknown, event: string): Event<any> {
		switch (event) {
S
Sandeep Somavarapu 已提交
235
			case 'onDidChangeAccount': return this.service.onDidChangeAccount;
236 237 238 239 240 241 242
			case 'onTokenFailed': return this.service.onTokenFailed;
		}
		throw new Error(`Event not found: ${event}`);
	}

	call(context: any, command: string, args?: any): Promise<any> {
		switch (command) {
S
Sandeep Somavarapu 已提交
243
			case '_getInitialData': return Promise.resolve(this.service.account);
244 245 246 247 248 249
			case 'updateAccount': return this.service.updateAccount(args);
		}
		throw new Error('Invalid call');
	}
}