Skip to content

Commit a669508

Browse files
committed
Create storage main service in shared process
1 parent bc8aaad commit a669508

2 files changed

Lines changed: 60 additions & 29 deletions

File tree

src/vs/code/electron-browser/sharedProcess/sharedProcessMain.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ import { KeytarCredentialsService } from 'vs/platform/credentials/node/credentia
6262
import { UserDataAutoSync } from 'vs/platform/userDataSync/electron-browser/userDataAutoSync';
6363
import { SettingsSynchroniser } from 'vs/platform/userDataSync/common/settingsSync';
6464
import { UserDataAuthTokenService } from 'vs/platform/userDataSync/common/userDataAuthTokenService';
65+
import { GlobalStorageDatabaseChannelClient } from 'vs/platform/storage/node/storageIpc';
66+
import { IStorageMainService, SimpleStorageMainService } from 'vs/platform/storage/node/storageMainService';
6567

6668
export interface ISharedProcessConfiguration {
6769
readonly machineId: string;
@@ -127,6 +129,10 @@ async function main(server: Server, initData: ISharedProcessInitData, configurat
127129
const mainProcessService = new MainProcessService(server, mainRouter);
128130
services.set(IMainProcessService, mainProcessService);
129131

132+
const storageMainService = new SimpleStorageMainService(new GlobalStorageDatabaseChannelClient(mainProcessService.getChannel('storage')));
133+
await storageMainService.initialize();
134+
services.set(IStorageMainService, storageMainService);
135+
130136
const electronService = createChannelSender<IElectronService>(mainProcessService.getChannel('electron'), { context: configuration.windowId });
131137
services.set(IElectronService, electronService);
132138

src/vs/platform/storage/node/storageMainService.ts

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Disposable } from 'vs/base/common/lifecycle';
99
import { ILogService, LogLevel } from 'vs/platform/log/common/log';
1010
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
1111
import { SQLiteStorageDatabase, ISQLiteStorageDatabaseLoggingOptions } from 'vs/base/parts/storage/node/storage';
12-
import { Storage, IStorage, InMemoryStorageDatabase } from 'vs/base/parts/storage/common/storage';
12+
import { Storage, IStorage, InMemoryStorageDatabase, IStorageDatabase } from 'vs/base/parts/storage/common/storage';
1313
import { join } from 'vs/base/common/path';
1414

1515
export const IStorageMainService = createDecorator<IStorageMainService>('storageMainService');
@@ -83,12 +83,10 @@ export interface IStorageChangeEvent {
8383
key: string;
8484
}
8585

86-
export class StorageMainService extends Disposable implements IStorageMainService {
86+
export abstract class AbstractStorageMainService extends Disposable implements IStorageMainService {
8787

8888
_serviceBrand: undefined;
8989

90-
private static readonly STORAGE_NAME = 'state.vscdb';
91-
9290
private readonly _onDidChangeStorage = this._register(new Emitter<IStorageChangeEvent>());
9391
readonly onDidChangeStorage = this._onDidChangeStorage.event;
9492

@@ -98,34 +96,15 @@ export class StorageMainService extends Disposable implements IStorageMainServic
9896
get items(): Map<string, string> { return this.storage.items; }
9997

10098
private storage: IStorage;
101-
10299
private initializePromise: Promise<void> | undefined;
103100

104-
constructor(
105-
@ILogService private readonly logService: ILogService,
106-
@IEnvironmentService private readonly environmentService: IEnvironmentService
107-
) {
101+
constructor() {
108102
super();
109103

110104
// Until the storage has been initialized, it can only be in memory
111105
this.storage = new Storage(new InMemoryStorageDatabase());
112106
}
113107

114-
private get storagePath(): string {
115-
if (!!this.environmentService.extensionTestsLocationURI) {
116-
return SQLiteStorageDatabase.IN_MEMORY_PATH; // no storage during extension tests!
117-
}
118-
119-
return join(this.environmentService.globalStorageHome, StorageMainService.STORAGE_NAME);
120-
}
121-
122-
private createLogginOptions(): ISQLiteStorageDatabaseLoggingOptions {
123-
return {
124-
logTrace: (this.logService.getLevel() === LogLevel.Trace) ? msg => this.logService.trace(msg) : undefined,
125-
logError: error => this.logService.error(error)
126-
};
127-
}
128-
129108
initialize(): Promise<void> {
130109
if (!this.initializePromise) {
131110
this.initializePromise = this.doInitialize();
@@ -134,12 +113,9 @@ export class StorageMainService extends Disposable implements IStorageMainServic
134113
return this.initializePromise;
135114
}
136115

137-
private doInitialize(): Promise<void> {
116+
private async doInitialize(): Promise<void> {
138117
this.storage.dispose();
139-
this.storage = new Storage(new SQLiteStorageDatabase(this.storagePath, {
140-
logging: this.createLogginOptions()
141-
}));
142-
118+
this.storage = this.createStorage();
143119
this._register(this.storage.onDidChangeStorage(key => this._onDidChangeStorage.fire({ key })));
144120

145121
return this.storage.init();
@@ -179,4 +155,53 @@ export class StorageMainService extends Disposable implements IStorageMainServic
179155
// Do it
180156
return this.storage.close();
181157
}
158+
159+
protected abstract createStorage(): IStorage;
160+
}
161+
162+
export class StorageMainService extends AbstractStorageMainService implements IStorageMainService {
163+
164+
private static readonly STORAGE_NAME = 'state.vscdb';
165+
166+
constructor(
167+
@ILogService private readonly logService: ILogService,
168+
@IEnvironmentService private readonly environmentService: IEnvironmentService
169+
) {
170+
super();
171+
}
172+
173+
private get storagePath(): string {
174+
if (!!this.environmentService.extensionTestsLocationURI) {
175+
return SQLiteStorageDatabase.IN_MEMORY_PATH; // no storage during extension tests!
176+
}
177+
178+
return join(this.environmentService.globalStorageHome, StorageMainService.STORAGE_NAME);
179+
}
180+
181+
protected createStorage(): IStorage {
182+
return new Storage(new SQLiteStorageDatabase(this.storagePath, {
183+
logging: this.createLogginOptions()
184+
}));
185+
}
186+
187+
private createLogginOptions(): ISQLiteStorageDatabaseLoggingOptions {
188+
return {
189+
logTrace: (this.logService.getLevel() === LogLevel.Trace) ? msg => this.logService.trace(msg) : undefined,
190+
logError: error => this.logService.error(error)
191+
};
192+
}
193+
}
194+
195+
export class SimpleStorageMainService extends AbstractStorageMainService implements IStorageMainService {
196+
197+
constructor(
198+
private database: IStorageDatabase
199+
) {
200+
super();
201+
}
202+
203+
protected createStorage(): IStorage {
204+
return new Storage(this.database);
205+
}
206+
182207
}

0 commit comments

Comments
 (0)