mirror of
https://github.com/bitwarden/browser.git
synced 2024-11-14 10:26:19 +01:00
[PM-10744] Remove Last Uses of getBgService
(#10947)
* Remove Last Uses of `getBgService` * Fix Merge Issue * Fix Merge
This commit is contained in:
parent
fc9c10340b
commit
008e928d0a
@ -257,12 +257,9 @@ import { BrowserPlatformUtilsService } from "../platform/services/platform-utils
|
||||
import { PopupViewCacheBackgroundService } from "../platform/services/popup-view-cache-background.service";
|
||||
import { BrowserSdkClientFactory } from "../platform/services/sdk/browser-sdk-client-factory";
|
||||
import { BackgroundTaskSchedulerService } from "../platform/services/task-scheduler/background-task-scheduler.service";
|
||||
import { ForegroundTaskSchedulerService } from "../platform/services/task-scheduler/foreground-task-scheduler.service";
|
||||
import { BackgroundMemoryStorageService } from "../platform/storage/background-memory-storage.service";
|
||||
import { BrowserStorageServiceProvider } from "../platform/storage/browser-storage-service.provider";
|
||||
import { ForegroundMemoryStorageService } from "../platform/storage/foreground-memory-storage.service";
|
||||
import { OffscreenStorageService } from "../platform/storage/offscreen-storage.service";
|
||||
import { ForegroundSyncService } from "../platform/sync/foreground-sync.service";
|
||||
import { SyncServiceListener } from "../platform/sync/sync-service.listener";
|
||||
import { fromChromeRuntimeMessaging } from "../platform/utils/from-chrome-runtime-messaging";
|
||||
import VaultTimeoutService from "../services/vault-timeout/vault-timeout.service";
|
||||
@ -401,7 +398,7 @@ export default class MainBackground {
|
||||
|
||||
private popupViewCacheBackgroundService: PopupViewCacheBackgroundService;
|
||||
|
||||
constructor(public popupOnlyContext: boolean = false) {
|
||||
constructor() {
|
||||
// Services
|
||||
const lockedCallback = async (userId?: string) => {
|
||||
if (this.notificationsService != null) {
|
||||
@ -460,6 +457,18 @@ export default class MainBackground {
|
||||
this.offscreenDocumentService,
|
||||
);
|
||||
|
||||
this.secureStorageService = this.storageService; // secure storage is not supported in browsers, so we use local storage and warn users when it is used
|
||||
|
||||
if (BrowserApi.isManifestVersion(3)) {
|
||||
// manifest v3 can reuse the same storage. They are split for v2 due to lacking a good sync mechanism, which isn't true for v3
|
||||
this.memoryStorageForStateProviders = new BrowserMemoryStorageService(); // mv3 stores to storage.session
|
||||
this.memoryStorageService = this.memoryStorageForStateProviders;
|
||||
} else {
|
||||
this.memoryStorageForStateProviders = new BackgroundMemoryStorageService(); // mv2 stores to memory
|
||||
this.memoryStorageService = this.memoryStorageForStateProviders;
|
||||
}
|
||||
|
||||
if (BrowserApi.isManifestVersion(3)) {
|
||||
// Creates a session key for mv3 storage of large memory items
|
||||
const sessionKey = new Lazy(async () => {
|
||||
// Key already in session storage
|
||||
@ -482,42 +491,20 @@ export default class MainBackground {
|
||||
return derivedKey;
|
||||
});
|
||||
|
||||
const mv3MemoryStorageCreator = () => {
|
||||
if (this.popupOnlyContext) {
|
||||
return new ForegroundMemoryStorageService();
|
||||
}
|
||||
|
||||
this.largeObjectMemoryStorageForStateProviders = new LocalBackedSessionStorageService(
|
||||
sessionKey,
|
||||
this.storageService,
|
||||
// For local backed session storage, we expect that the encrypted data on disk will persist longer than the encryption key in memory
|
||||
// and failures to decrypt because of that are completely expected. For this reason, we pass in `false` to the `EncryptServiceImplementation`
|
||||
// so that MAC failures are not logged.
|
||||
return new LocalBackedSessionStorageService(
|
||||
sessionKey,
|
||||
this.storageService,
|
||||
new EncryptServiceImplementation(this.cryptoFunctionService, this.logService, false),
|
||||
this.platformUtilsService,
|
||||
this.logService,
|
||||
);
|
||||
};
|
||||
|
||||
this.secureStorageService = this.storageService; // secure storage is not supported in browsers, so we use local storage and warn users when it is used
|
||||
|
||||
if (BrowserApi.isManifestVersion(3)) {
|
||||
// manifest v3 can reuse the same storage. They are split for v2 due to lacking a good sync mechanism, which isn't true for v3
|
||||
this.memoryStorageForStateProviders = new BrowserMemoryStorageService(); // mv3 stores to storage.session
|
||||
this.memoryStorageService = this.memoryStorageForStateProviders;
|
||||
} else {
|
||||
if (popupOnlyContext) {
|
||||
this.memoryStorageForStateProviders = new ForegroundMemoryStorageService();
|
||||
this.memoryStorageService = new ForegroundMemoryStorageService();
|
||||
} else {
|
||||
this.memoryStorageForStateProviders = new BackgroundMemoryStorageService(); // mv2 stores to memory
|
||||
this.memoryStorageService = this.memoryStorageForStateProviders;
|
||||
// mv2 stores to the same location
|
||||
this.largeObjectMemoryStorageForStateProviders = this.memoryStorageForStateProviders;
|
||||
}
|
||||
}
|
||||
|
||||
this.largeObjectMemoryStorageForStateProviders = BrowserApi.isManifestVersion(3)
|
||||
? mv3MemoryStorageCreator() // mv3 stores to local-backed session storage
|
||||
: this.memoryStorageForStateProviders; // mv2 stores to the same location
|
||||
|
||||
const localStorageStorageService = BrowserApi.isManifestVersion(3)
|
||||
? new OffscreenStorageService(this.offscreenDocumentService)
|
||||
@ -575,9 +562,10 @@ export default class MainBackground {
|
||||
this.derivedStateProvider,
|
||||
);
|
||||
|
||||
this.taskSchedulerService = this.popupOnlyContext
|
||||
? new ForegroundTaskSchedulerService(this.logService, this.stateProvider)
|
||||
: new BackgroundTaskSchedulerService(this.logService, this.stateProvider);
|
||||
this.taskSchedulerService = new BackgroundTaskSchedulerService(
|
||||
this.logService,
|
||||
this.stateProvider,
|
||||
);
|
||||
this.taskSchedulerService.registerTaskHandler(ScheduledTaskNames.scheduleNextSyncInterval, () =>
|
||||
this.fullSync(),
|
||||
);
|
||||
@ -873,7 +861,6 @@ export default class MainBackground {
|
||||
|
||||
this.vaultSettingsService = new VaultSettingsService(this.stateProvider);
|
||||
|
||||
if (!this.popupOnlyContext) {
|
||||
this.vaultTimeoutService = new VaultTimeoutService(
|
||||
this.accountService,
|
||||
this.masterPasswordService,
|
||||
@ -892,7 +879,6 @@ export default class MainBackground {
|
||||
lockedCallback,
|
||||
logoutCallback,
|
||||
);
|
||||
}
|
||||
this.containerService = new ContainerService(this.keyService, this.encryptService);
|
||||
|
||||
this.sendStateProvider = new SendStateProvider(this.stateProvider);
|
||||
@ -913,24 +899,6 @@ export default class MainBackground {
|
||||
|
||||
this.providerService = new ProviderService(this.stateProvider);
|
||||
|
||||
if (this.popupOnlyContext) {
|
||||
this.syncService = new ForegroundSyncService(
|
||||
this.stateService,
|
||||
this.folderService,
|
||||
this.folderApiService,
|
||||
this.messagingService,
|
||||
this.logService,
|
||||
this.cipherService,
|
||||
this.collectionService,
|
||||
this.apiService,
|
||||
this.accountService,
|
||||
this.authService,
|
||||
this.sendService,
|
||||
this.sendApiService,
|
||||
messageListener,
|
||||
this.stateProvider,
|
||||
);
|
||||
} else {
|
||||
this.syncService = new DefaultSyncService(
|
||||
this.masterPasswordService,
|
||||
this.accountService,
|
||||
@ -965,7 +933,7 @@ export default class MainBackground {
|
||||
this.messagingService,
|
||||
this.logService,
|
||||
);
|
||||
}
|
||||
|
||||
this.eventUploadService = new EventUploadService(
|
||||
this.apiService,
|
||||
this.stateProvider,
|
||||
@ -1112,7 +1080,7 @@ export default class MainBackground {
|
||||
this.isSafari = this.platformUtilsService.isSafari();
|
||||
|
||||
// Background
|
||||
if (!this.popupOnlyContext) {
|
||||
|
||||
this.fido2Background = new Fido2Background(
|
||||
this.logService,
|
||||
this.fido2ActiveRequestManager,
|
||||
@ -1203,7 +1171,14 @@ export default class MainBackground {
|
||||
|
||||
const contextMenuClickedHandler = new ContextMenuClickedHandler(
|
||||
(options) => this.platformUtilsService.copyToClipboard(options.text),
|
||||
async () => this.generatePasswordToClipboard(),
|
||||
async (_tab) => {
|
||||
const options = (await this.passwordGenerationService.getOptions())?.[0] ?? {};
|
||||
const password = await this.passwordGenerationService.generatePassword(options);
|
||||
this.platformUtilsService.copyToClipboard(password);
|
||||
// FIXME: Verify that this floating promise is intentional. If it is, add an explanatory comment and ensure there is proper error handling.
|
||||
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
||||
this.passwordGenerationService.addHistory(password);
|
||||
},
|
||||
async (tab, cipher) => {
|
||||
this.loginToAutoFill = cipher;
|
||||
if (tab == null) {
|
||||
@ -1227,7 +1202,6 @@ export default class MainBackground {
|
||||
);
|
||||
|
||||
this.contextMenusBackground = new ContextMenusBackground(contextMenuClickedHandler);
|
||||
}
|
||||
|
||||
this.idleBackground = new IdleBackground(
|
||||
this.vaultTimeoutService,
|
||||
@ -1246,7 +1220,6 @@ export default class MainBackground {
|
||||
this.stateProvider,
|
||||
);
|
||||
|
||||
if (!this.popupOnlyContext) {
|
||||
this.mainContextMenuHandler = new MainContextMenuHandler(
|
||||
this.stateService,
|
||||
this.autofillSettingsService,
|
||||
@ -1269,7 +1242,6 @@ export default class MainBackground {
|
||||
chrome.webRequest,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
this.userAutoUnlockKeyService = new UserAutoUnlockKeyService(this.keyService);
|
||||
|
||||
@ -1283,7 +1255,7 @@ export default class MainBackground {
|
||||
this.containerService.attachToGlobal(self);
|
||||
|
||||
// Only the "true" background should run migrations
|
||||
await this.stateService.init({ runMigrations: !this.popupOnlyContext });
|
||||
await this.stateService.init({ runMigrations: true });
|
||||
|
||||
// This is here instead of in in the InitService b/c we don't plan for
|
||||
// side effects to run in the Browser InitService.
|
||||
@ -1305,10 +1277,6 @@ export default class MainBackground {
|
||||
|
||||
this.popupViewCacheBackgroundService.startObservingTabChanges();
|
||||
|
||||
if (this.popupOnlyContext) {
|
||||
return;
|
||||
}
|
||||
|
||||
await this.vaultTimeoutService.init(true);
|
||||
this.fido2Background.init();
|
||||
await this.runtimeBackground.init();
|
||||
@ -1637,7 +1605,6 @@ export default class MainBackground {
|
||||
*/
|
||||
async initOverlayAndTabsBackground() {
|
||||
if (
|
||||
this.popupOnlyContext ||
|
||||
this.overlayBackground ||
|
||||
this.tabsBackground ||
|
||||
(await firstValueFrom(this.authService.activeAccountStatus$)) ===
|
||||
|
@ -24,8 +24,8 @@ import {
|
||||
LockComponentService,
|
||||
} from "@bitwarden/auth/angular";
|
||||
import { LockService, LoginEmailService, PinServiceAbstraction } from "@bitwarden/auth/common";
|
||||
import { ApiService } from "@bitwarden/common/abstractions/api.service";
|
||||
import { EventCollectionService as EventCollectionServiceAbstraction } from "@bitwarden/common/abstractions/event/event-collection.service";
|
||||
import { NotificationsService } from "@bitwarden/common/abstractions/notifications.service";
|
||||
import { VaultTimeoutService } from "@bitwarden/common/abstractions/vault-timeout/vault-timeout.service";
|
||||
import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction";
|
||||
import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
|
||||
@ -92,9 +92,15 @@ import { InlineDerivedStateProvider } from "@bitwarden/common/platform/state/imp
|
||||
import { PrimarySecondaryStorageService } from "@bitwarden/common/platform/storage/primary-secondary-storage.service";
|
||||
import { WindowStorageService } from "@bitwarden/common/platform/storage/window-storage.service";
|
||||
import { SyncService } from "@bitwarden/common/platform/sync";
|
||||
import { SendApiService } from "@bitwarden/common/tools/send/services/send-api.service.abstraction";
|
||||
import { InternalSendService } from "@bitwarden/common/tools/send/services/send.service.abstraction";
|
||||
import { VaultTimeoutStringType } from "@bitwarden/common/types/vault-timeout.type";
|
||||
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
|
||||
import { FolderService as FolderServiceAbstraction } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction";
|
||||
import { FolderApiServiceAbstraction } from "@bitwarden/common/vault/abstractions/folder/folder-api.service.abstraction";
|
||||
import {
|
||||
FolderService as FolderServiceAbstraction,
|
||||
InternalFolderService,
|
||||
} from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction";
|
||||
import { TotpService as TotpServiceAbstraction } from "@bitwarden/common/vault/abstractions/totp.service";
|
||||
import { TotpService } from "@bitwarden/common/vault/services/totp.service";
|
||||
import { DialogService, ToastService } from "@bitwarden/components";
|
||||
@ -107,7 +113,6 @@ import { ExtensionAnonLayoutWrapperDataService } from "../../auth/popup/extensio
|
||||
import { ExtensionLoginComponentService } from "../../auth/popup/login/extension-login-component.service";
|
||||
import { AutofillService as AutofillServiceAbstraction } from "../../autofill/services/abstractions/autofill.service";
|
||||
import AutofillService from "../../autofill/services/autofill.service";
|
||||
import MainBackground from "../../background/main.background";
|
||||
import { ForegroundBrowserBiometricsService } from "../../key-management/biometrics/foreground-browser-biometrics";
|
||||
import { BrowserKeyService } from "../../key-management/browser-key.service";
|
||||
import { BrowserApi } from "../../platform/browser/browser-api";
|
||||
@ -117,12 +122,12 @@ import { ChromeMessageSender } from "../../platform/messaging/chrome-message.sen
|
||||
/* eslint-enable no-restricted-imports */
|
||||
import { OffscreenDocumentService } from "../../platform/offscreen-document/abstractions/offscreen-document";
|
||||
import { DefaultOffscreenDocumentService } from "../../platform/offscreen-document/offscreen-document.service";
|
||||
import BrowserPopupUtils from "../../platform/popup/browser-popup-utils";
|
||||
import { BrowserFileDownloadService } from "../../platform/popup/services/browser-file-download.service";
|
||||
import { PopupViewCacheService } from "../../platform/popup/view-cache/popup-view-cache.service";
|
||||
import { ScriptInjectorService } from "../../platform/services/abstractions/script-injector.service";
|
||||
import { BrowserEnvironmentService } from "../../platform/services/browser-environment.service";
|
||||
import BrowserLocalStorageService from "../../platform/services/browser-local-storage.service";
|
||||
import BrowserMemoryStorageService from "../../platform/services/browser-memory-storage.service";
|
||||
import { BrowserScriptInjectorService } from "../../platform/services/browser-script-injector.service";
|
||||
import I18nService from "../../platform/services/i18n.service";
|
||||
import { ForegroundPlatformUtilsService } from "../../platform/services/platform-utils/foreground-platform-utils.service";
|
||||
@ -130,6 +135,7 @@ import { BrowserSdkClientFactory } from "../../platform/services/sdk/browser-sdk
|
||||
import { ForegroundTaskSchedulerService } from "../../platform/services/task-scheduler/foreground-task-scheduler.service";
|
||||
import { BrowserStorageServiceProvider } from "../../platform/storage/browser-storage-service.provider";
|
||||
import { ForegroundMemoryStorageService } from "../../platform/storage/foreground-memory-storage.service";
|
||||
import { ForegroundSyncService } from "../../platform/sync/foreground-sync.service";
|
||||
import { fromChromeRuntimeMessaging } from "../../platform/utils/from-chrome-runtime-messaging";
|
||||
import { ExtensionLockComponentService } from "../../services/extension-lock-component.service";
|
||||
import { ForegroundVaultTimeoutService } from "../../services/vault-timeout/foreground-vault-timeout.service";
|
||||
@ -151,26 +157,6 @@ const DISK_BACKUP_LOCAL_STORAGE = new SafeInjectionToken<
|
||||
AbstractStorageService & ObservableStorageService
|
||||
>("DISK_BACKUP_LOCAL_STORAGE");
|
||||
|
||||
const needsBackgroundInit = BrowserPopupUtils.backgroundInitializationRequired();
|
||||
const mainBackground: MainBackground = needsBackgroundInit
|
||||
? createLocalBgService()
|
||||
: BrowserApi.getBackgroundPage().bitwardenMain;
|
||||
|
||||
function createLocalBgService() {
|
||||
const localBgService = new MainBackground(true);
|
||||
// FIXME: Verify that this floating promise is intentional. If it is, add an explanatory comment and ensure there is proper error handling.
|
||||
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
||||
localBgService.bootstrap();
|
||||
return localBgService;
|
||||
}
|
||||
|
||||
/** @deprecated This method needs to be removed as part of MV3 conversion. Please do not add more and actively try to remove usages */
|
||||
function getBgService<T>(service: keyof MainBackground) {
|
||||
return (): T => {
|
||||
return mainBackground ? (mainBackground[service] as any as T) : null;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Provider definitions used in the ngModule.
|
||||
* Add your provider definition here using the safeProvider function as a wrapper. This will give you type safety.
|
||||
@ -307,8 +293,23 @@ const safeProviders: SafeProvider[] = [
|
||||
}),
|
||||
safeProvider({
|
||||
provide: SyncService,
|
||||
useFactory: getBgService<SyncService>("syncService"),
|
||||
deps: [],
|
||||
useClass: ForegroundSyncService,
|
||||
deps: [
|
||||
StateService,
|
||||
InternalFolderService,
|
||||
FolderApiServiceAbstraction,
|
||||
MessageSender,
|
||||
LogService,
|
||||
CipherService,
|
||||
CollectionService,
|
||||
ApiService,
|
||||
AccountServiceAbstraction,
|
||||
AuthService,
|
||||
InternalSendService,
|
||||
SendApiService,
|
||||
MessageListener,
|
||||
StateProvider,
|
||||
],
|
||||
}),
|
||||
safeProvider({
|
||||
provide: DomainSettingsService,
|
||||
@ -358,11 +359,6 @@ const safeProviders: SafeProvider[] = [
|
||||
useClass: ForegroundVaultTimeoutService,
|
||||
deps: [MessagingServiceAbstraction],
|
||||
}),
|
||||
safeProvider({
|
||||
provide: NotificationsService,
|
||||
useFactory: getBgService<NotificationsService>("notificationsService"),
|
||||
deps: [],
|
||||
}),
|
||||
safeProvider({
|
||||
provide: VaultFilterService,
|
||||
useClass: VaultFilterService,
|
||||
@ -382,8 +378,8 @@ const safeProviders: SafeProvider[] = [
|
||||
}),
|
||||
safeProvider({
|
||||
provide: MEMORY_STORAGE,
|
||||
useFactory: getBgService<AbstractStorageService>("memoryStorageService"),
|
||||
deps: [],
|
||||
useFactory: (memoryStorage: AbstractStorageService) => memoryStorage,
|
||||
deps: [OBSERVABLE_MEMORY_STORAGE],
|
||||
}),
|
||||
safeProvider({
|
||||
provide: OBSERVABLE_MEMORY_STORAGE,
|
||||
@ -392,9 +388,7 @@ const safeProviders: SafeProvider[] = [
|
||||
return new ForegroundMemoryStorageService();
|
||||
}
|
||||
|
||||
return getBgService<AbstractStorageService & ObservableStorageService>(
|
||||
"memoryStorageForStateProviders",
|
||||
)();
|
||||
return new BrowserMemoryStorageService();
|
||||
},
|
||||
deps: [],
|
||||
}),
|
||||
@ -407,9 +401,7 @@ const safeProviders: SafeProvider[] = [
|
||||
return regularMemoryStorageService;
|
||||
}
|
||||
|
||||
return getBgService<AbstractStorageService & ObservableStorageService>(
|
||||
"largeObjectMemoryStorageForStateProviders",
|
||||
)();
|
||||
return new ForegroundMemoryStorageService();
|
||||
},
|
||||
deps: [OBSERVABLE_MEMORY_STORAGE],
|
||||
}),
|
||||
@ -494,15 +486,7 @@ const safeProviders: SafeProvider[] = [
|
||||
}),
|
||||
safeProvider({
|
||||
provide: INTRAPROCESS_MESSAGING_SUBJECT,
|
||||
useFactory: () => {
|
||||
if (BrowserPopupUtils.backgroundInitializationRequired()) {
|
||||
// There is no persistent main background which means we have one in memory,
|
||||
// we need the same instance that our in memory background is utilizing.
|
||||
return getBgService("intraprocessMessagingSubject")();
|
||||
} else {
|
||||
return new Subject<Message<Record<string, unknown>>>();
|
||||
}
|
||||
},
|
||||
useFactory: () => new Subject<Message<Record<string, unknown>>>(),
|
||||
deps: [],
|
||||
}),
|
||||
safeProvider({
|
||||
@ -514,23 +498,6 @@ const safeProviders: SafeProvider[] = [
|
||||
),
|
||||
deps: [INTRAPROCESS_MESSAGING_SUBJECT, LogService],
|
||||
}),
|
||||
safeProvider({
|
||||
provide: INTRAPROCESS_MESSAGING_SUBJECT,
|
||||
useFactory: () => {
|
||||
if (needsBackgroundInit) {
|
||||
// We will have created a popup within this context, in that case
|
||||
// we want to make sure we have the same subject as that context so we
|
||||
// can message with it.
|
||||
return getBgService("intraprocessMessagingSubject")();
|
||||
} else {
|
||||
// There isn't a locally created background so we will communicate with
|
||||
// the true background through chrome apis, in that case, we can just create
|
||||
// one for ourself.
|
||||
return new Subject<Message<Record<string, unknown>>>();
|
||||
}
|
||||
},
|
||||
deps: [],
|
||||
}),
|
||||
safeProvider({
|
||||
provide: DISK_BACKUP_LOCAL_STORAGE,
|
||||
useFactory: (diskStorage: AbstractStorageService & ObservableStorageService) =>
|
||||
@ -572,13 +539,7 @@ const safeProviders: SafeProvider[] = [
|
||||
}),
|
||||
safeProvider({
|
||||
provide: ForegroundTaskSchedulerService,
|
||||
useFactory: (logService: LogService, stateProvider: StateProvider) => {
|
||||
if (needsBackgroundInit) {
|
||||
return getBgService<ForegroundTaskSchedulerService>("taskSchedulerService")();
|
||||
}
|
||||
|
||||
return new ForegroundTaskSchedulerService(logService, stateProvider);
|
||||
},
|
||||
useClass: ForegroundTaskSchedulerService,
|
||||
deps: [LogService, StateProvider],
|
||||
}),
|
||||
safeProvider({
|
||||
|
Loading…
Reference in New Issue
Block a user