Skip to content

Commit de91719

Browse files
hoxyqfacebook-github-bot
authored andcommitted
jsinspector: capture ReactNativeApplication domain notifications (react#54244)
Summary: # Changelog: [Internal] Refactors the logic a bit, and adds `ReactNativeApplication` domain to the ones that are currently being tracked. We will use it as a signal for installation of `console.createTask()` implementation. Differential Revision: D85274860
1 parent 79393ff commit de91719

4 files changed

Lines changed: 109 additions & 25 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,16 +224,18 @@ class HostAgent::Impl final {
224224
std::move(stashedTraceRecording.value()));
225225
}
226226

227+
// Percolate down to the RuntimeAgent.
227228
return {
228-
.isFinishedHandlingRequest = true,
229+
.isFinishedHandlingRequest = false,
229230
.shouldSendOKResponse = true,
230231
};
231232
}
232233
if (req.method == "ReactNativeApplication.disable") {
233234
sessionState_.isReactNativeApplicationDomainEnabled = false;
234235

236+
// Percolate down to the RuntimeAgent.
235237
return {
236-
.isFinishedHandlingRequest = true,
238+
.isFinishedHandlingRequest = false,
237239
.shouldSendOKResponse = true,
238240
};
239241
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ RuntimeAgent::RuntimeAgent(
4343
targetController_.notifyDomainStateChanged(
4444
RuntimeTargetController::Domain::Network, true, *this);
4545
}
46+
47+
if (sessionState_.isReactNativeApplicationDomainEnabled) {
48+
targetController_.notifyDomainStateChanged(
49+
RuntimeTargetController::Domain::ReactNativeApplication, true, *this);
50+
}
4651
}
4752

4853
bool RuntimeAgent::handleRequest(const cdp::PreparsedRequest& req) {
@@ -84,6 +89,16 @@ bool RuntimeAgent::handleRequest(const cdp::PreparsedRequest& req) {
8489
sessionState_.isNetworkDomainEnabled,
8590
*this);
8691

92+
// We are not responding to this request, just processing a side effect.
93+
return false;
94+
} else if (
95+
req.method == "ReactNativeApplication.enable" ||
96+
req.method == "ReactNativeApplication.disable") {
97+
targetController_.notifyDomainStateChanged(
98+
RuntimeTargetController::Domain::ReactNativeApplication,
99+
sessionState_.isReactNativeApplicationDomainEnabled,
100+
*this);
101+
87102
// We are not responding to this request, just processing a side effect.
88103
return false;
89104
}
@@ -137,6 +152,10 @@ RuntimeAgent::~RuntimeAgent() {
137152
targetController_.notifyDomainStateChanged(
138153
RuntimeTargetController::Domain::Network, false, *this);
139154
}
155+
if (sessionState_.isReactNativeApplicationDomainEnabled) {
156+
targetController_.notifyDomainStateChanged(
157+
RuntimeTargetController::Domain::ReactNativeApplication, false, *this);
158+
}
140159

141160
// TODO: Eventually, there may be more than one Runtime per Page, and we'll
142161
// need to store multiple agent states here accordingly. For now let's do

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

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -179,40 +179,80 @@ void RuntimeTarget::notifyDomainStateChanged(
179179
Domain domain,
180180
bool enabled,
181181
const RuntimeAgent& notifyingAgent) {
182-
bool runtimeAndLogStatusBefore = false, runtimeAndLogStatusAfter = false;
183-
if (domain == Domain::Log || domain == Domain::Runtime) {
184-
runtimeAndLogStatusBefore =
185-
agentsByEnabledDomain_[Domain::Runtime].contains(&notifyingAgent) &&
186-
agentsByEnabledDomain_[Domain::Log].contains(&notifyingAgent);
187-
}
188-
189-
if (enabled) {
190-
agentsByEnabledDomain_[domain].insert(&notifyingAgent);
191-
} else {
192-
agentsByEnabledDomain_[domain].erase(&notifyingAgent);
193-
}
194-
threadSafeDomainStatus_[domain] = !agentsByEnabledDomain_[domain].empty();
195-
196-
if (domain == Domain::Log || domain == Domain::Runtime) {
197-
runtimeAndLogStatusAfter =
198-
agentsByEnabledDomain_[Domain::Runtime].contains(&notifyingAgent) &&
199-
agentsByEnabledDomain_[Domain::Log].contains(&notifyingAgent);
182+
auto [domainStateChangedLocally, domainStateChangedGlobally] =
183+
processDomainChange(domain, enabled, notifyingAgent);
184+
185+
switch (domain) {
186+
case Domain::Log:
187+
case Domain::Runtime: {
188+
auto otherDomain = domain == Domain::Log ? Domain::Runtime : Domain::Log;
189+
// There should be an agent that enables both Log and Runtime domains.
190+
if (!agentsByEnabledDomain_[otherDomain].contains(&notifyingAgent)) {
191+
break;
192+
}
200193

201-
if (runtimeAndLogStatusBefore != runtimeAndLogStatusAfter) {
202-
if (runtimeAndLogStatusAfter) {
194+
if (domainStateChangedGlobally && enabled) {
195+
assert(agentsWithRuntimeAndLogDomainsEnabled_ == 0);
196+
emitDebuggerSessionCreated();
197+
++agentsWithRuntimeAndLogDomainsEnabled_;
198+
} else if (domainStateChangedGlobally) {
199+
assert(agentsWithRuntimeAndLogDomainsEnabled_ == 1);
200+
emitDebuggerSessionDestroyed();
201+
--agentsWithRuntimeAndLogDomainsEnabled_;
202+
} else if (domainStateChangedLocally && enabled) {
203+
// This is a case when given domain was already enabled by other Agent,
204+
// so global state didn't change.
203205
if (++agentsWithRuntimeAndLogDomainsEnabled_ == 1) {
204206
emitDebuggerSessionCreated();
205207
}
206-
} else {
207-
assert(agentsWithRuntimeAndLogDomainsEnabled_ > 0);
208+
} else if (domainStateChangedLocally) {
208209
if (--agentsWithRuntimeAndLogDomainsEnabled_ == 0) {
209210
emitDebuggerSessionDestroyed();
210211
}
211212
}
213+
214+
break;
215+
}
216+
case Domain::ReactNativeApplication: {
217+
if (domainStateChangedGlobally && enabled) {
218+
// installConsoleCreateTask();
219+
} else if (domainStateChangedGlobally) {
220+
// stubConsoleCreateTask();
221+
}
222+
223+
break;
224+
}
225+
case Domain::Network:
226+
break;
227+
case Domain::kMaxValue: {
228+
throw std::logic_error("Unexpected kMaxValue domain value provided");
212229
}
213230
}
214231
}
215232

233+
std::pair<bool, bool> RuntimeTarget::processDomainChange(
234+
Domain domain,
235+
bool enabled,
236+
const RuntimeAgent& notifyingAgent) {
237+
bool domainHadAgentsBefore = !agentsByEnabledDomain_[domain].empty();
238+
bool domainHasBeenEnabledBefore =
239+
agentsByEnabledDomain_[domain].contains(&notifyingAgent);
240+
241+
if (enabled) {
242+
agentsByEnabledDomain_[domain].insert(&notifyingAgent);
243+
} else {
244+
agentsByEnabledDomain_[domain].erase(&notifyingAgent);
245+
}
246+
threadSafeDomainStatus_[domain] = !agentsByEnabledDomain_[domain].empty();
247+
248+
bool domainHasAgentsAfter = !agentsByEnabledDomain_[domain].empty();
249+
250+
return {
251+
domainHasBeenEnabledBefore ^ enabled,
252+
domainHadAgentsBefore ^ domainHasAgentsAfter,
253+
};
254+
}
255+
216256
bool RuntimeTarget::isDomainEnabled(Domain domain) const {
217257
return threadSafeDomainStatus_[domain];
218258
}

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <jsinspector-modern/tracing/TraceRecordingState.h>
2222

2323
#include <memory>
24+
#include <utility>
2425

2526
#ifndef JSINSPECTOR_EXPORT
2627
#ifdef _MSC_VER
@@ -122,7 +123,13 @@ class RuntimeTargetDelegate {
122123
*/
123124
class RuntimeTargetController {
124125
public:
125-
enum class Domain { Network, Runtime, Log, kMaxValue };
126+
enum class Domain {
127+
Log,
128+
Network,
129+
ReactNativeApplication,
130+
Runtime,
131+
kMaxValue
132+
};
126133

127134
explicit RuntimeTargetController(RuntimeTarget& target);
128135

@@ -347,6 +354,22 @@ class JSINSPECTOR_EXPORT RuntimeTarget
347354
bool enabled,
348355
const RuntimeAgent& notifyingAgent);
349356

357+
/**
358+
* Processes the changes to the state of a given domain.
359+
*
360+
* Returns a pair of booleans:
361+
* 1. Returns true, if an only if the given domain state changed locally,
362+
* for a given session.
363+
* 2. Returns true, if and only if the given domain state changed globally:
364+
* when the given Agent is the only Agent that enabled given domain across
365+
* sessions, or when the only Agent that had this domain enabled has
366+
* disconnected.
367+
*/
368+
std::pair<bool, bool> processDomainChange(
369+
Domain domain,
370+
bool enabled,
371+
const RuntimeAgent& notifyingAgent);
372+
350373
/**
351374
* Checks whether the given domain is enabled in at least one session
352375
* that is currently connected. This may be called from any thread, with

0 commit comments

Comments
 (0)