|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +/** |
| 6 | + * This splash screen is the full-viewport loading screen shown to new users at |
| 7 | + * startup while first-run experiments enroll behind it. It shows at most once |
| 8 | + * per profile (`browser.preonboarding.splashShown`). |
| 9 | + * |
| 10 | + * When the TOU modal will show, it already includes a splash screen as |
| 11 | + * its first screen, so this standalone splash suppresses itself in that case. |
| 12 | + * |
| 13 | + * It is triggered from BrowserGlue._onWindowsRestored at |
| 14 | + * `sessionstore-windows-restored` time so that it appears before about:welcome |
| 15 | + * content renders. |
| 16 | + */ |
| 17 | + |
| 18 | +const lazy = {}; |
| 19 | + |
| 20 | +ChromeUtils.defineESModuleGetters(lazy, { |
| 21 | + ASRouterTargeting: "resource:///modules/asrouter/ASRouterTargeting.sys.mjs", |
| 22 | + BrowserWindowTracker: "resource:///modules/BrowserWindowTracker.sys.mjs", |
| 23 | + ExperimentAPI: "resource://nimbus/ExperimentAPI.sys.mjs", |
| 24 | + NimbusFeatures: "resource://nimbus/ExperimentAPI.sys.mjs", |
| 25 | + OnboardingMessageProvider: |
| 26 | + "resource:///modules/asrouter/OnboardingMessageProvider.sys.mjs", |
| 27 | + SpecialMessageActions: |
| 28 | + "resource://messaging-system/lib/SpecialMessageActions.sys.mjs", |
| 29 | + TelemetryReportingPolicy: |
| 30 | + "resource://gre/modules/TelemetryReportingPolicy.sys.mjs", |
| 31 | +}); |
| 32 | + |
| 33 | +const EXPERIMENTS_GATE_PREF = "browser.aboutwelcome.experimentsGate.enabled"; |
| 34 | +const SKIP_SPLASH_IF_LOADED_PREF = |
| 35 | + "browser.aboutwelcome.experimentsGate.skipSplashIfLoaded"; |
| 36 | +const DID_SEE_ABOUT_WELCOME_PREF = "trailhead.firstrun.didSeeAboutWelcome"; |
| 37 | +const SPLASH_SHOWN_PREF = "browser.preonboarding.splashShown"; |
| 38 | + |
| 39 | +export const PreonboardingSplash = { |
| 40 | + /** |
| 41 | + * Stub shims - tests replace these to observe or simulate showing the |
| 42 | + * spotlight without a real browser window. |
| 43 | + */ |
| 44 | + Policy: { |
| 45 | + getTopWindow: () => |
| 46 | + lazy.BrowserWindowTracker.getTopWindow({ |
| 47 | + allowFromInactiveWorkspace: true, |
| 48 | + }), |
| 49 | + showSpotlight: (config, browser) => |
| 50 | + lazy.SpecialMessageActions.handleAction(config, browser), |
| 51 | + }, |
| 52 | + |
| 53 | + /** |
| 54 | + * Entry point called from BrowserGlue at `sessionstore-windows-restored` |
| 55 | + * time. |
| 56 | + */ |
| 57 | + maybeShowStartupSplash() { |
| 58 | + if (!this._shouldShow()) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + const screens = this._getSplashScreens(); |
| 63 | + if (!screens.length) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + this._show(screens).catch(e => |
| 68 | + console.error(`PreonboardingSplash: failed to show splash: ${e}`) |
| 69 | + ); |
| 70 | + }, |
| 71 | + |
| 72 | + /** |
| 73 | + * Determine whether the standalone preonboarding splash should be shown. |
| 74 | + */ |
| 75 | + _shouldShow() { |
| 76 | + if (Services.prefs.getBoolPref(SPLASH_SHOWN_PREF, false)) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + if (!Services.prefs.getBoolPref(EXPERIMENTS_GATE_PREF, false)) { |
| 81 | + return false; |
| 82 | + } |
| 83 | + |
| 84 | + // The splash exists to allow for first-run experiments to load behind it. |
| 85 | + // If Nimbus is disabled (e.g. in automation) there is nothing to wait for, |
| 86 | + // and no reason to show it. |
| 87 | + if (!lazy.ExperimentAPI.enabled) { |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + // Likewise if experiments have already loaded there is nothing to wait |
| 92 | + // for. This mirrors the loading screen's own render-time targeting; the |
| 93 | + // decision must be made here because a spotlight whose only screen is |
| 94 | + // filtered out at render time shows an empty modal. |
| 95 | + if ( |
| 96 | + Services.prefs.getBoolPref(SKIP_SPLASH_IF_LOADED_PREF, true) && |
| 97 | + lazy.ASRouterTargeting.Environment.experimentsLoaded |
| 98 | + ) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + // When the full TOU modal will show, it already includes its own splash as its |
| 103 | + // first screen, so this standalone splash doesn't need to fire. |
| 104 | + if (lazy.TelemetryReportingPolicy.willShowTOUModal()) { |
| 105 | + return false; |
| 106 | + } |
| 107 | + |
| 108 | + // Only show the splash to new users who have not already interacted with the TOU. |
| 109 | + if ( |
| 110 | + Services.prefs.getBoolPref(DID_SEE_ABOUT_WELCOME_PREF, false) || |
| 111 | + lazy.TelemetryReportingPolicy.hasUserResolvedTermsOfUse() |
| 112 | + ) { |
| 113 | + return false; |
| 114 | + } |
| 115 | + |
| 116 | + return true; |
| 117 | + }, |
| 118 | + |
| 119 | + /** |
| 120 | + * Source the splash screen ("TOU_ONBOARDING_LOADING") from the `preonboarding` |
| 121 | + * Nimbus feature, falling back to the built-in default onboarding message |
| 122 | + * so the splash is consistent with the TOU modal's screen |
| 123 | + * |
| 124 | + * @return {object[]} Exactly one splash screen, or [] if unavailable. |
| 125 | + */ |
| 126 | + _getSplashScreens() { |
| 127 | + let variables = lazy.NimbusFeatures.preonboarding.getAllVariables(); |
| 128 | + |
| 129 | + // Explicit disablement is a hard opt-out, even when an experiment supplies |
| 130 | + // screens, and even on eligible Linux distributions where the TOU flow |
| 131 | + // enables itself at runtime. Automation relies on |
| 132 | + // `browser.preonboarding.enabled=false` to keep this UI out of tests, and |
| 133 | + // CI Linux builds include a `mozilla-official` distribution id, so enabling |
| 134 | + // preonboarding at runtime should never override the opt-out here. |
| 135 | + if (variables.enabled === false) { |
| 136 | + return []; |
| 137 | + } |
| 138 | + |
| 139 | + variables = |
| 140 | + lazy.OnboardingMessageProvider.getPreonboardingVariablesWithDefaults( |
| 141 | + variables |
| 142 | + ); |
| 143 | + |
| 144 | + const screens = Array.isArray(variables?.screens) ? variables.screens : []; |
| 145 | + const splashScreens = screens.filter( |
| 146 | + screen => screen.id === "TOU_ONBOARDING_LOADING" |
| 147 | + ); |
| 148 | + |
| 149 | + // We expect exactly one screen. |
| 150 | + if (splashScreens.length !== 1) { |
| 151 | + return []; |
| 152 | + } |
| 153 | + |
| 154 | + // _shouldShow already decided visibility; the render-time targeting filter |
| 155 | + // must not re-decide, or the spotlight could render zero screens. |
| 156 | + return splashScreens.map(({ targeting: _targeting, ...screen }) => screen); |
| 157 | + }, |
| 158 | + |
| 159 | + /** |
| 160 | + * Show the standalone preonboarding splash. |
| 161 | + * |
| 162 | + * @param {object[]} screens the screens from `_getSplashScreens`. |
| 163 | + * @return {Promise<boolean>} `true` if the splash was shown. |
| 164 | + */ |
| 165 | + async _show(screens) { |
| 166 | + let win = this.Policy.getTopWindow(); |
| 167 | + if (!win) { |
| 168 | + return false; |
| 169 | + } |
| 170 | + |
| 171 | + const config = { |
| 172 | + type: "SHOW_SPOTLIGHT", |
| 173 | + data: { |
| 174 | + content: { |
| 175 | + template: "multistage", |
| 176 | + id: "PRE_ONBOARDING_SPLASH", |
| 177 | + screens, |
| 178 | + // The splash auto-advances, but can be dismissed with esc |
| 179 | + disableEscClose: false, |
| 180 | + }, |
| 181 | + }, |
| 182 | + }; |
| 183 | + |
| 184 | + this.Policy.showSpotlight(config, win.gBrowser.selectedBrowser); |
| 185 | + Services.prefs.setBoolPref(SPLASH_SHOWN_PREF, true); |
| 186 | + |
| 187 | + return true; |
| 188 | + }, |
| 189 | +}; |
0 commit comments