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
16 #include "background_task_subscriber_stub.h"
17
18 #include <errors.h>
19 #include <ipc_skeleton.h>
20
21 #include "transient_task_log.h"
22 #include "background_task_subscriber_proxy.h"
23 #include "ibackground_task_subscriber_ipc_interface_code.h"
24
25 namespace OHOS {
26 namespace BackgroundTaskMgr {
27 namespace {
28 constexpr int32_t APP_FIRST_UID = 10000;
29 }
30
BackgroundTaskSubscriberStub()31 BackgroundTaskSubscriberStub::BackgroundTaskSubscriberStub() {}
~BackgroundTaskSubscriberStub()32 BackgroundTaskSubscriberStub::~BackgroundTaskSubscriberStub() {}
33
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)34 ErrCode BackgroundTaskSubscriberStub::OnRemoteRequest(uint32_t code,
35 MessageParcel& data, MessageParcel& reply, MessageOption& option)
36 {
37 std::u16string descriptor = BackgroundTaskSubscriberStub::GetDescriptor();
38 std::u16string remoteDescriptor = data.ReadInterfaceToken();
39 if (descriptor != remoteDescriptor) {
40 BGTASK_LOGE("BackgroundTaskSubscriberStub Local descriptor not match remote.");
41 return ERR_TRANSACTION_FAILED;
42 }
43
44 auto uid = IPCSkeleton::GetCallingUid();
45 if (uid >= APP_FIRST_UID) {
46 BGTASK_LOGE("BackgroundTaskSubscriberStub OnRemoteRequest failed, illegal calling uid %d.", uid);
47 return ERR_INVALID_DATA;
48 }
49
50 return OnRemoteRequestInner(code, data, reply, option);
51 }
52
OnRemoteRequestInner(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)53 ErrCode BackgroundTaskSubscriberStub::OnRemoteRequestInner(uint32_t code,
54 MessageParcel& data, MessageParcel& reply, MessageOption& option)
55 {
56 switch (code) {
57 case static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_CONNECTED): {
58 return HandleOnConnected();
59 }
60 case static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_DISCONNECTED): {
61 return HandleOnDisconnected();
62 }
63 case static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_TRANSIENT_TASK_START): {
64 return HandleOnTransientTaskStart(data);
65 }
66 case static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_TRANSIENT_TASK_END): {
67 return HandleOnTransientTaskEnd(data);
68 }
69 case static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_APP_TRANSIENT_TASK_START): {
70 return HandleOnAppTransientTaskStart(data);
71 }
72 case static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_APP_TRANSIENT_TASK_END): {
73 return HandleOnAppTransientTaskEnd(data);
74 }
75 case static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_CONTINUOUS_TASK_START): {
76 return HandleOnContinuousTaskStart(data);
77 }
78 case static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_CONTINUOUS_TASK_STOP): {
79 return HandleOnContinuousTaskCancel(data);
80 }
81 case static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_APP_CONTINUOUS_TASK_STOP): {
82 return HandleOnAppContinuousTaskStop(data);
83 }
84 case static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_APP_EFFICIENCY_RESOURCES_APPLY): {
85 return HandleOnAppEfficiencyResourcesApply(data);
86 }
87 case static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_APP_EFFICIENCY_RESOURCES_RESET): {
88 return HandleOnAppEfficiencyResourcesReset(data);
89 }
90 case static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_PROC_EFFICIENCY_RESOURCES_APPLY): {
91 return HandleOnProcEfficiencyResourcesApply(data);
92 }
93 case static_cast<uint32_t>(IBackgroundTaskSubscriberInterfaceCode::ON_PROC_EFFICIENCY_RESOURCES_RESET): {
94 return HandleOnProcEfficiencyResourcesReset(data);
95 }
96 default:
97 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
98 }
99 return ERR_OK;
100 }
101
HandleOnConnected()102 ErrCode BackgroundTaskSubscriberStub::HandleOnConnected()
103 {
104 OnConnected();
105 return ERR_OK;
106 }
107
HandleOnDisconnected()108 ErrCode BackgroundTaskSubscriberStub::HandleOnDisconnected()
109 {
110 OnDisconnected();
111 return ERR_OK;
112 }
113
HandleOnTransientTaskStart(MessageParcel & data)114 ErrCode BackgroundTaskSubscriberStub::HandleOnTransientTaskStart(MessageParcel& data)
115 {
116 auto info = TransientTaskAppInfo::Unmarshalling(data);
117 if (info == nullptr) {
118 BGTASK_LOGE("HandleOnTransientTaskStart Read parcel failed.");
119 return ERR_INVALID_DATA;
120 }
121 OnTransientTaskStart(info);
122 return ERR_NONE;
123 }
124
HandleOnTransientTaskEnd(MessageParcel & data)125 ErrCode BackgroundTaskSubscriberStub::HandleOnTransientTaskEnd(MessageParcel& data)
126 {
127 auto info = TransientTaskAppInfo::Unmarshalling(data);
128 if (info == nullptr) {
129 BGTASK_LOGE("HandleOnTransientTaskEnd Read parcel failed.");
130 return ERR_INVALID_DATA;
131 }
132 OnTransientTaskEnd(info);
133 return ERR_NONE;
134 }
135
HandleOnAppTransientTaskStart(MessageParcel & data)136 ErrCode BackgroundTaskSubscriberStub::HandleOnAppTransientTaskStart(MessageParcel& data)
137 {
138 auto info = TransientTaskAppInfo::Unmarshalling(data);
139 if (info == nullptr) {
140 BGTASK_LOGE("HandleOnAppTransientTaskStart Read parcel failed.");
141 return ERR_INVALID_DATA;
142 }
143 OnAppTransientTaskStart(info);
144 return ERR_NONE;
145 }
146
HandleOnAppTransientTaskEnd(MessageParcel & data)147 ErrCode BackgroundTaskSubscriberStub::HandleOnAppTransientTaskEnd(MessageParcel& data)
148 {
149 auto info = TransientTaskAppInfo::Unmarshalling(data);
150 if (info == nullptr) {
151 BGTASK_LOGE("HandleOnAppTransientTaskEnd Read parcel failed.");
152 return ERR_INVALID_DATA;
153 }
154 OnAppTransientTaskEnd(info);
155 return ERR_NONE;
156 }
157
HandleOnContinuousTaskStart(MessageParcel & data)158 ErrCode BackgroundTaskSubscriberStub::HandleOnContinuousTaskStart(MessageParcel &data)
159 {
160 std::shared_ptr<ContinuousTaskCallbackInfo> continuousTaskCallbackInfo
161 = std::shared_ptr<ContinuousTaskCallbackInfo>(data.ReadParcelable<ContinuousTaskCallbackInfo>());
162 if (!continuousTaskCallbackInfo) {
163 BGTASK_LOGE("HandleOnContinuousTaskStart ContinuousTaskCallbackInfo ReadParcelable failed");
164 return ERR_BGTASK_PARCELABLE_FAILED;
165 }
166
167 OnContinuousTaskStart(continuousTaskCallbackInfo);
168 return ERR_OK;
169 }
170
HandleOnContinuousTaskCancel(MessageParcel & data)171 ErrCode BackgroundTaskSubscriberStub::HandleOnContinuousTaskCancel(MessageParcel &data)
172 {
173 std::shared_ptr<ContinuousTaskCallbackInfo> continuousTaskCallbackInfo
174 = std::shared_ptr<ContinuousTaskCallbackInfo>(data.ReadParcelable<ContinuousTaskCallbackInfo>());
175 if (!continuousTaskCallbackInfo) {
176 BGTASK_LOGE("HandleOnContinuousTaskCancel ContinuousTaskCallbackInfo ReadParcelable failed");
177 return ERR_BGTASK_PARCELABLE_FAILED;
178 }
179
180 OnContinuousTaskStop(continuousTaskCallbackInfo);
181 return ERR_OK;
182 }
183
HandleOnAppContinuousTaskStop(MessageParcel & data)184 ErrCode BackgroundTaskSubscriberStub::HandleOnAppContinuousTaskStop(MessageParcel &data)
185 {
186 int32_t uid;
187 if (!data.ReadInt32(uid)) {
188 BGTASK_LOGE("HandleOnAppContinuousTaskStop read uid failed");
189 return ERR_BGTASK_PARCELABLE_FAILED;
190 }
191
192 OnAppContinuousTaskStop(uid);
193 return ERR_OK;
194 }
195
HandleOnAppEfficiencyResourcesApply(MessageParcel & data)196 ErrCode BackgroundTaskSubscriberStub::HandleOnAppEfficiencyResourcesApply(MessageParcel &data)
197 {
198 std::shared_ptr<ResourceCallbackInfo> resourceCallbackInfo
199 = std::shared_ptr<ResourceCallbackInfo>(data.ReadParcelable<ResourceCallbackInfo>());
200 if (!resourceCallbackInfo) {
201 BGTASK_LOGE("HandleOnAppEfficiencyResourcesApply ReadParcelable failed");
202 return ERR_BGTASK_PARCELABLE_FAILED;
203 }
204 OnAppEfficiencyResourcesApply(resourceCallbackInfo);
205 return ERR_OK;
206 }
207
HandleOnAppEfficiencyResourcesReset(MessageParcel & data)208 ErrCode BackgroundTaskSubscriberStub::HandleOnAppEfficiencyResourcesReset(MessageParcel &data)
209 {
210 std::shared_ptr<ResourceCallbackInfo> resourceCallbackInfo
211 = std::shared_ptr<ResourceCallbackInfo>(data.ReadParcelable<ResourceCallbackInfo>());
212 if (!resourceCallbackInfo) {
213 BGTASK_LOGE("HandleOnAppEfficiencyResourcesReset ReadParcelable failed");
214 return ERR_BGTASK_PARCELABLE_FAILED;
215 }
216 OnAppEfficiencyResourcesReset(resourceCallbackInfo);
217 return ERR_OK;
218 }
219
HandleOnProcEfficiencyResourcesApply(MessageParcel & data)220 ErrCode BackgroundTaskSubscriberStub::HandleOnProcEfficiencyResourcesApply(MessageParcel &data)
221 {
222 std::shared_ptr<ResourceCallbackInfo> resourceCallbackInfo
223 = std::shared_ptr<ResourceCallbackInfo>(data.ReadParcelable<ResourceCallbackInfo>());
224 if (!resourceCallbackInfo) {
225 BGTASK_LOGE("HandleOnProcEfficiencyResourcesApply ContinuousTaskCallbackInfo ReadParcelable failed");
226 return ERR_BGTASK_PARCELABLE_FAILED;
227 }
228 OnProcEfficiencyResourcesApply(resourceCallbackInfo);
229 return ERR_OK;
230 }
231
HandleOnProcEfficiencyResourcesReset(MessageParcel & data)232 ErrCode BackgroundTaskSubscriberStub::HandleOnProcEfficiencyResourcesReset(MessageParcel &data)
233 {
234 std::shared_ptr<ResourceCallbackInfo> resourceCallbackInfo
235 = std::shared_ptr<ResourceCallbackInfo>(data.ReadParcelable<ResourceCallbackInfo>());
236 if (!resourceCallbackInfo) {
237 BGTASK_LOGE("HandleOnProcEfficiencyResourcesReset ReadParcelable failed");
238 return ERR_BGTASK_PARCELABLE_FAILED;
239 }
240 OnProcEfficiencyResourcesReset(resourceCallbackInfo);
241 return ERR_OK;
242 }
243 } // namespace BackgroundTaskMgr
244 } // namespace OHOS