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