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     return true;
126 }
127 
ReadFromParcel(Parcel & parcel)128 bool AccessibilityEventInfoParcel::ReadFromParcel(Parcel &parcel)
129 {
130     if (!ReadFromParcelFirstPart(parcel)) {
131         return false;
132     }
133     if (!ReadFromParcelSecondPart(parcel)) {
134         return false;
135     }
136     if (!ReadFromParcelThirdPart(parcel)) {
137         return false;
138     }
139     return true;
140 }
141 
Marshalling(Parcel & parcel) const142 bool AccessibilityEventInfoParcel::Marshalling(Parcel &parcel) const
143 {
144     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, static_cast<uint32_t>(eventType_));
145     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, static_cast<uint32_t>(gestureType_));
146     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, bundleName_);
147     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(triggerAction_));
148     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int64, parcel, timeStamp_);
149     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(textMoveStep_));
150     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(windowContentChangeTypes_));
151     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(windowChangeTypes_));
152     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(category_));
153     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, pageId_);
154 
155     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int64, parcel, GetViewId());
156     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, GetWindowId());
157     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, GetCurrentIndex());
158     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, GetBeginIndex());
159     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, GetEndIndex());
160     auto contentList = GetContentList();
161     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, contentList.size());
162     for (auto &content : contentList) {
163         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, content);
164     }
165     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, GetComponentType());
166     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, GetDescription());
167     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, GetBeforeText());
168     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, GetLatestContent());
169     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int64, parcel, GetAccessibilityId());
170     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, GetItemCounts());
171     AccessibilityElementInfoParcel elementInfoParcel(elementInfo_);
172     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &elementInfoParcel);
173     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, GetTextAnnouncedForAccessibility());
174     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, GetInspectorKey());
175     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, GetRequestFocusElementId());
176     return true;
177 }
178 
Unmarshalling(Parcel & parcel)179 sptr<AccessibilityEventInfoParcel> AccessibilityEventInfoParcel::Unmarshalling(Parcel& parcel)
180 {
181     sptr<AccessibilityEventInfoParcel> accessibilityEventInfo = new(std::nothrow) AccessibilityEventInfoParcel();
182     if (accessibilityEventInfo == nullptr) {
183         HILOG_ERROR("Failed to create accessibilityEventInfo.");
184         return nullptr;
185     }
186     if (!accessibilityEventInfo->ReadFromParcel(parcel)) {
187         HILOG_ERROR("read from parcel failed");
188         return nullptr;
189     }
190     return accessibilityEventInfo;
191 }
192 } // namespace Accessibility
193 } // namespace OHOS
194