From 4a055e481269602e3113cfb77aa718d8020fc58e Mon Sep 17 00:00:00 2001 From: Alex Hunt Date: Wed, 26 Mar 2025 15:40:42 -0700 Subject: [PATCH] Configure prod version of NetworkReporter (#50286) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/50286 Configures a thinner version of `NetworkReporter.cpp` (the entry point to `jsinspector_network`) in production builds (i.e. dev or profiling build, as gated by `REACT_NATIVE_DEBUGGER_ENABLED`). Even though enabling the CDP Network domain isn't reachable in prod, this will reduce the compiled code footprint of `jsinspector_network`. We also don't need `glog` here any more — remove dep. **Implementation notes** The reason to gate the logic within each method, with inline `#ifdefs`, rather than swapping between complete implementation files, is because each `report*` function will also have load-bearing logic (for the Performance API) in all prod builds. Therefore, this will reduce duplication (and file switching) down the line — and can be understood at a glance with this pattern. Changelog: [Internal] Reviewed By: robhogan Differential Revision: D71636694 fbshipit-source-id: 50e7c008bb6fd128fe1248d767832b36ccb0219b --- .../jsinspector-modern/network/CMakeLists.txt | 1 - .../network/NetworkReporter.cpp | 26 +++++++++++++++++++ .../network/NetworkReporter.h | 2 ++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/react-native/ReactCommon/jsinspector-modern/network/CMakeLists.txt b/packages/react-native/ReactCommon/jsinspector-modern/network/CMakeLists.txt index 67017852a2f..1749a26b3e0 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/network/CMakeLists.txt +++ b/packages/react-native/ReactCommon/jsinspector-modern/network/CMakeLists.txt @@ -23,6 +23,5 @@ target_include_directories(jsinspector_network PUBLIC ${REACT_COMMON_DIR}) target_link_libraries(jsinspector_network folly_runtime - glog jsinspector_cdp ) diff --git a/packages/react-native/ReactCommon/jsinspector-modern/network/NetworkReporter.cpp b/packages/react-native/ReactCommon/jsinspector-modern/network/NetworkReporter.cpp index a3e0ee29c5b..44e49417ece 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/network/NetworkReporter.cpp +++ b/packages/react-native/ReactCommon/jsinspector-modern/network/NetworkReporter.cpp @@ -7,16 +7,23 @@ #include "NetworkReporter.h" +#ifdef REACT_NATIVE_DEBUGGER_ENABLED #include "CdpNetwork.h" +#endif +#ifdef REACT_NATIVE_DEBUGGER_ENABLED #include #include +#endif +#ifdef REACT_NATIVE_DEBUGGER_ENABLED #include +#endif #include namespace facebook::react::jsinspector_modern { +#ifdef REACT_NATIVE_DEBUGGER_ENABLED namespace { /** @@ -34,6 +41,7 @@ double getCurrentUnixTimestampSeconds() { } } // namespace +#endif NetworkReporter& NetworkReporter::getInstance() { static NetworkReporter instance; @@ -67,6 +75,8 @@ void NetworkReporter::reportRequestStart( const RequestInfo& requestInfo, int encodedDataLength, const std::optional& redirectResponse) const { +#ifdef REACT_NATIVE_DEBUGGER_ENABLED + // Debug build: CDP event handling if (!isDebuggingEnabledNoSync()) { return; } @@ -94,32 +104,41 @@ void NetworkReporter::reportRequestStart( frontendChannel_( cdp::jsonNotification("Network.requestWillBeSent", params.toDynamic())); +#endif } void NetworkReporter::reportConnectionTiming( const std::string& /*requestId*/) const { +#ifdef REACT_NATIVE_DEBUGGER_ENABLED + // Debug build: CDP event handling if (!isDebuggingEnabledNoSync()) { return; } // TODO(T218236597) throw std::runtime_error("Not implemented"); +#endif } void NetworkReporter::reportRequestFailed( const std::string& /*requestId*/) const { +#ifdef REACT_NATIVE_DEBUGGER_ENABLED + // Debug build: CDP event handling if (!isDebuggingEnabledNoSync()) { return; } // TODO(T218236855) throw std::runtime_error("Not implemented"); +#endif } void NetworkReporter::reportResponseStart( const std::string& requestId, const ResponseInfo& responseInfo, int encodedDataLength) const { +#ifdef REACT_NATIVE_DEBUGGER_ENABLED + // Debug build: CDP event handling if (!isDebuggingEnabledNoSync()) { return; } @@ -137,21 +156,27 @@ void NetworkReporter::reportResponseStart( frontendChannel_( cdp::jsonNotification("Network.responseReceived", params.toDynamic())); +#endif } void NetworkReporter::reportDataReceived( const std::string& /*requestId*/) const { +#ifdef REACT_NATIVE_DEBUGGER_ENABLED + // Debug build: CDP event handling if (!isDebuggingEnabledNoSync()) { return; } // TODO(T218236266) throw std::runtime_error("Not implemented"); +#endif } void NetworkReporter::reportResponseEnd( const std::string& requestId, int encodedDataLength) const { +#ifdef REACT_NATIVE_DEBUGGER_ENABLED + // Debug build: CDP event handling if (!isDebuggingEnabledNoSync()) { return; } @@ -164,6 +189,7 @@ void NetworkReporter::reportResponseEnd( frontendChannel_( cdp::jsonNotification("Network.loadingFinished", params.toDynamic())); +#endif } } // namespace facebook::react::jsinspector_modern diff --git a/packages/react-native/ReactCommon/jsinspector-modern/network/NetworkReporter.h b/packages/react-native/ReactCommon/jsinspector-modern/network/NetworkReporter.h index fc8c4d6f69c..c6d281e15c7 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/network/NetworkReporter.h +++ b/packages/react-native/ReactCommon/jsinspector-modern/network/NetworkReporter.h @@ -25,6 +25,8 @@ using FrontendChannel = std::function; /** * [Experimental] An interface for reporting network events to the modern * debugger server and Web Performance APIs. + * + * In a production (non dev or profiling) build, CDP reporting is disabled. */ class NetworkReporter { public: