• 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_SCHEDULER_SERVICES_H
17 #define FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_WORK_SCHEDULER_SERVICES_H
18 
19 #include <list>
20 #include <map>
21 #include <memory>
22 #include <mutex>
23 #include <string>
24 #include <vector>
25 
26 #include <iremote_object.h>
27 #include <system_ability.h>
28 
29 #include "ability_manager_interface.h"
30 #include "system_ability_status_change_stub.h"
31 #include "work_sched_service_stub.h"
32 #include "work_status.h"
33 #include "work_event_handler.h"
34 #include "delayed_sp_singleton.h"
35 
36 namespace OHOS {
37 namespace WorkScheduler {
38 class WorkQueueManager;
39 class WorkPolicyManager;
40 class WorkBundleGroupChangeCallback;
41 class SchedulerBgTaskSubscriber;
42 class WorkSchedulerService final : public SystemAbility, public WorkSchedServiceStub,
43     public std::enable_shared_from_this<WorkSchedulerService> {
44     DISALLOW_COPY_AND_MOVE(WorkSchedulerService);
45     DECLARE_SYSTEM_ABILITY(WorkSchedulerService);
46     DECLARE_DELAYED_SP_SINGLETON(WorkSchedulerService);
47 public:
48     WorkSchedulerService(const int32_t systemAbilityId, bool runOnCreate);
49 
50     /**
51      * @brief The OnStart callback.
52      */
53     void OnStart() override;
54     /**
55      * @brief The OnStop callback.
56      */
57     void OnStop() override;
58     /**
59      * @brief Start work.
60      *
61      * @param workInfo The info of work.
62      * @return error code, ERR_OK if success.
63      */
64     int32_t StartWork(WorkInfo& workInfo) override;
65     /**
66      * @brief Stop work.
67      *
68      * @param workInfo The info of work.
69      * @return error code, ERR_OK if success.
70      */
71     int32_t StopWork(WorkInfo& workInfo) override;
72     /**
73      * @brief Stop and cancel work.
74      *
75      * @param workInfo The info of work.
76      * @return error code, ERR_OK if success.
77      */
78     int32_t StopAndCancelWork(WorkInfo& workInfo) override;
79     /**
80      * @brief Stop and clear works.
81      *
82      * @return error code, ERR_OK if success.
83      */
84     int32_t StopAndClearWorks() override;
85     /**
86      * @brief The last work time out.
87      *
88      * @param workId The id of work.
89      * @param result True if the work executed time out, else false.
90      * @return error code, ERR_OK if success.
91      */
92     int32_t IsLastWorkTimeout(int32_t workId, bool &result) override;
93     /**
94      * @brief Obtain all works.
95      *
96      * @param uid The uid.
97      * @param pid The pid.
98      * @param workInfos The infos of work.
99      * @return error code, ERR_OK if success.
100      */
101     int32_t ObtainAllWorks(int32_t &uid, int32_t &pid,
102         std::list<std::shared_ptr<WorkInfo>>& workInfos) override;
103     /**
104      * @brief Get the status of work.
105      *
106      * @param uid The uid.
107      * @param workId The id of work.
108      * @param workInfo The info of work.
109      * @return error code, ERR_OK if success.
110      */
111     int32_t GetWorkStatus(int32_t &uid, int32_t &workId, std::shared_ptr<WorkInfo>& workInfo) override;
112     /**
113      * @brief Dump.
114      *
115      * @param fd The file descriptor.
116      * @param args The dump args.
117      * @return Status code, ERR_OK if success.
118      */
119     int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override;
120 
121     /**
122      * @brief Init persisted.
123      */
124     void InitPersisted();
125     /**
126      * @brief Stop and clear works by uid.
127      *
128      * @param uid The uid.
129      * @return True if success,else false.
130      */
131     bool StopAndClearWorksByUid(int32_t uid);
132     /**
133      * @brief Create node dir.
134      *
135      * @param dir The dir.
136      * @return ERR_OK.
137      */
138     int32_t CreateNodeDir(std::string dir);
139     /**
140      * @brief Create node file.
141      *
142      * @param filePath The file path.
143      * @return ERR_OK.
144      */
145     int32_t CreateNodeFile(std::string filePath);
146     /**
147      * @brief Update work before real start.
148      *
149      * @param work The work.
150      */
151     void UpdateWorkBeforeRealStart(std::shared_ptr<WorkStatus> work);
152     /**
153      * @brief The OnConditionReady callback.
154      *
155      * @param workStatusVector The work status vector.
156      */
157     void OnConditionReady(std::shared_ptr<std::vector<std::shared_ptr<WorkStatus>>> workStatusVector);
158     /**
159      * @brief Watchdog time out.
160      *
161      * @param workStatus The status of work.
162      */
163     void WatchdogTimeOut(std::shared_ptr<WorkStatus> workStatus);
164     /**
165      * @brief Init.
166      *
167      * @return True if success,else false.
168      */
169     bool Init();
170 
171     /**
172      * @brief Get handler.
173      *
174      * @return Handler.
175      */
GetHandler()176     std::shared_ptr<WorkEventHandler> GetHandler()
177     {
178         return handler_;
179     }
180 
181     /**
182      * @brief Get work queue manager.
183      *
184      * @return Work queue manager.
185      */
GetWorkQueueManager()186     std::shared_ptr<WorkQueueManager> GetWorkQueueManager()
187     {
188         return workQueueManager_;
189     }
190 
191     /**
192      * @brief Get work policy manager.
193      *
194      * @return work policy manager.
195      */
GetWorkPolicyManager()196     std::shared_ptr<WorkPolicyManager> GetWorkPolicyManager()
197     {
198         return workPolicyManager_;
199     }
200 
201     /**
202      * @brief add uid to the whitelist or delete uid from the whitelist.
203      *
204      * @param uid uid of the app.
205      * @param isAdd true if add name, else delete.
206      */
207     void UpdateEffiResApplyInfo(int32_t uid, bool isAdd);
208 
209     /**
210      * @brief init background task subscriber, subscribe self to bgtask service
211      *
212      * @return true seccess to init subscriber
213      * @return false fail to init subscriber
214      */
215     bool InitBgTaskSubscriber();
216 
217     /**
218      * @brief check uid has work_scheduler resources or not
219      *
220      * @param uid the uid of application
221      * @return true uid has work_scheduler resources or not
222      * @return false uid does not have work_scheduler resources or not
223      */
224     bool CheckEffiResApplyInfo(int32_t uid);
225 
226     /**
227      * @brief Get the efficiency resources uid List object
228      *
229      * @return std::string string consists of uid
230      */
231     std::string GetEffiResApplyUid();
232 
233     /**
234      * @brief Get the Efficiency Resources Infos object
235      *
236      * @return ErrCode ERR_OK if succeed, others if failed
237      */
238     ErrCode QueryResAppliedUid();
239 private:
240     std::set<int32_t> whitelist_;
241 #ifdef RESOURCESCHEDULE_BGTASKMGR_ENABLE
242     std::shared_ptr<SchedulerBgTaskSubscriber> subscriber_;
243 #endif
244 private:
245     class SystemAbilityStatusChangeListener : public OHOS::SystemAbilityStatusChangeStub {
246     public:
SystemAbilityStatusChangeListener()247         SystemAbilityStatusChangeListener() {};
248         void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
249     };
250 
251 private:
252     std::shared_ptr<WorkQueueManager> workQueueManager_;
253     std::shared_ptr<WorkPolicyManager> workPolicyManager_;
254     std::mutex mutex_;
255     std::map<std::string, std::shared_ptr<WorkInfo>> persistedMap_;
256     bool ready_ {false};
257     std::shared_ptr<WorkEventHandler> handler_;
258     std::shared_ptr<AppExecFwk::EventRunner> eventRunner_;
259     bool checkBundle_ {true};
260 #ifdef DEVICE_USAGE_STATISTICS_ENABLE
261     sptr<WorkBundleGroupChangeCallback> groupObserver_;
262 #endif
263 
264     void WorkQueueManagerInit();
265     bool WorkPolicyManagerInit();
266 #ifdef DEVICE_USAGE_STATISTICS_ENABLE
267     void GroupObserverInit();
268 #endif
269     void RefreshPersistedWorks();
270     std::list<std::shared_ptr<WorkInfo>> ReadPersistedWorks();
271     void InitPersistedWork(WorkInfo& workInfo);
272     void DumpAllInfo(std::string &result);
273     bool CheckWorkInfo(WorkInfo &workInfo, int32_t &uid);
274     bool StopWorkInner(std::shared_ptr<WorkStatus> workStatus, int32_t uid, const bool needCancel, bool isTimeOut);
275     bool CheckCondition(WorkInfo& workInfo);
276     bool IsBaseAbilityReady();
277     void DumpUsage(std::string &result);
278     void DumpParamSet(std::string &key, std::string &value, std::string &result);
279 };
280 } // namespace WorkScheduler
281 } // namespace OHOS
282 #endif // FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_WORK_SCHEDULER_SERVICES_H