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