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 "continuous_task_param.h"
17 
18 #include "string_ex.h"
19 
20 #include "continuous_task_log.h"
21 
22 namespace OHOS {
23 namespace BackgroundTaskMgr {
ReadFromParcel(Parcel & parcel)24 bool ContinuousTaskParam::ReadFromParcel(Parcel &parcel)
25 {
26     if (!parcel.ReadBool(isNewApi_)) {
27         BGTASK_LOGE("Failed to read the flag which indicate whether is called from newApi");
28         return false;
29     }
30 
31     if (!parcel.ReadUint32(bgModeId_)) {
32         BGTASK_LOGE("Failed to read request background mode info");
33         return false;
34     }
35     bool valid = parcel.ReadBool();
36     if (valid) {
37         wantAgent_ = std::shared_ptr<AbilityRuntime::WantAgent::WantAgent>(
38             parcel.ReadParcelable<AbilityRuntime::WantAgent::WantAgent>());
39         if (!wantAgent_) {
40             BGTASK_LOGE("Failed to read wantAgent");
41             return false;
42         }
43     }
44 
45     std::u16string u16AbilityName;
46     if (!parcel.ReadString16(u16AbilityName)) {
47         BGTASK_LOGE("Failed to read ability name");
48         return false;
49     }
50     abilityName_ = Str16ToStr8(u16AbilityName);
51 
52     std::u16string u16AppName;
53     if (!parcel.ReadString16(u16AppName)) {
54         BGTASK_LOGE("Failed to read app name");
55         return false;
56     }
57     appName_ = Str16ToStr8(u16AppName);
58 
59     if (!parcel.ReadBool(isBatchApi_)) {
60         BGTASK_LOGE("Failed to read the flag isBatchApi");
61         return false;
62     }
63     if (isBatchApi_) {
64         if (!parcel.ReadUInt32Vector(&bgModeIds_)) {
65             BGTASK_LOGE("read parce bgmodes error");
66             return false;
67         }
68         BGTASK_LOGD("read parce bgmodes_ size %{public}d", static_cast<uint32_t>(bgModeIds_.size()));
69     }
70     if (!parcel.ReadInt32(abilityId_)) {
71         BGTASK_LOGE("Failed to read the abilityId");
72         return false;
73     }
74     return true;
75 }
76 
ReadFromParcel(Parcel & parcel)77 bool ContinuousTaskParamForInner::ReadFromParcel(Parcel &parcel)
78 {
79     if (!parcel.ReadBool(isStart_)) {
80         BGTASK_LOGE("Failed to read the flag which indicate keep or stop running background");
81         return false;
82     }
83 
84     if (!parcel.ReadUint32(bgModeId_)) {
85         BGTASK_LOGE("Failed to read request background mode info");
86         return false;
87     }
88 
89     if (!parcel.ReadInt32(uid_)) {
90         BGTASK_LOGE("Failed to read uid info");
91         return false;
92     }
93 
94     if (!parcel.ReadInt32(abilityId_)) {
95         BGTASK_LOGE("Failed to read the abilityId");
96         return false;
97     }
98     if (!parcel.ReadUint64(tokenId_)) {
99         BGTASK_LOGE("read parce tokenId error");
100         return false;
101     }
102     return true;
103 }
104 
Unmarshalling(Parcel & parcel)105 ContinuousTaskParam *ContinuousTaskParam::Unmarshalling(Parcel &parcel)
106 {
107     ContinuousTaskParam *param = new (std::nothrow) ContinuousTaskParam();
108     if (param && !param->ReadFromParcel(parcel)) {
109         BGTASK_LOGE("read from parcel failed");
110         delete param;
111         param = nullptr;
112     }
113     return param;
114 }
115 
Unmarshalling(Parcel & parcel)116 ContinuousTaskParamForInner *ContinuousTaskParamForInner::Unmarshalling(Parcel &parcel)
117 {
118     ContinuousTaskParamForInner *param = new (std::nothrow) ContinuousTaskParamForInner();
119     if (param && !param->ReadFromParcel(parcel)) {
120         BGTASK_LOGE("read from parcel failed");
121         delete param;
122         param = nullptr;
123     }
124     return param;
125 }
126 
Marshalling(Parcel & parcel) const127 bool ContinuousTaskParam::Marshalling(Parcel &parcel) const
128 {
129     if (!parcel.WriteBool(isNewApi_)) {
130         BGTASK_LOGE("Failed to write the flag which indicate whether is called from newApi");
131         return false;
132     }
133 
134     if (!parcel.WriteUint32(bgModeId_)) {
135         BGTASK_LOGE("Failed to write request background mode info");
136         return false;
137     }
138     bool valid = wantAgent_ != nullptr;
139     if (!parcel.WriteBool(valid)) {
140         BGTASK_LOGE("Failed to write the flag which indicate whether wantAgent is null");
141         return false;
142     }
143     if (valid) {
144         if (!parcel.WriteParcelable(wantAgent_.get())) {
145             BGTASK_LOGE("Failed to write wantAgent");
146             return false;
147         }
148     }
149 
150     std::u16string u16AbilityName = Str8ToStr16(abilityName_);
151     if (!parcel.WriteString16(u16AbilityName)) {
152         BGTASK_LOGE("Failed to write abilityName");
153         return false;
154     }
155     std::u16string u16AppName = Str8ToStr16(appName_);
156     if (!parcel.WriteString16(u16AppName)) {
157         BGTASK_LOGE("Failed to write appName");
158         return false;
159     }
160     if (!parcel.WriteBool(isBatchApi_)) {
161         BGTASK_LOGE("Failed to write the isBatchApi");
162         return false;
163     }
164 
165     if (isBatchApi_) {
166         BGTASK_LOGD("write modes %{public}u", static_cast<uint32_t>(bgModeIds_.size()));
167         if (!parcel.WriteUInt32Vector(bgModeIds_)) {
168             BGTASK_LOGE("Failed to write bgModeIds");
169             return false;
170         }
171     }
172 
173     if (!parcel.WriteInt32(abilityId_)) {
174         BGTASK_LOGE("Failed to write the abilityId");
175         return false;
176     }
177     return true;
178 }
179 
Marshalling(Parcel & parcel) const180 bool ContinuousTaskParamForInner::Marshalling(Parcel &parcel) const
181 {
182     if (!parcel.WriteBool(isStart_)) {
183         BGTASK_LOGE("Failed to write the flag which indicate keep or stop running background");
184         return false;
185     }
186 
187     if (!parcel.WriteUint32(bgModeId_)) {
188         BGTASK_LOGE("Failed to write request background mode info");
189         return false;
190     }
191 
192     if (!parcel.WriteInt32(uid_)) {
193         BGTASK_LOGE("Failed to write uid info");
194         return false;
195     }
196 
197     if (!parcel.WriteInt32(abilityId_)) {
198         BGTASK_LOGE("Failed to write the abilityId");
199         return false;
200     }
201 
202     if (!parcel.WriteUint64(tokenId_)) {
203         BGTASK_LOGE("Failed to write tokenId_");
204         return false;
205     }
206     return true;
207 }
208 }  // namespace BackgroundTaskMgr
209 }  // namespace OHOS