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

--HG--
extra : rebase_source : e1fa7fb68c6f52b4e10c7f1f961400810b03ddc4
This commit is contained in:
Mike Conley 2017-07-06 15:15:44 -04:00
Родитель 5c3cefddb8
Коммит 3400474bb0
1 изменённых файлов: 27 добавлений и 0 удалений

Просмотреть файл

@ -7,6 +7,9 @@
* 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;
@ -51,6 +54,23 @@ var TalosContentProfiler;
* 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,
@ -261,4 +281,11 @@ var TalosContentProfiler;
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;
}
})();