1 /* 2 * Copyright (c) 2022 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 RESSCHED_SERVICES_RESSCHEDMGR_RESSCHEDFWK_INCLUDE_PLUGIN_MGR_H 17 #define RESSCHED_SERVICES_RESSCHEDMGR_RESSCHEDFWK_INCLUDE_PLUGIN_MGR_H 18 19 #include <functional> 20 #include <list> 21 #include <string> 22 #include <memory> 23 #include <map> 24 #include "datetime_ex.h" 25 #include "event_handler.h" 26 #include "config_reader.h" 27 #include "plugin_switch.h" 28 #include "plugin.h" 29 #include "nocopyable.h" 30 #include "res_data.h" 31 #include "res_type.h" 32 #include "single_instance.h" 33 #include "config_info.h" 34 35 namespace OHOS { 36 namespace ResourceSchedule { 37 using Clock = std::chrono::high_resolution_clock; 38 using TimePoint = std::chrono::time_point<Clock>; 39 using OnPluginInitFunc = bool (*)(const std::string); 40 using OnDispatchResourceFunc = void (*)(const std::shared_ptr<ResData>&); 41 using OnDumpFunc = void (*)(const std::vector<std::string>&, std::string&); 42 using OnPluginDisableFunc = void (*)(); 43 44 struct PluginLib { 45 std::shared_ptr<void> handle = nullptr; 46 OnPluginInitFunc onPluginInitFunc_; 47 OnDispatchResourceFunc onDispatchResourceFunc_; 48 OnDumpFunc onDumpFunc_; 49 OnPluginDisableFunc onPluginDisableFunc_; 50 }; 51 52 class PluginMgr { 53 DECLARE_SINGLE_INSTANCE_BASE(PluginMgr); 54 55 public: 56 ~PluginMgr(); 57 58 /** 59 * Init pluginmanager, load xml config file, construct plugin instances. 60 */ 61 void Init(); 62 63 /** 64 * Disable all plugins, maybe service exception happens or stopped. 65 */ 66 void Stop(); 67 68 /** 69 * receive all reported resource data, then dispatch all plugins. 70 * 71 * @param resData Reported resource data. 72 */ 73 void DispatchResource(const std::shared_ptr<ResData>& resData); 74 75 /** 76 * Subscribe resource type from plugin. 77 * 78 * @param pluginLib The lib name of plugin. 79 * @param resType interested in resource type. 80 */ 81 void SubscribeResource(const std::string& pluginLib, uint32_t resType); 82 83 /** 84 * Unsubscribe resource type from plugin. 85 * 86 * @param pluginLib The lib name of plugin. 87 * @param resType interested in resource type. 88 */ 89 void UnSubscribeResource(const std::string& pluginLib, uint32_t resType); 90 91 void DumpAllPlugin(std::string &result); 92 93 void DumpOnePlugin(std::string &result, std::string pluginName, std::vector<std::string>& args); 94 95 std::string DumpInfoFromPlugin(std::string& result, std::string libPath, std::vector<std::string>& args); 96 97 void DumpHelpFromPlugin(std::string& result); 98 99 PluginConfig GetConfig(const std::string& pluginName, const std::string& configName); 100 101 private: 102 PluginMgr() = default; 103 std::string GetRealConfigPath(const char* configName); 104 void OnDestroy(); 105 void LoadPlugin(); 106 std::shared_ptr<PluginLib> LoadOnePlugin(const PluginInfo& info); 107 void UnLoadPlugin(); 108 void ClearResource(); 109 void deliverResourceToPlugin(const std::string& pluginLib, const std::shared_ptr<ResData>& resData); 110 void RepairPlugin(TimePoint endTime, const std::string& pluginLib, PluginLib libInfo); 111 void RemoveDisablePluginHandler(); 112 void DumpPluginInfoAppend(std::string &result, PluginInfo info); 113 114 // plugin crash 3 times in 60s, will be disable forever 115 const int32_t MAX_PLUGIN_TIMEOUT_TIMES = 3; 116 const int32_t DISABLE_PLUGIN_TIME = 60000; 117 const int32_t DUMP_ONE_STRING_SIZE = 32; 118 std::unique_ptr<ConfigReader> configReader_ = nullptr; 119 std::unique_ptr<PluginSwitch> pluginSwitch_ = nullptr; 120 121 std::mutex pluginMutex_; 122 std::mutex dispatcherHandlerMutex_; 123 std::map<std::string, PluginLib> pluginLibMap_; 124 125 // mutex for resTypeMap_ 126 std::mutex resTypeMutex_; 127 std::map<uint32_t, std::list<std::string>> resTypeLibMap_; 128 129 // handler map use for dispatch resource data 130 std::map<std::string, std::shared_ptr<OHOS::AppExecFwk::EventHandler>> dispatcherHandlerMap_; 131 int32_t handlerNum_ = 0; 132 std::map<std::string, std::list<TimePoint>> pluginTimeoutTime_; 133 std::list<std::string> disablePlugins_; 134 std::mutex disablePluginsMutex_; 135 }; 136 } // namespace ResourceSchedule 137 } // namespace OHOS 138 139 #endif // RESSCHED_SERVICES_RESSCHEDMGR_RESSCHEDFWK_INCLUDE_PLUGIN_MGR_H 140