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 #include "config_data_manager.h"
17 
18 #include "security_guard_log.h"
19 #include "app_info_rdb_helper.h"
20 
21 namespace OHOS::Security::SecurityGuard {
GetInstance()22 ConfigDataManager &ConfigDataManager::GetInstance()
23 {
24     static ConfigDataManager instance;
25     return instance;
26 }
27 
InsertModelMap(uint32_t modelId,const ModelCfg & config)28 void ConfigDataManager::InsertModelMap(uint32_t modelId, const ModelCfg &config)
29 {
30     std::lock_guard<std::mutex> lock(modelMutex_);
31     modelMap_[modelId] = config;
32 }
33 
InsertEventMap(int64_t eventId,const EventCfg & config)34 void ConfigDataManager::InsertEventMap(int64_t eventId, const EventCfg &config)
35 {
36     std::lock_guard<std::mutex> lock(eventMutex_);
37     eventMap_[eventId] = config;
38 }
39 
InsertModelToEventMap(uint32_t modelId,std::set<int64_t> eventIds)40 void ConfigDataManager::InsertModelToEventMap(uint32_t modelId, std::set<int64_t> eventIds)
41 {
42     std::lock_guard<std::mutex> lock(modelToEventMutex_);
43     modelToEventMap_[modelId] = eventIds;
44 }
45 
InsertEventToTableMap(int64_t eventId,std::string table)46 void ConfigDataManager::InsertEventToTableMap(int64_t eventId, std::string table)
47 {
48     std::lock_guard<std::mutex> lock(eventToTableMutex_);
49     eventToTableMap_[eventId] = table;
50 }
51 
ResetModelMap()52 void ConfigDataManager::ResetModelMap()
53 {
54     std::lock_guard<std::mutex> lock(modelMutex_);
55     modelMap_.clear();
56 }
57 
ResetEventMap()58 void ConfigDataManager::ResetEventMap()
59 {
60     std::lock_guard<std::mutex> lock(eventMutex_);
61     eventMap_.clear();
62 }
63 
ResetModelToEventMap()64 void ConfigDataManager::ResetModelToEventMap()
65 {
66     std::lock_guard<std::mutex> lock(modelToEventMutex_);
67     modelToEventMap_.clear();
68 }
69 
ResetEventToTableMap()70 void ConfigDataManager::ResetEventToTableMap()
71 {
72     std::lock_guard<std::mutex> lock(eventToTableMutex_);
73     eventToTableMap_.clear();
74 }
75 
GetEventIds(uint32_t modelId)76 std::vector<int64_t> ConfigDataManager::GetEventIds(uint32_t modelId)
77 {
78     SGLOGD("modelId=%{public}u", modelId);
79     std::lock_guard<std::mutex> lock(modelToEventMutex_);
80     std::vector<int64_t> vector;
81     if (modelToEventMap_.find(modelId) != modelToEventMap_.end()) {
82         SGLOGD("map contains modelId=%{public}u", modelId);
83         vector.assign(modelToEventMap_[modelId].begin(), modelToEventMap_[modelId].end());
84     }
85     return vector;
86 }
87 
GetAllEventIds()88 std::vector<int64_t> ConfigDataManager::GetAllEventIds()
89 {
90     std::lock_guard<std::mutex> lock(eventMutex_);
91     std::vector<int64_t> vector;
92     for (const auto &entry : eventMap_) {
93         SGLOGD("eventId=%{public}" PRId64 "", entry.first);
94         vector.emplace_back(entry.first);
95     }
96     return vector;
97 }
98 
GetAllModelIds()99 std::vector<uint32_t> ConfigDataManager::GetAllModelIds()
100 {
101     std::lock_guard<std::mutex> lock(modelMutex_);
102     std::vector<uint32_t> vector;
103     for (const auto &entry : modelMap_) {
104         SGLOGD("modelId=%{public}u", entry.first);
105         vector.emplace_back(entry.first);
106     }
107     return vector;
108 }
109 
GetModelConfig(uint32_t modelId,ModelCfg & config)110 bool ConfigDataManager::GetModelConfig(uint32_t modelId, ModelCfg &config)
111 {
112     std::lock_guard<std::mutex> lock(modelMutex_);
113     auto it = modelMap_.find(modelId);
114     if (it != modelMap_.end()) {
115         config = it->second;
116         return true;
117     }
118     return false;
119 }
120 
GetEventConfig(int64_t eventId,EventCfg & config)121 bool ConfigDataManager::GetEventConfig(int64_t eventId, EventCfg &config)
122 {
123     std::lock_guard<std::mutex> lock(eventMutex_);
124     auto it = eventMap_.find(eventId);
125     if (it != eventMap_.end()) {
126         config = it->second;
127         return true;
128     }
129     return false;
130 }
131 
GetTableFromEventId(int64_t eventId)132 std::string ConfigDataManager::GetTableFromEventId(int64_t eventId)
133 {
134     std::lock_guard<std::mutex> lock(eventToTableMutex_);
135     return eventToTableMap_[eventId];
136 }
137 
GetAppInfosByName(const std::string & appName)138 std::vector<AppInfo> ConfigDataManager::GetAppInfosByName(const std::string &appName)
139 {
140     std::vector<AppInfo> infos;
141     AppInfoRdbHelper::GetInstance().QueryAppInfosByName(appName, infos);
142     return infos;
143 }
144 } // OHOS::Security::SecurityGuard