[EC-157] [BEEEP] Remove factory providers in Angular DI (#2516)

* Use initService

* Use InjectionTokens

* Update to use new locked and logout callbacks
This commit is contained in:
Thomas Rittson 2022-04-27 09:11:39 +10:00 committed by GitHub
parent 510c841359
commit 17c3fdd68b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 87 additions and 98 deletions

2
jslib

@ -1 +1 @@
Subproject commit 6bcadc4f408db2c150753f53a07d6f8888b6e9ff
Subproject commit e40e7de8083ecfe220ef7f94f7447d560f822272

View File

@ -267,21 +267,21 @@ export default class MainBackground {
this.cryptoFunctionService
);
const vaultTimeoutServiceCallbacks = {
locked: async (userId?: string) => {
if (this.notificationsService != null) {
this.notificationsService.updateConnection(false);
}
await this.setIcon();
await this.refreshBadgeAndMenu(true);
if (this.systemService != null) {
await this.systemService.clearPendingClipboard();
await this.reloadProcess();
}
},
logout: async (userId?: string) => await this.logout(false, userId),
const lockedCallback = async (userId?: string) => {
if (this.notificationsService != null) {
this.notificationsService.updateConnection(false);
}
await this.setIcon();
await this.refreshBadgeAndMenu(true);
if (this.systemService != null) {
await this.systemService.clearPendingClipboard();
await this.reloadProcess();
}
};
const logoutCallback = async (expired: boolean, userId?: string) =>
await this.logout(expired, userId);
this.vaultTimeoutService = new VaultTimeoutService(
this.cipherService,
this.folderService,
@ -294,8 +294,8 @@ export default class MainBackground {
this.policyService,
this.keyConnectorService,
this.stateService,
vaultTimeoutServiceCallbacks.locked,
vaultTimeoutServiceCallbacks.logout
lockedCallback,
logoutCallback
);
this.providerService = new ProviderService(this.stateService);
this.syncService = new SyncService(
@ -313,7 +313,7 @@ export default class MainBackground {
this.stateService,
this.organizationService,
this.providerService,
async (expired: boolean) => await this.logout(expired)
logoutCallback
);
this.eventService = new EventService(
this.apiService,
@ -354,7 +354,7 @@ export default class MainBackground {
this.apiService,
this.vaultTimeoutService,
this.environmentService,
() => this.logout(true),
logoutCallback,
this.logService,
this.stateService
);

View File

@ -0,0 +1,62 @@
import { Injectable } from "@angular/core";
import { I18nService } from "jslib-common/abstractions/i18n.service";
import { LogService as LogServiceAbstraction } from "jslib-common/abstractions/log.service";
import { PlatformUtilsService } from "jslib-common/abstractions/platformUtils.service";
import { ThemeType } from "jslib-common/enums/themeType";
import { StateService as StateServiceAbstraction } from "../../services/abstractions/state.service";
import { PopupUtilsService } from "./popup-utils.service";
@Injectable()
export class InitService {
constructor(
private platformUtilsService: PlatformUtilsService,
private i18nService: I18nService,
private popupUtilsService: PopupUtilsService,
private stateService: StateServiceAbstraction,
private logService: LogServiceAbstraction
) {}
init() {
return async () => {
await this.stateService.init();
if (!this.popupUtilsService.inPopup(window)) {
window.document.body.classList.add("body-full");
} else if (window.screen.availHeight < 600) {
window.document.body.classList.add("body-xs");
} else if (window.screen.availHeight <= 800) {
window.document.body.classList.add("body-sm");
}
const htmlEl = window.document.documentElement;
const theme = await this.platformUtilsService.getEffectiveTheme();
htmlEl.classList.add("theme_" + theme);
this.platformUtilsService.onDefaultSystemThemeChange(async (sysTheme) => {
const bwTheme = await this.stateService.getTheme();
if (bwTheme == null || bwTheme === ThemeType.System) {
htmlEl.classList.remove("theme_" + ThemeType.Light, "theme_" + ThemeType.Dark);
htmlEl.classList.add("theme_" + sysTheme);
}
});
htmlEl.classList.add("locale_" + this.i18nService.translationLocale);
// Workaround for slow performance on external monitors on Chrome + MacOS
// See: https://bugs.chromium.org/p/chromium/issues/detail?id=971701#c64
if (
this.platformUtilsService.isChrome() &&
navigator.platform.indexOf("Mac") > -1 &&
this.popupUtilsService.inPopup(window) &&
(window.screenLeft < 0 ||
window.screenTop < 0 ||
window.screenLeft > window.screen.width ||
window.screenTop > window.screen.height)
) {
htmlEl.classList.add("force_redraw");
this.logService.info("Force redraw is on");
}
};
}
}

View File

@ -1,6 +1,6 @@
import { APP_INITIALIZER, LOCALE_ID, NgModule } from "@angular/core";
import { JslibServicesModule } from "jslib-angular/services/jslib-services.module";
import { JslibServicesModule, SECURE_STORAGE } from "jslib-angular/services/jslib-services.module";
import { LockGuardService as BaseLockGuardService } from "jslib-angular/services/lock-guard.service";
import { UnauthGuardService as BaseUnauthGuardService } from "jslib-angular/services/unauth-guard.service";
import { ApiService } from "jslib-common/abstractions/api.service";
@ -39,7 +39,6 @@ import { TwoFactorService } from "jslib-common/abstractions/twoFactor.service";
import { UserVerificationService } from "jslib-common/abstractions/userVerification.service";
import { UsernameGenerationService } from "jslib-common/abstractions/usernameGeneration.service";
import { VaultTimeoutService } from "jslib-common/abstractions/vaultTimeout.service";
import { ThemeType } from "jslib-common/enums/themeType";
import { AuthService } from "jslib-common/services/auth.service";
import { ConsoleLogService } from "jslib-common/services/consoleLog.service";
import { SearchService } from "jslib-common/services/search.service";
@ -52,6 +51,7 @@ import BrowserMessagingService from "../../services/browserMessaging.service";
import BrowserMessagingPrivateModePopupService from "../../services/browserMessagingPrivateModePopup.service";
import { DebounceNavigationService } from "./debounceNavigationService";
import { InitService } from "./init.service";
import { LockGuardService } from "./lock-guard.service";
import { PasswordRepromptService } from "./password-reprompt.service";
import { PopupSearchService } from "./popup-search.service";
@ -75,73 +75,12 @@ function getBgService<T>(service: keyof MainBackground) {
};
}
export function initFactory(
platformUtilsService: PlatformUtilsService,
i18nService: I18nService,
popupUtilsService: PopupUtilsService,
stateService: StateServiceAbstraction,
logService: LogServiceAbstraction
): () => void {
return async () => {
await stateService.init();
if (!popupUtilsService.inPopup(window)) {
window.document.body.classList.add("body-full");
} else if (window.screen.availHeight < 600) {
window.document.body.classList.add("body-xs");
} else if (window.screen.availHeight <= 800) {
window.document.body.classList.add("body-sm");
}
const htmlEl = window.document.documentElement;
const theme = await platformUtilsService.getEffectiveTheme();
htmlEl.classList.add("theme_" + theme);
platformUtilsService.onDefaultSystemThemeChange(async (sysTheme) => {
const bwTheme = await stateService.getTheme();
if (bwTheme == null || bwTheme === ThemeType.System) {
htmlEl.classList.remove("theme_" + ThemeType.Light, "theme_" + ThemeType.Dark);
htmlEl.classList.add("theme_" + sysTheme);
}
});
htmlEl.classList.add("locale_" + i18nService.translationLocale);
// Workaround for slow performance on external monitors on Chrome + MacOS
// See: https://bugs.chromium.org/p/chromium/issues/detail?id=971701#c64
if (
platformUtilsService.isChrome() &&
navigator.platform.indexOf("Mac") > -1 &&
popupUtilsService.inPopup(window) &&
(window.screenLeft < 0 ||
window.screenTop < 0 ||
window.screenLeft > window.screen.width ||
window.screenTop > window.screen.height)
) {
htmlEl.classList.add("force_redraw");
logService.info("Force redraw is on");
}
htmlEl.classList.add("locale_" + i18nService.translationLocale);
// Workaround for slow performance on external monitors on Chrome + MacOS
// See: https://bugs.chromium.org/p/chromium/issues/detail?id=971701#c64
if (
platformUtilsService.isChrome() &&
navigator.platform.indexOf("Mac") > -1 &&
popupUtilsService.inPopup(window) &&
(window.screenLeft < 0 ||
window.screenTop < 0 ||
window.screenLeft > window.screen.width ||
window.screenTop > window.screen.height)
) {
htmlEl.classList.add("force_redraw");
logService.info("Force redraw is on");
}
};
}
@NgModule({
imports: [JslibServicesModule],
declarations: [],
providers: [
InitService,
DebounceNavigationService,
{
provide: LOCALE_ID,
useFactory: () => getBgService<I18nService>("i18nService")().translationLocale,
@ -149,19 +88,12 @@ export function initFactory(
},
{
provide: APP_INITIALIZER,
useFactory: initFactory,
deps: [
PlatformUtilsService,
I18nService,
PopupUtilsService,
StateServiceAbstraction,
LogServiceAbstraction,
],
useFactory: (initService: InitService) => initService.init(),
deps: [InitService],
multi: true,
},
{ provide: BaseLockGuardService, useClass: LockGuardService },
{ provide: BaseUnauthGuardService, useClass: UnauthGuardService },
DebounceNavigationService,
{ provide: PopupUtilsService, useFactory: () => new PopupUtilsService(isPrivateMode) },
{
provide: MessagingService,
@ -176,11 +108,6 @@ export function initFactory(
useFactory: getBgService<TwoFactorService>("twoFactorService"),
deps: [],
},
{
provide: TwoFactorService,
useFactory: getBgService<TwoFactorService>("twoFactorService"),
deps: [],
},
{
provide: AuthServiceAbstraction,
useFactory: getBgService<AuthService>("authService"),
@ -303,7 +230,7 @@ export function initFactory(
deps: [],
},
{
provide: "SECURE_STORAGE",
provide: SECURE_STORAGE,
useFactory: getBgService<StorageServiceAbstraction>("secureStorageService"),
deps: [],
},