Bug 1602800 - Move the function that gets BrowsingContext ID to a new file inside shared folder r=gregtatum a=jcristau
authorNazım Can Altınova <canaltinova@gmail.com>
Thu, 12 Dec 2019 13:53:31 +0000
changeset 566868 e8d6256cf1f7dbaeb6d7c11e43ff1e8fadc822f6
parent 566867 e4dc052eb3f3e7a3ed9667d91ec32ebabd0e7869
child 566869 63f6ab33a4e9f7bff3d89031370263636a59601e
push id12439
push useraiakab@mozilla.com
push dateWed, 18 Dec 2019 02:57:46 +0000
treeherdermozilla-beta@efdba899f9cf [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgregtatum, jcristau
bugs1602800
milestone72.0
Bug 1602800 - Move the function that gets BrowsingContext ID to a new file inside shared folder r=gregtatum a=jcristau Differential Revision: https://phabricator.services.mozilla.com/D56720
devtools/client/performance-new/browser.js
devtools/client/performance-new/popup/background.jsm.js
devtools/shared/performance-new/gecko-profiler-interface.js
devtools/shared/performance-new/moz.build
devtools/shared/performance-new/recording-utils.js
--- a/devtools/client/performance-new/browser.js
+++ b/devtools/client/performance-new/browser.js
@@ -484,45 +484,17 @@ function openFilePickerForObjdir(window,
       if (path && !objdirs.includes(path)) {
         const newObjdirs = [...objdirs, path];
         changeObjdirs(newObjdirs);
       }
     }
   });
 }
 
-/**
- * Gets the ID of active BrowsingContext from the browser.
- *
- * @type {GetActiveBrowsingContextID}
- */
-function getActiveBrowsingContextID() {
-  const { Services } = lazyServices();
-  const win = Services.wm.getMostRecentWindow("navigator:browser");
-
-  if (
-    win &&
-    win.gBrowser &&
-    win.gBrowser.selectedBrowser &&
-    win.gBrowser.selectedBrowser.browsingContext &&
-    win.gBrowser.selectedBrowser.browsingContext.id
-  ) {
-    return win.gBrowser.selectedBrowser.browsingContext.id;
-  }
-
-  console.error(
-    "Failed to get the active BrowsingContext ID while starting the profiler."
-  );
-  // `0` mean that we failed to ge the active BrowsingContext ID, and it's
-  // treated as null value in the platform.
-  return 0;
-}
-
 module.exports = {
   receiveProfile,
   getRecordingPreferencesFromDebuggee,
   setRecordingPreferencesOnDebuggee,
   createMultiModalGetSymbolTableFn,
   restartBrowserWithEnvironmentVariable,
   getEnvironmentVariable,
   openFilePickerForObjdir,
-  getActiveBrowsingContextID,
 };
--- a/devtools/client/performance-new/popup/background.jsm.js
+++ b/devtools/client/performance-new/popup/background.jsm.js
@@ -88,16 +88,26 @@ const lazyPreferenceManagement = require
     "resource://devtools/shared/Loader.jsm"
   );
 
   /** @type {import("devtools/client/performance-new/preference-management")} */
   const preferenceManagementModule = require("devtools/client/performance-new/preference-management");
   return preferenceManagementModule;
 });
 
+const lazyRecordingUtils = requireLazy(() => {
+  const { require } = ChromeUtils.import(
+    "resource://devtools/shared/Loader.jsm"
+  );
+
+  /** @type {import("devtools/shared/performance-new/recording-utils")} */
+  const recordingUtils = require("devtools/shared/performance-new/recording-utils");
+  return recordingUtils;
+});
+
 /**
  * This Map caches the symbols from the shared libraries.
  * @type {Map<string, { path: string, debugPath: string }>}
  */
 const symbolCache = new Map();
 
 /**
  * @param {string} debugName
@@ -180,18 +190,17 @@ function startProfiler() {
     entries,
     interval,
     features,
     threads,
     duration,
   } = translatePreferencesToState(getRecordingPreferencesFromBrowser());
 
   // Get the active BrowsingContext ID from browser.
-  const getActiveBrowsingContextID = lazyBrowserModule()
-    .getActiveBrowsingContextID;
+  const { getActiveBrowsingContextID } = lazyRecordingUtils();
   const activeBrowsingContextID = getActiveBrowsingContextID();
 
   Services.profiler.StartProfiler(
     entries,
     interval,
     features,
     threads,
     activeBrowsingContextID,
--- a/devtools/shared/performance-new/gecko-profiler-interface.js
+++ b/devtools/shared/performance-new/gecko-profiler-interface.js
@@ -15,18 +15,18 @@ loader.lazyImporter(
   this,
   "PrivateBrowsingUtils",
   "resource://gre/modules/PrivateBrowsingUtils.jsm"
 );
 
 loader.lazyRequireGetter(this, "EventEmitter", "devtools/shared/event-emitter");
 loader.lazyRequireGetter(
   this,
-  "BrowserModule",
-  "devtools/client/performance-new/browser"
+  "RecordingUtils",
+  "devtools/shared/performance-new/recording-utils"
 );
 
 // Some platforms are built without the Gecko Profiler.
 const IS_SUPPORTED_PLATFORM = "nsIProfiler" in Ci;
 
 /**
  * The GeckoProfiler already has an interface to control it through the
  * nsIProfiler component. However, this class implements an interface that can
@@ -96,17 +96,17 @@ class ActorReadyGeckoProfilerInterface {
       features: options.features || [
         "js",
         "stackwalk",
         "responsiveness",
         "threads",
         "leaf",
       ],
       threads: options.threads || ["GeckoMain", "Compositor"],
-      activeBrowsingContextID: BrowserModule.getActiveBrowsingContextID(),
+      activeBrowsingContextID: RecordingUtils.getActiveBrowsingContextID(),
     };
 
     try {
       // This can throw an error if the profiler is in the wrong state.
       Services.profiler.StartProfiler(
         settings.entries,
         settings.interval,
         settings.features,
--- a/devtools/shared/performance-new/moz.build
+++ b/devtools/shared/performance-new/moz.build
@@ -1,12 +1,13 @@
 # -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
 # vim: set filetype=python:
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 DevToolsModules(
     'gecko-profiler-interface.js',
+    'recording-utils.js',
 )
 
 with Files('**'):
     BUG_COMPONENT = ('DevTools', 'Performance Tools (Profiler/Timeline)')
new file mode 100644
--- /dev/null
+++ b/devtools/shared/performance-new/recording-utils.js
@@ -0,0 +1,69 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+// @ts-check
+"use strict";
+
+/**
+ * This file is for the new performance panel that targets profiler.firefox.com,
+ * not the default-enabled DevTools performance panel.
+ */
+
+/**
+ * @typedef {import("../../client/performance-new/@types/perf").GetActiveBrowsingContextID} GetActiveBrowsingContextID
+ */
+
+/**
+ * TS-TODO
+ *
+ * This function replaces lazyRequireGetter, and TypeScript can understand it. It's
+ * currently duplicated until we have consensus that TypeScript is a good idea.
+ *
+ * @template T
+ * @type {(callback: () => T) => () => T}
+ */
+function requireLazy(callback) {
+  /** @type {T | undefined} */
+  let cache;
+  return () => {
+    if (cache === undefined) {
+      cache = callback();
+    }
+    return cache;
+  };
+}
+
+const lazyServices = requireLazy(() =>
+  require("resource://gre/modules/Services.jsm")
+);
+
+/**
+ * Gets the ID of active BrowsingContext from the browser.
+ *
+ * @type {GetActiveBrowsingContextID}
+ */
+function getActiveBrowsingContextID() {
+  const { Services } = lazyServices();
+  const win = Services.wm.getMostRecentWindow("navigator:browser");
+
+  if (
+    win &&
+    win.gBrowser &&
+    win.gBrowser.selectedBrowser &&
+    win.gBrowser.selectedBrowser.browsingContext &&
+    win.gBrowser.selectedBrowser.browsingContext.id
+  ) {
+    return win.gBrowser.selectedBrowser.browsingContext.id;
+  }
+
+  console.error(
+    "Failed to get the active BrowsingContext ID while starting the profiler."
+  );
+  // `0` mean that we failed to ge the active BrowsingContext ID, and it's
+  // treated as null value in the platform.
+  return 0;
+}
+
+module.exports = {
+  getActiveBrowsingContextID,
+};