2024-03-13 16:25:39 +01:00
|
|
|
import { DOCUMENT } from "@angular/common";
|
|
|
|
import { Inject, Injectable } from "@angular/core";
|
2022-04-27 01:11:39 +02:00
|
|
|
|
2023-11-27 21:59:44 +01:00
|
|
|
import { AbstractThemingService } from "@bitwarden/angular/platform/services/theming/theming.service.abstraction";
|
2023-06-06 22:34:53 +02:00
|
|
|
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
|
|
|
|
import { LogService as LogServiceAbstraction } from "@bitwarden/common/platform/abstractions/log.service";
|
|
|
|
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
|
2023-09-08 16:05:37 +02:00
|
|
|
import { ConfigService } from "@bitwarden/common/platform/services/config/config.service";
|
2022-04-27 01:11:39 +02:00
|
|
|
|
2024-02-23 20:01:45 +01:00
|
|
|
import { BrowserApi } from "../../platform/browser/browser-api";
|
2023-11-08 19:57:44 +01:00
|
|
|
import BrowserPopupUtils from "../../platform/popup/browser-popup-utils";
|
2023-06-06 22:34:53 +02:00
|
|
|
import { BrowserStateService as StateServiceAbstraction } from "../../platform/services/abstractions/browser-state.service";
|
2022-04-27 01:11:39 +02:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class InitService {
|
|
|
|
constructor(
|
|
|
|
private platformUtilsService: PlatformUtilsService,
|
|
|
|
private i18nService: I18nService,
|
|
|
|
private stateService: StateServiceAbstraction,
|
2022-06-23 13:36:05 +02:00
|
|
|
private logService: LogServiceAbstraction,
|
2023-09-08 16:05:37 +02:00
|
|
|
private themingService: AbstractThemingService,
|
2023-11-29 22:15:20 +01:00
|
|
|
private configService: ConfigService,
|
2024-03-13 16:25:39 +01:00
|
|
|
@Inject(DOCUMENT) private document: Document,
|
2022-04-27 01:11:39 +02:00
|
|
|
) {}
|
|
|
|
|
|
|
|
init() {
|
|
|
|
return async () => {
|
|
|
|
await this.stateService.init();
|
2024-03-13 16:35:46 +01:00
|
|
|
await this.i18nService.init();
|
2022-04-27 01:11:39 +02:00
|
|
|
|
2023-11-08 19:57:44 +01:00
|
|
|
if (!BrowserPopupUtils.inPopup(window)) {
|
2022-04-27 01:11:39 +02:00
|
|
|
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;
|
2024-03-13 16:25:39 +01:00
|
|
|
this.themingService.applyThemeChangesTo(this.document);
|
2022-04-27 01:11:39 +02:00
|
|
|
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 &&
|
2023-11-08 19:57:44 +01:00
|
|
|
BrowserPopupUtils.inPopup(window) &&
|
2022-04-27 01:11:39 +02:00
|
|
|
(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");
|
|
|
|
}
|
2023-09-08 16:05:37 +02:00
|
|
|
|
|
|
|
this.configService.init();
|
2024-02-23 20:01:45 +01:00
|
|
|
this.setupVaultPopupHeartbeat();
|
2022-04-27 01:11:39 +02:00
|
|
|
};
|
|
|
|
}
|
2024-02-23 20:01:45 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets up a runtime message listener to indicate to the background
|
|
|
|
* script that the extension popup is open in some manner.
|
|
|
|
*/
|
|
|
|
private setupVaultPopupHeartbeat() {
|
|
|
|
const respondToHeartbeat = (
|
|
|
|
message: { command: string },
|
|
|
|
_sender: chrome.runtime.MessageSender,
|
|
|
|
sendResponse: (response?: any) => void,
|
|
|
|
) => {
|
|
|
|
if (message?.command === "checkVaultPopupHeartbeat") {
|
|
|
|
sendResponse(true);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
BrowserApi.messageListener("vaultPopupHeartbeat", respondToHeartbeat);
|
|
|
|
}
|
2022-04-27 01:11:39 +02:00
|
|
|
}
|