From e0e0daf66fdc3ea9295c8b10ee2ecd624d149ed6 Mon Sep 17 00:00:00 2001 From: Keeyou Date: Wed, 31 Jan 2024 16:45:35 +0800 Subject: [PATCH] harmony: code cleanup Fixes: #698. --- .../main/ets/constants/CommonConstants.ets | 3 + .../src/main/ets/entryability/EntryAbility.ts | 3 + .../entryabilitystage/EntryAbilityStage.ts | 3 + .../entry/src/main/ets/pages/DetailPage.ets | 4 +- .../src/main/ets/view/DetailListComponent.ets | 3 + .../src/main/ets/viewmodel/YassDataItem.ets | 3 + .../src/main/ets/viewmodel/YassViewModel.ets | 3 + src/harmony/yass.cpp | 181 ++++++++---------- 8 files changed, 104 insertions(+), 99 deletions(-) diff --git a/harmony/entry/src/main/ets/constants/CommonConstants.ets b/harmony/entry/src/main/ets/constants/CommonConstants.ets index 0581cfd1c..7ddb672fa 100644 --- a/harmony/entry/src/main/ets/constants/CommonConstants.ets +++ b/harmony/entry/src/main/ets/constants/CommonConstants.ets @@ -1,3 +1,6 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Chilledheart */ + /** * Common constants for all features. */ diff --git a/harmony/entry/src/main/ets/entryability/EntryAbility.ts b/harmony/entry/src/main/ets/entryability/EntryAbility.ts index 5e5ba9653..873b67642 100644 --- a/harmony/entry/src/main/ets/entryability/EntryAbility.ts +++ b/harmony/entry/src/main/ets/entryability/EntryAbility.ts @@ -1,3 +1,6 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Chilledheart */ + import AbilityConstant from '@ohos.app.ability.AbilityConstant'; import hilog from '@ohos.hilog'; import UIAbility from '@ohos.app.ability.UIAbility'; diff --git a/harmony/entry/src/main/ets/entryabilitystage/EntryAbilityStage.ts b/harmony/entry/src/main/ets/entryabilitystage/EntryAbilityStage.ts index e94342f46..a4e2ab177 100644 --- a/harmony/entry/src/main/ets/entryabilitystage/EntryAbilityStage.ts +++ b/harmony/entry/src/main/ets/entryabilitystage/EntryAbilityStage.ts @@ -1,3 +1,6 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Chilledheart */ + import AbilityStage from '@ohos.app.ability.AbilityStage'; export default class EntryAbilityStage extends AbilityStage { diff --git a/harmony/entry/src/main/ets/pages/DetailPage.ets b/harmony/entry/src/main/ets/pages/DetailPage.ets index 78fbbd822..9dcee5d86 100644 --- a/harmony/entry/src/main/ets/pages/DetailPage.ets +++ b/harmony/entry/src/main/ets/pages/DetailPage.ets @@ -1,3 +1,6 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Chilledheart */ + import router from '@ohos.router'; import { DetailListComponent } from '../view/DetailListComponent'; import { CommonConstants } from '../constants/CommonConstants'; @@ -167,7 +170,6 @@ struct DetailPage { onUpdateTransferRate() { let rates = yass.getTransferRate(); this.status = $r('app.string.status_started_with_rate', rates[0], rates[1]); - hilog.warn(0x0000, 'yass', '%{public}s', 'on refresh: ' + this.status); this.timer_id = setTimeout(() => { this.onUpdateTransferRate(); }, 1000); diff --git a/harmony/entry/src/main/ets/view/DetailListComponent.ets b/harmony/entry/src/main/ets/view/DetailListComponent.ets index e1f57bd46..cbb3d0480 100644 --- a/harmony/entry/src/main/ets/view/DetailListComponent.ets +++ b/harmony/entry/src/main/ets/view/DetailListComponent.ets @@ -1,3 +1,6 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Chilledheart */ + import { CommonConstants } from '../constants/CommonConstants'; import { YassDataItem } from '../viewmodel/YassDataItem'; import yass from 'libyass.so'; diff --git a/harmony/entry/src/main/ets/viewmodel/YassDataItem.ets b/harmony/entry/src/main/ets/viewmodel/YassDataItem.ets index cd9ba1edb..5c57bda51 100644 --- a/harmony/entry/src/main/ets/viewmodel/YassDataItem.ets +++ b/harmony/entry/src/main/ets/viewmodel/YassDataItem.ets @@ -1,3 +1,6 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Chilledheart */ + /** * Basic list item data bean. */ diff --git a/harmony/entry/src/main/ets/viewmodel/YassViewModel.ets b/harmony/entry/src/main/ets/viewmodel/YassViewModel.ets index 7e8c59c6d..276ea2a20 100644 --- a/harmony/entry/src/main/ets/viewmodel/YassViewModel.ets +++ b/harmony/entry/src/main/ets/viewmodel/YassViewModel.ets @@ -1,3 +1,6 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Chilledheart */ + import { YassDataItem } from './YassDataItem'; import yass from 'libyass.so'; diff --git a/src/harmony/yass.cpp b/src/harmony/yass.cpp index ea23ae693..46d3660c1 100644 --- a/src/harmony/yass.cpp +++ b/src/harmony/yass.cpp @@ -33,18 +33,11 @@ extern "C" bool OH_LOG_IsLoggable(unsigned int domain, const char *tag, HILOG_LogLevel level); static constexpr char kLogTag[] = YASS_APP_NAME; -static constexpr unsigned int kLogDomain = 0x15b0; +static constexpr unsigned int kLogDomain = 0x0; -static napi_env setProtectFdCallbackEnv = nullptr; static napi_threadsafe_function setProtectFdCallbackFunc = nullptr; struct AsyncProtectFdEx_t { - napi_async_work work_item; - int fd; - int write_end; -}; - -struct SetProtectFdEx_t { int fd; int write_end; }; @@ -58,97 +51,35 @@ static void setProtectFdWriteResult(int fd, napi_status status) { } } -static void setProtectFdExecuteWork(napi_env env, void *data) { - AsyncProtectFdEx_t *async_protect_fd_ex = (AsyncProtectFdEx_t *)data; - - auto ctx = new SetProtectFdEx_t; - if (ctx == nullptr) { - LOG(WARNING) << "failed to allocate new ctx"; - setProtectFdWriteResult(async_protect_fd_ex->fd, napi_queue_full); - return; - } +int setProtectFd(int fd) { + int pipefd[2] = {-1, -1}; + if (pipe(pipefd) == -1) { + PLOG(WARNING) << "create pipe failed"; + return -1; + }; + int read_end = pipefd[0]; - ctx->fd = async_protect_fd_ex->fd; - ctx->write_end = async_protect_fd_ex->write_end; + auto ctx = std::make_unique(); + ctx->fd = fd; + ctx->write_end = pipefd[1]; auto status = napi_acquire_threadsafe_function(setProtectFdCallbackFunc); if (status != napi_ok) { LOG(WARNING) << "napi_acquire_threadsafe_function: " << status; - setProtectFdWriteResult(async_protect_fd_ex->fd, status); - return; + return -1; } - status = napi_call_threadsafe_function(setProtectFdCallbackFunc, ctx, napi_tsfn_blocking); + auto ctx_raw = ctx.release(); + status = napi_call_threadsafe_function(setProtectFdCallbackFunc, ctx_raw, napi_tsfn_blocking); if (status != napi_ok) { LOG(WARNING) << "napi_call_threadsafe_function: " << status; - setProtectFdWriteResult(async_protect_fd_ex->fd, status); - return; + delete ctx_raw; + return -1; } status = napi_release_threadsafe_function(setProtectFdCallbackFunc, napi_tsfn_release); - if (status != napi_ok) { LOG(WARNING) << "napi_release_threadsafe_function: " << status; - setProtectFdWriteResult(async_protect_fd_ex->fd, status); - return; - } -} - -// This function runs on the main thread after `ExecuteWork` exited. -static void setProtectFdOnWorkComplete(napi_env env, napi_status status, void *data) { - AsyncProtectFdEx_t *async_protect_fd_ex = (AsyncProtectFdEx_t *)data; - - status = napi_delete_async_work(env, async_protect_fd_ex->work_item); - if (status != napi_ok) { - LOG(WARNING) << "napi_delete_async_work: " << status; - return; - } - - delete async_protect_fd_ex; -} - -int setProtectFd(int fd) { - napi_env env = setProtectFdCallbackEnv; - if (env == nullptr) { - LOG(WARNING) << "null env"; - return -1; - } - - napi_value work_name; - // Specify a name to describe this asynchronous operation. - auto status = napi_create_string_utf8(env, kAsyncResourceName, NAPI_AUTO_LENGTH, &work_name); - if (status != napi_ok) { - LOG(WARNING) << "napi_create_string_utf8: " << status; - return -1; - } - AsyncProtectFdEx_t *async_protect_fd_ex = new AsyncProtectFdEx_t; - napi_async_work work_item; - - status = napi_create_async_work(env, nullptr, work_name, - setProtectFdExecuteWork, setProtectFdOnWorkComplete, async_protect_fd_ex, - &work_item); // OUT: THE handle to the async work item - if (status != napi_ok) { - delete async_protect_fd_ex; - LOG(WARNING) << "napi_create_async_work: " << status; - return -1; - } - - int pipefd[2] = {-1, -1}; - if (pipe(pipefd) == -1) { - PLOG(WARNING) << "create pipe failed"; - return -1; - }; - int read_end = pipefd[0]; - async_protect_fd_ex->work_item = work_item; - async_protect_fd_ex->fd = fd; - async_protect_fd_ex->write_end = pipefd[1]; - - // Queue the work item for execution. - status = napi_queue_async_work(env, work_item); - if (status != napi_ok) { - IGNORE_EINTR(close(pipefd[0])); - IGNORE_EINTR(close(pipefd[1])); - LOG(WARNING) << "napi_create_async_work: " << status; return -1; } @@ -179,24 +110,45 @@ static napi_value setProtectFdCallingJSCallback(napi_env env, napi_callback_info return nullptr; } -static void setProtectFdCallingJS(napi_env env, napi_value js_cb, void *context, void *data) { - // This parameter is not used. - (void)context; - - auto ctx = reinterpret_cast(data); +static void setProtectFdCallingJS(napi_env env, napi_value /*js_cb*/, void *context, void *data) { + std::unique_ptr ctx(reinterpret_cast(data)); int fd_value = ctx->fd; int write_end = ctx->write_end; - delete ctx; + + auto cb_ref = reinterpret_cast(context); + + napi_value cb; + auto status = napi_get_reference_value(env, cb_ref, &cb); + if (status != napi_ok) { + LOG(WARNING) << "napi_get_reference_value: " << status; + setProtectFdWriteResult(fd_value, status); + return; + } + + napi_valuetype type; + status = napi_typeof(env, cb, &type); + if (status != napi_ok) { + LOG(WARNING) << "napi_typeof failed: " << status; + setProtectFdWriteResult(fd_value, status); + return; + } + + if (type != napi_function) { + LOG(WARNING) << "napi_typeof unexpected: " << type; + setProtectFdWriteResult(fd_value, status); + return; + } if (env == nullptr) { LOG(WARNING) << "null env"; + setProtectFdWriteResult(fd_value, status); return; } - napi_value undefined; - napi_status status = napi_get_undefined(env, &undefined); + napi_value global; + status = napi_get_global(env, &global); if (status != napi_ok) { - LOG(WARNING) << "napi_get_undefined: " << status; + LOG(WARNING) << "napi_get_global: " << status; setProtectFdWriteResult(fd_value, status); return; } @@ -218,12 +170,21 @@ static void setProtectFdCallingJS(napi_env env, napi_value js_cb, void *context, } napi_value argv[] = { fd, callback }; - status = napi_call_function(env, undefined, js_cb, std::size(argv), argv, nullptr); + status = napi_call_function(env, global, cb, std::size(argv), argv, nullptr); if (status != napi_ok) { LOG(WARNING) << "napi_call_function: " << status; setProtectFdWriteResult(fd_value, status); return; } + + status = napi_delete_reference(env, cb_ref); + if (status != napi_ok) { + LOG(WARNING) << "napi_delete_reference: " << status; + setProtectFdWriteResult(fd_value, status); + return; + } + + setProtectFdWriteResult(fd_value, status); } static napi_value setProtectFdCallback(napi_env env, napi_callback_info info) { @@ -250,6 +211,13 @@ static napi_value setProtectFdCallback(napi_env env, napi_callback_info info) { return nullptr; } + napi_ref cb_ref; + status = napi_create_reference(env, cb, 1, &cb_ref); + if (status != napi_ok) { + napi_throw_error(env, nullptr, "napi_create_reference failed"); + return nullptr; + } + napi_value work_name; // Specify a name to describe this asynchronous operation. status = napi_create_string_utf8(env, kAsyncResourceName, NAPI_AUTO_LENGTH, &work_name); @@ -260,7 +228,7 @@ static napi_value setProtectFdCallback(napi_env env, napi_callback_info info) { // Create a thread-safe N-API callback function correspond to the C/C++ callback function status = napi_create_threadsafe_function(env, cb, nullptr, work_name, 0, 1, nullptr, - nullptr, nullptr, setProtectFdCallingJS, // the C/C++ callback function + nullptr, cb_ref, setProtectFdCallingJS, // the C/C++ callback function &setProtectFdCallbackFunc // out: the asynchronous thread-safe JavaScript function ); if (status != napi_ok) { @@ -268,8 +236,6 @@ static napi_value setProtectFdCallback(napi_env env, napi_callback_info info) { return nullptr; } - setProtectFdCallbackEnv = env; - return nullptr; } @@ -585,6 +551,12 @@ static napi_value startWorker(napi_env env, napi_callback_info info) { if (status != napi_ok) { LOG(WARNING) << "napi_release_threadsafe_function: " << status; } + + // release callback + status = napi_release_threadsafe_function(startWorkerCallbackFunc, napi_tsfn_release); + if (status != napi_ok) { + LOG(WARNING) << "napi_release_threadsafe_function: " << status; + } }); return nullptr; } @@ -705,6 +677,12 @@ static napi_value stopWorker(napi_env env, napi_callback_info info) { if (status != napi_ok) { LOG(WARNING) << "napi_release_threadsafe_function: " << status; } + + // release callback + status = napi_release_threadsafe_function(stopWorkerCallbackFunc, napi_tsfn_release); + if (status != napi_ok) { + LOG(WARNING) << "napi_release_threadsafe_function: " << status; + } }); return nullptr; } @@ -787,6 +765,13 @@ static napi_value getTransferRate(napi_env env, napi_callback_info info) { return nullptr; } + std::stringstream ss; + ss << "Connected connections"; + ss << " rx rate: " << rx_rate; + ss << " tx rate: " << tx_rate; + + VLOG(1) << ss.str(); + return results; }