1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-04 05:08:06 +02:00

[PM-8276] Edge Cannot Copy to Clipboard When Using Mv3 (#9266)

* [PM-8276] Edge Cannot Copy to Clipboard When Using Mv3

* [PM-8276] Fixing jest tests

* [PM-8276] Fixing jest tests

* [PM-8276] Fixing jest tests
This commit is contained in:
Cesar Gonzalez 2024-05-20 14:37:55 -05:00 committed by GitHub
parent 6fd81fa4d0
commit ac633687ec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 18 additions and 9 deletions

View File

@ -20,6 +20,7 @@ export interface OffscreenDocument {
} }
export abstract class OffscreenDocumentService { export abstract class OffscreenDocumentService {
abstract offscreenApiSupported(): boolean;
abstract withDocument<T>( abstract withDocument<T>(
reasons: chrome.offscreen.Reason[], reasons: chrome.offscreen.Reason[],
justification: string, justification: string,

View File

@ -49,6 +49,12 @@ describe.each([
jest.resetAllMocks(); jest.resetAllMocks();
}); });
describe("offscreenApiSupported", () => {
it("indicates whether the offscreen API is supported", () => {
expect(sut.offscreenApiSupported()).toBe(true);
});
});
describe("withDocument", () => { describe("withDocument", () => {
it("creates a document when none exists", async () => { it("creates a document when none exists", async () => {
await sut.withDocument(reasons, justification, () => {}); await sut.withDocument(reasons, justification, () => {});

View File

@ -1,10 +1,16 @@
import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; 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; private workerCount = 0;
constructor(private logService: LogService) {} constructor(private logService: LogService) {}
offscreenApiSupported(): boolean {
return typeof chrome.offscreen !== "undefined";
}
async withDocument<T>( async withDocument<T>(
reasons: chrome.offscreen.Reason[], reasons: chrome.offscreen.Reason[],
justification: string, justification: string,

View File

@ -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 () => { it("copies the passed text using the offscreen document if the extension is using manifest v3", async () => {
const text = "test"; const text = "test";
jest offscreenDocumentService.offscreenApiSupported.mockReturnValue(true);
.spyOn(browserPlatformUtilsService, "getDevice")
.mockReturnValue(DeviceType.ChromeExtension);
getManifestVersionSpy.mockReturnValue(3); getManifestVersionSpy.mockReturnValue(3);
browserPlatformUtilsService.copyToClipboard(text); browserPlatformUtilsService.copyToClipboard(text);
@ -304,9 +302,7 @@ describe("Browser Utils Service", () => {
}); });
it("reads the clipboard text using the offscreen document", async () => { it("reads the clipboard text using the offscreen document", async () => {
jest offscreenDocumentService.offscreenApiSupported.mockReturnValue(true);
.spyOn(browserPlatformUtilsService, "getDevice")
.mockReturnValue(DeviceType.ChromeExtension);
getManifestVersionSpy.mockReturnValue(3); getManifestVersionSpy.mockReturnValue(3);
offscreenDocumentService.withDocument.mockImplementationOnce((_, __, callback) => offscreenDocumentService.withDocument.mockImplementationOnce((_, __, callback) =>
Promise.resolve("test"), Promise.resolve("test"),

View File

@ -243,7 +243,7 @@ export abstract class BrowserPlatformUtilsService implements PlatformUtilsServic
text = "\u0000"; text = "\u0000";
} }
if (this.isChrome() && BrowserApi.isManifestVersion(3)) { if (BrowserApi.isManifestVersion(3) && this.offscreenDocumentService.offscreenApiSupported()) {
void this.triggerOffscreenCopyToClipboard(text).then(handleClipboardWriteCallback); void this.triggerOffscreenCopyToClipboard(text).then(handleClipboardWriteCallback);
return; return;
@ -268,7 +268,7 @@ export abstract class BrowserPlatformUtilsService implements PlatformUtilsServic
return await SafariApp.sendMessageToApp("readFromClipboard"); return await SafariApp.sendMessageToApp("readFromClipboard");
} }
if (this.isChrome() && BrowserApi.isManifestVersion(3)) { if (BrowserApi.isManifestVersion(3) && this.offscreenDocumentService.offscreenApiSupported()) {
return await this.triggerOffscreenReadFromClipboard(); return await this.triggerOffscreenReadFromClipboard();
} }