1 /*
2 * Copyright (c) 2021-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 #include "plugin_factory.h"
16
17 #include "hiview_logger.h"
18 namespace OHOS {
19 namespace HiviewDFX {
20 DEFINE_LOG_TAG("HiView-PluginFactory");
GetGlobalPluginRegistryMap()21 std::shared_ptr<std::unordered_map<std::string, std::shared_ptr<PluginRegistInfo>>> PluginFactory::GetGlobalPluginRegistryMap()
22 {
23 static std::shared_ptr<std::unordered_map<std::string, std::shared_ptr<PluginRegistInfo>>> pluginMap;
24 if (pluginMap == nullptr) {
25 pluginMap = std::make_shared<std::unordered_map<std::string, std::shared_ptr<PluginRegistInfo>>>();
26 }
27 return pluginMap;
28 }
29
GetGlobalPluginInfo(const std::string & name)30 std::shared_ptr<PluginRegistInfo> PluginFactory::GetGlobalPluginInfo(const std::string& name)
31 {
32 auto map = GetGlobalPluginRegistryMap();
33 auto it = map->find(name);
34 if (it != map->end()) {
35 return it->second;
36 }
37 return nullptr;
38 }
39
GetPlugin(const std::string & name)40 std::shared_ptr<Plugin> PluginFactory::GetPlugin(const std::string& name)
41 {
42 auto info = GetGlobalPluginInfo(name);
43 if (info != nullptr) {
44 return info->getPluginObject();
45 }
46 return nullptr;
47 }
48
RegisterPlugin(const std::string & name,std::shared_ptr<PluginRegistInfo> func)49 void PluginFactory::RegisterPlugin(const std::string& name, std::shared_ptr<PluginRegistInfo> func)
50 {
51 if (func->getPluginObject == nullptr) {
52 HIVIEW_LOGW("Register null plugin constructor from %{public}s.", name.c_str());
53 return;
54 }
55 // force update plugin constructor
56 auto pluginMap = GetGlobalPluginRegistryMap();
57 if (pluginMap->find(name) == pluginMap->end()) {
58 pluginMap->insert(std::pair<std::string, std::shared_ptr<PluginRegistInfo>>(name, func));
59 } else {
60 HIVIEW_LOGW("plugin %{public}s already exists! register plugin failed", name.c_str());
61 }
62 #ifdef _WIN32
63 // When PluginFactory is loading, the logger is not loaded.
64 #else
65 HIVIEW_LOGD("Register plugin constructor from %{public}s.", name.c_str());
66 #endif // _WIN32
67 }
68
UnregisterPlugin(const std::string & name)69 void PluginFactory::UnregisterPlugin(const std::string& name)
70 {
71 HIVIEW_LOGI("Unregister plugin from %{public}s.", name.c_str());
72 auto pluginMap = GetGlobalPluginRegistryMap();
73 if (pluginMap->find(name) == pluginMap->end()) {
74 HIVIEW_LOGW("plugin %{public}s does not exist!", name.c_str());
75 return;
76 }
77 pluginMap->erase(name);
78 }
79 } // namespace HiviewDFX
80 } // namespace OHOS
81