• 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 
16 #include "background_task_mgr_stub.h"
17 
18 #include <ipc_skeleton.h>
19 #include <string_ex.h>
20 
21 #include "bgtaskmgr_inner_errors.h"
22 #include "bgtaskmgr_log_wrapper.h"
23 #include "delay_suspend_info.h"
24 
25 using namespace std;
26 
27 namespace OHOS {
28 namespace BackgroundTaskMgr {
29 const std::map<uint32_t, std::function<ErrCode(BackgroundTaskMgrStub *, MessageParcel &, MessageParcel &)>>
30     BackgroundTaskMgrStub::interfaces_ = {
31         {BackgroundTaskMgrStub::REQUEST_SUSPEND_DELAY,
32             std::bind(&BackgroundTaskMgrStub::HandleRequestSuspendDelay,
33                 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)},
34         {BackgroundTaskMgrStub::CANCEL_SUSPEND_DELAY,
35             std::bind(&BackgroundTaskMgrStub::HandleCancelSuspendDelay,
36                 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)},
37         {BackgroundTaskMgrStub::GET_REMAINING_DELAY_TIME,
38             std::bind(&BackgroundTaskMgrStub::HandleGetRemainingDelayTime,
39                 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)},
40         {BackgroundTaskMgrStub::START_BACKGROUND_RUNNING,
41             std::bind(&BackgroundTaskMgrStub::HandleStartBackgroundRunning,
42                 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)},
43         {BackgroundTaskMgrStub::STOP_BACKGROUND_RUNNING,
44             std::bind(&BackgroundTaskMgrStub::HandleStopBackgroundRunning,
45                 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)},
46         {BackgroundTaskMgrStub::SUBSCRIBE_BACKGROUND_TASK,
47             std::bind(&BackgroundTaskMgrStub::HandleSubscribeBackgroundTask,
48                 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)},
49         {BackgroundTaskMgrStub::UNSUBSCRIBE_BACKGROUND_TASK,
50             std::bind(&BackgroundTaskMgrStub::HandleUnsubscribeBackgroundTask,
51                 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)},
52         {BackgroundTaskMgrStub::SHELL_DUMP,
53             std::bind(&BackgroundTaskMgrStub::HandleShellDump,
54                 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)},
55 };
56 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)57 ErrCode BackgroundTaskMgrStub::OnRemoteRequest(uint32_t code,
58     MessageParcel& data, MessageParcel& reply, MessageOption& option)
59 {
60     std::u16string descriptor = BackgroundTaskMgrStub::GetDescriptor();
61     std::u16string remoteDescriptor = data.ReadInterfaceToken();
62     if (descriptor != remoteDescriptor) {
63         BGTASK_LOGE("Local descriptor not match remote.");
64         return ERR_TRANSACTION_FAILED;
65     }
66     auto it = interfaces_.find(code);
67     if (it == interfaces_.end()) {
68         return IRemoteStub<IBackgroundTaskMgr>::OnRemoteRequest(code, data, reply, option);
69     }
70 
71     auto fun = it->second;
72     if (fun == nullptr) {
73         return IRemoteStub<IBackgroundTaskMgr>::OnRemoteRequest(code, data, reply, option);
74     }
75 
76     ErrCode result = fun(this, data, reply);
77     if (SUCCEEDED(result)) {
78         return ERR_OK;
79     }
80 
81     BGTASK_LOGE("Failed to call interface %{public}u, err:%{public}d", code, result);
82     return result;
83 }
84 
HandleRequestSuspendDelay(MessageParcel & data,MessageParcel & reply)85 ErrCode BackgroundTaskMgrStub::HandleRequestSuspendDelay(MessageParcel& data, MessageParcel& reply)
86 {
87     std::u16string reason = data.ReadString16();
88     sptr<IRemoteObject> callback = data.ReadRemoteObject();
89     if (callback == nullptr) {
90         BGTASK_LOGE("Read callback fail.");
91         return ERR_BGTASK_PARCELABLE_FAILED;
92     }
93 
94     std::shared_ptr<DelaySuspendInfo> info;
95     ErrCode result = RequestSuspendDelay(reason, iface_cast<IExpiredCallback>(callback), info);
96     if (!reply.WriteInt32(result)) {
97         BGTASK_LOGE("Write result failed, ErrCode=%{public}d", result);
98         return ERR_BGTASK_PARCELABLE_FAILED;
99     }
100 
101     if (info == nullptr || !info->Marshalling(reply)) {
102         BGTASK_LOGE("Write result fail.");
103         return ERR_BGTASK_PARCELABLE_FAILED;
104     }
105     return ERR_OK;
106 }
107 
HandleCancelSuspendDelay(MessageParcel & data,MessageParcel & reply)108 ErrCode BackgroundTaskMgrStub::HandleCancelSuspendDelay(MessageParcel& data, MessageParcel& reply)
109 {
110     int32_t id = data.ReadInt32();
111     ErrCode result = CancelSuspendDelay(id);
112     if (!reply.WriteInt32(result)) {
113         BGTASK_LOGE("Write result failed, ErrCode=%{public}d", result);
114         return ERR_BGTASK_PARCELABLE_FAILED;
115     }
116     return ERR_OK;
117 }
118 
HandleGetRemainingDelayTime(MessageParcel & data,MessageParcel & reply)119 ErrCode BackgroundTaskMgrStub::HandleGetRemainingDelayTime(MessageParcel& data, MessageParcel& reply)
120 {
121     int32_t id = data.ReadInt32();
122     int32_t time = 0;
123     ErrCode result =  GetRemainingDelayTime(id, time);
124     if (!reply.WriteInt32(result)) {
125         BGTASK_LOGE("Write result failed, ErrCode=%{public}d", result);
126         return ERR_BGTASK_PARCELABLE_FAILED;
127     }
128     if (!reply.WriteInt32(time)) {
129         BGTASK_LOGE("Write result fail.");
130         return ERR_BGTASK_PARCELABLE_FAILED;
131     }
132     return ERR_OK;
133 }
134 
HandleStartBackgroundRunning(MessageParcel & data,MessageParcel & reply)135 ErrCode BackgroundTaskMgrStub::HandleStartBackgroundRunning(MessageParcel &data, MessageParcel &reply)
136 {
137     sptr<ContinuousTaskParam> taskParam = data.ReadParcelable<ContinuousTaskParam>();
138     if (taskParam == nullptr) {
139         BGTASK_LOGE("ContinuousTaskParam ReadParcelable failed");
140         return ERR_BGTASK_PARCELABLE_FAILED;
141     }
142     ErrCode result = StartBackgroundRunning(taskParam);
143     if (!reply.WriteInt32(result)) {
144         BGTASK_LOGE("write result failed, ErrCode=%{public}d", result);
145         return ERR_BGTASK_PARCELABLE_FAILED;
146     }
147     return ERR_OK;
148 }
149 
HandleStopBackgroundRunning(MessageParcel & data,MessageParcel & reply)150 ErrCode BackgroundTaskMgrStub::HandleStopBackgroundRunning(MessageParcel &data, MessageParcel &reply)
151 {
152     std::string abilityName;
153     if (!data.ReadString(abilityName)) {
154         return ERR_BGTASK_PARCELABLE_FAILED;
155     }
156     sptr<IRemoteObject> abilityToken = data.ReadRemoteObject();
157     ErrCode result = StopBackgroundRunning(abilityName, abilityToken);
158     if (!reply.WriteInt32(result)) {
159         BGTASK_LOGE("write result failed, ErrCode=%{public}d", result);
160         return ERR_BGTASK_PARCELABLE_FAILED;
161     }
162     return ERR_OK;
163 }
164 
HandleSubscribeBackgroundTask(MessageParcel & data,MessageParcel & reply)165 ErrCode BackgroundTaskMgrStub::HandleSubscribeBackgroundTask(MessageParcel& data, MessageParcel& reply)
166 {
167     sptr<IRemoteObject> subscriber = data.ReadRemoteObject();
168     if (subscriber == nullptr) {
169         BGTASK_LOGE("Read callback fail.");
170         return ERR_BGTASK_PARCELABLE_FAILED;
171     }
172 
173     ErrCode result = SubscribeBackgroundTask(iface_cast<IBackgroundTaskSubscriber>(subscriber));
174     if (!reply.WriteInt32(result)) {
175         BGTASK_LOGE("Write result failed, ErrCode=%{public}d", result);
176         return ERR_BGTASK_PARCELABLE_FAILED;
177     }
178     return ERR_OK;
179 }
180 
HandleUnsubscribeBackgroundTask(MessageParcel & data,MessageParcel & reply)181 ErrCode BackgroundTaskMgrStub::HandleUnsubscribeBackgroundTask(MessageParcel& data, MessageParcel& reply)
182 {
183     sptr<IRemoteObject> subscriber = data.ReadRemoteObject();
184     if (subscriber == nullptr) {
185         BGTASK_LOGE("Read callback fail.");
186         return ERR_BGTASK_PARCELABLE_FAILED;
187     }
188 
189     ErrCode result = UnsubscribeBackgroundTask(iface_cast<IBackgroundTaskSubscriber>(subscriber));
190     if (!reply.WriteInt32(result)) {
191         BGTASK_LOGE("Write result failed, ErrCode=%{public}d", result);
192         return ERR_BGTASK_PARCELABLE_FAILED;
193     }
194     return ERR_OK;
195 }
196 
HandleShellDump(MessageParcel & data,MessageParcel & reply)197 ErrCode BackgroundTaskMgrStub::HandleShellDump(MessageParcel& data, MessageParcel& reply)
198 {
199     std::vector<std::string> dumpOption;
200     if (!data.ReadStringVector(&dumpOption)) {
201         BGTASK_LOGE("Read dumpOption failed.");
202         return ERR_BGTASK_PARCELABLE_FAILED;
203     }
204 
205     std::vector<std::string> bgtaskmgrInfo;
206     ErrCode result = ShellDump(dumpOption, bgtaskmgrInfo);
207     if (!reply.WriteInt32(result)) {
208         BGTASK_LOGE("Write result failed, ErrCode=%{public}d", result);
209         return ERR_BGTASK_PARCELABLE_FAILED;
210     }
211     if (!reply.WriteStringVector(bgtaskmgrInfo)) {
212         BGTASK_LOGE("Write bgtaskmgrInfo fail.");
213         return ERR_BGTASK_PARCELABLE_FAILED;
214     }
215     return ERR_OK;
216 }
217 }  // namespace BackgroundTaskMgr
218 }  // namespace OHOS