• 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 "work_sched_service_stub.h"
16 
17 #include <message_parcel.h>
18 #include <string_ex.h>
19 
20 #include "iwork_sched_service_ipc_interface_code.h"
21 #include "work_sched_common.h"
22 #include "work_sched_errors.h"
23 
24 namespace OHOS {
25 namespace WorkScheduler {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)26 int32_t WorkSchedServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
27     MessageOption &option)
28 {
29     WS_HILOGD("cmd = %{public}u, flags = %{public}d", code, option.GetFlags());
30     std::u16string descriptor = WorkSchedServiceStub::GetDescriptor();
31     std::u16string remoteDescriptor = data.ReadInterfaceToken();
32     if (descriptor != remoteDescriptor) {
33         WS_HILOGE("failed, descriptor is not matched!");
34         return E_PARCEL_OPERATION_FAILED;
35     }
36     switch (code) {
37         case static_cast<int32_t>(IWorkSchedServiceInterfaceCode::START_WORK): {
38             int32_t ret = StartWorkStub(data);
39             reply.WriteInt32(ret);
40             return ret;
41         }
42         case static_cast<int32_t>(IWorkSchedServiceInterfaceCode::STOP_WORK): {
43             int32_t ret = StopWorkStub(data);
44             reply.WriteInt32(ret);
45             return ret;
46         }
47         case static_cast<int32_t>(IWorkSchedServiceInterfaceCode::STOP_AND_CANCEL_WORK): {
48             int32_t ret = StopAndCancelWorkStub(data);
49             reply.WriteInt32(ret);
50             return ret;
51         }
52         case static_cast<int32_t>(IWorkSchedServiceInterfaceCode::STOP_AND_CLEAR_WORKS): {
53             int32_t ret = StopAndClearWorksStub(data);
54             reply.WriteInt32(ret);
55             return ret;
56         }
57         case static_cast<int32_t>(IWorkSchedServiceInterfaceCode::IS_LAST_WORK_TIMEOUT): {
58             bool isLastWorkTimeout;
59             int32_t ret = IsLastWorkTimeoutStub(data, isLastWorkTimeout);
60             reply.WriteInt32(ret);
61             reply.WriteBool(isLastWorkTimeout);
62             return ret;
63         }
64         case static_cast<int32_t>(IWorkSchedServiceInterfaceCode::OBTAIN_ALL_WORKS): {
65             std::list<std::shared_ptr<WorkInfo>> workInfos;
66             int32_t ret = ObtainAllWorksStub(data, workInfos);
67             uint32_t worksize = workInfos.size();
68             reply.WriteInt32(ret);
69             reply.WriteInt32(worksize);
70             for (auto workInfo : workInfos) {
71                 reply.WriteParcelable(&*workInfo);
72             }
73             return ERR_OK;
74         }
75         case static_cast<int32_t>(IWorkSchedServiceInterfaceCode::GET_WORK_STATUS): {
76             WS_HILOGI("call GetWorkStatus");
77             std::shared_ptr<WorkInfo> workInfo;
78             int32_t ret = GetWorkStatusStub(data, workInfo);
79             reply.WriteInt32(ret);
80             reply.WriteParcelable(&*workInfo);
81             return ERR_OK;
82         }
83         case static_cast<int32_t>(IWorkSchedServiceInterfaceCode::GET_ALL_RUNNING_WORKS): {
84             std::list<std::shared_ptr<WorkInfo>> workInfos;
85             int32_t ret = GetAllRunningWorksStub(workInfos);
86             uint32_t worksize = workInfos.size();
87             reply.WriteInt32(ret);
88             reply.WriteInt32(worksize);
89             for (auto workInfo : workInfos) {
90                 reply.WriteParcelable(&*workInfo);
91             }
92             return ERR_OK;
93         }
94         default: {
95             WS_HILOGD("OnRemoteRequest switch default, code: %{public}u", code);
96             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
97         }
98     }
99     return ERR_OK;
100 }
101 
StartWorkStub(MessageParcel & data)102 int32_t WorkSchedServiceStub::StartWorkStub(MessageParcel& data)
103 {
104     sptr<WorkInfo> workInfo = data.ReadStrongParcelable<WorkInfo>();
105     if (workInfo == nullptr) {
106         WS_HILOGD("workInfo is nullptr");
107         return E_PARCEL_OPERATION_FAILED;
108     }
109     return StartWork(*workInfo);
110 }
111 
StopWorkStub(MessageParcel & data)112 int32_t WorkSchedServiceStub::StopWorkStub(MessageParcel& data)
113 {
114     sptr<WorkInfo> workInfo = data.ReadStrongParcelable<WorkInfo>();
115     if (workInfo == nullptr) {
116         WS_HILOGD("workInfo is nullptr");
117         return E_PARCEL_OPERATION_FAILED;
118     }
119     return StopWork(*workInfo);
120 }
121 
StopAndCancelWorkStub(MessageParcel & data)122 int32_t WorkSchedServiceStub::StopAndCancelWorkStub(MessageParcel& data)
123 {
124     sptr<WorkInfo> workInfo = data.ReadStrongParcelable<WorkInfo>();
125     if (workInfo == nullptr) {
126         WS_HILOGD("workInfo is nullptr");
127         return E_PARCEL_OPERATION_FAILED;
128     }
129     return StopAndCancelWork(*workInfo);
130 }
131 
StopAndClearWorksStub(MessageParcel & data)132 int32_t WorkSchedServiceStub::StopAndClearWorksStub(MessageParcel& data)
133 {
134     return StopAndClearWorks();
135 }
136 
IsLastWorkTimeoutStub(MessageParcel & data,bool & result)137 int32_t WorkSchedServiceStub::IsLastWorkTimeoutStub(MessageParcel& data, bool &result)
138 {
139     int32_t workId = data.ReadInt32();
140     return IsLastWorkTimeout(workId, result);
141 }
142 
ObtainAllWorksStub(MessageParcel & data,std::list<std::shared_ptr<WorkInfo>> & workInfos)143 int32_t WorkSchedServiceStub::ObtainAllWorksStub(MessageParcel& data, std::list<std::shared_ptr<WorkInfo>>& workInfos)
144 {
145     int32_t pid = 0;
146     int32_t uid = 0;
147     READ_PARCEL_WITHOUT_RET(data, Int32, uid);
148     READ_PARCEL_WITHOUT_RET(data, Int32, pid);
149     return ObtainAllWorks(uid, pid, workInfos);
150 }
151 
GetWorkStatusStub(MessageParcel & data,std::shared_ptr<WorkInfo> & workInfo)152 int32_t WorkSchedServiceStub::GetWorkStatusStub(MessageParcel& data, std::shared_ptr<WorkInfo>& workInfo)
153 {
154     int32_t uid;
155     int32_t workId;
156     READ_PARCEL_WITHOUT_RET(data, Int32, uid);
157     READ_PARCEL_WITHOUT_RET(data, Int32, workId);
158     return GetWorkStatus(uid, workId, workInfo);
159 }
160 
GetAllRunningWorksStub(std::list<std::shared_ptr<WorkInfo>> & workInfos)161 int32_t WorkSchedServiceStub::GetAllRunningWorksStub(std::list<std::shared_ptr<WorkInfo>>& workInfos)
162 {
163     return GetAllRunningWorks(workInfos);
164 }
165 } // namespace WorkScheduler
166 } // namespace OHOS