1eace7efcSopenharmony_ci/* 2eace7efcSopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd. 3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License. 5eace7efcSopenharmony_ci * You may obtain a copy of the License at 6eace7efcSopenharmony_ci * 7eace7efcSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8eace7efcSopenharmony_ci * 9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and 13eace7efcSopenharmony_ci * limitations under the License. 14eace7efcSopenharmony_ci */ 15eace7efcSopenharmony_ci#ifdef BGTASKMGR_CONTINUOUS_TASK_ENABLE 16eace7efcSopenharmony_ci#include "background_task_observer.h" 17eace7efcSopenharmony_ci 18eace7efcSopenharmony_ci#include "hilog_tag_wrapper.h" 19eace7efcSopenharmony_ci#include "sa_mgr_client.h" 20eace7efcSopenharmony_ci#include "system_ability_definition.h" 21eace7efcSopenharmony_ci#include "resource_type.h" 22eace7efcSopenharmony_ci 23eace7efcSopenharmony_cinamespace OHOS { 24eace7efcSopenharmony_cinamespace AAFwk { 25eace7efcSopenharmony_ciBackgroundTaskObserver::BackgroundTaskObserver() 26eace7efcSopenharmony_ci{} 27eace7efcSopenharmony_ci 28eace7efcSopenharmony_ciBackgroundTaskObserver::~BackgroundTaskObserver() 29eace7efcSopenharmony_ci{} 30eace7efcSopenharmony_ci 31eace7efcSopenharmony_civoid BackgroundTaskObserver::OnContinuousTaskStart(const std::shared_ptr<BackgroundTaskMgr::ContinuousTaskCallbackInfo> 32eace7efcSopenharmony_ci &continuousTaskCallbackInfo) 33eace7efcSopenharmony_ci{ 34eace7efcSopenharmony_ci TAG_LOGD( 35eace7efcSopenharmony_ci AAFwkTag::ABILITYMGR, "OnContinuousTaskStart, uid:%{public}d", continuousTaskCallbackInfo->GetCreatorUid()); 36eace7efcSopenharmony_ci std::lock_guard<std::mutex> lock(bgTaskMutex_); 37eace7efcSopenharmony_ci bgTaskUids_.push_front(continuousTaskCallbackInfo->GetCreatorUid()); 38eace7efcSopenharmony_ci if (appManager_ == nullptr) { 39eace7efcSopenharmony_ci GetAppManager(); 40eace7efcSopenharmony_ci } 41eace7efcSopenharmony_ci if (appManager_ != nullptr) { 42eace7efcSopenharmony_ci appManager_->SetContinuousTaskProcess(continuousTaskCallbackInfo->GetCreatorPid(), true); 43eace7efcSopenharmony_ci } 44eace7efcSopenharmony_ci} 45eace7efcSopenharmony_ci 46eace7efcSopenharmony_civoid BackgroundTaskObserver::OnContinuousTaskStop(const std::shared_ptr<BackgroundTaskMgr::ContinuousTaskCallbackInfo> 47eace7efcSopenharmony_ci &continuousTaskCallbackInfo) 48eace7efcSopenharmony_ci{ 49eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "OnContinuousTaskStop, uid:%{public}d", continuousTaskCallbackInfo->GetCreatorUid()); 50eace7efcSopenharmony_ci std::lock_guard<std::mutex> lock(bgTaskMutex_); 51eace7efcSopenharmony_ci bgTaskUids_.remove(continuousTaskCallbackInfo->GetCreatorUid()); 52eace7efcSopenharmony_ci if (appManager_ == nullptr) { 53eace7efcSopenharmony_ci GetAppManager(); 54eace7efcSopenharmony_ci } 55eace7efcSopenharmony_ci if (appManager_ != nullptr) { 56eace7efcSopenharmony_ci appManager_->SetContinuousTaskProcess(continuousTaskCallbackInfo->GetCreatorPid(), false); 57eace7efcSopenharmony_ci } 58eace7efcSopenharmony_ci} 59eace7efcSopenharmony_ci 60eace7efcSopenharmony_civoid BackgroundTaskObserver::OnProcEfficiencyResourcesApply( 61eace7efcSopenharmony_ci const std::shared_ptr<BackgroundTaskMgr::ResourceCallbackInfo> &resourceInfo) 62eace7efcSopenharmony_ci{ 63eace7efcSopenharmony_ci if (!resourceInfo || (resourceInfo->GetResourceNumber() & BackgroundTaskMgr::ResourceType::WORK_SCHEDULER) == 0) { 64eace7efcSopenharmony_ci return; 65eace7efcSopenharmony_ci } 66eace7efcSopenharmony_ci std::lock_guard<std::mutex> lock(efficiencyMutex_); 67eace7efcSopenharmony_ci efficiencyUids_.push_back(resourceInfo->GetUid()); 68eace7efcSopenharmony_ci} 69eace7efcSopenharmony_ci 70eace7efcSopenharmony_civoid BackgroundTaskObserver::OnProcEfficiencyResourcesReset( 71eace7efcSopenharmony_ci const std::shared_ptr<BackgroundTaskMgr::ResourceCallbackInfo> &resourceInfo) 72eace7efcSopenharmony_ci{ 73eace7efcSopenharmony_ci if (!resourceInfo || (resourceInfo->GetResourceNumber() & BackgroundTaskMgr::ResourceType::WORK_SCHEDULER) == 0) { 74eace7efcSopenharmony_ci return; 75eace7efcSopenharmony_ci } 76eace7efcSopenharmony_ci std::lock_guard<std::mutex> lock(efficiencyMutex_); 77eace7efcSopenharmony_ci int32_t uid = resourceInfo->GetUid(); 78eace7efcSopenharmony_ci auto iter = std::find(efficiencyUids_.begin(), efficiencyUids_.end(), uid); 79eace7efcSopenharmony_ci if (iter != efficiencyUids_.end()) { 80eace7efcSopenharmony_ci efficiencyUids_.erase(iter); 81eace7efcSopenharmony_ci } 82eace7efcSopenharmony_ci} 83eace7efcSopenharmony_ci 84eace7efcSopenharmony_civoid BackgroundTaskObserver::OnAppEfficiencyResourcesApply( 85eace7efcSopenharmony_ci const std::shared_ptr<BackgroundTaskMgr::ResourceCallbackInfo> &resourceInfo) 86eace7efcSopenharmony_ci{ 87eace7efcSopenharmony_ci OnProcEfficiencyResourcesApply(resourceInfo); 88eace7efcSopenharmony_ci} 89eace7efcSopenharmony_ci 90eace7efcSopenharmony_civoid BackgroundTaskObserver::OnAppEfficiencyResourcesReset( 91eace7efcSopenharmony_ci const std::shared_ptr<BackgroundTaskMgr::ResourceCallbackInfo> &resourceInfo) 92eace7efcSopenharmony_ci{ 93eace7efcSopenharmony_ci OnProcEfficiencyResourcesReset(resourceInfo); 94eace7efcSopenharmony_ci} 95eace7efcSopenharmony_ci 96eace7efcSopenharmony_civoid BackgroundTaskObserver::GetContinuousTaskApps() 97eace7efcSopenharmony_ci{ 98eace7efcSopenharmony_ci std::vector<std::shared_ptr<BackgroundTaskMgr::ContinuousTaskCallbackInfo>> continuousTasks; 99eace7efcSopenharmony_ci ErrCode result = BackgroundTaskMgr::BackgroundTaskMgrHelper::GetContinuousTaskApps(continuousTasks); 100eace7efcSopenharmony_ci if (result != ERR_OK) { 101eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "failed to GetContinuousTaskApps, err: %{public}d", result); 102eace7efcSopenharmony_ci return; 103eace7efcSopenharmony_ci } 104eace7efcSopenharmony_ci std::lock_guard<std::mutex> lock(bgTaskMutex_); 105eace7efcSopenharmony_ci bgTaskUids_.clear(); 106eace7efcSopenharmony_ci for (size_t index = 0; index < continuousTasks.size(); index++) { 107eace7efcSopenharmony_ci bgTaskUids_.push_front(continuousTasks[index]->GetCreatorUid()); 108eace7efcSopenharmony_ci } 109eace7efcSopenharmony_ci} 110eace7efcSopenharmony_ci 111eace7efcSopenharmony_civoid BackgroundTaskObserver::GetEfficiencyResourcesTaskApps() 112eace7efcSopenharmony_ci{ 113eace7efcSopenharmony_ci std::vector<std::shared_ptr<BackgroundTaskMgr::ResourceCallbackInfo>> appList; 114eace7efcSopenharmony_ci std::vector<std::shared_ptr<BackgroundTaskMgr::ResourceCallbackInfo>> procList; 115eace7efcSopenharmony_ci ErrCode result = BackgroundTaskMgr::BackgroundTaskMgrHelper::GetEfficiencyResourcesInfos(appList, procList); 116eace7efcSopenharmony_ci if (result != ERR_OK) { 117eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "failed to GetEfficiencyResourcesInfos, err: %{public}d", result); 118eace7efcSopenharmony_ci return; 119eace7efcSopenharmony_ci } 120eace7efcSopenharmony_ci std::lock_guard<std::mutex> lock(efficiencyMutex_); 121eace7efcSopenharmony_ci efficiencyUids_.clear(); 122eace7efcSopenharmony_ci for (auto& info : appList) { 123eace7efcSopenharmony_ci if (info == nullptr) { 124eace7efcSopenharmony_ci continue; 125eace7efcSopenharmony_ci } 126eace7efcSopenharmony_ci if ((info->GetResourceNumber() & BackgroundTaskMgr::ResourceType::WORK_SCHEDULER) != 0) { 127eace7efcSopenharmony_ci efficiencyUids_.push_back(info->GetUid()); 128eace7efcSopenharmony_ci } 129eace7efcSopenharmony_ci } 130eace7efcSopenharmony_ci for (auto& info : procList) { 131eace7efcSopenharmony_ci if (info == nullptr) { 132eace7efcSopenharmony_ci continue; 133eace7efcSopenharmony_ci } 134eace7efcSopenharmony_ci if ((info->GetResourceNumber() & BackgroundTaskMgr::ResourceType::WORK_SCHEDULER) != 0) { 135eace7efcSopenharmony_ci efficiencyUids_.push_back(info->GetUid()); 136eace7efcSopenharmony_ci } 137eace7efcSopenharmony_ci } 138eace7efcSopenharmony_ci} 139eace7efcSopenharmony_ci 140eace7efcSopenharmony_cibool BackgroundTaskObserver::IsBackgroundTaskUid(const int uid) 141eace7efcSopenharmony_ci{ 142eace7efcSopenharmony_ci std::lock_guard<std::mutex> lock(bgTaskMutex_); 143eace7efcSopenharmony_ci auto iter = find(bgTaskUids_.begin(), bgTaskUids_.end(), uid); 144eace7efcSopenharmony_ci if (iter != bgTaskUids_.end()) { 145eace7efcSopenharmony_ci return true; 146eace7efcSopenharmony_ci } 147eace7efcSopenharmony_ci return false; 148eace7efcSopenharmony_ci} 149eace7efcSopenharmony_ci 150eace7efcSopenharmony_cibool BackgroundTaskObserver::IsEfficiencyResourcesTaskUid(const int uid) 151eace7efcSopenharmony_ci{ 152eace7efcSopenharmony_ci std::lock_guard<std::mutex> lock(efficiencyMutex_); 153eace7efcSopenharmony_ci auto iter = std::find(efficiencyUids_.begin(), efficiencyUids_.end(), uid); 154eace7efcSopenharmony_ci if (iter != efficiencyUids_.end()) { 155eace7efcSopenharmony_ci return true; 156eace7efcSopenharmony_ci } 157eace7efcSopenharmony_ci return false; 158eace7efcSopenharmony_ci} 159eace7efcSopenharmony_ci 160eace7efcSopenharmony_cisptr<AppExecFwk::IAppMgr> BackgroundTaskObserver::GetAppManager() 161eace7efcSopenharmony_ci{ 162eace7efcSopenharmony_ci if (appManager_ == nullptr) { 163eace7efcSopenharmony_ci auto appObj = 164eace7efcSopenharmony_ci OHOS::DelayedSingleton<SaMgrClient>::GetInstance()->GetSystemAbility(APP_MGR_SERVICE_ID); 165eace7efcSopenharmony_ci if (appObj == nullptr) { 166eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "null appObj"); 167eace7efcSopenharmony_ci return nullptr; 168eace7efcSopenharmony_ci } 169eace7efcSopenharmony_ci appManager_ = iface_cast<AppExecFwk::IAppMgr>(appObj); 170eace7efcSopenharmony_ci } 171eace7efcSopenharmony_ci return appManager_; 172eace7efcSopenharmony_ci} 173eace7efcSopenharmony_ci} // namespace AAFwk 174eace7efcSopenharmony_ci} // namespace OHOS 175eace7efcSopenharmony_ci#endif 176