• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 #ifndef EXTRA_DATA_H
17 #define EXTRA_DATA_H
18 
19 #include <vector>
20 #include "parcel.h"
21 
22 namespace OHOS {
23 namespace MMI {
24 struct ExtraData : public Parcelable {
25     /*
26      * buffer的最大个数
27      *
28      * @since 9
29      */
30     static constexpr int32_t MAX_BUFFER_SIZE = 1024;
31     /*
32      * 是否添加buffer信息
33      *
34      * @since 9
35      */
36     bool appended { false };
37     /*
38      * buffer信息
39      *
40      * @since 9
41      */
42     std::vector<uint8_t> buffer;
43     /*
44      * 事件类型
45      *
46      * @since 9
47      */
48     int32_t sourceType { -1 };
49     /*
50      * 事件触发的pointer id
51      *
52      * @since 9
53      */
54     int32_t pointerId { -1 };
55     /*
56      * 当前拖拽实例的标识
57      *
58      * @since 13
59      */
60     int32_t pullId { -1 };
61     /*
62      * 开始拖拽实例的事件id
63      *
64      * @since 13
65      */
66     int32_t eventId { -1 };
67     /*
68      * 使用硬光标绘制功能
69      *
70      * @since 13
71      */
72     bool drawCursor { false };
73 
UnmarshalVectorExtraData74     static bool UnmarshalVector(Parcel &parcel, std::vector<uint8_t> &buffer)
75     {
76         int32_t size {};
77         if (!parcel.ReadInt32(size)) {
78             return false;
79         }
80         if (size < 0 ||size > MAX_BUFFER_SIZE) {
81             return false;
82         }
83         buffer.resize(size);
84         for (int32_t i = 0; i < size; ++i) {
85             uint8_t value = 0;
86             if (!parcel.ReadUint8(value)) {
87                 return false;
88             }
89             buffer.push_back(value);
90         }
91         return true;
92     }
93 
ReadFromParcelExtraData94     bool ReadFromParcel(Parcel &parcel)
95     {
96         return (
97             parcel.ReadBool(appended) &&
98             UnmarshalVector(parcel, buffer) &&
99             parcel.ReadInt32(sourceType) &&
100             parcel.ReadInt32(pointerId) &&
101             parcel.ReadInt32(pullId) &&
102             parcel.ReadInt32(eventId) &&
103             parcel.ReadBool(drawCursor)
104         );
105     }
106 
MarshallingExtraData107     bool Marshalling(Parcel &parcel) const
108     {
109         if (!parcel.WriteBool(appended)) {
110             return false;
111         }
112         if (!parcel.WriteInt32(static_cast<int32_t>(buffer.size()))) {
113             return false;
114         }
115         for (int32_t i = 0; i < static_cast<int32_t>(buffer.size()); i++) {
116             if (!parcel.WriteUint8(buffer[i])) {
117                 return false;
118             }
119         }
120         if (!parcel.WriteInt32(sourceType)) {
121             return false;
122         }
123         if (!parcel.WriteInt32(pointerId)) {
124             return false;
125         }
126         if (!parcel.WriteInt32(pullId)) {
127             return false;
128         }
129         if (!parcel.WriteInt32(eventId)) {
130             return false;
131         }
132         if (!parcel.WriteBool(drawCursor)) {
133             return false;
134         }
135         return true;
136     }
137 
UnmarshallingExtraData138     static struct ExtraData* Unmarshalling(Parcel &parcel)
139     {
140         auto extraData = new (std::nothrow) struct ExtraData();
141         if (extraData && !extraData->ReadFromParcel(parcel)) {
142             delete extraData;
143             extraData = nullptr;
144         }
145 
146         return extraData;
147     }
148 };
149 } // namespace MMI
150 } // namespace OHOS
151 #endif // EXTRA_DATA_H