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 #ifndef SECURITY_EVENT_CONFIG_H
17 #define SECURITY_EVENT_CONFIG_H
18 
19 #include <string>
20 #include <vector>
21 #include "parcel.h"
22 #include "security_event_info.h"
23 
24 namespace OHOS::Security::SecurityGuard {
25 class SecurityEventConfig : public Parcelable {
26 public:
27     SecurityEventConfig() = default;
SecurityEventConfig(const EventCfg &config)28     SecurityEventConfig(const EventCfg &config) : config_(config){};
29     ~SecurityEventConfig() override = default;
30 
GetEventConfig() const31     EventCfg GetEventConfig() const
32     {
33         return config_;
34     };
35 
36     bool Marshalling(Parcel &parcel) const override
37     {
38         if (!parcel.WriteInt64(config_.eventId)) {
39             return false;
40         }
41         if (!parcel.WriteString(config_.eventName)) {
42             return false;
43         }
44         if (!parcel.WriteUint32(config_.version)) {
45             return false;
46         }
47         if (!parcel.WriteUint32(config_.eventType)) {
48             return false;
49         }
50         if (!parcel.WriteUint32(config_.collectOnStart)) {
51             return false;
52         }
53         if (!parcel.WriteUint32(config_.dataSensitivityLevel)) {
54             return false;
55         }
56         if (!parcel.WriteUint32(config_.storageRamNums)) {
57             return false;
58         }
59         if (!parcel.WriteInt32(config_.storageRomNums)) {
60             return false;
61         }
62 
63         if (!parcel.WriteUint32(config_.storageTime)) {
64             return false;
65         }
66 
67         uint32_t ownerSize = config_.owner.size();
68         if (!parcel.WriteUint32(ownerSize)) {
69             return false;
70         }
71 
72         for (uint32_t index = 0; index < ownerSize; index++) {
73             if (!parcel.WriteString(config_.owner[index])) {
74                 return false;
75             }
76         }
77         if (!parcel.WriteUint32(config_.source)) {
78             return false;
79         }
80 
81         if (!parcel.WriteString(config_.dbTable)) {
82             return false;
83         }
84         if (!parcel.WriteString(config_.prog)) {
85             return false;
86         }
87         return true;
88     };
89 
ReadFromParcel(Parcel &parcel)90     bool ReadFromParcel(Parcel &parcel)
91     {
92         if (!parcel.ReadInt64(config_.eventId)) {
93             return false;
94         }
95         if (!parcel.ReadString(config_.eventName)) {
96             return false;
97         }
98         if (!parcel.ReadUint32(config_.version)) {
99             return false;
100         }
101         if (!parcel.ReadUint32(config_.eventType)) {
102             return false;
103         }
104         if (!parcel.ReadUint32(config_.collectOnStart)) {
105             return false;
106         }
107         if (!parcel.ReadUint32(config_.dataSensitivityLevel)) {
108             return false;
109         }
110         if (!parcel.ReadUint32(config_.storageRamNums)) {
111             return false;
112         }
113         if (!parcel.ReadUint32(config_.storageRomNums)) {
114             return false;
115         }
116 
117         if (!parcel.ReadInt32(config_.storageTime)) {
118             return false;
119         }
120 
121         uint32_t ownerSize = 0;
122         if (!parcel.ReadUint32(ownerSize)) {
123             return false;
124         }
125 
126         for (uint32_t index = 0; index < ownerSize; index++) {
127             if (!parcel.ReadString(config_.owner[index])) {
128                 return false;
129             }
130         }
131 
132         if (!parcel.ReadUint32(config_.source)) {
133             return false;
134         }
135 
136         if (!parcel.ReadString(config_.dbTable)) {
137             return false;
138         }
139         if (!parcel.ReadString(config_.prog)) {
140             return false;
141         }
142         return true;
143     };
144 
Unmarshalling(Parcel &parcel)145     static SecurityEventConfig *Unmarshalling(Parcel &parcel)
146     {
147         SecurityEventConfig *config = new (std::nothrow) SecurityEventConfig();
148         if (config != nullptr && !config->ReadFromParcel(parcel)) {
149             delete config;
150             config = nullptr;
151         }
152 
153         return config;
154     };
155 
156 private:
157     EventCfg config_{};
158 };
159 
160 }  // namespace OHOS::Security::SecurityGuard
161 
162 #endif  // SECURITY_EVENT_CONFIG_H
163