1 /* 2 * Copyright (c) 2025 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 #ifndef DETECT_PLUGIN_MANAGER_H 16 #define DETECT_PLUGIN_MANAGER_H 17 18 #include <dlfcn.h> 19 #include <string> 20 #include <vector> 21 #include <unordered_map> 22 #include <unordered_set> 23 #include "ffrt.h" 24 #include "cJSON.h" 25 26 #include "i_collector_subscriber.h" 27 #include "security_guard_log.h" 28 #include "security_guard_define.h" 29 #include "i_detect_plugin.h" 30 31 namespace OHOS::Security::SecurityGuard { 32 class DetectPluginManager { 33 public: 34 static DetectPluginManager& getInstance(); 35 DetectPluginManager(const DetectPluginManager&) = delete; 36 DetectPluginManager &operator=(const DetectPluginManager &) = delete; 37 void LoadAllPlugins(); 38 void DispatchEvent(const SecurityCollector::Event &event); 39 40 private: 41 class DetectPluginManagerSubscriber : public SecurityCollector::ICollectorSubscriber { 42 public: DetectPluginManagerSubscriber(SecurityCollector::Event event)43 DetectPluginManagerSubscriber(SecurityCollector::Event event) 44 : SecurityCollector::ICollectorSubscriber(event){}; 45 ~DetectPluginManagerSubscriber() override = default; OnNotify(const SecurityCollector::Event & event)46 int32_t OnNotify(const SecurityCollector::Event &event) override 47 { 48 DetectPluginManager::getInstance().DispatchEvent(event); 49 return SecurityGuard::SUCCESS; 50 }; 51 }; 52 53 class DetectPluginAttrs { 54 public: 55 DetectPluginAttrs() = default; ~DetectPluginAttrs()56 ~DetectPluginAttrs() 57 { 58 SGLOGI("~DetectPluginAttrs"); 59 if (instance_ != nullptr) { 60 instance_->Destroy(); 61 delete instance_; 62 instance_ = nullptr; 63 } 64 if (handle_ != nullptr) { 65 dlclose(handle_); 66 handle_ = nullptr; 67 } 68 }; 69 SetHandle(void * handle)70 void SetHandle(void *handle) { handle_ = handle; }; SetInstance(IDetectPlugin * instance)71 void SetInstance(IDetectPlugin *instance) { instance_ = instance; }; SetPluginName(std::string pluginName)72 void SetPluginName(std::string pluginName) { pluginName_ = pluginName; }; GetHandle()73 void *GetHandle() { return handle_; }; GetInstance()74 IDetectPlugin *GetInstance() { return instance_; }; GetPluginName()75 std::string GetPluginName() { return pluginName_; }; 76 77 private: 78 void *handle_; 79 IDetectPlugin *instance_; 80 std::string pluginName_; 81 }; 82 83 struct PluginCfg { 84 std::string pluginName; 85 std::string pluginPath; 86 std::unordered_set<int64_t> depEventIds; 87 std::string version; 88 }; 89 90 std::vector<PluginCfg> plugins_; 91 std::unordered_map<int64_t, std::vector<std::shared_ptr<DetectPluginAttrs>>> eventIdMap_; 92 std::unordered_set<int64_t> failedEventIdset_; 93 bool isFailedEventStartRetry_ = false; 94 DetectPluginManager() = default; 95 ~DetectPluginManager() = default; 96 void LoadPlugin(const PluginCfg &pluginCfg); 97 void SubscribeEvent(int64_t eventId); 98 void RetrySubscriptionTask(); 99 bool ParsePluginConfig(const std::string &fileName); 100 void ParsePluginConfigObjArray(const cJSON *plugins); 101 bool CheckPluginNameAndSize(PluginCfg &newPlugin); 102 bool ParsePluginDepEventIds(const cJSON *plugin, std::unordered_set<int64_t> &depEventIds); 103 std::string AssembleMetadata(const SecurityCollector::Event &event); 104 ffrt::mutex mutex_; 105 }; 106 } // namespace OHOS::Security::SecurityGuard 107 #endif