Use fbjni in JSLoader
Reviewed By: lexs Differential Revision: D4392412 fbshipit-source-id: a363b26d16cf9e39f9fefd1afd299fae210663c7
This commit is contained in:
parent
7cebe26681
commit
5cbb05c9cc
|
@ -101,26 +101,24 @@ CatalystInstanceImpl::CatalystInstanceImpl()
|
|||
|
||||
void CatalystInstanceImpl::registerNatives() {
|
||||
registerHybrid({
|
||||
makeNativeMethod("initHybrid", CatalystInstanceImpl::initHybrid),
|
||||
makeNativeMethod("initializeBridge", CatalystInstanceImpl::initializeBridge),
|
||||
makeNativeMethod("jniSetSourceURL", CatalystInstanceImpl::jniSetSourceURL),
|
||||
makeNativeMethod("jniLoadScriptFromAssets",
|
||||
"(Landroid/content/res/AssetManager;Ljava/lang/String;)V",
|
||||
CatalystInstanceImpl::jniLoadScriptFromAssets),
|
||||
makeNativeMethod("jniLoadScriptFromFile", CatalystInstanceImpl::jniLoadScriptFromFile),
|
||||
makeNativeMethod("jniLoadScriptFromOptimizedBundle",
|
||||
CatalystInstanceImpl::jniLoadScriptFromOptimizedBundle),
|
||||
makeNativeMethod("callJSFunction", CatalystInstanceImpl::callJSFunction),
|
||||
makeNativeMethod("callJSCallback", CatalystInstanceImpl::callJSCallback),
|
||||
makeNativeMethod("getMainExecutorToken", CatalystInstanceImpl::getMainExecutorToken),
|
||||
makeNativeMethod("setGlobalVariable", CatalystInstanceImpl::setGlobalVariable),
|
||||
makeNativeMethod("getJavaScriptContext", CatalystInstanceImpl::getJavaScriptContext),
|
||||
makeNativeMethod("handleMemoryPressureUiHidden", CatalystInstanceImpl::handleMemoryPressureUiHidden),
|
||||
makeNativeMethod("handleMemoryPressureModerate", CatalystInstanceImpl::handleMemoryPressureModerate),
|
||||
makeNativeMethod("handleMemoryPressureCritical", CatalystInstanceImpl::handleMemoryPressureCritical),
|
||||
makeNativeMethod("supportsProfiling", CatalystInstanceImpl::supportsProfiling),
|
||||
makeNativeMethod("startProfiler", CatalystInstanceImpl::startProfiler),
|
||||
makeNativeMethod("stopProfiler", CatalystInstanceImpl::stopProfiler),
|
||||
makeNativeMethod("initHybrid", CatalystInstanceImpl::initHybrid),
|
||||
makeNativeMethod("initializeBridge", CatalystInstanceImpl::initializeBridge),
|
||||
makeNativeMethod("jniSetSourceURL", CatalystInstanceImpl::jniSetSourceURL),
|
||||
makeNativeMethod("jniLoadScriptFromAssets", CatalystInstanceImpl::jniLoadScriptFromAssets),
|
||||
makeNativeMethod("jniLoadScriptFromFile", CatalystInstanceImpl::jniLoadScriptFromFile),
|
||||
makeNativeMethod("jniLoadScriptFromOptimizedBundle",
|
||||
CatalystInstanceImpl::jniLoadScriptFromOptimizedBundle),
|
||||
makeNativeMethod("callJSFunction", CatalystInstanceImpl::callJSFunction),
|
||||
makeNativeMethod("callJSCallback", CatalystInstanceImpl::callJSCallback),
|
||||
makeNativeMethod("getMainExecutorToken", CatalystInstanceImpl::getMainExecutorToken),
|
||||
makeNativeMethod("setGlobalVariable", CatalystInstanceImpl::setGlobalVariable),
|
||||
makeNativeMethod("getJavaScriptContext", CatalystInstanceImpl::getJavaScriptContext),
|
||||
makeNativeMethod("handleMemoryPressureUiHidden", CatalystInstanceImpl::handleMemoryPressureUiHidden),
|
||||
makeNativeMethod("handleMemoryPressureModerate", CatalystInstanceImpl::handleMemoryPressureModerate),
|
||||
makeNativeMethod("handleMemoryPressureCritical", CatalystInstanceImpl::handleMemoryPressureCritical),
|
||||
makeNativeMethod("supportsProfiling", CatalystInstanceImpl::supportsProfiling),
|
||||
makeNativeMethod("startProfiler", CatalystInstanceImpl::startProfiler),
|
||||
makeNativeMethod("stopProfiler", CatalystInstanceImpl::stopProfiler),
|
||||
});
|
||||
|
||||
JNativeRunnable::registerNatives();
|
||||
|
@ -163,8 +161,9 @@ void CatalystInstanceImpl::jniSetSourceURL(const std::string& sourceURL) {
|
|||
instance_->setSourceURL(sourceURL);
|
||||
}
|
||||
|
||||
void CatalystInstanceImpl::jniLoadScriptFromAssets(jobject assetManager,
|
||||
const std::string& assetURL) {
|
||||
void CatalystInstanceImpl::jniLoadScriptFromAssets(
|
||||
jni::alias_ref<JAssetManager::javaobject> assetManager,
|
||||
const std::string& assetURL) {
|
||||
const int kAssetsLength = 9; // strlen("assets://");
|
||||
auto sourceURL = assetURL.substr(kAssetsLength);
|
||||
|
||||
|
|
|
@ -2,12 +2,12 @@
|
|||
|
||||
#include <string>
|
||||
|
||||
#include <fb/fbjni.h>
|
||||
#include <folly/Memory.h>
|
||||
|
||||
#include <fb/fbjni.h>
|
||||
|
||||
#include "JMessageQueueThread.h"
|
||||
#include "JExecutorToken.h"
|
||||
#include "JMessageQueueThread.h"
|
||||
#include "JSLoader.h"
|
||||
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
@ -18,14 +18,12 @@ class ModuleRegistryHolder;
|
|||
class NativeArray;
|
||||
|
||||
struct ReactCallback : public jni::JavaClass<ReactCallback> {
|
||||
static constexpr auto kJavaDescriptor =
|
||||
"Lcom/facebook/react/cxxbridge/ReactCallback;";
|
||||
static constexpr auto kJavaDescriptor = "Lcom/facebook/react/cxxbridge/ReactCallback;";
|
||||
};
|
||||
|
||||
class CatalystInstanceImpl : public jni::HybridClass<CatalystInstanceImpl> {
|
||||
public:
|
||||
static constexpr auto kJavaDescriptor =
|
||||
"Lcom/facebook/react/cxxbridge/CatalystInstanceImpl;";
|
||||
static constexpr auto kJavaDescriptor = "Lcom/facebook/react/cxxbridge/CatalystInstanceImpl;";
|
||||
|
||||
static jni::local_ref<jhybriddata> initHybrid(jni::alias_ref<jclass>);
|
||||
|
||||
|
@ -55,7 +53,7 @@ class CatalystInstanceImpl : public jni::HybridClass<CatalystInstanceImpl> {
|
|||
*/
|
||||
void jniSetSourceURL(const std::string& sourceURL);
|
||||
|
||||
void jniLoadScriptFromAssets(jobject assetManager, const std::string& assetURL);
|
||||
void jniLoadScriptFromAssets(jni::alias_ref<JAssetManager::javaobject> assetManager, const std::string& assetURL);
|
||||
void jniLoadScriptFromFile(const std::string& fileName, const std::string& sourceURL);
|
||||
void jniLoadScriptFromOptimizedBundle(const std::string& bundlePath, const std::string& sourceURL, jint flags);
|
||||
void callJSFunction(JExecutorToken* token, std::string module, std::string method, NativeArray* arguments);
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
#include <folly/Memory.h>
|
||||
#include <android/asset_manager_jni.h>
|
||||
#include <fb/Environment.h>
|
||||
#include <fb/fbjni.h>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <streambuf>
|
||||
|
@ -15,26 +15,40 @@
|
|||
using fbsystrace::FbSystraceSection;
|
||||
#endif
|
||||
|
||||
using namespace facebook::jni;
|
||||
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
static jclass gApplicationHolderClass;
|
||||
static jmethodID gGetApplicationMethod;
|
||||
static jmethodID gGetAssetManagerMethod;
|
||||
struct JApplication : JavaClass<JApplication> {
|
||||
static constexpr auto kJavaDescriptor = "Landroid/app/Application;";
|
||||
|
||||
local_ref<JAssetManager::javaobject> getAssets() {
|
||||
static auto method = javaClassStatic()->getMethod<JAssetManager::javaobject()>("getAssets");
|
||||
return method(self());
|
||||
}
|
||||
};
|
||||
|
||||
struct JApplicationHolder : JavaClass<JApplicationHolder> {
|
||||
static constexpr auto kJavaDescriptor = "Lcom/facebook/react/common/ApplicationHolder;";
|
||||
|
||||
static local_ref<JApplication::javaobject> getApplication() {
|
||||
static auto method = javaClassStatic()
|
||||
->getStaticMethod<JApplication::javaobject()>("getApplication");
|
||||
return method(javaClassStatic());
|
||||
}
|
||||
};
|
||||
|
||||
std::unique_ptr<const JSBigString> loadScriptFromAssets(const std::string& assetName) {
|
||||
JNIEnv *env = jni::Environment::current();
|
||||
jobject application = env->CallStaticObjectMethod(
|
||||
gApplicationHolderClass,
|
||||
gGetApplicationMethod);
|
||||
jobject assetManager = env->CallObjectMethod(application, gGetAssetManagerMethod);
|
||||
return loadScriptFromAssets(AAssetManager_fromJava(env, assetManager), assetName);
|
||||
auto env = Environment::current();
|
||||
auto assetManager = JApplicationHolder::getApplication()->getAssets();
|
||||
return loadScriptFromAssets(AAssetManager_fromJava(env, assetManager.get()), assetName);
|
||||
}
|
||||
|
||||
__attribute__((visibility("default")))
|
||||
AAssetManager *extractAssetManager(jobject jassetManager) {
|
||||
auto env = jni::Environment::current();
|
||||
return AAssetManager_fromJava(env, jassetManager);
|
||||
AAssetManager *extractAssetManager(alias_ref<JAssetManager::javaobject> assetManager) {
|
||||
auto env = Environment::current();
|
||||
return AAssetManager_fromJava(env, assetManager.get());
|
||||
}
|
||||
|
||||
__attribute__((visibility("default")))
|
||||
|
@ -88,14 +102,4 @@ std::string loadScriptFromFile(const std::string& fileName) {
|
|||
return "";
|
||||
}
|
||||
|
||||
void registerJSLoaderNatives() {
|
||||
JNIEnv *env = jni::Environment::current();
|
||||
jclass applicationHolderClass = env->FindClass("com/facebook/react/common/ApplicationHolder");
|
||||
gApplicationHolderClass = (jclass)env->NewGlobalRef(applicationHolderClass);
|
||||
gGetApplicationMethod = env->GetStaticMethodID(applicationHolderClass, "getApplication", "()Landroid/app/Application;");
|
||||
|
||||
jclass appClass = env->FindClass("android/app/Application");
|
||||
gGetAssetManagerMethod = env->GetMethodID(appClass, "getAssets", "()Landroid/content/res/AssetManager;");
|
||||
}
|
||||
|
||||
} }
|
||||
|
|
|
@ -2,14 +2,19 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <cxxreact/Executor.h>
|
||||
#include <android/asset_manager.h>
|
||||
#include <string>
|
||||
#include <jni.h>
|
||||
|
||||
#include <android/asset_manager.h>
|
||||
#include <cxxreact/Executor.h>
|
||||
#include <fb/fbjni.h>
|
||||
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
struct JAssetManager : jni::JavaClass<JAssetManager> {
|
||||
static constexpr auto kJavaDescriptor = "Landroid/content/res/AssetManager;";
|
||||
};
|
||||
|
||||
/**
|
||||
* Helper method for loading a JS script from Android assets without
|
||||
* a reference to an AssetManager.
|
||||
|
@ -19,7 +24,7 @@ std::unique_ptr<const JSBigString> loadScriptFromAssets(const std::string& asset
|
|||
/**
|
||||
* Helper method for loading JS script from android asset
|
||||
*/
|
||||
AAssetManager *extractAssetManager(jobject jassetManager);
|
||||
AAssetManager *extractAssetManager(jni::alias_ref<JAssetManager::javaobject> assetManager);
|
||||
|
||||
std::unique_ptr<const JSBigString> loadScriptFromAssets(AAssetManager *assetManager, const std::string& assetName);
|
||||
|
||||
|
@ -28,6 +33,4 @@ std::unique_ptr<const JSBigString> loadScriptFromAssets(AAssetManager *assetMana
|
|||
*/
|
||||
std::string loadScriptFromFile(const std::string& fileName);
|
||||
|
||||
void registerJSLoaderNatives();
|
||||
|
||||
} }
|
||||
|
|
|
@ -166,7 +166,6 @@ extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
|
|||
#ifdef WITH_INSPECTOR
|
||||
JInspector::registerNatives();
|
||||
#endif
|
||||
registerJSLoaderNatives();
|
||||
|
||||
NativeArray::registerNatives();
|
||||
ReadableNativeArray::registerNatives();
|
||||
|
|
Loading…
Reference in New Issue