2022-04-29 09:45:47 +02:00
|
|
|
import { Injectable } from "@angular/core";
|
2021-05-05 09:46:14 +02:00
|
|
|
import Swal, { SweetAlertIcon } from "sweetalert2";
|
2018-06-08 20:56:26 +02:00
|
|
|
|
2022-06-14 17:10:53 +02:00
|
|
|
import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
|
|
|
|
import { LogService } from "@bitwarden/common/abstractions/log.service";
|
|
|
|
import { MessagingService } from "@bitwarden/common/abstractions/messaging.service";
|
|
|
|
import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service";
|
|
|
|
import { StateService } from "@bitwarden/common/abstractions/state.service";
|
|
|
|
import { ClientType } from "@bitwarden/common/enums/clientType";
|
|
|
|
import { DeviceType } from "@bitwarden/common/enums/deviceType";
|
|
|
|
import { ThemeType } from "@bitwarden/common/enums/themeType";
|
2018-06-05 21:02:53 +02:00
|
|
|
|
2022-04-29 09:45:47 +02:00
|
|
|
@Injectable()
|
2018-06-05 21:02:53 +02:00
|
|
|
export class WebPlatformUtilsService implements PlatformUtilsService {
|
2018-07-09 15:32:24 +02:00
|
|
|
private browserCache: DeviceType = null;
|
2021-09-30 00:06:20 +02:00
|
|
|
private prefersColorSchemeDark = window.matchMedia("(prefers-color-scheme: dark)");
|
2018-06-05 21:02:53 +02:00
|
|
|
|
2021-05-12 02:51:12 +02:00
|
|
|
constructor(
|
|
|
|
private i18nService: I18nService,
|
|
|
|
private messagingService: MessagingService,
|
2021-12-14 17:10:26 +01:00
|
|
|
private logService: LogService,
|
|
|
|
private stateService: StateService
|
|
|
|
) {}
|
2018-06-05 21:02:53 +02:00
|
|
|
|
|
|
|
getDevice(): DeviceType {
|
2018-07-09 15:32:24 +02:00
|
|
|
if (this.browserCache != null) {
|
|
|
|
return this.browserCache;
|
2018-06-05 21:02:53 +02:00
|
|
|
}
|
|
|
|
|
2021-12-17 15:57:11 +01:00
|
|
|
if (
|
2018-07-09 15:32:24 +02:00
|
|
|
navigator.userAgent.indexOf(" Firefox/") !== -1 ||
|
2018-06-05 21:02:53 +02:00
|
|
|
navigator.userAgent.indexOf(" Gecko/") !== -1
|
2021-12-17 15:57:11 +01:00
|
|
|
) {
|
2018-06-05 21:02:53 +02:00
|
|
|
this.browserCache = DeviceType.FirefoxBrowser;
|
|
|
|
} else if (navigator.userAgent.indexOf(" OPR/") >= 0) {
|
2018-07-09 15:32:24 +02:00
|
|
|
this.browserCache = DeviceType.OperaBrowser;
|
|
|
|
} else if (navigator.userAgent.indexOf(" Edg/") !== -1) {
|
|
|
|
this.browserCache = DeviceType.EdgeBrowser;
|
|
|
|
} else if (navigator.userAgent.indexOf(" Vivaldi/") !== -1) {
|
|
|
|
this.browserCache = DeviceType.VivaldiBrowser;
|
|
|
|
} else if (
|
|
|
|
navigator.userAgent.indexOf(" Safari/") !== -1 &&
|
|
|
|
navigator.userAgent.indexOf("Chrome") === -1
|
2021-12-17 15:57:11 +01:00
|
|
|
) {
|
2018-07-09 15:32:24 +02:00
|
|
|
this.browserCache = DeviceType.SafariBrowser;
|
|
|
|
} else if ((window as any).chrome && navigator.userAgent.indexOf(" Chrome/") !== -1) {
|
|
|
|
this.browserCache = DeviceType.ChromeBrowser;
|
|
|
|
} else if (navigator.userAgent.indexOf(" Trident/") !== -1) {
|
|
|
|
this.browserCache = DeviceType.IEBrowser;
|
2021-12-17 15:57:11 +01:00
|
|
|
} else {
|
2018-07-09 15:32:24 +02:00
|
|
|
this.browserCache = DeviceType.UnknownBrowser;
|
2018-06-05 21:02:53 +02:00
|
|
|
}
|
|
|
|
|
2018-07-09 15:32:24 +02:00
|
|
|
return this.browserCache;
|
2018-06-05 21:02:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
getDeviceString(): string {
|
2018-07-09 15:32:24 +02:00
|
|
|
const device = DeviceType[this.getDevice()].toLowerCase();
|
|
|
|
return device.replace("browser", "");
|
2018-06-05 21:02:53 +02:00
|
|
|
}
|
|
|
|
|
2022-02-08 13:22:31 +01:00
|
|
|
getClientType() {
|
|
|
|
return ClientType.Web;
|
|
|
|
}
|
|
|
|
|
2018-06-05 21:02:53 +02:00
|
|
|
isFirefox(): boolean {
|
2018-07-09 15:32:24 +02:00
|
|
|
return this.getDevice() === DeviceType.FirefoxBrowser;
|
2018-06-06 15:43:28 +02:00
|
|
|
}
|
|
|
|
|
2018-06-05 21:02:53 +02:00
|
|
|
isChrome(): boolean {
|
2018-07-09 15:32:24 +02:00
|
|
|
return this.getDevice() === DeviceType.ChromeBrowser;
|
2018-06-05 21:02:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isEdge(): boolean {
|
2018-07-09 15:32:24 +02:00
|
|
|
return this.getDevice() === DeviceType.EdgeBrowser;
|
2018-06-05 21:02:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isOpera(): boolean {
|
2018-07-09 15:32:24 +02:00
|
|
|
return this.getDevice() === DeviceType.OperaBrowser;
|
2018-06-07 23:12:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isVivaldi(): boolean {
|
2018-07-09 15:32:24 +02:00
|
|
|
return this.getDevice() === DeviceType.VivaldiBrowser;
|
2018-06-05 21:02:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isSafari(): boolean {
|
|
|
|
return this.getDevice() === DeviceType.SafariBrowser;
|
|
|
|
}
|
|
|
|
|
|
|
|
isMacAppStore(): boolean {
|
2018-06-07 23:12:11 +02:00
|
|
|
return false;
|
2018-06-05 21:02:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isViewOpen(): Promise<boolean> {
|
2018-06-08 18:23:46 +02:00
|
|
|
return Promise.resolve(false);
|
2021-03-12 01:05:24 +01:00
|
|
|
}
|
2018-06-05 21:02:53 +02:00
|
|
|
|
2021-04-07 20:42:57 +02:00
|
|
|
launchUri(uri: string, options?: any): void {
|
|
|
|
const a = document.createElement("a");
|
|
|
|
a.href = uri;
|
|
|
|
if (options == null || !options.sameWindow) {
|
|
|
|
a.target = "_blank";
|
|
|
|
a.rel = "noreferrer noopener";
|
2018-06-05 21:02:53 +02:00
|
|
|
}
|
2018-07-25 18:04:14 +02:00
|
|
|
a.classList.add("d-none");
|
2021-03-16 17:44:31 +01:00
|
|
|
document.body.appendChild(a);
|
2018-06-07 23:12:11 +02:00
|
|
|
a.click();
|
2018-07-25 18:04:14 +02:00
|
|
|
document.body.removeChild(a);
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2021-03-16 17:44:31 +01:00
|
|
|
saveFile(win: Window, blobData: any, blobOptions: any, fileName: string): void {
|
2018-06-12 20:35:44 +02:00
|
|
|
let blob: Blob = null;
|
2018-10-30 14:54:14 +01:00
|
|
|
let type: string = null;
|
2021-03-16 17:44:31 +01:00
|
|
|
const fileNameLower = fileName.toLowerCase();
|
2018-11-06 15:46:17 +01:00
|
|
|
let doDownload = true;
|
2021-03-16 17:44:31 +01:00
|
|
|
if (fileNameLower.endsWith(".pdf")) {
|
2018-10-30 14:54:14 +01:00
|
|
|
type = "application/pdf";
|
2018-11-06 15:46:17 +01:00
|
|
|
doDownload = false;
|
2018-10-30 14:54:14 +01:00
|
|
|
} else if (fileNameLower.endsWith(".xlsx")) {
|
2021-03-16 17:44:31 +01:00
|
|
|
type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
2021-11-18 19:03:20 +01:00
|
|
|
} else if (fileNameLower.endsWith(".docx")) {
|
|
|
|
type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
|
|
|
|
} else if (fileNameLower.endsWith(".pptx")) {
|
|
|
|
type = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
|
2018-10-30 14:54:14 +01:00
|
|
|
} else if (fileNameLower.endsWith(".csv")) {
|
|
|
|
type = "text/csv";
|
|
|
|
} else if (fileNameLower.endsWith(".png")) {
|
|
|
|
type = "image/png";
|
|
|
|
} else if (fileNameLower.endsWith(".jpg") || fileNameLower.endsWith(".jpeg")) {
|
|
|
|
type = "image/jpeg";
|
|
|
|
} else if (fileNameLower.endsWith(".gif")) {
|
|
|
|
type = "image/gif";
|
2018-06-05 21:02:53 +02:00
|
|
|
}
|
2018-10-30 14:54:14 +01:00
|
|
|
if (type != null) {
|
2018-06-05 21:02:53 +02:00
|
|
|
blobOptions = blobOptions || {};
|
|
|
|
if (blobOptions.type == null) {
|
|
|
|
blobOptions.type = type;
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2018-06-05 21:02:53 +02:00
|
|
|
}
|
2022-03-24 11:15:48 +01:00
|
|
|
if (blobOptions != null) {
|
2018-10-03 16:33:04 +02:00
|
|
|
blob = new Blob([blobData], blobOptions);
|
|
|
|
} else {
|
|
|
|
blob = new Blob([blobData]);
|
2018-06-05 21:02:53 +02:00
|
|
|
}
|
2022-03-24 11:15:48 +01:00
|
|
|
|
|
|
|
const a = win.document.createElement("a");
|
|
|
|
if (doDownload) {
|
|
|
|
a.download = fileName;
|
|
|
|
} else if (!this.isSafari()) {
|
|
|
|
a.target = "_blank";
|
2018-06-30 20:14:52 +02:00
|
|
|
}
|
2022-03-24 11:15:48 +01:00
|
|
|
a.href = URL.createObjectURL(blob);
|
|
|
|
a.style.position = "fixed";
|
|
|
|
win.document.body.appendChild(a);
|
|
|
|
a.click();
|
|
|
|
win.document.body.removeChild(a);
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2021-05-12 02:51:12 +02:00
|
|
|
getApplicationVersion(): Promise<string> {
|
|
|
|
return Promise.resolve(process.env.APPLICATION_VERSION || "-");
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2021-05-12 02:51:12 +02:00
|
|
|
supportsWebAuthn(win: Window): boolean {
|
2018-06-05 21:02:53 +02:00
|
|
|
return typeof PublicKeyCredential !== "undefined";
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2018-06-05 21:02:53 +02:00
|
|
|
supportsDuo(): boolean {
|
|
|
|
return true;
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2018-10-03 16:33:04 +02:00
|
|
|
showToast(
|
2018-06-05 21:02:53 +02:00
|
|
|
type: "error" | "success" | "warning" | "info",
|
|
|
|
title: string,
|
|
|
|
text: string | string[],
|
|
|
|
options?: any
|
|
|
|
): void {
|
|
|
|
this.messagingService.send("showToast", {
|
|
|
|
text: text,
|
|
|
|
title: title,
|
|
|
|
type: type,
|
|
|
|
options: options,
|
|
|
|
});
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2021-03-04 21:56:25 +01:00
|
|
|
async showDialog(
|
|
|
|
body: string,
|
|
|
|
title?: string,
|
2018-06-05 21:02:53 +02:00
|
|
|
confirmText?: string,
|
2021-03-04 21:56:25 +01:00
|
|
|
cancelText?: string,
|
|
|
|
type?: string,
|
2022-02-24 12:10:07 +01:00
|
|
|
bodyIsHtml = false
|
2021-12-17 15:57:11 +01:00
|
|
|
) {
|
2018-07-26 04:01:26 +02:00
|
|
|
let iconClasses: string = null;
|
2019-12-31 20:35:58 +01:00
|
|
|
if (type != null) {
|
|
|
|
// If you add custom types to this part, the type to SweetAlertIcon cast below needs to be changed.
|
|
|
|
switch (type) {
|
2021-05-12 02:51:12 +02:00
|
|
|
case "success":
|
2022-01-27 18:25:58 +01:00
|
|
|
iconClasses = "bwi-check text-success";
|
2021-05-12 02:51:12 +02:00
|
|
|
break;
|
|
|
|
case "warning":
|
2022-01-27 18:25:58 +01:00
|
|
|
iconClasses = "bwi-exclamation-triangle text-warning";
|
2021-12-17 15:57:11 +01:00
|
|
|
break;
|
2018-06-05 21:02:53 +02:00
|
|
|
case "error":
|
2022-01-27 18:25:58 +01:00
|
|
|
iconClasses = "bwi-error text-danger";
|
2018-07-26 04:01:26 +02:00
|
|
|
break;
|
|
|
|
case "info":
|
2022-01-27 18:25:58 +01:00
|
|
|
iconClasses = "bwi-info-circle text-info";
|
2018-07-26 04:01:26 +02:00
|
|
|
break;
|
2021-12-17 15:57:11 +01:00
|
|
|
default:
|
2021-05-12 02:51:12 +02:00
|
|
|
break;
|
2018-06-05 21:02:53 +02:00
|
|
|
}
|
|
|
|
}
|
2019-02-27 04:42:30 +01:00
|
|
|
|
|
|
|
const bootstrapModal = document.querySelector("div.modal");
|
|
|
|
if (bootstrapModal != null) {
|
|
|
|
bootstrapModal.removeAttribute("tabindex");
|
|
|
|
}
|
2020-07-24 20:39:39 +02:00
|
|
|
|
|
|
|
const iconHtmlStr =
|
2022-01-27 18:25:58 +01:00
|
|
|
iconClasses != null ? `<i class="swal-custom-icon bwi ${iconClasses}"></i>` : undefined;
|
2020-07-24 20:39:39 +02:00
|
|
|
const confirmed = await Swal.fire({
|
|
|
|
heightAuto: false,
|
2020-03-02 19:52:09 +01:00
|
|
|
buttonsStyling: false,
|
2020-07-24 20:39:39 +02:00
|
|
|
icon: type as SweetAlertIcon, // required to be any of the SweetAlertIcons to output the iconHtml.
|
2020-03-02 19:52:09 +01:00
|
|
|
iconHtml: iconHtmlStr,
|
2021-03-04 21:56:25 +01:00
|
|
|
text: bodyIsHtml ? null : body,
|
|
|
|
html: bodyIsHtml ? body : null,
|
2021-05-13 16:08:16 +02:00
|
|
|
titleText: title,
|
2020-03-02 19:52:09 +01:00
|
|
|
showCancelButton: cancelText != null,
|
|
|
|
cancelButtonText: cancelText,
|
|
|
|
showConfirmButton: true,
|
2020-07-24 20:39:39 +02:00
|
|
|
confirmButtonText: confirmText == null ? this.i18nService.t("ok") : confirmText,
|
|
|
|
});
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-05-20 22:52:44 +02:00
|
|
|
if (bootstrapModal != null) {
|
|
|
|
bootstrapModal.setAttribute("tabindex", "-1");
|
2020-07-24 20:39:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return confirmed.value;
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2020-07-24 20:39:39 +02:00
|
|
|
isDev(): boolean {
|
|
|
|
return process.env.NODE_ENV === "development";
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2020-07-24 20:39:39 +02:00
|
|
|
isSelfHost(): boolean {
|
|
|
|
return process.env.ENV.toString() === "selfhosted";
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2021-05-12 02:51:12 +02:00
|
|
|
copyToClipboard(text: string, options?: any): void | boolean {
|
2018-08-17 18:25:21 +02:00
|
|
|
let win = window;
|
|
|
|
let doc = window.document;
|
|
|
|
if (options && (options.window || options.win)) {
|
|
|
|
win = options.window || options.win;
|
|
|
|
doc = win.document;
|
2020-07-24 20:39:39 +02:00
|
|
|
} else if (options && options.doc) {
|
2018-08-17 18:25:21 +02:00
|
|
|
doc = options.doc;
|
2020-07-24 20:39:39 +02:00
|
|
|
}
|
2018-08-20 15:20:24 +02:00
|
|
|
if ((win as any).clipboardData && (win as any).clipboardData.setData) {
|
2020-11-23 16:12:12 +01:00
|
|
|
// IE specific code path to prevent textarea being shown while dialog is visible.
|
2018-08-17 18:25:21 +02:00
|
|
|
(win as any).clipboardData.setData("Text", text);
|
2020-11-23 16:12:12 +01:00
|
|
|
} else if (doc.queryCommandSupported && doc.queryCommandSupported("copy")) {
|
2021-09-30 00:06:20 +02:00
|
|
|
const textarea = doc.createElement("textarea");
|
|
|
|
textarea.textContent = text;
|
|
|
|
// Prevent scrolling to bottom of page in MS Edge.
|
|
|
|
textarea.style.position = "fixed";
|
|
|
|
let copyEl = doc.body;
|
|
|
|
// For some reason copy command won't work when modal is open if appending to body
|
|
|
|
if (doc.body.classList.contains("modal-open")) {
|
|
|
|
copyEl = doc.body.querySelector<HTMLElement>(".modal");
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2021-09-30 00:06:20 +02:00
|
|
|
copyEl.appendChild(textarea);
|
|
|
|
textarea.select();
|
|
|
|
let success = false;
|
2021-12-17 15:57:11 +01:00
|
|
|
try {
|
2021-09-30 00:06:20 +02:00
|
|
|
// Security exception may be thrown by some browsers.
|
2021-12-14 17:10:26 +01:00
|
|
|
success = doc.execCommand("copy");
|
2021-05-12 02:51:12 +02:00
|
|
|
if (!success) {
|
2021-12-14 17:10:26 +01:00
|
|
|
this.logService.debug("Copy command unsupported or disabled.");
|
2021-09-30 00:06:20 +02:00
|
|
|
}
|
2018-06-05 21:02:53 +02:00
|
|
|
} catch (e) {
|
2022-02-24 12:10:07 +01:00
|
|
|
// eslint-disable-next-line
|
2018-06-05 21:02:53 +02:00
|
|
|
console.warn("Copy to clipboard failed.", e);
|
|
|
|
} finally {
|
2018-07-26 04:01:26 +02:00
|
|
|
copyEl.removeChild(textarea);
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2021-05-12 02:51:12 +02:00
|
|
|
return success;
|
2020-12-16 22:40:10 +01:00
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2019-02-27 04:42:30 +01:00
|
|
|
readFromClipboard(options?: any): Promise<string> {
|
|
|
|
throw new Error("Cannot read from clipboard on web.");
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2020-07-24 20:39:39 +02:00
|
|
|
supportsBiometric() {
|
2021-04-07 20:42:57 +02:00
|
|
|
return Promise.resolve(false);
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2020-07-24 20:39:39 +02:00
|
|
|
authenticateBiometric() {
|
2021-04-07 20:42:57 +02:00
|
|
|
return Promise.resolve(false);
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2020-07-24 20:39:39 +02:00
|
|
|
supportsSecureStorage() {
|
2020-11-23 21:56:22 +01:00
|
|
|
return false;
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2021-09-30 00:06:20 +02:00
|
|
|
getDefaultSystemTheme(): Promise<ThemeType.Light | ThemeType.Dark> {
|
|
|
|
return Promise.resolve(this.prefersColorSchemeDark.matches ? ThemeType.Dark : ThemeType.Light);
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2021-09-30 00:06:20 +02:00
|
|
|
async getEffectiveTheme(): Promise<ThemeType.Light | ThemeType.Dark> {
|
2021-12-14 17:10:26 +01:00
|
|
|
const theme = await this.stateService.getTheme();
|
2021-09-30 00:06:20 +02:00
|
|
|
if (theme === ThemeType.Dark) {
|
|
|
|
return ThemeType.Dark;
|
|
|
|
} else if (theme === ThemeType.System) {
|
|
|
|
return this.getDefaultSystemTheme();
|
2021-12-17 15:57:11 +01:00
|
|
|
} else {
|
2021-09-30 00:06:20 +02:00
|
|
|
return ThemeType.Light;
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-30 00:06:20 +02:00
|
|
|
onDefaultSystemThemeChange(callback: (theme: ThemeType.Light | ThemeType.Dark) => unknown) {
|
2021-11-18 19:03:20 +01:00
|
|
|
try {
|
|
|
|
this.prefersColorSchemeDark.addEventListener("change", ({ matches }) => {
|
|
|
|
callback(matches ? ThemeType.Dark : ThemeType.Light);
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
// Safari older than v14
|
|
|
|
this.prefersColorSchemeDark.addListener((ev) => {
|
|
|
|
callback(ev.matches ? ThemeType.Dark : ThemeType.Light);
|
|
|
|
});
|
2020-12-16 22:40:10 +01:00
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2018-06-05 21:02:53 +02:00
|
|
|
}
|