diff --git a/services/common/include/bundle_active_common_event_subscriber.h b/services/common/include/bundle_active_common_event_subscriber.h index e54fbc07db82a2273d9a7cf88eda51a86eca72b4..c1e0b78f0238258438d0204232bfe085e11731ae 100644 --- a/services/common/include/bundle_active_common_event_subscriber.h +++ b/services/common/include/bundle_active_common_event_subscriber.h @@ -47,9 +47,14 @@ public: bundleActiveReportHandler_(bundleActiveReportHandler) {} ~BundleActiveCommonEventSubscriber() = default; void OnReceiveEvent(const CommonEventData &data) override; - void HandleLockEvent(const std::string& action, const int32_t userId); private: + void HandleScreenEvent(); + void HandleUserRemoveEvent(const CommonEventData &data); + void HandleUserSwitchEvent(const CommonEventData &data); + void HandlePackageRemoveEvent(const CommonEventData &data); + void HandlePackageAddEvent(const CommonEventData &data); + void HandleLockEvent(const CommonEventData &data); ffrt::mutex mutex_; std::weak_ptr activeGroupController_; std::weak_ptr bundleActiveReportHandler_; diff --git a/services/common/include/bundle_active_core.h b/services/common/include/bundle_active_core.h index eec0a7f19e3ce1df78d8ede0f25f1b20f8e64dd9..8fd5f3e66bb2c93b05457c8dafb97b929d3507f4 100644 --- a/services/common/include/bundle_active_core.h +++ b/services/common/include/bundle_active_core.h @@ -243,6 +243,8 @@ public: int32_t currentUsedUser_; void OnAppGroupChanged(const AppGroupCallbackInfo& callbackInfo); bool isUninstalledApp(const int32_t uid); + void OnBundleInstalled(const int32_t userId, const std::string& bundleName, const int32_t uid, + const int32_t appIndex); private: void NotifOberserverGroupChanged(const AppGroupCallbackInfo& callbackInfo, AccessToken::HapTokenInfo tokenInfo); @@ -265,6 +267,7 @@ private: ffrt::mutex mutex_; ffrt::recursive_mutex callbackMutex_; std::map> userStatServices_; + std::map taskMap_; void RegisterSubscriber(); std::shared_ptr commonEventSubscriber_; void RestoreAllData(); diff --git a/services/common/src/bundle_active_core.cpp b/services/common/src/bundle_active_core.cpp index d1725575247fafcc60c94b05714f10f45d2cee37..fdad002b40b99ce578ff4977a7444a1b516d7b58 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -23,13 +23,15 @@ #include "bundle_active_group_common.h" #include "bundle_active_bundle_mgr_helper.h" #include "bundle_active_constant.h" +#include "ffrt_inner.h" + namespace OHOS { namespace DeviceUsageStats { #ifndef OS_ACCOUNT_PART_ENABLED const int32_t DEFAULT_OS_ACCOUNT_ID = 0; // 0 is the default id when there is no os_account part #endif // OS_ACCOUNT_PART_ENABLED -constexpr int32_t BUNDLE_UNINSTALL_DELAY_TIME = 60 * 1000 * 1000; +constexpr int32_t BUNDLE_UNINSTALL_DELAY_TIME = 5 * 1000 * 1000; BundleActiveReportHandlerObject::BundleActiveReportHandlerObject() { @@ -76,52 +78,116 @@ void BundleActiveCommonEventSubscriber::OnReceiveEvent(const CommonEventData &da std::string action = data.GetWant().GetAction(); if (action == CommonEventSupport::COMMON_EVENT_SCREEN_OFF || action == CommonEventSupport::COMMON_EVENT_SCREEN_ON) { - if (!activeGroupController_.expired()) { - sptr timer = MiscServices::TimeServiceClient::GetInstance(); - bool isScreenOn = activeGroupController_.lock()->IsScreenOn(); - BUNDLE_ACTIVE_LOGI("screen state change to %{public}d", isScreenOn); - activeGroupController_.lock()->OnScreenChanged(isScreenOn, timer->GetBootTimeMs()); - } + HandleScreenEvent(); } else if (action == CommonEventSupport::COMMON_EVENT_USER_REMOVED) { - if (!bundleActiveReportHandler_.expired()) { - int32_t userId = data.GetCode(); - BUNDLE_ACTIVE_LOGI("remove user id %{public}d", userId); - BundleActiveReportHandlerObject tmpHandlerObject(userId, ""); - std::shared_ptr handlerobjToPtr = - std::make_shared(tmpHandlerObject); - bundleActiveReportHandler_.lock()->SendEvent(BundleActiveReportHandler::MSG_REMOVE_USER, handlerobjToPtr); - } + HandleUserRemoveEvent(data); } else if (action == CommonEventSupport::COMMON_EVENT_USER_SWITCHED) { + HandleUserSwitchEvent(data); + } else if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED || + action == CommonEventSupport::COMMON_EVENT_PACKAGE_FULLY_REMOVED) { + HandlePackageRemoveEvent(data); + } else if (action == COMMON_EVENT_UNLOCK_SCREEN || action == COMMON_EVENT_LOCK_SCREEN) { + HandleLockEvent(data); + } else if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED) { + HandlePackageAddEvent(data); + } +} + +void BundleActiveCommonEventSubscriber::HandleScreenEvent() +{ + if (!bundleActiveReportHandler_.expired()) { + sptr timer = MiscServices::TimeServiceClient::GetInstance(); + auto groupController = activeGroupController_.lock(); + if (groupController == nullptr) { + return; + } + bool isScreenOn = groupController->IsScreenOn(); + BUNDLE_ACTIVE_LOGI("screen state change to %{public}d", isScreenOn); + groupController->OnScreenChanged(isScreenOn, timer->GetBootTimeMs()); + } +} + +void BundleActiveCommonEventSubscriber::HandleUserRemoveEvent(const CommonEventData &data) +{ + if (!bundleActiveReportHandler_.expired()) { + int32_t userId = data.GetCode(); + BUNDLE_ACTIVE_LOGI("remove user id %{public}d", userId); + BundleActiveReportHandlerObject tmpHandlerObject(userId, ""); + std::shared_ptr handlerobjToPtr = + std::make_shared(tmpHandlerObject); + auto reportHandler = bundleActiveReportHandler_.lock(); + if (reportHandler == nullptr) { + return; + } + reportHandler->SendEvent(BundleActiveReportHandler::MSG_REMOVE_USER, handlerobjToPtr); + } +} + +void BundleActiveCommonEventSubscriber::HandleUserSwitchEvent(const CommonEventData &data) +{ + if (!bundleActiveReportHandler_.expired()) { int32_t userId = data.GetCode(); BUNDLE_ACTIVE_LOGI("OnReceiveEvent receive switched user event, user id is %{public}d", userId); BundleActiveReportHandlerObject tmpHandlerObject(userId, ""); std::shared_ptr handlerobjToPtr = std::make_shared(tmpHandlerObject); - bundleActiveReportHandler_.lock()->SendEvent(BundleActiveReportHandler::MSG_SWITCH_USER, handlerobjToPtr); - } else if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED || - action == CommonEventSupport::COMMON_EVENT_PACKAGE_FULLY_REMOVED) { - int32_t userId = data.GetWant().GetIntParam("userId", 0); - std::string bundleName = data.GetWant().GetElement().GetBundleName(); - BUNDLE_ACTIVE_LOGI("action is %{public}s, userID is %{public}d, bundlename is %{public}s", - action.c_str(), userId, bundleName.c_str()); - if (!bundleActiveReportHandler_.expired()) { - BundleActiveReportHandlerObject tmpHandlerObject(userId, bundleName); - tmpHandlerObject.uid_ = data.GetWant().GetIntParam("uid", -1); - tmpHandlerObject.appIndex_ = data.GetWant().GetIntParam("appIndex", -1); - std::shared_ptr handlerobjToPtr = - std::make_shared(tmpHandlerObject); - bundleActiveReportHandler_.lock()->SendEvent(BundleActiveReportHandler::MSG_BUNDLE_UNINSTALLED, - handlerobjToPtr); + auto reportHandler = bundleActiveReportHandler_.lock(); + if (reportHandler == nullptr) { + return; } - } else if (action == COMMON_EVENT_UNLOCK_SCREEN || action == COMMON_EVENT_LOCK_SCREEN) { - int32_t userId = data.GetWant().GetIntParam("userId", 0); - BUNDLE_ACTIVE_LOGI("action is %{public}s, userID is %{public}d", action.c_str(), userId); - HandleLockEvent(action, userId); + reportHandler->SendEvent(BundleActiveReportHandler::MSG_SWITCH_USER, handlerobjToPtr); } } -void BundleActiveCommonEventSubscriber::HandleLockEvent(const std::string& action, const int32_t userId) +void BundleActiveCommonEventSubscriber::HandlePackageRemoveEvent(const CommonEventData &data) { + std::string action = data.GetWant().GetAction(); + int32_t userId = data.GetWant().GetIntParam("userId", 0); + std::string bundleName = data.GetWant().GetElement().GetBundleName(); + BUNDLE_ACTIVE_LOGI("action is %{public}s, userID is %{public}d, bundlename is %{public}s", + action.c_str(), userId, bundleName.c_str()); + if (!bundleActiveReportHandler_.expired()) { + BundleActiveReportHandlerObject tmpHandlerObject(userId, bundleName); + tmpHandlerObject.uid_ = data.GetWant().GetIntParam("uid", -1); + tmpHandlerObject.appIndex_ = data.GetWant().GetIntParam("appIndex", -1); + std::shared_ptr handlerobjToPtr = + std::make_shared(tmpHandlerObject); + auto reportHandler = bundleActiveReportHandler_.lock(); + if (reportHandler == nullptr) { + return; + } + reportHandler->SendEvent(BundleActiveReportHandler::MSG_BUNDLE_UNINSTALLED, + handlerobjToPtr); + } +} + +void BundleActiveCommonEventSubscriber::HandlePackageAddEvent(const CommonEventData &data) +{ + std::string action = data.GetWant().GetAction(); + int32_t userId = data.GetWant().GetIntParam("userId", 0); + std::string bundleName = data.GetWant().GetElement().GetBundleName(); + BUNDLE_ACTIVE_LOGI("action is %{public}s, userID is %{public}d, bundlename is %{public}s", + action.c_str(), userId, bundleName.c_str()); + if (!bundleActiveReportHandler_.expired()) { + BundleActiveReportHandlerObject tmpHandlerObject(userId, bundleName); + tmpHandlerObject.uid_ = data.GetWant().GetIntParam("uid", -1); + tmpHandlerObject.appIndex_ = data.GetWant().GetIntParam("appIndex", -1); + std::shared_ptr handlerobjToPtr = + std::make_shared(tmpHandlerObject); + auto reportHandler = bundleActiveReportHandler_.lock(); + if (reportHandler == nullptr) { + return; + } + reportHandler->SendEvent(BundleActiveReportHandler::MSG_BUNDLE_INSTALLED, + handlerobjToPtr); + } +} + +void BundleActiveCommonEventSubscriber::HandleLockEvent(const CommonEventData &data) +{ + std::string action = data.GetWant().GetAction(); + int32_t userId = data.GetWant().GetIntParam("userId", 0); + BUNDLE_ACTIVE_LOGI("action is %{public}s, userID is %{public}d", action.c_str(), userId); if (bundleActiveReportHandler_.expired()) { return; } @@ -150,6 +216,7 @@ void BundleActiveCore::RegisterSubscriber() matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED); matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_BUNDLE_REMOVED); matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_FULLY_REMOVED); + matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED); matchingSkills.AddEvent(COMMON_EVENT_UNLOCK_SCREEN); matchingSkills.AddEvent(COMMON_EVENT_LOCK_SCREEN); CommonEventSubscribeInfo subscriberInfo(matchingSkills); @@ -245,6 +312,22 @@ void BundleActiveCore::OnBundleUninstalled(const int32_t userId, const std::stri bundleGroupController_->OnBundleUninstalled(userId, bundleName, uid, appIndex); } +void BundleActiveCore::OnBundleInstalled(const int32_t userId, const std::string& bundleName, + const int32_t uid, const int32_t appIndex) +{ + BUNDLE_ACTIVE_LOGD("OnBundleInstalled CALLED"); + std::lock_guard lock(bundleUninstalledMutex_); + if (bundleUninstalledSet_.find(uid) == bundleUninstalledSet_.end()) { + return; + } + bundleUninstalledSet_.erase(uid); + if (taskMap_.find(uid) == taskMap_.end()) { + return; + } + ffrt::skip(taskMap_[uid]); + taskMap_.erase(uid); +} + void BundleActiveCore::AddbundleUninstalledUid(const int32_t uid) { std::lock_guard lock(bundleUninstalledMutex_); @@ -254,10 +337,15 @@ void BundleActiveCore::AddbundleUninstalledUid(const int32_t uid) void BundleActiveCore::DelayRemoveBundleUninstalledUid(const int32_t uid) { auto bundleActiveCore = shared_from_this(); - ffrt::submit([bundleActiveCore, uid]() { + auto task = ffrt::submit_h([bundleActiveCore, uid]() { std::lock_guard lock(bundleActiveCore->bundleUninstalledMutex_); bundleActiveCore->bundleUninstalledSet_.erase(uid); + if (bundleActiveCore->taskMap_.find(uid) == bundleActiveCore->taskMap_.end()) { + return; + } + bundleActiveCore->taskMap_.erase(uid); }, {}, {}, ffrt::task_attr().delay(BUNDLE_UNINSTALL_DELAY_TIME)); + taskMap_[uid] = std::move(task); } bool BundleActiveCore::isUninstalledApp(const int32_t uid) diff --git a/services/packageusage/include/bundle_active_report_handler.h b/services/packageusage/include/bundle_active_report_handler.h index 6f26bb5c83526061379a274618088dc24b427611..7065b42fe3dcf4790c7b4b2816df64c5ee280e3f 100644 --- a/services/packageusage/include/bundle_active_report_handler.h +++ b/services/packageusage/include/bundle_active_report_handler.h @@ -46,8 +46,15 @@ public: static const int32_t MSG_REMOVE_USER; static const int32_t MSG_BUNDLE_UNINSTALLED; static const int32_t MSG_SWITCH_USER; + static const int32_t MSG_BUNDLE_INSTALLED; private: + void ProcessReportEvent(BundleActiveReportHandlerObject& tmpHandlerobj); + void ProcessFlushToDiskEvent(const BundleActiveReportHandlerObject& tmpHandlerobj); + void ProcessRmoveUserEvent(const BundleActiveReportHandlerObject& tmpHandlerobj); + void ProcessUserSwitchEvent(const BundleActiveReportHandlerObject& tmpHandlerobj); + void ProcessBundleUninstallEvent(const BundleActiveReportHandlerObject& tmpHandlerobj); + void ProcessBundleInstallEvent(const BundleActiveReportHandlerObject& tmpHandlerobj); bool isInited_ = false; ffrt::mutex taskHandlerMutex_; std::shared_ptr ffrtQueue_; diff --git a/services/packageusage/src/bundle_active_report_handler.cpp b/services/packageusage/src/bundle_active_report_handler.cpp index db03602b45fd80341481b0e5241622d3b3231082..c30c4224435fbbe488e5aacf9a288f8886f5e744 100644 --- a/services/packageusage/src/bundle_active_report_handler.cpp +++ b/services/packageusage/src/bundle_active_report_handler.cpp @@ -26,6 +26,7 @@ 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; +const int32_t BundleActiveReportHandler::MSG_BUNDLE_INSTALLED = 5; void BundleActiveReportHandler::Init(const std::shared_ptr& bundleActiveCore) { @@ -105,39 +106,27 @@ void BundleActiveReportHandler::ProcessEvent(const int32_t& eventId, BundleActiveReportHandlerObject tmpHandlerobj = *handlerobj; switch (eventId) { case MSG_REPORT_EVENT: { - BUNDLE_ACTIVE_LOGD("MSG_REPORT_EVENT CALLED"); - if (BundleActiveEvent::IsAppStateEvent(tmpHandlerobj.event_.eventId_) && - bundleActiveCore_->isUninstalledApp(tmpHandlerobj.event_.uid_)) { - BUNDLE_ACTIVE_LOGE("not report uninstall app event"); - return; - } - bundleActiveCore_->ReportEvent(tmpHandlerobj.event_, tmpHandlerobj.userId_); + ProcessReportEvent(tmpHandlerobj); break; } case MSG_FLUSH_TO_DISK: { - BUNDLE_ACTIVE_LOGI("FLUSH TO DISK HANDLE"); - if (tmpHandlerobj.userId_ != bundleActiveCore_->currentUsedUser_) { - BUNDLE_ACTIVE_LOGE("flush user is %{public}d, not last user %{public}d, return", - tmpHandlerobj.userId_, bundleActiveCore_->currentUsedUser_); - RemoveEvent(BundleActiveReportHandler::MSG_FLUSH_TO_DISK); - return; - } - bundleActiveCore_->RestoreToDatabase(tmpHandlerobj.userId_); + ProcessFlushToDiskEvent(tmpHandlerobj); break; } case MSG_REMOVE_USER: { - bundleActiveCore_->OnUserRemoved(tmpHandlerobj.userId_); + ProcessRmoveUserEvent(tmpHandlerobj); + break; + } + case MSG_SWITCH_USER: { + ProcessUserSwitchEvent(tmpHandlerobj); break; } case MSG_BUNDLE_UNINSTALLED: { - BUNDLE_ACTIVE_LOGI("MSG_BUNDLE_UNINSTALLED CALLED"); - bundleActiveCore_->OnBundleUninstalled(tmpHandlerobj.userId_, tmpHandlerobj.bundleName_, - tmpHandlerobj.uid_, tmpHandlerobj.appIndex_); + ProcessBundleUninstallEvent(tmpHandlerobj); break; } - case MSG_SWITCH_USER: { - BUNDLE_ACTIVE_LOGI("MSG_SWITCH_USER CALLED"); - bundleActiveCore_->OnUserSwitched(tmpHandlerobj.userId_); + case MSG_BUNDLE_INSTALLED: { + ProcessBundleInstallEvent(tmpHandlerobj); break; } default: { @@ -145,6 +134,56 @@ void BundleActiveReportHandler::ProcessEvent(const int32_t& eventId, } } } + +void BundleActiveReportHandler::ProcessReportEvent(BundleActiveReportHandlerObject& tmpHandlerobj) +{ + BUNDLE_ACTIVE_LOGD("MSG_REPORT_EVENT CALLED"); + if (BundleActiveEvent::IsAppStateEvent(tmpHandlerobj.event_.eventId_) && + bundleActiveCore_->isUninstalledApp(tmpHandlerobj.event_.uid_)) { + BUNDLE_ACTIVE_LOGE("not report uninstall app event"); + return; + } + bundleActiveCore_->ReportEvent(tmpHandlerobj.event_, tmpHandlerobj.userId_); +} + +void BundleActiveReportHandler::ProcessFlushToDiskEvent(const BundleActiveReportHandlerObject& tmpHandlerobj) +{ + BUNDLE_ACTIVE_LOGI("FLUSH TO DISK HANDLE"); + if (tmpHandlerobj.userId_ != bundleActiveCore_->currentUsedUser_) { + BUNDLE_ACTIVE_LOGE("flush user is %{public}d, not last user %{public}d, return", + tmpHandlerobj.userId_, bundleActiveCore_->currentUsedUser_); + RemoveEvent(BundleActiveReportHandler::MSG_FLUSH_TO_DISK); + return; + } + bundleActiveCore_->RestoreToDatabase(tmpHandlerobj.userId_); +} + +void BundleActiveReportHandler::ProcessRmoveUserEvent(const BundleActiveReportHandlerObject& tmpHandlerobj) +{ + BUNDLE_ACTIVE_LOGI("Remove user"); + bundleActiveCore_->OnUserRemoved(tmpHandlerobj.userId_); +} + +void BundleActiveReportHandler::ProcessUserSwitchEvent(const BundleActiveReportHandlerObject& tmpHandlerobj) +{ + BUNDLE_ACTIVE_LOGI("MSG_SWITCH_USER CALLED"); + bundleActiveCore_->OnUserSwitched(tmpHandlerobj.userId_); +} + +void BundleActiveReportHandler::ProcessBundleUninstallEvent(const BundleActiveReportHandlerObject& tmpHandlerobj) +{ + BUNDLE_ACTIVE_LOGI("MSG_BUNDLE_UNINSTALLED CALLED"); + bundleActiveCore_->OnBundleUninstalled(tmpHandlerobj.userId_, tmpHandlerobj.bundleName_, + tmpHandlerobj.uid_, tmpHandlerobj.appIndex_); +} + +void BundleActiveReportHandler::ProcessBundleInstallEvent(const BundleActiveReportHandlerObject& tmpHandlerobj) +{ + BUNDLE_ACTIVE_LOGI("MSG_BUNDLE_INSTALLED CALLED"); + bundleActiveCore_->OnBundleInstalled(tmpHandlerobj.userId_, tmpHandlerobj.bundleName_, + tmpHandlerobj.uid_, tmpHandlerobj.appIndex_); +} + } // namespace DeviceUsageStats } // namespace OHOS diff --git a/test/unittest/package_usage_test.cpp b/test/unittest/package_usage_test.cpp index 83ca4a62c0254c9843dd59cffd1f01b493a864d7..eabb8c1ae01dd9809b7db66de87928e05e1a27a8 100644 --- a/test/unittest/package_usage_test.cpp +++ b/test/unittest/package_usage_test.cpp @@ -863,6 +863,8 @@ HWTEST_F(PackageUsageTest, BundleActiveReportHandlerTest_004, Function | MediumT tmpObject.event_.timeStamp_ = timeNow; bundleActiveReportHandler->SendEvent(0, handlerObject); EXPECT_NE(service->currentStats_[0]->endTime_, timeNow); + bundleActiveCore_->OnBundleInstalled(userId, bundleName, uid, appIndex); + EXPECT_FALSE(bundleActiveCore_->isUninstalledApp(uid)); SUCCEED(); }