From 83044cdeb29d3b23d830d33a56d871615b351d16 Mon Sep 17 00:00:00 2001 From: csw Date: Sun, 20 Jul 2025 10:36:55 +0800 Subject: [PATCH] =?UTF-8?q?ANI=20=E9=80=82=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: csw --- frameworks/js/napi/BUILD.gn | 1 + .../app/error_manager/ani/include/ani_utils.h | 0 .../ani/include/errormanager_ani.h | 0 .../ani/include/errorobserver_ani.h | 0 .../ani/include/freezeobserver_ani.h | 0 .../ani/include/globalobserver_ani.h | 0 .../ani/include/loopobserver_ani.h | 0 .../app/error_manager/ani/src/ani_utils.cpp | 0 .../ani/src/errormanager_ani.cpp | 0 .../ani/src/errorobserver_ani.cpp | 0 .../ani/src/freezeobserver_ani.cpp | 0 .../ani/src/globalobserver_ani.cpp | 0 .../ani/src/loopobserver_ani.cpp | 0 .../appkit/app/application_data_manager.cpp | 29 ++++++++++++++++--- frameworks/native/appkit/app/main_thread.cpp | 23 +++++++++++++++ .../appkit/app/application_data_manager.h | 23 ++++++++++++++- 16 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 frameworks/js/napi/app/error_manager/ani/include/ani_utils.h create mode 100644 frameworks/js/napi/app/error_manager/ani/include/errormanager_ani.h create mode 100644 frameworks/js/napi/app/error_manager/ani/include/errorobserver_ani.h create mode 100644 frameworks/js/napi/app/error_manager/ani/include/freezeobserver_ani.h create mode 100644 frameworks/js/napi/app/error_manager/ani/include/globalobserver_ani.h create mode 100644 frameworks/js/napi/app/error_manager/ani/include/loopobserver_ani.h create mode 100644 frameworks/js/napi/app/error_manager/ani/src/ani_utils.cpp create mode 100644 frameworks/js/napi/app/error_manager/ani/src/errormanager_ani.cpp create mode 100644 frameworks/js/napi/app/error_manager/ani/src/errorobserver_ani.cpp create mode 100644 frameworks/js/napi/app/error_manager/ani/src/freezeobserver_ani.cpp create mode 100644 frameworks/js/napi/app/error_manager/ani/src/globalobserver_ani.cpp create mode 100644 frameworks/js/napi/app/error_manager/ani/src/loopobserver_ani.cpp diff --git a/frameworks/js/napi/BUILD.gn b/frameworks/js/napi/BUILD.gn index a1db6c7131d..4f63a157f2f 100644 --- a/frameworks/js/napi/BUILD.gn +++ b/frameworks/js/napi/BUILD.gn @@ -40,6 +40,7 @@ group("napi_packages") { "${ability_runtime_napi_path}/app/environment_callback:environmentcallback", "${ability_runtime_napi_path}/app/error_manager:errormanager", "${ability_runtime_napi_path}/app/error_manager:errormanager_napi", + "${ability_runtime_napi_path}/app/error_manager/ets:errormanagerani", "${ability_runtime_napi_path}/app/js_app_manager:appmanager", "${ability_runtime_napi_path}/app/recovery:apprecovery_napi", "${ability_runtime_napi_path}/app/sendable_context_manager:sendablecontextmanager_napi", diff --git a/frameworks/js/napi/app/error_manager/ani/include/ani_utils.h b/frameworks/js/napi/app/error_manager/ani/include/ani_utils.h new file mode 100644 index 00000000000..e69de29bb2d diff --git a/frameworks/js/napi/app/error_manager/ani/include/errormanager_ani.h b/frameworks/js/napi/app/error_manager/ani/include/errormanager_ani.h new file mode 100644 index 00000000000..e69de29bb2d diff --git a/frameworks/js/napi/app/error_manager/ani/include/errorobserver_ani.h b/frameworks/js/napi/app/error_manager/ani/include/errorobserver_ani.h new file mode 100644 index 00000000000..e69de29bb2d diff --git a/frameworks/js/napi/app/error_manager/ani/include/freezeobserver_ani.h b/frameworks/js/napi/app/error_manager/ani/include/freezeobserver_ani.h new file mode 100644 index 00000000000..e69de29bb2d diff --git a/frameworks/js/napi/app/error_manager/ani/include/globalobserver_ani.h b/frameworks/js/napi/app/error_manager/ani/include/globalobserver_ani.h new file mode 100644 index 00000000000..e69de29bb2d diff --git a/frameworks/js/napi/app/error_manager/ani/include/loopobserver_ani.h b/frameworks/js/napi/app/error_manager/ani/include/loopobserver_ani.h new file mode 100644 index 00000000000..e69de29bb2d diff --git a/frameworks/js/napi/app/error_manager/ani/src/ani_utils.cpp b/frameworks/js/napi/app/error_manager/ani/src/ani_utils.cpp new file mode 100644 index 00000000000..e69de29bb2d diff --git a/frameworks/js/napi/app/error_manager/ani/src/errormanager_ani.cpp b/frameworks/js/napi/app/error_manager/ani/src/errormanager_ani.cpp new file mode 100644 index 00000000000..e69de29bb2d diff --git a/frameworks/js/napi/app/error_manager/ani/src/errorobserver_ani.cpp b/frameworks/js/napi/app/error_manager/ani/src/errorobserver_ani.cpp new file mode 100644 index 00000000000..e69de29bb2d diff --git a/frameworks/js/napi/app/error_manager/ani/src/freezeobserver_ani.cpp b/frameworks/js/napi/app/error_manager/ani/src/freezeobserver_ani.cpp new file mode 100644 index 00000000000..e69de29bb2d diff --git a/frameworks/js/napi/app/error_manager/ani/src/globalobserver_ani.cpp b/frameworks/js/napi/app/error_manager/ani/src/globalobserver_ani.cpp new file mode 100644 index 00000000000..e69de29bb2d diff --git a/frameworks/js/napi/app/error_manager/ani/src/loopobserver_ani.cpp b/frameworks/js/napi/app/error_manager/ani/src/loopobserver_ani.cpp new file mode 100644 index 00000000000..e69de29bb2d diff --git a/frameworks/native/appkit/app/application_data_manager.cpp b/frameworks/native/appkit/app/application_data_manager.cpp index 6e1b282e84d..a3d137c527e 100644 --- a/frameworks/native/appkit/app/application_data_manager.cpp +++ b/frameworks/native/appkit/app/application_data_manager.cpp @@ -61,8 +61,8 @@ bool ApplicationDataManager::NotifyCJUnhandledException(const std::string &errMs bool ApplicationDataManager::NotifySTSUnhandledException(const std::string &errMsg) { - if (errorObserver_) { - errorObserver_->OnUnhandledException(errMsg); + if (stsUnhandledExceptionCallback_) { + stsUnhandledExceptionCallback_(errMsg); return true; } return AppRecovery::GetInstance().TryRecoverApp(StateReason::STS_ERROR); @@ -101,11 +101,32 @@ bool ApplicationDataManager::NotifyCJExceptionObject(const AppExecFwk::ErrorObje bool ApplicationDataManager::NotifySTSExceptionObject(const AppExecFwk::ErrorObject &errorObj) { TAG_LOGD(AAFwkTag::APPKIT, "Notify Exception error observer come"); - if (errorObserver_) { - errorObserver_->OnExceptionObject(errorObj); + if (stsExceptionObjectCallback_) { + stsExceptionObjectCallback_(errorObj); return true; } return AppRecovery::GetInstance().TryRecoverApp(StateReason::STS_ERROR); } + +void ApplicationDataManager::SetStsExceptionInfo(const ExpectionInfo &info) +{ + stsExceptionInfo_ = info; +} + +void ApplicationDataManager::RegisterSTSExceptionObjectCallback(const STSExceptionObjectCallback &callback) +{ + stsExceptionObjectCallback_ = callback; +} + +void ApplicationDataManager::RegisterSTSUnhandledExceptionCallback(const STSUnhandledExceptionCallback &callback) +{ + stsUnhandledExceptionCallback_ = callback; +} + +void ApplicationDataManager::GetStsExceptionInfo(ExpectionInfo &info) +{ + info = stsExceptionInfo_; +} + } // namespace AppExecFwk } // namespace OHOS diff --git a/frameworks/native/appkit/app/main_thread.cpp b/frameworks/native/appkit/app/main_thread.cpp index 2e7b5f3e418..0f4dc89786b 100644 --- a/frameworks/native/appkit/app/main_thread.cpp +++ b/frameworks/native/appkit/app/main_thread.cpp @@ -1764,6 +1764,29 @@ void MainThread::HandleLaunchApplication(const AppLaunchData &appLaunchData, con case AbilityRuntime::Runtime::Language::STS: { auto expectionInfo = CreateStsExceptionInfo(bundleName, versionCode, hapPath, appRunningId, pid, processName); + // + wptr weak = this; + auto GetForgroundInfo = [weak](bool &foreground) { + auto appThread = weak.promote(); + if (appThread == nullptr) { + TAG_LOGE(AAFwkTag::APPKIT, "null appThread"); + return; + } + if (appThread->applicationImpl_ && + appThread->applicationImpl_->GetState() == ApplicationImpl::APP_STATE_FOREGROUND) { + foreground = true; + } + }; + ExpectionInfo expectionInfo = { + .bundleName = bundleName, + .versionCode = versionCode, + .hapPath = hapPath, + .appRunningId = appRunningId, + .pid = pid, + .processName = processName, + .GetForgroundInfo = GetForgroundInfo + }; + ApplicationDataManager::GetInstance().SetStsExceptionInfo(expectionInfo); runtime->RegisterUncaughtExceptionHandler((void*)&expectionInfo); break; } diff --git a/interfaces/kits/native/appkit/app/application_data_manager.h b/interfaces/kits/native/appkit/app/application_data_manager.h index eed67a9aa3b..77558c67aed 100644 --- a/interfaces/kits/native/appkit/app/application_data_manager.h +++ b/interfaces/kits/native/appkit/app/application_data_manager.h @@ -21,8 +21,23 @@ #include "ierror_observer.h" #include "nocopyable.h" + namespace OHOS { namespace AppExecFwk { + +using STSExceptionObjectCallback = std::function; +using STSUnhandledExceptionCallback = std::function; +using GetForgroundInfoFunc = std::function; +struct ExpectionInfo { + std::string bundleName; + int32_t versionCode; + std::string hapPath; + int32_t appRunningId; + int32_t pid; + std::string processName; + GetForgroundInfoFunc GetForgroundInfo; +}; + class ApplicationDataManager { public: static ApplicationDataManager &GetInstance(); @@ -34,12 +49,18 @@ public: bool NotifyExceptionObject(const AppExecFwk::ErrorObject &errorObj); bool NotifyCJExceptionObject(const AppExecFwk::ErrorObject &errorObj); bool NotifySTSExceptionObject(const AppExecFwk::ErrorObject &errorObj); - + void SetStsExceptionInfo(const ExpectionInfo &info); + void RegisterSTSExceptionObjectCallback(const STSExceptionObjectCallback &callback); + void RegisterSTSUnhandledExceptionCallback(const STSUnhandledExceptionCallback &callback); + void GetStsExceptionInfo(ExpectionInfo &info); private: ApplicationDataManager(); ~ApplicationDataManager(); DISALLOW_COPY_AND_MOVE(ApplicationDataManager); std::shared_ptr errorObserver_; + ExpectionInfo stsExceptionInfo_; + STSExceptionObjectCallback stsExceptionObjectCallback_; + STSUnhandledExceptionCallback stsUnhandledExceptionCallback_; }; } // namespace AppExecFwk } // namespace OHOS -- Gitee