diff --git a/services/common/include/bundle_active_core.h b/services/common/include/bundle_active_core.h index 11061e3fb46ae20722f128d1d0883b09d5ee1a12..5d3b94bff089f29712ff2fa21b3df5ccea7f1d01 100644 --- a/services/common/include/bundle_active_core.h +++ b/services/common/include/bundle_active_core.h @@ -193,6 +193,8 @@ public: const sptr &observer); int32_t currentUsedUser_; void OnBundleGroupChanged(const BundleActiveGroupCallbackInfo& callbackInfo); + void OnFoundationkilled(); + void OnFoundationRestarted(); private: void AddObserverDeathRecipient(const sptr &observer); diff --git a/services/common/include/bundle_active_service.h b/services/common/include/bundle_active_service.h index ebe6615a511bac3b0b0c26af20b27304d643e221..2fa78705ea30aae28c8634a71172e5b2f6b8ceea 100644 --- a/services/common/include/bundle_active_service.h +++ b/services/common/include/bundle_active_service.h @@ -18,6 +18,7 @@ #include "bundle_mgr_interface.h" #include "singleton.h" +#include "system_ability_status_change_stub.h" #include "ibundle_active_service.h" #include "bundle_active_stub.h" @@ -149,11 +150,19 @@ protected: */ void OnStop() override; +private: +class SystemAbilityStatusChangeListener : public SystemAbilityStatusChangeStub { +public: + void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; + void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; +}; + private: std::shared_ptr bundleActiveCore_; std::shared_ptr reportHandler_; std::shared_ptr appStateObserver_; std::shared_ptr continuousTaskObserver_; + sptr sysAbilityListener_ = nullptr; sptr sptrBundleMgr_; sptr shutdownCallback_; sptr powerStateCallback_; @@ -162,7 +171,11 @@ private: bool ready_ {false}; int32_t ConvertIntervalType(const int32_t intervalType); void InitNecessaryState(); + void InitFoundationRestart(); + void InitSubscribe(); void InitService(); + void OnFoundationkilled(); + void OnFoundationRestarted(); bool GetBundleMgrProxy(); bool CheckBundleIsSystemAppAndHasPermission(const int32_t uid, OHOS::Security::AccessToken::AccessTokenID tokenId, int32_t& errCode); diff --git a/services/common/src/bundle_active_core.cpp b/services/common/src/bundle_active_core.cpp index e97e0db68ee9ef741e42a55f4480062dc5901a58..b72530867b937e443b56e16a61fd398872fa9ffc 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -315,6 +315,8 @@ void BundleActiveCore::RestoreToDatabaseLocked(const int32_t userId) } if (bundleGroupController_ != nullptr) { bundleGroupController_->RestoreDurationToDatabase(); + bundleGroupController_->CheckEachBundleState(userId); + bundleGroupController_->RestoreToDatabase(userId); } if (!handler_.expired()) { BUNDLE_ACTIVE_LOGI("RestoreToDatabaseLocked remove flush to disk event"); @@ -755,6 +757,22 @@ int32_t BundleActiveCore::UnregisterGroupCallBack(const AccessToken::AccessToken return 0; } +void BundleActiveCore::OnFoundationkilled() +{ + RestoreToDatabase(currentUsedUser_); +} + +void BundleActiveCore::OnFoundationRestarted() +{ + int64_t systemTime = GetSystemTimeMs(); + int64_t timeNow = CheckTimeChangeAndGetWallTime(currentUsedUser_); + if (timeNow == -1) { + return ; + } + auto userService = GetUserDataAndInitializeIfNeeded(currentUsedUser_, timeNow, debugCore_); + userService->LoadActiveStats(systemTime, true, false); +} + void BundleActiveCore::AddObserverDeathRecipient(const sptr &observer) { std::lock_guard lock(deathRecipientMutex_); diff --git a/services/common/src/bundle_active_service.cpp b/services/common/src/bundle_active_service.cpp index 37607d1c1bf322c1c6134de5bc7d2ff3375c8de5..e3cfd889320fba9a1759201785a84d6f4adf8e63 100644 --- a/services/common/src/bundle_active_service.cpp +++ b/services/common/src/bundle_active_service.cpp @@ -43,13 +43,9 @@ const std::string NEEDED_PERMISSION = "ohos.permission.BUNDLE_ACTIVE_INFO"; const bool REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(DelayedSingleton::GetInstance().get()); -BundleActiveService::BundleActiveService() : SystemAbility(DEVICE_USAGE_STATISTICS_SYS_ABILITY_ID, true) -{ -} +BundleActiveService::BundleActiveService() : SystemAbility(DEVICE_USAGE_STATISTICS_SYS_ABILITY_ID, true) { } -BundleActiveService::~BundleActiveService() -{ -} +BundleActiveService::~BundleActiveService() { } void BundleActiveService::OnStart() { @@ -68,6 +64,12 @@ void BundleActiveService::OnStart() BUNDLE_ACTIVE_LOGI("BundleActiveService handler create failed!"); return; } + + sysAbilityListener_ = new (std::nothrow) SystemAbilityStatusChangeListener(); + if (sysAbilityListener_ == nullptr) { + BUNDLE_ACTIVE_LOGE("Failed to create statusChangeListener due to no memory"); + return; + } auto registerTask = [this]() { this->InitNecessaryState(); }; handler_->PostSyncTask(registerTask); } @@ -101,6 +103,11 @@ void BundleActiveService::InitNecessaryState() return; } InitService(); + if (systemAbilityManager->SubscribeSystemAbility(POWER_MANAGER_SERVICE_ID, sysAbilityListener_) != ERR_OK) { + sysAbilityListener_ = nullptr; + BUNDLE_ACTIVE_LOGE("subscribe system ability id: %{public}d failed", POWER_MANAGER_SERVICE_ID); + } + ready_ = true; int32_t ret = Publish(DelayedSingleton::GetInstance().get()); if (!ret) { @@ -110,6 +117,40 @@ void BundleActiveService::InitNecessaryState() BUNDLE_ACTIVE_LOGI("[Server] OnStart, Register SystemAbility[1907] SUCCESS."); } +void BundleActiveService::InitFoundationRestart() +{ + appStateObserver_ = nullptr; + sptrBundleMgr_ = nullptr; + sptr systemAbilityManager + = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityManager == nullptr + || systemAbilityManager->CheckSystemAbility(APP_MGR_SERVICE_ID) == nullptr + || systemAbilityManager->CheckSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID) == nullptr + || systemAbilityManager->CheckSystemAbility(POWER_MANAGER_SERVICE_ID) == nullptr + || systemAbilityManager->CheckSystemAbility(COMMON_EVENT_SERVICE_ID) == nullptr + || systemAbilityManager->CheckSystemAbility(BACKGROUND_TASK_MANAGER_SERVICE_ID) == nullptr + || systemAbilityManager->CheckSystemAbility(TIME_SERVICE_ID) == nullptr) { + BUNDLE_ACTIVE_LOGI("request system service is not ready yet!"); + auto task = [this]() { this->InitFoundationRestart(); }; + handler_->PostTask(task, DELAY_TIME); + return; + } + + if (systemAbilityManager->GetSystemAbility(APP_MGR_SERVICE_ID) == nullptr + || systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID) == nullptr + || systemAbilityManager->GetSystemAbility(POWER_MANAGER_SERVICE_ID) == nullptr + || systemAbilityManager->GetSystemAbility(COMMON_EVENT_SERVICE_ID) == nullptr + || systemAbilityManager->GetSystemAbility(BACKGROUND_TASK_MANAGER_SERVICE_ID) == nullptr + || systemAbilityManager->GetSystemAbility(TIME_SERVICE_ID) == nullptr) { + BUNDLE_ACTIVE_LOGI("request system service object is not ready yet!"); + auto task = [this]() { this->InitFoundationRestart(); }; + handler_->PostTask(task, DELAY_TIME); + return; + } + InitSubscribe(); + bundleActiveCore_->OnFoundationRestarted(); +} + void BundleActiveService::InitService() { if (bundleActiveCore_ == nullptr) { @@ -135,6 +176,11 @@ void BundleActiveService::InitService() } else { return; } + InitSubscribe(); +} + +void BundleActiveService::InitSubscribe() +{ shutdownCallback_ = new (std::nothrow) BundleActiveShutdownCallbackService(bundleActiveCore_); powerStateCallback_ = new (std::nothrow) BundleActivePowerStateCallbackService(bundleActiveCore_); auto& powerManagerClient = OHOS::PowerMgr::PowerMgrClient::GetInstance(); @@ -151,6 +197,30 @@ void BundleActiveService::InitService() SubscribeContinuousTask(); } +void BundleActiveService::SystemAbilityStatusChangeListener::OnAddSystemAbility( + int32_t systemAbilityId, const std::string& deviceId) +{ + BUNDLE_ACTIVE_LOGD("BundleActiveService::SystemAbilityStatusChangeListener::OnAddSystemAbility"); + DelayedSingleton::GetInstance()->InitFoundationRestart(); +} + +void BundleActiveService::SystemAbilityStatusChangeListener::OnRemoveSystemAbility( + int32_t systemAbilityId, const std::string& deviceId) +{ + BUNDLE_ACTIVE_LOGD("BundleActiveService::SystemAbilityStatusChangeListener::OnRemoveSystemAbility"); + DelayedSingleton::GetInstance()->OnFoundationkilled(); +} + +void BundleActiveService::OnFoundationkilled() +{ + BUNDLE_ACTIVE_LOGD("BundleActiveService::OnFoundationkilled"); + continuousTaskObserver_ = nullptr; + shutdownCallback_ = nullptr; + powerStateCallback_ = nullptr; + sptrBundleMgr_ = nullptr; + bundleActiveCore_->OnFoundationkilled(); +} + OHOS::sptr BundleActiveService::GetAppManagerInstance() { OHOS::sptr systemAbilityManager = @@ -229,7 +299,7 @@ int32_t BundleActiveService::ReportEvent(BundleActiveEvent& event, const int32_t int32_t callingUid = OHOS::IPCSkeleton::GetCallingUid(); BUNDLE_ACTIVE_LOGI("calling process name is %{public}s, uid is %{public}d", callingTokenInfo.processName.c_str(), callingUid); - if (callingTokenInfo.processName == PERMITTED_PROCESS_NAME) { + if (callingTokenInfo.processName == PERMITTED_PROCESS_NAME && reportHandler_) { BundleActiveReportHandlerObject tmpHandlerObject(userId, ""); tmpHandlerObject.event_ = event; sptr timer = MiscServices::TimeServiceClient::GetInstance(); diff --git a/services/packageusage/include/bundle_active_event.h b/services/packageusage/include/bundle_active_event.h index 57889be08134327d902afce9ed7ef7c82493c8b9..74b5a820cd2095a94d795ca4c798b1c128fd419c 100644 --- a/services/packageusage/include/bundle_active_event.h +++ b/services/packageusage/include/bundle_active_event.h @@ -50,6 +50,7 @@ public: static const int32_t SYSTEM_UNLOCK = 21; static const int32_t SYSTEM_SLEEP = 22; static const int32_t SYSTEM_WAKEUP = 23; + static const int32_t FOUNDATION_KILL = 24; inline static const std::string DEVICE_EVENT_PACKAGE_NAME = "openharmony"; std::string bundleName_; std::string continuousTaskAbilityName_;