Skip to content

Commit 5dbaaab

Browse files
committed
1 parent 47952d5 commit 5dbaaab

3 files changed

Lines changed: 379 additions & 7 deletions

File tree

src/vs/platform/userDataSync/common/userDataSyncStoreService.ts

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ import { IProductService } from 'vs/platform/product/common/productService';
1515
import { getServiceMachineId } from 'vs/platform/serviceMachineId/common/serviceMachineId';
1616
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
1717
import { IFileService } from 'vs/platform/files/common/files';
18-
import { IStorageService } from 'vs/platform/storage/common/storage';
18+
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
1919
import { assign } from 'vs/base/common/objects';
20+
import { generateUuid } from 'vs/base/common/uuid';
21+
import { isWeb } from 'vs/base/common/platform';
2022

23+
const USER_SESSION_ID_KEY = 'sync.user-session-id';
24+
const MACHINE_SESSION_ID_KEY = 'sync.machine-session-id';
2125

2226
export class UserDataSyncStoreService extends Disposable implements IUserDataSyncStoreService {
2327

@@ -34,16 +38,17 @@ export class UserDataSyncStoreService extends Disposable implements IUserDataSyn
3438
@IUserDataSyncLogService private readonly logService: IUserDataSyncLogService,
3539
@IEnvironmentService environmentService: IEnvironmentService,
3640
@IFileService fileService: IFileService,
37-
@IStorageService storageService: IStorageService,
41+
@IStorageService private readonly storageService: IStorageService,
3842
) {
3943
super();
4044
this.userDataSyncStore = getUserDataSyncStore(productService, configurationService);
4145
this.commonHeadersPromise = getServiceMachineId(environmentService, fileService, storageService)
4246
.then(uuid => {
4347
const headers: IHeaders = {
44-
'X-Sync-Client-Id': productService.version,
48+
'X-Client-Name': `${productService.applicationName}${isWeb ? '-web' : ''}`,
49+
'X-Client-Version': productService.version,
50+
'X-Machine-Id': uuid
4551
};
46-
headers['X-Sync-Machine-Id'] = uuid;
4752
return headers;
4853
});
4954
}
@@ -169,7 +174,25 @@ export class UserDataSyncStoreService extends Disposable implements IUserDataSyn
169174
throw new UserDataSyncStoreError('Server returned ' + context.res.statusCode, UserDataSyncErrorCode.Unknown);
170175
}
171176

172-
return asJson(context);
177+
const manifest = await asJson<IUserDataManifest>(context);
178+
const currentSessionId = this.storageService.get(USER_SESSION_ID_KEY, StorageScope.GLOBAL);
179+
180+
if (currentSessionId && manifest && currentSessionId !== manifest.session) {
181+
// Server session is different from client session so clear cached session.
182+
this.clearSession();
183+
}
184+
185+
if (manifest === null && currentSessionId) {
186+
// server session is cleared so clear cached session.
187+
this.clearSession();
188+
}
189+
190+
if (manifest) {
191+
// update session
192+
this.storageService.store(USER_SESSION_ID_KEY, manifest.session, StorageScope.GLOBAL);
193+
}
194+
195+
return manifest;
173196
}
174197

175198
async clear(): Promise<void> {
@@ -185,6 +208,14 @@ export class UserDataSyncStoreService extends Disposable implements IUserDataSyn
185208
if (!isSuccess(context)) {
186209
throw new UserDataSyncStoreError('Server returned ' + context.res.statusCode, UserDataSyncErrorCode.Unknown);
187210
}
211+
212+
// clear cached session.
213+
this.clearSession();
214+
}
215+
216+
private clearSession(): void {
217+
this.storageService.remove(USER_SESSION_ID_KEY, StorageScope.GLOBAL);
218+
this.storageService.remove(MACHINE_SESSION_ID_KEY, StorageScope.GLOBAL);
188219
}
189220

190221
private async request(options: IRequestOptions, source: SyncResource | undefined, token: CancellationToken): Promise<IRequestContext> {
@@ -199,6 +230,9 @@ export class UserDataSyncStoreService extends Disposable implements IUserDataSyn
199230
'authorization': `Bearer ${authToken.token}`,
200231
});
201232

233+
// Add session headers
234+
this.addSessionHeaders(options.headers);
235+
202236
this.logService.trace('Sending request to server', { url: options.url, type: options.type, headers: { ...options.headers, ...{ authorization: undefined } } });
203237

204238
let context;
@@ -229,4 +263,18 @@ export class UserDataSyncStoreService extends Disposable implements IUserDataSyn
229263
return context;
230264
}
231265

266+
private addSessionHeaders(headers: IHeaders): void {
267+
let machineSessionId = this.storageService.get(MACHINE_SESSION_ID_KEY, StorageScope.GLOBAL);
268+
if (machineSessionId === undefined) {
269+
machineSessionId = generateUuid();
270+
this.storageService.store(MACHINE_SESSION_ID_KEY, machineSessionId, StorageScope.GLOBAL);
271+
}
272+
headers['X-Machine-Session-Id'] = machineSessionId;
273+
274+
const userSessionId = this.storageService.get(USER_SESSION_ID_KEY, StorageScope.GLOBAL);
275+
if (userSessionId !== undefined) {
276+
headers['X-User-Session-Id'] = userSessionId;
277+
}
278+
}
279+
232280
}

src/vs/platform/userDataSync/test/common/userDataSyncClient.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,12 @@ export class UserDataSyncTestServer implements IRequestService {
141141
private _requests: { url: string, type: string, headers?: IHeaders }[] = [];
142142
get requests(): { url: string, type: string, headers?: IHeaders }[] { return this._requests; }
143143

144+
private _requestsWithAllHeaders: { url: string, type: string, headers?: IHeaders }[] = [];
145+
get requestsWithAllHeaders(): { url: string, type: string, headers?: IHeaders }[] { return this._requestsWithAllHeaders; }
146+
144147
private _responses: { status: number }[] = [];
145148
get responses(): { status: number }[] { return this._responses; }
146-
reset(): void { this._requests = []; this._responses = []; }
149+
reset(): void { this._requests = []; this._responses = []; this._requestsWithAllHeaders = []; }
147150

148151
async resolveProxy(url: string): Promise<string | undefined> { return url; }
149152

@@ -158,6 +161,7 @@ export class UserDataSyncTestServer implements IRequestService {
158161
}
159162
}
160163
this._requests.push({ url: options.url!, type: options.type!, headers });
164+
this._requestsWithAllHeaders.push({ url: options.url!, type: options.type!, headers: options.headers });
161165
const requestContext = await this.doRequest(options);
162166
this._responses.push({ status: requestContext.res.statusCode! });
163167
return requestContext;
@@ -224,7 +228,7 @@ export class UserDataSyncTestServer implements IRequestService {
224228
return this.toResponse(204);
225229
}
226230

227-
private async clear(headers?: IHeaders): Promise<IRequestContext> {
231+
async clear(headers?: IHeaders): Promise<IRequestContext> {
228232
this.data.clear();
229233
this.session = null;
230234
return this.toResponse(204);

0 commit comments

Comments
 (0)