diff --git a/services/common/include/bundle_active_core.h b/services/common/include/bundle_active_core.h index ca02dab5e53e268d03a5422a08e8222eebbe10ca..4ad90d7c9bf3384b06ea177e70cd11ddf232dd82 100644 --- a/services/common/include/bundle_active_core.h +++ b/services/common/include/bundle_active_core.h @@ -249,6 +249,7 @@ public: bool isUninstalledApp(const int32_t uid); void OnBundleInstalled(const int32_t userId, const std::string& bundleName, const int32_t uid, const int32_t appIndex); + void DeInit(); private: void NotifOberserverGroupChanged(const AppGroupCallbackInfo& callbackInfo, AccessToken::HapTokenInfo tokenInfo); diff --git a/services/common/src/bundle_active_core.cpp b/services/common/src/bundle_active_core.cpp index 35f26779dc3cf26ce78b738267f90ec3c222c495..e3718774f82268e355d48cf44bc80a8580f184e5 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -76,6 +76,16 @@ BundleActiveCore::~BundleActiveCore() { } +void BundleActiveCore::DeInit() +{ + if (bundleGroupHandler_ != nullptr) { + bundleGroupHandler_->DeInit(); + } + if (!handler_.expired()) { + handler_.lock()->DeInit(); + } +} + void BundleActiveCommonEventSubscriber::OnReceiveEvent(const CommonEventData &data) { std::lock_guard lock(mutex_); diff --git a/services/packagegroup/include/bundle_active_group_handler.h b/services/packagegroup/include/bundle_active_group_handler.h index 64d5fbe73456acd14fe2ec2b19ae2b09ed4c42fb..6c8b18877f6e2f0642d47d37bbfdcada9450f514 100644 --- a/services/packagegroup/include/bundle_active_group_handler.h +++ b/services/packagegroup/include/bundle_active_group_handler.h @@ -56,6 +56,7 @@ public: void PostSyncTask(const std::function& fuc); void PostTask(const std::function& fuc); void Init(const std::shared_ptr& bundleActiveController); + void DeInit(); static const int32_t MSG_CHECK_DEFAULT_BUNDLE_STATE; static const int32_t MSG_ONE_TIME_CHECK_BUNDLE_STATE; static const int32_t MSG_CHECK_IDLE_STATE; diff --git a/services/packagegroup/src/bundle_active_group_handler.cpp b/services/packagegroup/src/bundle_active_group_handler.cpp index a3051b33ab6efe002f71c54f3875558cd42e0822..c7c8b7f00a2be912646498f681cfba18e80d34b5 100644 --- a/services/packagegroup/src/bundle_active_group_handler.cpp +++ b/services/packagegroup/src/bundle_active_group_handler.cpp @@ -48,6 +48,23 @@ BundleActiveGroupHandler::BundleActiveGroupHandler(const bool debug) } } +void BundleActiveGroupHandler::DeInit() +{ + isInited_ = false; + for (auto& iter : taskHandlerMap_) { + auto& queue = iter.second; + while (!queue.empty()) { + ffrtQueue_->cancel(queue.front()); + queue.pop(); + } + } + taskHandlerMap_.clear(); + for (const auto& iter: checkBundleTaskMap_) { + ffrtQueue_->cancel(iter.second); + } + checkBundleTaskMap_.clear(); +} + void BundleActiveGroupHandler::Init(const std::shared_ptr& bundleActiveController) { BUNDLE_ACTIVE_LOGI("Init called"); @@ -182,6 +199,9 @@ void BundleActiveGroupHandler::PostTask(const std::function& fuc) void BundleActiveGroupHandler::ProcessEvent(const int32_t& eventId, const std::shared_ptr& handlerobj) { + if (!isInited_) { + return; + } switch (eventId) { case MSG_CHECK_DEFAULT_BUNDLE_STATE: case MSG_CHECK_NOTIFICATION_SEEN_BUNDLE_STATE: diff --git a/services/packageusage/include/bundle_active_report_handler.h b/services/packageusage/include/bundle_active_report_handler.h index 7065b42fe3dcf4790c7b4b2816df64c5ee280e3f..1388ecacf6493399104b07cc1c2a5ef91fedcdbc 100644 --- a/services/packageusage/include/bundle_active_report_handler.h +++ b/services/packageusage/include/bundle_active_report_handler.h @@ -41,6 +41,7 @@ public: void RemoveEvent(const int32_t& eventId); bool HasEvent(const int32_t& eventId); void Init(const std::shared_ptr& bundleActiveCore); + void DeInit(); static const int32_t MSG_REPORT_EVENT; static const int32_t MSG_FLUSH_TO_DISK; static const int32_t MSG_REMOVE_USER; diff --git a/services/packageusage/src/bundle_active_report_handler.cpp b/services/packageusage/src/bundle_active_report_handler.cpp index c30c4224435fbbe488e5aacf9a288f8886f5e744..c45c62f70a06a0d64c0e33ca23c49966ce332a21 100644 --- a/services/packageusage/src/bundle_active_report_handler.cpp +++ b/services/packageusage/src/bundle_active_report_handler.cpp @@ -40,6 +40,19 @@ void BundleActiveReportHandler::Init(const std::shared_ptr& bu isInited_ = true; } +void BundleActiveReportHandler::DeInit() +{ + isInited_ = false; + for (auto& iter : taskHandlerMap_) { + auto& queue = iter.second; + while (!queue.empty()) { + ffrtQueue_->cancel(queue.front()); + queue.pop(); + } + } + taskHandlerMap_.clear(); +} + void BundleActiveReportHandler::SendEvent(const int32_t& eventId, const std::shared_ptr& handlerobj, const int64_t& delayTime) { @@ -99,6 +112,10 @@ bool BundleActiveReportHandler::HasEvent(const int32_t& eventId) void BundleActiveReportHandler::ProcessEvent(const int32_t& eventId, const std::shared_ptr& handlerobj) { + if (!isInited_) { + BUNDLE_ACTIVE_LOGE("init failed"); + return; + } if (handlerobj == nullptr) { BUNDLE_ACTIVE_LOGE("handlerobj is null, exit ProcessEvent"); return; diff --git a/test/unittest/device_usage_statistics_service_test.cpp b/test/unittest/device_usage_statistics_service_test.cpp index 6a2c2a0247ff77e7c1006fd7e839445396020e02..82b8c8e16ebaca114d712dca6217b1da0efbf153 100644 --- a/test/unittest/device_usage_statistics_service_test.cpp +++ b/test/unittest/device_usage_statistics_service_test.cpp @@ -45,6 +45,7 @@ public: void SetUp(); void TearDown(); static std::shared_ptr bundleActiveCore_; + static void TestBundleActiveCoreDeInit(std::shared_ptr& bundleActiveCore); }; std::shared_ptr DeviceUsageStatisticsServiceTest::bundleActiveCore_ = nullptr; @@ -75,7 +76,7 @@ void DeviceUsageStatisticsServiceTest::SetUpTestCase(void) void DeviceUsageStatisticsServiceTest::TearDownTestCase(void) { - bundleActiveCore_->bundleGroupHandler_->ffrtQueue_.reset(); + TestBundleActiveCoreDeInit(bundleActiveCore_); int64_t sleepTime = 10; std::this_thread::sleep_for(std::chrono::seconds(sleepTime)); } @@ -90,6 +91,18 @@ void DeviceUsageStatisticsServiceTest::TearDown(void) std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime)); } +void DeviceUsageStatisticsServiceTest::TestBundleActiveCoreDeInit(std::shared_ptr& bundleActiveCore) +{ + int64_t sleepTime = 1; + std::this_thread::sleep_for(std::chrono::seconds(sleepTime)); + bundleActiveCore->DeInit(); + if (bundleActiveCore->bundleGroupHandler_ && bundleActiveCore->bundleGroupHandler_->ffrtQueue_) { + auto handle = bundleActiveCore->bundleGroupHandler_->ffrtQueue_->submit_h([]() {}); + bundleActiveCore->bundleGroupHandler_->ffrtQueue_->wait(handle); + bundleActiveCore->bundleGroupHandler_->ffrtQueue_.reset(); + } +} + class TestServiceAppGroupChangeCallback : public AppGroupCallbackStub { public: ErrCode OnAppGroupChanged(const AppGroupCallbackInfo &appGroupCallbackInfo) override; @@ -192,6 +205,7 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_dump args.clear(); args = {to_utf16("-D")}; bundleActiveService->Dump(-1, args); + TestBundleActiveCoreDeInit(bundleActiveService->bundleActiveCore_); } /* @@ -243,6 +257,7 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_AppG wptr remote = nullptr; bundleActiveService->bundleActiveCore_->OnObserverDied(remote); bundleActiveService->bundleActiveCore_->OnObserverDiedInner(remote); + TestBundleActiveCoreDeInit(bundleActiveService->bundleActiveCore_); } /* @@ -704,23 +719,24 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_GetS HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_ReportEvent_001, Function | MediumTest | Level0) { - bundleActiveCore_ = std::make_shared(); - bundleActiveCore_->Init(); - bundleActiveCore_->InitBundleGroupController(); + auto bundleActiveCore = std::make_shared(); + bundleActiveCore->Init(); + bundleActiveCore->InitBundleGroupController(); BundleActiveEvent event; int32_t userId = 0; - EXPECT_NE(bundleActiveCore_->ReportEvent(event, userId), ERR_OK); + EXPECT_NE(bundleActiveCore->ReportEvent(event, userId), ERR_OK); userId = 101; event.bundleName_ = "com.ohos.launcher"; - EXPECT_EQ(bundleActiveCore_->ReportEvent(event, userId), ERR_OK); + EXPECT_EQ(bundleActiveCore->ReportEvent(event, userId), ERR_OK); event.bundleName_ = "com.ohos.settings"; event.eventId_ = 15; - EXPECT_EQ(bundleActiveCore_->ReportEvent(event, userId), ERR_OK); + EXPECT_EQ(bundleActiveCore->ReportEvent(event, userId), ERR_OK); event.eventId_ = 16; - EXPECT_EQ(bundleActiveCore_->ReportEvent(event, userId), ERR_OK); + EXPECT_EQ(bundleActiveCore->ReportEvent(event, userId), ERR_OK); + TestBundleActiveCoreDeInit(bundleActiveCore); } /* @@ -758,6 +774,7 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_Repo coreObject->InitBundleGroupController(); coreObject->ReportEventToAllUserId(event); EXPECT_NE(coreObject, nullptr); + TestBundleActiveCoreDeInit(coreObject); } /* @@ -769,7 +786,9 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_Repo HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_RestoreToDatabase_001, Function | MediumTest | Level0) { - auto coreObject = bundleActiveCore_; + auto coreObject = std::make_shared(); + coreObject->Init(); + coreObject->InitBundleGroupController(); int userId = 100; auto userService = std::make_shared(userId, *(coreObject.get()), false); int64_t timeStamp = 20000000000000; @@ -780,6 +799,7 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_Rest userId = 101; coreObject->RestoreToDatabase(userId); EXPECT_NE(coreObject, nullptr); + TestBundleActiveCoreDeInit(coreObject); } /* @@ -925,6 +945,7 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, BundleActiveGroupControllerTest_009, EXPECT_EQ(coreObject->bundleGroupController_->IsBundleInstalled("test", userId), false); coreObject->bundleGroupController_->sptrBundleMgr_ = nullptr; EXPECT_EQ(coreObject->bundleGroupController_->IsBundleInstalled("test", userId), false); + TestBundleActiveCoreDeInit(coreObject); } /* @@ -1116,8 +1137,9 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_Chec auto coreObject = bundleActiveCore_; coreObject->OnUserRemoved(100); auto userService = std::make_shared(userId, *(coreObject.get()), false); + int64_t timeStamp = 20000000000000; + userService->Init(timeStamp); coreObject->userStatServices_[userId] = userService; - coreObject->CheckTimeChangeAndGetWallTime(userId); coreObject->OnUserSwitched(userId); EXPECT_NE(coreObject, nullptr);