storageIpc.ts 7.1 KB
Newer Older
B
wip  
Benjamin Pasero 已提交
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 { IChannel, IServerChannel } from 'vs/base/parts/ipc/common/ipc';
J
Joao Moreno 已提交
7
import { Event, Emitter } from 'vs/base/common/event';
8 9
import { StorageMainService, IStorageChangeEvent } from 'vs/platform/storage/node/storageMainService';
import { IUpdateRequest, IStorageDatabase, IStorageItemsChangeEvent } from 'vs/base/node/storage';
10
import { mapToSerializable, serializableToMap, values } from 'vs/base/common/map';
11
import { Disposable, IDisposable, dispose } from 'vs/base/common/lifecycle';
12 13 14 15
import { onUnexpectedError } from 'vs/base/common/errors';
import { ILogService } from 'vs/platform/log/common/log';
import { generateUuid } from 'vs/base/common/uuid';
import { instanceStorageKey, firstSessionDateStorageKey, lastSessionDateStorageKey, currentSessionDateStorageKey } from 'vs/platform/telemetry/node/workbenchCommonProperties';
16

17 18 19 20 21 22 23 24 25 26 27
type Key = string;
type Value = string;
type Item = [Key, Value];

interface ISerializableUpdateRequest {
	insert?: Item[];
	delete?: Key[];
}

interface ISerializableItemsChangeEvent {
	items: Item[];
28
}
B
wip  
Benjamin Pasero 已提交
29

30
export class GlobalStorageDatabaseChannel extends Disposable implements IServerChannel {
B
wip  
Benjamin Pasero 已提交
31

32 33
	private static STORAGE_CHANGE_DEBOUNCE_TIME = 100;

M
Matt Bierner 已提交
34
	private readonly _onDidChangeItems: Emitter<ISerializableItemsChangeEvent> = this._register(new Emitter<ISerializableItemsChangeEvent>());
35
	get onDidChangeItems(): Event<ISerializableItemsChangeEvent> { return this._onDidChangeItems.event; }
B
wip  
Benjamin Pasero 已提交
36

37 38 39 40 41 42
	private whenReady: Promise<void>;

	constructor(
		private logService: ILogService,
		private storageMainService: StorageMainService
	) {
43 44
		super();

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
		this.whenReady = this.init();
	}

	private init(): Promise<void> {
		return this.storageMainService.initialize().then(undefined, error => {
			onUnexpectedError(error);
			this.logService.error(error);
		}).then(() => {

			// Apply global telemetry values as part of the initialization
			// These are global across all windows and thereby should be
			// written from the main process once.
			this.initTelemetry();

			// Setup storage change listeners
			this.registerListeners();
		});
	}

	private initTelemetry(): void {
		const instanceId = this.storageMainService.get(instanceStorageKey, undefined);
		if (instanceId === undefined) {
			this.storageMainService.store(instanceStorageKey, generateUuid());
		}

		const firstSessionDate = this.storageMainService.get(firstSessionDateStorageKey, undefined);
		if (firstSessionDate === undefined) {
			this.storageMainService.store(firstSessionDateStorageKey, new Date().toUTCString());
		}

		const lastSessionDate = this.storageMainService.get(currentSessionDateStorageKey, undefined); // previous session date was the "current" one at that time
		const currentSessionDate = new Date().toUTCString(); // current session date is "now"
		this.storageMainService.store(lastSessionDateStorageKey, typeof lastSessionDate === 'undefined' ? null : lastSessionDate);
		this.storageMainService.store(currentSessionDateStorageKey, currentSessionDate);
79 80 81 82 83 84
	}

	private registerListeners(): void {

		// Listen for changes in global storage to send to listeners
		// that are listening. Use a debouncer to reduce IPC traffic.
J
Joao Moreno 已提交
85
		this._register(Event.debounce(this.storageMainService.onDidChangeStorage, (prev: IStorageChangeEvent[], cur: IStorageChangeEvent) => {
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
			if (!prev) {
				prev = [cur];
			} else {
				prev.push(cur);
			}

			return prev;
		}, GlobalStorageDatabaseChannel.STORAGE_CHANGE_DEBOUNCE_TIME)(events => {
			if (events.length) {
				this._onDidChangeItems.fire(this.serializeEvents(events));
			}
		}));
	}

	private serializeEvents(events: IStorageChangeEvent[]): ISerializableItemsChangeEvent {
		const items = new Map<Key, Value>();
B
Benjamin Pasero 已提交
102
		events.forEach(event => items.set(event.key, this.storageMainService.get(event.key)));
103 104

		return { items: mapToSerializable(items) } as ISerializableItemsChangeEvent;
B
wip  
Benjamin Pasero 已提交
105 106
	}

107
	listen(_: unknown, event: string): Event<any> {
108 109 110
		switch (event) {
			case 'onDidChangeItems': return this.onDidChangeItems;
		}
B
wip  
Benjamin Pasero 已提交
111 112 113 114

		throw new Error(`Event not found: ${event}`);
	}

115
	call(_: unknown, command: string, arg?: any): Promise<any> {
B
wip  
Benjamin Pasero 已提交
116 117
		switch (command) {
			case 'getItems': {
118
				return this.whenReady.then(() => mapToSerializable(this.storageMainService.items));
B
wip  
Benjamin Pasero 已提交
119 120 121
			}

			case 'updateItems': {
122 123 124 125 126 127
				return this.whenReady.then(() => {
					const items = arg as ISerializableUpdateRequest;
					if (items.insert) {
						for (const [key, value] of items.insert) {
							this.storageMainService.store(key, value);
						}
128
					}
B
wip  
Benjamin Pasero 已提交
129

130 131 132 133
					if (items.delete) {
						items.delete.forEach(key => this.storageMainService.remove(key));
					}
				});
B
wip  
Benjamin Pasero 已提交
134 135 136
			}

			case 'checkIntegrity': {
137
				return this.whenReady.then(() => this.storageMainService.checkIntegrity(arg));
B
wip  
Benjamin Pasero 已提交
138 139 140 141 142 143 144
			}
		}

		throw new Error(`Call not found: ${command}`);
	}
}

145
export class GlobalStorageDatabaseChannelClient extends Disposable implements IStorageDatabase {
B
wip  
Benjamin Pasero 已提交
146 147 148

	_serviceBrand: any;

M
Matt Bierner 已提交
149
	private readonly _onDidChangeItemsExternal: Emitter<IStorageItemsChangeEvent> = this._register(new Emitter<IStorageItemsChangeEvent>());
150 151
	get onDidChangeItemsExternal(): Event<IStorageItemsChangeEvent> { return this._onDidChangeItemsExternal.event; }

152 153
	private onDidChangeItemsOnMainListener: IDisposable;

154 155
	constructor(private channel: IChannel) {
		super();
B
wip  
Benjamin Pasero 已提交
156

157 158 159 160
		this.registerListeners();
	}

	private registerListeners(): void {
161
		this.onDidChangeItemsOnMainListener = this.channel.listen('onDidChangeItems')((e: ISerializableItemsChangeEvent) => this.onDidChangeItemsOnMain(e));
162 163 164 165 166 167 168
	}

	private onDidChangeItemsOnMain(e: ISerializableItemsChangeEvent): void {
		if (Array.isArray(e.items)) {
			this._onDidChangeItemsExternal.fire({ items: serializableToMap(e.items) });
		}
	}
B
wip  
Benjamin Pasero 已提交
169

J
Johannes Rieken 已提交
170
	getItems(): Promise<Map<string, string>> {
171
		return this.channel.call('getItems').then((data: Item[]) => serializableToMap(data));
B
wip  
Benjamin Pasero 已提交
172 173
	}

J
Johannes Rieken 已提交
174
	updateItems(request: IUpdateRequest): Promise<void> {
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
		let updateCount = 0;
		const serializableRequest: ISerializableUpdateRequest = Object.create(null);

		if (request.insert) {
			serializableRequest.insert = mapToSerializable(request.insert);
			updateCount += request.insert.size;
		}

		if (request.delete) {
			serializableRequest.delete = values(request.delete);
			updateCount += request.delete.size;
		}

		if (updateCount === 0) {
			return Promise.resolve(); // prevent work if not needed
		}

		return this.channel.call('updateItems', serializableRequest);
B
wip  
Benjamin Pasero 已提交
193 194
	}

J
Johannes Rieken 已提交
195
	checkIntegrity(full: boolean): Promise<string> {
B
wip  
Benjamin Pasero 已提交
196 197 198
		return this.channel.call('checkIntegrity', full);
	}

J
Johannes Rieken 已提交
199
	close(): Promise<void> {
200 201 202 203

		// when we are about to close, we start to ignore main-side changes since we close anyway
		this.onDidChangeItemsOnMainListener = dispose(this.onDidChangeItemsOnMainListener);

B
wip  
Benjamin Pasero 已提交
204 205
		return Promise.resolve(); // global storage is closed on the main side
	}
206 207 208 209 210 211

	dispose(): void {
		super.dispose();

		this.onDidChangeItemsOnMainListener = dispose(this.onDidChangeItemsOnMainListener);
	}
B
wip  
Benjamin Pasero 已提交
212
}