From 07aa4764a39bac0b72e7868df49ec6a5e1f6a54a Mon Sep 17 00:00:00 2001 From: fengyang Date: Thu, 6 Jun 2024 19:53:15 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=B8=B8=E9=A9=BB=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fengyang --- BUILD.gn | 2 + services/common/include/bundle_active_core.h | 2 + .../common/include/bundle_active_service.h | 4 +- services/common/src/bundle_active_core.cpp | 24 ++-- services/common/src/bundle_active_service.cpp | 37 +++--- .../include/bundle_active_group_controller.h | 5 +- .../include/bundle_active_group_handler.h | 22 ++-- .../src/bundle_active_group_controller.cpp | 63 +++++----- .../src/bundle_active_group_handler.cpp | 109 +++++++++++++----- .../appgroupcallbackstub_fuzzer/BUILD.gn | 1 + .../bundleactiveobserver_fuzzer/BUILD.gn | 1 + .../BUILD.gn | 1 + .../BUILD.gn | 1 + test/unittest/BUILD.gn | 6 + .../device_usage_statistics_mock_test.cpp | 4 + .../device_usage_statistics_service_test.cpp | 5 + 16 files changed, 181 insertions(+), 106 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index 88c0693..551975c 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -204,6 +204,7 @@ ohos_shared_library("usagestatservice") { "c_utils:utils", "common_event_service:cesfwk_innerkits", "eventhandler:libeventhandler", + "ffrt:libffrt", "hilog:libhilog", "init:libbegetutil", "ipc:ipc_single", @@ -286,6 +287,7 @@ ohos_static_library("usagestatservice_static") { "c_utils:utils", "common_event_service:cesfwk_innerkits", "eventhandler:libeventhandler", + "ffrt:libffrt", "hilog:libhilog", "init:libbegetutil", "ipc:ipc_single", diff --git a/services/common/include/bundle_active_core.h b/services/common/include/bundle_active_core.h index 07b2cef..f2e10eb 100644 --- a/services/common/include/bundle_active_core.h +++ b/services/common/include/bundle_active_core.h @@ -118,6 +118,8 @@ public: */ void Init(); + void DeInit(); + /* * function: InitBundleGroupController, BundleAciveService call it to init bundleGroupController_ object, * set its handler and subscribe needed common event. diff --git a/services/common/include/bundle_active_service.h b/services/common/include/bundle_active_service.h index a8aea65..5532835 100644 --- a/services/common/include/bundle_active_service.h +++ b/services/common/include/bundle_active_service.h @@ -33,6 +33,7 @@ #include "file_ex.h" #include "string_ex.h" #include "system_ability.h" +#include "ffrt.h" namespace OHOS { namespace DeviceUsageStats { @@ -220,8 +221,7 @@ private: sptr shutdownCallback_; sptr powerStateCallback_; #endif - std::shared_ptr runner_; - std::shared_ptr handler_; + std::shared_ptr ffrtQueue_; bool ready_ {false}; int32_t ConvertIntervalType(const int32_t intervalType); void InitNecessaryState(); diff --git a/services/common/src/bundle_active_core.cpp b/services/common/src/bundle_active_core.cpp index 4f22b9d..98eec4b 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -181,16 +181,20 @@ void BundleActiveCore::Init() BUNDLE_ACTIVE_LOGD("system time shot is %{public}lld", (long long)systemTimeShot_); } +void BundleActiveCore::DeInit() +{ + if (bundleGroupHandler_ != nullptr) { + bundleGroupHandler_->DeInit(); + } + if (bundleGroupController_ != nullptr) { + bundleGroupController_.reset(); + } +} + void BundleActiveCore::InitBundleGroupController() { BUNDLE_ACTIVE_LOGD("InitBundleGroupController called"); - std::string threadName = "bundle_active_group_handler"; - auto runner = AppExecFwk::EventRunner::Create(threadName); - if (runner == nullptr) { - BUNDLE_ACTIVE_LOGE("report handler is null"); - return; - } - bundleGroupHandler_ = std::make_shared(runner, debugCore_); + bundleGroupHandler_ = std::make_shared(debugCore_); if (bundleGroupHandler_ == nullptr) { return; } @@ -800,8 +804,10 @@ void BundleActiveCore::OnObserverDied(const wptr &remote) BUNDLE_ACTIVE_LOGE("remote object is null."); return; } - - bundleGroupHandler_->PostSyncTask([this, &remote]() { this->OnObserverDiedInner(remote); }); + std::shared_ptr bundleActiveCore = shared_from_this(); + bundleGroupHandler_->SubmitTask([bundleActiveCore, &remote]() { + bundleActiveCore->OnObserverDiedInner(remote); + }); } void BundleActiveCore::OnObserverDiedInner(const wptr &remote) diff --git a/services/common/src/bundle_active_service.cpp b/services/common/src/bundle_active_service.cpp index 633bea2..4ffeb0b 100644 --- a/services/common/src/bundle_active_service.cpp +++ b/services/common/src/bundle_active_service.cpp @@ -48,6 +48,7 @@ static constexpr int32_t NO_DUMP_PARAM_NUMS = 0; const int32_t PACKAGE_USAGE_PARAM = 6; const int32_t MODULE_USAGE_PARAM = 4; const std::string NEEDED_PERMISSION = "ohos.permission.BUNDLE_ACTIVE_INFO"; +const std::string DEVICE_USAGE_INIT_QUEUE = "DeviceUsageStatsInitQueue"; const int32_t ENG_MODE = OHOS::system::GetIntParameter("const.debuggable", 0); const bool REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(DelayedSingleton::GetInstance().get()); @@ -67,18 +68,12 @@ void BundleActiveService::OnStart() BUNDLE_ACTIVE_LOGI("service is ready. nothing to do."); return; } - runner_ = AppExecFwk::EventRunner::Create("device_usage_stats_init_handler"); - if (runner_ == nullptr) { - BUNDLE_ACTIVE_LOGI("BundleActiveService runner create failed!"); - return; - } - handler_ = std::make_shared(runner_); - if (handler_ == nullptr) { - BUNDLE_ACTIVE_LOGI("BundleActiveService handler create failed!"); - return; - } - auto registerTask = [this]() { this->InitNecessaryState(); }; - handler_->PostSyncTask(registerTask); + ffrtQueue_ = std::make_shared(DEVICE_USAGE_INIT_QUEUE.c_str(), + ffrt::queue_attr().qos(ffrt::qos_user_initiated)); + std::shared_ptr service = shared_from_this(); + ffrtQueue_->submit([service]() { + service->InitNecessaryState(); + }); } void BundleActiveService::InitNecessaryState() @@ -91,16 +86,20 @@ void BundleActiveService::InitNecessaryState() = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (systemAbilityManager == nullptr) { BUNDLE_ACTIVE_LOGI("GetSystemAbilityManager fail!"); - auto task = [this]() { this->InitNecessaryState(); }; - handler_->PostTask(task, DELAY_TIME); + std::shared_ptr service = shared_from_this(); + ffrtQueue_->submit([service]() { + service->InitNecessaryState(); + }); return; } for (const auto& serviceItem : serviceIdSets) { auto checkResult = systemAbilityManager->CheckSystemAbility(serviceItem); if (!checkResult) { BUNDLE_ACTIVE_LOGI("request system service is not ready yet!"); - auto task = [this]() { this->InitNecessaryState(); }; - handler_->PostTask(task, DELAY_TIME); + std::shared_ptr service = shared_from_this(); + ffrtQueue_->submit([service]() { + service->InitNecessaryState(); + }); return; } } @@ -109,8 +108,10 @@ void BundleActiveService::InitNecessaryState() auto getAbility = systemAbilityManager->GetSystemAbility(serviceItem); if (!getAbility) { BUNDLE_ACTIVE_LOGI("request system service object is not ready yet!"); - auto task = [this]() { this->InitNecessaryState(); }; - handler_->PostTask(task, DELAY_TIME); + std::shared_ptr service = shared_from_this(); + ffrtQueue_->submit([service]() { + service->InitNecessaryState(); + }); return; } } diff --git a/services/packagegroup/include/bundle_active_group_controller.h b/services/packagegroup/include/bundle_active_group_controller.h index 057730c..816963d 100644 --- a/services/packagegroup/include/bundle_active_group_controller.h +++ b/services/packagegroup/include/bundle_active_group_controller.h @@ -28,6 +28,7 @@ #include "bundle_active_user_history.h" #include "bundle_state_inner_errors.h" #include "bundle_mgr_interface.h" +#include "ffrt.h" namespace OHOS { namespace DeviceUsageStats { @@ -81,9 +82,9 @@ public: void OnUserSwitched(const int32_t userId, const int32_t currentUsedUser); private: - std::mutex mutex_; + ffrt::mutex mutex_; bool GetBundleMgrProxy(); - std::weak_ptr activeGroupHandler_; + std::shared_ptr activeGroupHandler_; int64_t timeoutForDirectlyUse_; int64_t timeoutForNotifySeen_; int64_t timeoutForSystemInteraction_; diff --git a/services/packagegroup/include/bundle_active_group_handler.h b/services/packagegroup/include/bundle_active_group_handler.h index 921ab9c..d421c90 100644 --- a/services/packagegroup/include/bundle_active_group_handler.h +++ b/services/packagegroup/include/bundle_active_group_handler.h @@ -16,17 +16,17 @@ #ifndef BUNDLE_ACTIVE_GROUP_HANDLER_H #define BUNDLE_ACTIVE_GROUP_HANDLER_H -#include "event_handler.h" -#include "event_runner.h" - #include "ibundle_active_service.h" #include "bundle_active_group_controller.h" #include "bundle_active_group_common.h" +#include "ffrt.h" +#include namespace OHOS { namespace DeviceUsageStats { class BundleActiveGroupHandlerObject { public: + int32_t eventId_; std::string bundleName_; int32_t userId_; int32_t uid_; @@ -35,24 +35,32 @@ public: ~BundleActiveGroupHandlerObject() {} }; -class BundleActiveGroupHandler : public AppExecFwk::EventHandler { +class BundleActiveGroupHandler : public std::enable_shared_from_this { public: - explicit BundleActiveGroupHandler(const std::shared_ptr &runner, const bool debug); + explicit BundleActiveGroupHandler(const bool debug); ~BundleActiveGroupHandler() = default; /** - * Process the event. Developers should override this method. + * Send the event. Developers should override this method. * * @param event The event should be processed. */ - void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; + void SendEvent(const BundleActiveGroupHandlerObject& tmpHandlerobj, const int64_t delayTime = 0); + void RemoveEvent(const int32_t eventId); + void SubmitTask(std::function&& func); void Init(const std::shared_ptr& bundleActiveController); + void DeInit(); static const int32_t MSG_CHECK_BUNDLE_STATE = 0; static const int32_t MSG_ONE_TIME_CHECK_BUNDLE_STATE = 1; static const int32_t MSG_CHECK_IDLE_STATE = 2; int64_t checkIdleInterval_; private: + void ProcessCheckBundleState(const BundleActiveGroupHandlerObject& tmpHandlerobj, const int64_t delayTime); + void ProcessOneTimecheckBundleState(const BundleActiveGroupHandlerObject& tmpHandlerobj, const int64_t delayTime); + void ProcessCheckIdleState(const BundleActiveGroupHandlerObject& tmpHandlerobj, const int64_t delayTime); std::shared_ptr bundleActiveGroupController_; + std::shared_ptr ffrtQueue_ = nullptr; + std::map ffrtHandleMap_; }; } // namespace DeviceUsageStats } // namespace OHOS diff --git a/services/packagegroup/src/bundle_active_group_controller.cpp b/services/packagegroup/src/bundle_active_group_controller.cpp index 4b6f08e..03ebf26 100644 --- a/services/packagegroup/src/bundle_active_group_controller.cpp +++ b/services/packagegroup/src/bundle_active_group_controller.cpp @@ -52,23 +52,22 @@ BundleActiveGroupController::BundleActiveGroupController(const bool debug) void BundleActiveGroupController::RestoreDurationToDatabase() { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); bundleUserHistory_->WriteDeviceDuration(); } void BundleActiveGroupController::RestoreToDatabase(const int32_t userId) { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); bundleUserHistory_->WriteBundleUsage(userId); } void BundleActiveGroupController::OnUserRemoved(const int32_t userId) { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); bundleUserHistory_->userHistory_.erase(userId); - auto activeGroupHandler = activeGroupHandler_.lock(); - if (activeGroupHandler) { - activeGroupHandler->RemoveEvent(BundleActiveGroupHandler::MSG_CHECK_IDLE_STATE); + if (activeGroupHandler_ != nullptr) { + activeGroupHandler_->RemoveEvent(BundleActiveGroupHandler::MSG_CHECK_IDLE_STATE); } } @@ -77,18 +76,17 @@ void BundleActiveGroupController::OnUserSwitched(const int32_t userId, const int BUNDLE_ACTIVE_LOGI("last time check for user %{public}d", currentUsedUser); CheckEachBundleState(currentUsedUser); bundleUserHistory_->WriteBundleUsage(currentUsedUser); - std::lock_guard lock(mutex_); - auto activeGroupHandler = activeGroupHandler_.lock(); - if (activeGroupHandler) { - activeGroupHandler->RemoveEvent(BundleActiveGroupHandler::MSG_CHECK_IDLE_STATE); - activeGroupHandler->RemoveEvent(BundleActiveGroupHandler::MSG_CHECK_BUNDLE_STATE); + std::lock_guard lock(mutex_); + if (activeGroupHandler_ != nullptr) { + activeGroupHandler_->RemoveEvent(BundleActiveGroupHandler::MSG_CHECK_IDLE_STATE); + activeGroupHandler_->RemoveEvent(BundleActiveGroupHandler::MSG_CHECK_BUNDLE_STATE); } PeriodCheckBundleState(userId); } void BundleActiveGroupController::OnScreenChanged(const bool& isScreenOn, const int64_t bootFromTimeStamp) { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); bundleUserHistory_->UpdateBootBasedAndScreenTime(isScreenOn, bootFromTimeStamp); } @@ -108,7 +106,7 @@ void BundleActiveGroupController::SetHandlerAndCreateUserHistory( void BundleActiveGroupController::OnBundleUninstalled(const int32_t userId, const std::string bundleName, const int32_t uid, const int32_t appIndex) { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); BUNDLE_ACTIVE_LOGI("OnBundleUninstalled called, userId is %{public}d, bundlename is %{public}s", userId, bundleName.c_str()); auto oneUserHistory = bundleUserHistory_->GetUserHistory(userId, false); @@ -150,15 +148,11 @@ bool BundleActiveGroupController::GetBundleMgrProxy() void BundleActiveGroupController::PeriodCheckBundleState(const int32_t userId) { BUNDLE_ACTIVE_LOGI("PeriodCheckBundleState called"); - auto activeGroupHandler = activeGroupHandler_.lock(); - if (activeGroupHandler) { + if (activeGroupHandler_ != nullptr) { BundleActiveGroupHandlerObject tmpGroupHandlerObj; tmpGroupHandlerObj.userId_ = userId; - std::shared_ptr handlerobjToPtr = - std::make_shared(tmpGroupHandlerObj); - auto handlerEvent = AppExecFwk::InnerEvent::Get(BundleActiveGroupHandler::MSG_CHECK_IDLE_STATE, - handlerobjToPtr); - activeGroupHandler->SendEvent(handlerEvent, FIVE_SECOND); + tmpGroupHandlerObj.eventId_ = BundleActiveGroupHandler::MSG_CHECK_IDLE_STATE; + activeGroupHandler_->SendEvent(tmpGroupHandlerObj, FIVE_SECOND); } } @@ -181,11 +175,10 @@ bool BundleActiveGroupController::CheckEachBundleState(const int32_t userId) void BundleActiveGroupController::CheckIdleStatsOneTime() { - auto handlerEvent = AppExecFwk::InnerEvent::Get( - BundleActiveGroupHandler::MSG_ONE_TIME_CHECK_BUNDLE_STATE); - auto activeGroupHandler = activeGroupHandler_.lock(); - if (activeGroupHandler) { - activeGroupHandler->SendEvent(handlerEvent); + BundleActiveGroupHandlerObject tmpGroupHandlerObj; + tmpGroupHandlerObj.eventId_ = BundleActiveGroupHandler::MSG_ONE_TIME_CHECK_BUNDLE_STATE; + if (activeGroupHandler_ != nullptr) { + activeGroupHandler_->SendEvent(handlerEvent); } } @@ -217,7 +210,7 @@ void BundleActiveGroupController::ReportEvent(const BundleActiveEvent& event, co if (bundleGroupEnable_ == false) { return; } - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); if (IsBundleInstalled(event.bundleName_, userId) == false) { BUNDLE_ACTIVE_LOGE("Report an uninstalled package event, return!"); return; @@ -253,13 +246,9 @@ void BundleActiveGroupController::ReportEvent(const BundleActiveEvent& event, co tmpGroupHandlerObj.userId_ = userId; tmpGroupHandlerObj.bundleName_ = event.bundleName_; tmpGroupHandlerObj.uid_ = event.uid_; - std::shared_ptr handlerobjToPtr = - std::make_shared(tmpGroupHandlerObj); - auto handlerEvent = AppExecFwk::InnerEvent::Get(BundleActiveGroupHandler::MSG_CHECK_BUNDLE_STATE, - handlerobjToPtr); - auto activeGroupHandler = activeGroupHandler_.lock(); - if (activeGroupHandler) { - activeGroupHandler->SendEvent(handlerEvent, timeUntilNextCheck); + tmpGroupHandlerObj.eventId_ = BundleActiveGroupHandler::MSG_CHECK_BUNDLE_STATE; + if (activeGroupHandler_ != nullptr) { + activeGroupHandler_->SendEvent(tmpGroupHandlerObj, timeUntilNextCheck); } } } @@ -267,7 +256,7 @@ void BundleActiveGroupController::ReportEvent(const BundleActiveEvent& event, co void BundleActiveGroupController::CheckAndUpdateGroup(const std::string& bundleName, const int32_t userId, const int32_t uid, const int64_t bootBasedTimeStamp) { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); auto oneBundleHistory = bundleUserHistory_->GetUsageHistoryForBundle(bundleName, userId, bootBasedTimeStamp, true, uid); if (oneBundleHistory == nullptr) { @@ -313,7 +302,7 @@ void BundleActiveGroupController::CheckAndUpdateGroup(const std::string& bundleN ErrCode BundleActiveGroupController::SetAppGroup(const std::string& bundleName, const int32_t userId, int32_t newGroup, uint32_t reason, const int64_t bootBasedTimeStamp, const bool isFlush) { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); if (!IsBundleInstalled(bundleName, userId)) { return ERR_NO_APP_GROUP_INFO_IN_DATABASE; } @@ -344,7 +333,7 @@ ErrCode BundleActiveGroupController::SetAppGroup(const std::string& bundleName, int32_t BundleActiveGroupController::IsBundleIdle(const std::string& bundleName, const int32_t userId) { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); sptr timer = MiscServices::TimeServiceClient::GetInstance(); if (IsBundleInstalled(bundleName, userId) == false) { return -1; @@ -375,7 +364,7 @@ int32_t BundleActiveGroupController::IsBundleIdle(const std::string& bundleName, ErrCode BundleActiveGroupController::QueryAppGroup(int32_t& appGroup, const std::string& bundleName, const int32_t userId) { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); if (bundleName.empty()) { BUNDLE_ACTIVE_LOGE("bundleName can not get by userId"); return ERR_NO_APP_GROUP_INFO_IN_DATABASE; diff --git a/services/packagegroup/src/bundle_active_group_handler.cpp b/services/packagegroup/src/bundle_active_group_handler.cpp index a4932c2..32a7bf7 100644 --- a/services/packagegroup/src/bundle_active_group_handler.cpp +++ b/services/packagegroup/src/bundle_active_group_handler.cpp @@ -20,6 +20,7 @@ namespace OHOS { namespace DeviceUsageStats { +const std::string DEVICE_USAGE_GROUP_HANDLER_QUEUE = "DeviceUsageGroupHandlerQueue"; #ifndef OS_ACCOUNT_PART_ENABLED namespace { constexpr int32_t DEFAULT_OS_ACCOUNT_ID = 0; // 0 is the default id when there is no os_account part @@ -30,10 +31,10 @@ BundleActiveGroupHandlerObject::BundleActiveGroupHandlerObject(const BundleActiv bundleName_ = orig.bundleName_; userId_ = orig.userId_; uid_ = orig.uid_; + eventId_ = orig.eventId_; } -BundleActiveGroupHandler::BundleActiveGroupHandler - (const std::shared_ptr &runner, const bool debug) : AppExecFwk::EventHandler(runner) +BundleActiveGroupHandler::BundleActiveGroupHandler(const bool debug) { if (debug) { checkIdleInterval_ = ONE_MINUTE; @@ -50,48 +51,34 @@ void BundleActiveGroupHandler::Init(const std::shared_ptr(DEVICE_USAGE_GROUP_HANDLER_QUEUE.c_str(), + ffrt::queue_attr().qos(ffrt::qos_user_initiated)); } -void BundleActiveGroupHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) +void BundleActiveGroupHandler::DeInit() { - if (event == nullptr) { + bundleActiveGroupController_ = nullptr; + if (ffrtQueue_ != nullptr) { + ffrtQueue_.reset(); + } +} + +void BundleActiveGroupHandler::SendEvent(const BundleActiveGroupHandlerObject& tmpHandlerobj, const int64_t delayTime) +{ + if (event == nullptr || ffrtQueue_ == nullptr) { return; } switch (event->GetInnerEventId()) { case MSG_CHECK_BUNDLE_STATE: { - auto ptrToHandlerobj = event->GetSharedObject(); - BundleActiveGroupHandlerObject tmpHandlerobj = *ptrToHandlerobj; - sptr timer = MiscServices::TimeServiceClient::GetInstance(); - int64_t bootBasedTimeStamp = timer->GetBootTimeMs(); - bundleActiveGroupController_->CheckAndUpdateGroup( - tmpHandlerobj.bundleName_, tmpHandlerobj.userId_, tmpHandlerobj.uid_, bootBasedTimeStamp); - bundleActiveGroupController_->RestoreToDatabase(tmpHandlerobj.userId_); + ProcessCheckBundleState(tmpHandlerobj, delayTime); break; } case MSG_ONE_TIME_CHECK_BUNDLE_STATE: { - std::vector activatedOsAccountIds; - BundleActiveAccountHelper::GetActiveUserId(activatedOsAccountIds); - for (uint32_t i = 0; i < activatedOsAccountIds.size(); i++) { - bundleActiveGroupController_->CheckEachBundleState(activatedOsAccountIds[i]); - bundleActiveGroupController_->RestoreToDatabase(activatedOsAccountIds[i]); - } - RemoveEvent(MSG_ONE_TIME_CHECK_BUNDLE_STATE); + ProcessOneTimecheckBundleState(tmpHandlerobj, delayTime); break; } case MSG_CHECK_IDLE_STATE: { - auto ptrToHandlerobj = event->GetSharedObject(); - BundleActiveGroupHandlerObject tmpHandlerobj = *ptrToHandlerobj; - if (bundleActiveGroupController_->CheckEachBundleState(tmpHandlerobj.userId_) && - bundleActiveGroupController_->bundleGroupEnable_) { - BundleActiveGroupHandlerObject GroupHandlerObj; - GroupHandlerObj.userId_ = tmpHandlerobj.userId_; - std::shared_ptr handlerobjToPtr = - std::make_shared(GroupHandlerObj); - auto handlerEvent = AppExecFwk::InnerEvent::Get(BundleActiveGroupHandler::MSG_CHECK_IDLE_STATE, - handlerobjToPtr); - bundleActiveGroupController_->RestoreToDatabase(GroupHandlerObj.userId_); - SendEvent(handlerEvent, checkIdleInterval_); - } + ProcessCheckIdleState(tmpHandlerobj, delayTime); break; } default: { @@ -99,6 +86,66 @@ void BundleActiveGroupHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointe } } } + +void BundleActiveGroupHandler::ProcessCheckBundleState( + const BundleActiveGroupHandlerObject& tmpHandlerobj, const int64_t delayTime) +{ + std::shared_ptr bundleActiveGroupHandler = shared_from_this(); + ffrtHandleMap_[MSG_CHECK_BUNDLE_STATE] = ffrtQueue_->submit_h([bundleActiveGroupHandler, tmpHandlerobj]() { + sptr timer = MiscServices::TimeServiceClient::GetInstance(); + int64_t bootBasedTimeStamp = timer->GetBootTimeMs(); + bundleActiveGroupController_->CheckAndUpdateGroup(tmpHandlerobj.bundleName_, + tmpHandlerobj.userId_, tmpHandlerobj.uid_, bootBasedTimeStamp); + bundleActiveGroupController_->RestoreToDatabase(tmpHandlerobj.userId_); + }, ffrt::task_attr().delay(delayTime)); +} + +void BundleActiveGroupHandler::ProcessOneTimecheckBundleState( + const BundleActiveGroupHandlerObject& tmpHandlerobj, const int64_t delayTime) +{ + std::shared_ptr bundleActiveGroupHandler = shared_from_this(); + ffrtHandleMap_[MSG_ONE_TIME_CHECK_BUNDLE_STATE] = ffrtQueue_->submit_h([bundleActiveGroupHandler, tmpHandlerobj]() { + std::vector activatedOsAccountIds; + BundleActiveAccountHelper::GetActiveUserId(activatedOsAccountIds); + for (uint32_t i = 0; i < activatedOsAccountIds.size(); i++) { + bundleActiveGroupController_->CheckEachBundleState(activatedOsAccountIds[i]); + bundleActiveGroupController_->RestoreToDatabase(activatedOsAccountIds[i]); + } + RemoveEvent(MSG_ONE_TIME_CHECK_BUNDLE_STATE); + }, ffrt::task_attr().delay(delayTime)); +} + +void BundleActiveGroupHandler::ProcessCheckIdleState( + const BundleActiveGroupHandlerObject& tmpHandlerobj, const int64_t delayTime) +{ + + std::shared_ptr bundleActiveGroupHandler = shared_from_this(); + ffrtHandleMap_[MSG_CHECK_IDLE_STATE] = ffrtQueue_->submit_h([bundleActiveGroupHandler, tmpHandlerobj]() { + if (bundleActiveGroupController_->CheckEachBundleState(tmpHandlerobj.userId_) && + bundleActiveGroupController_->bundleGroupEnable_) { + BundleActiveGroupHandlerObject GroupHandlerObj; + GroupHandlerObj.userId_ = tmpHandlerobj.userId_; + GroupHandlerObj.eventId_ = BundleActiveGroupHandler::MSG_CHECK_IDLE_STATE; + bundleActiveGroupController_->RestoreToDatabase(GroupHandlerObj.userId_); + SendEvent(GroupHandlerObj, checkIdleInterval_); + } + }, ffrt::task_attr().delay(delayTime)); +} + +void BundleActiveGroupHandler::RemoveEvnet(const int32_t eventId) +{ + if (ffrtHandlerMap_.find(eventId) != ffrtHandleMap_.end()) { + ffrtQueue->cancel(ffrtHandleMap_[eventId]); + ffrtHandlerMap_.erase(eventId); + } +} + +void BundleActiveGroupHandler::SubmitTask(std::function&& func) +{ + ffrtQueue_->submit_h([func] { + func(); + }); +} } // namespace DeviceUsageStats } // namespace OHOS diff --git a/test/fuzztest/appgroupcallbackstub_fuzzer/BUILD.gn b/test/fuzztest/appgroupcallbackstub_fuzzer/BUILD.gn index eb9bb1f..e0bc1aa 100644 --- a/test/fuzztest/appgroupcallbackstub_fuzzer/BUILD.gn +++ b/test/fuzztest/appgroupcallbackstub_fuzzer/BUILD.gn @@ -63,6 +63,7 @@ ohos_fuzztest("AppgroupcallbackstubFuzzTest") { "c_utils:utils", "common_event_service:cesfwk_innerkits", "eventhandler:libeventhandler", + "ffrt:libffrt", "hilog:libhilog", "init:libbegetutil", "ipc:ipc_single", diff --git a/test/fuzztest/bundleactiveobserver_fuzzer/BUILD.gn b/test/fuzztest/bundleactiveobserver_fuzzer/BUILD.gn index e7d0611..d074932 100644 --- a/test/fuzztest/bundleactiveobserver_fuzzer/BUILD.gn +++ b/test/fuzztest/bundleactiveobserver_fuzzer/BUILD.gn @@ -66,6 +66,7 @@ ohos_fuzztest("BundleActiveObserverFuzzTest") { "c_utils:utils", "common_event_service:cesfwk_innerkits", "eventhandler:libeventhandler", + "ffrt:libffrt", "hilog:libhilog", "init:libbegetutil", "ipc:ipc_single", diff --git a/test/fuzztest/bundleactiveonremoterequest_fuzzer/BUILD.gn b/test/fuzztest/bundleactiveonremoterequest_fuzzer/BUILD.gn index 9c75314..1bb0906 100644 --- a/test/fuzztest/bundleactiveonremoterequest_fuzzer/BUILD.gn +++ b/test/fuzztest/bundleactiveonremoterequest_fuzzer/BUILD.gn @@ -62,6 +62,7 @@ ohos_fuzztest("BundleActiveOnRemoteRequestFuzzTest") { "c_utils:utils", "common_event_service:cesfwk_innerkits", "eventhandler:libeventhandler", + "ffrt:libffrt", "hilog:libhilog", "init:libbegetutil", "ipc:ipc_single", diff --git a/test/fuzztest/bundleactivepowerstatecallbackproxy_fuzzer/BUILD.gn b/test/fuzztest/bundleactivepowerstatecallbackproxy_fuzzer/BUILD.gn index 367d25f..3b2f2e6 100644 --- a/test/fuzztest/bundleactivepowerstatecallbackproxy_fuzzer/BUILD.gn +++ b/test/fuzztest/bundleactivepowerstatecallbackproxy_fuzzer/BUILD.gn @@ -67,6 +67,7 @@ ohos_fuzztest("BundleActivePowerstateCallbackproxyFuzzTest") { "c_utils:utils", "common_event_service:cesfwk_innerkits", "eventhandler:libeventhandler", + "ffrt:libffrt", "hilog:libhilog", "init:libbegetutil", "ipc:ipc_single", diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index b976982..b025dd5 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -57,6 +57,7 @@ ohos_unittest("BundleActiveTotalTest") { "c_utils:utils", "common_event_service:cesfwk_innerkits", "eventhandler:libeventhandler", + "ffrt:libffrt", "hilog:libhilog", "init:libbegetutil", "ipc:ipc_single", @@ -93,6 +94,7 @@ ohos_unittest("DeviceUsageStatsTest") { external_deps = [ "c_utils:utils", "eventhandler:libeventhandler", + "ffrt:libffrt", "hilog:libhilog", "ipc:ipc_single", "safwk:system_ability_fwk", @@ -126,6 +128,7 @@ ohos_unittest("DeviceUsageStatsMultiTest") { external_deps = [ "c_utils:utils", "eventhandler:libeventhandler", + "ffrt:libffrt", "hilog:libhilog", "ipc:ipc_single", "safwk:system_ability_fwk", @@ -164,6 +167,7 @@ ohos_unittest("DeviceUsageStatsServiceTest") { "c_utils:utils", "common_event_service:cesfwk_innerkits", "eventhandler:libeventhandler", + "ffrt:libffrt", "hilog:libhilog", "ipc:ipc_single", "relational_store:native_rdb", @@ -220,6 +224,7 @@ ohos_unittest("DeviceUsageStatsMockTest") { "c_utils:utils", "common_event_service:cesfwk_innerkits", "eventhandler:libeventhandler", + "ffrt:libffrt", "hilog:libhilog", "ipc:ipc_single", "relational_store:native_rdb", @@ -261,6 +266,7 @@ ohos_unittest("DeviceUsagePackageUsageTest") { "c_utils:utils", "common_event_service:cesfwk_innerkits", "eventhandler:libeventhandler", + "ffrt:libffrt", "hilog:libhilog", "ipc:ipc_single", "relational_store:native_rdb", diff --git a/test/unittest/device_usage_statistics_mock_test.cpp b/test/unittest/device_usage_statistics_mock_test.cpp index f630948..b2b93b6 100644 --- a/test/unittest/device_usage_statistics_mock_test.cpp +++ b/test/unittest/device_usage_statistics_mock_test.cpp @@ -14,6 +14,8 @@ */ #include +#include +#include #include "system_ability_definition.h" #include "bundle_active_client.h" @@ -57,6 +59,8 @@ void DeviceUsageStatisticsMockTest::SetUpTestCase(void) void DeviceUsageStatisticsMockTest::TearDownTestCase(void) { + int64_t sleepTime = 3; + std::this_thread::sleep_for(std::chrono::seconds(sleepTime)); } void DeviceUsageStatisticsMockTest::SetUp(void) diff --git a/test/unittest/device_usage_statistics_service_test.cpp b/test/unittest/device_usage_statistics_service_test.cpp index 8421061..ac9cf1b 100644 --- a/test/unittest/device_usage_statistics_service_test.cpp +++ b/test/unittest/device_usage_statistics_service_test.cpp @@ -16,6 +16,8 @@ #include #include +#include +#include #include "system_ability_definition.h" #include "bundle_active_service.h" @@ -46,6 +48,9 @@ void DeviceUsageStatisticsServiceTest::SetUpTestCase(void) void DeviceUsageStatisticsServiceTest::TearDownTestCase(void) { + DelayedSingleton::GetInstance()->bundleActiveCore_->DeInit(); + int64_t sleepTime = 6; + std::this_thread::sleep_for(std::chrono::seconds(sleepTime)); } void DeviceUsageStatisticsServiceTest::SetUp(void) -- Gitee From 27ea963a870b659271c65d4a296d1e1b80ea2fa8 Mon Sep 17 00:00:00 2001 From: fengyang Date: Thu, 6 Jun 2024 20:59:32 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E5=B8=B8=E9=A9=BB=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fengyang --- services/common/src/bundle_active_core.cpp | 6 ++-- .../include/bundle_active_group_handler.h | 2 +- .../src/bundle_active_group_handler.cpp | 33 +++++++++---------- 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/services/common/src/bundle_active_core.cpp b/services/common/src/bundle_active_core.cpp index 98eec4b..8788c66 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -805,9 +805,9 @@ void BundleActiveCore::OnObserverDied(const wptr &remote) return; } std::shared_ptr bundleActiveCore = shared_from_this(); - bundleGroupHandler_->SubmitTask([bundleActiveCore, &remote]() { - bundleActiveCore->OnObserverDiedInner(remote); - }); + bundleGroupHandler_->SubmitTask([bundleActiveCore, &remote]() { + bundleActiveCore->OnObserverDiedInner(remote); + }); } void BundleActiveCore::OnObserverDiedInner(const wptr &remote) diff --git a/services/packagegroup/include/bundle_active_group_handler.h b/services/packagegroup/include/bundle_active_group_handler.h index d421c90..46cbe04 100644 --- a/services/packagegroup/include/bundle_active_group_handler.h +++ b/services/packagegroup/include/bundle_active_group_handler.h @@ -46,7 +46,7 @@ public: */ void SendEvent(const BundleActiveGroupHandlerObject& tmpHandlerobj, const int64_t delayTime = 0); void RemoveEvent(const int32_t eventId); - void SubmitTask(std::function&& func); + void SubmitTask(const std::function& func); void Init(const std::shared_ptr& bundleActiveController); void DeInit(); static const int32_t MSG_CHECK_BUNDLE_STATE = 0; diff --git a/services/packagegroup/src/bundle_active_group_handler.cpp b/services/packagegroup/src/bundle_active_group_handler.cpp index 32a7bf7..8ed986a 100644 --- a/services/packagegroup/src/bundle_active_group_handler.cpp +++ b/services/packagegroup/src/bundle_active_group_handler.cpp @@ -65,10 +65,10 @@ void BundleActiveGroupHandler::DeInit() void BundleActiveGroupHandler::SendEvent(const BundleActiveGroupHandlerObject& tmpHandlerobj, const int64_t delayTime) { - if (event == nullptr || ffrtQueue_ == nullptr) { + if (ffrtQueue_ == nullptr) { return; } - switch (event->GetInnerEventId()) { + switch (tmpHandlerobj.eventId_) { case MSG_CHECK_BUNDLE_STATE: { ProcessCheckBundleState(tmpHandlerobj, delayTime); break; @@ -94,9 +94,9 @@ void BundleActiveGroupHandler::ProcessCheckBundleState( ffrtHandleMap_[MSG_CHECK_BUNDLE_STATE] = ffrtQueue_->submit_h([bundleActiveGroupHandler, tmpHandlerobj]() { sptr timer = MiscServices::TimeServiceClient::GetInstance(); int64_t bootBasedTimeStamp = timer->GetBootTimeMs(); - bundleActiveGroupController_->CheckAndUpdateGroup(tmpHandlerobj.bundleName_, + bundleActiveGroupHandler->bundleActiveGroupController_->CheckAndUpdateGroup(tmpHandlerobj.bundleName_, tmpHandlerobj.userId_, tmpHandlerobj.uid_, bootBasedTimeStamp); - bundleActiveGroupController_->RestoreToDatabase(tmpHandlerobj.userId_); + bundleActiveGroupHandler->bundleActiveGroupController_->RestoreToDatabase(tmpHandlerobj.userId_); }, ffrt::task_attr().delay(delayTime)); } @@ -108,39 +108,38 @@ void BundleActiveGroupHandler::ProcessOneTimecheckBundleState( std::vector activatedOsAccountIds; BundleActiveAccountHelper::GetActiveUserId(activatedOsAccountIds); for (uint32_t i = 0; i < activatedOsAccountIds.size(); i++) { - bundleActiveGroupController_->CheckEachBundleState(activatedOsAccountIds[i]); - bundleActiveGroupController_->RestoreToDatabase(activatedOsAccountIds[i]); + bundleActiveGroupHandler->bundleActiveGroupController_->CheckEachBundleState(activatedOsAccountIds[i]); + bundleActiveGroupHandler->bundleActiveGroupController_->RestoreToDatabase(activatedOsAccountIds[i]); } - RemoveEvent(MSG_ONE_TIME_CHECK_BUNDLE_STATE); + bundleActiveGroupHandler->RemoveEvent(MSG_ONE_TIME_CHECK_BUNDLE_STATE); }, ffrt::task_attr().delay(delayTime)); } void BundleActiveGroupHandler::ProcessCheckIdleState( const BundleActiveGroupHandlerObject& tmpHandlerobj, const int64_t delayTime) { - std::shared_ptr bundleActiveGroupHandler = shared_from_this(); ffrtHandleMap_[MSG_CHECK_IDLE_STATE] = ffrtQueue_->submit_h([bundleActiveGroupHandler, tmpHandlerobj]() { - if (bundleActiveGroupController_->CheckEachBundleState(tmpHandlerobj.userId_) && - bundleActiveGroupController_->bundleGroupEnable_) { + if (bundleActiveGroupHandler->bundleActiveGroupController_->CheckEachBundleState(tmpHandlerobj.userId_) && + bundleActiveGroupHandler->bundleActiveGroupController_->bundleGroupEnable_) { BundleActiveGroupHandlerObject GroupHandlerObj; GroupHandlerObj.userId_ = tmpHandlerobj.userId_; GroupHandlerObj.eventId_ = BundleActiveGroupHandler::MSG_CHECK_IDLE_STATE; - bundleActiveGroupController_->RestoreToDatabase(GroupHandlerObj.userId_); - SendEvent(GroupHandlerObj, checkIdleInterval_); + bundleActiveGroupHandler->bundleActiveGroupController_->RestoreToDatabase(GroupHandlerObj.userId_); + bundleActiveGroupHandler->SendEvent(GroupHandlerObj, checkIdleInterval_); } }, ffrt::task_attr().delay(delayTime)); } -void BundleActiveGroupHandler::RemoveEvnet(const int32_t eventId) +void BundleActiveGroupHandler::RemoveEvent(const int32_t eventId) { - if (ffrtHandlerMap_.find(eventId) != ffrtHandleMap_.end()) { - ffrtQueue->cancel(ffrtHandleMap_[eventId]); - ffrtHandlerMap_.erase(eventId); + if (ffrtHandleMap_.find(eventId) != ffrtHandleMap_.end()) { + ffrtQueue_->cancel(ffrtHandleMap_[eventId]); + ffrtHandleMap_.erase(eventId); } } -void BundleActiveGroupHandler::SubmitTask(std::function&& func) +void BundleActiveGroupHandler::SubmitTask(const std::function& func) { ffrtQueue_->submit_h([func] { func(); -- Gitee From f0869e80ef89739d1e4af6ef93031694207e5bd3 Mon Sep 17 00:00:00 2001 From: fengyang Date: Thu, 6 Jun 2024 22:20:53 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E5=B8=B8=E9=A9=BB=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fengyang --- services/packagegroup/src/bundle_active_group_handler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/packagegroup/src/bundle_active_group_handler.cpp b/services/packagegroup/src/bundle_active_group_handler.cpp index 8ed986a..62c61e7 100644 --- a/services/packagegroup/src/bundle_active_group_handler.cpp +++ b/services/packagegroup/src/bundle_active_group_handler.cpp @@ -126,7 +126,7 @@ void BundleActiveGroupHandler::ProcessCheckIdleState( GroupHandlerObj.userId_ = tmpHandlerobj.userId_; GroupHandlerObj.eventId_ = BundleActiveGroupHandler::MSG_CHECK_IDLE_STATE; bundleActiveGroupHandler->bundleActiveGroupController_->RestoreToDatabase(GroupHandlerObj.userId_); - bundleActiveGroupHandler->SendEvent(GroupHandlerObj, checkIdleInterval_); + bundleActiveGroupHandler->SendEvent(GroupHandlerObj, bundleActiveGroupHandler->checkIdleInterval_); } }, ffrt::task_attr().delay(delayTime)); } -- Gitee From 56daaacbf2e13e7414ac8ccc943c54bf2424f5c6 Mon Sep 17 00:00:00 2001 From: fengyang Date: Fri, 7 Jun 2024 09:07:45 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E5=B8=B8=E9=A9=BB=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fengyang --- services/packagegroup/src/bundle_active_group_controller.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/packagegroup/src/bundle_active_group_controller.cpp b/services/packagegroup/src/bundle_active_group_controller.cpp index 03ebf26..10224ce 100644 --- a/services/packagegroup/src/bundle_active_group_controller.cpp +++ b/services/packagegroup/src/bundle_active_group_controller.cpp @@ -178,7 +178,7 @@ void BundleActiveGroupController::CheckIdleStatsOneTime() BundleActiveGroupHandlerObject tmpGroupHandlerObj; tmpGroupHandlerObj.eventId_ = BundleActiveGroupHandler::MSG_ONE_TIME_CHECK_BUNDLE_STATE; if (activeGroupHandler_ != nullptr) { - activeGroupHandler_->SendEvent(handlerEvent); + activeGroupHandler_->SendEvent(tmpGroupHandlerObj); } } -- Gitee