• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "policy_type.h"
23 #include "policy/ipolicy_filter.h"
24 #include "work_conn_manager.h"
25 #include "work_queue.h"
26 #include "work_status.h"
27 
28 namespace OHOS {
29 namespace WorkScheduler {
30 class WorkSchedulerService;
31 class WorkEventHandler;
32 class AppRemovedListener;
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();
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      * @brief The Dump.
144      *
145      * @param result The result.
146      */
147     void Dump(std::string& result);
148     int32_t currentRunningCount = 0;
149     /**
150      * @brief Check work to run.
151      */
152     void CheckWorkToRun();
153     /**
154      * @brief Send retrigger.
155      *
156      * @param delayTime The delay time.
157      */
158     void SendRetrigger(int32_t delayTime);
159     /**
160      * @brief Add app remove listener.
161      *
162      * @param listener The listener.
163      */
164     void AddAppRemoveListener(std::shared_ptr<AppRemovedListener> listener);
165     /**
166      * @brief Add app data clear listener.
167      *
168      * @param listener The listener.
169      */
170     void AddAppDataClearListener(std::shared_ptr<AppDataClearListener> listener);
171     /**
172      * @brief Watchdog time out.
173      *
174      * @param watchdogId The id of watchdog.
175      */
176     void WatchdogTimeOut(uint32_t watchdogId);
177     /**
178      * @brief Set memory by dump.
179      *
180      * @param memory The memory.
181      */
182     void SetMemoryByDump(int32_t memory);
183     /**
184      * @brief Get dump set memory.
185      *
186      * @return Dump set memory.
187      */
188     int32_t GetDumpSetMemory();
189     /**
190      * @brief Set watchdog time by dump.
191      *
192      * @param time The time.
193      */
194     void SetWatchdogTimeByDump(int32_t time);
195     /**
196      * @brief Set watchdog time.
197      *
198      * @param time The time.
199      */
200     void SetWatchdogTime(int32_t time);
201     /**
202      * @brief Get the time of watchdog.
203      *
204      * @return The time of watchdog.
205      */
206     int32_t GetWatchdogTime();
207 
208 private:
209     int32_t GetMaxRunningCount();
210     int32_t GetRunningCount();
211     void DumpConditionReadyQueue(std::string& result);
212     void DumpUidQueueMap(std::string& result);
213     void RemoveFromUidQueue(std::shared_ptr<WorkStatus> workStatus, int32_t uid);
214     void RemoveFromReadyQueue(std::shared_ptr<WorkStatus> workStatus);
215     void AddToReadyQueue(std::shared_ptr<std::vector<std::shared_ptr<WorkStatus>>> workStatusVector);
216     void RealStartWork(std::shared_ptr<WorkStatus> workStatus);
217     void AddToRunningQueue(std::shared_ptr<WorkStatus> workStatus);
218     void RemoveConditionUnReady();
219     std::shared_ptr<WorkStatus> GetWorkToRun();
220     void RemoveAllUnReady();
221     uint32_t NewWatchdogId();
222     void AddWatchdogForWork(std::shared_ptr<WorkStatus> workStatus);
223     std::shared_ptr<WorkStatus> GetWorkFromWatchdog(uint32_t id);
224     void UpdateWatchdogTime(const wptr<WorkSchedulerService> &wmsptr,
225         std::shared_ptr<WorkStatus> &topWork);
226 
227     const wptr<WorkSchedulerService> wss_;
228     std::shared_ptr<WorkConnManager> workConnManager_;
229     std::shared_ptr<WorkEventHandler> handler_;
230 
231     std::mutex uidMapMutex_;
232     std::map<int32_t, std::shared_ptr<WorkQueue>> uidQueueMap_;
233 
234     std::mutex conditionReadyMutex_;
235     std::shared_ptr<WorkQueue> conditionReadyQueue_;
236 
237     std::list<std::shared_ptr<IPolicyFilter>> policyFilters_;
238     std::shared_ptr<AppRemovedListener> appRemovedListener_;
239     std::shared_ptr<AppDataClearListener> appDataClearListener_;
240 
241     std::shared_ptr<Watchdog> watchdog_;
242 
243     std::mutex watchdogIdMapMutex_;
244     std::map<uint32_t, std::shared_ptr<WorkStatus>> watchdogIdMap_;
245 
246     uint32_t watchdogId_;
247     int32_t dumpSetMemory_;
248     int32_t watchdogTime_;
249 };
250 } // namespace WorkScheduler
251 } // namespace OHOS
252 
253 #endif // FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_WORK_SCHED_SERVICES_POLICY_MANAGER_H