|
| 1 | +# Browser Startup |
| 2 | + |
| 3 | +## How do first run/first startup experiments work? |
| 4 | + |
| 5 | +Why does synchronously reading Nimbus feature values work for |
| 6 | +customizing display features like `about:welcome` onboarding and the |
| 7 | +default browser prompt? The key invariant is that the display |
| 8 | +decisions wait for `sessionstore-windows-restored` to show |
| 9 | +customizable UI, and therefore we just need Nimbus available to read |
| 10 | +at that point. This is arranged either via the `--first-startup` |
| 11 | +flag; or, for subsequent startups, the relevant Nimbus features being |
| 12 | +marked `isEarlyStartup: true`. When `isEarlyStartup: true`, Nimbus |
| 13 | +caches all its feature variables as Gecko preferences, ready to be |
| 14 | +read during early startup. (See [the early startup |
| 15 | +docs](https://experimenter.info/faq/early-startup/what-do-it-do).) |
| 16 | + |
| 17 | +Customizable display features like `about:welcome` or the default |
| 18 | +browser prompt are used in |
| 19 | +[`_maybeShowDefaultBrowserPrompt()`](https://searchfox.org/mozilla-central/rev/a965e3c683ecc035dee1de72bd33a8d91b1203ed/browser/components/BrowserGlue.sys.mjs#4685), |
| 20 | +which is invoked as part of a startup idle task. Startup idle tasks |
| 21 | +are [scheduled in response to |
| 22 | +`sessionstore-windows-restored`](https://searchfox.org/mozilla-central/rev/a965e3c683ecc035dee1de72bd33a8d91b1203ed/browser/components/BrowserGlue.sys.mjs#2423). |
| 23 | + |
| 24 | +Now, why is `sessionstore-windows-restored` late enough for a |
| 25 | +first startup experiment? The answer is subtle. |
| 26 | + |
| 27 | +During Firefox launch, [`final-ui-startup` is |
| 28 | +notified](https://searchfox.org/mozilla-central/rev/a965e3c683ecc035dee1de72bd33a8d91b1203ed/toolkit/xre/nsAppRunner.cpp#5764-5765), |
| 29 | +and in response `SessionStore` is initialized. Additionally, |
| 30 | +Nimbus/Normandy initialization is [started but not |
| 31 | +awaited](https://searchfox.org/mozilla-central/rev/a965e3c683ecc035dee1de72bd33a8d91b1203ed/browser/components/BrowserGlue.sys.mjs#1487). |
| 32 | + |
| 33 | +Then the [command line is |
| 34 | +handled](https://searchfox.org/mozilla-central/rev/a965e3c683ecc035dee1de72bd33a8d91b1203ed/toolkit/xre/nsAppRunner.cpp#5775-5778). |
| 35 | +When `--first-startup` is passed, we [spin the event loop to allow |
| 36 | +Nimbus/Normandy time to complete its initialization and first |
| 37 | +fetch](https://searchfox.org/mozilla-central/rev/a965e3c683ecc035dee1de72bd33a8d91b1203ed/browser/components/BrowserContentHandler.sys.mjs#677) |
| 38 | +before continuing to process the command line. See [the |
| 39 | +`FirstStartup` |
| 40 | +module](https://firefox-source-docs.mozilla.org/toolkit/modules/toolkit_modules/FirstStartup.html). |
| 41 | +(Important caveat: `--first-startup` is only used on Windows; see [Bug |
| 42 | +1872934](https://bugzilla.mozilla.org/show_bug.cgi?id=1872934), for |
| 43 | +example.) |
| 44 | + |
| 45 | +This races with `SessionStore`, which itself waits for the first |
| 46 | +browser window to be shown -- in particular, the |
| 47 | +`sessionstore-windows-restored` notification waits for the [first |
| 48 | +browser window's `browser-delayed-startup-finished` |
| 49 | +notification](https://searchfox.org/mozilla-central/rev/a965e3c683ecc035dee1de72bd33a8d91b1203ed/browser/components/sessionstore/SessionStore.sys.mjs#2147-2159). |
| 50 | + |
| 51 | +This first `browser-delayed-startup-finished` notification **is not |
| 52 | +guaranteed** to be after `--first-startup` has spun the event loop! |
| 53 | +But, when launched with only `--first-startup` and flags considered |
| 54 | +very early in `nsAppRunner.cpp` -- as [the stub installer |
| 55 | +does](https://searchfox.org/mozilla-central/rev/a965e3c683ecc035dee1de72bd33a8d91b1203ed/browser/installer/windows/nsis/stub.nsi#1424-1456) |
| 56 | +-- then the first window **is guaranteed** to be after the event loop |
| 57 | +has been spun, and therefore `sessionstore-windows-restored` is after |
| 58 | +as well. (As a counter-example: try `firefox.exe --browser |
| 59 | +--first-startup` and witness the [`--browser` |
| 60 | +flag](https://searchfox.org/mozilla-central/rev/a965e3c683ecc035dee1de72bd33a8d91b1203ed/browser/components/BrowserContentHandler.sys.mjs#505-508) |
| 61 | +creating a window before spinning the event loop, inadvertently racing |
| 62 | +against `sessionstore-windows-restored`.) Making this deterministic |
| 63 | +is tracked by [Bug |
| 64 | +1944431](https://bugzilla.mozilla.org/show_bug.cgi?id=1944431). |
| 65 | + |
| 66 | +Together, this means that first-startup experiments will be loaded in |
| 67 | +time to impact display features such as `about:welcome` and the |
| 68 | +default browser prompt, and we should not have a "split brain" |
| 69 | +scenario in which the Nimbus feature is intermittently unavailable to |
| 70 | +the relevant display features. |
0 commit comments