• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "security_event_filter.h"
17 #include "security_guard_log.h"
18 namespace {
19     constexpr size_t MAX_MUTE_SIZE = 10;
20 }
21 namespace OHOS::Security::SecurityGuard {
Marshalling(Parcel & parcel) const22 bool SecurityEventFilter::Marshalling(Parcel& parcel) const
23 {
24     if (!parcel.WriteInt64(filter_.eventId)) {
25         SGLOGE("failed to write eventId");
26         return false;
27     }
28     if (!parcel.WriteInt64(static_cast<int64_t>(filter_.type))) {
29         SGLOGE("failed to write type");
30         return false;
31     }
32     if (!parcel.WriteString(filter_.eventGroup)) {
33         SGLOGE("failed to write event group");
34         return false;
35     }
36     if (filter_.mutes.size() > MAX_MUTE_SIZE) {
37         SGLOGE("the mutes size err");
38         return false;
39     }
40     uint32_t size = static_cast<uint32_t>(filter_.mutes.size());
41     if (!parcel.WriteUint32(size)) {
42         SGLOGE("failed to write mutes size");
43         return false;
44     }
45     for (const auto &iter : filter_.mutes) {
46         if (!parcel.WriteString(iter)) {
47             SGLOGE("failed to write mute");
48             return false;
49         }
50     }
51     return true;
52 };
53 
ReadFromParcel(Parcel & parcel)54 bool SecurityEventFilter::ReadFromParcel(Parcel &parcel)
55 {
56     if (!parcel.ReadInt64(filter_.eventId)) {
57         SGLOGE("failed to read eventId");
58         return false;
59     }
60     int64_t muteType = 0;
61     if (!parcel.ReadInt64(muteType)) {
62         SGLOGE("failed to read type");
63         return false;
64     }
65     filter_.type = static_cast<EventMuteType>(muteType);
66     if (!parcel.ReadString(filter_.eventGroup)) {
67         SGLOGE("failed to read event group");
68         return false;
69     }
70     uint32_t size = 0;
71     if (!parcel.ReadUint32(size)) {
72         SGLOGE("failed to read mutes size");
73         return false;
74     }
75     if (size > MAX_MUTE_SIZE) {
76         SGLOGE("the event size error");
77         return false;
78     }
79     for (uint32_t index = 0; index < size; index++) {
80         std::string tmp;
81         if (!parcel.ReadString(tmp)) {
82             SGLOGE("failed to read mute");
83             return false;
84         }
85         filter_.mutes.emplace_back(tmp);
86     }
87     return true;
88 };
89 
Unmarshalling(Parcel & parcel)90 SecurityEventFilter* SecurityEventFilter::Unmarshalling(Parcel& parcel)
91 {
92     SecurityEventFilter *filter = new (std::nothrow) SecurityEventFilter();
93     if (filter != nullptr && !filter->ReadFromParcel(parcel)) {
94         delete filter;
95         filter = nullptr;
96     }
97     return filter;
98 };
99 
GetMuteFilter() const100 EventMuteFilter SecurityEventFilter::GetMuteFilter() const
101 {
102     return filter_;
103 }
104 }