1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-26 12:25:20 +01:00

add hack to get around duplicate instances of disk cache on browser

This commit is contained in:
Jacob Fink 2023-06-29 16:50:16 -04:00
parent 7b7fa276be
commit 301028f8db
No known key found for this signature in database
GPG Key ID: C2F7ACF05859D008
2 changed files with 41 additions and 6 deletions

View File

@ -1,5 +1,12 @@
import { BehaviorSubject } from "rxjs";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { StateMigrationService } from "@bitwarden/common/platform/abstractions/state-migration.service";
import {
AbstractStorageService,
AbstractMemoryStorageService,
} from "@bitwarden/common/platform/abstractions/storage.service";
import { StateFactory } from "@bitwarden/common/platform/factories/state-factory";
import { GlobalState } from "@bitwarden/common/platform/models/domain/global-state";
import { StorageOptions } from "@bitwarden/common/platform/models/domain/storage-options";
import { StateService as BaseStateService } from "@bitwarden/common/platform/services/state.service";
@ -26,14 +33,42 @@ export class BrowserStateService
protected activeAccountSubject: BehaviorSubject<string>;
@sessionSync({ initializer: (b: boolean) => b })
protected activeAccountUnlockedSubject: BehaviorSubject<boolean>;
@sessionSync({
initializer: Account.fromJSON as any, // TODO: Remove this any when all any types are removed from Account
initializeAs: "record",
})
protected accountDiskCache: BehaviorSubject<Record<string, Account>>;
protected accountDeserializer = Account.fromJSON;
constructor(
storageService: AbstractStorageService,
secureStorageService: AbstractStorageService,
memoryStorageService: AbstractMemoryStorageService,
logService: LogService,
stateMigrationService: StateMigrationService,
stateFactory: StateFactory<GlobalState, Account>,
useAccountCache = true
) {
super(
storageService,
secureStorageService,
memoryStorageService,
logService,
stateMigrationService,
stateFactory,
useAccountCache
);
// TODO: This is a hack to fix having a disk cache on both the popup and
// the background page that can get out of sync. We need to have a single
// instance of our state service that is shared so we can remove this.
chrome.storage.onChanged.addListener((changes, namespace) => {
if (namespace === "local") {
for (const key of Object.keys(changes)) {
if (key !== "accountActivity" && this.accountDiskCache.value[key]) {
this.deleteDiskCache(key);
}
}
}
});
}
async addAccount(account: Account) {
// Apply browser overrides to default account values
account = new Account(account);

View File

@ -3169,7 +3169,7 @@ export class StateService<
}
}
private deleteDiskCache(key: string) {
protected deleteDiskCache(key: string) {
if (this.useAccountCache) {
delete this.accountDiskCache.value[key];
this.accountDiskCache.next(this.accountDiskCache.value);