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 #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
StopWork(WorkInfo & workInfo)90 ErrCode WorkSchedulerSrvClient::StopWork(WorkInfo& workInfo)
91 {
92 WS_HILOGD("Stop Work");
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_->StopWork(workInfo);
100 }
101
StopAndCancelWork(WorkInfo & workInfo)102 ErrCode WorkSchedulerSrvClient::StopAndCancelWork(WorkInfo& workInfo)
103 {
104 WS_HILOGD("Stop And Cancel 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_->StopAndCancelWork(workInfo);
112 }
113
StopAndClearWorks()114 ErrCode WorkSchedulerSrvClient::StopAndClearWorks()
115 {
116 WS_HILOGD("Stop And Clear Works");
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_->StopAndClearWorks();
124 }
125
IsLastWorkTimeout(int32_t workId,bool & result)126 ErrCode WorkSchedulerSrvClient::IsLastWorkTimeout(int32_t workId, bool &result)
127 {
128 WS_HILOGD("Is LastWork Timeout");
129 std::lock_guard<std::mutex> lock(mutex_);
130 ErrCode errCode = Connect();
131 if (errCode != ERR_OK) {
132 return errCode;
133 }
134 errCode = iWorkSchedService_->IsLastWorkTimeout(workId, result);
135 return errCode;
136 }
137
ObtainAllWorks(std::list<std::shared_ptr<WorkInfo>> & workInfos)138 ErrCode WorkSchedulerSrvClient::ObtainAllWorks(std::list<std::shared_ptr<WorkInfo>> &workInfos)
139 {
140 WS_HILOGD("Obtain All Works");
141 std::lock_guard<std::mutex> lock(mutex_);
142 ErrCode errCode = Connect();
143 if (errCode != ERR_OK) {
144 return errCode;
145 }
146 return iWorkSchedService_->ObtainAllWorks(workInfos);
147 }
148
GetWorkStatus(int32_t workId,std::shared_ptr<WorkInfo> & workInfo)149 ErrCode WorkSchedulerSrvClient::GetWorkStatus(int32_t workId, std::shared_ptr<WorkInfo> &workInfo)
150 {
151 WS_HILOGD("Get Work Status");
152 if (workId <= 0) {
153 return E_WORKID_ERR;
154 }
155 std::lock_guard<std::mutex> lock(mutex_);
156 ErrCode code = Connect();
157 if (code != ERR_OK) {
158 return code;
159 }
160 return iWorkSchedService_->GetWorkStatus(workId, workInfo);
161 }
162
GetAllRunningWorks(std::list<std::shared_ptr<WorkInfo>> & workInfos)163 ErrCode WorkSchedulerSrvClient::GetAllRunningWorks(std::list<std::shared_ptr<WorkInfo>>& workInfos)
164 {
165 WS_HILOGD("Get Running Work Scheduler Work");
166 if (!workInfos.empty()) {
167 return E_PARAM_ERROR;
168 }
169 std::lock_guard<std::mutex> lock(mutex_);
170 ErrCode code = Connect();
171 if (code != ERR_OK) {
172 return code;
173 }
174 return iWorkSchedService_->GetAllRunningWorks(workInfos);
175 }
176
PauseRunningWorks(int32_t uid)177 ErrCode WorkSchedulerSrvClient::PauseRunningWorks(int32_t uid)
178 {
179 WS_HILOGD("Pause Running Work Scheduler Work, uid:%{public}d", uid);
180 if (uid < 0) {
181 return E_PARAM_ERROR;
182 }
183 std::lock_guard<std::mutex> lock(mutex_);
184 ErrCode code = Connect();
185 if (code != ERR_OK) {
186 return code;
187 }
188 return iWorkSchedService_->PauseRunningWorks(uid);
189 }
190
ResumePausedWorks(int32_t uid)191 ErrCode WorkSchedulerSrvClient::ResumePausedWorks(int32_t uid)
192 {
193 WS_HILOGD("Resume Paused Work Scheduler Work, uid:%{public}d", uid);
194 if (uid < 0) {
195 return E_PARAM_ERROR;
196 }
197 std::lock_guard<std::mutex> lock(mutex_);
198 ErrCode code = Connect();
199 if (code != ERR_OK) {
200 return code;
201 }
202 return iWorkSchedService_->ResumePausedWorks(uid);
203 }
204
SetWorkSchedulerConfig(const std::string & configData,int32_t sourceType)205 ErrCode WorkSchedulerSrvClient::SetWorkSchedulerConfig(const std::string &configData, int32_t sourceType)
206 {
207 WS_HILOGD("Set work scheduler config");
208 std::lock_guard<std::mutex> lock(mutex_);
209 ErrCode code = Connect();
210 if (code != ERR_OK) {
211 return code;
212 }
213 return iWorkSchedService_->SetWorkSchedulerConfig(configData, sourceType);
214 }
215 } // namespace WorkScheduler
216 } // namespace OHOS