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 "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 int32_t paramsSize = 0;
139 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, paramsSize);
140 int32_t paramsType;
141 std::string params;
142 if (!ContainerSecurityVerify(parcel, paramsSize, resourceParams_.max_size())) {
143 return false;
144 }
145 for (int32_t i = 0 ; i < paramsSize; i++) {
146 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, paramsType);
147 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, params);
148 resourceParams_.emplace_back(std::make_tuple(paramsType, params));
149 }
150 return true;
151 }
152
ReadFromParcel(Parcel & parcel)153 bool AccessibilityEventInfoParcel::ReadFromParcel(Parcel &parcel)
154 {
155 if (!ReadFromParcelFirstPart(parcel)) {
156 return false;
157 }
158 if (!ReadFromParcelSecondPart(parcel)) {
159 return false;
160 }
161 if (!ReadFromParcelThirdPart(parcel)) {
162 return false;
163 }
164 return true;
165 }
166
Marshalling(Parcel & parcel) const167 bool AccessibilityEventInfoParcel::Marshalling(Parcel &parcel) const
168 {
169 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, static_cast<uint32_t>(eventType_));
170 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, static_cast<uint32_t>(gestureType_));
171 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, bundleName_);
172 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(triggerAction_));
173 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int64, parcel, timeStamp_);
174 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(textMoveStep_));
175 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(windowContentChangeTypes_));
176 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(windowChangeTypes_));
177 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(category_));
178 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, pageId_);
179
180 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int64, parcel, GetViewId());
181 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, GetWindowId());
182 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, GetCurrentIndex());
183 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, GetBeginIndex());
184 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, GetEndIndex());
185 auto contentList = GetContentList();
186 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, contentList.size());
187 for (auto &content : contentList) {
188 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, content);
189 }
190 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, GetComponentType());
191 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, GetDescription());
192 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, GetBeforeText());
193 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, GetLatestContent());
194 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int64, parcel, GetAccessibilityId());
195 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, GetItemCounts());
196 AccessibilityElementInfoParcel elementInfoParcel(elementInfo_);
197 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &elementInfoParcel);
198 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, GetTextAnnouncedForAccessibility());
199 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, GetInspectorKey());
200 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, GetRequestFocusElementId());
201 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, GetResourceBundleName());
202 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, GetResourceModuleName());
203 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, GetResourceId());
204 auto resourceParams = GetResourceParams();
205 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, resourceParams.size());
206 for (auto ¶ms : resourceParams) {
207 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, std::get<0>(params));
208 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, std::get<1>(params));
209 }
210 return true;
211 }
212
Unmarshalling(Parcel & parcel)213 sptr<AccessibilityEventInfoParcel> AccessibilityEventInfoParcel::Unmarshalling(Parcel& parcel)
214 {
215 sptr<AccessibilityEventInfoParcel> accessibilityEventInfo = new(std::nothrow) AccessibilityEventInfoParcel();
216 if (accessibilityEventInfo == nullptr) {
217 HILOG_ERROR("Failed to create accessibilityEventInfo.");
218 return nullptr;
219 }
220 if (!accessibilityEventInfo->ReadFromParcel(parcel)) {
221 HILOG_ERROR("read from parcel failed");
222 return nullptr;
223 }
224 return accessibilityEventInfo;
225 }
226 } // namespace Accessibility
227 } // namespace OHOS
228