1 /*
2 * Copyright (c) 2023-2024 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 "fault_data.h"
17
18 #include "nlohmann/json.hpp"
19 #include "string_ex.h"
20 #include "hilog_tag_wrapper.h"
21
22 namespace OHOS {
23 namespace AppExecFwk {
ReadFromParcel(Parcel & parcel)24 bool FaultData::ReadFromParcel(Parcel &parcel)
25 {
26 std::string strValue;
27 if (!parcel.ReadString(strValue)) {
28 TAG_LOGE(AAFwkTag::APPMGR, "Name read string failed.");
29 return false;
30 }
31 errorObject.name = strValue;
32
33 if (!parcel.ReadString(strValue)) {
34 TAG_LOGE(AAFwkTag::APPMGR, "Message read string failed.");
35 return false;
36 }
37 errorObject.message = strValue;
38
39 if (!parcel.ReadString(strValue)) {
40 TAG_LOGE(AAFwkTag::APPMGR, "Stack read string failed.");
41 return false;
42 }
43 errorObject.stack = strValue;
44
45 int type = 0;
46 if (!parcel.ReadInt32(type)) {
47 TAG_LOGE(AAFwkTag::APPMGR, "FaultType read int32 failed.");
48 return false;
49 }
50 faultType = static_cast<FaultDataType>(type);
51
52 if (!parcel.ReadString(strValue)) {
53 TAG_LOGE(AAFwkTag::APPMGR, "TimeoutMarkers read string failed.");
54 return false;
55 }
56 timeoutMarkers = strValue;
57
58 waitSaveState = parcel.ReadBool();
59 notifyApp = parcel.ReadBool();
60 forceExit = parcel.ReadBool();
61 state = parcel.ReadUint32();
62 eventId = parcel.ReadInt32();
63 if (parcel.ReadBool()) {
64 token = (static_cast<MessageParcel*>(&parcel))->ReadRemoteObject();
65 }
66 return true;
67 }
68
Unmarshalling(Parcel & parcel)69 FaultData *FaultData::Unmarshalling(Parcel &parcel)
70 {
71 FaultData *info = new FaultData();
72 if (!info->ReadFromParcel(parcel)) {
73 delete info;
74 info = nullptr;
75 }
76 return info;
77 }
78
Marshalling(Parcel & parcel) const79 bool FaultData::Marshalling(Parcel &parcel) const
80 {
81 if (!parcel.WriteString(errorObject.name)) {
82 TAG_LOGE(AAFwkTag::APPMGR, "Name [%{public}s] write string failed.", errorObject.name.c_str());
83 return false;
84 }
85
86 if (!parcel.WriteString(errorObject.message)) {
87 TAG_LOGE(AAFwkTag::APPMGR, "Message [%{public}s] write string failed.", errorObject.message.c_str());
88 return false;
89 }
90
91 if (!parcel.WriteString(errorObject.stack)) {
92 TAG_LOGE(AAFwkTag::APPMGR, "Stack [%{public}s] write string failed.", errorObject.stack.c_str());
93 return false;
94 }
95
96 if (!parcel.WriteInt32(static_cast<int32_t>(faultType))) {
97 TAG_LOGE(AAFwkTag::APPMGR, "FaultType [%{public}d] write int32 failed.", static_cast<int32_t>(faultType));
98 return false;
99 }
100
101 if (!parcel.WriteString(timeoutMarkers)) {
102 TAG_LOGE(AAFwkTag::APPMGR, "TimeoutMarkers [%{public}s] write string failed.", timeoutMarkers.c_str());
103 return false;
104 }
105
106 if (!parcel.WriteBool(waitSaveState)) {
107 TAG_LOGE(AAFwkTag::APPMGR, "WaitSaveState [%{public}s] write bool failed.", waitSaveState ? "true" : "false");
108 return false;
109 }
110
111 if (!parcel.WriteBool(notifyApp)) {
112 TAG_LOGE(AAFwkTag::APPMGR, "NotifyApp [%{public}s] write bool failed.", notifyApp ? "true" : "false");
113 return false;
114 }
115
116 if (!parcel.WriteBool(forceExit)) {
117 TAG_LOGE(AAFwkTag::APPMGR, "ForceExit [%{public}s] write bool failed.", forceExit ? "true" : "false");
118 return false;
119 }
120
121 if (!parcel.WriteUint32(state)) {
122 TAG_LOGE(AAFwkTag::APPMGR, "State [%{public}u] write uint32 failed.", state);
123 return false;
124 }
125
126 if (!parcel.WriteInt32(eventId)) {
127 TAG_LOGE(AAFwkTag::APPMGR, "EventId [%{public}u] write int32 failed.", eventId);
128 return false;
129 }
130
131 if (token == nullptr) {
132 if (!parcel.WriteBool(false)) {
133 TAG_LOGE(AAFwkTag::APPMGR, "Token falge [false] write bool failed.");
134 return false;
135 }
136 } else {
137 if (!parcel.WriteBool(true) || !(static_cast<MessageParcel*>(&parcel))->WriteRemoteObject(token)) {
138 TAG_LOGE(AAFwkTag::APPMGR, "Token falge [true] write bool failed.");
139 return false;
140 }
141 }
142 return true;
143 }
144
ReadFromParcel(Parcel & parcel)145 bool AppFaultDataBySA::ReadFromParcel(Parcel &parcel)
146 {
147 std::string strValue;
148 if (!parcel.ReadString(strValue)) {
149 TAG_LOGE(AAFwkTag::APPMGR, "Name read string failed.");
150 return false;
151 }
152 errorObject.name = strValue;
153
154 if (!parcel.ReadString(strValue)) {
155 TAG_LOGE(AAFwkTag::APPMGR, "Message read string failed.");
156 return false;
157 }
158 errorObject.message = strValue;
159
160 if (!parcel.ReadString(strValue)) {
161 TAG_LOGE(AAFwkTag::APPMGR, "Stack read string failed.");
162 return false;
163 }
164 errorObject.stack = strValue;
165
166 int type = 0;
167 if (!parcel.ReadInt32(type)) {
168 TAG_LOGE(AAFwkTag::APPMGR, "Type read int32 failed.");
169 return false;
170 }
171 faultType = static_cast<FaultDataType>(type);
172
173 if (!parcel.ReadInt32(pid)) {
174 TAG_LOGE(AAFwkTag::APPMGR, "Pid read int32 failed.");
175 return false;
176 }
177
178 if (!parcel.ReadString(strValue)) {
179 TAG_LOGE(AAFwkTag::APPMGR, "TimeoutMarkers read string failed.");
180 return false;
181 }
182 timeoutMarkers = strValue;
183
184 waitSaveState = parcel.ReadBool();
185 notifyApp = parcel.ReadBool();
186 forceExit = parcel.ReadBool();
187 state = parcel.ReadUint32();
188 eventId = parcel.ReadInt32();
189 if (parcel.ReadBool()) {
190 token = (static_cast<MessageParcel*>(&parcel))->ReadRemoteObject();
191 }
192 return true;
193 }
194
Unmarshalling(Parcel & parcel)195 AppFaultDataBySA *AppFaultDataBySA::Unmarshalling(Parcel &parcel)
196 {
197 AppFaultDataBySA *info = new AppFaultDataBySA();
198 if (!info->ReadFromParcel(parcel)) {
199 delete info;
200 info = nullptr;
201 }
202 return info;
203 }
204
Marshalling(Parcel & parcel) const205 bool AppFaultDataBySA::Marshalling(Parcel &parcel) const
206 {
207 if (!parcel.WriteString(errorObject.name)) {
208 TAG_LOGE(AAFwkTag::APPMGR, "Name [%{public}s] write string failed.", errorObject.name.c_str());
209 return false;
210 }
211
212 if (!parcel.WriteString(errorObject.message)) {
213 TAG_LOGE(AAFwkTag::APPMGR, "Message [%{public}s] write string failed.", errorObject.message.c_str());
214 return false;
215 }
216
217 if (!parcel.WriteString(errorObject.stack)) {
218 TAG_LOGE(AAFwkTag::APPMGR, "Stack [%{public}s] write string failed.", errorObject.stack.c_str());
219 return false;
220 }
221
222 if (!parcel.WriteInt32(static_cast<int32_t>(faultType))) {
223 TAG_LOGE(AAFwkTag::APPMGR, "FaultType [%{public}d] write int32 failed.", static_cast<int32_t>(faultType));
224 return false;
225 }
226
227 if (!parcel.WriteInt32(pid)) {
228 TAG_LOGE(AAFwkTag::APPMGR, "Pid [%{public}d] write int32 failed.", static_cast<int32_t>(pid));
229 return false;
230 }
231
232 if (!parcel.WriteString(timeoutMarkers)) {
233 TAG_LOGE(AAFwkTag::APPMGR, "TimeoutMarkers [%{public}s] write string failed.", timeoutMarkers.c_str());
234 return false;
235 }
236
237 if (!parcel.WriteBool(waitSaveState)) {
238 TAG_LOGE(AAFwkTag::APPMGR, "WaitSaveState [%{public}s] write bool failed.", waitSaveState ? "true" : "false");
239 return false;
240 }
241
242 if (!parcel.WriteBool(notifyApp)) {
243 TAG_LOGE(AAFwkTag::APPMGR, "NotifyApp [%{public}s] write bool failed.", notifyApp ? "true" : "false");
244 return false;
245 }
246
247 if (!parcel.WriteBool(forceExit)) {
248 TAG_LOGE(AAFwkTag::APPMGR, "ForceExit [%{public}s] write bool failed.", forceExit ? "true" : "false");
249 return false;
250 }
251
252 if (!parcel.WriteUint32(state)) {
253 TAG_LOGE(AAFwkTag::APPMGR, "State [%{public}u] write uint32 failed.", state);
254 return false;
255 }
256
257 if (!parcel.WriteInt32(eventId)) {
258 TAG_LOGE(AAFwkTag::APPMGR, "EventId [%{public}u] write int32 failed.", eventId);
259 return false;
260 }
261
262 if (token == nullptr) {
263 if (!parcel.WriteBool(false)) {
264 TAG_LOGE(AAFwkTag::APPMGR, "Token falge [false] write bool failed.");
265 return false;
266 }
267 } else {
268 if (!parcel.WriteBool(true) || !(static_cast<MessageParcel*>(&parcel))->WriteRemoteObject(token)) {
269 TAG_LOGE(AAFwkTag::APPMGR, "Token falge [true] write bool failed.");
270 return false;
271 }
272 }
273 return true;
274 }
275 } // namespace AppExecFwk
276 } // namespace OHOS