Skip to content

Commit e699558

Browse files
motiz88facebook-github-bot
authored andcommitted
Save exported state across RuntimeAgent instances (#43098)
Summary: Pull Request resolved: #43098 Changelog: [Internal] Wraps Hermes's `CDPHandler::getState()` API in an engine-agnostic abstraction (`RuntimeAgentDelegate::getExportedState`). An Agent's lifetime ends when its Target is destroyed, but it can occasionally be useful to persist some state for the "next" Target+Agent of the same type (in the same session) to read. `RuntimeAgentDelegate` is polymorphic and can't just write arbitrary data to SessionState. Instead, it can now *export* a state object that we'll store and pass to the next `RuntimeTargetDelegate::createAgentDelegate` call. Reviewed By: huntie Differential Revision: D53919696 fbshipit-source-id: a8e9b921bc8fc2d195c5dddea9537e6ead3d0358
1 parent 01d704d commit e699558

21 files changed

Lines changed: 141 additions & 6 deletions

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ std::unique_ptr<jsinspector_modern::RuntimeAgentDelegate>
3939
JSExecutor::createAgentDelegate(
4040
jsinspector_modern::FrontendChannel frontendChannel,
4141
jsinspector_modern::SessionState& sessionState,
42+
std::unique_ptr<jsinspector_modern::RuntimeAgentDelegate::ExportedState>,
4243
const jsinspector_modern::ExecutionContextDescription&
4344
executionContextDescription) {
4445
(void)executionContextDescription;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ class RN_EXPORT JSExecutor : public jsinspector_modern::RuntimeTargetDelegate {
147147
createAgentDelegate(
148148
jsinspector_modern::FrontendChannel frontendChannel,
149149
jsinspector_modern::SessionState& sessionState,
150+
std::unique_ptr<jsinspector_modern::RuntimeAgentDelegate::ExportedState>
151+
previouslyExportedState,
150152
const jsinspector_modern::ExecutionContextDescription&
151153
executionContextDescription) override;
152154
};

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,16 @@ std::unique_ptr<jsinspector_modern::RuntimeAgentDelegate>
261261
HermesExecutor::createAgentDelegate(
262262
jsinspector_modern::FrontendChannel frontendChannel,
263263
jsinspector_modern::SessionState& sessionState,
264+
std::unique_ptr<jsinspector_modern::RuntimeAgentDelegate::ExportedState>
265+
previouslyExportedState,
264266
const jsinspector_modern::ExecutionContextDescription&
265267
executionContextDescription) {
266268
std::shared_ptr<HermesRuntime> hermesRuntimeShared(runtime_, &hermesRuntime_);
267269
return std::unique_ptr<jsinspector_modern::RuntimeAgentDelegate>(
268270
new jsinspector_modern::HermesRuntimeAgentDelegate(
269271
frontendChannel,
270272
sessionState,
273+
std::move(previouslyExportedState),
271274
executionContextDescription,
272275
hermesRuntimeShared,
273276
[jsQueueWeak = std::weak_ptr(jsQueue_),

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ class HermesExecutor : public JSIExecutor {
5858
createAgentDelegate(
5959
jsinspector_modern::FrontendChannel frontendChannel,
6060
jsinspector_modern::SessionState& sessionState,
61+
std::unique_ptr<jsinspector_modern::RuntimeAgentDelegate::ExportedState>
62+
previouslyExportedState,
6163
const jsinspector_modern::ExecutionContextDescription&
6264
executionContextDescription) override;
6365

‎packages/react-native/ReactCommon/hermes/inspector-modern/chrome/HermesRuntimeAgentDelegate.cpp‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,26 @@ class HermesRuntimeAgentDelegate::Impl final : public RuntimeAgentDelegate {
6767
using HermesCDPHandler = hermes::inspector_modern::chrome::CDPHandler;
6868
using HermesExecutionContextDescription =
6969
hermes::inspector_modern::chrome::CDPHandlerExecutionContextDescription;
70+
using HermesState = hermes::inspector_modern::chrome::State;
71+
72+
struct HermesStateWrapper : public ExportedState {
73+
explicit HermesStateWrapper(std::unique_ptr<HermesState> state)
74+
: state_(std::move(state)) {}
75+
76+
static std::unique_ptr<HermesState> unwrapDestructively(
77+
ExportedState* wrapper) {
78+
if (!wrapper) {
79+
return nullptr;
80+
}
81+
if (auto* typedWrapper = dynamic_cast<HermesStateWrapper*>(wrapper)) {
82+
return std::move(typedWrapper->state_);
83+
}
84+
return nullptr;
85+
}
86+
87+
private:
88+
std::unique_ptr<HermesState> state_;
89+
};
7090

7191
public:
7292
/**
@@ -75,6 +95,10 @@ class HermesRuntimeAgentDelegate::Impl final : public RuntimeAgentDelegate {
7595
* \param sessionState The state of the current CDP session. This will only
7696
* be accessed on the main thread (during the constructor, in handleRequest,
7797
* etc).
98+
* \param previouslyExportedState The exported state from a previous instance
99+
* of RuntimeAgentDelegate (NOT necessarily HermesRuntimeAgentDelegate). This
100+
* may be nullptr, and if not nullptr it may be of any concrete type that
101+
* implements RuntimeAgentDelegate::ExportedState.
78102
* \param executionContextDescription A description of the execution context
79103
* represented by this runtime. This is used for disambiguating the
80104
* source/destination of CDP messages when there are multiple runtimes
@@ -87,6 +111,8 @@ class HermesRuntimeAgentDelegate::Impl final : public RuntimeAgentDelegate {
87111
Impl(
88112
FrontendChannel frontendChannel,
89113
SessionState& sessionState,
114+
std::unique_ptr<RuntimeAgentDelegate::ExportedState>
115+
previouslyExportedState,
90116
const ExecutionContextDescription& executionContextDescription,
91117
std::shared_ptr<hermes::HermesRuntime> runtime,
92118
RuntimeExecutor runtimeExecutor)
@@ -96,7 +122,9 @@ class HermesRuntimeAgentDelegate::Impl final : public RuntimeAgentDelegate {
96122
runtimeExecutor),
97123
/* waitForDebugger */ false,
98124
/* enableConsoleAPICapturing */ false,
99-
/* state */ nullptr,
125+
/* state */
126+
HermesStateWrapper::unwrapDestructively(
127+
previouslyExportedState.get()),
100128
{.isRuntimeDomainEnabled = sessionState.isRuntimeDomainEnabled},
101129
HermesExecutionContextDescription{
102130
.id = executionContextDescription.id,
@@ -139,6 +167,10 @@ class HermesRuntimeAgentDelegate::Impl final : public RuntimeAgentDelegate {
139167
return true;
140168
}
141169

170+
virtual std::unique_ptr<ExportedState> getExportedState() override {
171+
return std::make_unique<HermesStateWrapper>(hermes_->getState());
172+
}
173+
142174
private:
143175
std::shared_ptr<HermesCDPHandler> hermes_;
144176
};
@@ -155,6 +187,7 @@ class HermesRuntimeAgentDelegate::Impl final
155187
Impl(
156188
FrontendChannel frontendChannel,
157189
SessionState& sessionState,
190+
std::unique_ptr<RuntimeAgentDelegate::ExportedState>,
158191
const ExecutionContextDescription&,
159192
std::shared_ptr<hermes::HermesRuntime> runtime,
160193
RuntimeExecutor)
@@ -169,12 +202,15 @@ class HermesRuntimeAgentDelegate::Impl final
169202
HermesRuntimeAgentDelegate::HermesRuntimeAgentDelegate(
170203
FrontendChannel frontendChannel,
171204
SessionState& sessionState,
205+
std::unique_ptr<RuntimeAgentDelegate::ExportedState>
206+
previouslyExportedState,
172207
const ExecutionContextDescription& executionContextDescription,
173208
std::shared_ptr<hermes::HermesRuntime> runtime,
174209
RuntimeExecutor runtimeExecutor)
175210
: impl_(std::make_unique<Impl>(
176211
std::move(frontendChannel),
177212
sessionState,
213+
std::move(previouslyExportedState),
178214
executionContextDescription,
179215
std::move(runtime),
180216
std::move(runtimeExecutor))) {}
@@ -184,4 +220,9 @@ bool HermesRuntimeAgentDelegate::handleRequest(
184220
return impl_->handleRequest(req);
185221
}
186222

223+
std::unique_ptr<HermesRuntimeAgentDelegate::ExportedState>
224+
HermesRuntimeAgentDelegate::getExportedState() {
225+
return impl_->getExportedState();
226+
}
227+
187228
} // namespace facebook::react::jsinspector_modern

‎packages/react-native/ReactCommon/hermes/inspector-modern/chrome/HermesRuntimeAgentDelegate.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ class HermesRuntimeAgentDelegate : public RuntimeAgentDelegate {
2626
* \param sessionState The state of the current CDP session. This will only
2727
* be accessed on the main thread (during the constructor, in handleRequest,
2828
* etc).
29+
* \param previouslyExportedState The exported state from a previous instance
30+
* of RuntimeAgentDelegate (NOT necessarily HermesRuntimeAgentDelegate). This
31+
* may be nullptr, and if not nullptr it may be of any concrete type that
32+
* implements RuntimeAgentDelegate::ExportedState.
2933
* \param executionContextDescription A description of the execution context
3034
* represented by this runtime. This is used for disambiguating the
3135
* source/destination of CDP messages when there are multiple runtimes
@@ -38,6 +42,8 @@ class HermesRuntimeAgentDelegate : public RuntimeAgentDelegate {
3842
HermesRuntimeAgentDelegate(
3943
FrontendChannel frontendChannel,
4044
SessionState& sessionState,
45+
std::unique_ptr<RuntimeAgentDelegate::ExportedState>
46+
previouslyExportedState,
4147
const ExecutionContextDescription& executionContextDescription,
4248
std::shared_ptr<hermes::HermesRuntime> runtime,
4349
RuntimeExecutor runtimeExecutor);
@@ -52,6 +58,8 @@ class HermesRuntimeAgentDelegate : public RuntimeAgentDelegate {
5258
*/
5359
bool handleRequest(const cdp::PreparsedRequest& req) override;
5460

61+
virtual std::unique_ptr<ExportedState> getExportedState() override;
62+
5563
private:
5664
// We use the private implementation idiom to keep HERMES_ENABLE_DEBUGGER
5765
// checks out of the header.

‎packages/react-native/ReactCommon/jsinspector-modern/RuntimeAgent.cpp‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
#include "RuntimeAgent.h"
9+
#include "SessionState.h"
910

1011
namespace facebook::react::jsinspector_modern {
1112

@@ -120,4 +121,18 @@ void RuntimeAgent::notifyBindingCalled(
120121
"name", bindingName)("payload", payload))));
121122
}
122123

124+
RuntimeAgent::ExportedState RuntimeAgent::getExportedState() {
125+
return {
126+
.delegateState = delegate_ ? delegate_->getExportedState() : nullptr,
127+
};
128+
}
129+
130+
RuntimeAgent::~RuntimeAgent() {
131+
// TODO: Eventually, there may be more than one Runtime per Page, and we'll
132+
// need to store multiple agent states here accordingly. For now let's do
133+
// the simple thing and assume (as we do elsewhere) that only one Runtime
134+
// per Page can exist at a time.
135+
sessionState_.lastRuntimeAgentExportedState = getExportedState();
136+
}
137+
123138
} // namespace facebook::react::jsinspector_modern

‎packages/react-native/ReactCommon/jsinspector-modern/RuntimeAgent.h‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
#include "InspectorInterfaces.h"
1111
#include "RuntimeAgentDelegate.h"
1212
#include "RuntimeTarget.h"
13-
#include "SessionState.h"
1413

1514
#include <jsinspector-modern/Parsing.h>
1615

1716
namespace facebook::react::jsinspector_modern {
1817

1918
class RuntimeTargetController;
19+
struct SessionState;
2020

2121
/**
2222
* An Agent that handles requests from the Chrome DevTools Protocol
@@ -48,6 +48,8 @@ class RuntimeAgent final {
4848
SessionState& sessionState,
4949
std::unique_ptr<RuntimeAgentDelegate> delegate);
5050

51+
~RuntimeAgent();
52+
5153
/**
5254
* Handle a CDP request. The response will be sent over the provided
5355
* \c FrontendChannel synchronously or asynchronously. Performs any
@@ -69,6 +71,17 @@ class RuntimeAgent final {
6971
const std::string& bindingName,
7072
const std::string& payload);
7173

74+
struct ExportedState {
75+
std::unique_ptr<RuntimeAgentDelegate::ExportedState> delegateState;
76+
};
77+
78+
/**
79+
* Export the RuntimeAgent's state, if available. This will be called
80+
* shortly before the RuntimeAgent is destroyed to preserve state that may be
81+
* needed when constructin a new RuntimeAgent.
82+
*/
83+
ExportedState getExportedState();
84+
7285
private:
7386
FrontendChannel frontendChannel_;
7487
RuntimeTargetController& targetController_;

‎packages/react-native/ReactCommon/jsinspector-modern/RuntimeAgentDelegate.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ namespace facebook::react::jsinspector_modern {
1919
*/
2020
class RuntimeAgentDelegate {
2121
public:
22+
class ExportedState {
23+
public:
24+
virtual ~ExportedState() = default;
25+
};
26+
2227
virtual ~RuntimeAgentDelegate() = default;
2328

2429
/**
@@ -30,6 +35,17 @@ class RuntimeAgentDelegate {
3035
* agent expects another agent to respond to the request instead.
3136
*/
3237
virtual bool handleRequest(const cdp::PreparsedRequest& req) = 0;
38+
39+
/**
40+
* Export RuntimeAgentDelegate-specific state that should persist across
41+
* consecutive RuntimeTargets in this session.
42+
* If the RuntimeTarget is destroyed and later logically replaced by a new
43+
* one (e.g. as part of an Instance reload), the state returned here will be
44+
* passed to \ref RuntimeTargetDelegate::createAgentDelegate.
45+
*/
46+
inline virtual std::unique_ptr<ExportedState> getExportedState() {
47+
return std::make_unique<ExportedState>();
48+
}
3349
};
3450

3551
} // namespace facebook::react::jsinspector_modern

‎packages/react-native/ReactCommon/jsinspector-modern/RuntimeTarget.cpp‎

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

8+
#include "SessionState.h"
9+
810
#include <jsinspector-modern/RuntimeTarget.h>
911

1012
using namespace facebook::jsi;
@@ -33,13 +35,18 @@ RuntimeTarget::RuntimeTarget(
3335
std::shared_ptr<RuntimeAgent> RuntimeTarget::createAgent(
3436
FrontendChannel channel,
3537
SessionState& sessionState) {
38+
auto runtimeAgentState =
39+
std::move(sessionState.lastRuntimeAgentExportedState);
3640
auto runtimeAgent = std::make_shared<RuntimeAgent>(
3741
channel,
3842
controller_,
3943
executionContextDescription_,
4044
sessionState,
4145
delegate_.createAgentDelegate(
42-
channel, sessionState, executionContextDescription_));
46+
channel,
47+
sessionState,
48+
std::move(runtimeAgentState.delegateState),
49+
executionContextDescription_));
4350
agents_.insert(runtimeAgent);
4451
return runtimeAgent;
4552
}

0 commit comments

Comments
 (0)