2022-06-14 17:10:53 +02:00
|
|
|
import { GlobalState } from "@bitwarden/common/models/domain/globalState";
|
|
|
|
import { StorageOptions } from "@bitwarden/common/models/domain/storageOptions";
|
|
|
|
import { StateService as BaseStateService } from "@bitwarden/common/services/state.service";
|
2022-01-27 22:22:51 +01:00
|
|
|
|
|
|
|
import { Account } from "../models/account";
|
|
|
|
import { BrowserComponentState } from "../models/browserComponentState";
|
|
|
|
import { BrowserGroupingsComponentState } from "../models/browserGroupingsComponentState";
|
|
|
|
import { BrowserSendComponentState } from "../models/browserSendComponentState";
|
2022-02-24 18:14:04 +01:00
|
|
|
|
2022-01-27 22:22:51 +01:00
|
|
|
import { StateService as StateServiceAbstraction } from "./abstractions/state.service";
|
|
|
|
|
2022-02-03 19:55:06 +01:00
|
|
|
export class StateService
|
|
|
|
extends BaseStateService<GlobalState, Account>
|
|
|
|
implements StateServiceAbstraction
|
|
|
|
{
|
2022-01-27 22:22:51 +01:00
|
|
|
async addAccount(account: Account) {
|
|
|
|
// Apply browser overrides to default account values
|
|
|
|
account = new Account(account);
|
|
|
|
await super.addAccount(account);
|
|
|
|
}
|
|
|
|
|
2022-02-15 23:06:35 +01:00
|
|
|
async getIsAuthenticated(options?: StorageOptions): Promise<boolean> {
|
|
|
|
// Firefox Private Mode can clash with non-Private Mode because they both read from the same onDiskOptions
|
|
|
|
// Check that there is an account in memory before considering the user authenticated
|
|
|
|
return (
|
|
|
|
(await super.getIsAuthenticated(options)) &&
|
|
|
|
(await this.getAccount(this.defaultInMemoryOptions)) != null
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-27 22:22:51 +01:00
|
|
|
async getBrowserGroupingComponentState(
|
|
|
|
options?: StorageOptions
|
|
|
|
): Promise<BrowserGroupingsComponentState> {
|
|
|
|
return (await this.getAccount(this.reconcileOptions(options, this.defaultInMemoryOptions)))
|
|
|
|
?.groupings;
|
|
|
|
}
|
|
|
|
|
|
|
|
async setBrowserGroupingComponentState(
|
|
|
|
value: BrowserGroupingsComponentState,
|
|
|
|
options?: StorageOptions
|
|
|
|
): Promise<void> {
|
|
|
|
const account = await this.getAccount(
|
|
|
|
this.reconcileOptions(options, this.defaultInMemoryOptions)
|
|
|
|
);
|
|
|
|
account.groupings = value;
|
|
|
|
await this.saveAccount(account, this.reconcileOptions(options, this.defaultInMemoryOptions));
|
|
|
|
}
|
|
|
|
|
|
|
|
async getBrowserCipherComponentState(options?: StorageOptions): Promise<BrowserComponentState> {
|
|
|
|
return (await this.getAccount(this.reconcileOptions(options, this.defaultInMemoryOptions)))
|
|
|
|
?.ciphers;
|
|
|
|
}
|
|
|
|
|
|
|
|
async setBrowserCipherComponentState(
|
|
|
|
value: BrowserComponentState,
|
|
|
|
options?: StorageOptions
|
|
|
|
): Promise<void> {
|
|
|
|
const account = await this.getAccount(
|
|
|
|
this.reconcileOptions(options, this.defaultInMemoryOptions)
|
|
|
|
);
|
|
|
|
account.ciphers = value;
|
|
|
|
await this.saveAccount(account, this.reconcileOptions(options, this.defaultInMemoryOptions));
|
|
|
|
}
|
|
|
|
|
|
|
|
async getBrowserSendComponentState(options?: StorageOptions): Promise<BrowserSendComponentState> {
|
|
|
|
return (await this.getAccount(this.reconcileOptions(options, this.defaultInMemoryOptions)))
|
|
|
|
?.send;
|
|
|
|
}
|
|
|
|
|
|
|
|
async setBrowserSendComponentState(
|
|
|
|
value: BrowserSendComponentState,
|
|
|
|
options?: StorageOptions
|
|
|
|
): Promise<void> {
|
|
|
|
const account = await this.getAccount(
|
|
|
|
this.reconcileOptions(options, this.defaultInMemoryOptions)
|
|
|
|
);
|
|
|
|
account.send = value;
|
|
|
|
await this.saveAccount(account, this.reconcileOptions(options, this.defaultInMemoryOptions));
|
|
|
|
}
|
|
|
|
async getBrowserSendTypeComponentState(options?: StorageOptions): Promise<BrowserComponentState> {
|
|
|
|
return (await this.getAccount(this.reconcileOptions(options, this.defaultInMemoryOptions)))
|
|
|
|
?.sendType;
|
|
|
|
}
|
|
|
|
|
|
|
|
async setBrowserSendTypeComponentState(
|
|
|
|
value: BrowserComponentState,
|
|
|
|
options?: StorageOptions
|
|
|
|
): Promise<void> {
|
|
|
|
const account = await this.getAccount(
|
|
|
|
this.reconcileOptions(options, this.defaultInMemoryOptions)
|
|
|
|
);
|
|
|
|
account.sendType = value;
|
|
|
|
await this.saveAccount(account, this.reconcileOptions(options, this.defaultInMemoryOptions));
|
|
|
|
}
|
|
|
|
}
|