From 5e99a424d6113f2cfa9d2b325d1bd6e7be4fb9d1 Mon Sep 17 00:00:00 2001 From: Haryslee Date: Thu, 21 Nov 2024 20:39:09 +0800 Subject: [PATCH] fix security component thread security issue Signed-off-by: Haryslee --- .../security_component_pattern.cpp | 55 ++++++++++++++++--- .../security_component_pattern.h | 2 +- 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/frameworks/core/components_ng/pattern/security_component/security_component_pattern.cpp b/frameworks/core/components_ng/pattern/security_component/security_component_pattern.cpp index 1d2213f626d..526dad74af6 100644 --- a/frameworks/core/components_ng/pattern/security_component/security_component_pattern.cpp +++ b/frameworks/core/components_ng/pattern/security_component/security_component_pattern.cpp @@ -28,6 +28,10 @@ namespace OHOS::Ace::NG { namespace { #ifdef SECURITY_COMPONENT_ENABLE const int32_t DELAY_RELEASE_MILLSEC = 10; +static std::unordered_map> g_scNodeMap; +static std::vector g_omittedNodeIndex; +static uint64_t g_scIndex = 0; +static std::mutex g_scMutex; #endif } SecurityComponentPattern::SecurityComponentPattern() @@ -650,15 +654,30 @@ void SecurityComponentPattern::DoTriggerOnclick(int32_t result) gestureEventHub->ActClick(jsonShrd); } -void SecurityComponentPattern::DelayReleaseNode(RefPtr& node) +void SecurityComponentPattern::DelayReleaseNode(uint64_t index) { if (uiEventHandler_ == nullptr) { SC_LOG_WARN("UIEventHandler invalid"); return; } - uiEventHandler_->PostTask( - [nodeInner = std::move(node)]() { return; }, - DELAY_RELEASE_MILLSEC); + bool res = uiEventHandler_->PostTask([index] { + std::lock_guard lock(g_scMutex); + g_scNodeMap.erase(index); + if (g_omittedNodeIndex.size() != 0) { + for (auto item : g_omittedNodeIndex) { + g_scNodeMap.erase(item); + } + g_omittedNodeIndex.clear(); + } + SC_LOG_INFO("Security component frameNode cached size: %{public}zu, index: %{public}" PRId64, + g_scNodeMap.size(), index); + return; + }, + DELAY_RELEASE_MILLSEC); + if (!res) { + SC_LOG_ERROR("Security component post task failed."); + g_omittedNodeIndex.push_back(index); + } } std::function SecurityComponentPattern::CreateFirstUseDialogCloseFunc( @@ -666,33 +685,51 @@ std::function SecurityComponentPattern::CreateFirstUseDialogCl { return [weak = WeakClaim(this), weakContext = WeakPtr(pipeline), node = frameNode, taskName = taskName](int32_t result) mutable { + std::lock_guard lock(g_scMutex); + g_scNodeMap[++g_scIndex] = std::move(node); auto pattern = weak.Upgrade(); if (pattern == nullptr) { return -1; } auto context = weakContext.Upgrade(); if (context == nullptr) { - pattern->DelayReleaseNode(node); + pattern->DelayReleaseNode(g_scIndex); return -1; } auto taskExecutor = context->GetTaskExecutor(); if (taskExecutor == nullptr) { - pattern->DelayReleaseNode(node); + pattern->DelayReleaseNode(g_scIndex); return -1; } - taskExecutor->PostTask( - [weak, instanceId = context->GetInstanceId(), result, nodeInner = std::move(node)] { + bool res = taskExecutor->PostTask( + [weak, instanceId = context->GetInstanceId(), result, index = g_scIndex] { + ContainerScope scope(instanceId); + std::lock_guard lock(g_scMutex); + SC_LOG_INFO("Security component frameNode cached size: %{public}zu, index: %{public}" PRId64, + g_scNodeMap.size(), index); + if (g_omittedNodeIndex.size() != 0) { + for (auto item : g_omittedNodeIndex) { + g_scNodeMap.erase(item); + } + g_omittedNodeIndex.clear(); + } if (result == static_cast(SecurityComponentHandleResult::GRANT_CANCEL)) { + g_scNodeMap.erase(index); return; } - ContainerScope scope(instanceId); auto pattern = weak.Upgrade(); if (pattern == nullptr) { + g_scNodeMap.erase(index); return; } pattern->DoTriggerOnclick(result); + g_scNodeMap.erase(index); }, TaskExecutor::TaskType::UI, taskName); + if (!res) { + SC_LOG_ERROR("Security component post task failed."); + g_omittedNodeIndex.push_back(g_scIndex); + } return 0; }; } diff --git a/frameworks/core/components_ng/pattern/security_component/security_component_pattern.h b/frameworks/core/components_ng/pattern/security_component/security_component_pattern.h index 797809907bb..d3c04645ebb 100644 --- a/frameworks/core/components_ng/pattern/security_component/security_component_pattern.h +++ b/frameworks/core/components_ng/pattern/security_component/security_component_pattern.h @@ -124,7 +124,7 @@ private: int32_t ReportSecurityComponentClickEvent(GestureEvent& event); int32_t ReportSecurityComponentClickEvent(const KeyEvent& event); void DoTriggerOnclick(int32_t result); - void DelayReleaseNode(RefPtr& node); + void DelayReleaseNode(uint64_t index); std::function CreateFirstUseDialogCloseFunc( RefPtr& frameNode, RefPtr& pipeline, const std::string& taskName); #endif