2023-01-12 21:39:33 +01:00
|
|
|
import {
|
|
|
|
AbstractMemoryStorageService,
|
|
|
|
AbstractStorageService,
|
|
|
|
} from "@bitwarden/common/abstractions/storage.service";
|
2022-08-18 16:23:16 +02:00
|
|
|
import { MemoryStorageService } from "@bitwarden/common/services/memoryStorage.service";
|
|
|
|
|
2022-10-19 15:55:57 +02:00
|
|
|
import { BrowserApi } from "../../browser/browserApi";
|
2022-08-18 16:23:16 +02:00
|
|
|
import BrowserLocalStorageService from "../../services/browserLocalStorage.service";
|
|
|
|
import { LocalBackedSessionStorageService } from "../../services/localBackedSessionStorage.service";
|
|
|
|
|
|
|
|
import { encryptServiceFactory, EncryptServiceInitOptions } from "./encrypt-service.factory";
|
|
|
|
import { CachedServices, factory, FactoryOptions } from "./factory-options";
|
|
|
|
import {
|
|
|
|
keyGenerationServiceFactory,
|
|
|
|
KeyGenerationServiceInitOptions,
|
|
|
|
} from "./key-generation-service.factory";
|
|
|
|
|
|
|
|
type StorageServiceFactoryOptions = FactoryOptions;
|
|
|
|
|
|
|
|
export type DiskStorageServiceInitOptions = StorageServiceFactoryOptions;
|
|
|
|
export type SecureStorageServiceInitOptions = StorageServiceFactoryOptions;
|
|
|
|
export type MemoryStorageServiceInitOptions = StorageServiceFactoryOptions &
|
|
|
|
EncryptServiceInitOptions &
|
|
|
|
KeyGenerationServiceInitOptions;
|
|
|
|
|
|
|
|
export function diskStorageServiceFactory(
|
|
|
|
cache: { diskStorageService?: AbstractStorageService } & CachedServices,
|
|
|
|
opts: DiskStorageServiceInitOptions
|
2022-08-29 20:46:42 +02:00
|
|
|
): Promise<AbstractStorageService> {
|
2022-08-18 16:23:16 +02:00
|
|
|
return factory(cache, "diskStorageService", opts, () => new BrowserLocalStorageService());
|
|
|
|
}
|
|
|
|
|
|
|
|
export function secureStorageServiceFactory(
|
|
|
|
cache: { secureStorageService?: AbstractStorageService } & CachedServices,
|
|
|
|
opts: SecureStorageServiceInitOptions
|
2022-08-29 20:46:42 +02:00
|
|
|
): Promise<AbstractStorageService> {
|
2022-08-18 16:23:16 +02:00
|
|
|
return factory(cache, "secureStorageService", opts, () => new BrowserLocalStorageService());
|
|
|
|
}
|
|
|
|
|
|
|
|
export function memoryStorageServiceFactory(
|
2023-01-12 21:39:33 +01:00
|
|
|
cache: { memoryStorageService?: AbstractMemoryStorageService } & CachedServices,
|
2022-08-18 16:23:16 +02:00
|
|
|
opts: MemoryStorageServiceInitOptions
|
2023-01-12 21:39:33 +01:00
|
|
|
): Promise<AbstractMemoryStorageService> {
|
2022-08-29 20:46:42 +02:00
|
|
|
return factory(cache, "memoryStorageService", opts, async () => {
|
2022-10-19 15:55:57 +02:00
|
|
|
if (BrowserApi.manifestVersion === 3) {
|
2022-08-18 16:23:16 +02:00
|
|
|
return new LocalBackedSessionStorageService(
|
2022-08-29 20:46:42 +02:00
|
|
|
await encryptServiceFactory(cache, opts),
|
|
|
|
await keyGenerationServiceFactory(cache, opts)
|
2022-08-18 16:23:16 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return new MemoryStorageService();
|
|
|
|
});
|
|
|
|
}
|