From c3bf0bb074ab0ef85c195d968bd57bbc5bdada44 Mon Sep 17 00:00:00 2001 From: h60047265 Date: Mon, 30 Dec 2024 18:53:11 +0800 Subject: [PATCH] =?UTF-8?q?=E2=80=9D=E6=9B=B4=E6=96=B0=E5=85=AC=E5=85=B1?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=E6=9D=A1=E4=BB=B6=EF=BC=8C=E6=A6=82=E7=8E=87?= =?UTF-8?q?=E6=BC=8F=E4=BA=8B=E4=BB=B6=E2=80=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: h60047265 --- .../include/collect/common_event_collect.h | 2 + .../source/collect/common_event_collect.cpp | 51 ++++++++++++++----- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/services/samgr/native/include/collect/common_event_collect.h b/services/samgr/native/include/collect/common_event_collect.h index 25e5e559..69e026b6 100644 --- a/services/samgr/native/include/collect/common_event_collect.h +++ b/services/samgr/native/include/collect/common_event_collect.h @@ -68,6 +68,7 @@ private: sptr commonEventDeath_; std::set commonEventNames_; std::shared_ptr workHandler_; + std::shared_ptr unsubHandler_; std::shared_ptr commonEventSubscriber_ = nullptr; std::mutex commonEventStateLock_; std::set commonEventWhitelist; @@ -101,6 +102,7 @@ class CommonHandler { bool SendEvent(uint32_t eventId, int64_t extraDataId, uint64_t delayTime); void CleanFfrt(); void SetFfrt(); + bool PostTask(std::function func, uint64_t delayTime); private: wptr commonCollect_; diff --git a/services/samgr/native/source/collect/common_event_collect.cpp b/services/samgr/native/source/collect/common_event_collect.cpp index 305f9d59..ac482b4d 100644 --- a/services/samgr/native/source/collect/common_event_collect.cpp +++ b/services/samgr/native/source/collect/common_event_collect.cpp @@ -36,6 +36,7 @@ constexpr uint32_t INIT_EVENT = 10; constexpr uint32_t SUB_COMMON_EVENT = 11; constexpr uint32_t REMOVE_EXTRA_DATA_EVENT = 12; constexpr uint32_t REMOVE_EXTRA_DATA_DELAY_TIME = 300000; +constexpr uint32_t UNSUB_DELAY_TIME = 10 * 1000; constexpr int64_t MAX_EXTRA_DATA_ID = 1000000000; constexpr int32_t COMMON_EVENT_SERVICE_ID = 3299; constexpr const char* UID = "uid"; @@ -61,6 +62,9 @@ void CommonEventCollect::CleanFfrt() if (workHandler_ != nullptr) { workHandler_->CleanFfrt(); } + if (unsubHandler_ != nullptr) { + unsubHandler_->CleanFfrt(); + } } void CommonEventCollect::SetFfrt() @@ -68,6 +72,9 @@ void CommonEventCollect::SetFfrt() if (workHandler_ != nullptr) { workHandler_->SetFfrt(); } + if (unsubHandler_ != nullptr) { + unsubHandler_->SetFfrt(); + } } int32_t CommonEventCollect::OnStart() @@ -79,6 +86,7 @@ int32_t CommonEventCollect::OnStart() } workHandler_ = std::make_shared(this); + unsubHandler_ = std::make_shared(this); workHandler_->SendEvent(INIT_EVENT); return ERR_OK; } @@ -88,6 +96,9 @@ int32_t CommonEventCollect::OnStop() if (workHandler_ != nullptr) { workHandler_ = nullptr; } + if (unsubHandler_ != nullptr) { + unsubHandler_ = nullptr; + } return ERR_OK; } @@ -178,24 +189,30 @@ bool CommonEventCollect::CreateCommonEventSubscriber() bool CommonEventCollect::CreateCommonEventSubscriberLocked() { int64_t begin = GetTickCount(); - if (commonEventSubscriber_ != nullptr) { - HILOGI("UnSubsComEvt start"); - { - SamgrXCollie samgrXCollie("samgr--UnSubscribeCommonEvent"); - bool isUnsubscribe = EventFwk::CommonEventManager::UnSubscribeCommonEvent(commonEventSubscriber_); - if (!isUnsubscribe) { - HILOGE("CreateCommonEventSubscriberLocked isUnsubscribe failed!"); - return false; - } - } - commonEventSubscriber_.reset(); - } EventFwk::MatchingSkills skill = EventFwk::MatchingSkills(); AddSkillsEvent(skill); EventFwk::CommonEventSubscribeInfo info(skill); + std::shared_ptr comEvtScrb = commonEventSubscriber_; commonEventSubscriber_ = std::make_shared(info, this); bool ret = EventFwk::CommonEventManager::SubscribeCommonEvent(commonEventSubscriber_); HILOGI("SubsComEvt %{public}" PRId64 "ms %{public}s", (GetTickCount() - begin), ret ? "suc" : "fail"); + if (comEvtScrb != nullptr) { + auto unsubTask = [comEvtScrb]() { + HILOGI("UnSubsComEvt start"); + { + SamgrXCollie samgrXCollie("samgr--UnSubscribeCommonEvent"); + bool isUnsubscribe = EventFwk::CommonEventManager::UnSubscribeCommonEvent(comEvtScrb); + if (!isUnsubscribe) { + HILOGE("CreateCommonEventSubscriberLocked isUnsubscribe failed!"); + } + } + }; + if (unsubHandler_ != nullptr) { + unsubHandler_->PostTask(unsubTask, UNSUB_DELAY_TIME); + } else { + HILOGE("CreateCommonEventSubscriberLocked unsubHandler is null!"); + } + } return ret; } @@ -508,6 +525,14 @@ int32_t CommonEventCollect::RemoveUnusedEvent(const OnDemandEvent& event) return ERR_OK; } +bool CommonHandler::PostTask(std::function func, uint64_t delayTime) +{ + if (handler_ == nullptr) { + HILOGE("CommonEventCollect PostTask handler is null!"); + return false; + } + return handler_->PostTask(func, delayTime); +} void CommonHandler::CleanFfrt() { if (handler_ != nullptr) { @@ -591,4 +616,4 @@ void CommonEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData& data OnDemandEvent event = {COMMON_EVENT, action, std::to_string(code), extraDataId}; collect->ReportEvent(event); } -} // namespace OHOS \ No newline at end of file +} // namespace OHOS -- Gitee