• 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 <datetime_ex.h>
18 #include <if_system_ability_manager.h>
19 #include <ipc_skeleton.h>
20 #include <iservice_registry.h>
21 #include <string_ex.h>
22 #include <system_ability_definition.h>
23 
24 #include "work_sched_common.h"
25 
26 namespace OHOS {
27 namespace WorkScheduler {
WorkSchedulerSrvClient()28 WorkSchedulerSrvClient::WorkSchedulerSrvClient() {}
29 
~WorkSchedulerSrvClient()30 WorkSchedulerSrvClient::~WorkSchedulerSrvClient()
31 {
32     if (iWorkSchedService_ != nullptr) {
33         auto remoteObject = iWorkSchedService_->AsObject();
34         if (remoteObject != nullptr) {
35             remoteObject->RemoveDeathRecipient(deathRecipient_);
36         }
37     }
38 }
39 
Connect()40 ErrCode WorkSchedulerSrvClient::Connect()
41 {
42     std::lock_guard<std::mutex> lock(mutex_);
43     if (iWorkSchedService_ != nullptr) {
44         return ERR_OK;
45     }
46     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
47     if (sam == nullptr) {
48         WS_HILOGE("GetSystemAbilityManager failed!");
49         return E_CLIENT_CONNECT_SERVICE_FAILED;
50     }
51     sptr<IRemoteObject> remoteObject_ = sam->CheckSystemAbility(WORK_SCHEDULE_SERVICE_ID);
52     if (remoteObject_ == nullptr) {
53         WS_HILOGE("GetSystemAbility failed!");
54         return E_CLIENT_CONNECT_SERVICE_FAILED;
55     }
56     deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new WorkSchedulerDeathRecipient());
57     if (deathRecipient_ == nullptr) {
58         WS_HILOGE("Failed to create WorkScheduelrDeathRecipient!");
59         return E_CLIENT_CONNECT_SERVICE_FAILED;
60     }
61     if ((remoteObject_->IsProxyObject()) && (!remoteObject_->AddDeathRecipient(deathRecipient_))) {
62         WS_HILOGE("Add death recipient to WorkSchedulerService failed!");
63         return E_CLIENT_CONNECT_SERVICE_FAILED;
64     }
65     iWorkSchedService_ = iface_cast<IWorkSchedService>(remoteObject_);
66     WS_HILOGD("Connecting WorkSchedService success.");
67     return ERR_OK;
68 }
69 
ResetProxy(const wptr<IRemoteObject> & remote)70 void WorkSchedulerSrvClient::ResetProxy(const wptr<IRemoteObject>& remote)
71 {
72     std::lock_guard<std::mutex> lock(mutex_);
73     if (iWorkSchedService_ == nullptr) {
74         return;
75     }
76     auto serviceRemote = iWorkSchedService_->AsObject();
77     if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
78         serviceRemote->RemoveDeathRecipient(deathRecipient_);
79         iWorkSchedService_ = nullptr;
80     }
81 }
82 
OnRemoteDied(const wptr<IRemoteObject> & remote)83 void WorkSchedulerSrvClient::WorkSchedulerDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
84 {
85     if (remote == nullptr) {
86         WS_HILOGE("WorkSchedulerDeathRecipient::OnRemoteDied failed, remote is nullptr");
87         return;
88     }
89     WorkSchedulerSrvClient::GetInstance().ResetProxy(remote);
90     WS_HILOGD("WorkSchedulerDeathRecipient::Recv death notice.");
91 }
92 
StartWork(WorkInfo & workInfo)93 bool WorkSchedulerSrvClient::StartWork(WorkInfo& workInfo)
94 {
95     WS_HILOGD("StartWork");
96     if (Connect() != ERR_OK) {
97         WS_HILOGE("%{public}s Connect() failed, errno: %{public}d", __func__, Connect());
98         return false;
99     }
100     return iWorkSchedService_->StartWork(workInfo);
101 }
102 
StopWork(WorkInfo & workInfo)103 bool WorkSchedulerSrvClient::StopWork(WorkInfo& workInfo)
104 {
105     WS_HILOGD("StopWork");
106     if (Connect() != ERR_OK) {
107         WS_HILOGE("%{public}s Connect() failed, errno: %{public}d", __func__, Connect());
108         return false;
109     }
110     return iWorkSchedService_->StopWork(workInfo);
111 }
112 
StopAndCancelWork(WorkInfo & workInfo)113 bool WorkSchedulerSrvClient::StopAndCancelWork(WorkInfo& workInfo)
114 {
115     WS_HILOGD("StopAndCancelWork");
116     if (Connect() != ERR_OK) {
117         WS_HILOGE("%{public}s Connect() failed, errno: %{public}d", __func__, Connect());
118         return false;
119     }
120     return iWorkSchedService_->StopAndCancelWork(workInfo);
121 }
122 
StopAndClearWorks()123 bool WorkSchedulerSrvClient::StopAndClearWorks()
124 {
125     WS_HILOGD("StopAndClearWorks");
126     if (Connect() != ERR_OK) {
127         WS_HILOGE("StopAndClearWorks connect service failed!");
128         return false;
129     }
130     return iWorkSchedService_->StopAndClearWorks();
131 }
132 
IsLastWorkTimeout(int32_t workId,bool & result)133 ErrCode WorkSchedulerSrvClient::IsLastWorkTimeout(int32_t workId, bool &result)
134 {
135     WS_HILOGD("IsLastWorkTimeout");
136     ErrCode errCode = Connect();
137     if (errCode != ERR_OK) {
138         return errCode;
139     }
140     result = iWorkSchedService_->IsLastWorkTimeout(workId);
141     return ERR_OK;
142 }
143 
ObtainAllWorks(std::list<std::shared_ptr<WorkInfo>> & workInfos)144 ErrCode WorkSchedulerSrvClient::ObtainAllWorks(std::list<std::shared_ptr<WorkInfo>> &workInfos)
145 {
146     WS_HILOGD("ObtainAllWorks");
147     ErrCode errCode = Connect();
148     if (errCode != ERR_OK) {
149         return errCode;
150     }
151     int32_t uid = IPCSkeleton::GetCallingUid();
152     int32_t pid = IPCSkeleton::GetCallingPid();
153     workInfos = iWorkSchedService_->ObtainAllWorks(uid, pid);
154     return ERR_OK;
155 }
156 
GetWorkStatus(int32_t workId,std::shared_ptr<WorkInfo> & workInfo)157 ErrCode WorkSchedulerSrvClient::GetWorkStatus(int32_t workId, std::shared_ptr<WorkInfo> &workInfo)
158 {
159     WS_HILOGD("GetWorkStatus");
160     if (workId <= 0) {
161         return E_WORK_ID_INVALID;
162     }
163     ErrCode code = Connect();
164     if (code != ERR_OK) {
165         return code;
166     }
167     int32_t uid = IPCSkeleton::GetCallingUid();
168     workInfo = iWorkSchedService_->GetWorkStatus(uid, workId);
169     if (workInfo == nullptr) {
170         return E_GET_WORK_STATUS_ERROR;
171     }
172     return ERR_OK;
173 }
174 
ShellDump(const std::vector<std::string> & dumpOption,std::vector<std::string> & dumpInfo)175 bool WorkSchedulerSrvClient::ShellDump(const std::vector<std::string> &dumpOption,
176     std::vector<std::string> &dumpInfo)
177 {
178     WS_HILOGD("ShellDump");
179     if (Connect() != ERR_OK) {
180         WS_HILOGE("%{public}s Connect() failed, errno: %{public}d", __func__, Connect());
181         dumpInfo.push_back("Connect() failed, errno:" + std::to_string(Connect()));
182         return false;
183     }
184     iWorkSchedService_->ShellDump(dumpOption, dumpInfo);
185     return true;
186 }
187 } // OHOS
188 } // WorkScheduler