-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Refactor offscreen creation logic (#25302)
## **Description** Refactor initialization logic to defer creation of the offscreen document until the `MetaMaskController` is initialized. This adds a `offscreenPromise` to the controller that can be awaited for functionality that requires the offscreen document to be created. Additionally this PR adds a message that the offscreen document will send once initial execution of the offscreen page has finished. This is awaited in the `offscreenPromise`. We await `offscreenPromise` before unlocking the keyrings as some keyrings rely on the offscreen document to process requests, e.g. hardware wallets. There may be room for more improvements here though, that I have not tackled in this PR. As the hardware wallet logic doesn't seem to wait for iframes to fully load, so there is a chance of some missed messages. I have tested that hardware wallet support, at least for Ledger, is still working following the changes in this PR. [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/25302?quickstart=1)
- Loading branch information
1 parent
68d35f0
commit e70e44f
Showing
6 changed files
with
64 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { OffscreenCommunicationTarget } from '../../shared/constants/offscreen-communication'; | ||
|
||
/** | ||
* Creates an offscreen document that can be used to load additional scripts | ||
* and iframes that can communicate with the extension through the chrome | ||
* runtime API. Only one offscreen document may exist, so any iframes required | ||
* by extension can be embedded in the offscreen.html file. See the offscreen | ||
* folder for more details. | ||
*/ | ||
export async function createOffscreen() { | ||
const { chrome } = globalThis; | ||
if (!chrome.offscreen || (await chrome.offscreen.hasDocument())) { | ||
return; | ||
} | ||
|
||
const loadPromise = new Promise((resolve) => { | ||
const messageListener = (msg) => { | ||
if ( | ||
msg.target === OffscreenCommunicationTarget.extensionMain && | ||
msg.isBooted | ||
) { | ||
chrome.runtime.onMessage.removeListener(messageListener); | ||
resolve(); | ||
} | ||
}; | ||
chrome.runtime.onMessage.addListener(messageListener); | ||
}); | ||
|
||
await chrome.offscreen.createDocument({ | ||
url: './offscreen.html', | ||
reasons: ['IFRAME_SCRIPTING'], | ||
justification: | ||
'Used for Hardware Wallet and Snaps scripts to communicate with the extension.', | ||
}); | ||
|
||
// In case we are in a bad state where the offscreen document is not loading, timeout and let execution continue. | ||
const timeoutPromise = new Promise((resolve) => { | ||
setTimeout(resolve, 5000); | ||
}); | ||
|
||
await Promise.race([loadPromise, timeoutPromise]); | ||
|
||
console.debug('Offscreen iframe loaded'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters