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
--- 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;
+ }
})();