1 /* 2 * Copyright (c) 2022 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 #ifndef FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_WORK_SCHED_SERVICES_POLICY_MANAGER_H 16 #define FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_WORK_SCHED_SERVICES_POLICY_MANAGER_H 17 #include <map> 18 #include <memory> 19 #include <mutex> 20 #include <vector> 21 22 #include <event_runner.h> 23 #include "policy_type.h" 24 #include "policy/ipolicy_filter.h" 25 #include "work_conn_manager.h" 26 #include "work_queue.h" 27 #include "work_status.h" 28 29 namespace OHOS { 30 namespace WorkScheduler { 31 class WorkSchedulerService; 32 class WorkEventHandler; 33 class AppDataClearListener; 34 class Watchdog; 35 class WorkPolicyManager { 36 public: 37 explicit WorkPolicyManager(const wptr<WorkSchedulerService>& wss); 38 ~WorkPolicyManager() = default; 39 /** 40 * @brief Init. 41 * 42 * @return True if success,else false. 43 */ 44 bool Init(const std::shared_ptr<AppExecFwk::EventRunner>& runner); 45 /** 46 * @brief Add listener. 47 * 48 * @return True if success,else false. 49 */ 50 bool AddListener(); 51 /** 52 * @brief Add policy filter. 53 * 54 * @param filter The filter. 55 */ 56 void AddPolicyFilter(std::shared_ptr<IPolicyFilter> filter); 57 /** 58 * @brief Add work. 59 * 60 * @param workStatus The status of work. 61 * @param uid The uid. 62 * @return True if success,else false. 63 */ 64 int32_t AddWork(std::shared_ptr<WorkStatus> workStatus, int32_t uid); 65 /** 66 * @brief Remove work. 67 * 68 * @param workStatus The status of work. 69 * @param uid The uid. 70 * @return True if success,else false. 71 */ 72 bool RemoveWork(std::shared_ptr<WorkStatus> workStatus, int32_t uid); 73 /** 74 * @brief Stop work. 75 * 76 * @param workStatus The status of work. 77 * @param uid The uid. 78 * @param needCancel The need cancel. 79 * @param isTimeOut The is time out. 80 * @return True if success,else false. 81 */ 82 bool StopWork(std::shared_ptr<WorkStatus> workStatus, int32_t uid, const bool needCancel, bool isTimeOut); 83 /** 84 * @brief Stop and clear works. 85 * 86 * @param uid The uid. 87 * @return True if success,else false. 88 */ 89 bool StopAndClearWorks(int32_t uid); 90 /** 91 * @brief The last work time out. 92 * 93 * @param workId The id of work. 94 * @param uid The uid. 95 * @param result True if success,else false. 96 * @return The error code. 97 */ 98 int32_t IsLastWorkTimeout(int32_t workId, int32_t uid, bool &result); 99 /** 100 * @brief Find work status. 101 * 102 * @param workInfo The info of work. 103 * @param uid The uid. 104 * @return The uid queue map. 105 */ 106 std::shared_ptr<WorkStatus> FindWorkStatus(WorkInfo& workInfo, int32_t uid); 107 /** 108 * @brief The OnConditionReady callback. 109 * 110 * @param workStatusVector The work status vector. 111 */ 112 void OnConditionReady(std::shared_ptr<std::vector<std::shared_ptr<WorkStatus>>> workStatusVector); 113 /** 114 * @brief The OnPolicyChanged callback. 115 * 116 * @param policyType The policy type. 117 * @param detectorVal The detector val. 118 */ 119 void OnPolicyChanged(PolicyType policyType, std::shared_ptr<DetectorValue> detectorVal); 120 /** 121 * @brief Obtain all works. 122 * 123 * @param uid The uid. 124 * @return All works. 125 */ 126 std::list<std::shared_ptr<WorkInfo>> ObtainAllWorks(int32_t &uid); 127 /** 128 * @brief Get the status of work. 129 * 130 * @param uid The uid. 131 * @param workInfo The info of work. 132 * @return The status of work. 133 */ 134 std::shared_ptr<WorkInfo> GetWorkStatus(int32_t &uid, int32_t &workInfo); 135 /** 136 * @brief Get all status of work. 137 * 138 * @param uid The uid. 139 * @return All status of work. 140 */ 141 std::list<std::shared_ptr<WorkStatus>> GetAllWorkStatus(int32_t &uid); 142 143 /** 144 * @brief Get the All Running Works object. 145 * 146 * @return All running works; 147 */ 148 std::list<std::shared_ptr<WorkInfo>> GetAllRunningWorks(); 149 150 /** 151 * @brief The Dump. 152 * 153 * @param result The result. 154 */ 155 void Dump(std::string& result); 156 int32_t currentRunningCount = 0; 157 /** 158 * @brief Check work to run. 159 */ 160 void CheckWorkToRun(); 161 /** 162 * @brief Send retrigger. 163 * 164 * @param delayTime The delay time. 165 */ 166 void SendRetrigger(int32_t delayTime); 167 /** 168 * @brief Add app data clear listener. 169 * 170 * @param listener The listener. 171 */ 172 void AddAppDataClearListener(std::shared_ptr<AppDataClearListener> listener); 173 /** 174 * @brief Watchdog time out. 175 * 176 * @param watchdogId The id of watchdog. 177 */ 178 void WatchdogTimeOut(uint32_t watchdogId); 179 /** 180 * @brief Set memory by dump. 181 * 182 * @param memory The memory. 183 */ 184 void SetMemoryByDump(int32_t memory); 185 /** 186 * @brief Get dump set memory. 187 * 188 * @return Dump set memory. 189 */ 190 int32_t GetDumpSetMemory(); 191 /** 192 * @brief Set watchdog time by dump. 193 * 194 * @param time The time. 195 */ 196 void SetWatchdogTimeByDump(int32_t time); 197 /** 198 * @brief Set watchdog time. 199 * 200 * @param time The time. 201 */ 202 void SetWatchdogTime(int32_t time); 203 /** 204 * @brief Get the time of watchdog. 205 * 206 * @return The time of watchdog. 207 */ 208 int32_t GetWatchdogTime(); 209 210 private: 211 int32_t GetMaxRunningCount(); 212 int32_t GetRunningCount(); 213 void DumpConditionReadyQueue(std::string& result); 214 void DumpUidQueueMap(std::string& result); 215 void RemoveFromUidQueue(std::shared_ptr<WorkStatus> workStatus, int32_t uid); 216 void RemoveFromReadyQueue(std::shared_ptr<WorkStatus> workStatus); 217 void AddToReadyQueue(std::shared_ptr<std::vector<std::shared_ptr<WorkStatus>>> workStatusVector); 218 void RealStartWork(std::shared_ptr<WorkStatus> workStatus); 219 void AddToRunningQueue(std::shared_ptr<WorkStatus> workStatus); 220 void RemoveConditionUnReady(); 221 std::shared_ptr<WorkStatus> GetWorkToRun(); 222 void RemoveAllUnReady(); 223 uint32_t NewWatchdogId(); 224 void AddWatchdogForWork(std::shared_ptr<WorkStatus> workStatus); 225 std::shared_ptr<WorkStatus> GetWorkFromWatchdog(uint32_t id); 226 void UpdateWatchdogTime(const wptr<WorkSchedulerService> &wmsptr, 227 std::shared_ptr<WorkStatus> &topWork); 228 229 const wptr<WorkSchedulerService> wss_; 230 std::shared_ptr<WorkConnManager> workConnManager_; 231 std::shared_ptr<WorkEventHandler> handler_; 232 233 std::mutex uidMapMutex_; 234 std::map<int32_t, std::shared_ptr<WorkQueue>> uidQueueMap_; 235 236 std::mutex conditionReadyMutex_; 237 std::shared_ptr<WorkQueue> conditionReadyQueue_; 238 239 std::list<std::shared_ptr<IPolicyFilter>> policyFilters_; 240 std::shared_ptr<AppDataClearListener> appDataClearListener_; 241 242 std::shared_ptr<Watchdog> watchdog_; 243 244 std::mutex watchdogIdMapMutex_; 245 std::map<uint32_t, std::shared_ptr<WorkStatus>> watchdogIdMap_; 246 247 uint32_t watchdogId_; 248 int32_t dumpSetMemory_; 249 int32_t watchdogTime_; 250 }; 251 } // namespace WorkScheduler 252 } // namespace OHOS 253 254 #endif // FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_WORK_SCHED_SERVICES_POLICY_MANAGER_H