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 16 #ifndef WORK_SCHED_SERVICES_WORK_STATUS_H 17 #define WORK_SCHED_SERVICES_WORK_STATUS_H 18 19 #include <memory> 20 #include <string> 21 #include <map> 22 #include <mutex> 23 24 #include "timer.h" 25 #include "work_info.h" 26 #include "ffrt.h" 27 28 namespace OHOS { 29 namespace WorkScheduler { 30 class WorkStatus { 31 public: 32 enum Status { 33 WAIT_CONDITION = 0, 34 CONDITION_READY, 35 RUNNING, 36 REMOVED 37 }; 38 WorkStatus(WorkInfo &workInfo, int32_t uid); 39 ~WorkStatus(); 40 41 /** 42 * @brief Make work id. 43 * 44 * @param workId The id of work. 45 * @param uid The uid. 46 * @return Workid and uid. 47 */ 48 static std::string MakeWorkId(int32_t workId, int32_t uid); 49 50 std::string workId_; 51 std::string bundleName_; 52 std::string abilityName_; 53 int32_t uid_; 54 int32_t userId_; 55 uint64_t workStartTime_ {0}; 56 uint64_t workWatchDogTime_ {0}; 57 uint64_t duration_ {0}; 58 bool paused_ {false}; 59 bool persisted_; 60 int32_t priority_; 61 bool needRetrigger_ {false}; 62 int32_t timeRetrigger_ {INT32_MAX}; 63 std::map<WorkCondition::Type, std::shared_ptr<Condition>> conditionMap_; 64 std::shared_ptr<WorkInfo> workInfo_; 65 std::string delayReason_; 66 67 /** 68 * @brief Judge state whether is ready. 69 * 70 * @return True if success,else false. 71 */ 72 bool IsReady(); 73 /** 74 * @brief Judge state whether is ready. 75 * 76 * @return True if success,else false. 77 */ 78 bool IsSAReady(); 79 /** 80 * @brief Judge state whether is ready status. 81 * 82 * @return True if success,else false. 83 */ 84 bool IsReadyStatus(); 85 /** 86 * @brief Judge state whether is running. 87 * 88 * @return True if success,else false. 89 */ 90 bool IsRunning(); 91 /** 92 * @brief Judge state whether is removed. 93 * 94 * @return True if success,else false. 95 */ 96 bool IsRemoved(); 97 /** 98 * @brief Judge state whether is repeating. 99 * 100 * @return True if success,else false. 101 */ 102 bool IsRepeating(); 103 /** 104 * @brief Judge state whether is paused. 105 * 106 * @return True if success,else false. 107 */ 108 bool IsPaused(); 109 /** 110 * @brief Judge state whether is last work timeout. 111 * 112 * @return True if success,else false. 113 */ 114 bool IsLastWorkTimeout(); 115 /** 116 * @brief The OnConditionChanged callback. 117 * 118 * @param type The type. 119 * @param value The value. 120 */ 121 int32_t OnConditionChanged(WorkCondition::Type &type, std::shared_ptr<Condition> value); 122 /** 123 * @brief Mark round. 124 */ 125 void MarkRound(); 126 /** 127 * @brief Mark status. 128 */ 129 void MarkStatus(Status status); 130 /** 131 * @brief Get status. 132 * 133 * @return The current status. 134 */ 135 Status GetStatus(); 136 /** 137 * @brief Dump. 138 * 139 * @param result The result. 140 */ 141 void Dump(std::string& result); 142 /** 143 * @brief Dump Condition. 144 * 145 * @param result The result. 146 */ 147 void DumpCondition(std::string& result); 148 /** 149 * @brief Update timer if need. 150 */ 151 void UpdateTimerIfNeed(); 152 /** 153 * @brief Need remove. 154 * 155 * @return True if success,else false. 156 */ 157 bool NeedRemove(); 158 159 bool lastTimeout_ {false}; 160 /** 161 * @brief Set min interval by group. 162 * 163 * @param group The new group. 164 * @return True if success,else false. 165 */ 166 bool SetMinIntervalByGroup(int32_t group); 167 /** 168 * @brief Update map<uid, lastTime>. 169 */ 170 void UpdateUidLastTimeMap(); 171 /** 172 * @brief clear uidLastTimeMap by uid. 173 */ 174 static void ClearUidLastTimeMap(int32_t uid); 175 /** 176 * @brief Set min interval by dump. 177 */ 178 void SetMinIntervalByDump(int64_t interval); 179 /** 180 * @brief get min interval. 181 */ 182 int64_t GetMinInterval(); 183 bool IsUriKeySwitchOn(); 184 void ToString(WorkCondition::Type conditionType); 185 private: 186 Status currentStatus_; 187 time_t baseTime_; 188 int64_t minInterval_; 189 bool groupChanged_; 190 ffrt::mutex conditionMapMutex_; 191 static ffrt::mutex s_uid_last_time_mutex; 192 static std::map<int32_t, time_t> s_uid_last_time_map; 193 std::string conditionStatus_; 194 void MarkTimeout(); 195 bool IsSameUser(); 196 bool SetMinInterval(); 197 bool IsBatteryAndNetworkReady(WorkCondition::Type type); 198 bool IsStorageReady(WorkCondition::Type type); 199 bool IsChargerReady(WorkCondition::Type type); 200 bool IsNapReady(WorkCondition::Type type); 201 int GetPriority(); 202 bool IsTimerReady(WorkCondition::Type type); 203 bool IsConditionReady(); 204 bool IsStandbyExemption(); 205 }; 206 } // namespace WorkScheduler 207 } // namespace OHOS 208 #endif // WORK_SCHED_SERVICES_WORK_STATUS_H