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 QueryRecentEventByEventId(int64_t eventId, SecEvent &event); 35 int QueryRecentEventByEventId(std::string table, const std::vector<int64_t> &eventId, std::vector<SecEvent> &event); 36 int QueryEventByEventIdAndDate(std::string table, std::vector<int64_t> &eventIds, std::vector<SecEvent> &events, 37 std::string beginTime, std::string endTime); 38 int QueryEventByEventId(int64_t eventId, std::vector<SecEvent> &events); 39 int QueryEventByEventId(std::string table, std::vector<int64_t> &eventIds, std::vector<SecEvent> &events); 40 int QueryEventByEventType(std::string table, int32_t eventType, std::vector<SecEvent> &events); 41 int QueryEventByLevel(std::string table, int32_t level, std::vector<SecEvent> &events); 42 int QueryEventByOwner(std::string table, std::string owner, std::vector<SecEvent> &events); 43 int64_t CountAllEvent(std::string table); 44 int64_t CountEventByEventId(int64_t eventId); 45 int DeleteOldEventByEventId(int64_t eventId, int64_t count); 46 int DeleteAllEventByEventId(int64_t eventId); 47 int32_t SubscribeDb(std::vector<int64_t> eventIds, std::shared_ptr<IDbListener> listener); 48 int32_t UnSubscribeDb(std::vector<int64_t> eventIds, std::shared_ptr<IDbListener> listener); 49 50 private: 51 std::mutex mutex_; 52 std::mutex dbMutex_; 53 std::unordered_map<int64_t, std::set<std::shared_ptr<IDbListener>>> listenerMap_; 54 std::string deviceId_; 55 void FillUserIdAndDeviceId(SecEvent& event); 56 void DbChanged(int32_t optType, const SecEvent &event); 57 int32_t OpenAudit(); 58 int32_t CloseAudit(); 59 }; 60 } // namespace OHOS::Security::SecurityGuard { 61 #endif // SECURITY_GUARD_DATABASE_MANAGER_H