Bug 1768494 - Preserve devtools profile outside of ephemeral background profile. r=devtools-reviewers,nchevobbe
authorNick Alexander <nalexander@mozilla.com>
Wed, 22 Jun 2022 04:05:26 +0000
changeset 621660 b22029c96f670c2a187682a280191f399c5de4d3
parent 621659 eb70c66be1274d65972581b04deefe86a5c5d4d1
child 621661 ed6630bb5d5069c15ac0ea481afed9e39fd48fe5
push id164977
push usernalexander@mozilla.com
push dateWed, 22 Jun 2022 04:07:53 +0000
treeherderautoland@b22029c96f67 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdevtools-reviewers, nchevobbe
bugs1768494
milestone103.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1768494 - Preserve devtools profile outside of ephemeral background profile. r=devtools-reviewers,nchevobbe For `--backgroundtask ... --jsdebugger` invocations, the devtools profile is kept inside the ephemeral background profile. This means that breakpoints, etc are not preserved across repeated debugging invocations. This change eases the debugging process. Differential Revision: https://phabricator.services.mozilla.com/D145893
devtools/client/framework/browser-toolbox/Launcher.jsm
--- a/devtools/client/framework/browser-toolbox/Launcher.jsm
+++ b/devtools/client/framework/browser-toolbox/Launcher.jsm
@@ -17,28 +17,38 @@ const {
 } = ChromeUtils.import("resource://devtools/shared/loader/Loader.jsm");
 
 const { Subprocess } = ChromeUtils.import(
   "resource://gre/modules/Subprocess.jsm"
 );
 const { AppConstants } = ChromeUtils.import(
   "resource://gre/modules/AppConstants.jsm"
 );
+const { XPCOMUtils } = ChromeUtils.import(
+  "resource://gre/modules/XPCOMUtils.jsm"
+);
 const lazy = {};
 ChromeUtils.defineModuleGetter(
   lazy,
   "BackgroundTasksUtils",
   "resource://gre/modules/BackgroundTasksUtils.jsm"
 );
 ChromeUtils.defineModuleGetter(
   lazy,
   "FileUtils",
   "resource://gre/modules/FileUtils.jsm"
 );
 
+XPCOMUtils.defineLazyServiceGetters(lazy, {
+  XreDirProvider: [
+    "@mozilla.org/xre/directory-provider;1",
+    "nsIXREDirProvider",
+  ],
+});
+
 const Telemetry = require("devtools/client/shared/telemetry");
 const EventEmitter = require("devtools/shared/event-emitter");
 
 const Services = require("Services");
 const env = Cc["@mozilla.org/process/environment;1"].getService(
   Ci.nsIEnvironment
 );
 
@@ -187,18 +197,46 @@ BrowserToolboxLauncher.prototype = {
   },
 
   /**
    * Initializes a profile for the remote debugger process.
    */
   _initProfile(overwritePreferences) {
     dumpn("Initializing the chrome toolbox user profile.");
 
-    const debuggingProfileDir = Services.dirsvc.get("ProfD", Ci.nsIFile);
-    debuggingProfileDir.append(CHROME_DEBUGGER_PROFILE_NAME);
+    const bts = Cc["@mozilla.org/backgroundtasks;1"]?.getService(
+      Ci.nsIBackgroundTasks
+    );
+
+    let debuggingProfileDir = Services.dirsvc.get("ProfD", Ci.nsIFile);
+    if (bts?.isBackgroundTaskMode) {
+      // Background tasks run with a temporary ephemeral profile.  We move the
+      // browser toolbox profile out of that ephemeral profile so that it has
+      // alonger life then the background task profile.  This preserves
+      // breakpoints, etc, across repeated debugging invocations.  This
+      // directory is close to the background task temporary profile name(s),
+      // but doesn't match the prefix that will get purged by the stale
+      // ephemeral profile cleanup mechanism.
+      //
+      // For example, the invocation
+      // `firefox --backgroundtask success --jsdebugger --wait-for-jsdebugger`
+      // might run with ephemeral profile
+      // `/tmp/MozillaBackgroundTask-<HASH>-success`
+      // and sibling directory browser toolbox profile
+      // `/tmp/MozillaBackgroundTask-<HASH>-chrome_debugger_profile-success`
+      //
+      // See `BackgroundTasks::Shutdown` for ephemeral profile cleanup details.
+      debuggingProfileDir = debuggingProfileDir.parent;
+      debuggingProfileDir.append(
+        `${Services.appinfo.vendor}BackgroundTask-` +
+          `${lazy.XreDirProvider.getInstallHash()}-${CHROME_DEBUGGER_PROFILE_NAME}-${bts.backgroundTaskName()}`
+      );
+    } else {
+      debuggingProfileDir.append(CHROME_DEBUGGER_PROFILE_NAME);
+    }
     try {
       debuggingProfileDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o755);
     } catch (ex) {
       if (ex.result === Cr.NS_ERROR_FILE_ALREADY_EXISTS) {
         if (!overwritePreferences) {
           this._dbgProfilePath = debuggingProfileDir.path;
           return;
         }
@@ -211,19 +249,16 @@ BrowserToolboxLauncher.prototype = {
     }
 
     this._dbgProfilePath = debuggingProfileDir.path;
 
     // We would like to copy prefs into this new profile...
     const prefsFile = debuggingProfileDir.clone();
     prefsFile.append("prefs.js");
 
-    const bts = Cc["@mozilla.org/backgroundtasks;1"]?.getService(
-      Ci.nsIBackgroundTasks
-    );
     if (bts?.isBackgroundTaskMode) {
       // Background tasks run under a temporary profile.  In order to set
       // preferences for the launched browser toolbox, take the preferences from
       // the default profile.  This is the standard pattern for controlling
       // background task settings.  Without this, there'd be no way to increase
       // logging in the browser toolbox process, etc.
       const defaultProfile = lazy.BackgroundTasksUtils.getDefaultProfile();
       if (!defaultProfile) {