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 status.
75      *
76      * @return True if success,else false.
77      */
78     bool IsReadyStatus();
79     /**
80      * @brief Judge state whether is running.
81      *
82      * @return True if success,else false.
83      */
84     bool IsRunning();
85     /**
86      * @brief Judge state whether is removed.
87      *
88      * @return True if success,else false.
89      */
90     bool IsRemoved();
91     /**
92      * @brief Judge state whether is repeating.
93      *
94      * @return True if success,else false.
95      */
96     bool IsRepeating();
97     /**
98      * @brief Judge state whether is paused.
99      *
100      * @return True if success,else false.
101      */
102     bool IsPaused();
103     /**
104      * @brief Judge state whether is last work timeout.
105      *
106      * @return True if success,else false.
107      */
108     bool IsLastWorkTimeout();
109     /**
110      * @brief The OnConditionChanged callback.
111      *
112      * @param type The type.
113      * @param value The value.
114      */
115     int32_t OnConditionChanged(WorkCondition::Type &type, std::shared_ptr<Condition> value);
116     /**
117      * @brief Mark round.
118      */
119     void MarkRound();
120     /**
121      * @brief Mark status.
122      */
123     void MarkStatus(Status status);
124     /**
125      * @brief Get status.
126      *
127      * @return The current status.
128      */
129     Status GetStatus();
130     /**
131      * @brief Dump.
132      *
133      * @param result The result.
134      */
135     void Dump(std::string& result);
136     /**
137      * @brief Update timer if need.
138      */
139     void UpdateTimerIfNeed();
140     /**
141      * @brief Need remove.
142      *
143      * @return True if success,else false.
144      */
145     bool NeedRemove();
146 
147     bool lastTimeout_ {false};
148     /**
149      * @brief Set min interval by group.
150      *
151      * @param group The new group.
152      * @return True if success,else false.
153      */
154     bool SetMinIntervalByGroup(int32_t group);
155     /**
156      * @brief Update map<uid, lastTime>.
157      */
158     void UpdateUidLastTimeMap();
159     /**
160      * @brief clear uidLastTimeMap by uid.
161      */
162     static void ClearUidLastTimeMap(int32_t uid);
163     /**
164      * @brief Set min interval by dump.
165      */
166     void SetMinIntervalByDump(int64_t interval);
167     /**
168      * @brief get min interval.
169      */
170     int64_t GetMinInterval();
171     bool IsUriKeySwitchOn();
172     void ToString(WorkCondition::Type conditionType);
173 private:
174     Status currentStatus_;
175     time_t baseTime_;
176     int64_t minInterval_;
177     bool groupChanged_;
178     ffrt::mutex conditionMapMutex_;
179     static ffrt::mutex s_uid_last_time_mutex;
180     static std::map<int32_t, time_t> s_uid_last_time_map;
181     std::string conditionStatus_;
182     void MarkTimeout();
183     bool IsSameUser();
184     bool SetMinInterval();
185     bool IsBatteryAndNetworkReady(WorkCondition::Type type);
186     bool IsStorageReady(WorkCondition::Type type);
187     bool IsChargerReady(WorkCondition::Type type);
188     bool IsNapReady(WorkCondition::Type type);
189     int GetPriority();
190     bool IsTimerReady(WorkCondition::Type type);
191     bool IsConditionReady();
192     bool IsStandbyExemption();
193 };
194 } // namespace WorkScheduler
195 } // namespace OHOS
196 #endif // WORK_SCHED_SERVICES_WORK_STATUS_H