• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 MMI_EVENT_MAP_H
17 #define MMI_EVENT_MAP_H
18 
19 #include <tuple>
20 #include <map>
21 #include "parcel.h"
22 
23 namespace OHOS {
24 namespace MMI {
25 const int32_t TUPLE_PID { 0 };
26 const int32_t TUPLE_UID { 1 };
27 const int32_t TUPLE_NAME { 2 };
28 struct MmiEventMap : public Parcelable {
29     std::map<std::tuple<int32_t, int32_t, std::string>, int32_t> datas;
30 
ReadFromParcelMmiEventMap31     bool ReadFromParcel(Parcel &parcel)
32     {
33         int32_t size = 0;
34         if (!parcel.ReadInt32(size)) {
35             return false;
36         }
37         if (size < 0) {
38             return false;
39         }
40         for (int32_t i = 0; i < size; ++i) {
41             int32_t pid;
42             int32_t uid;
43             std::string bundleName;
44             int32_t value;
45             bool result = (
46                 parcel.ReadInt32(pid) &&
47                 parcel.ReadInt32(uid) &&
48                 parcel.ReadString(bundleName) &&
49                 parcel.ReadInt32(value)
50             );
51             if (!result) {
52                 return false;
53             }
54             std::tuple<int32_t, int32_t, std::string> tuple(pid, uid, bundleName);
55             datas.emplace(tuple, value);
56         }
57         return true;
58     }
59 
MarshallingMmiEventMap60     bool Marshalling(Parcel &parcel) const
61     {
62         if (!parcel.WriteInt32(static_cast<int32_t>(datas.size()))) {
63             return false;
64         }
65         for (auto &data : datas) {
66             if (!parcel.WriteInt32(std::get<TUPLE_PID>(data.first))) {
67                 return false;
68             }
69             if (!parcel.WriteInt32(std::get<TUPLE_UID>(data.first))) {
70                 return false;
71             }
72             if (!parcel.WriteString(std::get<TUPLE_NAME>(data.first))) {
73                 return false;
74             }
75             if (!parcel.WriteInt32(data.second)) {
76                 return false;
77             }
78         }
79         return true;
80     }
81 
UnmarshallingMmiEventMap82     static struct MmiEventMap* Unmarshalling(Parcel &parcel)
83     {
84         auto mmiEventMap = new (std::nothrow) struct MmiEventMap();
85         if (mmiEventMap && !mmiEventMap->ReadFromParcel(parcel)) {
86             delete mmiEventMap;
87             mmiEventMap = nullptr;
88         }
89 
90         return mmiEventMap;
91     }
92 };
93 } // namespace MMI
94 } // namespace OHOS
95 #endif // MMI_EVENT_MAP_H