From 215d397147fffbfccd697bc1fbbcbe7fe8232696 Mon Sep 17 00:00:00 2001 From: xiatian Date: Mon, 6 Jul 2026 10:43:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=B9=B6=E5=8F=91=E5=AE=89?= =?UTF-8?q?=E5=85=A8=E4=BF=9D=E6=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xiatian Change-Id: I58d44c51ba0467d2dde10a03f269f9f5a600758e --- frameworks/cj/ffi/cj_ability_delegator.cpp | 134 ++++++++------------- 1 file changed, 49 insertions(+), 85 deletions(-) diff --git a/frameworks/cj/ffi/cj_ability_delegator.cpp b/frameworks/cj/ffi/cj_ability_delegator.cpp index c8ccf3bdbf..9f6b65daf5 100644 --- a/frameworks/cj/ffi/cj_ability_delegator.cpp +++ b/frameworks/cj/ffi/cj_ability_delegator.cpp @@ -37,6 +37,7 @@ std::map> g_monitorRecord; std::map> g_stageMonitorRecord; std::map> g_abilityRecord; std::mutex g_mutexAbilityRecord; +std::mutex g_mtxMonitorRecord; std::mutex g_mtxStageMonitorRecord; CJAbilityDelegator::CJAbilityDelegator(const std::shared_ptr& abilityDelegator) @@ -213,41 +214,58 @@ void CJAbilityDelegator::FinishTest(const char* msg, int64_t code) delegator_->FinishUserTest(msg, code); } -std::shared_ptr ParseMonitorPara( - int64_t monitorId, const std::string& abilityName, const std::string& moduleName, bool& isExisted) -{ - for (auto iter = g_monitorRecord.begin(); iter != g_monitorRecord.end(); ++iter) { - if (iter->first == monitorId) { - TAG_LOGE(AAFwkTag::DELEGATOR, "monitor existed"); - isExisted = true; - return iter->second; - } - } +enum class MonitorRecordOp { + ADD, + REMOVE, +}; +std::shared_ptr GetOrCreateMonitor( + int64_t monitorId, const std::string& abilityName, const std::string& moduleName, MonitorRecordOp op) +{ + std::unique_lock lck(g_mtxMonitorRecord); + auto iter = g_monitorRecord.find(monitorId); + if (iter != g_monitorRecord.end()) { + TAG_LOGE(AAFwkTag::DELEGATOR, "monitor existed"); + auto existing = iter->second; + if (op == MonitorRecordOp::REMOVE) { + g_monitorRecord.erase(iter); + } + return existing; + } + if (op == MonitorRecordOp::REMOVE) { + TAG_LOGE(AAFwkTag::DELEGATOR, "monitor not existed"); + return nullptr; + } auto cjMonitorObj = std::make_shared(monitorId); - std::shared_ptr cjMonitor = nullptr; + std::shared_ptr cjMonitor; if (moduleName.empty()) { cjMonitor = std::make_shared(abilityName, cjMonitorObj); } else { cjMonitor = std::make_shared(abilityName, moduleName, cjMonitorObj); } + g_monitorRecord.emplace(monitorId, cjMonitor); return cjMonitor; } -std::shared_ptr ParseStageMonitorPara( - int64_t stageMonitorId, const std::string& moduleName, const std::string& srcEntrance, bool& isExisted) +std::shared_ptr GetOrCreateStageMonitor( + int64_t stageMonitorId, const std::string& moduleName, const std::string& srcEntrance, MonitorRecordOp op) { - { - std::unique_lock lck(g_mtxStageMonitorRecord); - for (auto iter = g_stageMonitorRecord.begin(); iter != g_stageMonitorRecord.end(); ++iter) { - if (iter->first == stageMonitorId) { - TAG_LOGE(AAFwkTag::DELEGATOR, "stageMonitor existed"); - isExisted = true; - return iter->second; - } + std::unique_lock lck(g_mtxStageMonitorRecord); + auto iter = g_stageMonitorRecord.find(stageMonitorId); + if (iter != g_stageMonitorRecord.end()) { + TAG_LOGE(AAFwkTag::DELEGATOR, "stageMonitor existed"); + auto existing = iter->second; + if (op == MonitorRecordOp::REMOVE) { + g_stageMonitorRecord.erase(iter); } + return existing; + } + if (op == MonitorRecordOp::REMOVE) { + TAG_LOGE(AAFwkTag::DELEGATOR, "stageMonitor not existed"); + return nullptr; } auto cjStageMonitor = std::make_shared(moduleName, srcEntrance, stageMonitorId); + g_stageMonitorRecord.emplace(stageMonitorId, cjStageMonitor); return cjStageMonitor; } @@ -398,16 +416,11 @@ int32_t FFIAbilityDelegatorAddAbilityMonitor( TAG_LOGE(AAFwkTag::DELEGATOR, "null cj delegator"); return COMMON_FAILED; } - bool isExisted = false; - auto cjMonitor = ParseMonitorPara(monitorId, abilityName, moduleName, isExisted); + auto cjMonitor = GetOrCreateMonitor(monitorId, abilityName, moduleName, MonitorRecordOp::ADD); if (cjMonitor == nullptr) { TAG_LOGE(AAFwkTag::DELEGATOR, "parase cj monitor failed"); return INCORRECT_PARAMETERS; } - - if (!isExisted) { - g_monitorRecord.emplace(monitorId, cjMonitor); - } cjDelegator->AddAbilityMonitor(cjMonitor); return 0; } @@ -420,21 +433,11 @@ int32_t FFIAbilityDelegatorRemoveAbilityMonitor( TAG_LOGE(AAFwkTag::DELEGATOR, "null cj delegator"); return COMMON_FAILED; } - bool isExisted = false; - auto cjMonitor = ParseMonitorPara(monitorId, abilityName, moduleName, isExisted); + auto cjMonitor = GetOrCreateMonitor(monitorId, abilityName, moduleName, MonitorRecordOp::REMOVE); if (cjMonitor == nullptr) { - TAG_LOGE(AAFwkTag::DELEGATOR, "parase cj monitor failed"); + TAG_LOGE(AAFwkTag::DELEGATOR, "monitor not existed"); return INCORRECT_PARAMETERS; } - - if (isExisted) { - for (auto iter = g_monitorRecord.begin(); iter != g_monitorRecord.end(); ++iter) { - if (iter->first == monitorId) { - g_monitorRecord.erase(iter); - break; - } - } - } cjDelegator->RemoveAbilityMonitor(cjMonitor); return 0; } @@ -455,18 +458,13 @@ int32_t FFIAbilityDelegatorWaitAbilityMonitor( const char* abilityName = abilityInfo.abilityName; const char* moduleName = abilityInfo.moduleName; - bool isExisted = false; - auto cjMonitor = ParseMonitorPara(monitorId, abilityName, moduleName, isExisted); + auto cjMonitor = GetOrCreateMonitor(monitorId, abilityName, moduleName, MonitorRecordOp::ADD); if (cjMonitor == nullptr) { TAG_LOGE(AAFwkTag::DELEGATOR, "parase cj monitor failed"); *abilityId = 0; return INCORRECT_PARAMETERS; } - if (!isExisted) { - g_monitorRecord.emplace(monitorId, cjMonitor); - } - auto property = cjDelegator->WaitAbilityMonitor(cjMonitor); if (!property) { TAG_LOGE(AAFwkTag::DELEGATOR, "property is null"); @@ -498,18 +496,13 @@ int32_t FFIAbilityDelegatorWaitAbilityMonitorWithTimeout( const char* abilityName = abilityInfo.abilityName; const char* moduleName = abilityInfo.moduleName; - bool isExisted = false; - auto cjMonitor = ParseMonitorPara(monitorId, abilityName, moduleName, isExisted); + auto cjMonitor = GetOrCreateMonitor(monitorId, abilityName, moduleName, MonitorRecordOp::ADD); if (cjMonitor == nullptr) { TAG_LOGE(AAFwkTag::DELEGATOR, "parase cj monitor failed"); *abilityId = 0; return INCORRECT_PARAMETERS; } - if (!isExisted) { - g_monitorRecord.emplace(monitorId, cjMonitor); - } - auto property = cjDelegator->WaitAbilityMonitor(cjMonitor, timeout); if (!property) { TAG_LOGE(AAFwkTag::DELEGATOR, "property is null"); @@ -533,17 +526,11 @@ int32_t FFIAbilityDelegatorAddAbilityStageMonitor( TAG_LOGE(AAFwkTag::DELEGATOR, "null cj delegator"); return COMMON_FAILED; } - bool isExisted = false; - auto cjStageMonitor = ParseStageMonitorPara(stageMonitorId, moduleName, srcEntrance, isExisted); + auto cjStageMonitor = GetOrCreateStageMonitor(stageMonitorId, moduleName, srcEntrance, MonitorRecordOp::ADD); if (cjStageMonitor == nullptr) { TAG_LOGE(AAFwkTag::DELEGATOR, "parse cj stageMonitor failed"); return INCORRECT_PARAMETERS; } - - if (!isExisted) { - std::unique_lock lck(g_mtxStageMonitorRecord); - g_stageMonitorRecord.emplace(stageMonitorId, cjStageMonitor); - } cjDelegator->AddAbilityStageMonitor(cjStageMonitor); return 0; } @@ -556,22 +543,11 @@ int32_t FFIAbilityDelegatorRemoveAbilityStageMonitor( TAG_LOGE(AAFwkTag::DELEGATOR, "null cj delegator"); return COMMON_FAILED; } - bool isExisted = false; - auto cjStageMonitor = ParseStageMonitorPara(stageMonitorId, moduleName, srcEntrance, isExisted); + auto cjStageMonitor = GetOrCreateStageMonitor(stageMonitorId, moduleName, srcEntrance, MonitorRecordOp::REMOVE); if (cjStageMonitor == nullptr) { - TAG_LOGE(AAFwkTag::DELEGATOR, "parse cj stageMonitor failed"); + TAG_LOGE(AAFwkTag::DELEGATOR, "stageMonitor not existed"); return INCORRECT_PARAMETERS; } - - if (isExisted) { - std::unique_lock lck(g_mtxStageMonitorRecord); - for (auto iter = g_stageMonitorRecord.begin(); iter != g_stageMonitorRecord.end(); ++iter) { - if (iter->first == stageMonitorId) { - g_stageMonitorRecord.erase(iter); - break; - } - } - } cjDelegator->RemoveAbilityStageMonitor(cjStageMonitor); return 0; } @@ -591,19 +567,13 @@ int32_t FFIAbilityDelegatorWaitAbilityStageMonitor( } const char* moduleName = abilityStageInfo.moduleName; const char* srcEntrance = abilityStageInfo.srcEntrance; - bool isExisted = false; - auto cjStageMonitor = ParseStageMonitorPara(stageMonitorId, moduleName, srcEntrance, isExisted); + auto cjStageMonitor = GetOrCreateStageMonitor(stageMonitorId, moduleName, srcEntrance, MonitorRecordOp::ADD); if (cjStageMonitor == nullptr) { TAG_LOGE(AAFwkTag::DELEGATOR, "parse cj stageMonitor failed"); *abilityStageId = 0; return INCORRECT_PARAMETERS; } - if (!isExisted) { - std::unique_lock lck(g_mtxStageMonitorRecord); - g_stageMonitorRecord.emplace(stageMonitorId, cjStageMonitor); - } - auto property = cjDelegator->WaitAbilityStageMonitor(cjStageMonitor); if (!property) { TAG_LOGE(AAFwkTag::DELEGATOR, "stageProperty is null"); @@ -630,19 +600,13 @@ int32_t FFIAbilityDelegatorWaitAbilityStageMonitorWithTimeout( } const char* moduleName = abilityStageInfo.moduleName; const char* srcEntrance = abilityStageInfo.srcEntrance; - bool isExisted = false; - auto cjStageMonitor = ParseStageMonitorPara(stageMonitorId, moduleName, srcEntrance, isExisted); + auto cjStageMonitor = GetOrCreateStageMonitor(stageMonitorId, moduleName, srcEntrance, MonitorRecordOp::ADD); if (cjStageMonitor == nullptr) { TAG_LOGE(AAFwkTag::DELEGATOR, "parse cj stageMonitor failed"); *abilityStageId = 0; return INCORRECT_PARAMETERS; } - if (!isExisted) { - std::unique_lock lck(g_mtxStageMonitorRecord); - g_stageMonitorRecord.emplace(stageMonitorId, cjStageMonitor); - } - auto property = cjDelegator->WaitAbilityStageMonitor(cjStageMonitor, timeout); if (!property) { TAG_LOGE(AAFwkTag::DELEGATOR, "stageProperty is null");