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 #include <cinttypes>
18 #include "security_guard_log.h"
19
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<ffrt::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<ffrt::mutex> lock(eventToTableMutex_);
49 eventToTableMap_[eventId] = table;
50 }
51
InsertEventGroupMap(const std::unordered_map<std::string,EventGroupCfg> & eventGroupMap)52 void ConfigDataManager::InsertEventGroupMap(const std::unordered_map<std::string, EventGroupCfg> &eventGroupMap)
53 {
54 std::lock_guard<ffrt::mutex> lock(eventGroupMutex_);
55 eventGroupMap_ = eventGroupMap;
56 }
57
GetEventGroupConfig(const std::string & groupName,EventGroupCfg & config)58 bool ConfigDataManager::GetEventGroupConfig(const std::string &groupName, EventGroupCfg &config)
59 {
60 std::lock_guard<ffrt::mutex> lock(eventGroupMutex_);
61 auto it = eventGroupMap_.find(groupName);
62 if (it != eventGroupMap_.end()) {
63 config = it->second;
64 return true;
65 }
66 return false;
67 }
68
GetIsBatchUpload(const std::string & groupName)69 bool ConfigDataManager::GetIsBatchUpload(const std::string &groupName)
70 {
71 std::lock_guard<ffrt::mutex> lock(eventGroupMutex_);
72 auto it = eventGroupMap_.find(groupName);
73 if (it != eventGroupMap_.end()) {
74 return it->second.isBatchUpload;
75 }
76 return false;
77 }
78
ResetModelMap()79 void ConfigDataManager::ResetModelMap()
80 {
81 std::lock_guard<std::mutex> lock(modelMutex_);
82 modelMap_.clear();
83 }
84
ResetEventMap()85 void ConfigDataManager::ResetEventMap()
86 {
87 std::lock_guard<ffrt::mutex> lock(eventMutex_);
88 eventMap_.clear();
89 }
90
ResetModelToEventMap()91 void ConfigDataManager::ResetModelToEventMap()
92 {
93 std::lock_guard<std::mutex> lock(modelToEventMutex_);
94 modelToEventMap_.clear();
95 }
96
ResetEventToTableMap()97 void ConfigDataManager::ResetEventToTableMap()
98 {
99 std::lock_guard<ffrt::mutex> lock(eventToTableMutex_);
100 eventToTableMap_.clear();
101 }
102
GetEventIds(uint32_t modelId)103 std::vector<int64_t> ConfigDataManager::GetEventIds(uint32_t modelId)
104 {
105 SGLOGD("modelId=%{public}u", modelId);
106 std::lock_guard<std::mutex> lock(modelToEventMutex_);
107 std::vector<int64_t> vector;
108 if (modelToEventMap_.find(modelId) != modelToEventMap_.end()) {
109 SGLOGD("map contains modelId=%{public}u", modelId);
110 vector.assign(modelToEventMap_[modelId].begin(), modelToEventMap_[modelId].end());
111 }
112 return vector;
113 }
114
GetAllEventIds()115 std::vector<int64_t> ConfigDataManager::GetAllEventIds()
116 {
117 std::lock_guard<ffrt::mutex> lock(eventMutex_);
118 std::vector<int64_t> vector;
119 for (const auto &entry : eventMap_) {
120 SGLOGD("eventId=%{public}" PRId64, entry.first);
121 vector.emplace_back(entry.first);
122 }
123 return vector;
124 }
125
GetAllModelIds()126 std::vector<uint32_t> ConfigDataManager::GetAllModelIds()
127 {
128 std::lock_guard<std::mutex> lock(modelMutex_);
129 std::vector<uint32_t> vector;
130 for (const auto &entry : modelMap_) {
131 SGLOGD("modelId=%{public}u", entry.first);
132 vector.emplace_back(entry.first);
133 }
134 return vector;
135 }
136
GetAllEventConfigs()137 std::vector<EventCfg> ConfigDataManager::GetAllEventConfigs()
138 {
139 std::lock_guard<ffrt::mutex> lock(eventMutex_);
140 std::vector<EventCfg> vector;
141 for (const auto &entry : eventMap_) {
142 vector.emplace_back(entry.second);
143 }
144 return vector;
145 }
146
GetModelConfig(uint32_t modelId,ModelCfg & config)147 bool ConfigDataManager::GetModelConfig(uint32_t modelId, ModelCfg &config)
148 {
149 std::lock_guard<std::mutex> lock(modelMutex_);
150 auto it = modelMap_.find(modelId);
151 if (it != modelMap_.end()) {
152 config = it->second;
153 return true;
154 }
155 return false;
156 }
157
GetEventConfig(int64_t eventId,EventCfg & config)158 bool ConfigDataManager::GetEventConfig(int64_t eventId, EventCfg &config)
159 {
160 std::lock_guard<ffrt::mutex> lock(eventMutex_);
161 auto it = eventMap_.find(eventId);
162 if (it != eventMap_.end()) {
163 config = it->second;
164 return true;
165 }
166 return false;
167 }
168
GetTableFromEventId(int64_t eventId)169 std::string ConfigDataManager::GetTableFromEventId(int64_t eventId)
170 {
171 std::lock_guard<ffrt::mutex> lock(eventToTableMutex_);
172 if (eventToTableMap_.find(eventId) == eventToTableMap_.end()) {
173 SGLOGE("eventToTableMap_ did not find eventId=%{public}" PRId64, eventId);
174 return "";
175 }
176 return eventToTableMap_[eventId];
177 }
178 } // OHOS::Security::SecurityGuard