1 /* 2 * Copyright (c) 2022-2025 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 Start work for inner. 41 * 42 * @param workInfo The info of work. 43 * @return ERR_OK on success, others on failure. 44 */ 45 ErrCode StartWorkForInner(WorkInfo& workInfo); 46 /** 47 * @brief Stop work. 48 * 49 * @param workInfo The info of work. 50 * @return ERR_OK on success, others on failure. 51 */ 52 ErrCode StopWork(WorkInfo& workInfo); 53 /** 54 * @brief Stop work for inner. 55 * 56 * @param workInfo The info of work. 57 * @param needCancel True if force stop the work that is being executed, else false. 58 * @return ERR_OK on success, others on failure. 59 */ 60 ErrCode StopWorkForInner(WorkInfo& workInfo, bool needCancel = false); 61 /** 62 * @brief Stop and cancel work. 63 * 64 * @param workInfo The info of work. 65 * @return ERR_OK on success, others on failure. 66 */ 67 ErrCode StopAndCancelWork(WorkInfo& workInfo); 68 /** 69 * @brief Stop and clear works. 70 * 71 * @return ERR_OK on success, others on failure. 72 */ 73 ErrCode StopAndClearWorks(); 74 /** 75 * @brief The last work time out. 76 * 77 * @param workId The id of work. 78 * @param result True if the work executed time out, else false. 79 * @return ERR_OK on success, others on failure. 80 */ 81 ErrCode IsLastWorkTimeout(int32_t workId, bool &result); 82 /** 83 * @brief Get the status of work. 84 * 85 * @param workId The id of work. 86 * @param workInfo The info of work. 87 * @return ERR_OK on success, others on failure. 88 */ 89 ErrCode GetWorkStatus(int32_t workId, std::shared_ptr<WorkInfo> &workInfo); 90 /** 91 * @brief Obtain all works. 92 * 93 * @param workInfos The infos of work. 94 * @return ERR_OK on success, others on failure. 95 */ 96 ErrCode ObtainAllWorks(std::list<std::shared_ptr<WorkInfo>> &workInfos); 97 98 /** 99 * @brief Obtain works by uid and workId for inner. 100 * @param uid The uid. 101 * @param workInfos The infos of work. 102 * @param workId The id of work. 103 * @return error code, ERR_OK if success. 104 */ 105 ErrCode ObtainWorksByUidAndWorkIdForInner(int32_t uid, 106 std::list<std::shared_ptr<WorkInfo>> &workInfos, int32_t workId = -1); 107 108 /** 109 * @brief Get the Running Work Scheduler Work object 110 * 111 * @param workInfos The infos of work. 112 * @return ErrCode ERR_OK on success, others on failure 113 */ 114 ErrCode GetAllRunningWorks(std::list<std::shared_ptr<WorkInfo>>& workInfos); 115 116 /** 117 * @brief Pause Running Works. 118 * 119 * @param uid The uid. 120 * @return The errcode. ERR_OK on success, others on failure. 121 */ 122 ErrCode PauseRunningWorks(int32_t uid); 123 124 /** 125 * @brief Resume Paused works. 126 * 127 * @param uid The uid. 128 * @return ErrCode ERR_OK on success, others on failure 129 */ 130 ErrCode ResumePausedWorks(int32_t uid); 131 132 /** 133 * @brief Set work scheduler config. 134 * 135 * @param configData config param. 136 * @param sourceType data source. 137 * @return ErrCode ERR_OK on success, others on failure 138 */ 139 ErrCode SetWorkSchedulerConfig(const std::string &configData, int32_t sourceType); 140 141 /** 142 * @brief Stop SA. 143 * 144 * @param saId SA id. 145 * @return ErrCode ERR_OK on success, others on failure 146 */ 147 ErrCode StopWorkForSA(int32_t saId); 148 private: 149 class WorkSchedulerDeathRecipient : public IRemoteObject::DeathRecipient { 150 public: 151 explicit WorkSchedulerDeathRecipient(WorkSchedulerSrvClient &workSchedulerSrvClient); 152 ~WorkSchedulerDeathRecipient() override = default; 153 void OnRemoteDied(const wptr<IRemoteObject>& remote) override; 154 private: 155 WorkSchedulerSrvClient &workSchedulerSrvClient_; 156 }; 157 158 ErrCode Connect(); 159 sptr<IWorkSchedService> iWorkSchedService_ {nullptr}; 160 sptr<IRemoteObject::DeathRecipient> deathRecipient_ {nullptr}; 161 void ResetProxy(); 162 std::mutex mutex_; 163 }; 164 } // namespace WorkScheduler 165 } // namespace OHOS 166 #endif // FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_WORKSCHEDDULER_SRV_CLIENT_H