1 /* 2 * Copyright (C) 2023 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_GUARD_DATABASE_MANAGER_H 17 #define SECURITY_GUARD_DATABASE_MANAGER_H 18 19 #include <mutex> 20 #include <set> 21 #include <unordered_map> 22 23 #include "config_define.h" 24 #include "i_db_listener.h" 25 26 namespace OHOS::Security::SecurityGuard { 27 class DatabaseManager { 28 public: 29 static DatabaseManager &GetInstance(); 30 void Init(); 31 int32_t SetAuditState(bool enable); 32 int InsertEvent(uint32_t source, SecEvent& event); 33 int QueryAllEvent(std::string table, std::vector<SecEvent> &events); 34 int QueryAllEventFromMem(std::vector<SecEvent> &events); 35 int QueryRecentEventByEventId(int64_t eventId, SecEvent &event); 36 int QueryRecentEventByEventId(std::string table, const std::vector<int64_t> &eventId, std::vector<SecEvent> &event); 37 int QueryEventByEventIdAndDate(std::string table, std::vector<int64_t> &eventIds, std::vector<SecEvent> &events, 38 std::string beginTime, std::string endTime); 39 int QueryEventByEventId(int64_t eventId, std::vector<SecEvent> &events); 40 int QueryEventByEventId(std::string table, std::vector<int64_t> &eventIds, std::vector<SecEvent> &events); 41 int QueryEventByEventType(std::string table, int32_t eventType, std::vector<SecEvent> &events); 42 int QueryEventByLevel(std::string table, int32_t level, std::vector<SecEvent> &events); 43 int QueryEventByOwner(std::string table, std::string owner, std::vector<SecEvent> &events); 44 int64_t CountAllEvent(std::string table); 45 int64_t CountEventByEventId(int64_t eventId); 46 int DeleteOldEventByEventId(int64_t eventId, int64_t count); 47 int DeleteAllEventByEventId(int64_t eventId); 48 int32_t SubscribeDb(std::vector<int64_t> eventIds, std::shared_ptr<IDbListener> listener); 49 int32_t UnSubscribeDb(std::vector<int64_t> eventIds, std::shared_ptr<IDbListener> listener); 50 51 private: 52 std::mutex mutex_; 53 std::mutex dbMutex_; 54 std::unordered_map<int64_t, std::set<std::shared_ptr<IDbListener>>> listenerMap_; 55 std::string deviceId_; 56 void FillUserIdAndDeviceId(SecEvent& event); 57 void DbChanged(int32_t optType, const SecEvent &event); 58 int32_t OpenAudit(); 59 int32_t CloseAudit(); 60 }; 61 } // namespace OHOS::Security::SecurityGuard { 62 #endif // SECURITY_GUARD_DATABASE_MANAGER_H 63