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_callback_info.h"
17 
18 #include "string_ex.h"
19 
20 #include "continuous_task_log.h"
21 
22 namespace OHOS {
23 namespace BackgroundTaskMgr {
ContinuousTaskCallbackInfo()24 ContinuousTaskCallbackInfo::ContinuousTaskCallbackInfo() {}
25 
GetTypeId() const26 uint32_t ContinuousTaskCallbackInfo::GetTypeId() const
27 {
28     return typeId_;
29 }
30 
GetTypeIds() const31 const std::vector<uint32_t>& ContinuousTaskCallbackInfo::GetTypeIds() const
32 {
33     return typeIds_;
34 }
35 
IsBatchApi() const36 bool ContinuousTaskCallbackInfo::IsBatchApi() const
37 {
38     return isBatchApi_;
39 }
40 
GetAbilityId() const41 int32_t ContinuousTaskCallbackInfo::GetAbilityId() const
42 {
43     return abilityId_;
44 }
45 
GetCreatorUid() const46 int32_t ContinuousTaskCallbackInfo::GetCreatorUid() const
47 {
48     return creatorUid_;
49 }
50 
GetCreatorPid() const51 pid_t ContinuousTaskCallbackInfo::GetCreatorPid() const
52 {
53     return creatorPid_;
54 }
55 
GetAbilityName() const56 std::string ContinuousTaskCallbackInfo::GetAbilityName() const
57 {
58     return abilityName_;
59 }
60 
IsFromWebview() const61 bool ContinuousTaskCallbackInfo::IsFromWebview() const
62 {
63     return isFromWebview_;
64 }
65 
GetTokenId() const66 uint64_t ContinuousTaskCallbackInfo::GetTokenId() const
67 {
68     return tokenId_;
69 }
70 
Marshalling(Parcel & parcel) const71 bool ContinuousTaskCallbackInfo::Marshalling(Parcel &parcel) const
72 {
73     if (!parcel.WriteUint32(typeId_)) {
74         BGTASK_LOGE("Failed to write typeId");
75         return false;
76     }
77 
78     if (!parcel.WriteInt32(creatorUid_)) {
79         BGTASK_LOGE("Failed to write creator uid");
80         return false;
81     }
82 
83     if (!parcel.WriteInt32(creatorPid_)) {
84         BGTASK_LOGE("Failed to write creator pid");
85         return false;
86     }
87 
88     if (!parcel.WriteBool(isFromWebview_)) {
89         BGTASK_LOGE("Failed to write the flag which indicates from webview");
90         return false;
91     }
92 
93     std::u16string u16AbilityName = Str8ToStr16(abilityName_);
94     if (!parcel.WriteString16(u16AbilityName)) {
95         BGTASK_LOGE("Failed to write ability name");
96         return false;
97     }
98     if (!parcel.WriteBool(isBatchApi_)) {
99         BGTASK_LOGE("Failed to write isBatchApi_");
100         return false;
101     }
102     BGTASK_LOGD("write typeIds_ size %{public}u", static_cast<uint32_t>(typeIds_.size()));
103     if (!parcel.WriteUInt32Vector(typeIds_)) {
104         BGTASK_LOGE("Failed to write typeIds_");
105         return false;
106     }
107     if (!parcel.WriteInt32(abilityId_)) {
108         BGTASK_LOGE("Failed to write abilityId_");
109         return false;
110     }
111     if (!parcel.WriteUint64(tokenId_)) {
112         BGTASK_LOGE("Failed to write tokenId_");
113         return false;
114     }
115     return true;
116 }
117 
Unmarshalling(Parcel & parcel)118 ContinuousTaskCallbackInfo *ContinuousTaskCallbackInfo::Unmarshalling(Parcel &parcel)
119 {
120     auto object = new (std::nothrow) ContinuousTaskCallbackInfo();
121     if ((object != nullptr) && !object->ReadFromParcel(parcel)) {
122         delete object;
123         object = nullptr;
124     }
125 
126     return object;
127 }
128 
ReadFromParcel(Parcel & parcel)129 bool ContinuousTaskCallbackInfo::ReadFromParcel(Parcel &parcel)
130 {
131     if (!parcel.ReadUint32(typeId_)) {
132         BGTASK_LOGE("read parce typeId error");
133         return false;
134     }
135     if (!parcel.ReadInt32(creatorUid_)) {
136         BGTASK_LOGE("read parce creatorUid error");
137         return false;
138     }
139     if (!parcel.ReadInt32(creatorPid_)) {
140         BGTASK_LOGE("read parce creatorPid error");
141         return false;
142     }
143 
144     if (!parcel.ReadBool(isFromWebview_)) {
145         BGTASK_LOGE("Failed to read the flag which indicates from webview");
146         return false;
147     }
148 
149     std::u16string u16AbilityName;
150     if (!parcel.ReadString16(u16AbilityName)) {
151         BGTASK_LOGE("Failed to read creator ability name");
152         return false;
153     }
154     abilityName_ = Str16ToStr8(u16AbilityName);
155     if (!parcel.ReadBool(isBatchApi_)) {
156         BGTASK_LOGE("Failed to read the flag isBatchApi_");
157         return false;
158     }
159     if (!parcel.ReadUInt32Vector(&typeIds_)) {
160         BGTASK_LOGE("read parce typeIds_ error");
161         return false;
162     }
163     BGTASK_LOGD("read parce typeIds_ size %{public}u", static_cast<uint32_t>(typeIds_.size()));
164     if (!parcel.ReadInt32(abilityId_)) {
165         BGTASK_LOGE("read parce abilityId error");
166         return false;
167     }
168     if (!parcel.ReadUint64(tokenId_)) {
169         BGTASK_LOGE("read parce tokenId error");
170         return false;
171     }
172     return true;
173 }
174 }  // namespace BackgroundTaskMgr
175 }  // namespace OHOS