• 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 {
31     if (iWorkSchedService_ != nullptr) {
32         auto remoteObject = iWorkSchedService_->AsObject();
33         if (remoteObject != nullptr) {
34             remoteObject->RemoveDeathRecipient(deathRecipient_);
35         }
36     }
37 }
38 
Connect()39 ErrCode WorkSchedulerSrvClient::Connect()
40 {
41     std::lock_guard<std::mutex> lock(mutex_);
42     if (iWorkSchedService_ != nullptr) {
43         return ERR_OK;
44     }
45     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
46     if (sam == nullptr) {
47         WS_HILOGE("GetSystemAbilityManager failed!");
48         return E_CLIENT_CONNECT_SERVICE_FAILED;
49     }
50     sptr<IRemoteObject> remoteObject_ = sam->CheckSystemAbility(WORK_SCHEDULE_SERVICE_ID);
51     if (remoteObject_ == nullptr) {
52         WS_HILOGE("GetSystemAbility failed!");
53         return E_CLIENT_CONNECT_SERVICE_FAILED;
54     }
55     deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new WorkSchedulerDeathRecipient());
56     if (deathRecipient_ == nullptr) {
57         WS_HILOGE("Failed to create WorkScheduelrDeathRecipient!");
58         return E_CLIENT_CONNECT_SERVICE_FAILED;
59     }
60     if ((remoteObject_->IsProxyObject()) && (!remoteObject_->AddDeathRecipient(deathRecipient_))) {
61         WS_HILOGE("Add death recipient to WorkSchedulerService failed!");
62         return E_CLIENT_CONNECT_SERVICE_FAILED;
63     }
64     iWorkSchedService_ = iface_cast<IWorkSchedService>(remoteObject_);
65     WS_HILOGD("Connecting WorkSchedService success.");
66     return ERR_OK;
67 }
68 
ResetProxy(const wptr<IRemoteObject> & remote)69 void WorkSchedulerSrvClient::ResetProxy(const wptr<IRemoteObject>& remote)
70 {
71     std::lock_guard<std::mutex> lock(mutex_);
72     if (iWorkSchedService_ == nullptr) {
73         return;
74     }
75     auto serviceRemote = iWorkSchedService_->AsObject();
76     if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
77         serviceRemote->RemoveDeathRecipient(deathRecipient_);
78         iWorkSchedService_ = nullptr;
79     }
80 }
81 
OnRemoteDied(const wptr<IRemoteObject> & remote)82 void WorkSchedulerSrvClient::WorkSchedulerDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
83 {
84     if (remote == nullptr) {
85         WS_HILOGE("failed, remote is nullptr");
86         return;
87     }
88     WorkSchedulerSrvClient::GetInstance().ResetProxy(remote);
89     WS_HILOGD("Work Scheduler Death Recipient Recv death notice.");
90 }
91 
StartWork(WorkInfo & workInfo)92 ErrCode WorkSchedulerSrvClient::StartWork(WorkInfo& workInfo)
93 {
94     WS_HILOGD("Start Work");
95     ErrCode ret = Connect();
96     if (ret != ERR_OK) {
97         WS_HILOGE("Connect() failed, errno: %{public}d", ret);
98         return ret;
99     }
100     return iWorkSchedService_->StartWork(workInfo);
101 }
102 
StopWork(WorkInfo & workInfo)103 ErrCode WorkSchedulerSrvClient::StopWork(WorkInfo& workInfo)
104 {
105     WS_HILOGD("Stop Work");
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 
StopAndCancelWork(WorkInfo & workInfo)114 ErrCode WorkSchedulerSrvClient::StopAndCancelWork(WorkInfo& workInfo)
115 {
116     WS_HILOGD("Stop And Cancel Work");
117     ErrCode ret = Connect();
118     if (ret != ERR_OK) {
119         WS_HILOGE("Connect() failed, errno: %{public}d", ret);
120         return ret;
121     }
122     return iWorkSchedService_->StopAndCancelWork(workInfo);
123 }
124 
StopAndClearWorks()125 ErrCode WorkSchedulerSrvClient::StopAndClearWorks()
126 {
127     WS_HILOGD("Stop And Clear Works");
128     ErrCode ret = Connect();
129     if (ret != ERR_OK) {
130         WS_HILOGE("Connect() failed, errno: %{public}d", ret);
131         return ret;
132     }
133     return iWorkSchedService_->StopAndClearWorks();
134 }
135 
IsLastWorkTimeout(int32_t workId,bool & result)136 ErrCode WorkSchedulerSrvClient::IsLastWorkTimeout(int32_t workId, bool &result)
137 {
138     WS_HILOGD("Is LastWork Timeout");
139     ErrCode errCode = Connect();
140     if (errCode != ERR_OK) {
141         return errCode;
142     }
143     errCode = iWorkSchedService_->IsLastWorkTimeout(workId, result);
144     return errCode;
145 }
146 
ObtainAllWorks(std::list<std::shared_ptr<WorkInfo>> & workInfos)147 ErrCode WorkSchedulerSrvClient::ObtainAllWorks(std::list<std::shared_ptr<WorkInfo>> &workInfos)
148 {
149     WS_HILOGD("Obtain All Works");
150     ErrCode errCode = Connect();
151     if (errCode != ERR_OK) {
152         return errCode;
153     }
154     int32_t uid = IPCSkeleton::GetCallingUid();
155     int32_t pid = IPCSkeleton::GetCallingPid();
156     return iWorkSchedService_->ObtainAllWorks(uid, pid, workInfos);
157 }
158 
GetWorkStatus(int32_t workId,std::shared_ptr<WorkInfo> & workInfo)159 ErrCode WorkSchedulerSrvClient::GetWorkStatus(int32_t workId, std::shared_ptr<WorkInfo> &workInfo)
160 {
161     WS_HILOGD("Get Work Status");
162     if (workId <= 0) {
163         return E_WORKID_ERR;
164     }
165     ErrCode code = Connect();
166     if (code != ERR_OK) {
167         return code;
168     }
169     int32_t uid = IPCSkeleton::GetCallingUid();
170     return iWorkSchedService_->GetWorkStatus(uid, workId, workInfo);
171 }
172 } // namespace WorkScheduler
173 } // namespace OHOS