Skip to content

Commit

Permalink
feat(framework): adjust the jni module namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
churchill-zhang committed Aug 16, 2022
1 parent 3f8a85d commit 2a91e94
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 11 deletions.
6 changes: 3 additions & 3 deletions framework/android/src/main/cpp/src/bridge/entry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -546,12 +546,12 @@ jint JNI_OnLoad(JavaVM* j_vm, __unused void* reserved) {
return onLoad_err;
}

bool ret = JNIRegister::RegisterMethods(j_env);
bool ret = hippy::JNIRegister::RegisterMethods(j_env);
if (!ret) {
return onLoad_err;
}

JNIEnvironment::GetInstance()->init(j_vm, j_env);
hippy::JNIEnvironment::GetInstance()->init(j_vm, j_env);

hippy::Uri::Init();
hippy::ConvertUtils::Init();
Expand All @@ -576,5 +576,5 @@ void JNI_OnUnload(__unused JavaVM* j_vm, __unused void* reserved) {
#ifdef ANDROID_NATIVE_RENDER
NativeRenderJni::Destroy();
#endif
JNIEnvironment::DestroyInstance();
hippy::JNIEnvironment::DestroyInstance();
}
8 changes: 8 additions & 0 deletions modules/android/jni/include/jni/jni_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
#include <memory>
#include <mutex>

namespace hippy {
inline namespace framework {
inline namespace jni {

class JNIEnvironment {
public:
struct JNIWrapper {
Expand Down Expand Up @@ -56,3 +60,7 @@ class JNIEnvironment {
JavaVM* j_vm_;
JNIWrapper wrapper_;
};

}
}
}
10 changes: 9 additions & 1 deletion modules/android/jni/include/jni/jni_register.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
#include <unordered_map>
#include <vector>

namespace hippy {
inline namespace framework {
inline namespace jni {

class JNIRegisterData {
public:
JNIRegisterData(const char *name,
Expand Down Expand Up @@ -76,9 +80,13 @@ class JNIRegister {
std::unordered_map<std::string, std::vector<JNIRegisterData>> jni_modules_;
};

}
}
}

#define REGISTER_JNI_INTERNAL(clazz, name, signature, pointer, is_static, key) \
auto __REGISTER_JNI_##key = []() { \
JNIRegister::GetInstance()->RegisterJNIModule( \
hippy::JNIRegister::GetInstance()->RegisterJNIModule( \
clazz, name, signature, reinterpret_cast<void *>(pointer), is_static); \
return 0; \
}();
Expand Down
9 changes: 9 additions & 0 deletions modules/android/jni/include/jni/jni_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@

#include <jni.h>
#include <string>

#include "footstone/unicode_string_view.h"

namespace hippy {
inline namespace framework {
inline namespace jni {

class JniUtils {
using unicode_string_view = footstone::stringview::unicode_string_view;
using byte_string = std::string;
Expand All @@ -51,3 +56,7 @@ class JniUtils {

static unicode_string_view ToStrView(JNIEnv* j_env, jstring j_str);
};

}
}
}
8 changes: 8 additions & 0 deletions modules/android/jni/include/jni/scoped_java_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@

#include <jni.h>

namespace hippy {
inline namespace framework {
inline namespace jni {

class JavaRef {
public:
JavaRef(JNIEnv* env, jobject obj);
Expand All @@ -36,3 +40,7 @@ class JavaRef {
private:
jobject obj_;
};

}
}
}
16 changes: 12 additions & 4 deletions modules/android/jni/src/jni_env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@

#include "footstone/logging.h"

namespace hippy {
inline namespace framework {
inline namespace jni {

std::shared_ptr<JNIEnvironment> JNIEnvironment::instance_ = nullptr;
std::mutex JNIEnvironment::mutex_;

Expand All @@ -43,8 +47,7 @@ struct JNIEnvAutoRelease {
void JNIEnvironment::init(JavaVM* j_vm, JNIEnv* j_env) {
j_vm_ = j_vm;

jclass j_hippy_bridge_cls =
j_env->FindClass("com/tencent/mtt/hippy/bridge/HippyBridgeImpl");
jclass j_hippy_bridge_cls = j_env->FindClass("com/tencent/mtt/hippy/bridge/HippyBridgeImpl");
wrapper_.j_call_natives_direct_method_id =
j_env->GetMethodID(j_hippy_bridge_cls, "callNatives",
"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/"
Expand All @@ -58,8 +61,8 @@ void JNIEnvironment::init(JavaVM* j_vm, JNIEnv* j_env) {
wrapper_.j_inspector_channel_method_id =
j_env->GetMethodID(j_hippy_bridge_cls, "InspectorChannel", "([B)V");

wrapper_.j_fetch_resource_method_id = j_env->GetMethodID(
j_hippy_bridge_cls, "fetchResourceWithUri", "(Ljava/lang/String;J)V");
wrapper_.j_fetch_resource_method_id = j_env->GetMethodID(j_hippy_bridge_cls, "fetchResourceWithUri",
"(Ljava/lang/String;J)V");
j_env->DeleteLocalRef(j_hippy_bridge_cls);

if (j_env->ExceptionCheck()) {
Expand Down Expand Up @@ -121,3 +124,8 @@ JNIEnv* JNIEnvironment::AttachCurrentThread() {

return j_env;
}

}
}
}

8 changes: 8 additions & 0 deletions modules/android/jni/src/jni_register.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
#include "footstone/logging.h"
#include "include/jni/jni_env.h"

namespace hippy {
inline namespace framework {
inline namespace jni {

std::unique_ptr<JNIRegister>& JNIRegister::GetInstance() {
static std::unique_ptr<JNIRegister> instance = nullptr;
static std::once_flag flag;
Expand Down Expand Up @@ -91,3 +95,7 @@ JNIRegisterData::JNIRegisterData(const char* name,
JNINativeMethod JNIRegisterData::ToJNINativeMethod() {
return {name_.c_str(), sign_.c_str(), pointer_};
}

}
}
}
8 changes: 8 additions & 0 deletions modules/android/jni/src/jni_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
#include "footstone/logging.h"
#include "footstone/string_view_utils.h"

namespace hippy {
inline namespace framework {
inline namespace jni {

using unicode_string_view = footstone::stringview::unicode_string_view;
using StringViewUtils = footstone::stringview::StringViewUtils;

Expand Down Expand Up @@ -119,3 +123,7 @@ unicode_string_view JniUtils::ToStrView(JNIEnv* j_env, jstring j_str) {
j_env->ReleaseStringChars(j_str, j_char);
return ret;
}

}
}
}
10 changes: 8 additions & 2 deletions modules/android/jni/src/scoped_java_ref.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@
#include "footstone/logging.h"
#include "include/jni/jni_env.h"

namespace hippy {
inline namespace framework {
inline namespace jni {

JavaRef::JavaRef(JNIEnv* j_env, jobject j_obj) : obj_(nullptr) {
// FOOTSTONE_DLOG(INFO) << "JavaRef create";
if (!j_env) {
j_env = JNIEnvironment::GetInstance()->AttachCurrentThread();
} else {
Expand All @@ -39,8 +42,11 @@ JavaRef::JavaRef(JNIEnv* j_env, jobject j_obj) : obj_(nullptr) {
}

JavaRef::~JavaRef() {
// FOOTSTONE_DLOG(INFO) << "~JavaRef release";
if (obj_) {
JNIEnvironment::GetInstance()->AttachCurrentThread()->DeleteGlobalRef(obj_);
}
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ void NativeRenderJni::Destroy() {
}

jint OnCreateNativeRenderProvider(JNIEnv* j_env, jobject j_object, jfloat j_density) {
std::shared_ptr<RenderManager> render_manager = std::make_shared<NativeRenderManager>(std::make_shared<JavaRef>(j_env, j_object));
std::shared_ptr<RenderManager> render_manager = std::make_shared<NativeRenderManager>(
std::make_shared<hippy::JavaRef>(j_env, j_object));
auto native_render_manager = std::static_pointer_cast<NativeRenderManager>(render_manager);
auto density = static_cast<float>(j_density);
native_render_manager->SetDensity(density);
Expand Down

0 comments on commit 2a91e94

Please sign in to comment.