From 63b6d18a8b647b602b518c8774c5156a7f9db6da Mon Sep 17 00:00:00 2001 From: fengyang Date: Fri, 2 Aug 2024 14:44:00 +0800 Subject: [PATCH] =?UTF-8?q?Revert=20"eventHandler=E5=88=87=E6=8D=A2ffrt"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 3bbd93cfb70ba857537913a2f3dfd30b19ed5a37. Signed-off-by: fengyang --- services/common/include/bundle_active_core.h | 7 +- .../common/include/bundle_active_service.h | 1 + .../include/bundle_active_usage_database.h | 3 +- .../src/bundle_active_app_state_obsever.cpp | 3 +- ...bundle_active_continuous_task_observer.cpp | 3 +- services/common/src/bundle_active_core.cpp | 57 ++++--- services/common/src/bundle_active_service.cpp | 28 +++- .../src/bundle_active_usage_database.cpp | 42 ++--- .../include/bundle_active_group_controller.h | 4 +- .../include/bundle_active_group_handler.h | 15 +- .../include/bundle_active_user_history.h | 3 +- .../src/bundle_active_group_controller.cpp | 42 +++-- .../src/bundle_active_group_handler.cpp | 38 +++-- .../src/bundle_active_user_history.cpp | 2 +- .../include/bundle_active_report_handler.h | 19 ++- .../src/bundle_active_report_handler.cpp | 27 ++- test/unittest/bundle_active_total_test.cpp | 3 +- .../device_usage_statistics_service_test.cpp | 158 +++++++++++++++--- test/unittest/package_usage_test.cpp | 108 +++++++++++- 19 files changed, 434 insertions(+), 129 deletions(-) diff --git a/services/common/include/bundle_active_core.h b/services/common/include/bundle_active_core.h index a9ea52e..a8603ca 100644 --- a/services/common/include/bundle_active_core.h +++ b/services/common/include/bundle_active_core.h @@ -33,7 +33,6 @@ #include "ibundle_active_service.h" #include "remote_death_recipient.h" #include "iapp_group_callback.h" -#include "ffrt.h" #include "bundle_active_debug_mode.h" #include "bundle_active_stats_update_listener.h" #include "bundle_state_inner_errors.h" @@ -124,7 +123,7 @@ public: * set its handler and subscribe needed common event. * create bundleGroupController_ object. */ - void InitBundleGroupController(); + void InitBundleGroupController(const std::shared_ptr &runner); /* * function: SetHandler, BundleActiveService call it to set event report handler @@ -259,8 +258,8 @@ private: std::shared_ptr bundleGroupHandler_; int64_t systemTimeShot_; int64_t realTimeShot_; - ffrt::mutex mutex_; - ffrt::recursive_mutex callbackMutex_; + std::mutex mutex_; + std::recursive_mutex callbackMutex_; std::map> userStatServices_; void RegisterSubscriber(); std::shared_ptr commonEventSubscriber_; diff --git a/services/common/include/bundle_active_service.h b/services/common/include/bundle_active_service.h index 6e042cf..e58b648 100644 --- a/services/common/include/bundle_active_service.h +++ b/services/common/include/bundle_active_service.h @@ -221,6 +221,7 @@ private: sptr shutdownCallback_; sptr powerStateCallback_; #endif + std::shared_ptr ffrtQueue_; bool ready_ {false}; int32_t ConvertIntervalType(const int32_t intervalType); void InitNecessaryState(); diff --git a/services/common/include/bundle_active_usage_database.h b/services/common/include/bundle_active_usage_database.h index 7258f9d..d257eed 100644 --- a/services/common/include/bundle_active_usage_database.h +++ b/services/common/include/bundle_active_usage_database.h @@ -33,7 +33,6 @@ #include "rdb_store_config.h" #include "result_set.h" #include "values_bucket.h" -#include "ffrt.h" #include "bundle_active_period_stats.h" #include "bundle_active_calendar.h" @@ -148,7 +147,7 @@ private: std::string formRecordsTableName_; std::string versionFile_; uint32_t currentVersion_; - ffrt::mutex databaseMutex_; + std::mutex databaseMutex_; std::int64_t eventBeginTime_; bool debugDatabase_; }; diff --git a/services/common/src/bundle_active_app_state_obsever.cpp b/services/common/src/bundle_active_app_state_obsever.cpp index 872a075..3af4e86 100644 --- a/services/common/src/bundle_active_app_state_obsever.cpp +++ b/services/common/src/bundle_active_app_state_obsever.cpp @@ -74,7 +74,8 @@ void BundleActiveAppStateObserver::OnAbilityStateChanged(const AbilityStateData if (reportHandler_ != nullptr) { std::shared_ptr handlerobjToPtr = std::make_shared(tmpHandlerObject); - reportHandler_->SendEvent(BundleActiveReportHandler::MSG_REPORT_EVENT, handlerobjToPtr); + auto getEvent = AppExecFwk::InnerEvent::Get(BundleActiveReportHandler::MSG_REPORT_EVENT, handlerobjToPtr); + reportHandler_->SendEvent(getEvent); } } return; diff --git a/services/common/src/bundle_active_continuous_task_observer.cpp b/services/common/src/bundle_active_continuous_task_observer.cpp index ee8b664..8a9ae1a 100644 --- a/services/common/src/bundle_active_continuous_task_observer.cpp +++ b/services/common/src/bundle_active_continuous_task_observer.cpp @@ -115,7 +115,8 @@ void BundleActiveContinuousTaskObserver::ReportContinuousTaskEvent( BUNDLE_ACTIVE_LOGI("BundleActiveAppStateObserver::OnAbilityStateChanged handler not null, SEND"); std::shared_ptr handlerobjToPtr = std::make_shared(tmpHandlerObject); - reportHandler_->SendEvent(BundleActiveReportHandler::MSG_REPORT_EVENT, handlerobjToPtr); + auto getEvent = AppExecFwk::InnerEvent::Get(BundleActiveReportHandler::MSG_REPORT_EVENT, handlerobjToPtr); + reportHandler_->SendEvent(getEvent); } } } diff --git a/services/common/src/bundle_active_core.cpp b/services/common/src/bundle_active_core.cpp index 1bb14cd..c4ae7d5 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -71,7 +71,7 @@ BundleActiveCore::~BundleActiveCore() void BundleActiveCommonEventSubscriber::OnReceiveEvent(const CommonEventData &data) { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); std::string action = data.GetWant().GetAction(); if (action == CommonEventSupport::COMMON_EVENT_SCREEN_OFF || action == CommonEventSupport::COMMON_EVENT_SCREEN_ON) { @@ -88,7 +88,8 @@ void BundleActiveCommonEventSubscriber::OnReceiveEvent(const CommonEventData &da BundleActiveReportHandlerObject tmpHandlerObject(userId, ""); std::shared_ptr handlerobjToPtr = std::make_shared(tmpHandlerObject); - bundleActiveReportHandler_.lock()->SendEvent(BundleActiveReportHandler::MSG_REMOVE_USER, handlerobjToPtr); + auto event = AppExecFwk::InnerEvent::Get(BundleActiveReportHandler::MSG_REMOVE_USER, handlerobjToPtr); + bundleActiveReportHandler_.lock()->SendEvent(event); } } else if (action == CommonEventSupport::COMMON_EVENT_USER_SWITCHED) { int32_t userId = data.GetCode(); @@ -96,7 +97,8 @@ void BundleActiveCommonEventSubscriber::OnReceiveEvent(const CommonEventData &da BundleActiveReportHandlerObject tmpHandlerObject(userId, ""); std::shared_ptr handlerobjToPtr = std::make_shared(tmpHandlerObject); - bundleActiveReportHandler_.lock()->SendEvent(BundleActiveReportHandler::MSG_SWITCH_USER, handlerobjToPtr); + auto event = AppExecFwk::InnerEvent::Get(BundleActiveReportHandler::MSG_SWITCH_USER, handlerobjToPtr); + bundleActiveReportHandler_.lock()->SendEvent(event); } else if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED || action == CommonEventSupport::COMMON_EVENT_PACKAGE_FULLY_REMOVED) { int32_t userId = data.GetWant().GetIntParam("userId", 0); @@ -109,8 +111,14 @@ void BundleActiveCommonEventSubscriber::OnReceiveEvent(const CommonEventData &da tmpHandlerObject.appIndex_ = data.GetWant().GetIntParam("appIndex", -1); std::shared_ptr handlerobjToPtr = std::make_shared(tmpHandlerObject); +<<<<<<< HEAD bundleActiveReportHandler_.lock()->SendEvent(BundleActiveReportHandler::MSG_BUNDLE_UNINSTALLED, handlerobjToPtr); +======= + auto event = AppExecFwk::InnerEvent::Get(BundleActiveReportHandler::MSG_BUNDLE_UNINSTALLED, + handlerobjToPtr); + bundleActiveReportHandler_.lock()->SendEvent(event); +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) } } else if (action == COMMON_EVENT_UNLOCK_SCREEN || action == COMMON_EVENT_LOCK_SCREEN) { int32_t userId = data.GetWant().GetIntParam("userId", 0); @@ -176,10 +184,14 @@ void BundleActiveCore::Init() BUNDLE_ACTIVE_LOGD("system time shot is %{public}lld", (long long)systemTimeShot_); } -void BundleActiveCore::InitBundleGroupController() +void BundleActiveCore::InitBundleGroupController(const std::shared_ptr &runner) { BUNDLE_ACTIVE_LOGD("InitBundleGroupController called"); - bundleGroupHandler_ = std::make_shared(debugCore_); + if (runner == nullptr) { + BUNDLE_ACTIVE_LOGE("report handler is null"); + return; + } + bundleGroupHandler_ = std::make_shared(runner, debugCore_); if (bundleGroupHandler_ == nullptr) { return; } @@ -229,7 +241,7 @@ void BundleActiveCore::OnBundleUninstalled(const int32_t userId, const std::stri const int32_t uid, const int32_t appIndex) { BUNDLE_ACTIVE_LOGD("OnBundleUninstalled CALLED"); - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); int64_t timeNow = CheckTimeChangeAndGetWallTime(userId); if (timeNow == ERR_TIME_OPERATION_FAILED) { return; @@ -249,9 +261,10 @@ 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 event = AppExecFwk::InnerEvent::Get(BundleActiveReportHandler::MSG_FLUSH_TO_DISK, handlerobjToPtr); + if (handler_.lock()->HasInnerEvent(static_cast(userId)) == 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_); + handler_.lock()->SendEvent(event, flushInterval_); } } } @@ -332,7 +345,7 @@ void BundleActiveCore::PreservePowerStateInfo(const int32_t eventId) void BundleActiveCore::ShutDown() { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); BUNDLE_ACTIVE_LOGD("ShutDown called"); sptr timer = MiscServices::TimeServiceClient::GetInstance(); int64_t timeStamp = timer->GetBootTimeMs(); @@ -408,7 +421,7 @@ void BundleActiveCore::ConvertToSystemTimeLocked(BundleActiveEvent& event) void BundleActiveCore::OnUserRemoved(const int32_t userId) { BUNDLE_ACTIVE_LOGD("OnUserRemoved called"); - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); auto it = userStatServices_.find(userId); if (it == userStatServices_.end()) { return; @@ -452,7 +465,7 @@ int32_t BundleActiveCore::ReportEvent(BundleActiveEvent& event, int32_t userId) BUNDLE_ACTIVE_LOGD("FLUSH interval is %{public}lld, debug is %{public}d", (long long)flushInterval_, debugCore_); ObtainSystemEventName(event); event.PrintEvent(debugCore_); - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); if (userId == 0 || userId == -1) { return -1; } @@ -541,7 +554,7 @@ ErrCode BundleActiveCore::QueryBundleStatsInfos(std::vector lock(mutex_); + std::lock_guard lock(mutex_); int64_t timeNow = CheckTimeChangeAndGetWallTime(userId); if (timeNow == ERR_TIME_OPERATION_FAILED) { return ERR_TIME_OPERATION_FAILED; @@ -566,7 +579,7 @@ ErrCode BundleActiveCore::QueryBundleEvents(std::vector& bund const int64_t beginTime, const int64_t endTime, std::string bundleName) { BUNDLE_ACTIVE_LOGD("QueryBundleEvents called"); - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); int64_t timeNow = CheckTimeChangeAndGetWallTime(userId); if (timeNow == ERR_TIME_OPERATION_FAILED) { return ERR_TIME_OPERATION_FAILED; @@ -585,7 +598,7 @@ ErrCode BundleActiveCore::QueryBundleEvents(std::vector& bund ErrCode BundleActiveCore::QueryModuleUsageRecords(int32_t maxNum, std::vector& results, int32_t userId) { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); int64_t timeNow = CheckTimeChangeAndGetWallTime(userId); if (timeNow == ERR_TIME_OPERATION_FAILED) { return ERR_TIME_OPERATION_FAILED; @@ -600,7 +613,7 @@ ErrCode BundleActiveCore::QueryModuleUsageRecords(int32_t maxNum, std::vector& eventStats, int32_t userId) { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); int64_t timeNow = CheckTimeChangeAndGetWallTime(userId); if (timeNow == ERR_TIME_OPERATION_FAILED) { return ERR_TIME_OPERATION_FAILED; @@ -615,7 +628,7 @@ ErrCode BundleActiveCore::QueryDeviceEventStats(int64_t beginTime, int64_t endTi ErrCode BundleActiveCore::QueryNotificationEventStats(int64_t beginTime, int64_t endTime, std::vector& eventStats, int32_t userId) { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); int64_t timeNow = CheckTimeChangeAndGetWallTime(userId); if (timeNow == ERR_TIME_OPERATION_FAILED) { return ERR_TIME_OPERATION_FAILED; @@ -695,7 +708,7 @@ void BundleActiveCore::OnAppGroupChanged(const AppGroupCallbackInfo& callbackInf void BundleActiveCore::NotifOberserverGroupChanged(const AppGroupCallbackInfo& callbackInfo, AccessToken::HapTokenInfo tokenInfo) { - std::lock_guard lock(callbackMutex_); + std::lock_guard lock(callbackMutex_); for (const auto &item : groupChangeObservers_) { auto observer = item.second; if (!observer) { @@ -719,7 +732,7 @@ void BundleActiveCore::NotifOberserverGroupChanged(const AppGroupCallbackInfo& c ErrCode BundleActiveCore::RegisterAppGroupCallBack(const AccessToken::AccessTokenID& tokenId, const sptr &observer) { - std::lock_guard lock(callbackMutex_); + std::lock_guard lock(callbackMutex_); if (!observer) { return ERR_MEMORY_OPERATION_FAILED; } @@ -736,7 +749,7 @@ ErrCode BundleActiveCore::RegisterAppGroupCallBack(const AccessToken::AccessToke ErrCode BundleActiveCore::UnRegisterAppGroupCallBack(const AccessToken::AccessTokenID& tokenId, const sptr &observer) { - std::lock_guard lock(callbackMutex_); + std::lock_guard lock(callbackMutex_); auto item = groupChangeObservers_.find(tokenId); if (item == groupChangeObservers_.end()) { BUNDLE_ACTIVE_LOGI("UnRegisterAppGroupCallBack observer is not exist, return"); @@ -749,7 +762,7 @@ ErrCode BundleActiveCore::UnRegisterAppGroupCallBack(const AccessToken::AccessTo void BundleActiveCore::AddObserverDeathRecipient(const sptr &observer) { - std::lock_guard lock(callbackMutex_); + std::lock_guard lock(callbackMutex_); if (!observer) { BUNDLE_ACTIVE_LOGI("observer nullptr."); return; @@ -775,7 +788,7 @@ void BundleActiveCore::AddObserverDeathRecipient(const sptr & } void BundleActiveCore::RemoveObserverDeathRecipient(const sptr &observer) { - std::lock_guard lock(callbackMutex_); + std::lock_guard lock(callbackMutex_); if (!observer) { return; } @@ -809,7 +822,7 @@ void BundleActiveCore::OnObserverDiedInner(const wptr &remote) BUNDLE_ACTIVE_LOGE("get remote object failed"); return; } - std::lock_guard lock(callbackMutex_); + std::lock_guard lock(callbackMutex_); for (const auto& item : groupChangeObservers_) { if (!(item.second)) { continue; diff --git a/services/common/src/bundle_active_service.cpp b/services/common/src/bundle_active_service.cpp index a865ca4..1e2e4a2 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,8 +68,14 @@ void BundleActiveService::OnStart() BUNDLE_ACTIVE_LOGI("service is ready. nothing to do."); return; } + ffrtQueue_ = std::make_shared(DEVICE_USAGE_INIT_QUEUE.c_str(), + ffrt::queue_attr().qos(ffrt::qos_default)); + if (ffrtQueue_ == nullptr) { + BUNDLE_ACTIVE_LOGE("BundleActiveService ffrtQueue create failed!"); + return; + } std::shared_ptr service = shared_from_this(); - ffrt::submit([service]() { + ffrtQueue_->submit([service]() { service->InitNecessaryState(); }); } @@ -84,7 +91,7 @@ void BundleActiveService::InitNecessaryState() if (systemAbilityManager == nullptr) { BUNDLE_ACTIVE_LOGI("GetSystemAbilityManager fail!"); std::shared_ptr service = shared_from_this(); - ffrt::submit([service]() { + ffrtQueue_->submit([service]() { service->InitNecessaryState(); }, ffrt::task_attr().delay(DELAY_TIME)); return; @@ -94,7 +101,7 @@ void BundleActiveService::InitNecessaryState() if (!checkResult) { BUNDLE_ACTIVE_LOGI("request system service is not ready yet!"); std::shared_ptr service = shared_from_this(); - ffrt::submit([service]() { + ffrtQueue_->submit([service]() { service->InitNecessaryState(); }, ffrt::task_attr().delay(DELAY_TIME)); return; @@ -106,7 +113,7 @@ void BundleActiveService::InitNecessaryState() if (!getAbility) { BUNDLE_ACTIVE_LOGI("request system service object is not ready yet!"); std::shared_ptr service = shared_from_this(); - ffrt::submit([service]() { + ffrtQueue_->submit([service]() { service->InitNecessaryState(); }, ffrt::task_attr().delay(DELAY_TIME)); return; @@ -129,7 +136,13 @@ void BundleActiveService::InitService() bundleActiveCore_->Init(); } if (reportHandler_ == nullptr) { - reportHandler_ = std::make_shared(); + std::string threadName = "device_usage_handler"; + runner = AppExecFwk::EventRunner::Create(threadName); + if (runner == nullptr) { + BUNDLE_ACTIVE_LOGE("report handler is null"); + return; + } + reportHandler_ = std::make_shared(runner); if (reportHandler_ == nullptr) { return; } @@ -155,7 +168,7 @@ void BundleActiveService::InitService() #endif InitAppStateSubscriber(reportHandler_); InitContinuousSubscriber(reportHandler_); - bundleActiveCore_->InitBundleGroupController(); + bundleActiveCore_->InitBundleGroupController(runner); SubscribeAppState(); SubscribeContinuousTask(); } @@ -256,7 +269,8 @@ ErrCode BundleActiveService::ReportEvent(BundleActiveEvent& event, const int32_t tmpHandlerObject.event_.timeStamp_ = timer->GetBootTimeMs(); std::shared_ptr handlerobjToPtr = std::make_shared(tmpHandlerObject); - reportHandler_->SendEvent(BundleActiveReportHandler::MSG_REPORT_EVENT, handlerobjToPtr); + auto event = AppExecFwk::InnerEvent::Get(BundleActiveReportHandler::MSG_REPORT_EVENT, handlerobjToPtr); + reportHandler_->SendEvent(event); return ERR_OK; } else { BUNDLE_ACTIVE_LOGE("token does not belong to fms service process, return"); diff --git a/services/common/src/bundle_active_usage_database.cpp b/services/common/src/bundle_active_usage_database.cpp index 5bea57b..6a3c64f 100644 --- a/services/common/src/bundle_active_usage_database.cpp +++ b/services/common/src/bundle_active_usage_database.cpp @@ -76,7 +76,7 @@ void BundleActiveUsageDatabase::ChangeToDebug() void BundleActiveUsageDatabase::InitUsageGroupDatabase(const int32_t databaseType, const bool forModuleRecords) { - lock_guard lock(databaseMutex_); + lock_guard lock(databaseMutex_); if (CreateDatabasePath() == BUNDLE_ACTIVE_FAIL) { BUNDLE_ACTIVE_LOGE("database path is not exist"); return; @@ -136,7 +136,7 @@ int32_t BundleActiveUsageDatabase::CreateDatabasePath() void BundleActiveUsageDatabase::InitDatabaseTableInfo(int64_t currentTime) { - lock_guard lock(databaseMutex_); + lock_guard lock(databaseMutex_); if (CreateDatabasePath() == BUNDLE_ACTIVE_FAIL) { BUNDLE_ACTIVE_LOGE("database path is not exist"); return; @@ -803,7 +803,7 @@ int32_t BundleActiveUsageDatabase::CreateBundleHistoryTable(uint32_t databaseTyp void BundleActiveUsageDatabase::PutBundleHistoryData(int32_t userId, shared_ptr>> userHistory) { - lock_guard lock(databaseMutex_); + lock_guard lock(databaseMutex_); if (userHistory == nullptr) { return; } @@ -858,7 +858,7 @@ void BundleActiveUsageDatabase::PutBundleHistoryData(int32_t userId, shared_ptr>> BundleActiveUsageDatabase::GetBundleHistoryData( int32_t userId) { - lock_guard lock(databaseMutex_); + lock_guard lock(databaseMutex_); if (bundleHistoryTableName_ == UNKNOWN_TABLE_NAME) { return nullptr; } @@ -901,7 +901,7 @@ shared_ptr>> BundleActiveUsag void BundleActiveUsageDatabase::PutDurationData(int64_t bootBasedDuration, int64_t screenOnDuration) { - lock_guard lock(databaseMutex_); + lock_guard lock(databaseMutex_); CheckDatabaseFile(APP_GROUP_DATABASE_INDEX); if (durationTableName_ == UNKNOWN_TABLE_NAME) { CreateDurationTable(APP_GROUP_DATABASE_INDEX); @@ -924,7 +924,7 @@ void BundleActiveUsageDatabase::PutDurationData(int64_t bootBasedDuration, int64 pair BundleActiveUsageDatabase::GetDurationData() { - lock_guard lock(databaseMutex_); + lock_guard lock(databaseMutex_); pair durationData; if (durationTableName_ == UNKNOWN_TABLE_NAME) { return durationData; @@ -994,7 +994,7 @@ void BundleActiveUsageDatabase::FlushPackageInfo(uint32_t databaseType, const Bu shared_ptr BundleActiveUsageDatabase::GetCurrentUsageData(int32_t databaseType, int32_t userId) { - lock_guard lock(databaseMutex_); + lock_guard lock(databaseMutex_); if (databaseType < 0 || databaseType >= static_cast(sortedTableArray_.size())) { return nullptr; } @@ -1219,7 +1219,7 @@ int32_t BundleActiveUsageDatabase::ExecuteRenameTableName(std::string tablePrefi int32_t BundleActiveUsageDatabase::GetOptimalIntervalType(int64_t beginTime, int64_t endTime) { - lock_guard lock(databaseMutex_); + lock_guard lock(databaseMutex_); int32_t optimalIntervalType = -1; int64_t leastTimeDiff = numeric_limits::max(); for (int32_t i = static_cast(sortedTableArray_.size() - 1); i >= 0; i--) { @@ -1239,7 +1239,7 @@ int32_t BundleActiveUsageDatabase::GetOptimalIntervalType(int64_t beginTime, int void BundleActiveUsageDatabase::RemoveOldData(int64_t currentTime) { - lock_guard lock(databaseMutex_); + lock_guard lock(databaseMutex_); calendar_->SetMilliseconds(currentTime); calendar_->IncreaseYears(-1 * MAX_FILES_EVERY_INTERVAL_TYPE[YEARLY_DATABASE_INDEX]); std::unique_ptr> overdueYearsTableCreateTime = GetOverdueTableCreateTime(YEARLY_DATABASE_INDEX, @@ -1284,7 +1284,7 @@ void BundleActiveUsageDatabase::RemoveOldData(int64_t currentTime) void BundleActiveUsageDatabase::RenewTableTime(int64_t timeDiffMillis) { - lock_guard lock(databaseMutex_); + lock_guard lock(databaseMutex_); for (uint32_t i = 0; i < sortedTableArray_.size(); i++) { if (sortedTableArray_.at(i).empty()) { continue; @@ -1331,7 +1331,7 @@ void BundleActiveUsageDatabase::RenewTableTime(int64_t timeDiffMillis) void BundleActiveUsageDatabase::UpdateEventData(int32_t databaseType, BundleActivePeriodStats &stats) { - lock_guard lock(databaseMutex_); + lock_guard lock(databaseMutex_); CheckDatabaseFile(databaseType); if (databaseType != DAILY_DATABASE_INDEX) { return; @@ -1344,7 +1344,7 @@ void BundleActiveUsageDatabase::UpdateEventData(int32_t databaseType, BundleActi void BundleActiveUsageDatabase::UpdateBundleUsageData(int32_t databaseType, BundleActivePeriodStats &stats) { - lock_guard lock(databaseMutex_); + lock_guard lock(databaseMutex_); if (databaseType < 0 || databaseType >= EVENT_DATABASE_INDEX) { BUNDLE_ACTIVE_LOGE("databaseType is invalid : %{public}d", databaseType); return; @@ -1436,7 +1436,7 @@ void BundleActiveUsageDatabase::GetQuerySqlCommand(const int64_t beginTime, vector BundleActiveUsageDatabase::QueryDatabaseUsageStats(int32_t databaseType, int64_t beginTime, int64_t endTime, int32_t userId) { - lock_guard lock(databaseMutex_); + lock_guard lock(databaseMutex_); vector databaseUsageStats; int32_t startIndex = 0; int32_t endIndex = 0; @@ -1485,7 +1485,7 @@ vector BundleActiveUsageDatabase::QueryDatabaseUsageSt vector BundleActiveUsageDatabase::QueryDatabaseEvents(int64_t beginTime, int64_t endTime, int32_t userId, string bundleName) { - lock_guard lock(databaseMutex_); + lock_guard lock(databaseMutex_); vector databaseEvents; int64_t eventTableTime = ParseStartTime(eventTableName_); if (JudgeQueryCondition(beginTime, endTime, eventTableTime) == QUERY_CONDITION_INVALID) { @@ -1533,7 +1533,7 @@ vector BundleActiveUsageDatabase::QueryDatabaseEvents(int64_t void BundleActiveUsageDatabase::OnPackageUninstalled(const int32_t userId, const string& bundleName, const int32_t uid, const int32_t appIndex) { - lock_guard lock(databaseMutex_); + lock_guard lock(databaseMutex_); for (uint32_t i = 0; i < sortedTableArray_.size(); i++) { if (sortedTableArray_.at(i).empty()) { continue; @@ -1603,7 +1603,7 @@ int64_t BundleActiveUsageDatabase::GetSystemTimeMs() void BundleActiveUsageDatabase::UpdateModuleData(const int32_t userId, std::map>& moduleRecords, const int64_t timeStamp) { - lock_guard lock(databaseMutex_); + lock_guard lock(databaseMutex_); CheckDatabaseFile(APP_GROUP_DATABASE_INDEX); shared_ptr rdbStore = GetBundleActiveRdbStore(APP_GROUP_DATABASE_INDEX); if (rdbStore == nullptr) { @@ -1702,7 +1702,7 @@ void BundleActiveUsageDatabase::RemoveFormData(const int32_t userId, const std:: const std::string moduleName, const std::string formName, const int32_t formDimension, const int64_t formId, const int32_t uid) { - lock_guard lock(databaseMutex_); + lock_guard lock(databaseMutex_); shared_ptr rdbStore = GetBundleActiveRdbStore(APP_GROUP_DATABASE_INDEX); if (rdbStore == nullptr) { BUNDLE_ACTIVE_LOGE("remove for data fail, rdbStore is nullptr"); @@ -1730,7 +1730,7 @@ void BundleActiveUsageDatabase::RemoveFormData(const int32_t userId, const std:: void BundleActiveUsageDatabase::LoadModuleData(const int32_t userId, std::map>& moduleRecords) { - lock_guard lock(databaseMutex_); + lock_guard lock(databaseMutex_); string queryModuleSql = "select * from " + moduleRecordsTableName_ + " where userId = ?"; vector queryCondition; queryCondition.emplace_back(to_string(userId)); @@ -1762,7 +1762,7 @@ void BundleActiveUsageDatabase::LoadModuleData(const int32_t userId, std::map>& moduleRecords) { - lock_guard lock(databaseMutex_); + lock_guard lock(databaseMutex_); string queryFormSql = "select * from " + formRecordsTableName_ + " where userId = ?"; vector queryCondition; queryCondition.emplace_back(to_string(userId)); @@ -1800,7 +1800,7 @@ void BundleActiveUsageDatabase::LoadFormData(const int32_t userId, std::map& eventStats, int32_t userId) { - lock_guard lock(databaseMutex_); + lock_guard lock(databaseMutex_); int64_t eventTableTime = ParseStartTime(eventTableName_); if (JudgeQueryCondition(beginTime, endTime, eventTableTime) == QUERY_CONDITION_INVALID) { return; @@ -1859,7 +1859,7 @@ std::string BundleActiveUsageDatabase::GetSystemEventName(const int32_t userId) void BundleActiveUsageDatabase::QueryNotificationEventStats(int32_t eventId, int64_t beginTime, int64_t endTime, std::map& notificationEventStats, int32_t userId) { - lock_guard lock(databaseMutex_); + lock_guard lock(databaseMutex_); int64_t eventTableTime = ParseStartTime(eventTableName_); if (JudgeQueryCondition(beginTime, endTime, eventTableTime) == QUERY_CONDITION_INVALID) { return; diff --git a/services/packagegroup/include/bundle_active_group_controller.h b/services/packagegroup/include/bundle_active_group_controller.h index d587f93..c64ec2c 100644 --- a/services/packagegroup/include/bundle_active_group_controller.h +++ b/services/packagegroup/include/bundle_active_group_controller.h @@ -22,7 +22,6 @@ #include "power_mgr_client.h" #endif #include "application_info.h" -#include "ffrt.h" #include "ibundle_active_service.h" #include "bundle_active_event.h" @@ -85,7 +84,7 @@ public: void OnUserSwitched(const int32_t userId, const int32_t currentUsedUser); private: - ffrt::mutex mutex_; + std::mutex mutex_; bool GetBundleMgrProxy(); std::weak_ptr activeGroupHandler_; int64_t timeoutForDirectlyUse_; @@ -100,6 +99,7 @@ private: const int32_t uid); void SendCheckBundleMsg(const BundleActiveEvent& event, const int32_t& userId, const int64_t& timeUntilNextCheck, const int64_t& checkBundleMsgEventId); + int64_t GetMsgKey(const BundleActiveEvent& event, const int32_t& userId); }; } // namespace DeviceUsageStats } // namespace OHOS diff --git a/services/packagegroup/include/bundle_active_group_handler.h b/services/packagegroup/include/bundle_active_group_handler.h index 08dbb38..f5b3537 100644 --- a/services/packagegroup/include/bundle_active_group_handler.h +++ b/services/packagegroup/include/bundle_active_group_handler.h @@ -18,9 +18,13 @@ #include "event_handler.h" #include "event_runner.h" +<<<<<<< HEAD #include "ffrt.h" #include #include +======= + +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) #include "ibundle_active_service.h" #include "bundle_active_group_controller.h" #include "bundle_active_group_common.h" @@ -37,15 +41,16 @@ public: ~BundleActiveGroupHandlerObject() {} }; -class BundleActiveGroupHandler : public std::enable_shared_from_this { +class BundleActiveGroupHandler : public AppExecFwk::EventHandler { public: - explicit BundleActiveGroupHandler(const bool debug); + explicit BundleActiveGroupHandler(const std::shared_ptr &runner, const bool debug); ~BundleActiveGroupHandler() = default; /** * Process the event. Developers should override this method. * * @param event The event should be processed. */ +<<<<<<< HEAD void ProcessEvent(const int32_t& eventId, const std::shared_ptr& handlerobj); void SendEvent(const int32_t& eventId, const std::shared_ptr& handlerobj, const int64_t& delayTime = 0); @@ -57,6 +62,9 @@ public: void RemoveCheckBundleMsg(const std::string& msgKey); void PostSyncTask(const std::function& fuc); void PostTask(const std::function& fuc); +======= + void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) void Init(const std::shared_ptr& bundleActiveController); static const int32_t MSG_CHECK_DEFAULT_BUNDLE_STATE; static const int32_t MSG_ONE_TIME_CHECK_BUNDLE_STATE; @@ -66,12 +74,15 @@ public: int64_t checkIdleInterval_; private: +<<<<<<< HEAD bool isInited_ = false; ffrt::mutex taskHandlerMutex_; ffrt::mutex checkBundleTaskMutex_; std::shared_ptr ffrtQueue_; std::map> taskHandlerMap_; std::map checkBundleTaskMap_; +======= +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) std::shared_ptr bundleActiveGroupController_; }; } // namespace DeviceUsageStats diff --git a/services/packagegroup/include/bundle_active_user_history.h b/services/packagegroup/include/bundle_active_user_history.h index 761a043..4e43f0b 100644 --- a/services/packagegroup/include/bundle_active_user_history.h +++ b/services/packagegroup/include/bundle_active_user_history.h @@ -20,7 +20,6 @@ #include #include -#include "ffrt.h" #include "bundle_active_package_history.h" #include "bundle_active_group_common.h" #include "bundle_active_usage_database.h" @@ -65,7 +64,7 @@ public: const int32_t appIndex); private: - ffrt::mutex setGroupMutex_; + std::mutex setGroupMutex_; bool isScreenOn_; std::weak_ptr bundleActiveCore_; }; diff --git a/services/packagegroup/src/bundle_active_group_controller.cpp b/services/packagegroup/src/bundle_active_group_controller.cpp index 34a968f..a1e4d58 100644 --- a/services/packagegroup/src/bundle_active_group_controller.cpp +++ b/services/packagegroup/src/bundle_active_group_controller.cpp @@ -55,19 +55,19 @@ 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) { @@ -80,7 +80,7 @@ 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_); + std::lock_guard lock(mutex_); auto activeGroupHandler = activeGroupHandler_.lock(); if (activeGroupHandler) { activeGroupHandler->RemoveEvent(BundleActiveGroupHandler::MSG_CHECK_IDLE_STATE); @@ -119,7 +119,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); @@ -184,8 +184,14 @@ void BundleActiveGroupController::PeriodCheckBundleState(const int32_t userId) tmpGroupHandlerObj.userId_ = userId; std::shared_ptr handlerobjToPtr = std::make_shared(tmpGroupHandlerObj); +<<<<<<< HEAD activeGroupHandler->SendEvent(BundleActiveGroupHandler::MSG_CHECK_DEFAULT_BUNDLE_STATE, handlerobjToPtr, FIVE_SECOND); +======= + auto handlerEvent = AppExecFwk::InnerEvent::Get(BundleActiveGroupHandler::MSG_CHECK_IDLE_STATE, + handlerobjToPtr); + activeGroupHandler->SendEvent(handlerEvent, FIVE_SECOND); +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) } } @@ -208,14 +214,11 @@ bool BundleActiveGroupController::CheckEachBundleState(const int32_t userId) void BundleActiveGroupController::CheckIdleStatsOneTime() { - BundleActiveGroupHandlerObject tmpGroupHandlerObj; - std::shared_ptr handlerobjToPtr = - std::make_shared(tmpGroupHandlerObj); auto handlerEvent = AppExecFwk::InnerEvent::Get( BundleActiveGroupHandler::MSG_ONE_TIME_CHECK_BUNDLE_STATE); auto activeGroupHandler = activeGroupHandler_.lock(); if (activeGroupHandler) { - activeGroupHandler->SendEvent(BundleActiveGroupHandler::MSG_ONE_TIME_CHECK_BUNDLE_STATE, handlerobjToPtr); + activeGroupHandler->SendEvent(handlerEvent); } } @@ -247,7 +250,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; @@ -293,17 +296,28 @@ void BundleActiveGroupController::SendCheckBundleMsg(const BundleActiveEvent& ev tmpGroupHandlerObj.userId_ = userId; tmpGroupHandlerObj.bundleName_ = event.bundleName_; tmpGroupHandlerObj.uid_ = event.uid_; + int64_t msgKey = GetMsgKey(event, userId); std::shared_ptr handlerobjToPtr = std::make_shared(tmpGroupHandlerObj); +<<<<<<< HEAD if (!activeGroupHandler_.expired()) { activeGroupHandler_.lock()->SendCheckBundleMsg(checkBundleMsgEventId, handlerobjToPtr, timeUntilNextCheck); +======= + auto handlerEvent = AppExecFwk::InnerEvent::Get(checkBundleMsgEventId, handlerobjToPtr, msgKey); + auto activeGroupHandler = activeGroupHandler_.lock(); + if (activeGroupHandler) { + if (activeGroupHandler->HasInnerEvent(msgKey) == true) { + activeGroupHandler->RemoveEvent(checkBundleMsgEventId, msgKey); + } + activeGroupHandler->SendEvent(handlerEvent, timeUntilNextCheck); +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) } } 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) { @@ -349,7 +363,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; } @@ -380,7 +394,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; @@ -411,7 +425,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 c3c0c6e..fca1e78 100644 --- a/services/packagegroup/src/bundle_active_group_handler.cpp +++ b/services/packagegroup/src/bundle_active_group_handler.cpp @@ -21,12 +21,15 @@ namespace OHOS { namespace DeviceUsageStats { +<<<<<<< HEAD const std::string DEVICE_GROUP_HANDLE_QUEUE = "DeviceUsageGroupHandleQueue"; const int32_t BundleActiveGroupHandler::MSG_CHECK_DEFAULT_BUNDLE_STATE = 0; const int32_t BundleActiveGroupHandler::MSG_ONE_TIME_CHECK_BUNDLE_STATE = 1; const int32_t BundleActiveGroupHandler::MSG_CHECK_IDLE_STATE = 2; const int32_t BundleActiveGroupHandler::MSG_CHECK_NOTIFICATION_SEEN_BUNDLE_STATE = 3; const int32_t BundleActiveGroupHandler::MSG_CHECK_SYSTEM_INTERACTIVE_BUNDLE_STATE = 4; +======= +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) #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 @@ -39,7 +42,8 @@ BundleActiveGroupHandlerObject::BundleActiveGroupHandlerObject(const BundleActiv uid_ = orig.uid_; } -BundleActiveGroupHandler::BundleActiveGroupHandler(const bool debug) +BundleActiveGroupHandler::BundleActiveGroupHandler + (const std::shared_ptr &runner, const bool debug) : AppExecFwk::EventHandler(runner) { if (debug) { checkIdleInterval_ = ONE_MINUTE; @@ -56,22 +60,19 @@ void BundleActiveGroupHandler::Init(const std::shared_ptr(DEVICE_GROUP_HANDLE_QUEUE.c_str(), - ffrt::queue_attr().qos(ffrt::qos_default)); - if (ffrtQueue_ == nullptr) { - BUNDLE_ACTIVE_LOGE("Init failed ffrtQueue is null"); - return; - } - isInited_ = true; } +<<<<<<< HEAD void BundleActiveGroupHandler::SendCheckBundleMsg(const int32_t& eventId, const std::shared_ptr& handlerobj, const int64_t& delayTime) +======= +void BundleActiveGroupHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) { - if (!isInited_) { - BUNDLE_ACTIVE_LOGE("init failed"); + if (event == nullptr) { return; } +<<<<<<< HEAD std::string msgKey = GetMsgKey(eventId, handlerobj, delayTime); if (msgKey == "") { return; @@ -184,6 +185,14 @@ void BundleActiveGroupHandler::ProcessEvent(const int32_t& eventId, return; } BundleActiveGroupHandlerObject tmpHandlerobj = *handlerobj; +======= + switch (event->GetInnerEventId()) { + case MSG_CHECK_DEFAULT_BUNDLE_STATE: + case MSG_CHECK_NOTIFICATION_SEEN_BUNDLE_STATE: + case MSG_CHECK_SYSTEM_INTERACTIVE_BUNDLE_STATE: { + auto ptrToHandlerobj = event->GetSharedObject(); + BundleActiveGroupHandlerObject tmpHandlerobj = *ptrToHandlerobj; +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) sptr timer = MiscServices::TimeServiceClient::GetInstance(); int64_t bootBasedTimeStamp = timer->GetBootTimeMs(); bundleActiveGroupController_->CheckAndUpdateGroup( @@ -202,18 +211,25 @@ void BundleActiveGroupHandler::ProcessEvent(const int32_t& eventId, break; } case MSG_CHECK_IDLE_STATE: { +<<<<<<< HEAD if (handlerobj == nullptr) { return; } BundleActiveGroupHandlerObject tmpHandlerobj = *handlerobj; +======= + auto ptrToHandlerobj = event->GetSharedObject(); + BundleActiveGroupHandlerObject tmpHandlerobj = *ptrToHandlerobj; +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) 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(BundleActiveGroupHandler::MSG_CHECK_IDLE_STATE, handlerobjToPtr, checkIdleInterval_); + SendEvent(handlerEvent, checkIdleInterval_); } break; } diff --git a/services/packagegroup/src/bundle_active_user_history.cpp b/services/packagegroup/src/bundle_active_user_history.cpp index 57821c9..7c006dd 100644 --- a/services/packagegroup/src/bundle_active_user_history.cpp +++ b/services/packagegroup/src/bundle_active_user_history.cpp @@ -223,7 +223,7 @@ void BundleActiveUserHistory::ReportUsage(shared_ptr int32_t BundleActiveUserHistory::SetAppGroup(const string& bundleName, const int32_t userId, const int32_t uid, const int64_t bootBasedTimeStamp, int32_t newGroup, uint32_t groupReason, const bool isFlush) { - std::lock_guard lock(setGroupMutex_); + std::lock_guard lock(setGroupMutex_); BUNDLE_ACTIVE_LOGI("set %{public}s to group %{public}d, reason is %{public}d, userId is %{public}d", bundleName.c_str(), newGroup, groupReason, userId); shared_ptr>> userBundleHistory = GetUserHistory(userId, false); diff --git a/services/packageusage/include/bundle_active_report_handler.h b/services/packageusage/include/bundle_active_report_handler.h index 6f26bb5..a66317e 100644 --- a/services/packageusage/include/bundle_active_report_handler.h +++ b/services/packageusage/include/bundle_active_report_handler.h @@ -16,30 +16,38 @@ #ifndef BUNDLE_ACTIVE_REPORT_HANDLER_H #define BUNDLE_ACTIVE_REPORT_HANDLER_H +<<<<<<< HEAD #include "ffrt.h" #include #include +======= +#include "event_handler.h" +#include "event_runner.h" +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) #include "ibundle_active_service.h" #include "bundle_active_core.h" namespace OHOS { namespace DeviceUsageStats { -class BundleActiveReportHandler : public std::enable_shared_from_this { +class BundleActiveReportHandler : public AppExecFwk::EventHandler { public: - BundleActiveReportHandler() = default; + explicit BundleActiveReportHandler(const std::shared_ptr &runner); ~BundleActiveReportHandler() = default; /** * Process the event. Developers should override this method. * - * @param eventId The event id - * @param handlerobj The eventobj + * @param event The event should be processed. */ +<<<<<<< HEAD void ProcessEvent(const int32_t& eventId, const std::shared_ptr& handlerobj); void SendEvent(const int32_t& eventId, const std::shared_ptr& handlerobj, const int64_t& delayTime = 0); void RemoveEvent(const int32_t& eventId); bool HasEvent(const int32_t& eventId); +======= + void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) void Init(const std::shared_ptr& bundleActiveCore); static const int32_t MSG_REPORT_EVENT; static const int32_t MSG_FLUSH_TO_DISK; @@ -48,10 +56,13 @@ public: static const int32_t MSG_SWITCH_USER; private: +<<<<<<< HEAD bool isInited_ = false; ffrt::mutex taskHandlerMutex_; std::shared_ptr ffrtQueue_; std::map> taskHandlerMap_; +======= +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) std::shared_ptr bundleActiveCore_; }; } // namespace DeviceUsageStats diff --git a/services/packageusage/src/bundle_active_report_handler.cpp b/services/packageusage/src/bundle_active_report_handler.cpp index 298c15d..e376621 100644 --- a/services/packageusage/src/bundle_active_report_handler.cpp +++ b/services/packageusage/src/bundle_active_report_handler.cpp @@ -20,16 +20,24 @@ namespace OHOS { namespace DeviceUsageStats { +<<<<<<< HEAD const std::string DEVICE_USAGE_REPORT_HANDLE_QUEUE = "DeviceUsageReportHandleQueue"; const int32_t BundleActiveReportHandler::MSG_REPORT_EVENT = 0; const int32_t BundleActiveReportHandler::MSG_FLUSH_TO_DISK = 1; const int32_t BundleActiveReportHandler::MSG_REMOVE_USER = 2; const int32_t BundleActiveReportHandler::MSG_BUNDLE_UNINSTALLED = 3; const int32_t BundleActiveReportHandler::MSG_SWITCH_USER = 4; +======= +BundleActiveReportHandler::BundleActiveReportHandler + (const std::shared_ptr &runner) : AppExecFwk::EventHandler(runner) +{ +} +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) void BundleActiveReportHandler::Init(const std::shared_ptr& bundleActiveCore) { bundleActiveCore_ = bundleActiveCore; +<<<<<<< HEAD ffrtQueue_ = std::make_shared(DEVICE_USAGE_REPORT_HANDLE_QUEUE.c_str(), ffrt::queue_attr().qos(ffrt::qos_default)); if (ffrtQueue_ == nullptr) { @@ -41,11 +49,17 @@ void BundleActiveReportHandler::Init(const std::shared_ptr& bu void BundleActiveReportHandler::SendEvent(const int32_t& eventId, const std::shared_ptr& handlerobj, const int64_t& delayTime) +======= +} + +void BundleActiveReportHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) { - if (!isInited_) { - BUNDLE_ACTIVE_LOGE("init failed"); + if (event == nullptr) { + BUNDLE_ACTIVE_LOGE("event is null, exit ProcessEvent"); return; } +<<<<<<< HEAD auto reportHandler = shared_from_this(); int64_t ffrtDelayTime = BundleActiveUtil::GetFFRTDelayTime(delayTime); std::lock_guard lock(taskHandlerMutex_); @@ -99,13 +113,19 @@ void BundleActiveReportHandler::ProcessEvent(const int32_t& eventId, } BundleActiveReportHandlerObject tmpHandlerobj = *handlerobj; switch (eventId) { +======= + auto ptrToHandlerobj = event->GetSharedObject(); + switch (event->GetInnerEventId()) { +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) case MSG_REPORT_EVENT: { BUNDLE_ACTIVE_LOGD("MSG_REPORT_EVENT CALLED"); + BundleActiveReportHandlerObject tmpHandlerobj = *ptrToHandlerobj; bundleActiveCore_->ReportEvent(tmpHandlerobj.event_, tmpHandlerobj.userId_); break; } case MSG_FLUSH_TO_DISK: { BUNDLE_ACTIVE_LOGI("FLUSH TO DISK HANDLE"); + BundleActiveReportHandlerObject tmpHandlerobj = *ptrToHandlerobj; if (tmpHandlerobj.userId_ != bundleActiveCore_->currentUsedUser_) { BUNDLE_ACTIVE_LOGE("flush user is %{public}d, not last user %{public}d, return", tmpHandlerobj.userId_, bundleActiveCore_->currentUsedUser_); @@ -116,17 +136,20 @@ void BundleActiveReportHandler::ProcessEvent(const int32_t& eventId, break; } case MSG_REMOVE_USER: { + BundleActiveReportHandlerObject tmpHandlerobj = *ptrToHandlerobj; bundleActiveCore_->OnUserRemoved(tmpHandlerobj.userId_); break; } case MSG_BUNDLE_UNINSTALLED: { BUNDLE_ACTIVE_LOGI("MSG_BUNDLE_UNINSTALLED CALLED"); + BundleActiveReportHandlerObject tmpHandlerobj = *ptrToHandlerobj; bundleActiveCore_->OnBundleUninstalled(tmpHandlerobj.userId_, tmpHandlerobj.bundleName_, tmpHandlerobj.uid_, tmpHandlerobj.appIndex_); break; } case MSG_SWITCH_USER: { BUNDLE_ACTIVE_LOGI("MSG_SWITCH_USER CALLED"); + BundleActiveReportHandlerObject tmpHandlerobj = *ptrToHandlerobj; bundleActiveCore_->OnUserSwitched(tmpHandlerobj.userId_); break; } diff --git a/test/unittest/bundle_active_total_test.cpp b/test/unittest/bundle_active_total_test.cpp index 43c2ee8..7b96bbd 100644 --- a/test/unittest/bundle_active_total_test.cpp +++ b/test/unittest/bundle_active_total_test.cpp @@ -172,7 +172,8 @@ HWTEST_F(BundleActiveTotalTest, BundleActiveBundleMgrHelperTest_003, Function | HWTEST_F(BundleActiveTotalTest, BundleActiveAppStateObserverTest_001, Function | MediumTest | Level0) { AbilityStateData abilityStateData; - auto reportHandler = std::make_shared(); + std::shared_ptr runner; + auto reportHandler = std::make_shared(runner); BundleActiveAppStateObserver test; test.Init(reportHandler); test.OnAbilityStateChanged(abilityStateData); diff --git a/test/unittest/device_usage_statistics_service_test.cpp b/test/unittest/device_usage_statistics_service_test.cpp index 0b356e8..b9c4779 100644 --- a/test/unittest/device_usage_statistics_service_test.cpp +++ b/test/unittest/device_usage_statistics_service_test.cpp @@ -14,7 +14,6 @@ */ #include -#include #include #include "system_ability_definition.h" @@ -39,23 +38,32 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); +<<<<<<< HEAD static std::shared_ptr bundleActiveCore_; +======= +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) }; std::shared_ptr DeviceUsageStatisticsServiceTest::bundleActiveCore_ = nullptr; void DeviceUsageStatisticsServiceTest::SetUpTestCase(void) { +<<<<<<< HEAD bundleActiveCore_ = std::make_shared(); bundleActiveCore_->Init(); bundleActiveCore_->InitBundleGroupController(); +======= +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) } void DeviceUsageStatisticsServiceTest::TearDownTestCase(void) { +<<<<<<< HEAD bundleActiveCore_->bundleGroupHandler_->ffrtQueue_.reset(); int64_t sleepTime = 10; std::this_thread::sleep_for(std::chrono::seconds(sleepTime)); +======= +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) } void DeviceUsageStatisticsServiceTest::SetUp(void) @@ -211,9 +219,12 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_AppG HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_AppGroupCallback_002, Function | MediumTest | Level0) { + auto bundleActiveCore = std::make_shared(); + auto runner = AppExecFwk::EventRunner::Create("test"); + bundleActiveCore->InitBundleGroupController(runner); sptr observer = new (std::nothrow) TestServiceAppGroupChangeCallback(); Security::AccessToken::AccessTokenID tokenId {}; - bundleActiveCore_->groupChangeObservers_[tokenId] = observer; + bundleActiveCore->groupChangeObservers_[tokenId] = observer; int32_t userId = 100; int32_t newGroup = 10; int32_t oldGroup = 60; @@ -233,7 +244,10 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_OnUs Function | MediumTest | Level0) { int userId = 100; - auto coreObject = bundleActiveCore_; + auto coreObject = std::make_shared(); + coreObject->bundleGroupController_ = std::make_shared(coreObject->debugCore_); + auto runner = AppExecFwk::EventRunner::Create("test"); + coreObject->InitBundleGroupController(runner); coreObject->RestoreToDatabase(userId); auto userService = std::make_shared(userId, *(coreObject.get()), false); coreObject->userStatServices_[userId] = userService; @@ -255,7 +269,11 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_OnUs HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_RestoreAllData_001, Function | MediumTest | Level0) { - auto coreObject = bundleActiveCore_; + auto coreObject = std::make_shared(); + coreObject->bundleGroupController_ = std::make_shared(coreObject->debugCore_); + auto runner = AppExecFwk::EventRunner::Create("test"); + coreObject->InitBundleGroupController(runner); + int userId = 100; auto userService = std::make_shared(userId, *(coreObject.get()), false); int64_t timeStamp = 20000000000000; @@ -279,7 +297,8 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_Rest HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_ObtainSystemEventName_001, Function | MediumTest | Level0) { - auto coreObject = bundleActiveCore_; + auto coreObject = std::make_shared(); + coreObject->bundleGroupController_ = std::make_shared(coreObject->debugCore_); BundleActiveEvent event; event.eventId_ = BundleActiveEvent::SYSTEM_LOCK; coreObject->ObtainSystemEventName(event); @@ -661,9 +680,17 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_GetS HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_ReportEvent_001, Function | MediumTest | Level0) { +<<<<<<< HEAD bundleActiveCore_ = std::make_shared(); bundleActiveCore_->Init(); bundleActiveCore_->InitBundleGroupController(); +======= + auto coreObject = std::make_shared(); + coreObject->Init(); + auto runner = AppExecFwk::EventRunner::Create("test"); + coreObject->InitBundleGroupController(runner); + +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) BundleActiveEvent event; int32_t userId = 0; EXPECT_NE(bundleActiveCore_->ReportEvent(event, userId), ERR_OK); @@ -689,8 +716,9 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_Repo HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_InitBundleGroupController_001, Function | MediumTest | Level0) { - auto coreObject = bundleActiveCore_; - coreObject->InitBundleGroupController(); + auto coreObject = std::make_shared(); + auto runner = AppExecFwk::EventRunner::Create("test"); + coreObject->InitBundleGroupController(runner); } /* @@ -712,7 +740,8 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_Repo coreObject->ReportEventToAllUserId(event); coreObject->Init(); - coreObject->InitBundleGroupController(); + auto runner = AppExecFwk::EventRunner::Create("test"); + coreObject->InitBundleGroupController(runner); coreObject->ReportEventToAllUserId(event); EXPECT_NE(coreObject, nullptr); } @@ -726,7 +755,11 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_Repo HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_RestoreToDatabase_001, Function | MediumTest | Level0) { - auto coreObject = bundleActiveCore_; + auto coreObject = std::make_shared(); + coreObject->bundleGroupController_ = std::make_shared(coreObject->debugCore_); + auto runner = AppExecFwk::EventRunner::Create("test"); + coreObject->InitBundleGroupController(runner); + int userId = 100; auto userService = std::make_shared(userId, *(coreObject.get()), false); int64_t timeStamp = 20000000000000; @@ -748,7 +781,11 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_Rest HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveGroupControllerTest_001, Function | MediumTest | Level0) { - auto coreObject = bundleActiveCore_; + auto coreObject = std::make_shared(); + coreObject->bundleGroupController_ = std::make_shared(coreObject->debugCore_); + auto runner = AppExecFwk::EventRunner::Create("test"); + coreObject->InitBundleGroupController(runner); + int userId = 100; auto userService = std::make_shared(userId, *(coreObject.get()), false); int64_t timeStamp = 20000000000000; @@ -766,7 +803,11 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveGroupControllerTest_001, HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveGroupControllerTest_002, Function | MediumTest | Level0) { - auto coreObject = bundleActiveCore_; + auto coreObject = std::make_shared(); + coreObject->bundleGroupController_ = std::make_shared(coreObject->debugCore_); + auto runner = AppExecFwk::EventRunner::Create("test"); + coreObject->InitBundleGroupController(runner); + int userId = 100; auto userService = std::make_shared(userId, *(coreObject.get()), false); int64_t timeStamp = 20000000000000; @@ -784,7 +825,15 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveGroupControllerTest_002, HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveGroupControllerTest_003, Function | MediumTest | Level0) { +<<<<<<< HEAD auto coreObject = bundleActiveCore_; +======= + auto coreObject = std::make_shared(); + coreObject->bundleGroupController_ = std::make_shared(true); + auto runner = AppExecFwk::EventRunner::Create("test"); + coreObject->InitBundleGroupController(runner); + +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) int userId = 100; auto userService = std::make_shared(userId, *(coreObject.get()), false); int64_t timeStamp = 20000000000000; @@ -802,7 +851,10 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveGroupControllerTest_003, HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveGroupControllerTest_004, Function | MediumTest | Level0) { - auto coreObject = bundleActiveCore_; + auto coreObject = std::make_shared(); + coreObject->bundleGroupController_ = std::make_shared(true); + auto runner = AppExecFwk::EventRunner::Create("test"); + coreObject->InitBundleGroupController(runner); int userId = 100; BundleActiveEvent event; int64_t timeStamp = 20000000000000; @@ -822,7 +874,10 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveGroupControllerTest_004, HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveGroupControllerTest_005, Function | MediumTest | Level0) { - auto coreObject = bundleActiveCore_; + auto coreObject = std::make_shared(); + coreObject->bundleGroupController_ = std::make_shared(true); + auto runner = AppExecFwk::EventRunner::Create("test"); + coreObject->InitBundleGroupController(runner); int userId = 100; int64_t timeStamp = 20000000000000; coreObject->bundleGroupController_->CheckAndUpdateGroup("test", userId, 0, timeStamp); @@ -837,7 +892,10 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveGroupControllerTest_005, HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveGroupControllerTest_006, Function | MediumTest | Level0) { - auto coreObject = bundleActiveCore_; + auto coreObject = std::make_shared(); + coreObject->bundleGroupController_ = std::make_shared(true); + auto runner = AppExecFwk::EventRunner::Create("test"); + coreObject->InitBundleGroupController(runner); int userId = 100; int64_t timeStamp = 20000000000000; coreObject->bundleGroupController_->SetAppGroup("test", userId, 0, 0, timeStamp, true); @@ -853,7 +911,10 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveGroupControllerTest_006, HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveGroupControllerTest_007, Function | MediumTest | Level0) { - auto coreObject = bundleActiveCore_; + auto coreObject = std::make_shared(); + coreObject->bundleGroupController_ = std::make_shared(true); + auto runner = AppExecFwk::EventRunner::Create("test"); + coreObject->InitBundleGroupController(runner); int userId = 100; coreObject->bundleGroupController_->IsBundleIdle("test", userId); } @@ -869,7 +930,12 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveGroupControllerTest_009, { auto coreObject = std::make_shared(); coreObject->bundleGroupController_ = std::make_shared(true); +<<<<<<< HEAD coreObject->InitBundleGroupController(); +======= + auto runner = AppExecFwk::EventRunner::Create("test"); + coreObject->InitBundleGroupController(runner); +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) int userId = 100; coreObject->bundleGroupController_->IsBundleInstalled("test", userId); coreObject->bundleGroupController_->sptrBundleMgr_ = nullptr; @@ -885,7 +951,10 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveGroupControllerTest_009, HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveGroupControllerTest_010, Function | MediumTest | Level0) { - auto coreObject = bundleActiveCore_; + auto coreObject = std::make_shared(); + coreObject->bundleGroupController_ = std::make_shared(true); + auto runner = AppExecFwk::EventRunner::Create("test"); + coreObject->InitBundleGroupController(runner); int userId = 100; int64_t timeStamp = 20000000000000; coreObject->bundleGroupController_->ShutDown(timeStamp, userId); @@ -900,6 +969,13 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveGroupControllerTest_010, HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveGroupControllerTest_011, Function | MediumTest | Level0) { +<<<<<<< HEAD +======= + auto bundleActiveCore = std::make_shared(); + bundleActiveCore->bundleGroupController_ = std::make_shared(true); + auto runner = AppExecFwk::EventRunner::Create("test"); + bundleActiveCore->InitBundleGroupController(runner); +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) bool isScreenOn = true; int64_t timeStamp = 0; bundleActiveCore_->bundleGroupController_->OnScreenChanged(isScreenOn, timeStamp); @@ -916,9 +992,10 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveUserHistoryTest_001, Function | MediumTest | Level0) { int64_t bootBasedTimeStamp = 2000; + std::shared_ptr bundleActiveCore; std::vector screenTimeLevel; std::vector bootFromTimeLevel; - auto bundleUserHistory_ = std::make_shared(bootBasedTimeStamp, bundleActiveCore_); + auto bundleUserHistory_ = std::make_shared(bootBasedTimeStamp, bundleActiveCore); bundleUserHistory_->GetLevelIndex("test", 0, 20000, screenTimeLevel, bootFromTimeLevel, 0); } @@ -932,7 +1009,8 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveUserHistoryTest_002, Function | MediumTest | Level0) { int64_t bootBasedTimeStamp = 2000; - auto bundleUserHistory_ = std::make_shared(bootBasedTimeStamp, bundleActiveCore_); + std::shared_ptr bundleActiveCore; + auto bundleUserHistory_ = std::make_shared(bootBasedTimeStamp, bundleActiveCore); bundleUserHistory_->WriteDeviceDuration(); bundleUserHistory_->OnBundleUninstalled(0, "test", 0, 0); } @@ -947,8 +1025,9 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveUserHistoryTest_003, Function | MediumTest | Level0) { int64_t bootBasedTimeStamp = 2000; + std::shared_ptr bundleActiveCore; auto oneBundleUsageHistory = std::make_shared(); - auto bundleUserHistory_ = std::make_shared(bootBasedTimeStamp, bundleActiveCore_); + auto bundleUserHistory_ = std::make_shared(bootBasedTimeStamp, bundleActiveCore); bundleUserHistory_->ReportUsage(oneBundleUsageHistory, "test", 0, 0, 1000, 2000, 100, 0); bundleUserHistory_->ReportUsage(oneBundleUsageHistory, "test", 0, 0, 2000, 1000, 100, 0); bundleUserHistory_->ReportUsage(oneBundleUsageHistory, "test", 10, 0, 1000, 2000, 100, 0); @@ -965,12 +1044,13 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveUserHistoryTest_003, HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveUserHistoryTest_004, Function | MediumTest | Level0) { + std::shared_ptr bundleActiveCore; int32_t userId = 100; int64_t bootBasedTimeStamp = 2000; int32_t newgroup = 0; uint32_t groupReason = 0; int32_t uid = 0; - auto bundleUserHistory_ = std::make_shared(bootBasedTimeStamp, bundleActiveCore_); + auto bundleUserHistory_ = std::make_shared(bootBasedTimeStamp, bundleActiveCore); bundleUserHistory_->SetAppGroup("test", userId, uid, bootBasedTimeStamp, newgroup, groupReason, true); bundleUserHistory_->SetAppGroup("test", userId, uid, bootBasedTimeStamp, newgroup, groupReason, false); } @@ -984,8 +1064,9 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveUserHistoryTest_004, HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveUserHistoryTest_005, Function | MediumTest | Level0) { + std::shared_ptr bundleActiveCore; int64_t bootBasedTimeStamp = 2000; - auto bundleUserHistory_ = std::make_shared(bootBasedTimeStamp, bundleActiveCore_); + auto bundleUserHistory_ = std::make_shared(bootBasedTimeStamp, bundleActiveCore); bundleUserHistory_->PrintData(0); } @@ -998,7 +1079,10 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveUserHistoryTest_005, HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_ShutDown_001, Function | MediumTest | Level0) { - auto coreObject = bundleActiveCore_; + auto coreObject = std::make_shared(); + coreObject->Init(); + auto runner = AppExecFwk::EventRunner::Create("test"); + coreObject->InitBundleGroupController(runner); coreObject->ShutDown(); EXPECT_NE(coreObject, nullptr); } @@ -1013,8 +1097,11 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_Chec Function | MediumTest | Level0) { int userId = 100; - auto coreObject = bundleActiveCore_; + auto coreObject = std::make_shared(); coreObject->OnUserRemoved(100); + coreObject->Init(); + auto runner = AppExecFwk::EventRunner::Create("test"); + coreObject->InitBundleGroupController(runner); auto userService = std::make_shared(userId, *(coreObject.get()), false); coreObject->userStatServices_[userId] = userService; @@ -1032,7 +1119,11 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_Chec HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_QueryAppGroup_001, Function | MediumTest | Level0) { +<<<<<<< HEAD auto groupController = bundleActiveCore_->bundleGroupController_; +======= + auto groupController = std::make_shared(false); +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) int32_t appGroup = 10; std::string bundleName = ""; int32_t userId = 100; @@ -1052,7 +1143,14 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_Quer HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_ControllerReportEvent_001, Function | MediumTest | Level0) { +<<<<<<< HEAD auto coreObject = bundleActiveCore_; +======= + auto coreObject = std::make_shared(); + coreObject->Init(); + auto runner = AppExecFwk::EventRunner::Create("test"); + coreObject->InitBundleGroupController(runner); +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) int64_t bootBasedTimeStamp = 20000000000000; BundleActiveEvent event; @@ -1081,7 +1179,10 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_Cont HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_CheckAndUpdateGroup_001, Function | MediumTest | Level0) { - auto coreObject = bundleActiveCore_; + auto coreObject = std::make_shared(); + coreObject->Init(); + auto runner = AppExecFwk::EventRunner::Create("test"); + coreObject->InitBundleGroupController(runner); std::string bundleName = "com.ohos.camera"; int32_t userId = 100; @@ -1112,7 +1213,10 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_Chec HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_PreservePowerStateInfo_001, Function | MediumTest | Level0) { - auto coreObject = bundleActiveCore_; + auto coreObject = std::make_shared(); + coreObject->Init(); + auto runner = AppExecFwk::EventRunner::Create("test"); + coreObject->InitBundleGroupController(runner); int32_t eventId = BundleActiveEvent::ABILITY_FOREGROUND; coreObject->PreservePowerStateInfo(eventId); @@ -1391,7 +1495,7 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_onSt HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_DeleteUninstalledBundleStats_001, Function | MediumTest | Level0) { - auto coreObject = bundleActiveCore_; + auto coreObject = std::make_shared(); int userId = 100; auto userService = std::make_shared(userId, *(coreObject.get()), false); int appIndex = 1; @@ -1419,7 +1523,7 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_Dele HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_DeleteUninstalledBundleStats_002, Function | MediumTest | Level0) { - auto coreObject = bundleActiveCore_; + auto coreObject = std::make_shared(); int userId = 100; auto userService = std::make_shared(userId, *(coreObject.get()), false); int appIndex = 1; diff --git a/test/unittest/package_usage_test.cpp b/test/unittest/package_usage_test.cpp index ffb8dab..91ec49a 100644 --- a/test/unittest/package_usage_test.cpp +++ b/test/unittest/package_usage_test.cpp @@ -43,16 +43,22 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); +<<<<<<< HEAD static std::shared_ptr bundleActiveCore_; +======= +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) }; std::shared_ptr PackageUsageTest::bundleActiveCore_ = nullptr; void PackageUsageTest::SetUpTestCase(void) { +<<<<<<< HEAD bundleActiveCore_ = std::make_shared(); bundleActiveCore_->Init(); bundleActiveCore_->InitBundleGroupController(); +======= +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) } void PackageUsageTest::TearDownTestCase(void) @@ -285,7 +291,10 @@ HWTEST_F(PackageUsageTest, PackageUsageTest_BundleActiveEventStats_001, Function HWTEST_F(PackageUsageTest, PackageUsageTest_ReportForShutdown_001, Function | MediumTest | Level0) { int32_t userId = 100; - auto bundleActiveCore = bundleActiveCore_; + auto bundleActiveCore = std::make_shared(); + bundleActiveCore->Init(); + auto runner = AppExecFwk::EventRunner::Create("test"); + bundleActiveCore->InitBundleGroupController(runner); auto bundleUserService = std::make_shared(userId, *(bundleActiveCore.get()), false); BundleActiveEvent event; event.eventId_ = BundleActiveEvent::ABILITY_FOREGROUND; @@ -309,7 +318,11 @@ HWTEST_F(PackageUsageTest, PackageUsageTest_ReportForShutdown_001, Function | Me HWTEST_F(PackageUsageTest, PackageUsageTest_ReportFormEvent_001, Function | MediumTest | Level0) { int32_t userId = 100; +<<<<<<< HEAD auto bundleActiveCore = bundleActiveCore_; +======= + auto bundleActiveCore = std::make_shared(); +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) auto bundleUserService = std::make_shared(userId, *(bundleActiveCore.get()), true); BundleActiveEvent event; event.bundleName_ = "defaultBundleName"; @@ -331,7 +344,11 @@ HWTEST_F(PackageUsageTest, PackageUsageTest_ReportFormEvent_001, Function | Medi HWTEST_F(PackageUsageTest, PackageUsageTest_PrintInMemFormStats_001, Function | MediumTest | Level0) { int32_t userId = 100; +<<<<<<< HEAD auto bundleActiveCore = bundleActiveCore_; +======= + auto bundleActiveCore = std::make_shared(); +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) auto bundleUserService = std::make_shared(userId, *(bundleActiveCore.get()), true); BundleActiveEvent event; event.bundleName_ = "defaultBundleName"; @@ -352,7 +369,14 @@ HWTEST_F(PackageUsageTest, PackageUsageTest_PrintInMemFormStats_001, Function | HWTEST_F(PackageUsageTest, PackageUsageTest_QueryDeviceEventStats_001, Function | MediumTest | Level0) { int32_t userId = 100; +<<<<<<< HEAD auto bundleActiveCore = bundleActiveCore_; +======= + auto bundleActiveCore = std::make_shared(); + bundleActiveCore->Init(); + auto runner = AppExecFwk::EventRunner::Create("test"); + bundleActiveCore->InitBundleGroupController(runner); +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) auto bundleUserService = std::make_shared(userId, *(bundleActiveCore.get()), true); int64_t timeStamp = 20000000000000; bundleUserService->Init(timeStamp); @@ -383,7 +407,14 @@ HWTEST_F(PackageUsageTest, PackageUsageTest_QueryDeviceEventStats_001, Function HWTEST_F(PackageUsageTest, PackageUsageTest_QueryNotificationEventStats_001, Function | MediumTest | Level0) { int32_t userId = 100; +<<<<<<< HEAD auto bundleActiveCore = bundleActiveCore_; +======= + auto bundleActiveCore = std::make_shared(); + bundleActiveCore->Init(); + auto runner = AppExecFwk::EventRunner::Create("test"); + bundleActiveCore->InitBundleGroupController(runner); +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) auto bundleUserService = std::make_shared(userId, *(bundleActiveCore.get()), true); int64_t timeStamp = 20000000000000; bundleUserService->Init(timeStamp); @@ -419,7 +450,14 @@ HWTEST_F(PackageUsageTest, PackageUsageTest_QueryNotificationEventStats_001, Fun HWTEST_F(PackageUsageTest, PackageUsageTest_QueryBundleEvents_001, Function | MediumTest | Level0) { int32_t userId = 100; +<<<<<<< HEAD auto bundleActiveCore = bundleActiveCore_; +======= + auto bundleActiveCore = std::make_shared(); + bundleActiveCore->Init(); + auto runner = AppExecFwk::EventRunner::Create("test"); + bundleActiveCore->InitBundleGroupController(runner); +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) auto bundleUserService = std::make_shared(userId, *(bundleActiveCore.get()), true); int64_t timeStamp = 20000000000000; bundleUserService->Init(timeStamp); @@ -457,7 +495,14 @@ HWTEST_F(PackageUsageTest, PackageUsageTest_QueryBundleEvents_001, Function | Me HWTEST_F(PackageUsageTest, PackageUsageTest_PrintInMemPackageStats_001, Function | MediumTest | Level0) { int32_t userId = 100; +<<<<<<< HEAD auto bundleActiveCore = bundleActiveCore_; +======= + auto bundleActiveCore = std::make_shared(); + bundleActiveCore->Init(); + auto runner = AppExecFwk::EventRunner::Create("test"); + bundleActiveCore->InitBundleGroupController(runner); +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) auto bundleUserService = std::make_shared(userId, *(bundleActiveCore.get()), true); int64_t timeStamp = 20000000000000; bundleUserService->Init(timeStamp); @@ -478,7 +523,14 @@ HWTEST_F(PackageUsageTest, PackageUsageTest_PrintInMemPackageStats_001, Function HWTEST_F(PackageUsageTest, PackageUsageTest_QueryBundleStatsInfos_001, Function | MediumTest | Level0) { int32_t userId = 100; +<<<<<<< HEAD auto bundleActiveCore = bundleActiveCore_; +======= + auto bundleActiveCore = std::make_shared(); + bundleActiveCore->Init(); + auto runner = AppExecFwk::EventRunner::Create("test"); + bundleActiveCore->InitBundleGroupController(runner); +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) auto bundleUserService = std::make_shared(userId, *(bundleActiveCore.get()), true); int64_t timeStamp = 20000000000000; bundleUserService->Init(timeStamp); @@ -523,7 +575,14 @@ HWTEST_F(PackageUsageTest, PackageUsageTest_QueryBundleStatsInfos_001, Function HWTEST_F(PackageUsageTest, PackageUsageTest_QueryBundleStatsInfos_002, Function | MediumTest | Level0) { int32_t userId = 100; +<<<<<<< HEAD auto bundleActiveCore = bundleActiveCore_; +======= + auto bundleActiveCore = std::make_shared(); + bundleActiveCore->Init(); + auto runner = AppExecFwk::EventRunner::Create("test"); + bundleActiveCore->InitBundleGroupController(runner); +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) auto bundleUserService = std::make_shared(userId, *(bundleActiveCore.get()), true); int64_t timeStamp = 20000000000000; bundleUserService->Init(timeStamp); @@ -577,7 +636,10 @@ HWTEST_F(PackageUsageTest, PackageUsageTest_QueryBundleStatsInfos_002, Function HWTEST_F(PackageUsageTest, PackageUsageTest_RestoreStats_001, Function | MediumTest | Level0) { int32_t userId = 100; - auto bundleActiveCore = bundleActiveCore_; + auto bundleActiveCore = std::make_shared(); + bundleActiveCore->Init(); + auto runner = AppExecFwk::EventRunner::Create("test"); + bundleActiveCore->InitBundleGroupController(runner); auto bundleUserService = std::make_shared(userId, *(bundleActiveCore.get()), true); int64_t timeStamp = 20000000000000; bundleUserService->Init(timeStamp); @@ -607,7 +669,10 @@ HWTEST_F(PackageUsageTest, PackageUsageTest_RestoreStats_001, Function | MediumT HWTEST_F(PackageUsageTest, PackageUsageTest_LoadActiveStats_001, Function | MediumTest | Level0) { int32_t userId = 100; - auto bundleActiveCore = bundleActiveCore_; + auto bundleActiveCore = std::make_shared(); + bundleActiveCore->Init(); + auto runner = AppExecFwk::EventRunner::Create("test"); + bundleActiveCore->InitBundleGroupController(runner); auto bundleUserService = std::make_shared(userId, *(bundleActiveCore.get()), true); int64_t timeStamp = 20000000000000; bundleUserService->Init(timeStamp); @@ -671,7 +736,10 @@ HWTEST_F(PackageUsageTest, PackageUsageTest_combine_001, Function | MediumTest | HWTEST_F(PackageUsageTest, PackageUsageTest_ReportEvent_001, Function | MediumTest | Level0) { int32_t userId = 100; - auto bundleActiveCore = bundleActiveCore_; + auto bundleActiveCore = std::make_shared(); + bundleActiveCore->Init(); + auto runner = AppExecFwk::EventRunner::Create("test"); + bundleActiveCore->InitBundleGroupController(runner); auto bundleUserService = std::make_shared(userId, *(bundleActiveCore.get()), true); int64_t timeStamp = 20000000000; bundleUserService->Init(timeStamp); @@ -708,7 +776,10 @@ HWTEST_F(PackageUsageTest, PackageUsageTest_ReportEvent_001, Function | MediumTe HWTEST_F(PackageUsageTest, PackageUsageTest_RenewStatsInMemory_001, Function | MediumTest | Level0) { int32_t userId = 100; - auto bundleActiveCore = bundleActiveCore_; + auto bundleActiveCore = std::make_shared(); + bundleActiveCore->Init(); + auto runner = AppExecFwk::EventRunner::Create("test"); + bundleActiveCore->InitBundleGroupController(runner); auto bundleUserService = std::make_shared(userId, *(bundleActiveCore.get()), true); int64_t timeStamp = 20000000000; bundleUserService->Init(timeStamp); @@ -731,6 +802,33 @@ HWTEST_F(PackageUsageTest, PackageUsageTest_RenewStatsInMemory_001, Function | M } /* +<<<<<<< HEAD +======= + * @tc.name: BundleActiveReportHandlerTest_001 + * @tc.desc: RenewStatsInMemory + * @tc.type: FUNC + * @tc.require: DTS2023121404861 + */ +HWTEST_F(PackageUsageTest, BundleActiveReportHandlerTest_001, Function | MediumTest | Level0) +{ + std::string threadName = "bundle_active_report_handler"; + auto runner = AppExecFwk::EventRunner::Create(threadName); + if (runner == nullptr) { + BUNDLE_ACTIVE_LOGE("report handler is null"); + return; + } + std::shared_ptr bundleActiveReportHandler; + std::shared_ptr bundleActiveCore; + AppExecFwk::InnerEvent::Pointer pointer = AppExecFwk::InnerEvent::Get(0); + pointer.release(); + bundleActiveReportHandler = std::make_shared(runner); + bundleActiveReportHandler->Init(bundleActiveCore); + bundleActiveReportHandler->ProcessEvent(pointer); + bundleActiveReportHandler->ProcessEvent(pointer); +} + +/* +>>>>>>> parent of 3bbd93c (eventHandler切换ffrt) * @tc.name: BundleActiveGroupController_001 * @tc.desc: DeleteMemoryUsageGroup * @tc.type: FUNC -- Gitee