Bug 1398729 - Share devtools.inspectedWindow.eval helper methods in ext-devtools API module. draft
authorLuca Greco <lgreco@mozilla.com>
Sat, 09 Sep 2017 15:44:35 +0200
changeset 664878 dea04abd833221d63faa5e23a9b8a32d477f1daf
parent 664736 dd6b788f149763c4014c27f2fe1a1d13228bda82
child 664879 a8562fa11eeffdb3e12120581ceab1b92e3f0005
push id79837
push userluca.greco@alcacoop.it
push dateThu, 14 Sep 2017 14:30:47 +0000
bugs1398729
milestone57.0a1
Bug 1398729 - Share devtools.inspectedWindow.eval helper methods in ext-devtools API module. MozReview-Commit-ID: 9ezZNjEOdUY
browser/components/extensions/ext-devtools-inspectedWindow.js
browser/components/extensions/ext-devtools.js
--- a/browser/components/extensions/ext-devtools-inspectedWindow.js
+++ b/browser/components/extensions/ext-devtools-inspectedWindow.js
@@ -1,81 +1,58 @@
 /* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
 /* vim: set sts=2 sw=2 et tw=80: */
 "use strict";
 
-/* global getDevToolsTargetForContext */
+// The ext-* files are imported into the same scopes.
+/* import-globals-from ext-devtools.js */
+/* import-globals-from ext-browser.js */
 
 XPCOMUtils.defineLazyModuleGetter(this, "DevToolsShim",
                                   "chrome://devtools-shim/content/DevToolsShim.jsm");
 
 var {
   SpreadArgs,
 } = ExtensionCommon;
 
 this.devtools_inspectedWindow = class extends ExtensionAPI {
   getAPI(context) {
-    // Lazily retrieve and store an inspectedWindow actor front per child context.
+    // Lazily retrieved inspectedWindow actor front per child context.
     let waitForInspectedWindowFront;
-    async function getInspectedWindowFront() {
-      // If there is not yet a front instance, then a lazily cloned target for the context is
-      // retrieved using the DevtoolsParentContextsManager helper (which is an asynchronous operation,
-      // because the first time that the target has been cloned, it is not ready to be used to create
-      // the front instance until it is connected to the remote debugger successfully).
-      const clonedTarget = await getDevToolsTargetForContext(context);
-      return DevToolsShim.createWebExtensionInspectedWindowFront(clonedTarget);
-    }
-
-    function getToolboxOptions() {
-      const options = {};
-      const toolbox = context.devToolsToolbox;
-      const selectedNode = toolbox.selection;
-
-      if (selectedNode && selectedNode.nodeFront) {
-        // If there is a selected node in the inspector, we hand over
-        // its actor id to the eval request in order to provide the "$0" binding.
-        options.toolboxSelectedNodeActorID = selectedNode.nodeFront.actorID;
-      }
-
-      // Provide the console actor ID to implement the "inspect" binding.
-      options.toolboxConsoleActorID = toolbox.target.form.consoleActor;
-
-      return options;
-    }
 
     // TODO(rpl): retrive a more detailed callerInfo object, like the filename and
     // lineNumber of the actual extension called, in the child process.
     const callerInfo = {
       addonId: context.extension.id,
       url: context.extension.baseURI.spec,
     };
 
     return {
       devtools: {
         inspectedWindow: {
           async eval(expression, options) {
             if (!waitForInspectedWindowFront) {
-              waitForInspectedWindowFront = getInspectedWindowFront();
+              waitForInspectedWindowFront = getInspectedWindowFront(context);
             }
 
             const front = await waitForInspectedWindowFront;
 
-            const evalOptions = Object.assign({}, options, getToolboxOptions());
+            const evalOptions = Object.assign({}, options, getToolboxEvalOptions(context));
 
             const evalResult = await front.eval(callerInfo, expression, evalOptions);
 
             // TODO(rpl): check for additional undocumented behaviors on chrome
             // (e.g. if we should also print error to the console or set lastError?).
             return new SpreadArgs([evalResult.value, evalResult.exceptionInfo]);
           },
           async reload(options) {
             const {ignoreCache, userAgent, injectedScript} = options || {};
 
             if (!waitForInspectedWindowFront) {
-              waitForInspectedWindowFront = getInspectedWindowFront();
+              waitForInspectedWindowFront = getInspectedWindowFront(context);
             }
 
             const front = await waitForInspectedWindowFront;
             front.reload(callerInfo, {ignoreCache, userAgent, injectedScript});
           },
         },
       },
     };
--- a/browser/components/extensions/ext-devtools.js
+++ b/browser/components/extensions/ext-devtools.js
@@ -1,14 +1,14 @@
 /* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
 /* vim: set sts=2 sw=2 et tw=80: */
 "use strict";
 
-/* exported getDevToolsTargetForContext */
-/* global getTargetTabIdForToolbox, getDevToolsTargetForContext */
+/* exported getDevToolsTargetForContext, getInspectedWindowFront, getToolboxEvalOptions */
+/* global getTargetTabIdForToolbox, getDevToolsTargetForContext, getInspectedWindowFront, getToolboxEvalOptions */
 
 // The ext-* files are imported into the same scopes.
 /* import-globals-from ext-browser.js */
 
 /**
  * This module provides helpers used by the other specialized `ext-devtools-*.js` modules
  * and the implementation of the `devtools_page`.
  */
@@ -80,16 +80,46 @@ global.getTargetTabIdForToolbox = (toolb
   }
 
   let parentWindow = target.tab.linkedBrowser.ownerGlobal;
   let tab = parentWindow.gBrowser.getTabForBrowser(target.tab.linkedBrowser);
 
   return tabTracker.getId(tab);
 };
 
+// Create an InspectedWindowFront instance for a given context (used in devtoools.inspectedWindow.eval
+// and in sidebar.setExpression API methods).
+global.getInspectedWindowFront = async function(context) {
+  // If there is not yet a front instance, then a lazily cloned target for the context is
+  // retrieved using the DevtoolsParentContextsManager helper (which is an asynchronous operation,
+  // because the first time that the target has been cloned, it is not ready to be used to create
+  // the front instance until it is connected to the remote debugger successfully).
+  const clonedTarget = await getDevToolsTargetForContext(context);
+  return DevToolsShim.createWebExtensionInspectedWindowFront(clonedTarget);
+};
+
+// Get the WebExtensionInspectedWindowActor eval options (needed to provide the $0 and inspect
+// binding provided to the evaluated js code).
+global.getToolboxEvalOptions = function(context) {
+  const options = {};
+  const toolbox = context.devToolsToolbox;
+  const selectedNode = toolbox.selection;
+
+  if (selectedNode && selectedNode.nodeFront) {
+    // If there is a selected node in the inspector, we hand over
+    // its actor id to the eval request in order to provide the "$0" binding.
+    options.toolboxSelectedNodeActorID = selectedNode.nodeFront.actorID;
+  }
+
+  // Provide the console actor ID to implement the "inspect" binding.
+  options.toolboxConsoleActorID = toolbox.target.form.consoleActor;
+
+  return options;
+};
+
 /**
  * The DevToolsPage represents the "devtools_page" related to a particular
  * Toolbox and WebExtension.
  *
  * The devtools_page contexts are invisible WebExtensions contexts, similar to the
  * background page, associated to a single developer toolbox (e.g. If an add-on
  * registers a devtools_page and the user opens 3 developer toolbox in 3 webpages,
  * 3 devtools_page contexts will be created for that add-on).