1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 import { BackgroundUpdate } from "resource://gre/modules/BackgroundUpdate.sys.mjs";
7 import { DevToolsSocketStatus } from "resource://devtools/shared/security/DevToolsSocketStatus.sys.mjs";
9 const { EXIT_CODE } = BackgroundUpdate;
11 import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
12 import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs";
16 ChromeUtils.defineESModuleGetters(lazy, {
17 AppUpdater: "resource://gre/modules/AppUpdater.sys.mjs",
18 BackgroundTasksUtils: "resource://gre/modules/BackgroundTasksUtils.sys.mjs",
19 ExtensionUtils: "resource://gre/modules/ExtensionUtils.sys.mjs",
20 NimbusFeatures: "resource://nimbus/ExperimentAPI.sys.mjs",
21 UpdateUtils: "resource://gre/modules/UpdateUtils.sys.mjs",
24 XPCOMUtils.defineLazyServiceGetter(
27 "@mozilla.org/updates/update-service;1",
28 "nsIApplicationUpdateService"
31 ChromeUtils.defineLazyGetter(lazy, "log", () => {
32 let { ConsoleAPI } = ChromeUtils.importESModule(
33 "resource://gre/modules/Console.sys.mjs"
35 let consoleOptions = {
36 // tip: set maxLogLevel to "debug" and use log.debug() to create detailed
37 // messages during development. See LOG_LEVELS in Console.sys.mjs for details.
39 maxLogLevelPref: "app.update.background.loglevel",
40 prefix: "BackgroundUpdate",
42 return new ConsoleAPI(consoleOptions);
45 export const backgroundTaskTimeoutSec = Services.prefs.getIntPref(
46 "app.update.background.timeoutSec",
51 * Verify that pre-conditions to update this installation (both persistent and
52 * transient) are fulfilled, and if they are all fulfilled, pump the update
55 * This means checking for, downloading, and potentially applying updates.
57 * @returns {any} - Returns AppUpdater status upon update loop exit.
59 async function _attemptBackgroundUpdate() {
60 let SLUG = "_attemptBackgroundUpdate";
62 // Here's where we do `post-update-processing`. Creating the stub invokes the
63 // `UpdateServiceStub()` constructor, which handles various migrations (which should not be
64 // necessary, but we want to run for consistency and any migrations added in the future) and then
65 // dispatches `post-update-processing` (if appropriate). We want to do this very early, so that
66 // the real update service is in its fully initialized state before any usage.
68 `${SLUG}: creating UpdateServiceStub() for "post-update-processing"`
70 Cc["@mozilla.org/updates/update-service-stub;1"].createInstance(
75 `${SLUG}: checking for preconditions necessary to update this installation`
77 let reasons = await BackgroundUpdate._reasonsToNotUpdateInstallation();
79 if (BackgroundUpdate._force()) {
80 // We want to allow developers and testers to monkey with the system.
82 `${SLUG}: app.update.background.force=true, ignoring reasons: ${JSON.stringify(
90 for (let reason of reasons) {
91 Glean.backgroundUpdate.reasons.add(reason);
94 let enabled = !reasons.length;
97 `${SLUG}: not running background update task: '${JSON.stringify(
102 return lazy.AppUpdater.STATUS.NEVER_CHECKED;
105 let result = new Promise(resolve => {
106 let appUpdater = new lazy.AppUpdater();
108 let _appUpdaterListener = (status, progress, progressMax) => {
109 let stringStatus = lazy.AppUpdater.STATUS.debugStringFor(status);
110 Glean.backgroundUpdate.states.add(stringStatus);
111 Glean.backgroundUpdate.finalState.set(stringStatus);
113 if (lazy.AppUpdater.STATUS.isTerminalStatus(status)) {
115 `${SLUG}: background update transitioned to terminal status ${status}: ${stringStatus}`
117 appUpdater.removeListener(_appUpdaterListener);
120 } else if (status == lazy.AppUpdater.STATUS.CHECKING) {
121 // The usual initial flow for the Background Update Task is to kick off
122 // the update download and immediately exit. For consistency, we are
123 // going to enforce this flow. So if we are just now checking for
124 // updates, we will limit the updater such that it cannot start staging,
125 // even if we immediately download the entire update.
127 `${SLUG}: This session will be limited to downloading updates only.`
129 lazy.UpdateService.onlyDownloadUpdatesThisSession = true;
131 status == lazy.AppUpdater.STATUS.DOWNLOADING &&
132 (lazy.UpdateService.onlyDownloadUpdatesThisSession ||
133 (progress !== undefined && progressMax !== undefined))
135 // We get a DOWNLOADING callback with no progress or progressMax values
136 // when we initially switch to the DOWNLOADING state. But when we get
137 // onProgress notifications, progress and progressMax will be defined.
138 // Remember to keep in mind that progressMax is a required value that
139 // we can count on being meaningful, but it will be set to -1 for BITS
140 // transfers that haven't begun yet.
142 lazy.UpdateService.onlyDownloadUpdatesThisSession ||
144 progress != progressMax
147 `${SLUG}: Download in progress. Exiting task while download ` +
150 // If the download is still in progress, we don't want the Background
151 // Update Task to hang around waiting for it to complete.
152 lazy.UpdateService.onlyDownloadUpdatesThisSession = true;
154 appUpdater.removeListener(_appUpdaterListener);
158 lazy.log.debug(`${SLUG}: Download has completed!`);
162 `${SLUG}: background update transitioned to status ${status}: ${stringStatus}`
166 appUpdater.addListener(_appUpdaterListener);
175 * Maybe submit a "background-update" custom Glean ping.
177 * If data reporting upload in general is enabled Glean will submit a ping. To determine if
178 * telemetry is enabled, Glean will look at the relevant pref, which was mirrored from the default
179 * profile. Note that the Firefox policy mechanism will manage this pref, locking it to particular
180 * values as appropriate.
182 export async function maybeSubmitBackgroundUpdatePing() {
183 let SLUG = "maybeSubmitBackgroundUpdatePing";
185 // It should be possible to turn AUSTLMY data into Glean data, but mapping histograms isn't
186 // trivial, so we don't do it at this time. Bug 1703313.
188 // Including a reason allows to differentiate pings sent as part of the task
189 // and pings queued and sent by Glean on a different schedule.
190 GleanPings.backgroundUpdate.submit("backgroundupdate_task");
192 lazy.log.info(`${SLUG}: submitted "background-update" ping`);
195 export async function runBackgroundTask(commandLine) {
196 let SLUG = "runBackgroundTask";
197 lazy.log.error(`${SLUG}: backgroundupdate`);
198 let automaticRestartFound =
199 -1 != commandLine.findFlag("automatic-restart", false);
201 // Modify Glean metrics for a successful automatic restart.
202 if (automaticRestartFound) {
203 Glean.backgroundUpdate.automaticRestartSuccess.set(true);
204 lazy.log.debug(`${SLUG}: application automatic restart completed`);
207 // Help debugging. This is a pared down version of
208 // `dataProviders.application` in `Troubleshoot.sys.mjs`. When adding to this
209 // debugging data, try to follow the form from that module.
211 name: Services.appinfo.name,
213 Services.sysinfo.getProperty("name") +
215 Services.sysinfo.getProperty("version") +
217 Services.sysinfo.getProperty("build"),
218 version: AppConstants.MOZ_APP_VERSION_DISPLAY,
219 buildID: Services.appinfo.appBuildID,
220 distributionID: Services.prefs
221 .getDefaultBranch("")
222 .getCharPref("distribution.id", ""),
223 updateChannel: lazy.UpdateUtils.UpdateChannel,
224 UpdRootD: Services.dirsvc.get("UpdRootD", Ci.nsIFile).path,
226 lazy.log.debug(`${SLUG}: current configuration`, data);
228 // Other instances running are a transient precondition (during this invocation). We'd prefer to
229 // check this later, as a reason for not updating, but Glean is not tested in multi-process
230 // environments and while its storage (backed by rkv) can in theory support multiple processes, it
231 // is not clear that it in fact does support multiple processes. So we are conservative here.
232 // There is a potential time-of-check/time-of-use race condition here, but if process B starts
233 // after we pass this test, that process should exit after it gets to this check, avoiding
234 // multiple processes using the same Glean storage. If and when more and longer-running
235 // background tasks become common, we may need to be more fine-grained and share just the Glean
237 lazy.log.debug(`${SLUG}: checking if other instance is running`);
238 let syncManager = Cc["@mozilla.org/updates/update-sync-manager;1"].getService(
239 Ci.nsIUpdateSyncManager
241 if (DevToolsSocketStatus.hasSocketOpened()) {
243 `${SLUG}: Ignoring the 'multiple instances' check because a DevTools server is listening.`
245 } else if (syncManager.isOtherInstanceRunning()) {
246 lazy.log.error(`${SLUG}: another instance is running`);
247 return EXIT_CODE.OTHER_INSTANCE;
250 // Here we mirror specific prefs from the default profile into our temporary profile. We want to
251 // do this early because some of the prefs may impact internals such as log levels. Generally,
252 // however, we want prefs from the default profile to not impact the mechanics of checking for,
253 // downloading, and applying updates, since such prefs should be be per-installation prefs, using
254 // the mechanisms of Bug 1691486. Sadly using this mechanism for many relevant prefs (namely
255 // `app.update.BITS.enabled` and `app.update.service.enabled`) is difficult: see Bug 1657533.
257 // We also read any Nimbus targeting snapshot from the default profile.
258 let defaultProfileTargetingSnapshot = {};
260 let defaultProfilePrefs;
261 await lazy.BackgroundTasksUtils.withProfileLock(async lock => {
262 let predicate = name => {
264 name.startsWith("app.update.") || // For obvious reasons.
265 name.startsWith("datareporting.") || // For Glean.
266 name.startsWith("logging.") || // For Glean.
267 name.startsWith("telemetry.fog.") || // For Glean.
268 name.startsWith("app.partner.") || // For our metrics.
269 name === "app.shield.optoutstudies.enabled" || // For Nimbus.
270 name === "services.settings.server" || // For Remote Settings via Nimbus.
271 name === "services.settings.preview_enabled" || // For Remote Settings via Nimbus.
272 name === "messaging-system.rsexperimentloader.collection_id" // For Firefox Messaging System.
276 defaultProfilePrefs = await lazy.BackgroundTasksUtils.readPreferences(
280 let telemetryClientID =
281 await lazy.BackgroundTasksUtils.readTelemetryClientID(lock);
282 Glean.backgroundUpdate.clientId.set(telemetryClientID);
284 // Read targeting snapshot, collect background update specific telemetry. Never throws.
285 defaultProfileTargetingSnapshot =
286 await BackgroundUpdate.readFirefoxMessagingSystemTargetingSnapshot(
291 for (let [name, value] of Object.entries(defaultProfilePrefs)) {
292 switch (typeof value) {
294 Services.prefs.setBoolPref(name, value);
297 Services.prefs.setIntPref(name, value);
300 Services.prefs.setCharPref(name, value);
304 `Pref from default profile with name "${name}" has unrecognized type`
309 if (!lazy.BackgroundTasksUtils.hasDefaultProfile()) {
310 lazy.log.error(`${SLUG}: caught exception; no default profile exists`, e);
311 return EXIT_CODE.DEFAULT_PROFILE_DOES_NOT_EXIST;
314 if (e.name == "CannotLockProfileError") {
316 `${SLUG}: caught exception; could not lock default profile`,
319 return EXIT_CODE.DEFAULT_PROFILE_CANNOT_BE_LOCKED;
323 `${SLUG}: caught exception reading preferences and telemetry client ID from default profile`,
326 return EXIT_CODE.DEFAULT_PROFILE_CANNOT_BE_READ;
329 // Now that we have prefs from the default profile, we can configure Firefox-on-Glean.
331 // Glean has a preinit queue for metric operations that happen before init, so
332 // this is safe. We want to have these metrics set before the first possible
333 // time we might send (built-in) pings.
334 await BackgroundUpdate.recordUpdateEnvironment();
336 // To help debugging, use the `GLEAN_LOG_PINGS` and `GLEAN_DEBUG_VIEW_TAG`
337 // environment variables: see
338 // https://mozilla.github.io/glean/book/user/debugging/index.html.
339 let gleanRoot = await IOUtils.getDirectory(
340 Services.dirsvc.get("UpdRootD", Ci.nsIFile).path,
345 Services.fog.initializeFOG(
347 "firefox.desktop.background.update"
350 // For convenience, mirror our loglevel.
351 let logLevel = Services.prefs.getCharPref(
352 "app.update.background.loglevel",
355 const logLevelPrefs = [
356 "browser.newtabpage.activity-stream.asrouter.debugLogLevel",
357 "messaging-system.log",
358 "services.settings.loglevel",
359 "toolkit.backgroundtasks.loglevel",
361 for (let logLevelPref of logLevelPrefs) {
362 lazy.log.info(`${SLUG}: setting ${logLevelPref}=${logLevel}`);
363 Services.prefs.setCharPref(logLevelPref, logLevel);
366 // The langpack updating mechanism expects the addons manager, but in background task mode, the
367 // addons manager is not present. Since we can't update langpacks from the background task
368 // temporary profile, we disable the langpack updating mechanism entirely. This relies on the
369 // default profile being the only profile that schedules the OS-level background task and ensuring
370 // the task is not scheduled when langpacks are present. Non-default profiles that have langpacks
371 // installed may experience the issues that motivated Bug 1647443. If this turns out to be a
372 // significant problem in the wild, we could store more information about profiles and their
373 // active langpacks to disable background updates in more cases, maybe in per-installation prefs.
374 Services.prefs.setBoolPref("app.update.langpack.enabled", false);
376 let result = EXIT_CODE.SUCCESS;
378 let stringStatus = lazy.AppUpdater.STATUS.debugStringFor(
379 lazy.AppUpdater.STATUS.NEVER_CHECKED
381 Glean.backgroundUpdate.states.add(stringStatus);
382 Glean.backgroundUpdate.finalState.set(stringStatus);
384 let updateStatus = lazy.AppUpdater.STATUS.NEVER_CHECKED;
386 // Return AppUpdater status from _attemptBackgroundUpdate() to
387 // check if the status is STATUS.READY_FOR_RESTART.
388 updateStatus = await _attemptBackgroundUpdate();
390 lazy.log.info(`${SLUG}: attempted background update`);
391 Glean.backgroundUpdate.exitCodeSuccess.set(true);
394 // Now that we've pumped the update loop, we can start Nimbus and the Firefox Messaging System
395 // and see if we should message the user. This minimizes the risk of messaging impacting the
396 // function of the background update system.
397 await lazy.BackgroundTasksUtils.enableNimbus(
399 defaultProfileTargetingSnapshot.environment
402 await lazy.BackgroundTasksUtils.enableFirefoxMessagingSystem(
403 defaultProfileTargetingSnapshot.environment
406 // Try to make it easy to witness errors in this system. We can pass through any exception
407 // without disrupting (future) background updates.
409 // Most meaningful issues with the Nimbus/experiments system will be reported via Glean
412 `${SLUG}: exception raised from Nimbus/Firefox Messaging System`,
418 // TODO: in the future, we might want to classify failures into transient and persistent and
419 // backoff the update task in the face of continuous persistent errors.
420 lazy.log.error(`${SLUG}: caught exception attempting background update`, e);
422 result = EXIT_CODE.EXCEPTION;
423 Glean.backgroundUpdate.exitCodeException.set(true);
425 // This is the point to report telemetry, assuming that the default profile's data reporting
426 // configuration allows it.
427 await maybeSubmitBackgroundUpdatePing();
430 // TODO: ensure the update service has persisted its state before we exit. Bug 1700846.
431 // TODO: ensure that Glean's upload mechanism is aware of Gecko shutdown. Bug 1703572.
432 await lazy.ExtensionUtils.promiseTimeout(500);
434 // If we're in a staged background update, we need to restart Firefox to complete the update.
436 `${SLUG}: Checking if staged background update is ready for restart`
438 // If a restart loop is occurring then automaticRestartFound will be true.
440 lazy.NimbusFeatures.backgroundUpdateAutomaticRestart.getVariable(
443 updateStatus === lazy.AppUpdater.STATUS.READY_FOR_RESTART &&
444 !automaticRestartFound
447 `${SLUG}: Starting Firefox restart after staged background update`
450 // We need to restart Firefox with the same arguments to ensure
451 // the background update continues from where it was before the restart.
453 Cc["@mozilla.org/updates/update-processor;1"]
454 .createInstance(Ci.nsIUpdateProcessor)
455 .attemptAutomaticApplicationRestartWithLaunchArgs([
456 "-automatic-restart",
458 // Report an attempted automatic restart.
459 Glean.backgroundUpdate.automaticRestartAttempted.set(true);
460 lazy.log.debug(`${SLUG}: automatic application restart queued`);
463 `${SLUG}: caught exception; failed to queue automatic application restart`,