Skip to content

Commit 04eadf6

Browse files
motiz88facebook-github-bot
authored andcommitted
RELAND [RN][CDP] [3/n] RuntimeTarget refactor - RuntimeAgent --> RuntimeAgentDelegate
Summary: This is a resubmission of D53266707 with a fix in the OSS version of `HermesExecutorFactory` (it was incorrectly referencing `HermesRuntimeAgent.h` which doesn't exist anymore). The original diff summary follows. --- Changelog: [Internal] I'm refactoring the way the Runtime concept works in the modern CDP backend to bring it in line with the Page/Instance concepts. Overall, this will let us: * Integrate with engines that require us to instantiate a shared Target-like object (e.g. Hermes AsyncDebuggingAPI) in addition to an per-session Agent-like object. * Access JSI in a CDP context (both at target setup/teardown time and during a CDP session) to implement our own engine-agnostic functionality (`console` interception, `Runtime.addBinding`, etc). * Manage CDP execution contexts natively in RN, and (down the line) enable first-class debugging support for multiple Runtimes in an Instance. The core diffs in this stack will: * ~~Introduce a `RuntimeTarget` class similar to `{Page,Instance}Target`.~~ (D53233914) * ~~Make runtime registration explicit (`InstanceTarget::registerRuntime` similar to `PageTarget::registerInstance`).~~ (D53233914) * Rename the existing `RuntimeAgent` interface to `RuntimeAgentDelegate`. *← This diff* * Create a new concrete `RuntimeAgent` class similar to `{Page,Instance}Agent`. *← Also in this diff* * Provide `RuntimeTarget` and `RuntimeAgent` with primitives for safe JSI access, namely a `RuntimeExecutor` for scheduling work on the JS thread. * We'll likely develop a similar mechanism for scheduling work on the "main" thread from the JS thread, for when we need to do more than just send a CDP message (which we can already do with the thread-safe `FrontendChannel`) in response to a JS event. ## Architecture diagrams Before this stack: https://pxl.cl/4h7m0 After this stack: https://pxl.cl/4h7m7 Reviewed By: EdmondChuiHW Differential Revision: D53748590 fbshipit-source-id: bd0cf9f74b95abc52b4903f8a7afddcefa303d8a
1 parent b97f3e7 commit 04eadf6

24 files changed

+207
-105
lines changed

packages/react-native/ReactCommon/cxxreact/JSExecutor.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ double JSExecutor::performanceNow() {
3535
return duration / NANOSECONDS_IN_MILLISECOND;
3636
}
3737

38-
std::unique_ptr<jsinspector_modern::RuntimeAgent> JSExecutor::createAgent(
38+
std::unique_ptr<jsinspector_modern::RuntimeAgentDelegate>
39+
JSExecutor::createAgentDelegate(
3940
jsinspector_modern::FrontendChannel frontendChannel,
4041
jsinspector_modern::SessionState& sessionState) {
41-
return std::make_unique<jsinspector_modern::FallbackRuntimeAgent>(
42+
return std::make_unique<jsinspector_modern::FallbackRuntimeAgentDelegate>(
4243
std::move(frontendChannel), sessionState, getDescription());
4344
}
4445

packages/react-native/ReactCommon/cxxreact/JSExecutor.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class RN_EXPORT JSExecutor : public jsinspector_modern::RuntimeTargetDelegate {
114114
/**
115115
* Returns whether or not the underlying executor supports debugging via the
116116
* Chrome remote debugging protocol. If true, the executor should also
117-
* override the \c createAgent method.
117+
* override the \c createAgentDelegate method.
118118
*/
119119
virtual bool isInspectable() {
120120
return false;
@@ -141,9 +141,10 @@ class RN_EXPORT JSExecutor : public jsinspector_modern::RuntimeTargetDelegate {
141141
static double performanceNow();
142142

143143
/**
144-
* Create a RuntimeAgent that can be used to debug the JS VM instance.
144+
* Create a RuntimeAgentDelegate that can be used to debug the JS VM instance.
145145
*/
146-
virtual std::unique_ptr<jsinspector_modern::RuntimeAgent> createAgent(
146+
virtual std::unique_ptr<jsinspector_modern::RuntimeAgentDelegate>
147+
createAgentDelegate(
147148
jsinspector_modern::FrontendChannel frontendChannel,
148149
jsinspector_modern::SessionState& sessionState) override;
149150
};

packages/react-native/ReactCommon/hermes/executor/HermesExecutorFactory.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include <jsi/decorator.h>
1616
#include <jsinspector-modern/InspectorFlags.h>
1717

18-
#include <hermes/inspector-modern/chrome/HermesRuntimeAgent.h>
18+
#include <hermes/inspector-modern/chrome/HermesRuntimeAgentDelegate.h>
1919
#include <hermes/inspector-modern/chrome/Registration.h>
2020
#include <hermes/inspector/RuntimeAdapter.h>
2121

@@ -257,12 +257,13 @@ HermesExecutor::HermesExecutor(
257257
runtime_(runtime),
258258
hermesRuntime_(hermesRuntime) {}
259259

260-
std::unique_ptr<jsinspector_modern::RuntimeAgent> HermesExecutor::createAgent(
260+
std::unique_ptr<jsinspector_modern::RuntimeAgentDelegate>
261+
HermesExecutor::createAgentDelegate(
261262
jsinspector_modern::FrontendChannel frontendChannel,
262263
jsinspector_modern::SessionState& sessionState) {
263264
std::shared_ptr<HermesRuntime> hermesRuntimeShared(runtime_, &hermesRuntime_);
264-
return std::unique_ptr<jsinspector_modern::RuntimeAgent>(
265-
new jsinspector_modern::HermesRuntimeAgent(
265+
return std::unique_ptr<jsinspector_modern::RuntimeAgentDelegate>(
266+
new jsinspector_modern::HermesRuntimeAgentDelegate(
266267
frontendChannel,
267268
sessionState,
268269
hermesRuntimeShared,

packages/react-native/ReactCommon/hermes/executor/HermesExecutorFactory.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ class HermesExecutor : public JSIExecutor {
5454
RuntimeInstaller runtimeInstaller,
5555
hermes::HermesRuntime& hermesRuntime);
5656

57-
virtual std::unique_ptr<jsinspector_modern::RuntimeAgent> createAgent(
57+
virtual std::unique_ptr<jsinspector_modern::RuntimeAgentDelegate>
58+
createAgentDelegate(
5859
jsinspector_modern::FrontendChannel frontendChannel,
5960
jsinspector_modern::SessionState& sessionState) override;
6061

packages/react-native/ReactCommon/hermes/inspector-modern/chrome/HermesRuntimeAgent.cpp renamed to packages/react-native/ReactCommon/hermes/inspector-modern/chrome/HermesRuntimeAgentDelegate.cpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
#include "HermesRuntimeAgent.h"
8+
#include "HermesRuntimeAgentDelegate.h"
99

1010
// If HERMES_ENABLE_DEBUGGER isn't defined, we can't access any Hermes
1111
// CDPHandler headers or types.
@@ -14,7 +14,7 @@
1414
#include <hermes/inspector/RuntimeAdapter.h>
1515
#include <hermes/inspector/chrome/CDPHandler.h>
1616
#else // HERMES_ENABLE_DEBUGGER
17-
#include <jsinspector-modern/FallbackRuntimeAgent.h>
17+
#include <jsinspector-modern/FallbackRuntimeAgentDelegate.h>
1818
#endif // HERMES_ENABLE_DEBUGGER
1919

2020
#include <hermes/hermes.h>
@@ -30,12 +30,12 @@ namespace {
3030

3131
/**
3232
* An implementation of the Hermes RuntimeAdapter interface (part of
33-
* Hermes's CDPHandler API) for use within a React Native RuntimeAgent.
33+
* Hermes's CDPHandler API) for use within a React Native RuntimeAgentDelegate.
3434
*/
35-
class HermesRuntimeAgentAdapter
35+
class HermesRuntimeAgentDelegateAdapter
3636
: public hermes::inspector_modern::RuntimeAdapter {
3737
public:
38-
HermesRuntimeAgentAdapter(
38+
HermesRuntimeAgentDelegateAdapter(
3939
std::shared_ptr<hermes::HermesRuntime> runtime,
4040
RuntimeExecutor runtimeExecutor)
4141
: runtime_(runtime), runtimeExecutor_(runtimeExecutor) {}
@@ -60,10 +60,10 @@ class HermesRuntimeAgentAdapter
6060
} // namespace
6161

6262
/**
63-
* A RuntimeAgent that handles requests from the Chrome DevTools Protocol for
64-
* an instance of Hermes.
63+
* A RuntimeAgentDelegate that handles requests from the Chrome DevTools
64+
* Protocol for an instance of Hermes.
6565
*/
66-
class HermesRuntimeAgent::Impl final : public RuntimeAgent {
66+
class HermesRuntimeAgentDelegate::Impl final : public RuntimeAgentDelegate {
6767
using HermesCDPHandler = hermes::inspector_modern::chrome::CDPHandler;
6868

6969
public:
@@ -84,7 +84,7 @@ class HermesRuntimeAgent::Impl final : public RuntimeAgent {
8484
std::shared_ptr<hermes::HermesRuntime> runtime,
8585
RuntimeExecutor runtimeExecutor)
8686
: hermes_(HermesCDPHandler::create(
87-
std::make_unique<HermesRuntimeAgentAdapter>(
87+
std::make_unique<HermesRuntimeAgentDelegateAdapter>(
8888
runtime,
8989
runtimeExecutor),
9090
/* waitForDebugger */ false,
@@ -133,25 +133,26 @@ class HermesRuntimeAgent::Impl final : public RuntimeAgent {
133133
#else // !HERMES_ENABLE_DEBUGGER
134134

135135
/**
136-
* A stub for HermesRuntimeAgent when Hermes is compiled without debugging
137-
* support.
136+
* A stub for HermesRuntimeAgentDelegate when Hermes is compiled without
137+
* debugging support.
138138
*/
139-
class HermesRuntimeAgent::Impl final : public FallbackRuntimeAgent {
139+
class HermesRuntimeAgentDelegate::Impl final
140+
: public FallbackRuntimeAgentDelegate {
140141
public:
141142
Impl(
142143
FrontendChannel frontendChannel,
143144
SessionState& sessionState,
144145
std::shared_ptr<hermes::HermesRuntime> runtime,
145146
RuntimeExecutor)
146-
: FallbackRuntimeAgent(
147+
: FallbackRuntimeAgentDelegate(
147148
std::move(frontendChannel),
148149
sessionState,
149150
runtime->description()) {}
150151
};
151152

152153
#endif // HERMES_ENABLE_DEBUGGER
153154

154-
HermesRuntimeAgent::HermesRuntimeAgent(
155+
HermesRuntimeAgentDelegate::HermesRuntimeAgentDelegate(
155156
FrontendChannel frontendChannel,
156157
SessionState& sessionState,
157158
std::shared_ptr<hermes::HermesRuntime> runtime,
@@ -162,7 +163,8 @@ HermesRuntimeAgent::HermesRuntimeAgent(
162163
std::move(runtime),
163164
std::move(runtimeExecutor))) {}
164165

165-
bool HermesRuntimeAgent::handleRequest(const cdp::PreparsedRequest& req) {
166+
bool HermesRuntimeAgentDelegate::handleRequest(
167+
const cdp::PreparsedRequest& req) {
166168
return impl_->handleRequest(req);
167169
}
168170

packages/react-native/ReactCommon/hermes/inspector-modern/chrome/HermesRuntimeAgent.h renamed to packages/react-native/ReactCommon/hermes/inspector-modern/chrome/HermesRuntimeAgentDelegate.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
namespace facebook::react::jsinspector_modern {
1616

1717
/**
18-
* A RuntimeAgent that handles requests from the Chrome DevTools Protocol for
19-
* an instance of Hermes.
18+
* A RuntimeAgentDelegate that handles requests from the Chrome DevTools
19+
* Protocol for an instance of Hermes.
2020
*/
21-
class HermesRuntimeAgent : public RuntimeAgent {
21+
class HermesRuntimeAgentDelegate : public RuntimeAgentDelegate {
2222
public:
2323
/**
2424
* \param frontendChannel A channel used to send responses and events to the
@@ -31,7 +31,7 @@ class HermesRuntimeAgent : public RuntimeAgent {
3131
* \c runtimeExecutor may drop scheduled work if the runtime is destroyed
3232
* first.
3333
*/
34-
HermesRuntimeAgent(
34+
HermesRuntimeAgentDelegate(
3535
FrontendChannel frontendChannel,
3636
SessionState& sessionState,
3737
std::shared_ptr<hermes::HermesRuntime> runtime,

packages/react-native/ReactCommon/jsinspector-modern/CONCEPTS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@ A single connection between a debugger frontend and a target. There can be multi
2121
### Agent
2222

2323
A handler for a subset of CDP messages for a specific target as part of a specific session.
24+
25+
### Agent Delegate
26+
27+
An interface between an Agent class and some integration-specific, per-session logic/state it relies on (that does not fit in a Target Delegate). For example, a RuntimeAgentDelegate is used by RuntimeAgent to host Hermes's native CDP handler and delegate messages to it. The interface may look exactly like an Agent (purely CDP messages in/out) or there may be a more involved API to expose state/functionality needed by the Agent.

packages/react-native/ReactCommon/jsinspector-modern/FallbackRuntimeAgent.cpp renamed to packages/react-native/ReactCommon/jsinspector-modern/FallbackRuntimeAgentDelegate.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
#include <jsinspector-modern/FallbackRuntimeAgent.h>
8+
#include "FallbackRuntimeAgentDelegate.h"
99

1010
#include <chrono>
1111
#include <string>
@@ -22,7 +22,7 @@ namespace facebook::react::jsinspector_modern {
2222
#define ANSI_STYLE_RESET "\x1B[23m"
2323
#define ANSI_COLOR_BG_YELLOW "\x1B[48;2;253;247;231m"
2424

25-
FallbackRuntimeAgent::FallbackRuntimeAgent(
25+
FallbackRuntimeAgentDelegate::FallbackRuntimeAgentDelegate(
2626
FrontendChannel frontendChannel,
2727
const SessionState& sessionState,
2828
std::string engineDescription)
@@ -32,7 +32,8 @@ FallbackRuntimeAgent::FallbackRuntimeAgent(
3232
}
3333
}
3434

35-
bool FallbackRuntimeAgent::handleRequest(const cdp::PreparsedRequest& req) {
35+
bool FallbackRuntimeAgentDelegate::handleRequest(
36+
const cdp::PreparsedRequest& req) {
3637
if (req.method == "Log.enable") {
3738
sendFallbackRuntimeWarning();
3839

@@ -44,15 +45,15 @@ bool FallbackRuntimeAgent::handleRequest(const cdp::PreparsedRequest& req) {
4445
return false;
4546
}
4647

47-
void FallbackRuntimeAgent::sendFallbackRuntimeWarning() {
48+
void FallbackRuntimeAgentDelegate::sendFallbackRuntimeWarning() {
4849
sendWarningLogEntry(
4950
"The current JavaScript engine, " ANSI_STYLE_ITALIC + engineDescription_ +
5051
ANSI_STYLE_RESET
5152
", does not support debugging over the Chrome DevTools Protocol. "
5253
"See https://reactnative.dev/docs/debugging for more information.");
5354
}
5455

55-
void FallbackRuntimeAgent::sendWarningLogEntry(std::string_view text) {
56+
void FallbackRuntimeAgentDelegate::sendWarningLogEntry(std::string_view text) {
5657
frontendChannel_(
5758
folly::toJson(folly::dynamic::object("method", "Log.entryAdded")(
5859
"params",

packages/react-native/ReactCommon/jsinspector-modern/FallbackRuntimeAgent.h renamed to packages/react-native/ReactCommon/jsinspector-modern/FallbackRuntimeAgentDelegate.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
namespace facebook::react::jsinspector_modern {
1616

1717
/**
18-
* A RuntimeAgent that handles requests from the Chrome DevTools Protocol for
19-
* a JavaScript runtime that does not support debugging.
18+
* A RuntimeAgentDelegate that handles requests from the Chrome DevTools
19+
* Protocol for a JavaScript runtime that does not support debugging.
2020
*/
21-
class FallbackRuntimeAgent : public RuntimeAgent {
21+
class FallbackRuntimeAgentDelegate : public RuntimeAgentDelegate {
2222
public:
2323
/**
2424
* \param frontendChannel A channel used to send responses and events to the
@@ -27,7 +27,7 @@ class FallbackRuntimeAgent : public RuntimeAgent {
2727
* \param engineDescription A description of the JavaScript engine being
2828
* debugged. This string will be used in messages sent to the frontend.
2929
*/
30-
FallbackRuntimeAgent(
30+
FallbackRuntimeAgentDelegate(
3131
FrontendChannel frontendChannel,
3232
const SessionState& sessionState,
3333
std::string engineDescription);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class InstanceTarget;
2424
* An Agent that handles requests from the Chrome DevTools Protocol for the
2525
* given InstanceTarget.
2626
*/
27-
class InstanceAgent {
27+
class InstanceAgent final {
2828
public:
2929
/**
3030
* \param frontendChannel A channel used to send responses and events to the

0 commit comments

Comments
 (0)