diff --git a/packages/react-native/ReactCommon/jsinspector-modern/RuntimeAgent.cpp b/packages/react-native/ReactCommon/jsinspector-modern/RuntimeAgent.cpp index d1821f6b74b728..8f6b0b548720a5 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/RuntimeAgent.cpp +++ b/packages/react-native/ReactCommon/jsinspector-modern/RuntimeAgent.cpp @@ -145,18 +145,6 @@ RuntimeAgent::~RuntimeAgent() { sessionState_.lastRuntimeAgentExportedState = getExportedState(); } -void RuntimeAgent::enableSamplingProfiler() { - targetController_.enableSamplingProfiler(); -} - -void RuntimeAgent::disableSamplingProfiler() { - targetController_.disableSamplingProfiler(); -} - -tracing::RuntimeSamplingProfile RuntimeAgent::collectSamplingProfile() { - return targetController_.collectSamplingProfile(); -} - #pragma mark - Tracing RuntimeTracingAgent::RuntimeTracingAgent( diff --git a/packages/react-native/ReactCommon/jsinspector-modern/RuntimeAgent.h b/packages/react-native/ReactCommon/jsinspector-modern/RuntimeAgent.h index 186eb0ba4e8d37..593f6d5d9e1286 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/RuntimeAgent.h +++ b/packages/react-native/ReactCommon/jsinspector-modern/RuntimeAgent.h @@ -85,21 +85,6 @@ class RuntimeAgent final { */ ExportedState getExportedState(); - /** - * Start sampling profiler for the corresponding RuntimeTarget. - */ - void enableSamplingProfiler(); - - /** - * Stop sampling profiler for the corresponding RuntimeTarget. - */ - void disableSamplingProfiler(); - - /** - * Return recorded sampling profile for the previous sampling session. - */ - tracing::RuntimeSamplingProfile collectSamplingProfile(); - private: FrontendChannel frontendChannel_; RuntimeTargetController& targetController_; diff --git a/packages/react-native/ReactCommon/jsinspector-modern/RuntimeTarget.cpp b/packages/react-native/ReactCommon/jsinspector-modern/RuntimeTarget.cpp index a42493ad55cc9e..0f84ecd0e17987 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/RuntimeTarget.cpp +++ b/packages/react-native/ReactCommon/jsinspector-modern/RuntimeTarget.cpp @@ -179,40 +179,71 @@ void RuntimeTarget::notifyDomainStateChanged( Domain domain, bool enabled, const RuntimeAgent& notifyingAgent) { - bool runtimeAndLogStatusBefore = false, runtimeAndLogStatusAfter = false; - if (domain == Domain::Log || domain == Domain::Runtime) { - runtimeAndLogStatusBefore = - agentsByEnabledDomain_[Domain::Runtime].contains(¬ifyingAgent) && - agentsByEnabledDomain_[Domain::Log].contains(¬ifyingAgent); - } - - if (enabled) { - agentsByEnabledDomain_[domain].insert(¬ifyingAgent); - } else { - agentsByEnabledDomain_[domain].erase(¬ifyingAgent); - } - threadSafeDomainStatus_[domain] = !agentsByEnabledDomain_[domain].empty(); - - if (domain == Domain::Log || domain == Domain::Runtime) { - runtimeAndLogStatusAfter = - agentsByEnabledDomain_[Domain::Runtime].contains(¬ifyingAgent) && - agentsByEnabledDomain_[Domain::Log].contains(¬ifyingAgent); + auto [domainStateChangedLocally, domainStateChangedGlobally] = + processDomainChange(domain, enabled, notifyingAgent); + + switch (domain) { + case Domain::Log: + case Domain::Runtime: { + auto otherDomain = domain == Domain::Log ? Domain::Runtime : Domain::Log; + // There should be an agent that enables both Log and Runtime domains. + if (!agentsByEnabledDomain_[otherDomain].contains(¬ifyingAgent)) { + break; + } - if (runtimeAndLogStatusBefore != runtimeAndLogStatusAfter) { - if (runtimeAndLogStatusAfter) { + if (domainStateChangedGlobally && enabled) { + assert(agentsWithRuntimeAndLogDomainsEnabled_ == 0); + emitDebuggerSessionCreated(); + ++agentsWithRuntimeAndLogDomainsEnabled_; + } else if (domainStateChangedGlobally) { + assert(agentsWithRuntimeAndLogDomainsEnabled_ == 1); + emitDebuggerSessionDestroyed(); + --agentsWithRuntimeAndLogDomainsEnabled_; + } else if (domainStateChangedLocally && enabled) { + // This is a case when given domain was already enabled by other Agent, + // so global state didn't change. if (++agentsWithRuntimeAndLogDomainsEnabled_ == 1) { emitDebuggerSessionCreated(); } - } else { - assert(agentsWithRuntimeAndLogDomainsEnabled_ > 0); + } else if (domainStateChangedLocally) { if (--agentsWithRuntimeAndLogDomainsEnabled_ == 0) { emitDebuggerSessionDestroyed(); } } + + break; + } + case Domain::Network: + break; + case Domain::kMaxValue: { + throw std::logic_error("Unexpected kMaxValue domain value provided"); } } } +std::pair RuntimeTarget::processDomainChange( + Domain domain, + bool enabled, + const RuntimeAgent& notifyingAgent) { + bool domainHadAgentsBefore = !agentsByEnabledDomain_[domain].empty(); + bool domainHasBeenEnabledBefore = + agentsByEnabledDomain_[domain].contains(¬ifyingAgent); + + if (enabled) { + agentsByEnabledDomain_[domain].insert(¬ifyingAgent); + } else { + agentsByEnabledDomain_[domain].erase(¬ifyingAgent); + } + threadSafeDomainStatus_[domain] = !agentsByEnabledDomain_[domain].empty(); + + bool domainHasAgentsAfter = !agentsByEnabledDomain_[domain].empty(); + + return { + domainHasBeenEnabledBefore ^ enabled, + domainHadAgentsBefore ^ domainHasAgentsAfter, + }; +} + bool RuntimeTarget::isDomainEnabled(Domain domain) const { return threadSafeDomainStatus_[domain]; } diff --git a/packages/react-native/ReactCommon/jsinspector-modern/RuntimeTarget.h b/packages/react-native/ReactCommon/jsinspector-modern/RuntimeTarget.h index 61e57959486644..480dfcbac5485f 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/RuntimeTarget.h +++ b/packages/react-native/ReactCommon/jsinspector-modern/RuntimeTarget.h @@ -21,6 +21,7 @@ #include #include +#include #ifndef JSINSPECTOR_EXPORT #ifdef _MSC_VER @@ -122,7 +123,7 @@ class RuntimeTargetDelegate { */ class RuntimeTargetController { public: - enum class Domain { Network, Runtime, Log, kMaxValue }; + enum class Domain { Log, Network, Runtime, kMaxValue }; explicit RuntimeTargetController(RuntimeTarget& target); @@ -347,6 +348,22 @@ class JSINSPECTOR_EXPORT RuntimeTarget bool enabled, const RuntimeAgent& notifyingAgent); + /** + * Processes the changes to the state of a given domain. + * + * Returns a pair of booleans: + * 1. Returns true, if an only if the given domain state changed locally, + * for a given session. + * 2. Returns true, if and only if the given domain state changed globally: + * when the given Agent is the only Agent that enabled given domain across + * sessions, or when the only Agent that had this domain enabled has + * disconnected. + */ + std::pair processDomainChange( + Domain domain, + bool enabled, + const RuntimeAgent& notifyingAgent); + /** * Checks whether the given domain is enabled in at least one session * that is currently connected. This may be called from any thread, with