Skip to content

Commit eb94727

Browse files
motiz88facebook-github-bot
authored andcommitted
Inject log with CDP integration name if provided (#42384)
Summary: Pull Request resolved: #42384 Changelog: [Internal] Similar to D52894171, adds a console log message identifying the specific CDP backend integration, based on an optional `SessionMetadata` object passed to `PageTarget::connect()`. This is helpful during development+rollout as we will have 4+ such call sites (iOS/Android, Bridge/Bridgeless). Reviewed By: huntie Differential Revision: D52905488 fbshipit-source-id: d26aae1d07c2c42965498a81f03d826de98fa222
1 parent 2e47770 commit eb94727

File tree

5 files changed

+66
-22
lines changed

5 files changed

+66
-22
lines changed

packages/react-native/ReactCommon/jsinspector-modern/PageAgent.cpp

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ static constexpr auto kModernCDPBackendNotice =
2727
"NOTE:" ANSI_WEIGHT_RESET " You are using the " ANSI_STYLE_ITALIC
2828
"modern" ANSI_STYLE_RESET " CDP backend for React Native (PageTarget)."sv;
2929

30-
PageAgent::PageAgent(FrontendChannel frontendChannel)
31-
: frontendChannel_(frontendChannel) {}
30+
PageAgent::PageAgent(
31+
FrontendChannel frontendChannel,
32+
PageTarget::SessionMetadata sessionMetadata)
33+
: frontendChannel_(frontendChannel),
34+
sessionMetadata_(std::move(sessionMetadata)) {}
3235

3336
void PageAgent::handleRequest(const cdp::PreparsedRequest& req) {
3437
if (req.method == "Log.enable") {
@@ -37,17 +40,12 @@ void PageAgent::handleRequest(const cdp::PreparsedRequest& req) {
3740
folly::toJson(folly::dynamic::object("id", req.id)("result", nullptr)));
3841

3942
// Send a log entry identifying the modern CDP backend.
40-
frontendChannel_(
41-
folly::toJson(folly::dynamic::object("method", "Log.entryAdded")(
42-
"params",
43-
folly::dynamic::object(
44-
"entry",
45-
folly::dynamic::object(
46-
"timestamp",
47-
duration_cast<milliseconds>(
48-
system_clock::now().time_since_epoch())
49-
.count())("source", "other")(
50-
"level", "info")("text", kModernCDPBackendNotice)))));
43+
sendInfoLogEntry(kModernCDPBackendNotice);
44+
45+
// Send a log entry with the integration name.
46+
if (sessionMetadata_.integrationName) {
47+
sendInfoLogEntry("Integration: " + *sessionMetadata_.integrationName);
48+
}
5149

5250
return;
5351
}
@@ -59,4 +57,18 @@ void PageAgent::handleRequest(const cdp::PreparsedRequest& req) {
5957
frontendChannel_(json);
6058
}
6159

60+
void PageAgent::sendInfoLogEntry(std::string_view text) {
61+
frontendChannel_(
62+
folly::toJson(folly::dynamic::object("method", "Log.entryAdded")(
63+
"params",
64+
folly::dynamic::object(
65+
"entry",
66+
folly::dynamic::object(
67+
"timestamp",
68+
duration_cast<milliseconds>(
69+
system_clock::now().time_since_epoch())
70+
.count())("source", "other")(
71+
"level", "info")("text", text)))));
72+
}
73+
6274
} // namespace facebook::react::jsinspector_modern

packages/react-native/ReactCommon/jsinspector-modern/PageAgent.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77

88
#pragma once
99

10+
#include "PageTarget.h"
11+
1012
#include <jsinspector-modern/InspectorInterfaces.h>
1113
#include <jsinspector-modern/Parsing.h>
14+
1215
#include <functional>
16+
#include <string_view>
1317

1418
namespace facebook::react::jsinspector_modern {
1519

@@ -25,7 +29,9 @@ class PageAgent {
2529
* \param frontendChannel A channel used to send responses and events to the
2630
* frontend.
2731
*/
28-
explicit PageAgent(FrontendChannel frontendChannel);
32+
PageAgent(
33+
FrontendChannel frontendChannel,
34+
PageTarget::SessionMetadata sessionMetadata);
2935

3036
/**
3137
* Handle a CDP request. The response will be sent over the provided
@@ -35,7 +41,19 @@ class PageAgent {
3541
void handleRequest(const cdp::PreparsedRequest& req);
3642

3743
private:
44+
/**
45+
* Send a simple Log.entryAdded notification with the given
46+
* \param text. You must ensure that the frontend has enabled Log
47+
* notifications (using Log.enable) prior to calling this function. In Chrome
48+
* DevTools, the message will appear in the Console tab along with regular
49+
* console messages. The difference between Log.entryAdded and
50+
* Runtime.consoleAPICalled is that the latter requires an execution context
51+
* ID, which does not exist at the Page level.
52+
*/
53+
void sendInfoLogEntry(std::string_view text);
54+
3855
FrontendChannel frontendChannel_;
56+
const PageTarget::SessionMetadata sessionMetadata_;
3957
};
4058

4159
} // namespace facebook::react::jsinspector_modern

packages/react-native/ReactCommon/jsinspector-modern/PageTarget.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,17 @@ namespace {
2525
*/
2626
class PageTargetSession {
2727
public:
28-
explicit PageTargetSession(std::unique_ptr<IRemoteConnection> remote)
28+
explicit PageTargetSession(
29+
std::unique_ptr<IRemoteConnection> remote,
30+
PageTarget::SessionMetadata sessionMetadata)
2931
: remote_(std::make_shared<RAIIRemoteConnection>(std::move(remote))),
3032
frontendChannel_(
3133
[remoteWeak = std::weak_ptr(remote_)](std::string_view message) {
3234
if (auto remote = remoteWeak.lock()) {
3335
remote->onMessage(std::string(message));
3436
}
3537
}),
36-
pageAgent_(frontendChannel_) {}
38+
pageAgent_(frontendChannel_, std::move(sessionMetadata)) {}
3739
/**
3840
* Called by CallbackLocalConnection to send a message to this Session's
3941
* Agent.
@@ -77,9 +79,10 @@ class PageTargetSession {
7779
} // namespace
7880

7981
std::unique_ptr<ILocalConnection> PageTarget::connect(
80-
std::unique_ptr<IRemoteConnection> connectionToFrontend) {
81-
return std::make_unique<CallbackLocalConnection>(
82-
PageTargetSession(std::move(connectionToFrontend)));
82+
std::unique_ptr<IRemoteConnection> connectionToFrontend,
83+
SessionMetadata sessionMetadata) {
84+
return std::make_unique<CallbackLocalConnection>(PageTargetSession(
85+
std::move(connectionToFrontend), std::move(sessionMetadata)));
8386
}
8487

8588
} // namespace facebook::react::jsinspector_modern

packages/react-native/ReactCommon/jsinspector-modern/PageTarget.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
#include <jsinspector-modern/InspectorInterfaces.h>
1111

12+
#include <optional>
13+
#include <string>
14+
1215
namespace facebook::react::jsinspector_modern {
1316

1417
/**
@@ -18,6 +21,10 @@ namespace facebook::react::jsinspector_modern {
1821
*/
1922
class PageTarget {
2023
public:
24+
struct SessionMetadata {
25+
std::optional<std::string> integrationName;
26+
};
27+
2128
/**
2229
* Creates a new Session connected to this PageTarget, wrapped in an
2330
* interface which is compatible with \c IInspector::addPage.
@@ -26,7 +33,8 @@ class PageTarget {
2633
* destructor execute.
2734
*/
2835
std::unique_ptr<ILocalConnection> connect(
29-
std::unique_ptr<IRemoteConnection> connectionToFrontend);
36+
std::unique_ptr<IRemoteConnection> connectionToFrontend,
37+
SessionMetadata sessionMetadata = {});
3038
};
3139

3240
} // namespace facebook::react::jsinspector_modern

packages/react-native/ReactCommon/jsinspector-modern/tests/PageTargetTest.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ namespace {
3131
class PageTargetProtocolTest : public Test {
3232
public:
3333
PageTargetProtocolTest() {
34-
toPage_ = page_.connect(remoteConnections_.make_unique());
34+
toPage_ = page_.connect(
35+
remoteConnections_.make_unique(),
36+
{.integrationName = "PageTargetProtocolTest"});
3537

3638
// In protocol tests, we'll always get an onDisconnect call when we tear
3739
// down the test. Expect it in order to satisfy the strict mock.
@@ -102,7 +104,7 @@ TEST_F(PageTargetProtocolTest, MalformedJson) {
102104
toPage_->sendMessage("{");
103105
}
104106

105-
TEST_F(PageTargetProtocolTest, InjectLogToIdentifyBackend) {
107+
TEST_F(PageTargetProtocolTest, InjectLogsToIdentifyBackend) {
106108
InSequence s;
107109

108110
EXPECT_CALL(fromPage(), onMessage(JsonEq(R"({
@@ -116,6 +118,7 @@ TEST_F(PageTargetProtocolTest, InjectLogToIdentifyBackend) {
116118
onMessage(JsonParsed(AllOf(
117119
AtJsonPtr("/method", "Log.entryAdded"),
118120
AtJsonPtr("/params/entry", Not(IsEmpty()))))))
121+
.Times(2)
119122
.RetiresOnSaturation();
120123
toPage_->sendMessage(R"({
121124
"id": 1,

0 commit comments

Comments
 (0)