From b6d4f26f9aba61c98c345b11f0843c1b6c8acff9 Mon Sep 17 00:00:00 2001 From: Ivan Panferov Date: Thu, 19 Sep 2024 16:19:52 +0800 Subject: [PATCH] Implement appstate for interop Signed-off-by: Ivan Panferov --- .../plugins/ets/runtime/ets_app_state.h | 47 +++++++++++++++++++ static_core/plugins/ets/runtime/ets_vm.h | 16 +++++++ .../ets/runtime/interop_js/ets_vm_plugin.cpp | 17 +++++++ 3 files changed, 80 insertions(+) create mode 100644 static_core/plugins/ets/runtime/ets_app_state.h diff --git a/static_core/plugins/ets/runtime/ets_app_state.h b/static_core/plugins/ets/runtime/ets_app_state.h new file mode 100644 index 0000000000..2ae7bacd00 --- /dev/null +++ b/static_core/plugins/ets/runtime/ets_app_state.h @@ -0,0 +1,47 @@ +/** + * Copyright (c) 2024 Huawei Device Co., Ltd. + * 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. + */ + +#ifndef PANDA_PLUGINS_ETS_RUNTIME_ETS_APP_STATE_H_ +#define PANDA_PLUGINS_ETS_RUNTIME_ETS_APP_STATE_H_ + +#include +#include + +namespace ark::ets { + +class AppState { +public: + enum class State { FOREGROUND, BACKGROUND, SENSITIVE_START, SENSITIVE_END, IDLE_START, IDLE_END, INITIAL }; + + AppState() = default; + AppState(State state, int64_t timestamp) : state_(state), timestamp_(timestamp) {} + + State GetState() + { + return state_; + } + + int64_t GetTimeStamp() + { + return timestamp_; + } + +private: + State state_ = State::INITIAL; + int64_t timestamp_ = 0; +}; +} // namespace ark::ets + +#endif // PANDA_PLUGINS_ETS_RUNTIME_ETS_APP_STATE_H_ diff --git a/static_core/plugins/ets/runtime/ets_vm.h b/static_core/plugins/ets/runtime/ets_vm.h index 918f1dc799..250ed2cbbd 100644 --- a/static_core/plugins/ets/runtime/ets_vm.h +++ b/static_core/plugins/ets/runtime/ets_vm.h @@ -60,6 +60,7 @@ #include "plugins/ets/runtime/ets_handle_scope.h" #include "plugins/ets/runtime/ets_handle.h" #include "plugins/ets/runtime/ets_taskpool.h" +#include "plugins/ets/runtime/ets_app_state.h" namespace ark::ets { @@ -355,6 +356,18 @@ public: void BeforeShutdown() override; + void UpdateAppState(AppState appState) + { + os::memory::LockHolder lh(appStateLock_); + appState_ = appState; + } + + AppState GetAppState() + { + os::memory::LockHolder lh(appStateLock_); + return appState_; + } + protected: bool CheckEntrypointSignature(Method *entrypoint) override; Expected InvokeEntrypointImpl(Method *entrypoint, @@ -409,6 +422,9 @@ private: FloatToStringCache *floatToStringCache_ {nullptr}; LongToStringCache *longToStringCache_ {nullptr}; + os::memory::Mutex appStateLock_; + AppState appState_; + ExternalData externalData_ {}; NO_MOVE_SEMANTIC(PandaEtsVM); diff --git a/static_core/plugins/ets/runtime/interop_js/ets_vm_plugin.cpp b/static_core/plugins/ets/runtime/interop_js/ets_vm_plugin.cpp index 8cd95649d1..770156b616 100644 --- a/static_core/plugins/ets/runtime/interop_js/ets_vm_plugin.cpp +++ b/static_core/plugins/ets/runtime/interop_js/ets_vm_plugin.cpp @@ -14,6 +14,7 @@ */ #include +#include "plugins/ets/runtime/ets_app_state.h" #include "plugins/ets/runtime/ets_panda_file_items.h" #include "plugins/ets/runtime/ets_vm_api.h" #include "plugins/ets/runtime/interop_js/interop_context.h" @@ -178,6 +179,20 @@ static bool RegisterTimerModule(napi_env jsEnv) return TimerModule::Init(etsEnv, jsEnv); } +static bool RegisterAppStateCallback(napi_env jsEnv) +{ +#if defined(PANDA_TARGET_OHOS) + auto callback = [](int state, int64_t timeStamp) { + auto appState = AppState(static_cast(state), timeStamp); + EtsCoroutine::GetCurrent()->GetPandaVM()->UpdateAppState(appState); + }; + auto status = napi_register_appstate_callback(jsEnv, callback); + return status == napi_ok; +#else + return true; +#endif +} + static std::vector GetArgs(napi_env env, napi_callback_info info) { size_t argc = 0; @@ -235,6 +250,7 @@ static napi_value CreateEtsRuntime(napi_env env, napi_callback_info info) bool res = ark::ets::CreateRuntime(stdlibPath, indexPath, useJit, useAot); res &= RegisterTimerModule(env); + res &= RegisterAppStateCallback(env); if (res) { auto coro = EtsCoroutine::GetCurrent(); ScopedManagedCodeThread scoped(coro); @@ -351,6 +367,7 @@ static napi_value CreateRuntime(napi_env env, napi_callback_info info) bool res = ets::CreateRuntime(addOpts); res &= RegisterTimerModule(env); + res &= RegisterAppStateCallback(env); if (res) { auto coro = EtsCoroutine::GetCurrent(); ScopedManagedCodeThread scoped(coro); -- Gitee