userDataSyncIpc.ts 9.7 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;
25
			case 'onDidChangeConflicts': return this.service.onDidChangeConflicts;
26
			case 'onDidChangeLocal': return this.service.onDidChangeLocal;
27
			case 'onDidChangeLastSyncTime': return this.service.onDidChangeLastSyncTime;
S
Sandeep Somavarapu 已提交
28
			case 'onSyncErrors': return this.service.onSyncErrors;
29 30 31 32
		}
		throw new Error(`Event not found: ${event}`);
	}

S
Sandeep Somavarapu 已提交
33 34 35 36 37 38
	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);
39
			throw e;
S
Sandeep Somavarapu 已提交
40 41 42 43
		}
	}

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

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

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

65
	private async createManualSyncTask(): Promise<{ id: string, manifest: IUserDataManifest | null }> {
66 67
		const manualSyncTask = await this.service.createManualSyncTask();
		const manualSyncTaskChannel = new ManualSyncTaskChannel(manualSyncTask);
68 69
		this.server.registerChannel(`manualSyncTask-${manualSyncTask.id}`, manualSyncTaskChannel);
		return { id: manualSyncTask.id, manifest: manualSyncTask.manifest };
70 71 72 73 74 75 76 77
	}
}

class ManualSyncTaskChannel implements IServerChannel {

	constructor(private readonly manualSyncTask: IManualSyncTask) { }

	listen(_: unknown, event: string): Event<any> {
78 79 80
		switch (event) {
			case 'onSynchronizeResources': return this.manualSyncTask.onSynchronizeResources;
		}
81 82 83 84 85 86 87
		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]);
88
			case 'merge': return this.manualSyncTask.merge(URI.revive(args[0]));
89 90 91
			case 'pull': return this.manualSyncTask.pull();
			case 'push': return this.manualSyncTask.push();
			case 'stop': return this.manualSyncTask.stop();
92
			case 'dispose': return this.manualSyncTask.dispose();
93 94 95 96
		}
		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 105 106
		switch (event) {
			case 'onError': return this.service.onError;
		}
S
Sandeep Somavarapu 已提交
107 108 109 110 111
		throw new Error(`Event not found: ${event}`);
	}

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

S
Sandeep Somavarapu 已提交
120 121 122 123 124 125 126 127 128 129
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 已提交
130
			case 'resolveDefaultIgnoredSettings': return this.service.resolveDefaultIgnoredSettings();
S
Sandeep Somavarapu 已提交
131 132 133 134 135 136 137 138 139
			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 {

140
	declare readonly _serviceBrand: undefined;
S
Sandeep Somavarapu 已提交
141 142 143 144

	constructor(private readonly channel: IChannel) {
	}

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

S
Sandeep Somavarapu 已提交
149 150 151 152 153 154 155 156 157 158
	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]);
	}

}

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
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 {

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

	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 已提交
206 207 208 209 210 211 212 213 214 215 216 217

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();
218
			case 'addCurrentMachine': return this.service.addCurrentMachine();
S
Sandeep Somavarapu 已提交
219 220
			case 'removeCurrentMachine': return this.service.removeCurrentMachine();
			case 'renameMachine': return this.service.renameMachine(args[0], args[1]);
221
			case 'setEnablement': return this.service.setEnablement(args[0], args[1]);
S
Sandeep Somavarapu 已提交
222 223 224 225 226
		}
		throw new Error('Invalid call');
	}

}
227 228 229 230 231 232

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

	listen(_: unknown, event: string): Event<any> {
		switch (event) {
S
Sandeep Somavarapu 已提交
233
			case 'onDidChangeAccount': return this.service.onDidChangeAccount;
234 235 236 237 238 239 240
			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 已提交
241
			case '_getInitialData': return Promise.resolve(this.service.account);
242 243 244 245 246 247
			case 'updateAccount': return this.service.updateAccount(args);
		}
		throw new Error('Invalid call');
	}
}