• 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 
16 #ifndef FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_WORK_QUEUE_MANAGER_H
17 #define FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_WORK_QUEUE_MANAGER_H
18 
19 #include <memory>
20 #include <vector>
21 #include <map>
22 
23 #include "conditions/icondition_listener.h"
24 #include "work_queue.h"
25 #include "work_status.h"
26 
27 namespace OHOS {
28 namespace WorkScheduler {
29 class WorkSchedulerService;
30 class WorkQueueManager : std::enable_shared_from_this<WorkQueueManager> {
31 public:
32     explicit WorkQueueManager(const wptr<WorkSchedulerService>& wss);
33     ~WorkQueueManager() = default;
34     /**
35      * @brief Init.
36      *
37      * @return True if success,else false.
38      */
39     bool Init();
40     /**
41      * @brief Add listener.
42      *
43      * @param type The type.
44      * @param listener The listener.
45      * @return True if success,else false.
46      */
47     bool AddListener(WorkCondition::Type type, std::shared_ptr<IConditionListener> listener);
48     /**
49      * @brief Add work.
50      *
51      * @param workStatus The status of work.
52      * @return True if success,else false.
53      */
54     bool AddWork(std::shared_ptr<WorkStatus> workStatus);
55     /**
56      * @brief Remove work.
57      *
58      * @param workStatus The status of work.
59      * @return True if success,else false.
60      */
61     bool RemoveWork(std::shared_ptr<WorkStatus> workStatus);
62     /**
63      * @brief Cancel work.
64      *
65      * @param workStatus The status of work.
66      * @return True if success,else false.
67      */
68     bool CancelWork(std::shared_ptr<WorkStatus> workStatus);
69 
70     /**
71      * @brief The OnConditionChanged callback.
72      *
73      * @param conditionType The condition type.
74      * @param conditionVal The condition val.
75      */
76     void OnConditionChanged(WorkCondition::Type conditionType,
77         std::shared_ptr<DetectorValue> conditionVal);
78     /**
79      * @brief Stop and clear works.
80      *
81      * @param workList The list of work.
82      * @return True if success,else false.
83      */
84     bool StopAndClearWorks(std::list<std::shared_ptr<WorkStatus>> workList);
85     /**
86      * @brief Set time cycle.
87      *
88      * @param time The time.
89      */
90     void SetTimeCycle(uint32_t time);
91     /**
92      * @brief Get the cycle of time.
93      *
94      * @return The cycle of time.
95      */
96     uint32_t GetTimeCycle();
97     /**
98      * @brief Set time retrigger.
99      *
100      * @param time The time.
101      */
102     void SetTimeRetrigger(int32_t time);
103     /**
104      * @brief Get time retrigger.
105      *
106      * @param time The time.
107      */
108     int32_t GetTimeRetrigger();
109     /**
110      * @brief Dump.
111      *
112      * @param result The result.
113      */
114     void Dump(std::string& result);
115     /**
116      * @brief Set min interval by dump.
117      */
118     void SetMinIntervalByDump(int64_t interval);
119 private:
120     std::vector<std::shared_ptr<WorkStatus>> GetReayQueue(WorkCondition::Type conditionType,
121         std::shared_ptr<DetectorValue> conditionVal);
122     std::mutex mutex_;
123     const wptr<WorkSchedulerService> wss_;
124     std::map<WorkCondition::Type, std::shared_ptr<WorkQueue>> queueMap_;
125     std::map<WorkCondition::Type, std::shared_ptr<IConditionListener>> listenerMap_;
126 
127     uint32_t timeCycle_;
128 };
129 } // namespace WorkScheduler
130 } // namespace OHOS
131 #endif // FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_WORK_QUEUE_MANAGER_H