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 #include "workscheduler_srv_client.h"
16
17 #include <if_system_ability_manager.h>
18 #include <ipc_skeleton.h>
19 #include <iservice_registry.h>
20 #include <system_ability_definition.h>
21
22 #include "work_sched_errors.h"
23 #include "work_sched_hilog.h"
24
25 namespace OHOS {
26 namespace WorkScheduler {
WorkSchedulerSrvClient()27 WorkSchedulerSrvClient::WorkSchedulerSrvClient() {}
28
~WorkSchedulerSrvClient()29 WorkSchedulerSrvClient::~WorkSchedulerSrvClient() {}
30
Connect()31 ErrCode WorkSchedulerSrvClient::Connect()
32 {
33 if (iWorkSchedService_ != nullptr) {
34 return ERR_OK;
35 }
36 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
37 if (sam == nullptr) {
38 WS_HILOGE("GetSystemAbilityManager failed!");
39 return E_CLIENT_CONNECT_SERVICE_FAILED;
40 }
41 sptr<IRemoteObject> remoteObject_ = sam->CheckSystemAbility(WORK_SCHEDULE_SERVICE_ID);
42 if (remoteObject_ == nullptr) {
43 WS_HILOGE("GetSystemAbility failed!");
44 return E_CLIENT_CONNECT_SERVICE_FAILED;
45 }
46 deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new WorkSchedulerDeathRecipient(*this));
47 if (deathRecipient_ == nullptr) {
48 WS_HILOGE("Failed to create WorkScheduelrDeathRecipient!");
49 return E_CLIENT_CONNECT_SERVICE_FAILED;
50 }
51 if ((remoteObject_->IsProxyObject()) && (!remoteObject_->AddDeathRecipient(deathRecipient_))) {
52 WS_HILOGE("Add death recipient to WorkSchedulerService failed!");
53 return E_CLIENT_CONNECT_SERVICE_FAILED;
54 }
55 iWorkSchedService_ = iface_cast<IWorkSchedService>(remoteObject_);
56 WS_HILOGD("Connecting WorkSchedService success.");
57 return ERR_OK;
58 }
59
ResetProxy()60 void WorkSchedulerSrvClient::ResetProxy()
61 {
62 std::lock_guard<std::mutex> lock(mutex_);
63 if (iWorkSchedService_ != nullptr && (iWorkSchedService_->AsObject() != nullptr)) {
64 iWorkSchedService_->AsObject()->RemoveDeathRecipient(deathRecipient_);
65 }
66 iWorkSchedService_ = nullptr;
67 }
68
WorkSchedulerDeathRecipient(WorkSchedulerSrvClient & workSchedulerSrvClient)69 WorkSchedulerSrvClient::WorkSchedulerDeathRecipient::WorkSchedulerDeathRecipient(
70 WorkSchedulerSrvClient &workSchedulerSrvClient) : workSchedulerSrvClient_(workSchedulerSrvClient) {}
71
OnRemoteDied(const wptr<IRemoteObject> & remote)72 void WorkSchedulerSrvClient::WorkSchedulerDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
73 {
74 WS_HILOGD("Work Scheduler Death Recipient Recv death notice.");
75 workSchedulerSrvClient_.ResetProxy();
76 }
77
StartWork(WorkInfo & workInfo)78 ErrCode WorkSchedulerSrvClient::StartWork(WorkInfo& workInfo)
79 {
80 WS_HILOGD("Start Work");
81 std::lock_guard<std::mutex> lock(mutex_);
82 ErrCode ret = Connect();
83 if (ret != ERR_OK) {
84 WS_HILOGE("Connect() failed, errno: %{public}d", ret);
85 return ret;
86 }
87 return iWorkSchedService_->StartWork(workInfo);
88 }
89
StartWorkForInner(WorkInfo & workInfo)90 ErrCode WorkSchedulerSrvClient::StartWorkForInner(WorkInfo& workInfo)
91 {
92 WS_HILOGD("start work for inner");
93 std::lock_guard<std::mutex> lock(mutex_);
94 ErrCode ret = Connect();
95 if (ret != ERR_OK) {
96 WS_HILOGE("Connect() failed, errno: %{public}d", ret);
97 return ret;
98 }
99 return iWorkSchedService_->StartWorkForInner(workInfo);
100 }
101
StopWork(WorkInfo & workInfo)102 ErrCode WorkSchedulerSrvClient::StopWork(WorkInfo& workInfo)
103 {
104 WS_HILOGD("Stop Work");
105 std::lock_guard<std::mutex> lock(mutex_);
106 ErrCode ret = Connect();
107 if (ret != ERR_OK) {
108 WS_HILOGE("Connect() failed, errno: %{public}d", ret);
109 return ret;
110 }
111 return iWorkSchedService_->StopWork(workInfo);
112 }
113
StopWorkForInner(WorkInfo & workInfo,bool needCancel)114 ErrCode WorkSchedulerSrvClient::StopWorkForInner(WorkInfo& workInfo, bool needCancel)
115 {
116 WS_HILOGD("stop work for inner");
117 std::lock_guard<std::mutex> lock(mutex_);
118 ErrCode ret = Connect();
119 if (ret != ERR_OK) {
120 WS_HILOGE("Connect() failed, errno: %{public}d", ret);
121 return ret;
122 }
123 return iWorkSchedService_->StopWorkForInner(workInfo, needCancel);
124 }
125
StopAndCancelWork(WorkInfo & workInfo)126 ErrCode WorkSchedulerSrvClient::StopAndCancelWork(WorkInfo& workInfo)
127 {
128 WS_HILOGD("Stop And Cancel Work");
129 std::lock_guard<std::mutex> lock(mutex_);
130 ErrCode ret = Connect();
131 if (ret != ERR_OK) {
132 WS_HILOGE("Connect() failed, errno: %{public}d", ret);
133 return ret;
134 }
135 return iWorkSchedService_->StopAndCancelWork(workInfo);
136 }
137
StopAndClearWorks()138 ErrCode WorkSchedulerSrvClient::StopAndClearWorks()
139 {
140 WS_HILOGD("Stop And Clear Works");
141 std::lock_guard<std::mutex> lock(mutex_);
142 ErrCode ret = Connect();
143 if (ret != ERR_OK) {
144 WS_HILOGE("Connect() failed, errno: %{public}d", ret);
145 return ret;
146 }
147 return iWorkSchedService_->StopAndClearWorks();
148 }
149
IsLastWorkTimeout(int32_t workId,bool & result)150 ErrCode WorkSchedulerSrvClient::IsLastWorkTimeout(int32_t workId, bool &result)
151 {
152 WS_HILOGD("Is LastWork Timeout");
153 std::lock_guard<std::mutex> lock(mutex_);
154 ErrCode errCode = Connect();
155 if (errCode != ERR_OK) {
156 return errCode;
157 }
158 errCode = iWorkSchedService_->IsLastWorkTimeout(workId, result);
159 return errCode;
160 }
161
ObtainAllWorks(std::list<std::shared_ptr<WorkInfo>> & workInfos)162 ErrCode WorkSchedulerSrvClient::ObtainAllWorks(std::list<std::shared_ptr<WorkInfo>> &workInfos)
163 {
164 WS_HILOGD("Obtain All Works");
165 std::lock_guard<std::mutex> lock(mutex_);
166 ErrCode errCode = Connect();
167 if (errCode != ERR_OK) {
168 return errCode;
169 }
170 std::vector<WorkInfo> vectorWorkInfos;
171 ErrCode ret = iWorkSchedService_->ObtainAllWorks(vectorWorkInfos);
172 for (const auto& workInfo : vectorWorkInfos) {
173 workInfos.push_back(std::make_shared<WorkInfo>(workInfo));
174 }
175 return ret;
176 }
177
ObtainWorksByUidAndWorkIdForInner(int32_t uid,std::list<std::shared_ptr<WorkInfo>> & workInfos,int32_t workId)178 ErrCode WorkSchedulerSrvClient::ObtainWorksByUidAndWorkIdForInner(int32_t uid,
179 std::list<std::shared_ptr<WorkInfo>> &workInfos, int32_t workId)
180 {
181 WS_HILOGD("Obtain Works By uid and workId for inner");
182 std::lock_guard<std::mutex> lock(mutex_);
183 ErrCode errCode = Connect();
184 if (errCode != ERR_OK) {
185 return errCode;
186 }
187 if (uid < 0) {
188 WS_HILOGE("param uid: %{public}d invaild.", uid);
189 return E_PARAM_INVAILD_UID;
190 }
191 std::vector<WorkInfo> vectorWorkInfos;
192 ErrCode ret = iWorkSchedService_->ObtainWorksByUidAndWorkIdForInner(uid, vectorWorkInfos, workId);
193 for (const auto& workInfo : vectorWorkInfos) {
194 workInfos.push_back(std::make_shared<WorkInfo>(workInfo));
195 }
196 return ret;
197 }
198
GetWorkStatus(int32_t workId,std::shared_ptr<WorkInfo> & workInfo)199 ErrCode WorkSchedulerSrvClient::GetWorkStatus(int32_t workId, std::shared_ptr<WorkInfo> &workInfo)
200 {
201 WS_HILOGD("Get Work Status");
202 if (workId <= 0) {
203 return E_WORKID_ERR;
204 }
205 std::lock_guard<std::mutex> lock(mutex_);
206 ErrCode code = Connect();
207 if (code != ERR_OK) {
208 return code;
209 }
210 WorkInfo workInfoTemp;
211 ErrCode ret = iWorkSchedService_->GetWorkStatus(workId, workInfoTemp);
212 if (ret == ERR_OK) {
213 workInfo = std::make_shared<WorkInfo>(workInfoTemp);
214 }
215 return ret;
216 }
217
GetAllRunningWorks(std::list<std::shared_ptr<WorkInfo>> & workInfos)218 ErrCode WorkSchedulerSrvClient::GetAllRunningWorks(std::list<std::shared_ptr<WorkInfo>>& workInfos)
219 {
220 WS_HILOGD("Get Running Work Scheduler Work");
221 if (!workInfos.empty()) {
222 return E_PARAM_ERROR;
223 }
224 std::lock_guard<std::mutex> lock(mutex_);
225 ErrCode code = Connect();
226 if (code != ERR_OK) {
227 return code;
228 }
229 std::vector<WorkInfo> vectorWorkInfos;
230 ErrCode ret = iWorkSchedService_->GetAllRunningWorks(vectorWorkInfos);
231 for (const auto& workInfo : vectorWorkInfos) {
232 workInfos.push_back(std::make_shared<WorkInfo>(workInfo));
233 }
234 return ret;
235 }
236
PauseRunningWorks(int32_t uid)237 ErrCode WorkSchedulerSrvClient::PauseRunningWorks(int32_t uid)
238 {
239 WS_HILOGD("Pause Running Work Scheduler Work, uid:%{public}d", uid);
240 if (uid < 0) {
241 return E_PARAM_ERROR;
242 }
243 std::lock_guard<std::mutex> lock(mutex_);
244 ErrCode code = Connect();
245 if (code != ERR_OK) {
246 return code;
247 }
248 return iWorkSchedService_->PauseRunningWorks(uid);
249 }
250
ResumePausedWorks(int32_t uid)251 ErrCode WorkSchedulerSrvClient::ResumePausedWorks(int32_t uid)
252 {
253 WS_HILOGD("Resume Paused Work Scheduler Work, uid:%{public}d", uid);
254 if (uid < 0) {
255 return E_PARAM_ERROR;
256 }
257 std::lock_guard<std::mutex> lock(mutex_);
258 ErrCode code = Connect();
259 if (code != ERR_OK) {
260 return code;
261 }
262 return iWorkSchedService_->ResumePausedWorks(uid);
263 }
264
SetWorkSchedulerConfig(const std::string & configData,int32_t sourceType)265 ErrCode WorkSchedulerSrvClient::SetWorkSchedulerConfig(const std::string &configData, int32_t sourceType)
266 {
267 WS_HILOGD("Set work scheduler config");
268 std::lock_guard<std::mutex> lock(mutex_);
269 ErrCode code = Connect();
270 if (code != ERR_OK) {
271 return code;
272 }
273 return iWorkSchedService_->SetWorkSchedulerConfig(configData, sourceType);
274 }
275
StopWorkForSA(int32_t saId)276 ErrCode WorkSchedulerSrvClient::StopWorkForSA(int32_t saId)
277 {
278 WS_HILOGD("Stop SA");
279 std::lock_guard<std::mutex> lock(mutex_);
280 ErrCode code = Connect();
281 if (code != ERR_OK) {
282 return code;
283 }
284 return iWorkSchedService_->StopWorkForSA(saId);
285 }
286 } // namespace WorkScheduler
287 } // namespace OHOS