Skip to content

Commit

Permalink
yass: add setProtectFd for open harmony
Browse files Browse the repository at this point in the history
Ref: #698.
  • Loading branch information
Chilledheart committed Jan 24, 2024
1 parent acd60e2 commit 0fdff1e
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 1 deletion.
26 changes: 25 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ if (IOS)
elseif(APPLE)
list(APPEND YASS_APP_FEATURES "mac ${CMAKE_OSX_DEPLOYMENT_TARGET}")
elseif(OHOS)
list(APPEND YASS_APP_FEATURES "ohos ${OHOS_APILEVEL}")
list(APPEND YASS_APP_FEATURES "harmony ${OHOS_APILEVEL}")
elseif(ANDROID)
list(APPEND YASS_APP_FEATURES "android ${ANDROID_API_VERSION}")
elseif(MSVC)
Expand Down Expand Up @@ -1897,6 +1897,14 @@ if (ANDROID AND GUI)
set(GUI_C_CXX_FLAGS "")
set(GUI_LIBRARY_DIRS "")
set(GUI_LIBRARIES "")
elseif (OHOS AND GUI)
set(GUI_FLAVOUR "harmony")
set(GUI_USE_FILE "")
set(GUI_INCLUDE_DIRS "")
set(GUI_DEFINITIONS "")
set(GUI_C_CXX_FLAGS "")
set(GUI_LIBRARY_DIRS "")
set(GUI_LIBRARIES "libace_napi.z.so")
elseif (WIN32 AND GUI)
message(STATUS "Compiling with GUI support: Windows")
set(GUI_FLAVOUR "windows")
Expand Down Expand Up @@ -3719,6 +3727,10 @@ if (GUI)
src/android/utils.hpp
src/android/utils.cpp
)
elseif (GUI_FLAVOUR STREQUAL "harmony")
list(APPEND SRC_FILES
src/harmony/yass.cpp
)
elseif (GUI_FLAVOUR STREQUAL "windows")
set(APP_MSVC_MANIFEST "${CMAKE_CURRENT_BINARY_DIR}/yass.manifest")
configure_file("src/win32/yass.manifest.in" "${APP_MSVC_MANIFEST}" @ONLY)
Expand Down Expand Up @@ -3871,6 +3883,15 @@ if (GUI)
target_link_libraries(${APP_NAME} PRIVATE tun2proxy)
set_target_properties(${APP_NAME} PROPERTIES
LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/android/export.lds -u Java_it_gui_yass_MainActivity_tun2ProxyInit -u Java_it_gui_yass_MainActivity_tun2ProxyRun -u Java_it_gui_yass_MainActivity_tun2ProxyDestroy")
elseif (GUI_FLAVOUR STREQUAL "harmony")
add_library(${APP_NAME} SHARED ${GUI_OPTIONS}
${SRC_FILES}
${GUI_USE_FILE}
)

# target_link_libraries(${APP_NAME} PRIVATE tun2proxy)
set_target_properties(${APP_NAME} PROPERTIES
LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/harmony/export.lds -u RegisterEntryModule")
else()
add_executable(${APP_NAME} WIN32 ${GUI_OPTIONS}
${SRC_FILES}
Expand All @@ -3888,6 +3909,9 @@ if (GUI)
if (GUI_FLAVOUR STREQUAL "android")
target_include_directories(${APP_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/android)
elseif (GUI_FLAVOUR STREQUAL "harmony")
target_include_directories(${APP_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/harmony)
elseif (GUI_FLAVOUR STREQUAL "windows")
target_include_directories(${APP_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/win32)
Expand Down
4 changes: 4 additions & 0 deletions src/harmony/export.lds
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
global: RegisterEntryModule;
local: *;
};
75 changes: 75 additions & 0 deletions src/harmony/yass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2024 Chilledheart */
#ifdef __OHOS__

#include "harmony/yass.hpp"

#include "core/logging.hpp"

#include <napi/native_api.h>
#include <js_native_api.h>
#include <js_native_api_types.h>

static napi_value setProtectFdCallbackThis = nullptr;
static napi_value setProtectFdCallbackFunc = nullptr;

static napi_value setProtectFdCallback(napi_env env, napi_callback_info info) {
napi_status status;

size_t argc = 1;
napi_value args[1] = {nullptr};
status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
if (status != napi_ok) {
LOG(WARNING) << "napi_get_cb_info: " << status;
return 0;
}

napi_value cb = args[0];
napi_valuetype type;
status = napi_typeof(env, cb, &type);
if (status != napi_ok) {
LOG(WARNING) << "napi_typeof: " << status;
return 0;
}

if (type != napi_function) {
LOG(WARNING) << "invalid type: " << type << " expected: napi_function";
return 0;
}

napi_value global;
status = napi_get_global(env, &global);
if (status != napi_ok) {
LOG(WARNING) << "napi_get_global: " << status;
return 0;
}

setProtectFdCallbackThis = global;
setProtectFdCallbackFunc = cb;

return 0;
}

static napi_value Init(napi_env env, napi_value exports) {
napi_property_descriptor desc[] = {
{"setProtectFdCallback", nullptr, setProtectFdCallback, nullptr, nullptr, nullptr, napi_default, nullptr},
};
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
return exports;
}

static napi_module demoModule = {
.nm_version = 1,
.nm_flags = 0,
.nm_filename = nullptr,
.nm_register_func = Init,
.nm_modname = "entry",
.nm_priv = ((void *)0),
.reserved = {0},
};

extern "C" __attribute__((constructor)) void RegisterEntryModule(void) {
napi_module_register(&demoModule);
}

#endif // __OHOS__
13 changes: 13 additions & 0 deletions src/harmony/yass.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2024 Chilledheart */
#ifndef _H_HARMONY_YASS_HPP
#define _H_HARMONY_YASS_HPP

#ifdef __OHOS__

__attribute__((visibility ("default")))
extern "C" void RegisterEntryModule(void);

#endif // __OHOS__

#endif // _H_HARMONY_YASS_HPP

0 comments on commit 0fdff1e

Please sign in to comment.