1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-19 01:33:33 +00:00

[bug] Allow for GlobalState to be extended and modified in clients (#646)

Some clients have unique global setting defaults (and unique global settings)
For example: the web vault defaults to light theme, but most clients with theme support default to system theme.

The current way we handle GlobalState is buried in jslib and not easily extendible in clients.

To fix this, we need to treat GlobalState as a generic in the StateService and StateMigration service and allow for its extension in those methods and anywhere GlobalState is inited.
This commit is contained in:
Addison Beck
2022-01-31 14:33:31 -05:00
committed by GitHub
parent e372bf242b
commit 92a65b7b36
9 changed files with 109 additions and 38 deletions

View File

@@ -20,6 +20,8 @@ import { ThemeType } from "../enums/themeType";
import { EnvironmentUrls } from "../models/domain/environmentUrls";
import { GlobalStateFactory } from "../factories/globalStateFactory";
// Originally (before January 2022) storage was handled as a flat key/value pair store.
// With the move to a typed object for state storage these keys should no longer be in use anywhere outside of this migration.
const v1Keys: { [key: string]: string } = {
@@ -126,10 +128,11 @@ const partialKeys = {
masterKey: "_masterkey",
};
export class StateMigrationService {
export class StateMigrationService<TGlobalState extends GlobalState = GlobalState> {
constructor(
protected storageService: StorageService,
protected secureStorageService: StorageService
protected secureStorageService: StorageService,
protected globalStateFactory: GlobalStateFactory<TGlobalState>
) {}
async needsMigration(): Promise<boolean> {
@@ -174,7 +177,7 @@ export class StateMigrationService {
// 1. Check for an existing storage value from the old storage structure OR
// 2. Check for a value already set by processes that run before migration OR
// 3. Assign the default value
const globals = (await this.get<GlobalState>(keys.global)) ?? new GlobalState();
const globals = (await this.get<GlobalState>(keys.global)) ?? this.globalStateFactory.create();
globals.stateVersion = StateVersion.Two;
globals.environmentUrls =
(await this.get<EnvironmentUrls>(v1Keys.environmentUrls)) ?? globals.environmentUrls;
@@ -438,8 +441,8 @@ export class StateMigrationService {
return this.storageService.save(key, value, this.options);
}
protected async getGlobals(): Promise<GlobalState> {
return await this.get<GlobalState>(keys.global);
protected async getGlobals(): Promise<TGlobalState> {
return await this.get<TGlobalState>(keys.global);
}
protected async getCurrentStateVersion(): Promise<StateVersion> {