fix security component thread security issue

Signed-off-by: Haryslee <lihao189@huawei.com>
This commit is contained in:
Haryslee 2024-11-21 20:39:09 +08:00
parent 67fc6cf82b
commit 5e99a424d6
2 changed files with 47 additions and 10 deletions

View File

@ -28,6 +28,10 @@ namespace OHOS::Ace::NG {
namespace {
#ifdef SECURITY_COMPONENT_ENABLE
const int32_t DELAY_RELEASE_MILLSEC = 10;
static std::unordered_map<uint64_t, RefPtr<FrameNode>> g_scNodeMap;
static std::vector<uint64_t> 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<FrameNode>& 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<std::mutex> 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<int32_t(int32_t)> SecurityComponentPattern::CreateFirstUseDialogCloseFunc(
@ -666,33 +685,51 @@ std::function<int32_t(int32_t)> SecurityComponentPattern::CreateFirstUseDialogCl
{
return [weak = WeakClaim(this), weakContext = WeakPtr(pipeline),
node = frameNode, taskName = taskName](int32_t result) mutable {
std::lock_guard<std::mutex> 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<std::mutex> 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<int32_t>(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;
};
}

View File

@ -124,7 +124,7 @@ private:
int32_t ReportSecurityComponentClickEvent(GestureEvent& event);
int32_t ReportSecurityComponentClickEvent(const KeyEvent& event);
void DoTriggerOnclick(int32_t result);
void DelayReleaseNode(RefPtr<FrameNode>& node);
void DelayReleaseNode(uint64_t index);
std::function<int32_t(int32_t)> CreateFirstUseDialogCloseFunc(
RefPtr<FrameNode>& frameNode, RefPtr<PipelineContext>& pipeline, const std::string& taskName);
#endif