diff --git a/BUILD.gn b/BUILD.gn index 88c0693732abbb3272c0d7fcb19f34a8a92020c6..551975cac28f815944ea620524618c6f7ed3da4a 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 07b2cef1efb7b2c4c36b1cfbaee57a2109d5c8d0..f2e10eb4ca23b14caacea81c611f742a1e3c09eb 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 a8aea65d4a90cb17e41929fb593f1b081a9c53b0..5532835a74b5916984a0893fbabebc83f32651f3 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 4f22b9d708ef4183a14aa5741b85be21d9451e97..8788c66597a261eea5d7cf1602cbb7658aff2730 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 633bea2fb8484329f691e9bed39d090c39128d88..4ffeb0b1d4b9cf4c26dac8e6541c1d9551cab7f6 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 057730c34006b0bfda5e82ae34d286fc18bd7f80..816963d938dddb94fd409f4d54d3458803e7add2 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 921ab9cc96c434800c1d95356fa6991606a62c81..46cbe04bac7c81b46cdc4bcb3495c66730bbbde5 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(const 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 4b6f08eedbbfc116c75669777d3d50ce9b5604f7..10224ceda4a78fd4b30c7ef14b4841e8ae992288 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(tmpGroupHandlerObj); } } @@ -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 a4932c26898e35d66cc85591cb5b1c08d9edec97..62c61e796ced13520bea66a1a52a41fe6cdc0d23 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 (ffrtQueue_ == nullptr) { return; } - switch (event->GetInnerEventId()) { + switch (tmpHandlerobj.eventId_) { 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,65 @@ 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(); + bundleActiveGroupHandler->bundleActiveGroupController_->CheckAndUpdateGroup(tmpHandlerobj.bundleName_, + tmpHandlerobj.userId_, tmpHandlerobj.uid_, bootBasedTimeStamp); + bundleActiveGroupHandler->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++) { + bundleActiveGroupHandler->bundleActiveGroupController_->CheckEachBundleState(activatedOsAccountIds[i]); + bundleActiveGroupHandler->bundleActiveGroupController_->RestoreToDatabase(activatedOsAccountIds[i]); + } + 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 (bundleActiveGroupHandler->bundleActiveGroupController_->CheckEachBundleState(tmpHandlerobj.userId_) && + bundleActiveGroupHandler->bundleActiveGroupController_->bundleGroupEnable_) { + BundleActiveGroupHandlerObject GroupHandlerObj; + GroupHandlerObj.userId_ = tmpHandlerobj.userId_; + GroupHandlerObj.eventId_ = BundleActiveGroupHandler::MSG_CHECK_IDLE_STATE; + bundleActiveGroupHandler->bundleActiveGroupController_->RestoreToDatabase(GroupHandlerObj.userId_); + bundleActiveGroupHandler->SendEvent(GroupHandlerObj, bundleActiveGroupHandler->checkIdleInterval_); + } + }, ffrt::task_attr().delay(delayTime)); +} + +void BundleActiveGroupHandler::RemoveEvent(const int32_t eventId) +{ + if (ffrtHandleMap_.find(eventId) != ffrtHandleMap_.end()) { + ffrtQueue_->cancel(ffrtHandleMap_[eventId]); + ffrtHandleMap_.erase(eventId); + } +} + +void BundleActiveGroupHandler::SubmitTask(const 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 eb9bb1f802428111df143fb5f379d163c096a09c..e0bc1aa8aacbe93f0f8a62dbd1cd932557cc4524 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 e7d0611a9fe86c1b0864ae1bc5e42461f9c5c2c8..d074932a4263bf9a7b3d9d664404c1dc2d4ed7e4 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 9c7531495d02d5452854fafa1ceb52cd2e6a1bf4..1bb0906419d2fb0b2fc8a332e0020f00732eb6fe 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 367d25f36090a5d6be08f26c2db04740ff62e881..3b2f2e645ee0b7792869053fc3bae94ae421c66e 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 b97698266aca7183dd2aa50a9911e25d6448beef..b025dd563148ddabca72399c27047d845a4d9a3f 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 f63094856c5367ee05bed2372d86c79f23c6c6db..b2b93b6f954abced4d0a754b63afd08653d7ec61 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 84210616bce3a037b0402ce32034c6db075dfbb3..ac9cf1b733c6f9983223a1da795aa83c4d838d7b 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)