Skip to content

Commit

Permalink
Add InspectorFlags, conditionally disable Hermes CDP registration (#4…
Browse files Browse the repository at this point in the history
…1672)

Summary:
Pull Request resolved: #41672

Progress towards an opt-in setup for our new CDP backend.

- Adds `InspectorFlags.h`, a singleton intended to allow convienient access to static boolean feature flags for the new CDP backend/inspector features across platforms. This will be written to in upcoming diffs, with the accessor for `enable_modern_cdp_registry` soft-defaulting to `false` here.
- References this to conditionally disable legacy ~CDP registration in `HermesExecutorFactory` (Bridge) and `HermesInstance` (Bridgeless) code paths.
- Stubs a `false` value for `react_native_devx:enable_modern_cdp_registry` in `EmptyReactNativeConfig` (documentation/convenience point for open source partners and integrators).

Changelog: [Internal]

Reviewed By: motiz88

Differential Revision: D51563107

fbshipit-source-id: 446f319228ec627fdc0ecba9517a1a3faad9d262
  • Loading branch information
huntie authored and facebook-github-bot committed Dec 12, 2023
1 parent 1727ffa commit b6adbf7
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <cxxreact/SystraceSection.h>
#include <hermes/hermes.h>
#include <jsi/decorator.h>
#include <jsinspector-modern/InspectorFlags.h>

#include <hermes/inspector-modern/chrome/Registration.h>
#include <hermes/inspector/RuntimeAdapter.h>
Expand Down Expand Up @@ -200,11 +201,14 @@ std::unique_ptr<JSExecutor> HermesExecutorFactory::createJSExecutor(
}

HermesRuntime& hermesRuntimeRef = *hermesRuntime;
auto& inspectorFlags = jsinspector_modern::InspectorFlags::getInstance();
bool enableDebugger =
!inspectorFlags.getEnableModernCDPRegistry() && enableDebugger_;
auto decoratedRuntime = std::make_shared<DecoratedRuntime>(
std::move(hermesRuntime),
hermesRuntimeRef,
jsQueue,
enableDebugger_,
enableDebugger,
debuggerName_);

// So what do we have now?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ file(GLOB jsinspector_SRC CONFIGURE_DEPENDS *.cpp)
add_library(jsinspector STATIC ${jsinspector_SRC})

target_include_directories(jsinspector PUBLIC ${REACT_COMMON_DIR})

target_link_libraries(jsinspector
glog
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#include <glog/logging.h>
#include <cassert>

#include "InspectorFlags.h"

namespace facebook::react::jsinspector_modern {

InspectorFlags& InspectorFlags::getInstance() {
static InspectorFlags instance;
return instance;
}

void InspectorFlags::initFromConfig(
const ReactNativeConfig& reactNativeConfig) {
bool enableModernCDPRegistry =
reactNativeConfig.getBool("react_native_devx:enable_modern_cdp_registry");
if (enableModernCDPRegistry_.has_value()) {
assert(
*enableModernCDPRegistry_ == enableModernCDPRegistry &&
"Flag value was changed after init");
}
enableModernCDPRegistry_ = enableModernCDPRegistry;
}

bool InspectorFlags::getEnableModernCDPRegistry() const {
if (!enableModernCDPRegistry_.has_value()) {
LOG(WARNING)
<< "InspectorFlags::getEnableModernCDPRegistry was called before init";
}
return enableModernCDPRegistry_.value_or(false);
}

} // namespace facebook::react::jsinspector_modern
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#pragma once

#include <optional>

#include <react/config/ReactNativeConfig.h>

namespace facebook::react::jsinspector_modern {

/**
* A container for all inspector related feature flags (Meyers singleton
* pattern). Flags must be set before they are accessed and are static for the
* lifetime of the app.
*/
class InspectorFlags {
public:
static InspectorFlags& getInstance();

/**
* Initialize flags from a `ReactNativeConfig` instance. Validates that flag
* values are not changed across multiple calls.
*/
void initFromConfig(const ReactNativeConfig& reactNativeConfig);

/**
* Flag determining if the modern CDP backend should be enabled.
*/
bool getEnableModernCDPRegistry() const;

private:
InspectorFlags() = default;
InspectorFlags(const InspectorFlags&) = delete;
InspectorFlags& operator=(const InspectorFlags&) = delete;
~InspectorFlags() = default;

std::optional<bool> enableModernCDPRegistry_;
};

} // namespace facebook::react::jsinspector_modern
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ Pod::Spec.new do |s|
s.source_files = "*.{cpp,h}"
s.header_dir = 'jsinspector'
s.pod_target_xcconfig = { "CLANG_CXX_LANGUAGE_STANDARD" => "c++20" }

s.dependency "glog"

add_dependency(s, "React-nativeconfig")
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,13 @@

namespace facebook::react {

/**
* ReactNative configuration as provided by the hosting app.
* Provide a sub-class implementation to allow app specific customization.
*/
ReactNativeConfig::ReactNativeConfig() {}

ReactNativeConfig::~ReactNativeConfig() {}

EmptyReactNativeConfig::EmptyReactNativeConfig() {}

bool EmptyReactNativeConfig::getBool(const std::string& param) const {
if (param == "react_fabric:enabled_layout_animations_ios") {
return true;
}
if (param == "react_native_devx:enable_modern_cdp_registry") {
return false;
}
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ namespace facebook::react {
*/
class ReactNativeConfig {
public:
ReactNativeConfig();
virtual ~ReactNativeConfig();
ReactNativeConfig() = default;
virtual ~ReactNativeConfig() = default;

virtual bool getBool(const std::string& param) const = 0;
virtual std::string getString(const std::string& param) const = 0;
Expand All @@ -31,7 +31,7 @@ class ReactNativeConfig {
*/
class EmptyReactNativeConfig : public ReactNativeConfig {
public:
EmptyReactNativeConfig();
EmptyReactNativeConfig() = default;

bool getBool(const std::string& param) const override;
std::string getString(const std::string& param) const override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "HermesInstance.h"

#include <jsi/jsilib.h>
#include <jsinspector-modern/InspectorFlags.h>

#ifdef HERMES_ENABLE_DEBUGGER
#include <hermes/inspector-modern/chrome/Registration.h>
Expand Down Expand Up @@ -141,10 +142,13 @@ std::unique_ptr<JSRuntime> HermesInstance::createJSRuntime(
hermes::makeHermesRuntime(runtimeConfigBuilder.build());

#ifdef HERMES_ENABLE_DEBUGGER
std::unique_ptr<DecoratedRuntime> decoratedRuntime =
std::make_unique<DecoratedRuntime>(
std::move(hermesRuntime), msgQueueThread);
return std::make_unique<JSIRuntimeHolder>(std::move(decoratedRuntime));
auto& inspectorFlags = jsinspector_modern::InspectorFlags::getInstance();
if (!inspectorFlags.getEnableModernCDPRegistry()) {
std::unique_ptr<DecoratedRuntime> decoratedRuntime =
std::make_unique<DecoratedRuntime>(
std::move(hermesRuntime), msgQueueThread);
return std::make_unique<JSIRuntimeHolder>(std::move(decoratedRuntime));
}
#endif

return std::make_unique<JSIRuntimeHolder>(std::move(hermesRuntime));
Expand Down
Loading

0 comments on commit b6adbf7

Please sign in to comment.