/* * Copyright (c) 2021-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef HIVIEW_BASE_PLUGIN_PLATFORM_H #define HIVIEW_BASE_PLUGIN_PLATFORM_H #include #include #include #include "defines.h" #include "dynamic_module.h" #include "event_dispatch_queue.h" #include "event_loop.h" #include "event_source.h" #include "pipeline.h" #include "plugin.h" #include "plugin_bundle.h" #include "plugin_config.h" #include "singleton.h" namespace OHOS { namespace HiviewDFX { using PipelineConfigMap = std::map>; class HiviewPlatform : public HiviewContext, public Singleton { public: HiviewPlatform(); ~HiviewPlatform(); bool InitEnvironment(const std::string& platformConfigDir = ""); void ProcessArgsRequest(int argc, char* argv[]); void StartLoop(); void SetMaxProxyIdleTime(time_t idleTime); void SetCheckProxyIdlePeriod(time_t period); void PostUnorderedEvent(std::shared_ptr plugin, std::shared_ptr event) override; void RegisterUnorderedEventListener(std::weak_ptr listener) override; bool PostSyncEventToTarget(std::shared_ptr caller, const std::string& calleeName, std::shared_ptr event) override; void PostAsyncEventToTarget(std::shared_ptr caller, const std::string& calleeName, std::shared_ptr event) override; void RequestUnloadPlugin(std::shared_ptr caller) override; std::list> GetPipelineSequenceByName(const std::string& name) override; std::shared_ptr GetSharedWorkLoop() override; std::string GetHiViewDirectory(DirectoryType type) override; std::string GetHiviewProperty(const std::string& key, const std::string& defaultValue) override; bool SetHiviewProperty(const std::string& key, const std::string& value, bool forceUpdate) override; bool IsReady() override; void AppendPluginToPipeline(const std::string& pluginName, const std::string& pipelineName) override; void RequestLoadBundle(const std::string& bundleName __UNUSED) override; void RequestUnloadBundle(const std::string& bundleName, uint64_t delay = 0) override; std::shared_ptr InstancePluginByProxy(std::shared_ptr proxy) override; std::shared_ptr GetPluginByName(const std::string& name) override; void AddDispatchInfo(std::weak_ptr plugin, const std::unordered_set& types, const std::unordered_set& eventNames, const std::unordered_set& tags, const std::unordered_map& domainRulesMap) override; std::vector> GetDisPatcherInfo(uint32_t type, const std::string& eventName, const std::string& tag, const std::string& domain) override; void AddListenerInfo(uint32_t type, const std::string& name, const std::set& eventNames, const std::map& domainRulesMap) override; void AddListenerInfo(uint32_t type, const std::string& name) override; std::vector> GetListenerInfo(uint32_t type, const std::string& eventName, const std::string& domain) override; PipelineConfigMap& GetPipelineConfigMap() { return pipelineRules_; } const std::map>& GetPluginMap() { return pluginMap_; } const std::map>& GetPipelineMap() { return pipelines_; } const std::map>& GetWorkLoopMap() { return privateWorkLoopMap_; } const std::map>& GetPluginBundleInfoMap() { return pluginBundleInfos_; } private: struct ListenerInfo { std::weak_ptr listener_; std::vector messageTypes_; std::map> eventsInfo_; std::map> domainsInfo_; bool Match(uint32_t type, const std::string& eventName, const std::string& domain) { auto it = std::find(messageTypes_.begin(), messageTypes_.end(), type); if (it != messageTypes_.end()) { return true; } auto itEventList = eventsInfo_.find(type); if (itEventList != eventsInfo_.end()) { auto eventList = itEventList->second; if (eventList.find(eventName) != eventList.end()) { return true; } } auto itDomainsInfo = domainsInfo_.find(type); if (itDomainsInfo != domainsInfo_.end()) { auto itDomainRule = itDomainsInfo->second.find(domain); if (itDomainRule != itDomainsInfo->second.end()) { return itDomainRule->second.FindEvent(eventName); } } return false; } }; struct DispatchInfo { std::weak_ptr plugin_; std::unordered_set typesInfo_; std::unordered_set eventsInfo_; std::unordered_set tagsInfo_; std::unordered_map domainsInfo_; bool Match(uint8_t type, const std::string& eventName, const std::string& tag, const std::string& domain) { if (typesInfo_.find(type) != typesInfo_.end()) { return true; } if (tagsInfo_.find(tag) != tagsInfo_.end()) { return true; } if (eventsInfo_.find(eventName) != eventsInfo_.end()) { return true; } auto itDomainRule = domainsInfo_.find(domain); if (itDomainRule != domainsInfo_.end()) { return itDomainRule->second.FindEvent(eventName); } return false; } }; void CreateWorkingDirectories(const std::string& platformConfigDir); void StartPlatformDispatchQueue(); void CreatePlugin(const PluginConfig::PluginInfo& pluginInfo); void CreatePipeline(const PluginConfig::PipelineInfo& pipelineInfo); void InitPlugin(const PluginConfig& config __UNUSED, const PluginConfig::PluginInfo& pluginInfo); void NotifyPluginReady(); void ScheduleCreateAndInitPlugin(const PluginConfig::PluginInfo& pluginInfo); DynamicModule LoadDynamicPlugin(const std::string& name) const; std::string GetDynamicLibName(const std::string& name, bool hasOhosSuffix) const; std::shared_ptr GetAvailableWorkLoop(const std::string& name); void CleanupUnusedResources(); void UnloadPlugin(const std::string& name); void StartEventSource(std::shared_ptr source); void ValidateAndCreateDirectory(std::string& defaultPath, const std::string& realPath); void ValidateAndCreateDirectories(const std::string& localPath, const std::string& workPath, const std::string& persistPath); void LoadBusinessPlugin(const PluginConfig& config); void ExitHiviewIfNeed(); std::string GetPluginConfigPath(); std::string SplitBundleNameFromPath(const std::string& filePath); void UpdateBetaConfigIfNeed(); void LoadPluginBundles(); void LoadPluginBundle(const std::string& bundleName, const std::string& filePath); void ScheduleCheckUnloadablePlugins(); void CheckUnloadablePlugins(); std::string SearchPluginBundle(const std::string& name) const; void AddWatchDog(); void SaveStack(); bool isReady_; std::string defaultConfigDir_; std::string defaultWorkDir_; std::string defaultCommercialWorkDir_; std::string defaultPersistDir_; std::string defaultConfigName_; std::vector dynamicLibSearchDir_; std::shared_ptr unorderQueue_; std::shared_ptr sharedWorkLoop_; std::map> pluginMap_; std::map> pipelines_; std::map> privateWorkLoopMap_; std::map hiviewProperty_; std::map> pluginBundleInfos_; // Listener data structure:> std::unordered_map> listeners_; std::unordered_map> dispatchers_; PipelineConfigMap pipelineRules_; std::vector> eventSourceList_; // the max waited time before destroy plugin instance const time_t DEFAULT_IDLE_TIME = 300; // 300 seconds time_t maxIdleTime_ = DEFAULT_IDLE_TIME; time_t checkIdlePeriod_ = DEFAULT_IDLE_TIME / 2; // 2 : half idle time int watchDogTimer_ = 0; bool hasDumpStack_ = false; }; } // namespace HiviewDFX } // namespace OHOS #endif // HIVIEW_BASE_PLUGIN_PLATFORM_H