• 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 #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     std::vector<WorkInfo> vectorWorkInfos;
147     ErrCode ret = iWorkSchedService_->ObtainAllWorks(vectorWorkInfos);
148     for (const auto& workInfo : vectorWorkInfos) {
149         workInfos.push_back(std::make_shared<WorkInfo>(workInfo));
150     }
151     return ret;
152 }
153 
GetWorkStatus(int32_t workId,std::shared_ptr<WorkInfo> & workInfo)154 ErrCode WorkSchedulerSrvClient::GetWorkStatus(int32_t workId, std::shared_ptr<WorkInfo> &workInfo)
155 {
156     WS_HILOGD("Get Work Status");
157     if (workId <= 0) {
158         return E_WORKID_ERR;
159     }
160     std::lock_guard<std::mutex> lock(mutex_);
161     ErrCode code = Connect();
162     if (code != ERR_OK) {
163         return code;
164     }
165     WorkInfo workInfoTemp;
166     ErrCode ret = iWorkSchedService_->GetWorkStatus(workId, workInfoTemp);
167     if (ret == ERR_OK) {
168         workInfo = std::make_shared<WorkInfo>(workInfoTemp);
169     }
170     return ret;
171 }
172 
GetAllRunningWorks(std::list<std::shared_ptr<WorkInfo>> & workInfos)173 ErrCode WorkSchedulerSrvClient::GetAllRunningWorks(std::list<std::shared_ptr<WorkInfo>>& workInfos)
174 {
175     WS_HILOGD("Get Running Work Scheduler Work");
176     if (!workInfos.empty()) {
177         return E_PARAM_ERROR;
178     }
179     std::lock_guard<std::mutex> lock(mutex_);
180     ErrCode code = Connect();
181     if (code != ERR_OK) {
182         return code;
183     }
184     std::vector<WorkInfo> vectorWorkInfos;
185     ErrCode ret = iWorkSchedService_->GetAllRunningWorks(vectorWorkInfos);
186     for (const auto& workInfo : vectorWorkInfos) {
187         workInfos.push_back(std::make_shared<WorkInfo>(workInfo));
188     }
189     return ret;
190 }
191 
PauseRunningWorks(int32_t uid)192 ErrCode WorkSchedulerSrvClient::PauseRunningWorks(int32_t uid)
193 {
194     WS_HILOGD("Pause Running Work Scheduler Work, uid:%{public}d", uid);
195     if (uid < 0) {
196         return E_PARAM_ERROR;
197     }
198     std::lock_guard<std::mutex> lock(mutex_);
199     ErrCode code = Connect();
200     if (code != ERR_OK) {
201         return code;
202     }
203     return iWorkSchedService_->PauseRunningWorks(uid);
204 }
205 
ResumePausedWorks(int32_t uid)206 ErrCode WorkSchedulerSrvClient::ResumePausedWorks(int32_t uid)
207 {
208     WS_HILOGD("Resume Paused Work Scheduler Work, uid:%{public}d", uid);
209     if (uid < 0) {
210         return E_PARAM_ERROR;
211     }
212     std::lock_guard<std::mutex> lock(mutex_);
213     ErrCode code = Connect();
214     if (code != ERR_OK) {
215         return code;
216     }
217     return iWorkSchedService_->ResumePausedWorks(uid);
218 }
219 
SetWorkSchedulerConfig(const std::string & configData,int32_t sourceType)220 ErrCode WorkSchedulerSrvClient::SetWorkSchedulerConfig(const std::string &configData, int32_t sourceType)
221 {
222     WS_HILOGD("Set work scheduler config");
223     std::lock_guard<std::mutex> lock(mutex_);
224     ErrCode code = Connect();
225     if (code != ERR_OK) {
226         return code;
227     }
228     return iWorkSchedService_->SetWorkSchedulerConfig(configData, sourceType);
229 }
230 
StopWorkForSA(int32_t saId)231 ErrCode WorkSchedulerSrvClient::StopWorkForSA(int32_t saId)
232 {
233     WS_HILOGD("Stop SA");
234     std::lock_guard<std::mutex> lock(mutex_);
235     ErrCode code = Connect();
236     if (code != ERR_OK) {
237         return code;
238     }
239     return iWorkSchedService_->StopWorkForSA(saId);
240 }
241 } // namespace WorkScheduler
242 } // namespace OHOS