Bug 1374333 - Allow TalosContentProfiler.js to be loaded as both a frame script and as a normal DOM script. r?rwood draft
authorMike Conley <mconley@mozilla.com>
Thu, 06 Jul 2017 15:15:44 -0400
changeset 610827 ce09d8d2f93aab89e82a09b28b1c3f93717b227a
parent 610826 8339b5047f39ae56982564edad9ddc2d2511721a
child 610828 4e24d4e5e3a142c4b48439b2b8e7b05d1d379fe0
push id69002
push usermconley@mozilla.com
push dateTue, 18 Jul 2017 19:11:35 +0000
reviewersrwood
bugs1374333
milestone56.0a1
Bug 1374333 - Allow TalosContentProfiler.js to be loaded as both a frame script and as a normal DOM script. r?rwood MozReview-Commit-ID: FMejiIsulkS
testing/talos/talos/talos-powers/content/TalosContentProfiler.js
--- a/testing/talos/talos/talos-powers/content/TalosContentProfiler.js
+++ b/testing/talos/talos/talos-powers/content/TalosContentProfiler.js
@@ -2,16 +2,19 @@
  * 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/. */
 
 /**
  * This utility script is for instrumenting your Talos test for
  * performance profiles while running within content. If your test
  * is running in the parent process, you should use
  * TalosParentProfiler.js instead to avoid the messaging overhead.
+ *
+ * This file can be loaded directly into a test page, or can be loaded
+ * as a frame script into a browser by the parent process.
  */
 
 var TalosContentProfiler;
 
 (function() {
 
   // Whether or not this TalosContentProfiler object has had initFromObject
   // or initFromURLQueryParams called on it. Any functions that will send
@@ -46,16 +49,33 @@ var TalosContentProfiler;
    *        eventually sent to the parent.
    * @param data (optional)
    *        The data that will be sent to the parent.
    * @returns Promise
    *        Resolves when a corresponding acknowledgement event is dispatched
    *        on this document.
    */
   function sendEventAndWait(name, data = {}) {
+    // If we're running as a frame script, we can send messages directly to
+    // the parent, rather than going through the talos-powers-content.js
+    // mediator, which ends up being more complicated.
+    if (typeof(sendAsyncMessage) !== "undefined") {
+      return new Promise(resolve => {
+        sendAsyncMessage("TalosContentProfiler:Command", { name, data });
+        addMessageListener("TalosContentProfiler:Response", function onMsg(msg) {
+          if (msg.data.name != name) {
+            return;
+          }
+
+          removeMessageListener("TalosContentProfiler:Response", onMsg);
+          resolve(msg.data);
+        });
+      });
+    }
+
     return new Promise((resolve) => {
       var event = new CustomEvent("TalosContentProfilerCommand", {
         bubbles: true,
         detail: {
           name,
           data,
         }
       });
@@ -256,9 +276,16 @@ var TalosContentProfiler;
     /**
      * Add a marker to the profile on the content process samples.
      * This occurs synchronously.
      */
     contentMarker(marker) {
       Services.profiler.AddMarker(marker);
     },
   };
+
+  // sendAsyncMessage is a hack-y mechanism to determine whether or not
+  // we're running as a frame script. If we are, jam TalosContentProfiler
+  // into the content scope.
+  if (typeof(sendAsyncMessage) !== "undefined") {
+    content.wrappedJSObject.TalosContentProfiler = TalosContentProfiler;
+  }
 })();