diff --git a/services/abilitymgr/include/ability_manager_service.h b/services/abilitymgr/include/ability_manager_service.h index d6bad6bcef..58771f740a 100644 --- a/services/abilitymgr/include/ability_manager_service.h +++ b/services/abilitymgr/include/ability_manager_service.h @@ -2315,6 +2315,8 @@ private: */ void ShowDeveloperModeDialog(const std::string &bundleName, const std::string &abilityName); + bool CheckWorkSchedulerPermission(const sptr &callerToken, const uint32_t uid); + constexpr static int REPOLL_TIME_MICRO_SECONDS = 1000000; std::shared_ptr taskHandler_; diff --git a/services/abilitymgr/include/background_task_observer.h b/services/abilitymgr/include/background_task_observer.h index 17ef08402c..f7a03e3f90 100644 --- a/services/abilitymgr/include/background_task_observer.h +++ b/services/abilitymgr/include/background_task_observer.h @@ -30,21 +30,37 @@ public: BackgroundTaskObserver(); virtual ~BackgroundTaskObserver(); bool IsBackgroundTaskUid(const int uid); + bool IsEfficiencyResourcesTaskUid(const int uid); void GetContinuousTaskApps(); + void GetEfficiencyResourcesTaskApps(); private: void OnContinuousTaskStart(const std::shared_ptr - &continuousTaskCallbackInfo); + &continuousTaskCallbackInfo) override; void OnContinuousTaskStop(const std::shared_ptr - &continuousTaskCallbackInfo); + &continuousTaskCallbackInfo) override; + + void OnAppEfficiencyResourcesApply( + const std::shared_ptr &resourceInfo) override; + + void OnAppEfficiencyResourcesReset( + const std::shared_ptr &resourceInfo) override; + + void OnProcEfficiencyResourcesApply( + const std::shared_ptr &resourceInfo) override; + + void OnProcEfficiencyResourcesReset( + const std::shared_ptr &resourceInfo) override; sptr GetAppManager(); private: std::list bgTaskUids_; std::mutex bgTaskMutex_; + std::list efficiencyUids_; + std::mutex efficiencyMutex_; sptr appManager_ = nullptr; }; } // namespace AAFwk diff --git a/services/abilitymgr/src/ability_manager_service.cpp b/services/abilitymgr/src/ability_manager_service.cpp index 30bdc5ef0e..0a6d521be4 100644 --- a/services/abilitymgr/src/ability_manager_service.cpp +++ b/services/abilitymgr/src/ability_manager_service.cpp @@ -2511,6 +2511,7 @@ void AbilityManagerService::SubscribeBackgroundTask() return; } bgtaskObserver_->GetContinuousTaskApps(); + bgtaskObserver_->GetEfficiencyResourcesTaskApps(); TAG_LOGI(AAFwkTag::ABILITYMGR, "%{public}s success", __func__); #endif } @@ -2775,6 +2776,26 @@ int AbilityManagerService::ChangeUIAbilityVisibilityBySCB(sptr sess return uiAbilityManager->ChangeUIAbilityVisibilityBySCB(sessionInfo, isShow); } +bool AbilityManagerService::CheckWorkSchedulerPermission(const sptr &callerToken, const uint32_t uid) +{ +#ifdef BGTASKMGR_CONTINUOUS_TASK_ENABLE + auto abilityRecord = Token::GetAbilityRecordByToken(callerToken); + std::unique_lock lock(bgtaskObserverMutex_); + if (bgtaskObserver_ && abilityRecord) { + auto callerAbilityInfo = abilityRecord->GetAbilityInfo(); + if (callerAbilityInfo.extensionAbilityType != AppExecFwk::ExtensionAbilityType::WORK_SCHEDULER) { + return true; + } + + if (!bgtaskObserver_->IsEfficiencyResourcesTaskUid(uid)) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "no permission to start extension by WorkScheduler"); + return false; + } + } +#endif + return true; +} + int32_t AbilityManagerService::StartExtensionAbilityInner(const Want &want, const sptr &callerToken, int32_t userId, AppExecFwk::ExtensionAbilityType extensionType, bool checkSystemCaller, bool isImplicit, bool isDlp, bool isStartAsCaller) @@ -2858,6 +2879,9 @@ int32_t AbilityManagerService::StartExtensionAbilityInner(const Want &want, cons return result; } + if (!CheckWorkSchedulerPermission(callerToken, abilityRequest.abilityInfo.applicationInfo.uid)) { + return CHECK_PERMISSION_FAILED; + } auto abilityInfo = abilityRequest.abilityInfo; validUserId = abilityInfo.applicationInfo.singleton ? U0_USER_ID : validUserId; TAG_LOGD(AAFwkTag::ABILITYMGR, "userId is : %{public}d, singleton is : %{public}d", diff --git a/services/abilitymgr/src/background_task_observer.cpp b/services/abilitymgr/src/background_task_observer.cpp index b6682fecc0..89476e12f1 100644 --- a/services/abilitymgr/src/background_task_observer.cpp +++ b/services/abilitymgr/src/background_task_observer.cpp @@ -18,6 +18,7 @@ #include "hilog_tag_wrapper.h" #include "sa_mgr_client.h" #include "system_ability_definition.h" +#include "resource_type.h" namespace OHOS { namespace AAFwk { @@ -56,6 +57,42 @@ void BackgroundTaskObserver::OnContinuousTaskStop(const std::shared_ptr &resourceInfo) +{ + if (!resourceInfo || (resourceInfo->GetResourceNumber() & BackgroundTaskMgr::ResourceType::WORK_SCHEDULER) == 0) { + return; + } + std::lock_guard lock(efficiencyMutex_); + efficiencyUids_.push_back(resourceInfo->GetUid()); +} + +void BackgroundTaskObserver::OnProcEfficiencyResourcesReset( + const std::shared_ptr &resourceInfo) +{ + if (!resourceInfo || (resourceInfo->GetResourceNumber() & BackgroundTaskMgr::ResourceType::WORK_SCHEDULER) == 0) { + return; + } + std::lock_guard lock(efficiencyMutex_); + int32_t uid = resourceInfo->GetUid(); + auto iter = std::find(efficiencyUids_.begin(), efficiencyUids_.end(), uid); + if (iter != efficiencyUids_.end()) { + efficiencyUids_.erase(iter); + } +} + +void BackgroundTaskObserver::OnAppEfficiencyResourcesApply( + const std::shared_ptr &resourceInfo) +{ + OnProcEfficiencyResourcesApply(resourceInfo); +} + +void BackgroundTaskObserver::OnAppEfficiencyResourcesReset( + const std::shared_ptr &resourceInfo) +{ + OnProcEfficiencyResourcesReset(resourceInfo); +} + void BackgroundTaskObserver::GetContinuousTaskApps() { std::vector> continuousTasks; @@ -71,6 +108,35 @@ void BackgroundTaskObserver::GetContinuousTaskApps() } } +void BackgroundTaskObserver::GetEfficiencyResourcesTaskApps() +{ + std::vector> appList; + std::vector> procList; + ErrCode result = BackgroundTaskMgr::BackgroundTaskMgrHelper::GetEfficiencyResourcesInfos(appList, procList); + if (result != ERR_OK) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "failed to GetEfficiencyResourcesInfos, err: %{public}d", result); + return; + } + std::lock_guard lock(efficiencyMutex_); + efficiencyUids_.clear(); + for (auto& info : appList) { + if (info == nullptr) { + continue; + } + if ((info->GetResourceNumber() & BackgroundTaskMgr::ResourceType::WORK_SCHEDULER) != 0) { + efficiencyUids_.push_back(info->GetUid()); + } + } + for (auto& info : procList) { + if (info == nullptr) { + continue; + } + if ((info->GetResourceNumber() & BackgroundTaskMgr::ResourceType::WORK_SCHEDULER) != 0) { + efficiencyUids_.push_back(info->GetUid()); + } + } +} + bool BackgroundTaskObserver::IsBackgroundTaskUid(const int uid) { std::lock_guard lock(bgTaskMutex_); @@ -81,6 +147,16 @@ bool BackgroundTaskObserver::IsBackgroundTaskUid(const int uid) return false; } +bool BackgroundTaskObserver::IsEfficiencyResourcesTaskUid(const int uid) +{ + std::lock_guard lock(efficiencyMutex_); + auto iter = std::find(efficiencyUids_.begin(), efficiencyUids_.end(), uid); + if (iter != efficiencyUids_.end()) { + return true; + } + return false; +} + sptr BackgroundTaskObserver::GetAppManager() { if (appManager_ == nullptr) {