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/. */
5 import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs";
9 ChromeUtils.defineESModuleGetters(lazy, {
10 Normandy: "resource://normandy/Normandy.sys.mjs",
11 TaskScheduler: "resource://gre/modules/TaskScheduler.sys.mjs",
14 const PREF_TIMEOUT = "first-startup.timeout";
17 * Service for blocking application startup, to be used on the first install. The intended
18 * use case is for `FirstStartup` to be invoked when the application is called by an installer,
19 * such as the Windows Stub Installer, to allow the application to do some first-install tasks
20 * such as performance tuning and downloading critical data.
22 * In this scenario, the installer does not exit until the first application window appears,
23 * which gives the user experience of the application starting up quickly on first install.
25 export var FirstStartup = {
32 _state: 0, // NOT_STARTED,
34 * Initialize and run first-startup services. This will always run synchronously
35 * and spin the event loop until either all required services have
36 * completed, or until a timeout is reached.
38 * In the latter case, services are expected to run post-UI instead as usual.
41 this._state = this.IN_PROGRESS;
42 const timeout = Services.prefs.getIntPref(PREF_TIMEOUT, 30000); // default to 30 seconds
43 let startingTime = Cu.now();
44 let initialized = false;
48 let normandyInitEndTime = null;
49 if (AppConstants.MOZ_NORMANDY) {
51 lazy.Normandy.init({ runAsync: false }).finally(() => {
52 normandyInitEndTime = Cu.now();
57 let deleteTasksEndTime = null;
58 if (AppConstants.MOZ_UPDATE_AGENT) {
59 // It's technically possible for a previous installation to leave an old
60 // OS-level scheduled task around. Start fresh.
62 lazy.TaskScheduler.deleteAllTasks()
65 deleteTasksEndTime = Cu.now();
70 if (promises.length) {
71 Promise.all(promises).then(() => (initialized = true));
74 Services.tm.spinEventLoopUntil("FirstStartup.sys.mjs:init", () => {
75 this.elapsed = Math.round(Cu.now() - startingTime);
76 if (this.elapsed >= timeout) {
77 this._state = this.TIMED_OUT;
79 } else if (initialized) {
80 this._state = this.SUCCESS;
86 this._state = this.UNSUPPORTED;
89 if (AppConstants.MOZ_NORMANDY) {
90 Glean.firstStartup.normandyInitTime.set(
91 Math.ceil(normandyInitEndTime || Cu.now() - startingTime)
95 if (AppConstants.MOZ_UPDATE_AGENT) {
96 Glean.firstStartup.deleteTasksTime.set(
97 Math.ceil(deleteTasksEndTime || Cu.now() - startingTime)
101 Glean.firstStartup.statusCode.set(this._state);
102 Glean.firstStartup.elapsed.set(this.elapsed);
103 GleanPings.firstStartup.submit();