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