Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 34 additions & 12 deletions packages/react-native/ReactCommon/jsinspector-modern/HostAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,8 @@ namespace facebook::react::jsinspector_modern {

#define ANSI_WEIGHT_BOLD "\x1B[1m"
#define ANSI_WEIGHT_RESET "\x1B[22m"
#define ANSI_STYLE_ITALIC "\x1B[3m"
#define ANSI_STYLE_RESET "\x1B[23m"
#define ANSI_COLOR_BG_YELLOW "\x1B[48;2;253;247;231m"

static constexpr auto kModernCDPBackendNotice =
ANSI_COLOR_BG_YELLOW ANSI_WEIGHT_BOLD
"NOTE:" ANSI_WEIGHT_RESET " You are using the " ANSI_STYLE_ITALIC
"modern" ANSI_STYLE_RESET " CDP backend for React Native (HostTarget)."sv;
#define CSS_STYLE_PLACEHOLDER "%c"

HostAgent::HostAgent(
FrontendChannel frontendChannel,
Expand All @@ -51,12 +45,15 @@ void HostAgent::handleRequest(const cdp::PreparsedRequest& req) {
if (req.method == "Log.enable") {
sessionState_.isLogDomainEnabled = true;

// Send a log entry identifying the modern CDP backend.
sendInfoLogEntry(kModernCDPBackendNotice);
if (sessionState_.isFuseboxClientDetected) {
sendFuseboxNotice();
}

// Send a log entry with the integration name.
if (sessionMetadata_.integrationName) {
sendInfoLogEntry("Integration: " + *sessionMetadata_.integrationName);
sendInfoLogEntry(
ANSI_COLOR_BG_YELLOW "Debugger integration: " +
*sessionMetadata_.integrationName);
}

shouldSendOKResponse = true;
Expand Down Expand Up @@ -100,6 +97,15 @@ void HostAgent::handleRequest(const cdp::PreparsedRequest& req) {
: std::nullopt,
});

shouldSendOKResponse = true;
isFinishedHandlingRequest = true;
} else if (req.method == "FuseboxClient.setClientMetadata") {
sessionState_.isFuseboxClientDetected = true;

if (sessionState_.isLogDomainEnabled) {
sendFuseboxNotice();
}

shouldSendOKResponse = true;
isFinishedHandlingRequest = true;
}
Expand All @@ -120,7 +126,23 @@ void HostAgent::handleRequest(const cdp::PreparsedRequest& req) {
req.method + " not implemented yet"));
}

void HostAgent::sendInfoLogEntry(std::string_view text) {
void HostAgent::sendFuseboxNotice() {
static constexpr auto kFuseboxNotice = ANSI_COLOR_BG_YELLOW
"Welcome to the new React Native debugger (codename " ANSI_WEIGHT_BOLD
"React Fusebox " CSS_STYLE_PLACEHOLDER
"⚡️" CSS_STYLE_PLACEHOLDER ANSI_WEIGHT_RESET ")."sv;

sendInfoLogEntry(
kFuseboxNotice, {"font-family: sans-serif;", "font-family: monospace;"});
}

void HostAgent::sendInfoLogEntry(
std::string_view text,
std::initializer_list<std::string_view> args) {
folly::dynamic argsArray = folly::dynamic::array();
for (auto arg : args) {
argsArray.push_back(arg);
}
frontendChannel_(cdp::jsonNotification(
"Log.entryAdded",
folly::dynamic::object(
Expand All @@ -130,7 +152,7 @@ void HostAgent::sendInfoLogEntry(std::string_view text) {
duration_cast<milliseconds>(
system_clock::now().time_since_epoch())
.count())("source", "other")(
"level", "info")("text", text))));
"level", "info")("text", text)("args", std::move(argsArray)))));
}

void HostAgent::setCurrentInstanceAgent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ class HostAgent final {
* Runtime.consoleAPICalled is that the latter requires an execution context
* ID, which does not exist at the Host level.
*/
void sendInfoLogEntry(std::string_view text);
void sendInfoLogEntry(
std::string_view text,
std::initializer_list<std::string_view> args = {});

void sendFuseboxNotice();

FrontendChannel frontendChannel_;
HostTargetController& targetController_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ struct SessionState {
std::unordered_map<std::string, ExecutionContextSelectorSet>
subscribedBindings;

bool isFuseboxClientDetected{false};

/**
* Stores the state object exported from the last main RuntimeAgent, if any,
* before it was destroyed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ TEST_F(HostTargetProtocolTest, InjectLogsToIdentifyBackend) {
onMessage(JsonParsed(AllOf(
AtJsonPtr("/method", "Log.entryAdded"),
AtJsonPtr("/params/entry", Not(IsEmpty()))))))
.Times(2)
.RetiresOnSaturation();
.Times(AtLeast(1));
EXPECT_CALL(fromPage(), onMessage(JsonEq(R"({
"id": 1,
"result": {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,21 @@ TYPED_TEST(JsiIntegrationPortableTest, ExceptionDuringAddBindingIsIgnored) {
EXPECT_TRUE(this->eval("globalThis.foo === 42").getBool());
}

TYPED_TEST(JsiIntegrationPortableTest, FuseboxSetClientMetadata) {
this->connect();

this->expectMessageFromPage(JsonEq(R"({
"id": 1,
"result": {}
})"));

this->toPage_->sendMessage(R"({
"id": 1,
"method": "FuseboxClient.setClientMetadata",
"params": {}
})");
}

#pragma endregion // AllEngines
#pragma region AllHermesVariants

Expand Down