[PM-8159] [PM-8158] [PM-8156] Swallow multiple offscreen document errors (#9195)

* Swallow multiple offscreen document errors

The API has race issues with determining if an offscreen document exists (https://groups.google.com/a/chromium.org/g/chromium-extensions/c/s2Wp55bjySE/m/SnjJu1MdAAAJ). However, there are no negative effects of attempting to open multiple other than this throw.

* Resolve circular dependency
This commit is contained in:
Matt Gibson 2024-05-15 17:41:04 -04:00 committed by GitHub
parent 4ccf920da8
commit e55e3d5b9b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 33 additions and 13 deletions

View File

@ -380,7 +380,8 @@ export default class MainBackground {
const logoutCallback = async (expired: boolean, userId?: UserId) =>
await this.logout(expired, userId);
this.logService = new ConsoleLogService(false);
const isDev = process.env.ENV === "development";
this.logService = new ConsoleLogService(isDev);
this.cryptoFunctionService = new WebCryptoFunctionService(self);
this.keyGenerationService = new KeyGenerationService(this.cryptoFunctionService);
this.storageService = new BrowserLocalStorageService();
@ -399,7 +400,7 @@ export default class MainBackground {
),
);
this.offscreenDocumentService = new DefaultOffscreenDocumentService();
this.offscreenDocumentService = new DefaultOffscreenDocumentService(this.logService);
this.platformUtilsService = new BackgroundPlatformUtilsService(
this.messagingService,

View File

@ -1,3 +1,7 @@
import { mock } from "jest-mock-extended";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { DefaultOffscreenDocumentService } from "./offscreen-document.service";
class TestCase {
@ -21,6 +25,7 @@ describe.each([
new TestCase("synchronous callback", () => 42),
new TestCase("asynchronous callback", () => Promise.resolve(42)),
])("DefaultOffscreenDocumentService %s", (testCase) => {
const logService = mock<LogService>();
let sut: DefaultOffscreenDocumentService;
const reasons = [chrome.offscreen.Reason.TESTING];
const justification = "justification is testing";
@ -37,7 +42,7 @@ describe.each([
callback = testCase.callback;
chrome.offscreen = api;
sut = new DefaultOffscreenDocumentService();
sut = new DefaultOffscreenDocumentService(logService);
});
afterEach(() => {

View File

@ -1,7 +1,9 @@
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
export class DefaultOffscreenDocumentService implements DefaultOffscreenDocumentService {
private workerCount = 0;
constructor() {}
constructor(private logService: LogService) {}
async withDocument<T>(
reasons: chrome.offscreen.Reason[],
@ -24,11 +26,21 @@ export class DefaultOffscreenDocumentService implements DefaultOffscreenDocument
}
private async create(reasons: chrome.offscreen.Reason[], justification: string): Promise<void> {
await chrome.offscreen.createDocument({
url: "offscreen-document/index.html",
reasons,
justification,
});
try {
await chrome.offscreen.createDocument({
url: "offscreen-document/index.html",
reasons,
justification,
});
} catch (e) {
// gobble multiple offscreen document creation errors
// TODO: remove this when the offscreen document service is fixed PM-8014
if (e.message === "Only a single offscreen document may be created.") {
this.logService.info("Ignoring offscreen document creation error.");
return;
}
throw e;
}
}
private async close(): Promise<void> {

View File

@ -195,9 +195,11 @@ const safeProviders: SafeProvider[] = [
}),
safeProvider({
provide: LogService,
useFactory: (platformUtilsService: PlatformUtilsService) =>
new ConsoleLogService(platformUtilsService.isDev()),
deps: [PlatformUtilsService],
useFactory: () => {
const isDev = process.env.ENV === "development";
return new ConsoleLogService(isDev);
},
deps: [],
}),
safeProvider({
provide: EnvironmentService,
@ -286,7 +288,7 @@ const safeProviders: SafeProvider[] = [
safeProvider({
provide: OffscreenDocumentService,
useClass: DefaultOffscreenDocumentService,
deps: [],
deps: [LogService],
}),
safeProvider({
provide: PlatformUtilsService,