• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     bool SubscribeCollectors(const std::vector<int64_t> &eventIds, std::shared_ptr<ICollectorFwk> api);
40     bool UnsubscribeCollectors(const std::vector<int64_t> &eventIds);
41     ErrorCode GetCollectorType(int64_t eventId, int32_t &collectorType);
42     int32_t QuerySecurityEvent(const std::vector<SecurityEventRuler> rulers,
43                                std::vector<SecurityEvent> &events);
44     bool SecurityGuardSubscribeCollector(const std::vector<int64_t> &eventIds);
45     void CloseLib();
46     int32_t AddFilter(const SecurityCollectorEventMuteFilter &filter);
47     int32_t RemoveFilter(const SecurityCollectorEventMuteFilter &filter);
48 private:
49     DataCollection() = default;
50     ErrorCode LoadCollector(int64_t eventId, std::string path, std::shared_ptr<ICollectorFwk> api);
51     ErrorCode LoadCollector(std::string path, const SecurityEventRuler &ruler, std::vector<SecurityEvent> &events);
52     ErrorCode GetCollectorPath(int64_t eventId, std::string& path);
53     ErrorCode CheckFileStream(std::ifstream &stream);
54     bool IsCollectorStarted(int64_t eventId);
55     std::mutex mutex_;
56     std::mutex closeLibmutex_;
57     std::unordered_map<int64_t, LibLoader> eventIdToLoaderMap_;
58     std::unordered_map<int64_t, LibLoader> needCloseLibMap_;
59 };
60 }
61 #endif // DATA_COLLECTION_H