Skip to content

Commit

Permalink
feat(connector): refactor invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
churchill-zhang committed Dec 1, 2022
1 parent 9146f9c commit 574356e
Show file tree
Hide file tree
Showing 23 changed files with 252 additions and 253 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ namespace hippy::devtools {

class DevtoolsJni {
public:
static void Init(JavaVM* j_vm, void* reserved, JNIEnv* j_env);
static void Init(JavaVM* j_vm, void* reserved);

static void Destroy(JavaVM* j_vm, void* reserved, JNIEnv* j_env);
static void Destroy(JavaVM* j_vm, void* reserved);
};

jint OnCreateDevtools(JNIEnv* j_env,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ REGISTER_JNI("com/tencent/devtools/vfs/DevtoolsProcessor", // NOLINT(cert-err58


// needs to call by JNI_OnLoad
void DevtoolsJni::Init(JavaVM* j_vm, void* reserved, JNIEnv* j_env) {}
void DevtoolsJni::Init(JavaVM* j_vm, void* reserved) {}

// needs to call by JNI_OnUnload
void DevtoolsJni::Destroy(JavaVM* j_vm, void* reserved, JNIEnv* j_env) {}
void DevtoolsJni::Destroy(JavaVM* j_vm, void* reserved) {}

constexpr uint32_t kPoolSize = 1;
std::make_shared<WorkerManager> worker_manager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#include "footstone/worker_manager.h"
#include "jni/data_holder.h"
#include "jni/jni_env.h"
#include "jni/jni_load.h"
#include "jni/jni_invocation.h"
#include "jni/jni_register.h"
#include "jni/jni_utils.h"
#include "jni/scoped_java_ref.h"
Expand Down Expand Up @@ -362,24 +362,26 @@ void SetDomManager(JNIEnv* j_env,
runtime->GetScope()->SetDomManager(dom_manager_object);
}

bool JsDriverOnLoad(JavaVM* j_vm, void* reserved, JNIEnv* j_env) {
static jint JNI_OnLoad(__unused JavaVM* j_vm, __unused void* reserved) {
auto j_env = JNIEnvironment::GetInstance()->AttachCurrentThread();
hippy::ExceptionHandler::Init(j_env);
hippy::ConvertUtils::Init(j_env);
hippy::JavaTurboModule::Init(j_env);
hippy::TurboModuleManager::Init(j_env);
hippy::InitBridge(j_env);
return true;
return JNI_VERSION_1_4;
}

void JsDriverOnUnLoad(JavaVM* j_vm, void* reserved, JNIEnv* j_env) {
static void JNI_OnUnload(__unused JavaVM* j_vm, __unused void* reserved) {
auto j_env = JNIEnvironment::GetInstance()->AttachCurrentThread();
hippy::napi::V8VM::PlatformDestroy();
hippy::ConvertUtils::Destroy(j_env);
hippy::JavaTurboModule::Destroy(j_env);
hippy::TurboModuleManager::Destroy(j_env);
}

REGISTER_JNI_ONLOAD(JsDriverOnLoad)
REGISTER_JNI_ONUNLOAD(JsDriverOnUnLoad)
REGISTER_JNI_ONLOAD(JNI_OnLoad)
REGISTER_JNI_ONUNLOAD(JNI_OnUnload)

}
}
Expand Down
5 changes: 0 additions & 5 deletions framework/android/connector/support/.gitignore

This file was deleted.

39 changes: 2 additions & 37 deletions framework/android/src/main/cpp/src/jni/entry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#include <string>
#include <unordered_map>

#include "connector/js_driver_jni.h"
#include "footstone/check.h"
#include "footstone/deserializer.h"
#include "footstone/hippy_value.h"
Expand All @@ -40,7 +39,7 @@
#include "footstone/worker_manager.h"
#include "jni/data_holder.h"
#include "jni/jni_env.h"
#include "jni/jni_load.h"
#include "jni/jni_invocation.h"
#include "jni/jni_register.h"
#include "jni/jni_utils.h"
#include "vfs/handler/asset_handler.h"
Expand Down Expand Up @@ -145,42 +144,8 @@ void OnDestroyVfs(__unused JNIEnv* j_env, __unused jobject j_object, jint j_id)
bool flag = hippy::global_data_holder.Erase(id);
FOOTSTONE_DCHECK(flag);
}

} // namespace bridge
} // namespace framework
} // namespace hippy

jint JNI_OnLoad(JavaVM* j_vm, __unused void* reserved) {
JNIEnv* j_env;
jint onLoad_err = -1;
if ((j_vm)->GetEnv(reinterpret_cast<void**>(&j_env), JNI_VERSION_1_4) != JNI_OK) {
return onLoad_err;
}
if (!j_env) {
return onLoad_err;
}

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

hippy::JNIEnvironment::GetInstance()->init(j_vm, j_env);
hippy::Uri::Init();
hippy::JniDelegateHandler::Init(j_env);
hippy::ResourceHolder::Init();

hippy::JniLoad::Instance()->Onload(j_vm, reserved, j_env);

return JNI_VERSION_1_4;
}

void JNI_OnUnload(__unused JavaVM* j_vm, __unused void* reserved) {
auto j_env = hippy::JNIEnvironment::GetInstance()->AttachCurrentThread();

hippy::JniLoad::Instance()->Onunload(j_vm, reserved, j_env);
hippy::JniDelegateHandler::Destroy();
hippy::ResourceHolder::Destroy();
hippy::Uri::Destroy();

hippy::JNIEnvironment::DestroyInstance();
}
2 changes: 1 addition & 1 deletion modules/android/jni/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ target_link_libraries(${PROJECT_NAME} PRIVATE footstone)
set(SOURCE_SET
src/data_holder.cc
src/jni_env.cc
src/jni_load.cc
src/jni_invocation.cc
src/jni_register.cc
src/jni_utils.cc
src/scoped_java_ref.cc)
Expand Down
2 changes: 1 addition & 1 deletion modules/android/jni/include/jni/jni_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class JNIEnvironment {
JNIEnvironment() = default;
~JNIEnvironment() = default;

void init(JavaVM* vm, JNIEnv* env);
jint JNI_OnLoad(JavaVM* vm, __unused void* reserved);
JNIEnv* AttachCurrentThread();

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,24 @@ inline namespace framework {
inline namespace jni {


class JniLoad {
class JniInvocation {
public:
using JniOnloadFunc = std::function<bool(JavaVM* j_vm, void* reserved, JNIEnv* j_env)>;
using JniOnunloadFunc = std::function<void(JavaVM* j_vm, void* reserved, JNIEnv* j_env)>;
using JniOnloadFunc = std::function<jint(JavaVM* j_vm, void* reserved)>;
using JniOnunloadFunc = std::function<void(JavaVM* j_vm, void* reserved)>;

JniLoad() = default;
JniInvocation() = default;

inline void PushOnload(JniOnloadFunc f) {
inline void PushJniOnLoad(JniOnloadFunc f) {
jni_onload_.emplace_back(f);
}
inline void PushOnunload(JniOnunloadFunc f) {
inline void PushJniOnUnload(JniOnunloadFunc f) {
jni_onunload_.emplace_back(f);
}

bool Onload(JavaVM* j_vm, void* reserved, JNIEnv* j_env);
void Onunload(JavaVM* j_vm, void* reserved, JNIEnv* j_env);
jint JNI_OnLoad(JavaVM* j_vm, void* reserved);
void JNI_OnUnload(JavaVM* j_vm, void* reserved);

static std::shared_ptr<JniLoad> Instance();
static std::shared_ptr<JniInvocation> Instance();

private:
std::vector<JniOnloadFunc> jni_onload_;
Expand All @@ -61,14 +61,17 @@ class JniLoad {
}
}

#define REGISTER_JNI_ONLOAD(FUNC_NAME) \
auto __REGISTER_JNI_ONLOAD_##FUNC_NAME = []() { \
JniLoad::Instance()->PushOnload(FUNC_NAME); \
return 0; \
jint JNI_OnLoad(JavaVM* j_vm, void* reserved);
void JNI_OnUnload(JavaVM* j_vm, void* reserved);

#define REGISTER_JNI_ONLOAD(FUNC_NAME) \
auto onload = []() { \
JniInvocation::Instance()->PushJniOnLoad(FUNC_NAME); \
return 0; \
}();

#define REGISTER_JNI_ONUNLOAD(FUNC_NAME) \
auto __REGISTER_JNI_ONUNLOAD_##FUNC_NAME = []() { \
JniLoad::Instance()->PushOnunload(FUNC_NAME); \
return 0; \
#define REGISTER_JNI_ONUNLOAD(FUNC_NAME) \
auto onunload = []() { \
JniInvocation::Instance()->PushJniOnUnload(FUNC_NAME); \
return 0; \
}();
4 changes: 3 additions & 1 deletion modules/android/jni/include/jni/jni_register.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class JNIRegisterData {
class JNIRegister {
public:
static std::unique_ptr<JNIRegister> &GetInstance();
static bool RegisterMethods(JNIEnv *j_env);

JNIRegister() = default;
JNIRegister(const JNIRegister &) = delete;
Expand Down Expand Up @@ -100,3 +99,6 @@ class JNIRegister {

#define REGISTER_STATIC_JNI(clazz, name, signature, pointer) \
REGISTER_JNI_TEMP(clazz, name, signature, pointer, true, __COUNTER__)

//#undef REGISTER_JNI_TEMP
//#undef REGISTER_JNI_INTERNAL
5 changes: 4 additions & 1 deletion modules/android/jni/src/jni_env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,15 @@ struct JNIEnvAutoRelease {
}
};

void JNIEnvironment::init(JavaVM* j_vm, JNIEnv* j_env) {
jint JNIEnvironment::JNI_OnLoad(JavaVM* j_vm, __unused void* reserved) {
j_vm_ = j_vm;

JNIEnv* j_env;
FOOTSTONE_CHECK((j_vm)->GetEnv(reinterpret_cast<void**>(&j_env), JNI_VERSION_1_4) == JNI_OK);
if (j_env->ExceptionCheck()) {
j_env->ExceptionClear();
}
return JNI_VERSION_1_4;
}

std::shared_ptr<JNIEnvironment> JNIEnvironment::GetInstance() {
Expand Down
77 changes: 77 additions & 0 deletions modules/android/jni/src/jni_invocation.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
*
* Tencent is pleased to support the open source community by making
* Hippy available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include "jni/jni_invocation.h"

#include <algorithm>
#include <mutex>

#include "footstone/logging.h"

#include "jni/jni_env.h"
#include "jni/jni_register.h"

namespace hippy {
inline namespace framework {
inline namespace jni {

std::shared_ptr<JniInvocation> JniInvocation::Instance() {
static std::shared_ptr<JniInvocation> instance = nullptr;
static std::once_flag flag;

std::call_once(flag, [] { instance = std::make_shared<JniInvocation>(); });

return instance;
}

jint JniInvocation::JNI_OnLoad(JavaVM* j_vm, void* reserved) {
jint version = hippy::JNIEnvironment::GetInstance()->JNI_OnLoad(j_vm, reserved);
for (const auto& func: jni_onload_) {
auto ret = func(j_vm, reserved);
if (ret != version){
FOOTSTONE_CHECK(false);
}
}
return version;
}

void JniInvocation::JNI_OnUnload(JavaVM* j_vm, void* reserved) {
for (const auto& func: jni_onunload_) {
func(j_vm, reserved);
}
hippy::JNIEnvironment::DestroyInstance();
}

}
}
}


jint JNI_OnLoad(JavaVM* j_vm, __unused void* reserved) {
hippy::JniInvocation::Instance()->JNI_OnLoad(j_vm, reserved);

return JNI_VERSION_1_4;
}

void JNI_OnUnload(__unused JavaVM* j_vm, __unused void* reserved) {
hippy::JniInvocation::Instance()->JNI_OnUnload(j_vm, reserved);
}
58 changes: 0 additions & 58 deletions modules/android/jni/src/jni_load.cc

This file was deleted.

Loading

0 comments on commit 574356e

Please sign in to comment.