1 /* 2 * Copyright (c) 2022-2024 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 SERVICES_EDM_INCLUDE_EDM_PLUGIN_MANAGER_H 17 #define SERVICES_EDM_INCLUDE_EDM_PLUGIN_MANAGER_H 18 19 #include <chrono> 20 #include <condition_variable> 21 #include <map> 22 #include <memory> 23 #include <shared_mutex> 24 #include <thread> 25 26 #include "enhance_execute_strategy.h" 27 #include "iplugin.h" 28 #include "iplugin_execute_strategy.h" 29 #include "iplugin_manager.h" 30 #include "replace_execute_strategy.h" 31 #include "single_execute_strategy.h" 32 33 namespace OHOS { 34 namespace EDM { 35 struct SoLoadState { 36 bool pluginHasInit = false; 37 std::mutex waitMutex; 38 bool notifySignal = false; 39 std::condition_variable waitSignal; 40 std::chrono::system_clock::time_point lastCallTime; 41 void* pluginHandles; 42 }; 43 namespace SONAME { 44 const char* const DEVICE_CORE_PLUGIN_SO = "libdevice_core_plugin.z.so"; 45 const char* const COMMUNICATION_PLUGIN_SO = "libcommunication_plugin.z.so"; 46 const char* const SYS_SERVICE_PLUGIN_SO = "libsys_service_plugin.z.so"; 47 const char* const NEED_EXTRA_PLUGIN_SO = "libneed_extra_plugin.z.so"; 48 const char* const OLD_EDM_PLUGIN_SO = "libedmplugin.z.so"; 49 } 50 51 class PluginManager : public std::enable_shared_from_this<PluginManager>, IPluginManager { 52 public: 53 static std::shared_ptr<PluginManager> GetInstance(); 54 std::shared_ptr<IPlugin> GetPluginByFuncCode(std::uint32_t funcCode); 55 std::shared_ptr<IPlugin> GetPluginByPolicyName(const std::string &policyName); 56 std::shared_ptr<IPlugin> GetPluginByCode(std::uint32_t code); 57 bool AddPlugin(std::shared_ptr<IPlugin> plugin) override; 58 bool AddExtensionPlugin(std::shared_ptr<IPlugin> extensionPlugin, uint32_t basicPluginCode, 59 ExecuteStrategy strategy) override; 60 virtual ~PluginManager(); 61 62 void LoadAllPlugin(); 63 ErrCode LoadPluginByCode(uint32_t code); 64 ErrCode LoadPluginByFuncCode(uint32_t funcCode); 65 void DumpPlugin(); 66 void NotifyUnloadAllPlugin(); 67 68 private: 69 inline static std::map<std::uint32_t, std::shared_ptr<IPlugin>> pluginsCode_; 70 inline static std::map<std::string, std::shared_ptr<IPlugin>> pluginsName_; 71 inline static std::map<std::uint32_t, std::uint32_t> extensionPluginMap_; 72 inline static std::map<std::uint32_t, ExecuteStrategy> executeStrategyMap_; 73 inline static std::unordered_map<std::string, std::shared_ptr<SoLoadState>> soLoadStateMap_; 74 static std::vector<uint32_t> deviceCoreSoCodes_; 75 static std::vector<uint32_t> communicationSoCodes_; 76 static std::vector<uint32_t> sysServiceSoCodes_; 77 static std::vector<uint32_t> needExtraSoCodes_; 78 79 static std::shared_timed_mutex mutexLock_; 80 static std::shared_ptr<PluginManager> instance_; 81 PluginManager(); 82 void DlopenPlugin(const std::string &pluginPath, std::shared_ptr<SoLoadState> loadStatePtr); 83 void LoadPlugin(const std::string &soName); 84 void UnloadPlugin(const std::string &soName); 85 void LoadExtraPlugin(); 86 bool IsExtraPlugin(const std::string &soName); 87 void UnloadPluginTask(const std::string &soName, std::shared_ptr<SoLoadState> loadStatePtr); 88 void RemovePlugin(std::shared_ptr<IPlugin> plugin); 89 bool GetSoNameByCode(std::uint32_t code, std::string &soName); 90 void GetExtraPluginCodeList(std::vector<uint32_t>* targetVec); 91 bool AddPluginInner(std::shared_ptr<IPlugin> plugin); 92 void DumpPluginInner(std::map<std::uint32_t, std::shared_ptr<IPlugin>> pluginsCode, 93 std::map<std::string, std::shared_ptr<IPlugin>> pluginsName); 94 void DumpPluginConfig(IPlugin::PolicyPermissionConfig config); 95 std::shared_ptr<IPluginExecuteStrategy> CreateExecuteStrategy(ExecuteStrategy strategy); 96 std::shared_ptr<IPluginExecuteStrategy> enhanceStrategy_ = std::make_shared<EnhanceExecuteStrategy>(); 97 std::shared_ptr<IPluginExecuteStrategy> singleStrategy_ = std::make_shared<SingleExecuteStrategy>(); 98 std::shared_ptr<IPluginExecuteStrategy> replaceStrategy_ = std::make_shared<ReplaceExecuteStrategy>(); 99 }; 100 } // namespace EDM 101 } // namespace OHOS 102 103 #endif // SERVICES_EDM_INCLUDE_EDM_PLUGIN_MANAGER_H 104