@@ -15,9 +15,13 @@ import { IProductService } from 'vs/platform/product/common/productService';
1515import { getServiceMachineId } from 'vs/platform/serviceMachineId/common/serviceMachineId' ;
1616import { IEnvironmentService } from 'vs/platform/environment/common/environment' ;
1717import { 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' ;
1919import { 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
2226export 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}
0 commit comments