Skip to content

Commit

Permalink
Make UIConstantsProviderManager a static method
Browse files Browse the repository at this point in the history
Differential Revision: D55241233
  • Loading branch information
javache authored and facebook-github-bot committed Mar 22, 2024
1 parent b1066ba commit fefb6c0
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 216 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
import com.facebook.react.uimanager.ComponentNameResolverBinding;
import com.facebook.react.uimanager.DisplayMetricsHolder;
import com.facebook.react.uimanager.IllegalViewOperationException;
import com.facebook.react.uimanager.UIConstantsProviderManager;
import com.facebook.react.uimanager.UIConstantsProviderBinding;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.UIManagerModuleConstantsHelper;
import com.facebook.react.uimanager.ViewManager;
Expand Down Expand Up @@ -97,8 +97,6 @@ final class ReactInstance {

private JavaScriptContextHolder mJavaScriptContextHolder;

@DoNotStrip @Nullable private UIConstantsProviderManager mUIConstantsProviderManager;

static {
loadLibraryIfNeeded();
}
Expand Down Expand Up @@ -231,46 +229,43 @@ final class ReactInstance {
if (ReactFeatureFlags.useNativeViewConfigsInBridgelessMode) {
Map<String, Object> customDirectEvents = new HashMap<>();

mUIConstantsProviderManager =
new UIConstantsProviderManager(
// Use unbuffered RuntimeExecutor to install binding
unbufferedRuntimeExecutor,
// Here we are construncting the return value for UIManager.getConstants call.
// The old architectre relied on the constatnts struct to contain:
// 1. Eagerly loaded view configs for all native components.
// 2. genericBubblingEventTypes.
// 3. genericDirectEventTypes.
// We want to match this beahavior.
() -> {
return (NativeMap)
Arguments.makeNativeMap(
UIManagerModuleConstantsHelper.getDefaultExportableEventTypes());
},
(String viewManagerName) -> {
ViewManager viewManager = mViewManagerResolver.getViewManager(viewManagerName);
if (viewManager == null) {
return null;
}
return (NativeMap)
UIManagerModule.getConstantsForViewManager(viewManager, customDirectEvents);
},
() -> {
List<ViewManager> viewManagers =
new ArrayList<ViewManager>(
mViewManagerResolver.getEagerViewManagerMap().values());

Map<String, Object> constants =
UIManagerModule.createConstants(viewManagers, null, customDirectEvents);

Collection<String> lazyViewManagers =
mViewManagerResolver.getLazyViewManagerNames();
if (lazyViewManagers.size() > 0) {
constants.put("ViewManagerNames", new ArrayList<>(lazyViewManagers));
constants.put("LazyViewManagersEnabled", true);
}

return Arguments.makeNativeMap(constants);
});
UIConstantsProviderBinding.install(
// Use unbuffered RuntimeExecutor to install binding
unbufferedRuntimeExecutor,
// Here we are construncting the return value for UIManager.getConstants call.
// The old architectre relied on the constatnts struct to contain:
// 1. Eagerly loaded view configs for all native components.
// 2. genericBubblingEventTypes.
// 3. genericDirectEventTypes.
// We want to match this beahavior.
() -> {
return (NativeMap)
Arguments.makeNativeMap(
UIManagerModuleConstantsHelper.getDefaultExportableEventTypes());
},
(String viewManagerName) -> {
ViewManager viewManager = mViewManagerResolver.getViewManager(viewManagerName);
if (viewManager == null) {
return null;
}
return (NativeMap)
UIManagerModule.getConstantsForViewManager(viewManager, customDirectEvents);
},
() -> {
List<ViewManager> viewManagers =
new ArrayList<ViewManager>(mViewManagerResolver.getEagerViewManagerMap().values());

Map<String, Object> constants =
UIManagerModule.createConstants(viewManagers, null, customDirectEvents);

Collection<String> lazyViewManagers = mViewManagerResolver.getLazyViewManagerNames();
if (lazyViewManagers.size() > 0) {
constants.put("ViewManagerNames", new ArrayList<>(lazyViewManagers));
constants.put("LazyViewManagersEnabled", true);
}

return Arguments.makeNativeMap(constants);
});
}

EventBeatManager eventBeatManager = new EventBeatManager();
Expand Down Expand Up @@ -430,7 +425,6 @@ public Collection<NativeModule> getNativeModules() {
mFabricUIManager.invalidate();
mJavaTimerManager.onInstanceDestroy();
mHybridData.resetNative();
mUIConstantsProviderManager = null;
mJavaScriptContextHolder.clear();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
package com.facebook.react.uimanager;

import com.facebook.infer.annotation.Nullsafe;
import com.facebook.jni.HybridData;
import com.facebook.proguard.annotations.DoNotStrip;
import com.facebook.proguard.annotations.DoNotStripAny;
import com.facebook.react.bridge.NativeMap;
import com.facebook.react.bridge.RuntimeExecutor;
Expand All @@ -18,42 +16,18 @@

@Nullsafe(Nullsafe.Mode.LOCAL)
@DoNotStripAny
public class UIConstantsProviderManager {
public class UIConstantsProviderBinding {

static {
staticInit();
}

@DoNotStrip
@SuppressWarnings("unused")
private final HybridData mHybridData;

public UIConstantsProviderManager(
RuntimeExecutor runtimeExecutor,
DefaultEventTypesProvider defaultEventTypesProvider,
ConstantsForViewManagerProvider viewManagerConstantsProvider,
ConstantsProvider constantsProvider) {
mHybridData =
initHybrid(
runtimeExecutor,
defaultEventTypesProvider,
viewManagerConstantsProvider,
constantsProvider);
installJSIBindings();
SoLoader.loadLibrary("uimanagerjni");
}

private native HybridData initHybrid(
public static native void install(
RuntimeExecutor runtimeExecutor,
DefaultEventTypesProvider defaultEventTypesProvider,
ConstantsForViewManagerProvider viewManagerConstantsProvider,
ConstantsProvider constantsProvider);

private native void installJSIBindings();

private static void staticInit() {
SoLoader.loadLibrary("uimanagerjni");
}

@DoNotStripAny
public static interface DefaultEventTypesProvider {
/* Returns UIManager's constants. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
#include <fbjni/fbjni.h>

#include "ComponentNameResolverBinding.h"
#include "UIConstantsProviderManager.h"
#include "UIConstantsProviderBinding.h"

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*) {
return facebook::jni::initialize(vm, [] {
facebook::react::ComponentNameResolverBinding::registerNatives();
facebook::react::UIConstantsProviderManager::registerNatives();
facebook::react::UIConstantsProviderBinding::registerNatives();
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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 "UIConstantsProviderBinding.h"

#include <react/runtime/nativeviewconfig/LegacyUIManagerConstantsProviderBinding.h>

namespace facebook::react {

using namespace facebook::jni;

void UIConstantsProviderBinding::registerNatives() {
javaClassStatic()->registerNatives({
makeNativeMethod("install", UIConstantsProviderBinding::install),
});
}

void UIConstantsProviderBinding::install(
jni::alias_ref<jclass> /* unused */,
jni::alias_ref<JRuntimeExecutor::javaobject> runtimeExecutor,
jni::alias_ref<DefaultEventTypesProvider::javaobject>
defaultExportableEventTypesProvider,
jni::alias_ref<ConstantsForViewManagerProvider::javaobject>
constantsForViewManagerProvider,
jni::alias_ref<ConstantsProvider::javaobject> constantsProvider) {
auto executor = runtimeExecutor->cthis()->get();
executor([defaultExportableEventTypesProvider =
make_global(defaultExportableEventTypesProvider),
constantsForViewManagerProvider =
make_global(constantsForViewManagerProvider),
constantsProvider =
make_global(constantsProvider)](jsi::Runtime& runtime) {
LegacyUIManagerConstantsProviderBinding::install(
runtime,
"getDefaultEventTypes",
[provider = std::move(defaultExportableEventTypesProvider)](
jsi::Runtime& runtime) {
return jsi::valueFromDynamic(
runtime, provider->getDefaultEventTypes());
});

LegacyUIManagerConstantsProviderBinding::install(
runtime,
"getConstantsForViewManager",
[provider = std::move(constantsForViewManagerProvider)](
jsi::Runtime& runtime, const std::string& viewManagerName) {
return jsi::valueFromDynamic(
runtime, provider->getConstantsForViewManager(viewManagerName));
});

LegacyUIManagerConstantsProviderBinding::install(
runtime,
"getConstants",
[provider = std::move(constantsProvider)](jsi::Runtime& runtime) {
return jsi::valueFromDynamic(runtime, provider->getConstants());
});
});
}

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ class DefaultEventTypesProvider
static constexpr const char* kJavaDescriptor =
"Lcom/facebook/react/uimanager/UIConstantsProviderManager$DefaultEventTypesProvider;";

jsi::Value getDefaultEventTypes(jsi::Runtime& runtime) {
folly::dynamic getDefaultEventTypes() const {
static auto method =
javaClassStatic()
->getMethod<jni::alias_ref<NativeMap::jhybridobject>()>(
"getDefaultEventTypes");
auto result = method(self());
return jsi::valueFromDynamic(runtime, result->cthis()->consume());
return method(self())->cthis()->consume();
}
};

Expand All @@ -38,18 +37,17 @@ class ConstantsForViewManagerProvider
static constexpr const char* kJavaDescriptor =
"Lcom/facebook/react/uimanager/UIConstantsProviderManager$ConstantsForViewManagerProvider;";

jsi::Value getConstantsForViewManager(
jsi::Runtime& runtime,
std::string viewManagerName) {
folly::dynamic getConstantsForViewManager(
const std::string& viewManagerName) const {
static auto method =
javaClassStatic()
->getMethod<jni::alias_ref<NativeMap::jhybridobject>(std::string)>(
"getConstantsForViewManager");
->getMethod<jni::alias_ref<NativeMap::jhybridobject>(
const std::string&)>("getConstantsForViewManager");
auto result = method(self(), viewManagerName);
if (result == nullptr) {
return jsi::Value::null();
return nullptr;
}
return jsi::valueFromDynamic(runtime, result->cthis()->consume());
return result->cthis()->consume();
}
};

Expand All @@ -58,24 +56,23 @@ class ConstantsProvider : public jni::JavaClass<ConstantsProvider> {
static constexpr const char* kJavaDescriptor =
"Lcom/facebook/react/uimanager/UIConstantsProviderManager$ConstantsProvider;";

jsi::Value getConstants(jsi::Runtime& runtime) {
folly::dynamic getConstants() const {
static auto method =
javaClassStatic()
->getMethod<jni::alias_ref<NativeMap::jhybridobject>()>(
"getConstants");
auto result = method(self());
return jsi::valueFromDynamic(runtime, result->cthis()->consume());
return method(self())->cthis()->consume();
}
};

class UIConstantsProviderManager
: public facebook::jni::HybridClass<UIConstantsProviderManager> {
class UIConstantsProviderBinding
: public facebook::jni::JavaClass<UIConstantsProviderBinding> {
public:
static auto constexpr kJavaDescriptor =
"Lcom/facebook/react/uimanager/UIConstantsProviderManager;";
"Lcom/facebook/react/uimanager/UIConstantsProviderBinding;";

static facebook::jni::local_ref<jhybriddata> initHybrid(
facebook::jni::alias_ref<jhybridobject> /* unused */,
static void install(
facebook::jni::alias_ref<jclass> /* unused */,
facebook::jni::alias_ref<JRuntimeExecutor::javaobject> runtimeExecutor,
facebook::jni::alias_ref<DefaultEventTypesProvider::javaobject>
defaultExportableEventTypesProvider,
Expand All @@ -85,27 +82,6 @@ class UIConstantsProviderManager
constantsProvider);

static void registerNatives();

private:
friend HybridBase;
RuntimeExecutor runtimeExecutor_;

facebook::jni::global_ref<DefaultEventTypesProvider::javaobject>
defaultExportableEventTypesProvider_;
facebook::jni::global_ref<ConstantsForViewManagerProvider::javaobject>
constantsForViewManagerProvider_;
facebook::jni::global_ref<ConstantsProvider::javaobject> constantsProvider_;

void installJSIBindings();

explicit UIConstantsProviderManager(
RuntimeExecutor runtimeExecutor,
facebook::jni::alias_ref<DefaultEventTypesProvider::javaobject>
defaultExportableEventTypesProvider,
facebook::jni::alias_ref<ConstantsForViewManagerProvider::javaobject>
constantsForViewManagerProvider,
facebook::jni::alias_ref<ConstantsProvider::javaobject>
constantsProvider);
};

} // namespace facebook::react
Loading

0 comments on commit fefb6c0

Please sign in to comment.