1 /* 2 * Copyright (c) 2021-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 #ifndef HIVIEW_BASE_PLUGIN_PLATFORM_H 16 #define HIVIEW_BASE_PLUGIN_PLATFORM_H 17 #include <memory> 18 #include <mutex> 19 #include <string> 20 21 #include "defines.h" 22 #include "dynamic_module.h" 23 #include "event_dispatch_queue.h" 24 #include "event_loop.h" 25 #include "event_source.h" 26 #include "pipeline.h" 27 #include "plugin.h" 28 #include "plugin_bundle.h" 29 #include "plugin_config.h" 30 #include "plugin_extra_info.h" 31 32 #include "singleton.h" 33 namespace OHOS { 34 namespace HiviewDFX { 35 class HiviewPlatform : public HiviewContext, public Singleton<HiviewPlatform> { 36 public: 37 HiviewPlatform(); 38 ~HiviewPlatform(); 39 bool InitEnvironment(const std::string& platformConfigDir = ""); 40 void ProcessArgsRequest(int argc, char* argv[]); 41 void StartLoop(); 42 void SetMaxProxyIdleTime(time_t idleTime); 43 void SetCheckProxyIdlePeriod(time_t period); 44 45 void PostUnorderedEvent(std::shared_ptr<Plugin> plugin, std::shared_ptr<Event> event) override; 46 void RegisterUnorderedEventListener(std::weak_ptr<EventListener> listener) override; 47 void RegisterDynamicListenerInfo(std::weak_ptr<Plugin> listener) override; 48 bool PostSyncEventToTarget(std::shared_ptr<Plugin> caller, const std::string& calleeName, 49 std::shared_ptr<Event> event) override; 50 void PostAsyncEventToTarget(std::shared_ptr<Plugin> caller, const std::string& calleeName, 51 std::shared_ptr<Event> event) override; 52 void RequestUnloadPlugin(std::shared_ptr<Plugin> caller) override; 53 std::list<std::weak_ptr<Plugin>> GetPipelineSequenceByName(const std::string& name) override; 54 std::shared_ptr<EventLoop> GetSharedWorkLoop() override; 55 std::string GetHiViewDirectory(DirectoryType type) override; 56 std::string GetHiviewProperty(const std::string& key, const std::string& defaultValue) override; 57 bool SetHiviewProperty(const std::string& key, const std::string& value, bool forceUpdate) override; 58 void PublishPluginCapacity(PluginCapacityInfo &pluginCapacityInfo) override; 59 void GetRemoteByCapacity(const std::string& plugin, const std::string& capacity, 60 std::list<std::string> &deviceIdList) override; 61 int32_t PostEventToRemote(std::shared_ptr<Plugin> caller, const std::string& deviceId, 62 const std::string& targetPlugin, std::shared_ptr<Event> event) override; 63 bool IsReady() override; 64 void AppendPluginToPipeline(const std::string& pluginName, const std::string& pipelineName) override; 65 void RequestLoadBundle(const std::string& bundleName __UNUSED) override; 66 std::shared_ptr<Plugin> InstancePluginByProxy(std::shared_ptr<Plugin> proxy) override; 67 std::shared_ptr<Plugin> GetPluginByName(const std::string& name) override; 68 void AddListenerInfo(uint32_t type, std::weak_ptr<Plugin> plugin, 69 const std::set<std::string>& eventNames, const std::set<EventListener::EventIdRange>& listenerInfo) override; 70 void AddListenerInfo(uint32_t type, const std::string& name, 71 const std::set<std::string>& eventNames, const std::set<EventListener::EventIdRange>& listenerInfo) override; 72 std::vector<std::weak_ptr<InstanceInfo>> GetListenerInfo(uint32_t type, 73 const std::string& eventNames, uint32_t eventId) override; 74 bool GetListenerInfo(uint32_t type, const std::string& name, 75 std::set<EventListener::EventIdRange> &listenerInfo) override; 76 bool GetListenerInfo(uint32_t type, const std::string& name, std::set<std::string> &eventNames) override; 77 GetPluginMap()78 const std::map<std::string, std::shared_ptr<Plugin>>& GetPluginMap() 79 { 80 return pluginMap_; 81 } 82 GetPipelineMap()83 const std::map<std::string, std::shared_ptr<Pipeline>>& GetPipelineMap() 84 { 85 return pipelines_; 86 } 87 GetWorkLoopMap()88 const std::map<std::string, std::shared_ptr<EventLoop>>& GetWorkLoopMap() 89 { 90 return privateWorkLoopMap_; 91 } 92 GetPluginBundleInfoMap()93 const std::map<std::string, std::shared_ptr<PluginBundle>>& GetPluginBundleInfoMap() 94 { 95 return pluginBundleInfos_; 96 } 97 98 private: 99 struct ListenerInfo { 100 std::shared_ptr<InstanceInfo> instanceInfo; 101 std::map<uint32_t, std::set<EventListener::EventIdRange>> idListenerInfo; 102 std::map<uint32_t, std::set<std::string>> strListenerInfo; 103 }; 104 105 void StartPlatformDispatchQueue(); 106 void CreatePlugin(const PluginConfig::PluginInfo& pluginInfo); 107 void CreatePipeline(const PluginConfig::PipelineInfo& pipelineInfo); 108 void InitPlugin(const PluginConfig& config __UNUSED, const PluginConfig::PluginInfo& pluginInfo); 109 void NotifyPluginReady(); 110 void ScheduleCreateAndInitPlugin(const PluginConfig::PluginInfo& pluginInfo); 111 DynamicModule LoadDynamicPlugin(const std::string& name) const; 112 std::string GetDynamicLibName(const std::string& name, bool hasOhosSuffix) const; 113 std::shared_ptr<EventLoop> GetAvaliableWorkLoop(const std::string& name); 114 void CleanupUnusedResources(); 115 void UnloadPlugin(const std::string& name); 116 void StartEventSource(std::shared_ptr<EventSource> source); 117 void ValidateAndCreateDirectory(std::string& defaultPath, const std::string& realPath); 118 void ValidateAndCreateDirectories(const std::string& localPath, const std::string& workPath, 119 const std::string& persistPath); 120 void LoadBusinessPlugin(const PluginConfig& config); 121 void ExitHiviewIfNeed(); 122 std::string GetPluginConfigPath(); 123 std::string SplitBundleNameFromPath(const std::string& filePath); 124 void UpdateBetaConfigIfNeed(); 125 void LoadPluginBundles(); 126 void LoadPluginBundle(const std::string& bundleName, const std::string& filePath); 127 void ScheduleCheckUnloadablePlugins(); 128 void CheckUnloadablePlugins(); 129 std::string SearchPluginBundle(const std::string& name) const; 130 131 bool isReady_; 132 std::string defaultConfigDir_; 133 std::string defaultWorkDir_; 134 std::string defaultCommercialWorkDir_; 135 std::string defaultPersistDir_; 136 std::string defaultConfigName_; 137 std::vector<std::string> dynamicLibSearchDir_; 138 std::unique_ptr<EventDispatchQueue> unorderQueue_; 139 std::shared_ptr<EventLoop> sharedWorkLoop_; 140 std::map<std::string, std::shared_ptr<Plugin>> pluginMap_; 141 std::map<std::string, std::shared_ptr<Pipeline>> pipelines_; 142 std::map<std::string, std::shared_ptr<EventLoop>> privateWorkLoopMap_; 143 std::map<std::string, std::string> hiviewProperty_; 144 std::map<std::string, std::shared_ptr<PluginBundle>> pluginBundleInfos_; 145 146 // Listener data structure:<pluginName, <domain_eventName, Plugin>> 147 std::unordered_map<std::string, std::shared_ptr<ListenerInfo>> listeners_; 148 std::vector<std::shared_ptr<Plugin>> eventSourceList_; 149 150 // the max waited time before destroy plugin instance 151 const time_t DEFAULT_IDLE_TIME = 300; // 300 seconds 152 time_t maxIdleTime_ = DEFAULT_IDLE_TIME; 153 time_t checkIdlePeriod_ = DEFAULT_IDLE_TIME / 2; // 2 : half idle time 154 }; 155 } // namespace HiviewDFX 156 } // namespace OHOS 157 #endif // HIVIEW_BASE_PLUGIN_PLATFORM_H 158