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_WORKSCHEDDULER_SRV_CLIENT_H
16 #define FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_WORKSCHEDDULER_SRV_CLIENT_H
17 
18 #include <list>
19 #include <string>
20 
21 #include <singleton.h>
22 
23 #include "iwork_sched_service.h"
24 
25 namespace OHOS {
26 namespace WorkScheduler {
27 class WorkSchedulerSrvClient final : public DelayedRefSingleton<WorkSchedulerSrvClient> {
28     DECLARE_DELAYED_REF_SINGLETON(WorkSchedulerSrvClient)
29 public:
30     DISALLOW_COPY_AND_MOVE(WorkSchedulerSrvClient);
31 
32     /**
33      * @brief Start work.
34      *
35      * @param workInfo The info of work.
36      * @return ERR_OK on success, others on failure.
37      */
38     ErrCode StartWork(WorkInfo& workInfo);
39     /**
40      * @brief Stop work.
41      *
42      * @param workInfo The info of work.
43      * @return ERR_OK on success, others on failure.
44      */
45     ErrCode StopWork(WorkInfo& workInfo);
46     /**
47      * @brief Stop and cancel work.
48      *
49      * @param workInfo The info of work.
50      * @return ERR_OK on success, others on failure.
51      */
52     ErrCode StopAndCancelWork(WorkInfo& workInfo);
53     /**
54      * @brief Stop and clear works.
55      *
56      * @return ERR_OK on success, others on failure.
57      */
58     ErrCode StopAndClearWorks();
59     /**
60      * @brief The last work time out.
61      *
62      * @param workId The id of work.
63      * @param result True if the work executed time out, else false.
64      * @return ERR_OK on success, others on failure.
65      */
66     ErrCode IsLastWorkTimeout(int32_t workId, bool &result);
67     /**
68      * @brief Get the status of work.
69      *
70      * @param workId The id of work.
71      * @param workInfo The info of work.
72      * @return ERR_OK on success, others on failure.
73      */
74     ErrCode GetWorkStatus(int32_t workId, std::shared_ptr<WorkInfo> &workInfo);
75     /**
76      * @brief Obtain all works.
77      *
78      * @param workInfos The infos of work.
79      * @return ERR_OK on success, others on failure.
80      */
81     ErrCode ObtainAllWorks(std::list<std::shared_ptr<WorkInfo>> &workInfos);
82 
83     /**
84      * @brief Get the Running Work Scheduler Work object
85      *
86      * @param workInfos The infos of work.
87      * @return ErrCode ERR_OK on success, others on failure
88      */
89     ErrCode GetAllRunningWorks(std::list<std::shared_ptr<WorkInfo>>& workInfos);
90 
91     /**
92      * @brief Pause Running Works.
93      *
94      * @param uid The uid.
95      * @return The errcode. ERR_OK on success, others on failure.
96      */
97     ErrCode PauseRunningWorks(int32_t uid);
98 
99     /**
100      * @brief Resume Paused works.
101      *
102      * @param uid The uid.
103      * @return ErrCode ERR_OK on success, others on failure
104      */
105     ErrCode ResumePausedWorks(int32_t uid);
106 
107     /**
108      * @brief Set work scheduler config.
109      *
110      * @param configData config param.
111      * @param sourceType data source.
112      * @return ErrCode ERR_OK on success, others on failure
113      */
114     ErrCode SetWorkSchedulerConfig(const std::string &configData, int32_t sourceType);
115 private:
116     class WorkSchedulerDeathRecipient : public IRemoteObject::DeathRecipient {
117     public:
118         explicit WorkSchedulerDeathRecipient(WorkSchedulerSrvClient &workSchedulerSrvClient);
119         ~WorkSchedulerDeathRecipient() override = default;
120         void OnRemoteDied(const wptr<IRemoteObject>& remote) override;
121     private:
122         WorkSchedulerSrvClient &workSchedulerSrvClient_;
123     };
124 
125     ErrCode Connect();
126     sptr<IWorkSchedService> iWorkSchedService_ {nullptr};
127     sptr<IRemoteObject::DeathRecipient> deathRecipient_ {nullptr};
128     void ResetProxy();
129     std::mutex mutex_;
130 };
131 } // namespace WorkScheduler
132 } // namespace OHOS
133 #endif // FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_WORKSCHEDDULER_SRV_CLIENT_H