From 4e8a6e6c8b4fb2fc8e24825de1197453af288e26 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 8 Aug 2022 14:41:09 +0800 Subject: [PATCH] add user Signed-off-by: unknown --- .../app/error_manager/js_error_manager.cpp | 6 ++++- .../app/error_manager/js_error_observer.cpp | 6 +++-- .../app/error_manager/js_error_observer.h | 2 +- .../appkit/app/application_data_manager.cpp | 16 +++++++++--- frameworks/native/appkit/app/main_thread.cpp | 26 ++++++------------- .../appkit/app/application_data_manager.h | 5 ++-- .../kits/native/appkit/app/main_thread.h | 2 -- 7 files changed, 33 insertions(+), 30 deletions(-) diff --git a/frameworks/js/napi/app/error_manager/js_error_manager.cpp b/frameworks/js/napi/app/error_manager/js_error_manager.cpp index 6bfba89f96..1bfd48e29c 100644 --- a/frameworks/js/napi/app/error_manager/js_error_manager.cpp +++ b/frameworks/js/napi/app/error_manager/js_error_manager.cpp @@ -105,11 +105,15 @@ private: return; } auto observer = observerWptr.lock(); - if (observer && observer->RemoveJsObserverObject(observerId)) { + bool isEmpty = false; + if (observer && observer->RemoveJsObserverObject(observerId, isEmpty)) { task.Resolve(engine, engine.CreateUndefined()); } else { task.Reject(engine, CreateJsError(engine, ERROR_CODE, "observer is not exist!")); } + if (isEmpty) { + AppExecFwk::ApplicationDataManager::GetInstance().RemoveErrorObserver(); + } }; NativeValue* lastParam = (info.argc <= ARGC_ONE) ? nullptr : info.argv[INDEX_ONE]; diff --git a/frameworks/js/napi/app/error_manager/js_error_observer.cpp b/frameworks/js/napi/app/error_manager/js_error_observer.cpp index 7860d15880..85116aa883 100644 --- a/frameworks/js/napi/app/error_manager/js_error_observer.cpp +++ b/frameworks/js/napi/app/error_manager/js_error_observer.cpp @@ -80,9 +80,11 @@ void JsErrorObserver::AddJsObserverObject(int32_t observerId, NativeValue* jsObs observerId, std::shared_ptr(engine_.CreateReference(jsObserverObject, 1))); } -bool JsErrorObserver::RemoveJsObserverObject(int32_t observerId) +bool JsErrorObserver::RemoveJsObserverObject(int32_t observerId, bool &isEmpty) { - return jsObserverObjectMap_.erase(observerId) == 1; + bool result = (jsObserverObjectMap_.erase(observerId) == 1); + isEmpty = jsObserverObjectMap_.empty(); + return result; } } // namespace AbilityRuntime } // namespace OHOS diff --git a/frameworks/js/napi/app/error_manager/js_error_observer.h b/frameworks/js/napi/app/error_manager/js_error_observer.h index c04425d2ac..28d1110e3c 100644 --- a/frameworks/js/napi/app/error_manager/js_error_observer.h +++ b/frameworks/js/napi/app/error_manager/js_error_observer.h @@ -29,7 +29,7 @@ public: ~JsErrorObserver(); void OnUnhandledException(std::string errMsg) override; void AddJsObserverObject(int32_t observerId, NativeValue* jsObserverObject); - bool RemoveJsObserverObject(int32_t observerId); + bool RemoveJsObserverObject(int32_t observerId, bool &isEmpty); private: void CallJsFunction(NativeValue* value, const char* methodName, NativeValue* const* argv, size_t argc); diff --git a/frameworks/native/appkit/app/application_data_manager.cpp b/frameworks/native/appkit/app/application_data_manager.cpp index 9a341ee266..4b7a69945a 100644 --- a/frameworks/native/appkit/app/application_data_manager.cpp +++ b/frameworks/native/appkit/app/application_data_manager.cpp @@ -34,13 +34,21 @@ void ApplicationDataManager::AddErrorObserver(const std::shared_ptr observer = errorObserver_.lock(); - if (observer) { - observer->OnUnhandledException(errMsg); + if (errorObserver_) { + errorObserver_->OnUnhandledException(errMsg); + return true; } + + return false; +} + +void ApplicationDataManager::RemoveErrorObserver() +{ + HILOG_DEBUG("Remove error observer come."); + errorObserver_ = nullptr; } } // namespace AppExecFwk } // namespace OHOS diff --git a/frameworks/native/appkit/app/main_thread.cpp b/frameworks/native/appkit/app/main_thread.cpp index a5e8799800..41e89d854e 100644 --- a/frameworks/native/appkit/app/main_thread.cpp +++ b/frameworks/native/appkit/app/main_thread.cpp @@ -68,7 +68,6 @@ namespace { constexpr int32_t DELIVERY_TIME = 200; constexpr int32_t DISTRIBUTE_TIME = 100; constexpr int32_t UNSPECIFIED_USERID = -2; -constexpr int32_t JS_CRASH_DELAY_TIME = 3000; constexpr int SIGNAL_JS_HEAP = 39; constexpr int SIGNAL_JS_HEAP_PRIV = 40; @@ -410,31 +409,20 @@ void MainThread::ScheduleMemoryLevel(const int level) */ void MainThread::ScheduleProcessSecurityExit() { - PostProcessSecurityExitTask(false); -} - -void MainThread::PostProcessSecurityExitTask(bool delay) -{ - HILOG_DEBUG("PostProcessSecurityExitTask called start"); + HILOG_DEBUG("ScheduleProcessSecurityExit called"); wptr weak = this; auto task = [weak]() { auto appThread = weak.promote(); if (appThread == nullptr) { - HILOG_ERROR("appThread is nullptr, PostProcessSecurityExitTask failed."); + HILOG_ERROR("appThread is nullptr, ScheduleProcessSecurityExit failed."); return; } appThread->HandleProcessSecurityExit(); }; - bool result; - if (delay) { - result = mainHandler_->PostTask(task, JS_CRASH_DELAY_TIME); - } else { - result = mainHandler_->PostTask(task); - } + bool result = mainHandler_->PostTask(task); if (!result) { - HILOG_ERROR("PostProcessSecurityExitTask post task failed"); + HILOG_ERROR("ScheduleProcessSecurityExit post task failed"); } - HILOG_DEBUG("PostProcessSecurityExitTask called end"); } /** @@ -964,7 +952,6 @@ void MainThread::HandleLaunchApplication(const AppLaunchData &appLaunchData, con std::string errorName = GetNativeStrFromJsTaggedObj(obj, "name"); std::string errorStack = GetNativeStrFromJsTaggedObj(obj, "stack"); std::string summary = "Error message:" + errorMsg + "\nStacktrace:\n" + errorStack; - ApplicationDataManager::GetInstance().NotifyUnhandledException(summary); time_t timet; time(&timet); OHOS::HiviewDFX::HiSysEvent::Write(OHOS::HiviewDFX::HiSysEvent::Domain::AAFWK, "JS_ERROR", @@ -976,6 +963,9 @@ void MainThread::HandleLaunchApplication(const AppLaunchData &appLaunchData, con EVENT_KEY_REASON, errorName, EVENT_KEY_JSVM, JSVM_TYPE, EVENT_KEY_SUMMARY, summary); + if (ApplicationDataManager::GetInstance().NotifyUnhandledException(summary)) { + return; + } auto appThread = weak.promote(); if (appThread == nullptr) { HILOG_ERROR("appThread is nullptr, HandleLaunchApplication failed."); @@ -984,7 +974,7 @@ void MainThread::HandleLaunchApplication(const AppLaunchData &appLaunchData, con // if app's callback has been registered, let app decide whether exit or not. HILOG_ERROR("\n%{public}s is about to exit due to RuntimeError\nError type:%{public}s\n%{public}s", bundleName.c_str(), errorName.c_str(), summary.c_str()); - appThread->PostProcessSecurityExitTask(true); + appThread->ScheduleProcessSecurityExit(); }; jsEngine.RegisterUncaughtExceptionHandler(uncaughtTask); application_->SetRuntime(std::move(runtime)); diff --git a/interfaces/kits/native/appkit/app/application_data_manager.h b/interfaces/kits/native/appkit/app/application_data_manager.h index e8f7ae6231..8e9c268a02 100644 --- a/interfaces/kits/native/appkit/app/application_data_manager.h +++ b/interfaces/kits/native/appkit/app/application_data_manager.h @@ -27,13 +27,14 @@ class ApplicationDataManager { public: static ApplicationDataManager &GetInstance(); void AddErrorObserver(const std::shared_ptr &observer); - void NotifyUnhandledException(const std::string &errMsg); + bool NotifyUnhandledException(const std::string &errMsg); + void RemoveErrorObserver(); private: ApplicationDataManager(); ~ApplicationDataManager(); DISALLOW_COPY_AND_MOVE(ApplicationDataManager); - std::weak_ptr errorObserver_; + std::shared_ptr errorObserver_; }; } // namespace AppExecFwk } // namespace OHOS diff --git a/interfaces/kits/native/appkit/app/main_thread.h b/interfaces/kits/native/appkit/app/main_thread.h index cc7dc5f40b..7beab0f51b 100644 --- a/interfaces/kits/native/appkit/app/main_thread.h +++ b/interfaces/kits/native/appkit/app/main_thread.h @@ -419,8 +419,6 @@ private: bool PrepareAbilityDelegator(const std::shared_ptr &record, bool isStageBased, BundleInfo& bundleInfo); - void PostProcessSecurityExitTask(bool delay); - /** * * @brief The handle of application not response process.