1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-20 02:03:39 +00:00

[PM-5535] Migrate Environment Service to StateProvider (#7621)

* Migrate EnvironmentService

* Move Migration Test Helper

* Claim StateDefinition

* Add State Migration

* Update StateServices

* Update EnvironmentService Abstraction

* Update DI

* Update Browser Instantiation

* Fix BrowserEnvironmentService

* Update Desktop & CLI Instantiation

* Update Usage

* Create isStringRecord helper

* Fix Old Tests

* Use Existing AccountService

* Don't Rely on Parameter Mutation

* Fix Conflicts
This commit is contained in:
Justin Baur
2024-01-24 14:21:50 -05:00
committed by GitHub
parent 842fa5153b
commit c1d5351075
26 changed files with 648 additions and 232 deletions

View File

@@ -344,6 +344,11 @@ export default class MainBackground {
this.globalStateProvider,
this.derivedStateProvider,
);
this.environmentService = new BrowserEnvironmentService(
this.logService,
this.stateProvider,
this.accountService,
);
this.stateService = new BrowserStateService(
this.storageService,
this.secureStorageService,
@@ -351,6 +356,7 @@ export default class MainBackground {
this.logService,
new StateFactory(GlobalState, Account),
this.accountService,
this.environmentService,
);
this.platformUtilsService = new BrowserPlatformUtilsService(
this.messagingService,
@@ -386,7 +392,6 @@ export default class MainBackground {
);
this.tokenService = new TokenService(this.stateService);
this.appIdService = new AppIdService(this.storageService);
this.environmentService = new BrowserEnvironmentService(this.stateService, this.logService);
this.apiService = new ApiService(
this.tokenService,
this.platformUtilsService,

View File

@@ -1,16 +1,18 @@
import {
accountServiceFactory,
AccountServiceInitOptions,
} from "../../../auth/background/service-factories/account-service.factory";
import { BrowserEnvironmentService } from "../../services/browser-environment.service";
import { CachedServices, factory, FactoryOptions } from "./factory-options";
import { logServiceFactory, LogServiceInitOptions } from "./log-service.factory";
import {
stateServiceFactory as stateServiceFactory,
StateServiceInitOptions,
} from "./state-service.factory";
import { stateProviderFactory, StateProviderInitOptions } from "./state-provider.factory";
type EnvironmentServiceFactoryOptions = FactoryOptions;
export type EnvironmentServiceInitOptions = EnvironmentServiceFactoryOptions &
StateServiceInitOptions &
StateProviderInitOptions &
AccountServiceInitOptions &
LogServiceInitOptions;
export function environmentServiceFactory(
@@ -23,8 +25,9 @@ export function environmentServiceFactory(
opts,
async () =>
new BrowserEnvironmentService(
await stateServiceFactory(cache, opts),
await logServiceFactory(cache, opts),
await stateProviderFactory(cache, opts),
await accountServiceFactory(cache, opts),
),
);
}

View File

@@ -8,6 +8,10 @@ import {
import { Account } from "../../../models/account";
import { BrowserStateService } from "../../services/browser-state.service";
import {
environmentServiceFactory,
EnvironmentServiceInitOptions,
} from "./environment-service.factory";
import { CachedServices, factory, FactoryOptions } from "./factory-options";
import { logServiceFactory, LogServiceInitOptions } from "./log-service.factory";
import {
@@ -31,7 +35,8 @@ export type StateServiceInitOptions = StateServiceFactoryOptions &
SecureStorageServiceInitOptions &
MemoryStorageServiceInitOptions &
LogServiceInitOptions &
AccountServiceInitOptions;
AccountServiceInitOptions &
EnvironmentServiceInitOptions;
export async function stateServiceFactory(
cache: { stateService?: BrowserStateService } & CachedServices,
@@ -42,13 +47,14 @@ export async function stateServiceFactory(
"stateService",
opts,
async () =>
await new BrowserStateService(
new BrowserStateService(
await diskStorageServiceFactory(cache, opts),
await secureStorageServiceFactory(cache, opts),
await memoryStorageServiceFactory(cache, opts),
await logServiceFactory(cache, opts),
opts.stateServiceOptions.stateFactory,
await accountServiceFactory(cache, opts),
await environmentServiceFactory(cache, opts),
opts.stateServiceOptions.useAccountCache,
),
);

View File

@@ -1,16 +1,18 @@
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { StateService } from "@bitwarden/common/platform/abstractions/state.service";
import { EnvironmentService } from "@bitwarden/common/platform/services/environment.service";
import { StateProvider } from "@bitwarden/common/platform/state";
import { GroupPolicyEnvironment } from "../../admin-console/types/group-policy-environment";
import { devFlagEnabled, devFlagValue } from "../flags";
export class BrowserEnvironmentService extends EnvironmentService {
constructor(
stateService: StateService,
private logService: LogService,
stateProvider: StateProvider,
accountService: AccountService,
) {
super(stateService);
super(stateProvider, accountService);
}
async hasManagedEnvironment(): Promise<boolean> {

View File

@@ -1,6 +1,7 @@
import { mock, MockProxy } from "jest-mock-extended";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import {
AbstractMemoryStorageService,
@@ -29,6 +30,7 @@ describe("Browser State Service", () => {
let stateFactory: MockProxy<StateFactory<GlobalState, Account>>;
let useAccountCache: boolean;
let accountService: MockProxy<AccountService>;
let environmentService: MockProxy<EnvironmentService>;
let state: State<GlobalState, Account>;
const userId = "userId";
@@ -41,6 +43,7 @@ describe("Browser State Service", () => {
logService = mock();
stateFactory = mock();
accountService = mock();
environmentService = mock();
// turn off account cache for tests
useAccountCache = false;
@@ -66,6 +69,7 @@ describe("Browser State Service", () => {
logService,
stateFactory,
accountService,
environmentService,
useAccountCache,
);
});

View File

@@ -1,6 +1,7 @@
import { BehaviorSubject } from "rxjs";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import {
AbstractStorageService,
@@ -44,6 +45,7 @@ export class BrowserStateService
logService: LogService,
stateFactory: StateFactory<GlobalState, Account>,
accountService: AccountService,
environmentService: EnvironmentService,
useAccountCache = true,
) {
super(
@@ -53,6 +55,7 @@ export class BrowserStateService
logService,
stateFactory,
accountService,
environmentService,
useAccountCache,
);

View File

@@ -483,6 +483,7 @@ function getBgService<T>(service: keyof MainBackground) {
memoryStorageService: AbstractMemoryStorageService,
logService: LogServiceAbstraction,
accountService: AccountServiceAbstraction,
environmentService: EnvironmentService,
) => {
return new BrowserStateService(
storageService,
@@ -491,6 +492,7 @@ function getBgService<T>(service: keyof MainBackground) {
logService,
new StateFactory(GlobalState, Account),
accountService,
environmentService,
);
},
deps: [
@@ -499,6 +501,7 @@ function getBgService<T>(service: keyof MainBackground) {
MEMORY_STORAGE,
LogServiceAbstraction,
AccountServiceAbstraction,
EnvironmentService,
],
},
{