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 { 22 // LCOV_EXCL_START Marshalling(Parcel & parcel) const23bool SecurityEventFilter::Marshalling(Parcel& parcel) const 24 { 25 if (!parcel.WriteInt64(filter_.eventId)) { 26 SGLOGE("failed to write eventId"); 27 return false; 28 } 29 if (!parcel.WriteInt64(filter_.type)) { 30 SGLOGE("failed to write type"); 31 return false; 32 } 33 if (!parcel.WriteBool(filter_.isInclude)) { 34 SGLOGE("failed to write isInclude"); 35 return false; 36 } 37 if (filter_.mutes.size() > MAX_MUTE_SIZE) { 38 SGLOGE("the mutes size err"); 39 return false; 40 } 41 uint32_t size = static_cast<uint32_t>(filter_.mutes.size()); 42 if (!parcel.WriteUint32(size)) { 43 SGLOGE("failed to write mutes size"); 44 return false; 45 } 46 for (const auto &iter : filter_.mutes) { 47 if (!parcel.WriteString(iter)) { 48 SGLOGE("failed to write mute"); 49 return false; 50 } 51 } 52 return true; 53 }; 54 ReadFromParcel(Parcel & parcel)55bool SecurityEventFilter::ReadFromParcel(Parcel &parcel) 56 { 57 if (!parcel.ReadInt64(filter_.eventId)) { 58 SGLOGE("failed to read eventId"); 59 return false; 60 } 61 if (!parcel.ReadInt64(filter_.type)) { 62 SGLOGE("failed to read type"); 63 return false; 64 } 65 if (!parcel.ReadBool(filter_.isInclude)) { 66 SGLOGE("failed to read isInclude"); 67 return false; 68 } 69 uint32_t size = 0; 70 if (!parcel.ReadUint32(size)) { 71 SGLOGE("failed to read mutes size"); 72 return false; 73 } 74 if (size > MAX_MUTE_SIZE) { 75 SGLOGE("the event size error"); 76 return false; 77 } 78 for (uint32_t index = 0; index < size; index++) { 79 std::string tmp; 80 if (!parcel.ReadString(tmp)) { 81 SGLOGE("failed to read mute"); 82 return false; 83 } 84 filter_.mutes.insert(tmp); 85 } 86 return true; 87 }; 88 Unmarshalling(Parcel & parcel)89SecurityEventFilter* SecurityEventFilter::Unmarshalling(Parcel& parcel) 90 { 91 SecurityEventFilter *filter = new (std::nothrow) SecurityEventFilter(); 92 if (filter != nullptr && !filter->ReadFromParcel(parcel)) { 93 delete filter; 94 filter = nullptr; 95 } 96 return filter; 97 }; 98 GetMuteFilter() const99EventMuteFilter SecurityEventFilter::GetMuteFilter() const 100 { 101 return filter_; 102 } 103 // LCOV_EXCL_STOP 104 }