mirror of
https://github.com/bitwarden/browser
synced 2025-12-12 06:13:38 +00:00
share disk cache between contexts on browser
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { BehaviorSubject, Observable } from "rxjs";
|
import { BehaviorSubject } from "rxjs";
|
||||||
|
|
||||||
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
|
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
|
||||||
import { StateMigrationService } from "@bitwarden/common/platform/abstractions/state-migration.service";
|
import { StateMigrationService } from "@bitwarden/common/platform/abstractions/state-migration.service";
|
||||||
@@ -44,7 +44,7 @@ export class BrowserStateService
|
|||||||
stateMigrationService: StateMigrationService,
|
stateMigrationService: StateMigrationService,
|
||||||
stateFactory: StateFactory<GlobalState, Account>,
|
stateFactory: StateFactory<GlobalState, Account>,
|
||||||
useAccountCache = true,
|
useAccountCache = true,
|
||||||
accountCache: Observable<Record<string, Account>> = null
|
accountCache: BehaviorSubject<Record<string, Account>> = null
|
||||||
) {
|
) {
|
||||||
super(
|
super(
|
||||||
storageService,
|
storageService,
|
||||||
@@ -59,7 +59,7 @@ export class BrowserStateService
|
|||||||
// Hack to allow shared disk cache between contexts on browser
|
// Hack to allow shared disk cache between contexts on browser
|
||||||
// TODO: Remove when services are consolidated to a single context
|
// TODO: Remove when services are consolidated to a single context
|
||||||
if (useAccountCache && accountCache) {
|
if (useAccountCache && accountCache) {
|
||||||
accountCache.subscribe(this.accountDiskCacheSubject);
|
this.accountDiskCache = accountCache;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -444,7 +444,7 @@ function getBgService<T>(service: keyof MainBackground) {
|
|||||||
// TODO: we need to figure out a better way of sharing/syncing
|
// TODO: we need to figure out a better way of sharing/syncing
|
||||||
// the disk cache
|
// the disk cache
|
||||||
const bgStateService = getBgService<StateServiceAbstraction>("stateService");
|
const bgStateService = getBgService<StateServiceAbstraction>("stateService");
|
||||||
const bgDiskCache = bgStateService().accountDiskCache$;
|
const bgDiskCache = bgStateService().accountDiskCache;
|
||||||
return new BrowserStateService(
|
return new BrowserStateService(
|
||||||
storageService,
|
storageService,
|
||||||
secureStorageService,
|
secureStorageService,
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { Observable } from "rxjs";
|
import { BehaviorSubject, Observable } from "rxjs";
|
||||||
|
|
||||||
import { EncryptedOrganizationKeyData } from "../../admin-console/models/data/encrypted-organization-key.data";
|
import { EncryptedOrganizationKeyData } from "../../admin-console/models/data/encrypted-organization-key.data";
|
||||||
import { OrganizationData } from "../../admin-console/models/data/organization.data";
|
import { OrganizationData } from "../../admin-console/models/data/organization.data";
|
||||||
@@ -37,7 +37,8 @@ export abstract class StateService<T extends Account = Account> {
|
|||||||
accounts$: Observable<{ [userId: string]: T }>;
|
accounts$: Observable<{ [userId: string]: T }>;
|
||||||
activeAccount$: Observable<string>;
|
activeAccount$: Observable<string>;
|
||||||
activeAccountUnlocked$: Observable<boolean>;
|
activeAccountUnlocked$: Observable<boolean>;
|
||||||
accountDiskCache$: Observable<Record<string, T>>;
|
// eslint-disable-next-line rxjs/no-exposed-subjects
|
||||||
|
accountDiskCache: BehaviorSubject<Record<string, T>>;
|
||||||
|
|
||||||
addAccount: (account: T) => Promise<void>;
|
addAccount: (account: T) => Promise<void>;
|
||||||
setActiveUser: (userId: string) => Promise<void>;
|
setActiveUser: (userId: string) => Promise<void>;
|
||||||
|
|||||||
@@ -95,8 +95,8 @@ export class StateService<
|
|||||||
private hasBeenInited = false;
|
private hasBeenInited = false;
|
||||||
private isRecoveredSession = false;
|
private isRecoveredSession = false;
|
||||||
|
|
||||||
protected accountDiskCacheSubject = new BehaviorSubject<Record<string, TAccount>>({});
|
// eslint-disable-next-line rxjs/no-exposed-subjects
|
||||||
accountDiskCache$ = this.accountDiskCacheSubject.asObservable();
|
accountDiskCache = new BehaviorSubject<Record<string, TAccount>>({});
|
||||||
|
|
||||||
// default account serializer, must be overridden by child class
|
// default account serializer, must be overridden by child class
|
||||||
protected accountDeserializer = Account.fromJSON as (json: Jsonify<TAccount>) => TAccount;
|
protected accountDeserializer = Account.fromJSON as (json: Jsonify<TAccount>) => TAccount;
|
||||||
@@ -2769,7 +2769,7 @@ export class StateService<
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (this.useAccountCache) {
|
if (this.useAccountCache) {
|
||||||
const cachedAccount = this.accountDiskCacheSubject.value[options.userId];
|
const cachedAccount = this.accountDiskCache.value[options.userId];
|
||||||
if (cachedAccount != null) {
|
if (cachedAccount != null) {
|
||||||
return cachedAccount;
|
return cachedAccount;
|
||||||
}
|
}
|
||||||
@@ -3165,15 +3165,15 @@ export class StateService<
|
|||||||
|
|
||||||
private setDiskCache(key: string, value: TAccount, options?: StorageOptions) {
|
private setDiskCache(key: string, value: TAccount, options?: StorageOptions) {
|
||||||
if (this.useAccountCache) {
|
if (this.useAccountCache) {
|
||||||
this.accountDiskCacheSubject.value[key] = value;
|
this.accountDiskCache.value[key] = value;
|
||||||
this.accountDiskCacheSubject.next(this.accountDiskCacheSubject.value);
|
this.accountDiskCache.next(this.accountDiskCache.value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private deleteDiskCache(key: string) {
|
private deleteDiskCache(key: string) {
|
||||||
if (this.useAccountCache) {
|
if (this.useAccountCache) {
|
||||||
delete this.accountDiskCacheSubject.value[key];
|
delete this.accountDiskCache.value[key];
|
||||||
this.accountDiskCacheSubject.next(this.accountDiskCacheSubject.value);
|
this.accountDiskCache.next(this.accountDiskCache.value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user