• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-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 "i_anco_consumer.h"
17 
18 #include "mmi_log.h"
19 
20 #undef MMI_LOG_TAG
21 #define MMI_LOG_TAG "IAncoConsumer"
22 
23 namespace OHOS {
24 namespace MMI {
25 namespace {
26 constexpr uint64_t MAX_UNMARSHAL_VECTOR_SIZE { 512 };
27 }
28 
29 template<typename T>
MarshalVector(const std::vector<T> & data,Parcel & parcel,bool (* writeOne)(const T & arg,Parcel & parcel))30 static bool MarshalVector(const std::vector<T> &data, Parcel &parcel, bool (*writeOne)(const T &arg, Parcel &parcel))
31 {
32     if (writeOne == nullptr) {
33         return false;
34     }
35     uint64_t nItems = static_cast<uint64_t>(data.size());
36     if (!parcel.WriteUint64(nItems)) {
37         return false;
38     }
39     for (uint64_t index = 0; index < nItems; ++index) {
40         if (!(*writeOne)(data[index], parcel)) {
41             return false;
42         }
43     }
44     return true;
45 }
46 
47 template<typename T>
UnmarshalVector(Parcel & parcel,std::vector<T> & data,bool (* readOne)(Parcel & parcel,T & arg))48 static bool UnmarshalVector(Parcel &parcel, std::vector<T> &data, bool (*readOne)(Parcel &parcel, T &arg))
49 {
50     if (readOne == nullptr) {
51         return false;
52     }
53     uint64_t nItems {};
54     if (!parcel.ReadUint64(nItems)) {
55         return false;
56     }
57     if (nItems > MAX_UNMARSHAL_VECTOR_SIZE) {
58         MMI_HILOGE("The nItems:%{public}" PRIu64 ", exceeds maximum allowed size:%{public}"
59             PRIu64, nItems, MAX_UNMARSHAL_VECTOR_SIZE);
60         return false;
61     }
62     data.resize(nItems);
63 
64     for (uint64_t index = 0; index < nItems; ++index) {
65         if (!(*readOne)(parcel, data[index])) {
66             return false;
67         }
68     }
69     return true;
70 }
71 
MarshalRect(const Rect & rect,Parcel & parcel)72 static bool MarshalRect(const Rect &rect, Parcel &parcel)
73 {
74     return (
75         parcel.WriteInt32(rect.x) &&
76         parcel.WriteInt32(rect.y) &&
77         parcel.WriteInt32(rect.width) &&
78         parcel.WriteInt32(rect.height)
79     );
80 }
81 
UnmarshalRect(Parcel & parcel,Rect & rect)82 static bool UnmarshalRect(Parcel &parcel, Rect &rect)
83 {
84     return (
85         parcel.ReadInt32(rect.x) &&
86         parcel.ReadInt32(rect.y) &&
87         parcel.ReadInt32(rect.width) &&
88         parcel.ReadInt32(rect.height)
89     );
90 }
91 
MarshalWindowInfo(const AncoWindowInfo & windowInfo,Parcel & parcel)92 static bool MarshalWindowInfo(const AncoWindowInfo &windowInfo, Parcel &parcel)
93 {
94     return (
95         parcel.WriteInt32(windowInfo.id) &&
96         parcel.WriteUint32(windowInfo.flags) &&
97         parcel.WriteUint32(static_cast<uint32_t>(windowInfo.action)) &&
98         parcel.WriteInt32(windowInfo.displayId) &&
99         parcel.WriteFloat(windowInfo.zOrder) &&
100         parcel.WriteFloatVector(windowInfo.transform) &&
101         MarshalVector(windowInfo.defaultHotAreas, parcel, &MarshalRect) &&
102         MarshalVector(windowInfo.ancoExcludedAreas, parcel, &MarshalRect)
103     );
104 }
105 
UnmarshalWindowInfo(Parcel & parcel,AncoWindowInfo & windowInfo)106 static bool UnmarshalWindowInfo(Parcel &parcel, AncoWindowInfo &windowInfo)
107 {
108     uint32_t action {};
109 
110     bool result = (
111         parcel.ReadInt32(windowInfo.id) &&
112         parcel.ReadUint32(windowInfo.flags) &&
113         parcel.ReadUint32(action) &&
114         parcel.ReadInt32(windowInfo.displayId) &&
115         parcel.ReadFloat(windowInfo.zOrder) &&
116         parcel.ReadFloatVector(&windowInfo.transform) &&
117         UnmarshalVector(parcel, windowInfo.defaultHotAreas, &UnmarshalRect) &&
118         UnmarshalVector(parcel, windowInfo.ancoExcludedAreas, &UnmarshalRect)
119     );
120     windowInfo.action = static_cast<WINDOW_UPDATE_ACTION>(action);
121     return result;
122 }
123 
Marshalling(Parcel & out) const124 bool AncoWindows::Marshalling(Parcel &out) const
125 {
126     return (
127         out.WriteUint32(static_cast<uint32_t>(updateType)) &&
128         out.WriteInt32(focusWindowId) &&
129         MarshalVector(windows, out, &MarshalWindowInfo)
130     );
131 }
132 
ReadFromParcel(Parcel & in)133 bool AncoWindows::ReadFromParcel(Parcel &in)
134 {
135     uint32_t updateTypeData {};
136     bool result = (
137         in.ReadUint32(updateTypeData) &&
138         in.ReadInt32(focusWindowId) &&
139         UnmarshalVector(in, windows, &UnmarshalWindowInfo)
140     );
141     updateType = static_cast<ANCO_WINDOW_UPDATE_TYPE>(updateTypeData);
142     return result;
143 }
144 
Unmarshalling(Parcel & in)145 AncoWindows *AncoWindows::Unmarshalling(Parcel &in)
146 {
147     AncoWindows *data = new (std::nothrow) AncoWindows();
148     if (data && !data->ReadFromParcel(in)) {
149         delete data;
150         data = nullptr;
151     }
152     return data;
153 }
154 
Marshalling(Parcel & parcel) const155 bool AncoOneHandData::Marshalling(Parcel &parcel) const
156 {
157     return (parcel.WriteInt32(oneHandX) && parcel.WriteInt32(oneHandY) &&
158             parcel.WriteInt32(expandHeight) && parcel.WriteInt32(scalePercent));
159 }
160 
ReadFromParcel(Parcel & parcel)161 bool AncoOneHandData::ReadFromParcel(Parcel &parcel)
162 {
163     return (parcel.ReadInt32(oneHandX) && parcel.ReadInt32(oneHandY) &&
164             parcel.ReadInt32(expandHeight) && parcel.ReadInt32(scalePercent));
165 }
166 
Unmarshalling(Parcel & parcel)167 AncoOneHandData *AncoOneHandData::Unmarshalling(Parcel &parcel)
168 {
169     AncoOneHandData *data = new (std::nothrow) AncoOneHandData();
170     if (data && !data->ReadFromParcel(parcel)) {
171         delete data;
172         data = nullptr;
173     }
174     return data;
175 }
176 } // namespace MMI
177 } // namespace OHOS