1 /*
2 * Copyright (c) 2023 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 #include "sec_comp_click_event_parcel.h"
16
17 #include "sec_comp_log.h"
18 #include "securec.h"
19
20 namespace OHOS {
21 namespace Security {
22 namespace SecurityComponent {
23 namespace {
24 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
25 LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompClickEventParcel"};
26 }
27
MarshallingPointEvent(Parcel & out) const28 bool SecCompClickEventParcel::MarshallingPointEvent(Parcel& out) const
29 {
30 if (!(out.WriteDouble(this->clickInfoParams_.point.touchX)) ||
31 !(out.WriteDouble(this->clickInfoParams_.point.touchY))) {
32 SC_LOG_ERROR(LABEL, "Write touch xy pointer fail");
33 return false;
34 }
35
36 if (!(out.WriteUint64(this->clickInfoParams_.point.timestamp))) {
37 SC_LOG_ERROR(LABEL, "Write touch timestamp fail");
38 return false;
39 }
40 return true;
41 }
42
MarshallingKeyEvent(Parcel & out) const43 bool SecCompClickEventParcel::MarshallingKeyEvent(Parcel& out) const
44 {
45 if (!(out.WriteUint64(this->clickInfoParams_.key.timestamp))) {
46 SC_LOG_ERROR(LABEL, "Write key timestamp fail");
47 return false;
48 }
49
50 if (!(out.WriteInt32(this->clickInfoParams_.key.keyCode))) {
51 SC_LOG_ERROR(LABEL, "Write key code fail");
52 return false;
53 }
54 return true;
55 }
56
MarshallingAccessibilityEvent(Parcel & out) const57 bool SecCompClickEventParcel::MarshallingAccessibilityEvent(Parcel& out) const
58 {
59 if (!(out.WriteInt64(this->clickInfoParams_.accessibility.timestamp))) {
60 SC_LOG_ERROR(LABEL, "Write accessibility timestamp fail.");
61 return false;
62 }
63
64 if (!(out.WriteInt64(this->clickInfoParams_.accessibility.componentId))) {
65 SC_LOG_ERROR(LABEL, "Write accessibility componentId fail.");
66 return false;
67 }
68 return true;
69 }
70
Marshalling(Parcel & out) const71 bool SecCompClickEventParcel::Marshalling(Parcel& out) const
72 {
73 if (!(out.WriteInt32(static_cast<int32_t>(this->clickInfoParams_.type)))) {
74 SC_LOG_ERROR(LABEL, "Write click type fail");
75 return false;
76 }
77
78 switch (this->clickInfoParams_.type) {
79 case ClickEventType::POINT_EVENT_TYPE:
80 if (!MarshallingPointEvent(out)) {
81 return false;
82 }
83 break;
84 case ClickEventType::KEY_EVENT_TYPE:
85 if (!MarshallingKeyEvent(out)) {
86 return false;
87 }
88 break;
89 case ClickEventType::ACCESSIBILITY_EVENT_TYPE:
90 if (!MarshallingAccessibilityEvent(out)) {
91 return false;
92 }
93 break;
94 default:
95 SC_LOG_ERROR(LABEL, "Click type %{public}d is error", this->clickInfoParams_.type);
96 return false;
97 }
98
99 if (!(out.WriteUint32(this->clickInfoParams_.extraInfo.dataSize))) {
100 SC_LOG_ERROR(LABEL, "Write extraInfo dataSize fail");
101 return false;
102 }
103 if (this->clickInfoParams_.extraInfo.dataSize != 0 &&
104 !(out.WriteBuffer(this->clickInfoParams_.extraInfo.data, this->clickInfoParams_.extraInfo.dataSize))) {
105 SC_LOG_ERROR(LABEL, "Write click extraInfo data fail");
106 return false;
107 }
108
109 return true;
110 }
111
UnmarshallingPointEvent(Parcel & in,SecCompClickEvent & clickInfo)112 bool SecCompClickEventParcel::UnmarshallingPointEvent(Parcel& in, SecCompClickEvent& clickInfo)
113 {
114 if (!in.ReadDouble(clickInfo.point.touchX) || !in.ReadDouble(clickInfo.point.touchY)) {
115 SC_LOG_ERROR(LABEL, "Read touch xy porinter fail");
116 return false;
117 }
118
119 if (!in.ReadUint64(clickInfo.point.timestamp)) {
120 SC_LOG_ERROR(LABEL, "Read timestamp fail");
121 return false;
122 }
123 return true;
124 }
125
UnmarshallingKeyEvent(Parcel & in,SecCompClickEvent & clickInfo)126 bool SecCompClickEventParcel::UnmarshallingKeyEvent(Parcel& in, SecCompClickEvent& clickInfo)
127 {
128 if (!in.ReadUint64(clickInfo.key.timestamp)) {
129 SC_LOG_ERROR(LABEL, "Read timestamp fail");
130 return false;
131 }
132
133 if (!in.ReadInt32(clickInfo.key.keyCode)) {
134 SC_LOG_ERROR(LABEL, "Read keyCode fail");
135 return false;
136 }
137 return true;
138 }
139
UnmarshallingAccessibilityEvent(Parcel & in,SecCompClickEvent & clickInfo)140 bool SecCompClickEventParcel::UnmarshallingAccessibilityEvent(Parcel& in, SecCompClickEvent& clickInfo)
141 {
142 if (!in.ReadInt64(clickInfo.accessibility.timestamp)) {
143 SC_LOG_ERROR(LABEL, "Read timestamp fail.");
144 return false;
145 }
146
147 if (!in.ReadInt64(clickInfo.accessibility.componentId)) {
148 SC_LOG_ERROR(LABEL, "Read componentId fail.");
149 return false;
150 }
151 return true;
152 }
153
UnmarshallingEvent(Parcel & in,SecCompClickEvent & clickInfo)154 bool SecCompClickEventParcel::UnmarshallingEvent(Parcel& in, SecCompClickEvent& clickInfo)
155 {
156 switch (clickInfo.type) {
157 case ClickEventType::POINT_EVENT_TYPE:
158 if (!UnmarshallingPointEvent(in, clickInfo)) {
159 return false;
160 }
161 break;
162 case ClickEventType::KEY_EVENT_TYPE:
163 if (!UnmarshallingKeyEvent(in, clickInfo)) {
164 return false;
165 }
166 break;
167 case ClickEventType::ACCESSIBILITY_EVENT_TYPE:
168 if (!UnmarshallingAccessibilityEvent(in, clickInfo)) {
169 return false;
170 }
171 break;
172 default:
173 SC_LOG_ERROR(LABEL, "Click type %{public}d is error", clickInfo.type);
174 return false;
175 }
176
177 return true;
178 }
179
Unmarshalling(Parcel & in)180 SecCompClickEventParcel* SecCompClickEventParcel::Unmarshalling(Parcel& in)
181 {
182 SecCompClickEventParcel* clickInfoParcel = new (std::nothrow) SecCompClickEventParcel();
183 if (clickInfoParcel == nullptr) {
184 return nullptr;
185 }
186
187 SecCompClickEvent clickInfo = {};
188 int32_t type;
189 if (!in.ReadInt32(type)) {
190 SC_LOG_ERROR(LABEL, "Read click type fail");
191 delete clickInfoParcel;
192 return nullptr;
193 }
194 clickInfo.type = static_cast<ClickEventType>(type);
195
196 if (!UnmarshallingEvent(in, clickInfo)) {
197 delete clickInfoParcel;
198 return nullptr;
199 }
200
201 if (!in.ReadUint32(clickInfo.extraInfo.dataSize)) {
202 SC_LOG_ERROR(LABEL, "Read extraInfo data size fail");
203 delete clickInfoParcel;
204 return nullptr;
205 }
206
207 if (clickInfo.extraInfo.dataSize == 0) {
208 clickInfoParcel->clickInfoParams_ = clickInfo;
209 return clickInfoParcel;
210 } else if (clickInfo.extraInfo.dataSize > MAX_EXTRA_SIZE) {
211 SC_LOG_ERROR(LABEL, "Read extraInfo data size invalid");
212 delete clickInfoParcel;
213 return nullptr;
214 }
215
216 clickInfo.extraInfo.data = const_cast<uint8_t*>(in.ReadBuffer(clickInfo.extraInfo.dataSize));
217 if (clickInfo.extraInfo.data == nullptr) {
218 SC_LOG_ERROR(LABEL, "Read extraInfo data failed");
219 delete clickInfoParcel;
220 return nullptr;
221 }
222
223 clickInfoParcel->clickInfoParams_ = clickInfo;
224 return clickInfoParcel;
225 }
226 } // namespace SecurityComponent
227 } // namespace Security
228 } // namespace OHOS
229