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_mgr_stub.h"
17
18 #include <ipc_skeleton.h>
19 #include <string_ex.h>
20
21 #include "hitrace_meter.h"
22 #include "bgtaskmgr_inner_errors.h"
23 #include "bgtaskmgr_log_wrapper.h"
24 #include "delay_suspend_info.h"
25
26 using namespace std;
27
28 namespace OHOS {
29 namespace BackgroundTaskMgr {
30 const std::map<uint32_t, std::function<ErrCode(BackgroundTaskMgrStub *, MessageParcel &, MessageParcel &)>>
31 BackgroundTaskMgrStub::interfaces_ = {
32 {BackgroundTaskMgrStub::REQUEST_SUSPEND_DELAY,
33 std::bind(&BackgroundTaskMgrStub::HandleRequestSuspendDelay,
34 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)},
35 {BackgroundTaskMgrStub::CANCEL_SUSPEND_DELAY,
36 std::bind(&BackgroundTaskMgrStub::HandleCancelSuspendDelay,
37 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)},
38 {BackgroundTaskMgrStub::GET_REMAINING_DELAY_TIME,
39 std::bind(&BackgroundTaskMgrStub::HandleGetRemainingDelayTime,
40 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)},
41 {BackgroundTaskMgrStub::START_BACKGROUND_RUNNING,
42 std::bind(&BackgroundTaskMgrStub::HandleStartBackgroundRunning,
43 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)},
44 {BackgroundTaskMgrStub::STOP_BACKGROUND_RUNNING,
45 std::bind(&BackgroundTaskMgrStub::HandleStopBackgroundRunning,
46 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)},
47 {BackgroundTaskMgrStub::SUBSCRIBE_BACKGROUND_TASK,
48 std::bind(&BackgroundTaskMgrStub::HandleSubscribeBackgroundTask,
49 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)},
50 {BackgroundTaskMgrStub::UNSUBSCRIBE_BACKGROUND_TASK,
51 std::bind(&BackgroundTaskMgrStub::HandleUnsubscribeBackgroundTask,
52 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)},
53 {BackgroundTaskMgrStub::GET_TRANSIENT_TASK_APPS,
54 std::bind(&BackgroundTaskMgrStub::HandleGetTransientTaskApps,
55 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)},
56 {BackgroundTaskMgrStub::GET_CONTINUOUS_TASK_APPS,
57 std::bind(&BackgroundTaskMgrStub::HandleGetContinuousTaskApps,
58 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)},
59 {BackgroundTaskMgrStub::APPLY_EFFICIENCY_RESOURCES,
60 std::bind(&BackgroundTaskMgrStub::HandleApplyEfficiencyResources,
61 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)},
62 {BackgroundTaskMgrStub::RESET_ALL_EFFICIENCY_RESOURCES,
63 std::bind(&BackgroundTaskMgrStub::HandleResetAllEfficiencyResources,
64 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)},
65 {BackgroundTaskMgrStub::GET_EFFICIENCY_RESOURCES_INFOS,
66 std::bind(&BackgroundTaskMgrStub::HandleGetEfficiencyResourcesInfos,
67 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)},
68 {BackgroundTaskMgrStub::STOP_CONTINUOUS_TASK,
69 std::bind(&BackgroundTaskMgrStub::HandleStopContinuousTask,
70 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)},
71 };
72
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)73 ErrCode BackgroundTaskMgrStub::OnRemoteRequest(uint32_t code,
74 MessageParcel& data, MessageParcel& reply, MessageOption& option)
75 {
76 std::u16string descriptor = BackgroundTaskMgrStub::GetDescriptor();
77 std::u16string remoteDescriptor = data.ReadInterfaceToken();
78 if (descriptor != remoteDescriptor) {
79 BGTASK_LOGE("BackgroundTaskMgrStub: Local descriptor not match remote.");
80 return ERR_TRANSACTION_FAILED;
81 }
82 auto it = interfaces_.find(code);
83 if (it == interfaces_.end()) {
84 return IRemoteStub<IBackgroundTaskMgr>::OnRemoteRequest(code, data, reply, option);
85 }
86
87 auto fun = it->second;
88 if (fun == nullptr) {
89 return IRemoteStub<IBackgroundTaskMgr>::OnRemoteRequest(code, data, reply, option);
90 }
91
92 ErrCode result = fun(this, data, reply);
93 if (SUCCEEDED(result)) {
94 return ERR_OK;
95 }
96
97 BGTASK_LOGE("BackgroundTaskMgrStub: Failed to call interface %{public}u, err:%{public}d", code, result);
98 return result;
99 }
100
HandleRequestSuspendDelay(MessageParcel & data,MessageParcel & reply)101 ErrCode BackgroundTaskMgrStub::HandleRequestSuspendDelay(MessageParcel& data, MessageParcel& reply)
102 {
103 std::u16string reason = data.ReadString16();
104 sptr<IRemoteObject> callback = data.ReadRemoteObject();
105 if (callback == nullptr) {
106 BGTASK_LOGE("HandleRequestSuspendDelay Read callback fail.");
107 return ERR_BGTASK_PARCELABLE_FAILED;
108 }
109
110 std::shared_ptr<DelaySuspendInfo> info;
111 ErrCode result = RequestSuspendDelay(reason, iface_cast<IExpiredCallback>(callback), info);
112 if (!reply.WriteInt32(result)) {
113 BGTASK_LOGE("HandleRequestSuspendDelay Write result failed, ErrCode=%{public}d", result);
114 return ERR_BGTASK_PARCELABLE_FAILED;
115 }
116
117 if (info == nullptr || !info->Marshalling(reply)) {
118 BGTASK_LOGE("HandleRequestSuspendDelay Write result fail.");
119 return ERR_BGTASK_PARCELABLE_FAILED;
120 }
121 return ERR_OK;
122 }
123
HandleCancelSuspendDelay(MessageParcel & data,MessageParcel & reply)124 ErrCode BackgroundTaskMgrStub::HandleCancelSuspendDelay(MessageParcel& data, MessageParcel& reply)
125 {
126 int32_t id = data.ReadInt32();
127 ErrCode result = CancelSuspendDelay(id);
128 if (!reply.WriteInt32(result)) {
129 BGTASK_LOGE("HandleCancelSuspendDelay Write result failed, ErrCode=%{public}d", result);
130 return ERR_BGTASK_PARCELABLE_FAILED;
131 }
132 return ERR_OK;
133 }
134
HandleGetRemainingDelayTime(MessageParcel & data,MessageParcel & reply)135 ErrCode BackgroundTaskMgrStub::HandleGetRemainingDelayTime(MessageParcel& data, MessageParcel& reply)
136 {
137 int32_t id = data.ReadInt32();
138 int32_t time = 0;
139 ErrCode result = GetRemainingDelayTime(id, time);
140 if (!reply.WriteInt32(result)) {
141 BGTASK_LOGE("HandleGetRemainingDelayTime Write result failed, ErrCode=%{public}d", result);
142 return ERR_BGTASK_PARCELABLE_FAILED;
143 }
144 if (!reply.WriteInt32(time)) {
145 BGTASK_LOGE("HandleGetRemainingDelayTime Write result fail.");
146 return ERR_BGTASK_PARCELABLE_FAILED;
147 }
148 return ERR_OK;
149 }
150
HandleStartBackgroundRunning(MessageParcel & data,MessageParcel & reply)151 ErrCode BackgroundTaskMgrStub::HandleStartBackgroundRunning(MessageParcel &data, MessageParcel &reply)
152 {
153 StartTrace(HITRACE_TAG_OHOS, "BackgroundTaskMgrStub::HandleStartBackgroundRunning");
154 sptr<ContinuousTaskParam> taskParam = data.ReadParcelable<ContinuousTaskParam>();
155 if (taskParam == nullptr) {
156 BGTASK_LOGE("ContinuousTaskParam ReadParcelable failed");
157 FinishTrace(HITRACE_TAG_OHOS);
158 return ERR_BGTASK_PARCELABLE_FAILED;
159 }
160 ErrCode result = StartBackgroundRunning(taskParam);
161 if (!reply.WriteInt32(result)) {
162 BGTASK_LOGE("HandleStopBackgroundRunning write result failed, ErrCode=%{public}d", result);
163 FinishTrace(HITRACE_TAG_OHOS);
164 return ERR_BGTASK_PARCELABLE_FAILED;
165 }
166 FinishTrace(HITRACE_TAG_OHOS);
167 return ERR_OK;
168 }
169
HandleStopBackgroundRunning(MessageParcel & data,MessageParcel & reply)170 ErrCode BackgroundTaskMgrStub::HandleStopBackgroundRunning(MessageParcel &data, MessageParcel &reply)
171 {
172 StartTrace(HITRACE_TAG_OHOS, "BackgroundTaskMgrStub::HandleStopBackgroundRunning");
173 std::u16string u16AbilityName;
174 if (!data.ReadString16(u16AbilityName)) {
175 FinishTrace(HITRACE_TAG_OHOS);
176 return ERR_BGTASK_PARCELABLE_FAILED;
177 }
178 std::string abilityName = Str16ToStr8(u16AbilityName);
179 sptr<IRemoteObject> abilityToken = data.ReadRemoteObject();
180 ErrCode result = StopBackgroundRunning(abilityName, abilityToken);
181 if (!reply.WriteInt32(result)) {
182 BGTASK_LOGE("HandleStopBackgroundRunning write result failed, ErrCode=%{public}d", result);
183 FinishTrace(HITRACE_TAG_OHOS);
184 return ERR_BGTASK_PARCELABLE_FAILED;
185 }
186 FinishTrace(HITRACE_TAG_OHOS);
187 return ERR_OK;
188 }
189
HandleSubscribeBackgroundTask(MessageParcel & data,MessageParcel & reply)190 ErrCode BackgroundTaskMgrStub::HandleSubscribeBackgroundTask(MessageParcel& data, MessageParcel& reply)
191 {
192 sptr<IRemoteObject> subscriber = data.ReadRemoteObject();
193 if (subscriber == nullptr) {
194 BGTASK_LOGE("HandleSubscribeBackgroundTask Read callback fail.");
195 return ERR_BGTASK_PARCELABLE_FAILED;
196 }
197
198 ErrCode result = SubscribeBackgroundTask(iface_cast<IBackgroundTaskSubscriber>(subscriber));
199 if (!reply.WriteInt32(result)) {
200 BGTASK_LOGE("HandleSubscribeBackgroundTask Write result failed, ErrCode=%{public}d", result);
201 return ERR_BGTASK_PARCELABLE_FAILED;
202 }
203 return ERR_OK;
204 }
205
HandleUnsubscribeBackgroundTask(MessageParcel & data,MessageParcel & reply)206 ErrCode BackgroundTaskMgrStub::HandleUnsubscribeBackgroundTask(MessageParcel& data, MessageParcel& reply)
207 {
208 sptr<IRemoteObject> subscriber = data.ReadRemoteObject();
209 if (subscriber == nullptr) {
210 BGTASK_LOGE("HandleUnsubscribeBackgroundTask Read callback fail.");
211 return ERR_BGTASK_PARCELABLE_FAILED;
212 }
213
214 ErrCode result = UnsubscribeBackgroundTask(iface_cast<IBackgroundTaskSubscriber>(subscriber));
215 if (!reply.WriteInt32(result)) {
216 BGTASK_LOGE("HandleUnsubscribeBackgroundTask Write result failed, ErrCode=%{public}d", result);
217 return ERR_BGTASK_PARCELABLE_FAILED;
218 }
219 return ERR_OK;
220 }
221
HandleGetTransientTaskApps(MessageParcel & data,MessageParcel & reply)222 ErrCode BackgroundTaskMgrStub::HandleGetTransientTaskApps(MessageParcel& data, MessageParcel& reply)
223 {
224 std::vector<std::shared_ptr<TransientTaskAppInfo>> appinfos;
225 ErrCode result = GetTransientTaskApps(appinfos);
226 if (!reply.WriteInt32(result)) {
227 return ERR_BGTASK_PARCELABLE_FAILED;
228 }
229 reply.WriteInt32(appinfos.size());
230 for (auto &info : appinfos) {
231 if (info == nullptr) {
232 return ERR_BGTASK_INVALID_PARAM;
233 }
234 if (!info->Marshalling(reply)) {
235 return ERR_BGTASK_PARCELABLE_FAILED;
236 }
237 }
238 return ERR_OK;
239 }
240
HandleGetContinuousTaskApps(MessageParcel & data,MessageParcel & reply)241 ErrCode BackgroundTaskMgrStub::HandleGetContinuousTaskApps(MessageParcel& data, MessageParcel& reply)
242 {
243 std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> appInfos;
244 ErrCode result = GetContinuousTaskApps(appInfos);
245 if (!reply.WriteInt32(result)) {
246 return ERR_BGTASK_PARCELABLE_FAILED;
247 }
248 reply.WriteInt32(appInfos.size());
249 for (auto &info : appInfos) {
250 if (info == nullptr) {
251 return ERR_BGTASK_INVALID_PARAM;
252 }
253 if (!info->Marshalling(reply)) {
254 return ERR_BGTASK_PARCELABLE_FAILED;
255 }
256 }
257 return ERR_OK;
258 }
259
HandleApplyEfficiencyResources(MessageParcel & data,MessageParcel & reply)260 ErrCode BackgroundTaskMgrStub::HandleApplyEfficiencyResources(MessageParcel& data, MessageParcel& reply)
261 {
262 BGTASK_LOGD("start receive data in apply res function from bgtask proxy");
263 sptr<EfficiencyResourceInfo> resourceInfoPtr = data.ReadParcelable<EfficiencyResourceInfo>();
264 if (resourceInfoPtr == nullptr) {
265 BGTASK_LOGE("EfficiencyResourceInfo ReadParcelable failed");
266 return ERR_BGTASK_PARCELABLE_FAILED;
267 }
268 ErrCode result = ApplyEfficiencyResources(resourceInfoPtr);
269 if (!reply.WriteInt32(result)) {
270 BGTASK_LOGE("HandleApplyEfficiencyResources write result failed, ErrCode=%{public}d", result);
271 return ERR_BGTASK_PARCELABLE_FAILED;
272 }
273 return ERR_OK;
274 }
275
HandleResetAllEfficiencyResources(MessageParcel & data,MessageParcel & reply)276 ErrCode BackgroundTaskMgrStub::HandleResetAllEfficiencyResources(MessageParcel& data, MessageParcel& reply)
277 {
278 BGTASK_LOGD("start receive data in reset all res function from bgtask proxy");
279 ErrCode result = ResetAllEfficiencyResources();
280 if (!reply.WriteInt32(result)) {
281 BGTASK_LOGE("HandleCancelSuspendDelay Write result failed, ErrCode=%{public}d", result);
282 return ERR_BGTASK_PARCELABLE_FAILED;
283 }
284 return ERR_OK;
285 }
286
HandleGetEfficiencyResourcesInfos(MessageParcel & data,MessageParcel & reply)287 ErrCode BackgroundTaskMgrStub::HandleGetEfficiencyResourcesInfos(MessageParcel& data, MessageParcel& reply)
288 {
289 std::vector<std::shared_ptr<ResourceCallbackInfo>> appInfos;
290 std::vector<std::shared_ptr<ResourceCallbackInfo>> procInfos;
291 ErrCode result = GetEfficiencyResourcesInfos(appInfos, procInfos);
292 if (!reply.WriteInt32(result)) {
293 BGTASK_LOGE("HandleGetEfficiencyResourcesInfos write result failed, ErrCode=%{public}d", result);
294 return ERR_BGTASK_PARCELABLE_FAILED;
295 }
296 if (WriteInfoToParcel(appInfos, reply) != ERR_OK
297 || WriteInfoToParcel(procInfos, reply) != ERR_OK) {
298 BGTASK_LOGE("HandleGetEfficiencyResourcesInfos write result failed");
299 return ERR_BGTASK_PARCELABLE_FAILED;
300 }
301 return ERR_OK;
302 }
303
WriteInfoToParcel(const std::vector<std::shared_ptr<ResourceCallbackInfo>> & infoMap,MessageParcel & reply)304 ErrCode BackgroundTaskMgrStub::WriteInfoToParcel(
305 const std::vector<std::shared_ptr<ResourceCallbackInfo>>& infoMap, MessageParcel& reply)
306 {
307 reply.WriteInt32(infoMap.size());
308 for (auto &info : infoMap) {
309 if (info == nullptr) {
310 return ERR_BGTASK_INVALID_PARAM;
311 }
312 if (!info->Marshalling(reply)) {
313 return ERR_BGTASK_PARCELABLE_FAILED;
314 }
315 }
316 return ERR_OK;
317 }
318
HandleStopContinuousTask(MessageParcel & data,MessageParcel & reply)319 ErrCode BackgroundTaskMgrStub::HandleStopContinuousTask(MessageParcel& data, MessageParcel& reply)
320 {
321 int32_t uid = data.ReadInt32();
322 int32_t pid = data.ReadInt32();
323 uint32_t taskType = data.ReadUint32();
324 ErrCode result = StopContinuousTask(uid, pid, taskType);
325 if (!reply.WriteInt32(result)) {
326 BGTASK_LOGE("HandleStopContinuousTask write result failed, ErrCode=%{public}d", result);
327 return ERR_BGTASK_PARCELABLE_FAILED;
328 }
329 return ERR_OK;
330 }
331 } // namespace BackgroundTaskMgr
332 } // namespace OHOS