Skip to content

Conversation

@Gudahtt
Copy link
Member

@Gudahtt Gudahtt commented Mar 26, 2025

Description

Prevent tab from opening upon every browser startup.

A recent PR updated the logic for how we determine whether the extension was just installed or not; we used to check state, but now we use the runtime.onInstalled event instead to store a flag in session storage indicating whether the extension was just installed.

Unfortunately this PR had a couple of bugs; the condition using this flag was accidentally reversed, and the read from session storage was never successful because of a race condition (the write had not finished yet).

To address both problems, the logic for detecting first install was refactored to use a global variable instead of session storage. Globals can be accessed and written synchronously, preventing any possibility of a race condition.

The solution was complicated by the fact that MV3 builds must add an onInstalled listener in the root service worker module, otherwise the listener is never called (we tested this experimentally). It was also unclear whether the listener would always run before or after the background.js script loading, and we need to trigger an action in background.js on install. But we've accounted for both possibilities in this solution.

Open in GitHub Codespaces

Related issues

Fixes: #30924

Manual testing steps

  • Create a production-like build (yarn dist and yarn dist:mv2), or download the builds from the metamaskbot comment
  • Test that the window opens only on first install, not on later browser restarts.

Screenshots/Recordings

N/A

Pre-merge author checklist

Pre-merge reviewer checklist

  • I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed).
  • I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.

@Gudahtt Gudahtt force-pushed the fix-first-time-install-check branch 2 times, most recently from 5578d50 to 79237e9 Compare March 27, 2025 21:33
@Gudahtt
Copy link
Member Author

Gudahtt commented Mar 27, 2025

I haven't written any tests for this yet. This is a bit trick to test because we can't really unit test these integrations with the extension/service worker API. We won't know that the integration works without a real browser. And we can't test this using a standard E2E test build because the behavior we're testing (opening a new tab on install) is disabled.

We should be able to write an E2E test that uses a production build though, similar to the vault decryption build. Given the urgency of fixing this problem, I'd prefer to use manual testing for now. I'll follow up with the E2E test in a later PR.

@Gudahtt Gudahtt force-pushed the fix-first-time-install-check branch from 79237e9 to d4713a6 Compare March 27, 2025 21:57
addAppInstalledEvent();
platform.openExtensionInBrowser();
}
onNavigateToTab();
Copy link
Member Author

@Gudahtt Gudahtt Mar 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why this function was here, it's not really related to the install event. This was being called irrespective of whether this session was the first after install as well, which is confusing.

I've moved it down to initBackground, so it should get called as part of initialization the same way it always was before.

Copy link
Contributor

@davidmurdoch davidmurdoch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Solution is a maybe bit non-idiomatic, but still much easier to reason about than the async storage complexity.

Left only some nits/questions, nothing that must be changed.

I'll give it a few runs locally tomorrow before give it the ✅

// This condition is for when `background.js` was loaded before the `onInstalled` listener was
// called.
} else {
globalThis.__metamaskTriggerOnInstall = () => onInstall();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

neat!

@Gudahtt
Copy link
Member Author

Gudahtt commented Mar 28, 2025

Not quite sure why the "App Installed Event" E2E tests are failing.

@Gudahtt Gudahtt force-pushed the fix-first-time-install-check branch from 2f3b1e3 to ac3a801 Compare March 28, 2025 16:50
@Gudahtt Gudahtt changed the title fix: Prevent fullscreen UI from opening every startup fix: cp-12.15.0 Prevent fullscreen UI from opening every startup Mar 28, 2025
@Gudahtt Gudahtt force-pushed the fix-first-time-install-check branch from 006de3c to 7e06ea8 Compare March 28, 2025 18:13
@Gudahtt Gudahtt marked this pull request as ready for review March 28, 2025 18:45
@metamaskbot
Copy link
Collaborator

Builds ready [7e06ea8]
UI Startup Metrics (1191 ± 58 ms)
PlatformBuildTypePageMetricMean (ms)Min (ms)Max (ms)Std Dev (ms)P 75 (ms)P 95 (ms)
ChromeBrowserifyHomeuiStartup1191107313435812371296
load1040942122158953991
domContentLoaded1035938121657956992
domInteractive16133541527
firstPaint771761220397249977
backgroundConnect96454910
firstReactRender19144141928
getState11433768
initialActions001001
loadScripts819728100457850924
setupStore7513278
WebpackHomeuiStartup933753120178944971
load79661191958829870
domContentLoaded78959291159825864
domInteractive15114171334
firstPaint43760875339819863
backgroundConnect16104781439
firstReactRender14122841325
getState6413268
initialActions001001
loadScripts78758289558823857
setupStore7517278
FirefoxBrowserifyHomeuiStartup14051221196214514321753
load12661080180714113091574
domContentLoaded12661080180614113091574
domInteractive10137163288497
firstPaintNaNNaNNaNNaNNaNNaN
backgroundConnect24175472540
firstReactRender23195152531
getState7430378
initialActions001001
loadScripts12441062178313912881546
setupStore6318267
WebpackHomeuiStartup10078461751161911973
load8837371495144831977
domContentLoaded8837361494144830977
domInteractive115361732514490
firstPaintNaNNaNNaNNaNNaNNaN
backgroundConnect21147492236
firstReactRender19164031925
getState1041061389
initialActions001001
loadScripts8657221464140824983
setupStore8537478
Bundle size diffs [🚀 Bundle size reduced!]
  • background: -404 Bytes (-0.01%)
  • ui: 0 Bytes (0%)
  • common: 0 Bytes (0%)

@metamaskbot
Copy link
Collaborator

Builds ready [18e2d1b]
UI Startup Metrics (1172 ± 54 ms)
PlatformBuildTypePageMetricMean (ms)Min (ms)Max (ms)Std Dev (ms)P 75 (ms)P 95 (ms)
ChromeBrowserifyHomeuiStartup1172105713295412061259
load1021931116550954992
domContentLoaded1014928115751950990
domInteractive15123241526
firstPaint723921165399247981
backgroundConnect116629910
firstReactRender19134652029
getState11435768
initialActions004001
loadScripts80070095152835884
setupStore7513279
WebpackHomeuiStartup987819129383982995
load84169696249861935
domContentLoaded83669295048854924
domInteractive16124471435
firstPaint51158906348852868
backgroundConnect16114281540
firstReactRender14122931425
getState7412188
initialActions001001
loadScripts83369193947852914
setupStore7516279
FirefoxBrowserifyHomeuiStartup13721188192915214001782
load12341063179014612681623
domContentLoaded12331063179014612671623
domInteractive10839180248196
firstPaintNaNNaNNaNNaNNaNNaN
backgroundConnect2416152142535
firstReactRender22185152429
getState7423378
initialActions001001
loadScripts12111044176714312491598
setupStore6427367
WebpackHomeuiStartup9688271573160883982
load8497231334141808936
domContentLoaded8487231334141808935
domInteractive115341972317696
firstPaintNaNNaNNaNNaNNaNNaN
backgroundConnect201382112233
firstReactRender18162721824
getState948513785
initialActions001001
loadScripts8327091310138795925
setupStore8559878
Bundle size diffs [🚀 Bundle size reduced!]
  • background: -404 Bytes (-0.01%)
  • ui: 0 Bytes (0%)
  • common: 0 Bytes (0%)

Copy link
Contributor

@davidmurdoch davidmurdoch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Firefox I was automatically greeted with the "Welcome back" login screen, not the "Let's get started" screen, in fullscreen after installing the extension as a "Temporary Add-on".

My hunch is that this is because I had previously installed it this way, so this was a new install, but firefox persisted the database from the last time I installed MM as a Temporary add-on.

If I manually remove MM (instead of waiting for it to expire and get automatically removed by FF) and then install it again it opens in full screen with the "Let's get started" screen, as expected.

I think this behavior is fine, it is a change from the original way we did this (check if we had written to storage before), so I just wanted to mention it.

@Gudahtt Gudahtt enabled auto-merge March 28, 2025 21:13
@Gudahtt Gudahtt added this pull request to the merge queue Mar 28, 2025
Merged via the queue into main with commit 108263d Mar 28, 2025
149 checks passed
@Gudahtt Gudahtt deleted the fix-first-time-install-check branch March 28, 2025 23:08
@github-actions github-actions bot locked and limited conversation to collaborators Mar 28, 2025
@metamaskbot metamaskbot added the release-12.17.0 Issue or pull request that will be included in release 12.17.0 label Mar 28, 2025
@Gudahtt
Copy link
Member Author

Gudahtt commented Mar 31, 2025

E2E test added in #31435

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

release-12.17.0 Issue or pull request that will be included in release 12.17.0 team-wallet-framework

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: The extension starts automatically after restart / reload

5 participants