1 /*
2 * Copyright (C) 2022-2025 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 "accessibility_event_info_parcel.h"
17 #include "accessibility_element_info_parcel.h"
18 #include "hilog_wrapper.h"
19 #include "parcel_util.h"
20
21 namespace OHOS {
22 namespace Accessibility {
AccessibilityEventInfoParcel(const AccessibilityEventInfo & eventInfo)23 AccessibilityEventInfoParcel::AccessibilityEventInfoParcel(const AccessibilityEventInfo &eventInfo)
24 : AccessibilityEventInfo(eventInfo)
25 {
26 }
27
ReadFromParcelFirstPart(Parcel & parcel)28 bool AccessibilityEventInfoParcel::ReadFromParcelFirstPart(Parcel &parcel)
29 {
30 uint32_t eventType = TYPE_VIEW_INVALID;
31 uint32_t gestureType = GESTURE_INVALID;
32 int32_t triggerAction = ACCESSIBILITY_ACTION_INVALID;
33 int32_t textMoveStep = STEP_CHARACTER;
34 int32_t windowContentChangeTypes = CONTENT_CHANGE_TYPE_INVALID;
35 int32_t windowChangeTypes = WINDOW_UPDATE_INVALID;
36 int32_t category = CATEGORY_INVALID;
37 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, eventType);
38 eventType_ = static_cast<EventType>(eventType);
39 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, gestureType);
40 gestureType_ = static_cast<GestureType>(gestureType);
41 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, bundleName_);
42 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, triggerAction);
43 triggerAction_ = static_cast<ActionType>(triggerAction);
44 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int64, parcel, timeStamp_);
45 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, textMoveStep);
46 textMoveStep_ = static_cast<TextMoveUnit>(textMoveStep);
47 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, windowContentChangeTypes);
48 windowContentChangeTypes_ = static_cast<WindowsContentChangeTypes>(windowContentChangeTypes);
49 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, windowChangeTypes);
50 windowChangeTypes_ = static_cast<WindowUpdateType>(windowChangeTypes);
51 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, category);
52 category_ = static_cast<NotificationCategory>(category);
53 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, pageId_);
54 return true;
55 }
56
ReadFromParcelSecondPart(Parcel & parcel)57 bool AccessibilityEventInfoParcel::ReadFromParcelSecondPart(Parcel &parcel)
58 {
59 int64_t componentId = 0;
60 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int64, parcel, componentId);
61 SetSource(componentId);
62 int32_t windowId = 0;
63 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, windowId);
64 SetWindowId(windowId);
65 int32_t currentIndex = 0;
66 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, currentIndex);
67 SetCurrentIndex(currentIndex);
68 int32_t beginIndex = 0;
69 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, beginIndex);
70 SetBeginIndex(beginIndex);
71 int32_t endIndex = 0;
72 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, endIndex);
73 SetEndIndex(endIndex);
74 int32_t contentSize = 0;
75 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, contentSize);
76 std::string content;
77 if (!ContainerSecurityVerify(parcel, contentSize, contents_.max_size())) {
78 return false;
79 }
80 for (auto i = 0 ; i < contentSize; i++) {
81 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, content);
82 AddContent(content);
83 }
84 std::string componentType;
85 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, componentType);
86 SetComponentType(componentType);
87 std::string description;
88 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, description);
89 SetDescription(description);
90 std::string beforeText;
91 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, beforeText);
92 SetBeforeText(beforeText);
93 std::string latestConent;
94 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, latestConent);
95 SetLatestContent(latestConent);
96 int64_t elementId = 0;
97 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int64, parcel, elementId);
98
99 int32_t itemCounts = 0;
100 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, itemCounts);
101 SetItemCounts(itemCounts);
102 sptr<AccessibilityElementInfoParcel> elementInfoParcel =
103 parcel.ReadStrongParcelable<AccessibilityElementInfoParcel>();
104 if (elementInfoParcel == nullptr) {
105 HILOG_ERROR("ReadStrongParcelable elementInfo failed.");
106 return false;
107 }
108 SetElementInfo(*elementInfoParcel);
109 return true;
110 }
111
ReadFromParcelThirdPart(Parcel & parcel)112 bool AccessibilityEventInfoParcel::ReadFromParcelThirdPart(Parcel &parcel)
113 {
114 std::string textAnnouncedForAccessibility;
115 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, textAnnouncedForAccessibility);
116 SetTextAnnouncedForAccessibility(textAnnouncedForAccessibility);
117
118 std::string inspectorKey;
119 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, inspectorKey);
120 SetInspectorKey(inspectorKey);
121
122 int32_t requestFocusElementId = 0;
123 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, requestFocusElementId);
124 SetRequestFocusElementId(requestFocusElementId);
125
126 std::string bundleName;
127 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, bundleName);
128 SetResourceBundleName(bundleName);
129
130 std::string moduleName;
131 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, moduleName);
132 SetResourceModuleName(moduleName);
133
134 uint32_t resourceId = 0;
135 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, resourceId);
136 SetResourceId(resourceId);
137
138 sptr<ExtraEventInfoParcel> extraEventInfo = parcel.ReadStrongParcelable<ExtraEventInfoParcel>();
139 if (extraEventInfo == nullptr) {
140 return false;
141 }
142 extraEventInfo_ = *extraEventInfo;
143 return true;
144 }
145
ReadFromParcel(Parcel & parcel)146 bool AccessibilityEventInfoParcel::ReadFromParcel(Parcel &parcel)
147 {
148 if (!ReadFromParcelFirstPart(parcel)) {
149 return false;
150 }
151 if (!ReadFromParcelSecondPart(parcel)) {
152 return false;
153 }
154 if (!ReadFromParcelThirdPart(parcel)) {
155 return false;
156 }
157 return true;
158 }
159
MarshallingFirstPart(Parcel & parcel) const160 bool AccessibilityEventInfoParcel::MarshallingFirstPart(Parcel &parcel) const
161 {
162 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, static_cast<uint32_t>(eventType_));
163 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, static_cast<uint32_t>(gestureType_));
164 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, bundleName_);
165 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(triggerAction_));
166 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int64, parcel, timeStamp_);
167 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(textMoveStep_));
168 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(windowContentChangeTypes_));
169 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(windowChangeTypes_));
170 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(category_));
171 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, pageId_);
172
173 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int64, parcel, GetViewId());
174 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, GetWindowId());
175 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, GetCurrentIndex());
176 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, GetBeginIndex());
177 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, GetEndIndex());
178 auto contentList = GetContentList();
179 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, contentList.size());
180 for (auto &content : contentList) {
181 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, content);
182 }
183 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, GetComponentType());
184 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, GetDescription());
185 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, GetBeforeText());
186 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, GetLatestContent());
187 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int64, parcel, GetAccessibilityId());
188 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, GetItemCounts());
189 AccessibilityElementInfoParcel elementInfoParcel(elementInfo_);
190 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &elementInfoParcel);
191 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, GetTextAnnouncedForAccessibility());
192 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, GetInspectorKey());
193 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, GetRequestFocusElementId());
194 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, GetResourceBundleName());
195 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, GetResourceModuleName());
196 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, GetResourceId());
197 return true;
198 }
199
MarshallingSecondPart(Parcel & parcel) const200 bool AccessibilityEventInfoParcel::MarshallingSecondPart(Parcel &parcel) const
201 {
202 ExtraEventInfoParcel extraEventInfoParcel(extraEventInfo_);
203 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &extraEventInfoParcel);
204 return true;
205 }
206
Marshalling(Parcel & parcel) const207 bool AccessibilityEventInfoParcel::Marshalling(Parcel &parcel) const
208 {
209 if (!MarshallingFirstPart(parcel)) {
210 return false;
211 }
212 if (!MarshallingSecondPart(parcel)) {
213 return false;
214 }
215 return true;
216 }
217
Unmarshalling(Parcel & parcel)218 AccessibilityEventInfoParcel *AccessibilityEventInfoParcel::Unmarshalling(Parcel& parcel)
219 {
220 AccessibilityEventInfoParcel *accessibilityEventInfo = new(std::nothrow) AccessibilityEventInfoParcel();
221 if (accessibilityEventInfo == nullptr) {
222 HILOG_ERROR("Failed to create accessibilityEventInfo.");
223 return nullptr;
224 }
225 if (!accessibilityEventInfo->ReadFromParcel(parcel)) {
226 HILOG_ERROR("read from parcel failed");
227 delete accessibilityEventInfo;
228 accessibilityEventInfo = nullptr;
229 return nullptr;
230 }
231 return accessibilityEventInfo;
232 }
233
ExtraEventInfoParcel(const ExtraEventInfo & extraEventInfo)234 ExtraEventInfoParcel::ExtraEventInfoParcel(const ExtraEventInfo &extraEventInfo)
235 : ExtraEventInfo(extraEventInfo)
236 {
237 }
238
ReadFromParcel(Parcel & parcel)239 bool ExtraEventInfoParcel::ReadFromParcel(Parcel &parcel)
240 {
241 int32_t mapValueInt = 0;
242 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, mapValueInt);
243 HILOG_DEBUG("ReadFromParcel: size is map, mapValueInt: %{public}d", mapValueInt);
244
245 if (!ContainerSecurityVerify(parcel, mapValueInt, extraEventValueStr_.max_size())) {
246 HILOG_WARN("extraEventValueStr : ExtraEventInfoParcel verify is false");
247 return false;
248 }
249 for (int32_t i = 0; i < mapValueInt; i++) {
250 std::string tempMapKey;
251 std::string tempMapVal;
252 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, tempMapKey);
253 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, tempMapVal);
254 HILOG_DEBUG("ReadFromParcel: extraEventValueStr's tempMapKey: %{public}s, tempMapVal: %{public}s",
255 tempMapKey.c_str(), tempMapVal.c_str());
256 extraEventValueStr_[tempMapKey] = tempMapVal;
257 }
258
259 return true;
260 }
261
Marshalling(Parcel & parcel) const262 bool ExtraEventInfoParcel::Marshalling(Parcel &parcel) const
263 {
264 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, extraEventValueStr_.size());
265 for (auto iterStr = extraEventValueStr_.begin(); iterStr != extraEventValueStr_.end(); ++iterStr) {
266 std::string tempMapKey;
267 std::string tempMapVal;
268 tempMapKey = iterStr->first;
269 tempMapVal = iterStr->second;
270 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, tempMapKey);
271 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, tempMapVal);
272 }
273 return true;
274 }
275
Unmarshalling(Parcel & parcel)276 ExtraEventInfoParcel *ExtraEventInfoParcel::Unmarshalling(Parcel &parcel)
277 {
278 ExtraEventInfoParcel *extraEventInfo = new(std::nothrow) ExtraEventInfoParcel();
279 if (extraEventInfo == nullptr) {
280 HILOG_ERROR("Failed to create extraEventInfo.");
281 return nullptr;
282 }
283 if (!extraEventInfo->ReadFromParcel(parcel)) {
284 HILOG_ERROR("read from parcel failed");
285 delete extraEventInfo;
286 extraEventInfo = nullptr;
287 return nullptr;
288 }
289 return extraEventInfo;
290 }
291 } // namespace Accessibility
292 } // namespace OHOS
293