Skip to content

Commit 575b3ef

Browse files
committed
Add comments, cleanup global namespace
1 parent acdd7dc commit 575b3ef

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

app/scripts/app-init.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,19 @@ const registerInPageContentScript = async () => {
184184
}
185185
};
186186

187+
// On MV3 builds we must listen for this event in `app-init`, otherwise we found that the listener
188+
// is never called.
189+
// For MV2 builds, the listener is added in `background.js` instead.
187190
chrome.runtime.onInstalled.addListener(function (details) {
188191
if (details.reason === 'install') {
192+
// This condition is for when `background.js` was loaded before the `onInstalled` listener was
193+
// called.
189194
if (globalThis.__metamaskTriggerOnInstall) {
190195
globalThis.__metamaskTriggerOnInstall();
196+
// Delete just to clean up global namespace
197+
delete globalThis.__metamaskTriggerOnInstall;
198+
// This condition is for when the `onInstalled` listener in `app-init` was called before
199+
// `background.js` was loaded.
191200
} else {
192201
globalThis.__metamaskWasJustInstalled = true;
193202
}

app/scripts/background.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,26 @@ const PHISHING_WARNING_PAGE_TIMEOUT = ONE_SECOND_IN_MILLISECONDS;
141141
// Event emitter for state persistence
142142
export const statePersistenceEvents = new EventEmitter();
143143

144-
if (isFirefox || !isManifestV3) {
144+
// On MV3 builds we must listen for this event in `app-init`, otherwise we found that the listener
145+
// is never called.
146+
// There is no `app-init` file on MV2 builds, so we add a listener here instead.
147+
if (!isManifestV3) {
145148
browser.runtime.onInstalled.addListener(function (details) {
146149
if (details.reason === 'install') {
147-
onFirstInstall();
150+
onInstall();
148151
}
149152
});
153+
154+
// This condition is for when the `onInstalled` listener in `app-init` was called before
155+
// `background.js` was loaded.
150156
} else if (globalThis.__metamaskWasJustInstalled) {
151-
onFirstInstall();
157+
onInstall();
158+
// Delete just to clean up global namespace
159+
delete globalThis.__metamaskWasJustInstalled;
160+
// This condition is for when `background.js` was loaded before the `onInstalled` listener was
161+
// called.
152162
} else {
153-
globalThis.__metamaskTriggerOnInstall = () => onFirstInstall();
163+
globalThis.__metamaskTriggerOnInstall = () => onInstall();
154164
}
155165

156166
/**
@@ -1274,8 +1284,10 @@ const addAppInstalledEvent = () => {
12741284
}, 500);
12751285
};
12761286

1277-
// On first install, open a new tab with MetaMask
1278-
function onFirstInstall() {
1287+
/**
1288+
* Trigger actions that should happen only upon initial install (e.g. open tab for onboarding).
1289+
*/
1290+
function onInstall() {
12791291
log.debug('First install detected');
12801292
if (process.env.IN_TEST) {
12811293
addAppInstalledEvent();

0 commit comments

Comments
 (0)