Encapsulate TracingAgent (#46986)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/46986

# Changelog: [Internal]

Encapsulate integration with `FuseboxTracer.cpp` into `TracingAgent.cpp`, which will be responsible for handling `Tracing` domain CDP events.

Closer to the top of the stack, more changes will be added to it.

Reviewed By: huntie

Differential Revision: D64249695

fbshipit-source-id: 4adf5973299275e210e7cdf9c816efab881c9daf
This commit is contained in:
Ruslan Lesiutin authored and Facebook GitHub Bot committed 2024-10-16 10:14:52 -07:00
1 parent 4e12b86bfc
commit f53cd7238c
4 files changed
+109 -40

No files matched your search

@@ -15,8 +15,6 @@
#include <chrono>
#include <reactperflogger/fusebox/FuseboxTracer.h>
using namespace std::chrono;
using namespace std::literals::string_view_literals;
@@ -36,7 +34,8 @@ HostAgent::HostAgent(
targetController_(targetController),
hostMetadata_(std::move(hostMetadata)),
sessionState_(sessionState),
networkIOAgent_(NetworkIOAgent(frontendChannel, executor)) {}
networkIOAgent_(NetworkIOAgent(frontendChannel, std::move(executor))),
tracingAgent_(TracingAgent(frontendChannel)) {}
void HostAgent::handleRequest(const cdp::PreparsedRequest& req) {
bool shouldSendOKResponse = false;
@@ -149,43 +148,6 @@ void HostAgent::handleRequest(const cdp::PreparsedRequest& req) {
shouldSendOKResponse = true;
isFinishedHandlingRequest = true;
} else if (req.method == "Tracing.start") {
// @cdp Tracing.start support is experimental.
if (FuseboxTracer::getFuseboxTracer().startTracing()) {
shouldSendOKResponse = true;
} else {
frontendChannel_(cdp::jsonError(
req.id,
cdp::ErrorCode::InternalError,
"Tracing session already started"));
return;
}
isFinishedHandlingRequest = true;
} else if (req.method == "Tracing.end") {
// @cdp Tracing.end support is experimental.
bool firstChunk = true;
auto id = req.id;
bool wasStopped = FuseboxTracer::getFuseboxTracer().stopTracing(
[this, firstChunk, id](const folly::dynamic& eventsChunk) {
if (firstChunk) {
frontendChannel_(cdp::jsonResult(id));
}
frontendChannel_(cdp::jsonNotification(
"Tracing.dataCollected",
folly::dynamic::object("value", eventsChunk)));
});
if (!wasStopped) {
frontendChannel_(cdp::jsonError(
req.id,
cdp::ErrorCode::InternalError,
"Tracing session not started"));
return;
}
frontendChannel_(cdp::jsonNotification(
"Tracing.tracingComplete",
folly::dynamic::object("dataLossOccurred", false)));
shouldSendOKResponse = true;
isFinishedHandlingRequest = true;
}
if (!isFinishedHandlingRequest &&
@@ -193,6 +155,10 @@ void HostAgent::handleRequest(const cdp::PreparsedRequest& req) {
return;
}
if (!isFinishedHandlingRequest && tracingAgent_.handleRequest(req)) {
return;
}
if (!isFinishedHandlingRequest && instanceAgent_ &&
instanceAgent_->handleRequest(req)) {
return;
@@ -11,6 +11,7 @@
#include "HostTarget.h"
#include "NetworkIOAgent.h"
#include "SessionState.h"
#include "TracingAgent.h"
#include <jsinspector-modern/InspectorInterfaces.h>
#include <jsinspector-modern/InstanceAgent.h>
@@ -110,6 +111,8 @@ class HostAgent final {
SessionState& sessionState_;
NetworkIOAgent networkIOAgent_;
TracingAgent tracingAgent_;
};
} // namespace facebook::react::jsinspector_modern
@@ -0,0 +1,59 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "TracingAgent.h"
#include <reactperflogger/fusebox/FuseboxTracer.h>
namespace facebook::react::jsinspector_modern {
bool TracingAgent::handleRequest(const cdp::PreparsedRequest& req) {
if (req.method == "Tracing.start") {
// @cdp Tracing.start support is experimental.
if (FuseboxTracer::getFuseboxTracer().startTracing()) {
frontendChannel_(cdp::jsonResult(req.id));
} else {
frontendChannel_(cdp::jsonError(
req.id,
cdp::ErrorCode::InternalError,
"Tracing session already started"));
}
return true;
} else if (req.method == "Tracing.end") {
// @cdp Tracing.end support is experimental.
bool firstChunk = true;
auto id = req.id;
bool wasStopped = FuseboxTracer::getFuseboxTracer().stopTracing(
[this, firstChunk, id](const folly::dynamic& eventsChunk) {
if (firstChunk) {
frontendChannel_(cdp::jsonResult(id));
}
frontendChannel_(cdp::jsonNotification(
"Tracing.dataCollected",
folly::dynamic::object("value", eventsChunk)));
});
if (!wasStopped) {
frontendChannel_(cdp::jsonError(
req.id,
cdp::ErrorCode::InternalError,
"Tracing session not started"));
} else {
frontendChannel_(cdp::jsonNotification(
"Tracing.tracingComplete",
folly::dynamic::object("dataLossOccurred", false)));
}
frontendChannel_(cdp::jsonResult(req.id));
return true;
}
return false;
}
} // namespace facebook::react::jsinspector_modern
@@ -0,0 +1,41 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include "CdpJson.h"
#include "InspectorInterfaces.h"
namespace facebook::react::jsinspector_modern {
/**
* Provides an agent for handling CDP's Tracing.start, Tracing.stop.
*/
class TracingAgent {
public:
/**
* \param frontendChannel A channel used to send responses to the
* frontend.
*/
explicit TracingAgent(FrontendChannel frontendChannel)
: frontendChannel_(std::move(frontendChannel)) {}
/**
* Handle a CDP request. The response will be sent over the provided
* \c FrontendChannel synchronously or asynchronously.
* \param req The parsed request.
*/
bool handleRequest(const cdp::PreparsedRequest& req);
private:
/**
* A channel used to send responses and events to the frontend.
*/
FrontendChannel frontendChannel_;
};
} // namespace facebook::react::jsinspector_modern