• 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_proxy.h"
17 
18 #include <message_parcel.h>
19 #include <string_ex.h>
20 
21 #include "bgtaskmgr_log_wrapper.h"
22 
23 using namespace std;
24 
25 namespace OHOS {
26 namespace BackgroundTaskMgr {
BackgroundTaskMgrProxy(const sptr<IRemoteObject> & impl)27 BackgroundTaskMgrProxy::BackgroundTaskMgrProxy(const sptr<IRemoteObject>& impl)
28     :IRemoteProxy<IBackgroundTaskMgr>(impl) {}
~BackgroundTaskMgrProxy()29 BackgroundTaskMgrProxy::~BackgroundTaskMgrProxy() {}
30 
RequestSuspendDelay(const std::u16string & reason,const sptr<IExpiredCallback> & callback,std::shared_ptr<DelaySuspendInfo> & delayInfo)31 ErrCode BackgroundTaskMgrProxy::RequestSuspendDelay(const std::u16string& reason,
32     const sptr<IExpiredCallback>& callback, std::shared_ptr<DelaySuspendInfo> &delayInfo)
33 {
34     if (callback == nullptr) {
35         BGTASK_LOGE("callback is null");
36         return ERR_BGTASK_INVALID_PARAM;
37     }
38 
39     MessageParcel data;
40     MessageParcel reply;
41     MessageOption option = {MessageOption::TF_SYNC};
42     if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
43         BGTASK_LOGE("write descriptor failed");
44         return ERR_BGTASK_PARCELABLE_FAILED;
45     }
46     if (!data.WriteString16(reason)) {
47         BGTASK_LOGE("write reason failed");
48         return ERR_BGTASK_PARCELABLE_FAILED;
49     }
50     if (!data.WriteRemoteObject(callback->AsObject())) {
51         BGTASK_LOGE("write callback failed");
52         return ERR_BGTASK_PARCELABLE_FAILED;
53     }
54 
55     ErrCode result = InnerTransact(REQUEST_SUSPEND_DELAY, option, data, reply);
56     if (result != ERR_OK) {
57         BGTASK_LOGE("transact ErrCode=%{public}d", result);
58         return ERR_BGTASK_TRANSACT_FAILED;
59     }
60     if (!reply.ReadInt32(result)) {
61         BGTASK_LOGE("fail: read result failed.");
62         return ERR_BGTASK_PARCELABLE_FAILED;
63     }
64     delayInfo = DelaySuspendInfo::Unmarshalling(reply);
65     if (delayInfo == nullptr) {
66         BGTASK_LOGE("read result failed");
67         return ERR_BGTASK_PARCELABLE_FAILED;
68     }
69     return result;
70 }
71 
CancelSuspendDelay(int32_t requestId)72 ErrCode BackgroundTaskMgrProxy::CancelSuspendDelay(int32_t requestId)
73 {
74     MessageParcel data;
75     MessageParcel reply;
76     MessageOption option = {MessageOption::TF_SYNC};
77     if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
78         BGTASK_LOGE("write descriptor failed");
79         return ERR_BGTASK_PARCELABLE_FAILED;
80     }
81     if (!data.WriteInt32(requestId)) {
82         BGTASK_LOGE("write requestId failed");
83         return ERR_BGTASK_PARCELABLE_FAILED;
84     }
85 
86     ErrCode result = InnerTransact(CANCEL_SUSPEND_DELAY, option, data, reply);
87     if (result != ERR_OK) {
88         BGTASK_LOGE("fail: transact ErrCode=%{public}d", result);
89         return ERR_BGTASK_TRANSACT_FAILED;
90     }
91     if (!reply.ReadInt32(result)) {
92         BGTASK_LOGE("fail: read result failed.");
93         return ERR_BGTASK_PARCELABLE_FAILED;
94     }
95     return result;
96 }
97 
GetRemainingDelayTime(int32_t requestId,int32_t & delayTime)98 ErrCode BackgroundTaskMgrProxy::GetRemainingDelayTime(int32_t requestId, int32_t &delayTime)
99 {
100     MessageParcel data;
101     MessageParcel reply;
102     MessageOption option = {MessageOption::TF_SYNC};
103     if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
104         BGTASK_LOGE(" BackgroundTaskMgrProxy::%{public}s, write descriptor failed", __func__);
105         return ERR_BGTASK_PARCELABLE_FAILED;
106     }
107     if (!data.WriteInt32(requestId)) {
108         BGTASK_LOGE(" BackgroundTaskMgrProxy::%{public}s, write requestId failed", __func__);
109         return ERR_BGTASK_PARCELABLE_FAILED;
110     }
111 
112     ErrCode result = InnerTransact(GET_REMAINING_DELAY_TIME, option, data, reply);
113     if (result != ERR_OK) {
114         BGTASK_LOGE("fail: transact ErrCode=%{public}d", result);
115         return ERR_BGTASK_TRANSACT_FAILED;
116     }
117     if (!reply.ReadInt32(result)) {
118         BGTASK_LOGE("fail: read result failed.");
119         return ERR_BGTASK_PARCELABLE_FAILED;
120     }
121     if (!reply.ReadInt32(delayTime)) {
122         BGTASK_LOGE("fail: read result failed.");
123         return ERR_BGTASK_PARCELABLE_FAILED;
124     }
125     return result;
126 }
127 
StartBackgroundRunning(const sptr<ContinuousTaskParam> taskParam)128 ErrCode BackgroundTaskMgrProxy::StartBackgroundRunning(const sptr<ContinuousTaskParam> taskParam)
129 {
130     BGTASK_LOGI("begin");
131     if (taskParam == nullptr) {
132         return ERR_BGTASK_INVALID_PARAM;
133     }
134 
135     MessageParcel data;
136     if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
137         return ERR_BGTASK_PARCELABLE_FAILED;
138     }
139 
140     if (!data.WriteParcelable(taskParam)) {
141         return ERR_BGTASK_PARCELABLE_FAILED;
142     }
143 
144     MessageParcel reply;
145     MessageOption option = {MessageOption::TF_SYNC};
146 
147     ErrCode result = InnerTransact(START_BACKGROUND_RUNNING, option, data, reply);
148     if (result != ERR_OK) {
149         BGTASK_LOGE("fail: transact ErrCode=%{public}d", result);
150         return ERR_BGTASK_TRANSACT_FAILED;
151     }
152     if (!reply.ReadInt32(result)) {
153         BGTASK_LOGE("fail: read result failed.");
154         return ERR_BGTASK_PARCELABLE_FAILED;
155     }
156     BGTASK_LOGI("end");
157     return result;
158 }
159 
StopBackgroundRunning(const std::string & abilityName,const sptr<IRemoteObject> & abilityToken)160 ErrCode BackgroundTaskMgrProxy::StopBackgroundRunning(const std::string &abilityName,
161     const sptr<IRemoteObject> &abilityToken)
162 {
163     BGTASK_LOGI("begin");
164     MessageParcel data;
165     if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
166         return ERR_BGTASK_PARCELABLE_FAILED;
167     }
168 
169     if (!data.WriteString(abilityName)) {
170         BGTASK_LOGE("parcel ability Name failed");
171         return ERR_BGTASK_PARCELABLE_FAILED;
172     }
173 
174     if (!data.WriteRemoteObject(abilityToken)) {
175         BGTASK_LOGE("parcel ability token failed");
176         return ERR_BGTASK_PARCELABLE_FAILED;
177     }
178 
179     MessageParcel reply;
180     MessageOption option = {MessageOption::TF_SYNC};
181 
182     ErrCode result = InnerTransact(STOP_BACKGROUND_RUNNING, option, data, reply);
183     if (result != ERR_OK) {
184         BGTASK_LOGE("transact ErrCode=%{public}d", result);
185         return ERR_BGTASK_TRANSACT_FAILED;
186     }
187     if (!reply.ReadInt32(result)) {
188         BGTASK_LOGE("read result failed.");
189         return ERR_BGTASK_PARCELABLE_FAILED;
190     }
191     BGTASK_LOGI("end");
192     return result;
193 }
194 
SubscribeBackgroundTask(const sptr<IBackgroundTaskSubscriber> & subscriber)195 ErrCode BackgroundTaskMgrProxy::SubscribeBackgroundTask(const sptr<IBackgroundTaskSubscriber>& subscriber)
196 {
197     if (subscriber == nullptr) {
198         BGTASK_LOGE("subscriber is null");
199         return ERR_BGTASK_PARCELABLE_FAILED;
200     }
201 
202     MessageParcel data;
203     MessageParcel reply;
204     MessageOption option = {MessageOption::TF_SYNC};
205     if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
206         BGTASK_LOGE("write descriptor failed");
207         return ERR_BGTASK_PARCELABLE_FAILED;
208     }
209     if (!data.WriteRemoteObject(subscriber->AsObject())) {
210         BGTASK_LOGE("write subscriber failed");
211         return ERR_BGTASK_PARCELABLE_FAILED;
212     }
213 
214     ErrCode result = InnerTransact(SUBSCRIBE_BACKGROUND_TASK, option, data, reply);
215     if (result != ERR_OK) {
216         BGTASK_LOGE("fail: transact ErrCode=%{public}d", result);
217         return ERR_BGTASK_TRANSACT_FAILED;
218     }
219     if (!reply.ReadInt32(result)) {
220         BGTASK_LOGE("fail: read result failed.");
221         return ERR_BGTASK_PARCELABLE_FAILED;
222     }
223     return result;
224 }
225 
UnsubscribeBackgroundTask(const sptr<IBackgroundTaskSubscriber> & subscriber)226 ErrCode BackgroundTaskMgrProxy::UnsubscribeBackgroundTask(const sptr<IBackgroundTaskSubscriber>& subscriber)
227 {
228     if (subscriber == nullptr) {
229         BGTASK_LOGE("subscriber is null");
230         return ERR_BGTASK_PARCELABLE_FAILED;
231     }
232 
233     MessageParcel data;
234     MessageParcel reply;
235     MessageOption option = {MessageOption::TF_SYNC};
236     if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
237         BGTASK_LOGE("write descriptor failed");
238         return ERR_BGTASK_PARCELABLE_FAILED;
239     }
240     if (!data.WriteRemoteObject(subscriber->AsObject())) {
241         BGTASK_LOGE("write subscriber failed");
242         return ERR_BGTASK_PARCELABLE_FAILED;
243     }
244 
245     ErrCode result = InnerTransact(UNSUBSCRIBE_BACKGROUND_TASK, option, data, reply);
246     if (result != ERR_OK) {
247         BGTASK_LOGE("fail: transact ErrCode=%{public}d", result);
248         return ERR_BGTASK_TRANSACT_FAILED;
249     }
250     if (!reply.ReadInt32(result)) {
251         BGTASK_LOGE("fail: read result failed.");
252         return ERR_BGTASK_PARCELABLE_FAILED;
253     }
254     return result;
255 }
256 
ShellDump(const std::vector<std::string> & dumpOption,std::vector<std::string> & dumpInfo)257 ErrCode BackgroundTaskMgrProxy::ShellDump(const std::vector<std::string> &dumpOption,
258     std::vector<std::string> &dumpInfo)
259 {
260     MessageParcel data;
261     MessageParcel reply;
262     MessageOption option = {MessageOption::TF_SYNC};
263     if (!data.WriteInterfaceToken(BackgroundTaskMgrProxy::GetDescriptor())) {
264         BGTASK_LOGE("write interface token failed.");
265         return ERR_BGTASK_PARCELABLE_FAILED;
266     }
267 
268     if (!data.WriteStringVector(dumpOption)) {
269         BGTASK_LOGE("write option failed.");
270         return ERR_BGTASK_PARCELABLE_FAILED;
271     }
272 
273     ErrCode result = InnerTransact(SHELL_DUMP, option, data, reply);
274     if (result != ERR_OK) {
275         BGTASK_LOGE("fail: transact ErrCode=%{public}d", result);
276         return ERR_BGTASK_TRANSACT_FAILED;
277     }
278     if (!reply.ReadInt32(result)) {
279         BGTASK_LOGE("fail: read result failed.");
280         return ERR_BGTASK_PARCELABLE_FAILED;
281     }
282     if (!reply.ReadStringVector(&dumpInfo)) {
283         BGTASK_LOGE("read dumpInfo failed.");
284         return ERR_BGTASK_PARCELABLE_FAILED;
285     }
286     return result;
287 }
288 
InnerTransact(uint32_t code,MessageOption & flags,MessageParcel & data,MessageParcel & reply)289 ErrCode BackgroundTaskMgrProxy::InnerTransact(uint32_t code, MessageOption &flags,
290     MessageParcel &data, MessageParcel &reply)
291 {
292     auto remote = Remote();
293     if (remote == nullptr) {
294         BGTASK_LOGE("get Remote fail code %{public}d", code);
295         return ERR_DEAD_OBJECT;
296     }
297     int err = remote->SendRequest(code, data, reply, flags);
298     switch (err) {
299         case NO_ERROR: {
300             return ERR_OK;
301         }
302         case DEAD_OBJECT: {
303             BGTASK_LOGE("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
304             return ERR_DEAD_OBJECT;
305         }
306         default: {
307             BGTASK_LOGE("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
308             return ERR_BGTASK_TRANSACT_FAILED;
309         }
310     }
311 }
312 }  // namespace BackgroundTaskMgr
313 }  // namespace OHOS