Skip to content

Commit

Permalink
fix: Handle error when offscreen document already exists
Browse files Browse the repository at this point in the history
A `try..catch` block has been added to catch errors about the Offscreen
document already existing. This circumstance is not a bug, it's
expected to happen sometimes due to race conditions between
`hasDocument` and the creation step. The code in question is just meant
to create the document if it doesn't exist, so it's OK if we get this
error.

The `hasDocument` call has been removed as well, since it's now
redundant. This function wasn't safe to call anyway; it's an
undocumented private function.

See here for more information:
* https://stackoverflow.com/a/7725839
* https://groups.google.com/a/chromium.org/g/chromium-extensions/c/D5Jg2ukyvUc4

Fixes #25118
  • Loading branch information
Gudahtt committed Jun 7, 2024
1 parent 47706eb commit c00e8f7
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions app/scripts/app-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,24 @@ registerInPageContentScript();
* folder for more details.
*/
async function createOffscreen() {
if (await chrome.offscreen.hasDocument()) {
return;
try {
await chrome.offscreen.createDocument({
url: './offscreen.html',
reasons: ['IFRAME_SCRIPTING'],
justification:
'Used for Hardware Wallet and Snaps scripts to communicate with the extension.',
});
console.debug('Offscreen iframe loaded');
} catch (error) {
if (
!error?.message?.startsWith(
'Only a single offscreen document may be created',
)
) {
throw error;
}
console.debug('Offscreen document already exists; skipping creation');
}

await chrome.offscreen.createDocument({
url: './offscreen.html',
reasons: ['IFRAME_SCRIPTING'],
justification:
'Used for Hardware Wallet and Snaps scripts to communicate with the extension.',
});

console.debug('Offscreen iframe loaded');
}

createOffscreen();

0 comments on commit c00e8f7

Please sign in to comment.