From 7242ed6a2f903611f9454a71c853875a319a13b3 Mon Sep 17 00:00:00 2001 From: fengyang Date: Mon, 25 Nov 2024 20:12:27 +0800 Subject: [PATCH] push Signed-off-by: fengyang --- services/common/src/bundle_active_core.cpp | 38 +++++++++++++--- .../src/bundle_active_group_controller.cpp | 43 ++++++++++++++----- .../src/bundle_active_user_history.cpp | 12 +++++- 3 files changed, 74 insertions(+), 19 deletions(-) diff --git a/services/common/src/bundle_active_core.cpp b/services/common/src/bundle_active_core.cpp index 1d14e9a..5840a3a 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -204,7 +204,11 @@ void BundleActiveCommonEventSubscriber::HandleLockEvent(const CommonEventData &d sptr timer = MiscServices::TimeServiceClient::GetInstance(); tmpHandlerObject.event_.timeStamp_ = timer->GetBootTimeMs(); auto handlerobjToPtr = std::make_shared(tmpHandlerObject); - bundleActiveReportHandler_.lock()->SendEvent(BundleActiveReportHandler::MSG_REPORT_EVENT, handlerobjToPtr); + auto reportHandler = bundleActiveReportHandler_.lock(); + if (reportHandler == nullptr) { + return; + } + reportHandler->SendEvent(BundleActiveReportHandler::MSG_REPORT_EVENT, handlerobjToPtr); } void BundleActiveCore::RegisterSubscriber() @@ -377,9 +381,13 @@ void BundleActiveCore::OnStatsChanged(const int32_t userId) tmpHandlerObject.userId_ = userId; std::shared_ptr handlerobjToPtr = std::make_shared(tmpHandlerObject); - if (handler_.lock()->HasEvent(BundleActiveReportHandler::MSG_FLUSH_TO_DISK) == false) { + auto reportHandler = handler_.lock(); + if (reportHandler == nullptr) { + return; + } + if (reportHandler->->HasEvent(BundleActiveReportHandler::MSG_FLUSH_TO_DISK) == false) { BUNDLE_ACTIVE_LOGI("OnStatsChanged send flush to disk event for user %{public}d", userId); - handler_.lock()->SendEvent(BundleActiveReportHandler::MSG_FLUSH_TO_DISK, handlerobjToPtr, flushInterval_); + reportHandler->->SendEvent(BundleActiveReportHandler::MSG_FLUSH_TO_DISK, handlerobjToPtr, flushInterval_); } } } @@ -402,8 +410,12 @@ void BundleActiveCore::RestoreAllData() bundleGroupController_->RestoreDurationToDatabase(); } if (!handler_.expired()) { + auto reportHandler = handler_.lock(); + if (reportHandler == nullptr) { + return; + } BUNDLE_ACTIVE_LOGI("RestoreAllData remove flush to disk event"); - handler_.lock()->RemoveEvent(BundleActiveReportHandler::MSG_FLUSH_TO_DISK); + reportHandler->RemoveEvent(BundleActiveReportHandler::MSG_FLUSH_TO_DISK); } } @@ -432,8 +444,12 @@ void BundleActiveCore::RestoreToDatabaseLocked(const int32_t userId) bundleGroupController_->RestoreDurationToDatabase(); } if (!handler_.expired()) { + auto reportHandler = handler_.lock(); + if (reportHandler == nullptr) { + return; + } BUNDLE_ACTIVE_LOGI("RestoreToDatabaseLocked remove flush to disk event"); - handler_.lock()->RemoveEvent(BundleActiveReportHandler::MSG_FLUSH_TO_DISK); + reportHandler->RemoveEvent(BundleActiveReportHandler::MSG_FLUSH_TO_DISK); } } @@ -454,7 +470,11 @@ void BundleActiveCore::PreservePowerStateInfo(const int32_t eventId) tmpHandlerObject.event_.timeStamp_ = timer->GetBootTimeMs(); std::shared_ptr handlerobjToPtr = std::make_shared(tmpHandlerObject); - handler_.lock()->SendEvent(BundleActiveReportHandler::MSG_REPORT_EVENT, handlerobjToPtr); + auto reportHandler = handler_.lock(); + if (reportHandler == nullptr) { + return; + } + reportHandler->SendEvent(BundleActiveReportHandler::MSG_REPORT_EVENT, handlerobjToPtr); } } @@ -517,8 +537,12 @@ int64_t WEAK_FUNC BundleActiveCore::CheckTimeChangeAndGetWallTime(int32_t userId it->second->LoadActiveStats(actualSystemTime, true, true); it->second->LoadModuleAndFormStats(); if (!handler_.expired()) { + auto reportHandler = handler_.lock(); + if (reportHandler == nullptr) { + return; + } BUNDLE_ACTIVE_LOGI("CheckTimeChangeAndGetWallTime remove flush to disk event"); - handler_.lock()->RemoveEvent(BundleActiveReportHandler::MSG_FLUSH_TO_DISK); + reportHandler->RemoveEvent(BundleActiveReportHandler::MSG_FLUSH_TO_DISK); } } realTimeShot_ = actualRealTime; diff --git a/services/packagegroup/src/bundle_active_group_controller.cpp b/services/packagegroup/src/bundle_active_group_controller.cpp index 0c36e11..3554567 100644 --- a/services/packagegroup/src/bundle_active_group_controller.cpp +++ b/services/packagegroup/src/bundle_active_group_controller.cpp @@ -69,7 +69,11 @@ void BundleActiveGroupController::OnUserRemoved(const int32_t userId) std::lock_guard lock(mutex_); bundleUserHistory_->userHistory_.erase(userId); if (!activeGroupHandler_.expired()) { - activeGroupHandler_.lock()->RemoveEvent(BundleActiveGroupHandler::MSG_CHECK_IDLE_STATE); + auto groupHandler = activeGroupHandler_.lock(); + if (groupHandler == nullptr) { + return; + } + groupHandler->RemoveEvent(BundleActiveGroupHandler::MSG_CHECK_IDLE_STATE); } } @@ -79,12 +83,15 @@ void BundleActiveGroupController::OnUserSwitched(const int32_t userId, const int CheckEachBundleState(currentUsedUser); bundleUserHistory_->WriteBundleUsage(currentUsedUser); std::lock_guard lock(mutex_); - auto activeGroupHandler = activeGroupHandler_.lock(); if (!activeGroupHandler_.expired()) { - activeGroupHandler_.lock()->RemoveEvent(BundleActiveGroupHandler::MSG_CHECK_IDLE_STATE); - activeGroupHandler_.lock()->RemoveEvent(BundleActiveGroupHandler::MSG_CHECK_DEFAULT_BUNDLE_STATE); - activeGroupHandler_.lock()->RemoveEvent(BundleActiveGroupHandler::MSG_CHECK_NOTIFICATION_SEEN_BUNDLE_STATE); - activeGroupHandler_.lock()->RemoveEvent(BundleActiveGroupHandler::MSG_CHECK_SYSTEM_INTERACTIVE_BUNDLE_STATE); + auto groupHandler = activeGroupHandler_.lock(); + if (groupHandler == nullptr) { + return; + } + groupHandler->RemoveEvent(BundleActiveGroupHandler::MSG_CHECK_IDLE_STATE); + groupHandler->RemoveEvent(BundleActiveGroupHandler::MSG_CHECK_DEFAULT_BUNDLE_STATE); + groupHandler->RemoveEvent(BundleActiveGroupHandler::MSG_CHECK_NOTIFICATION_SEEN_BUNDLE_STATE); + groupHandler->RemoveEvent(BundleActiveGroupHandler::MSG_CHECK_SYSTEM_INTERACTIVE_BUNDLE_STATE); } PeriodCheckBundleState(userId); } @@ -92,8 +99,12 @@ void BundleActiveGroupController::OnUserSwitched(const int32_t userId, const int void BundleActiveGroupController::OnScreenChanged(const bool& isScreenOn, const int64_t bootFromTimeStamp) { if (!activeGroupHandler_.expired()) { + auto groupHandler = activeGroupHandler_.lock(); + if (groupHandler == nullptr) { + return; + } std::shared_ptr bundleActiveGroupController = shared_from_this(); - activeGroupHandler_.lock()->PostTask([bundleActiveGroupController, isScreenOn, bootFromTimeStamp]() { + groupHandler->PostTask([bundleActiveGroupController, isScreenOn, bootFromTimeStamp]() { std::lock_guard lock(bundleActiveGroupController->mutex_); bundleActiveGroupController->bundleUserHistory_->UpdateBootBasedAndScreenTime(isScreenOn, bootFromTimeStamp); @@ -181,7 +192,11 @@ void BundleActiveGroupController::PeriodCheckBundleState(const int32_t userId) tmpGroupHandlerObj.userId_ = userId; std::shared_ptr handlerobjToPtr = std::make_shared(tmpGroupHandlerObj); - activeGroupHandler_.lock()->SendEvent(BundleActiveGroupHandler::MSG_CHECK_DEFAULT_BUNDLE_STATE, + auto groupHandler = activeGroupHandler_.lock(); + if (groupHandler == nullptr) { + return; + } + groupHandler->SendEvent(BundleActiveGroupHandler::MSG_CHECK_DEFAULT_BUNDLE_STATE, handlerobjToPtr, FIVE_SECOND); } } @@ -212,7 +227,11 @@ void BundleActiveGroupController::CheckIdleStatsOneTime() BundleActiveGroupHandler::MSG_ONE_TIME_CHECK_BUNDLE_STATE); auto activeGroupHandler = activeGroupHandler_.lock(); if (!activeGroupHandler_.expired()) { - activeGroupHandler_.lock()->SendEvent(BundleActiveGroupHandler::MSG_ONE_TIME_CHECK_BUNDLE_STATE, + auto groupHandler = activeGroupHandler_.lock(); + if (groupHandler == nullptr) { + return; + } + groupHandler->SendEvent(BundleActiveGroupHandler::MSG_ONE_TIME_CHECK_BUNDLE_STATE, handlerobjToPtr); } } @@ -294,7 +313,11 @@ void BundleActiveGroupController::SendCheckBundleMsg(const BundleActiveEvent& ev std::shared_ptr handlerobjToPtr = std::make_shared(tmpGroupHandlerObj); if (!activeGroupHandler_.expired()) { - activeGroupHandler_.lock()->SendCheckBundleMsg(checkBundleMsgEventId, handlerobjToPtr, timeUntilNextCheck); + auto groupHandler = activeGroupHandler_.lock(); + if (groupHandler == nullptr) { + return; + } + groupHandler->SendCheckBundleMsg(checkBundleMsgEventId, handlerobjToPtr, timeUntilNextCheck); } } diff --git a/services/packagegroup/src/bundle_active_user_history.cpp b/services/packagegroup/src/bundle_active_user_history.cpp index 7a4f678..e8348f5 100644 --- a/services/packagegroup/src/bundle_active_user_history.cpp +++ b/services/packagegroup/src/bundle_active_user_history.cpp @@ -213,8 +213,12 @@ void BundleActiveUserHistory::ReportUsage(shared_ptr userId, oldGroup, newGroup, oneBundleUsageHistory->reasonInGroup_, bundleName); BUNDLE_ACTIVE_LOGI("RegisterAppGroupCallBack AppGroupCallbackInfo build success"); if (!bundleActiveCore_.expired()) { + auto bundleActiveCore = activeGroupHandler_.lock(); + if (bundleActiveCore == nullptr) { + return; + } BUNDLE_ACTIVE_LOGD("RegisterAppGroupCallBack will callback!"); - bundleActiveCore_.lock()->OnAppGroupChanged(callbackInfo); + bundleActiveCore->OnAppGroupChanged(callbackInfo); } } } @@ -252,7 +256,11 @@ int32_t BundleActiveUserHistory::SetAppGroup(const string& bundleName, const int AppGroupCallbackInfo callbackInfo( userId, oldGroup, newGroup, oneBundleHistory->reasonInGroup_, bundleName); if (!bundleActiveCore_.expired()) { - bundleActiveCore_.lock()->OnAppGroupChanged(callbackInfo); + auto bundleActiveCore = activeGroupHandler_.lock(); + if (bundleActiveCore == nullptr) { + return; + } + bundleActiveCore->OnAppGroupChanged(callbackInfo); } } return ERR_OK; -- Gitee