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