1 /*
2  * Copyright (c) 2024 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(const AncoWindows & windows,Parcel & parcel)124 bool AncoWindows::Marshalling(const AncoWindows &windows, Parcel &parcel)
125 {
126     return (
127         parcel.WriteUint32(static_cast<uint32_t>(windows.updateType)) &&
128         parcel.WriteInt32(windows.focusWindowId) &&
129         MarshalVector(windows.windows, parcel, &MarshalWindowInfo)
130     );
131 }
132 
Unmarshalling(Parcel & parcel,AncoWindows & windows)133 bool AncoWindows::Unmarshalling(Parcel &parcel, AncoWindows &windows)
134 {
135     uint32_t updateType {};
136 
137     bool result = (
138         parcel.ReadUint32(updateType) &&
139         parcel.ReadInt32(windows.focusWindowId) &&
140         UnmarshalVector(parcel, windows.windows, &UnmarshalWindowInfo)
141     );
142     windows.updateType = static_cast<ANCO_WINDOW_UPDATE_TYPE>(updateType);
143     return result;
144 }
145 } // namespace MMI
146 } // namespace OHOS