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 DATA_COLLECTION_H
17 #define DATA_COLLECTION_H
18 
19 #include <fstream>
20 #include <mutex>
21 #include <unordered_map>
22 #include <vector>
23 #include <string>
24 #include <sstream>
25 #include "nlohmann/json.hpp"
26 
27 #include "lib_loader.h"
28 #include "i_collector_fwk.h"
29 #include "security_event.h"
30 #include "security_event_ruler.h"
31 
32 namespace OHOS::Security::SecurityCollector {
33 
34 class DataCollection {
35 public:
36     static DataCollection &GetInstance();
37     bool StartCollectors(const std::vector<int64_t>& eventIds, std::shared_ptr<ICollectorFwk> api);
38     bool StopCollectors(const std::vector<int64_t>& eventIds);
39     ErrorCode GetCollectorType(int64_t eventId, int32_t& collectorType);
40     int32_t QuerySecurityEvent(const std::vector<SecurityEventRuler> rulers,
41     std::vector<SecurityEvent> &events);
42     bool SecurityGuardSubscribeCollector(const std::vector<int64_t>& eventIds);
43 private:
44     DataCollection() = default;
45     ErrorCode LoadCollector(int64_t eventId, std::string path, std::shared_ptr<ICollectorFwk> api);
46     ErrorCode LoadCollector(std::string path, const SecurityEventRuler &ruler, std::vector<SecurityEvent> &events);
47     ErrorCode GetCollectorPath(int64_t eventId, std::string& path);
48     ErrorCode CheckFileStream(std::ifstream &stream);
49     bool IsCollectorStarted(int64_t eventId);
50     std::mutex mutex_;
51     std::unordered_map<int64_t, std::unique_ptr<LibLoader>> eventIdToLoaderMap_;
52 };
53 }
54 #endif // DATA_COLLECTION_H