Introduce ComponentNameResolverManager and ComponentNameResolver classes

Summary:
This diff introduces the ComponentNameResolverManager and ComponentNameResolver classes. The purpose of these classes is to integrate NativeComponentRegistryBinding into RN Android

changelog: [internal] internal

Reviewed By: JoshuaGross

Differential Revision: D26716899

fbshipit-source-id: c62fb5c38ddce5325890d2506a6fb17d26043175
This commit is contained in:
David Vacca
2021-03-03 14:16:02 -08:00
committed by Facebook GitHub Bot
parent 50621078b5
commit 2f5f6c40c2
5 changed files with 188 additions and 0 deletions
@@ -0,0 +1,17 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.fabric;
import com.facebook.proguard.annotations.DoNotStrip;
@DoNotStrip
public interface ComponentNameResolver {
/* returns a list of all the component names that are registered in React Native. */
String[] getComponentNames();
}
@@ -0,0 +1,34 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.fabric;
import com.facebook.jni.HybridData;
import com.facebook.proguard.annotations.DoNotStrip;
import com.facebook.react.bridge.RuntimeExecutor;
public class ComponentNameResolverManager {
static {
FabricSoLoader.staticInit();
}
@DoNotStrip
@SuppressWarnings("unused")
private final HybridData mHybridData;
public ComponentNameResolverManager(
RuntimeExecutor runtimeExecutor, Object componentNameResolver) {
mHybridData = initHybrid(runtimeExecutor, componentNameResolver);
installJSIBindings();
}
private native HybridData initHybrid(
RuntimeExecutor runtimeExecutor, Object componentNameResolver);
private native void installJSIBindings();
}
@@ -0,0 +1,76 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <memory>
#include <string>
#include <fbjni/fbjni.h>
#include <jsi/jsi.h>
#include "ComponentNameResolverManager.h"
#include <react/renderer/componentregistry/NativeComponentRegistryBinding.h>
namespace facebook {
namespace react {
ComponentNameResolverManager::ComponentNameResolverManager(
jni::alias_ref<ComponentNameResolverManager::javaobject> jThis,
RuntimeExecutor runtimeExecutor,
jni::alias_ref<jobject> componentNameResolver)
: javaPart_(jni::make_global(jThis)),
runtimeExecutor_(runtimeExecutor),
componentNameResolver_(jni::make_global(componentNameResolver)) {}
jni::local_ref<ComponentNameResolverManager::jhybriddata>
ComponentNameResolverManager::initHybrid(
jni::alias_ref<jhybridobject> jThis,
jni::alias_ref<JRuntimeExecutor::javaobject> runtimeExecutor,
jni::alias_ref<jobject> componentNameResolver) {
return makeCxxInstance(
jThis, runtimeExecutor->cthis()->get(), componentNameResolver);
}
void ComponentNameResolverManager::registerNatives() {
registerHybrid({
makeNativeMethod("initHybrid", ComponentNameResolverManager::initHybrid),
makeNativeMethod(
"installJSIBindings",
ComponentNameResolverManager::installJSIBindings),
});
}
void ComponentNameResolverManager::installJSIBindings() {
runtimeExecutor_([thizz = this](jsi::Runtime &runtime) {
auto viewManagerProvider = [thizz](const std::string &name) -> bool {
if (thizz->componentNames_.size() == 0) {
static auto getComponentNames =
jni::findClassStatic(ComponentNameResolverManager::
ComponentNameResolverJavaDescriptor)
->getMethod<jni::alias_ref<jtypeArray<jstring>>()>(
"getComponentNames");
auto componentNamesJArray =
getComponentNames(thizz->componentNameResolver_.get());
auto len = componentNamesJArray->size();
for (size_t i = 0; i < len; i++) {
jni::local_ref<jstring> elem = (*componentNamesJArray)[i];
auto componentName = elem->toStdString();
thizz->componentNames_.insert(componentName);
}
}
return thizz->componentNames_.find(name) != thizz->componentNames_.end();
};
react::NativeComponentRegistryBinding::install(
runtime, std::move(viewManagerProvider));
});
}
} // namespace react
} // namespace facebook
@@ -0,0 +1,59 @@
/*
* Copyright (c) Facebook, Inc. and its 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 <ReactCommon/CallInvokerHolder.h>
#include <ReactCommon/RuntimeExecutor.h>
#include <fbjni/fbjni.h>
#include <jsi/jsi.h>
#include <react/jni/JRuntimeExecutor.h>
#include <memory>
#include <set>
#include <unordered_map>
using namespace facebook::jni;
using namespace facebook::jsi;
namespace facebook {
namespace react {
class ComponentNameResolverManager
: public jni::HybridClass<ComponentNameResolverManager> {
public:
static auto constexpr kJavaDescriptor =
"Lcom/facebook/react/fabric/ComponentNameResolverManager;";
constexpr static auto ComponentNameResolverJavaDescriptor =
"com/facebook/react/fabric/ComponentNameResolver";
static jni::local_ref<jhybriddata> initHybrid(
jni::alias_ref<jhybridobject> jThis,
jni::alias_ref<JRuntimeExecutor::javaobject> runtimeExecutor,
jni::alias_ref<jobject> componentNameResolver);
static void registerNatives();
private:
friend HybridBase;
jni::global_ref<ComponentNameResolverManager::javaobject> javaPart_;
RuntimeExecutor runtimeExecutor_;
jni::global_ref<jobject> componentNameResolver_;
std::set<std::string> componentNames_;
void installJSIBindings();
explicit ComponentNameResolverManager(
jni::alias_ref<ComponentNameResolverManager::jhybridobject> jThis,
RuntimeExecutor runtimeExecutor,
jni::alias_ref<jobject> componentNameResolver);
};
} // namespace react
} // namespace facebook
@@ -9,6 +9,7 @@
#include "Binding.h"
#include "ComponentFactory.h"
#include "ComponentNameResolverManager.h"
#include "CoreComponentsRegistry.h"
#include "EventBeatManager.h"
#include "EventEmitterWrapper.h"
@@ -21,6 +22,7 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *) {
facebook::react::EventEmitterWrapper::registerNatives();
facebook::react::StateWrapperImpl::registerNatives();
facebook::react::ComponentFactory::registerNatives();
facebook::react::ComponentNameResolverManager::registerNatives();
facebook::react::CoreComponentsRegistry::registerNatives();
});
}