-
Notifications
You must be signed in to change notification settings - Fork 27.9k
/
extensionsProfileScannerService.ts
388 lines (347 loc) · 15.9 KB
/
extensionsProfileScannerService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Queue } from 'vs/base/common/async';
import { VSBuffer } from 'vs/base/common/buffer';
import { Disposable } from 'vs/base/common/lifecycle';
import { Emitter, Event } from 'vs/base/common/event';
import { ResourceMap } from 'vs/base/common/map';
import { URI, UriComponents } from 'vs/base/common/uri';
import { Metadata } from 'vs/platform/extensionManagement/common/extensionManagement';
import { areSameExtensions } from 'vs/platform/extensionManagement/common/extensionManagementUtil';
import { IExtension, IExtensionIdentifier, isIExtensionIdentifier } from 'vs/platform/extensions/common/extensions';
import { FileOperationResult, IFileService, toFileOperationResult } from 'vs/platform/files/common/files';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { ILogService } from 'vs/platform/log/common/log';
import { IUserDataProfilesService } from 'vs/platform/userDataProfile/common/userDataProfile';
import { IUriIdentityService } from 'vs/platform/uriIdentity/common/uriIdentity';
import { Mutable, isObject, isString } from 'vs/base/common/types';
import { getErrorMessage } from 'vs/base/common/errors';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
interface IStoredProfileExtension {
identifier: IExtensionIdentifier;
location: UriComponents | string;
version: string;
metadata?: Metadata;
}
export const enum ExtensionsProfileScanningErrorCode {
/**
* Error when trying to scan extensions from a profile that does not exist.
*/
ERROR_PROFILE_NOT_FOUND = 'ERROR_PROFILE_NOT_FOUND',
/**
* Error when profile file is invalid.
*/
ERROR_INVALID_CONTENT = 'ERROR_INVALID_CONTENT',
}
export class ExtensionsProfileScanningError extends Error {
constructor(message: string, public code: ExtensionsProfileScanningErrorCode) {
super(message);
}
}
export interface IScannedProfileExtension {
readonly identifier: IExtensionIdentifier;
readonly version: string;
readonly location: URI;
readonly metadata?: Metadata;
}
export interface ProfileExtensionsEvent {
readonly extensions: readonly IScannedProfileExtension[];
readonly profileLocation: URI;
}
export interface DidAddProfileExtensionsEvent extends ProfileExtensionsEvent {
readonly error?: Error;
}
export interface DidRemoveProfileExtensionsEvent extends ProfileExtensionsEvent {
readonly error?: Error;
}
export interface IProfileExtensionsScanOptions {
readonly bailOutWhenFileNotFound?: boolean;
}
export const IExtensionsProfileScannerService = createDecorator<IExtensionsProfileScannerService>('IExtensionsProfileScannerService');
export interface IExtensionsProfileScannerService {
readonly _serviceBrand: undefined;
readonly onAddExtensions: Event<ProfileExtensionsEvent>;
readonly onDidAddExtensions: Event<DidAddProfileExtensionsEvent>;
readonly onRemoveExtensions: Event<ProfileExtensionsEvent>;
readonly onDidRemoveExtensions: Event<DidRemoveProfileExtensionsEvent>;
scanProfileExtensions(profileLocation: URI, options?: IProfileExtensionsScanOptions): Promise<IScannedProfileExtension[]>;
addExtensionsToProfile(extensions: [IExtension, Metadata | undefined][], profileLocation: URI): Promise<IScannedProfileExtension[]>;
updateMetadata(extensions: [IExtension, Metadata | undefined][], profileLocation: URI): Promise<IScannedProfileExtension[]>;
removeExtensionFromProfile(extension: IExtension, profileLocation: URI): Promise<void>;
}
export abstract class AbstractExtensionsProfileScannerService extends Disposable implements IExtensionsProfileScannerService {
readonly _serviceBrand: undefined;
private readonly _onAddExtensions = this._register(new Emitter<ProfileExtensionsEvent>());
readonly onAddExtensions = this._onAddExtensions.event;
private readonly _onDidAddExtensions = this._register(new Emitter<DidAddProfileExtensionsEvent>());
readonly onDidAddExtensions = this._onDidAddExtensions.event;
private readonly _onRemoveExtensions = this._register(new Emitter<ProfileExtensionsEvent>());
readonly onRemoveExtensions = this._onRemoveExtensions.event;
private readonly _onDidRemoveExtensions = this._register(new Emitter<DidRemoveProfileExtensionsEvent>());
readonly onDidRemoveExtensions = this._onDidRemoveExtensions.event;
private readonly resourcesAccessQueueMap = new ResourceMap<Queue<IScannedProfileExtension[]>>();
constructor(
private readonly extensionsLocation: URI,
@IFileService private readonly fileService: IFileService,
@IUserDataProfilesService private readonly userDataProfilesService: IUserDataProfilesService,
@IUriIdentityService private readonly uriIdentityService: IUriIdentityService,
@ITelemetryService private readonly telemetryService: ITelemetryService,
@ILogService private readonly logService: ILogService,
) {
super();
}
scanProfileExtensions(profileLocation: URI, options?: IProfileExtensionsScanOptions): Promise<IScannedProfileExtension[]> {
return this.withProfileExtensions(profileLocation, undefined, options);
}
async addExtensionsToProfile(extensions: [IExtension, Metadata | undefined][], profileLocation: URI): Promise<IScannedProfileExtension[]> {
const extensionsToRemove: IScannedProfileExtension[] = [];
const extensionsToAdd: IScannedProfileExtension[] = [];
try {
await this.withProfileExtensions(profileLocation, profileExtensions => {
const result: IScannedProfileExtension[] = [];
for (const extension of profileExtensions) {
if (extensions.some(([e]) => areSameExtensions(e.identifier, extension.identifier) && e.manifest.version !== extension.version)) {
// Remove the existing extension with different version
extensionsToRemove.push(extension);
} else {
result.push(extension);
}
}
for (const [extension, metadata] of extensions) {
if (!result.some(e => areSameExtensions(e.identifier, extension.identifier) && e.version === extension.manifest.version)) {
// Add only if the same version of the extension is not already added
const extensionToAdd = { identifier: extension.identifier, version: extension.manifest.version, location: extension.location, metadata };
extensionsToAdd.push(extensionToAdd);
result.push(extensionToAdd);
}
}
if (extensionsToAdd.length) {
this._onAddExtensions.fire({ extensions: extensionsToAdd, profileLocation });
}
if (extensionsToRemove.length) {
this._onRemoveExtensions.fire({ extensions: extensionsToRemove, profileLocation });
}
return result;
});
if (extensionsToAdd.length) {
this._onDidAddExtensions.fire({ extensions: extensionsToAdd, profileLocation });
}
if (extensionsToRemove.length) {
this._onDidRemoveExtensions.fire({ extensions: extensionsToRemove, profileLocation });
}
return extensionsToAdd;
} catch (error) {
if (extensionsToAdd.length) {
this._onDidAddExtensions.fire({ extensions: extensionsToAdd, error, profileLocation });
}
if (extensionsToRemove.length) {
this._onDidRemoveExtensions.fire({ extensions: extensionsToRemove, error, profileLocation });
}
throw error;
}
}
async updateMetadata(extensions: [IExtension, Metadata][], profileLocation: URI): Promise<IScannedProfileExtension[]> {
const updatedExtensions: IScannedProfileExtension[] = [];
await this.withProfileExtensions(profileLocation, profileExtensions => {
const result: IScannedProfileExtension[] = [];
for (const profileExtension of profileExtensions) {
const extension = extensions.find(([e]) => areSameExtensions(e.identifier, profileExtension.identifier) && e.manifest.version === profileExtension.version);
if (extension) {
profileExtension.metadata = { ...profileExtension.metadata, ...extension[1] };
updatedExtensions.push(profileExtension);
result.push(profileExtension);
} else {
result.push(profileExtension);
}
}
return result;
});
return updatedExtensions;
}
async removeExtensionFromProfile(extension: IExtension, profileLocation: URI): Promise<void> {
const extensionsToRemove: IScannedProfileExtension[] = [];
this._onRemoveExtensions.fire({ extensions: extensionsToRemove, profileLocation });
try {
await this.withProfileExtensions(profileLocation, profileExtensions => {
const result: IScannedProfileExtension[] = [];
for (const e of profileExtensions) {
if (areSameExtensions(e.identifier, extension.identifier)) {
extensionsToRemove.push(e);
} else {
result.push(e);
}
}
if (extensionsToRemove.length) {
this._onRemoveExtensions.fire({ extensions: extensionsToRemove, profileLocation });
}
return result;
});
if (extensionsToRemove.length) {
this._onDidRemoveExtensions.fire({ extensions: extensionsToRemove, profileLocation });
}
} catch (error) {
if (extensionsToRemove.length) {
this._onDidRemoveExtensions.fire({ extensions: extensionsToRemove, error, profileLocation });
}
throw error;
}
}
private async withProfileExtensions(file: URI, updateFn?: (extensions: Mutable<IScannedProfileExtension>[]) => IScannedProfileExtension[], options?: IProfileExtensionsScanOptions): Promise<IScannedProfileExtension[]> {
return this.getResourceAccessQueue(file).queue(async () => {
let extensions: IScannedProfileExtension[] = [];
// Read
let storedProfileExtensions;
try {
const content = await this.fileService.readFile(file);
storedProfileExtensions = JSON.parse(content.value.toString());
} catch (error) {
if (toFileOperationResult(error) !== FileOperationResult.FILE_NOT_FOUND) {
throw error;
}
// migrate from old location, remove this after couple of releases
if (this.uriIdentityService.extUri.isEqual(file, this.userDataProfilesService.defaultProfile.extensionsResource)) {
storedProfileExtensions = await this.migrateFromOldDefaultProfileExtensionsLocation();
}
if (!storedProfileExtensions && options?.bailOutWhenFileNotFound) {
throw new ExtensionsProfileScanningError(getErrorMessage(error), ExtensionsProfileScanningErrorCode.ERROR_PROFILE_NOT_FOUND);
}
}
if (storedProfileExtensions) {
if (!Array.isArray(storedProfileExtensions)) {
this.reportAndThrowInvalidConentError(file);
}
// TODO @sandy081: Remove this migration after couple of releases
let migrate = false;
for (const e of storedProfileExtensions) {
if (!isStoredProfileExtension(e)) {
this.reportAndThrowInvalidConentError(file);
}
let location: URI;
if (isString(e.location)) {
location = this.resolveExtensionLocation(e.location);
} else {
location = URI.revive(e.location);
const relativePath = this.toRelativePath(location);
if (relativePath) {
migrate = true;
e.location = relativePath;
}
}
extensions.push({
identifier: e.identifier,
location,
version: e.version,
metadata: e.metadata,
});
}
if (migrate) {
await this.fileService.writeFile(file, VSBuffer.fromString(JSON.stringify(storedProfileExtensions)));
}
}
// Update
if (updateFn) {
extensions = updateFn(extensions);
const storedProfileExtensions: IStoredProfileExtension[] = extensions.map(e => ({
identifier: e.identifier,
version: e.version,
location: this.toRelativePath(e.location) ?? e.location.toJSON(),
metadata: e.metadata
}));
await this.fileService.writeFile(file, VSBuffer.fromString(JSON.stringify(storedProfileExtensions)));
}
return extensions;
});
}
private reportAndThrowInvalidConentError(file: URI): void {
type ErrorClassification = {
owner: 'sandy081';
comment: 'Information about the error that occurred while scanning';
code: { classification: 'SystemMetaData'; purpose: 'FeatureInsight'; isMeasurement: true; comment: 'error code' };
};
const error = new ExtensionsProfileScanningError(`Invalid extensions content in ${file.toString()}`, ExtensionsProfileScanningErrorCode.ERROR_INVALID_CONTENT);
this.telemetryService.publicLogError2<{ code: string }, ErrorClassification>('extensionsProfileScanningError', { code: error.code });
throw error;
}
private toRelativePath(extensionLocation: URI): string | undefined {
return this.uriIdentityService.extUri.isEqualOrParent(extensionLocation, this.extensionsLocation)
? this.uriIdentityService.extUri.relativePath(this.extensionsLocation, extensionLocation)
: undefined;
}
private resolveExtensionLocation(path: string): URI {
return this.uriIdentityService.extUri.joinPath(this.extensionsLocation, path);
}
private _migrationPromise: Promise<IStoredProfileExtension[] | undefined> | undefined;
private async migrateFromOldDefaultProfileExtensionsLocation(): Promise<IStoredProfileExtension[] | undefined> {
if (!this._migrationPromise) {
this._migrationPromise = (async () => {
const oldDefaultProfileExtensionsLocation = this.uriIdentityService.extUri.joinPath(this.userDataProfilesService.defaultProfile.location, 'extensions.json');
let content: string;
try {
content = (await this.fileService.readFile(oldDefaultProfileExtensionsLocation)).value.toString();
} catch (error) {
if (toFileOperationResult(error) === FileOperationResult.FILE_NOT_FOUND) {
return undefined;
}
throw error;
}
this.logService.info('Migrating extensions from old default profile location', oldDefaultProfileExtensionsLocation.toString());
let storedProfileExtensions: IStoredProfileExtension[] | undefined;
try {
const parsedData = JSON.parse(content);
if (Array.isArray(parsedData) && parsedData.every(candidate => isStoredProfileExtension(candidate))) {
storedProfileExtensions = parsedData;
} else {
this.logService.warn('Skipping migrating from old default profile locaiton: Found invalid data', parsedData);
}
} catch (error) {
/* Ignore */
this.logService.error(error);
}
if (storedProfileExtensions) {
try {
await this.fileService.createFile(this.userDataProfilesService.defaultProfile.extensionsResource, VSBuffer.fromString(JSON.stringify(storedProfileExtensions)), { overwrite: false });
this.logService.info('Migrated extensions from old default profile location to new location', oldDefaultProfileExtensionsLocation.toString(), this.userDataProfilesService.defaultProfile.extensionsResource.toString());
} catch (error) {
if (toFileOperationResult(error) === FileOperationResult.FILE_MODIFIED_SINCE) {
this.logService.info('Migration from old default profile location to new location is done by another window', oldDefaultProfileExtensionsLocation.toString(), this.userDataProfilesService.defaultProfile.extensionsResource.toString());
} else {
throw error;
}
}
}
try {
await this.fileService.del(oldDefaultProfileExtensionsLocation);
} catch (error) {
if (toFileOperationResult(error) !== FileOperationResult.FILE_NOT_FOUND) {
this.logService.error(error);
}
}
return storedProfileExtensions;
})();
}
return this._migrationPromise;
}
private getResourceAccessQueue(file: URI): Queue<IScannedProfileExtension[]> {
let resourceQueue = this.resourcesAccessQueueMap.get(file);
if (!resourceQueue) {
resourceQueue = new Queue<IScannedProfileExtension[]>();
this.resourcesAccessQueueMap.set(file, resourceQueue);
}
return resourceQueue;
}
}
function isStoredProfileExtension(candidate: any): candidate is IStoredProfileExtension {
return isObject(candidate)
&& isIExtensionIdentifier(candidate.identifier)
&& (isUriComponents(candidate.location) || isString(candidate.location))
&& candidate.version && isString(candidate.version);
}
function isUriComponents(thing: unknown): thing is UriComponents {
if (!thing) {
return false;
}
return isString((<any>thing).path) &&
isString((<any>thing).scheme);
}