diff --git a/apps/browser/src/platform/offscreen-document/abstractions/offscreen-document.ts b/apps/browser/src/platform/offscreen-document/abstractions/offscreen-document.ts index 2d3c6a3e71..012f908f78 100644 --- a/apps/browser/src/platform/offscreen-document/abstractions/offscreen-document.ts +++ b/apps/browser/src/platform/offscreen-document/abstractions/offscreen-document.ts @@ -20,6 +20,7 @@ export interface OffscreenDocument { } export abstract class OffscreenDocumentService { + abstract offscreenApiSupported(): boolean; abstract withDocument( reasons: chrome.offscreen.Reason[], justification: string, diff --git a/apps/browser/src/platform/offscreen-document/offscreen-document.service.spec.ts b/apps/browser/src/platform/offscreen-document/offscreen-document.service.spec.ts index c9bdd823a5..da54140396 100644 --- a/apps/browser/src/platform/offscreen-document/offscreen-document.service.spec.ts +++ b/apps/browser/src/platform/offscreen-document/offscreen-document.service.spec.ts @@ -49,6 +49,12 @@ describe.each([ jest.resetAllMocks(); }); + describe("offscreenApiSupported", () => { + it("indicates whether the offscreen API is supported", () => { + expect(sut.offscreenApiSupported()).toBe(true); + }); + }); + describe("withDocument", () => { it("creates a document when none exists", async () => { await sut.withDocument(reasons, justification, () => {}); diff --git a/apps/browser/src/platform/offscreen-document/offscreen-document.service.ts b/apps/browser/src/platform/offscreen-document/offscreen-document.service.ts index a260e3ca6c..3a1227ea5e 100644 --- a/apps/browser/src/platform/offscreen-document/offscreen-document.service.ts +++ b/apps/browser/src/platform/offscreen-document/offscreen-document.service.ts @@ -1,10 +1,16 @@ import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; -export class DefaultOffscreenDocumentService implements DefaultOffscreenDocumentService { +import { OffscreenDocumentService } from "./abstractions/offscreen-document"; + +export class DefaultOffscreenDocumentService implements OffscreenDocumentService { private workerCount = 0; constructor(private logService: LogService) {} + offscreenApiSupported(): boolean { + return typeof chrome.offscreen !== "undefined"; + } + async withDocument( reasons: chrome.offscreen.Reason[], justification: string, diff --git a/apps/browser/src/platform/services/platform-utils/browser-platform-utils.service.spec.ts b/apps/browser/src/platform/services/platform-utils/browser-platform-utils.service.spec.ts index 02c10b62cc..c86c915801 100644 --- a/apps/browser/src/platform/services/platform-utils/browser-platform-utils.service.spec.ts +++ b/apps/browser/src/platform/services/platform-utils/browser-platform-utils.service.spec.ts @@ -229,9 +229,7 @@ describe("Browser Utils Service", () => { it("copies the passed text using the offscreen document if the extension is using manifest v3", async () => { const text = "test"; - jest - .spyOn(browserPlatformUtilsService, "getDevice") - .mockReturnValue(DeviceType.ChromeExtension); + offscreenDocumentService.offscreenApiSupported.mockReturnValue(true); getManifestVersionSpy.mockReturnValue(3); browserPlatformUtilsService.copyToClipboard(text); @@ -304,9 +302,7 @@ describe("Browser Utils Service", () => { }); it("reads the clipboard text using the offscreen document", async () => { - jest - .spyOn(browserPlatformUtilsService, "getDevice") - .mockReturnValue(DeviceType.ChromeExtension); + offscreenDocumentService.offscreenApiSupported.mockReturnValue(true); getManifestVersionSpy.mockReturnValue(3); offscreenDocumentService.withDocument.mockImplementationOnce((_, __, callback) => Promise.resolve("test"), diff --git a/apps/browser/src/platform/services/platform-utils/browser-platform-utils.service.ts b/apps/browser/src/platform/services/platform-utils/browser-platform-utils.service.ts index 4163ca9310..26108e60b7 100644 --- a/apps/browser/src/platform/services/platform-utils/browser-platform-utils.service.ts +++ b/apps/browser/src/platform/services/platform-utils/browser-platform-utils.service.ts @@ -243,7 +243,7 @@ export abstract class BrowserPlatformUtilsService implements PlatformUtilsServic text = "\u0000"; } - if (this.isChrome() && BrowserApi.isManifestVersion(3)) { + if (BrowserApi.isManifestVersion(3) && this.offscreenDocumentService.offscreenApiSupported()) { void this.triggerOffscreenCopyToClipboard(text).then(handleClipboardWriteCallback); return; @@ -268,7 +268,7 @@ export abstract class BrowserPlatformUtilsService implements PlatformUtilsServic return await SafariApp.sendMessageToApp("readFromClipboard"); } - if (this.isChrome() && BrowserApi.isManifestVersion(3)) { + if (BrowserApi.isManifestVersion(3) && this.offscreenDocumentService.offscreenApiSupported()) { return await this.triggerOffscreenReadFromClipboard(); }