adf6563c7de23f46db2a24a4db61982a6bccdd39
[platform/framework/web/wrt-plugins-common.git] / src / plugins-api-support / PluginRegistry.cpp
1 /*
2  * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @file    PluginRegistry.h
18  * @author  Grzegorz Krawczyk (g.krawczyk@samgsung.com)
19  * @version
20  * @brief
21  */
22 #include "PluginRegistry.h"
23 #include "PluginRegistration.h"
24 #include "PluginRegistrationImpl.h"
25 #include "ExportedApi.h"
26
27 #include <dlfcn.h>
28 #include <cstdio>
29 #include <cstdlib>
30 #include <string>
31 #include <algorithm>
32
33 #include <dpl/log/log.h>
34 #include <dpl/foreach.h>
35
36 namespace WrtPluginsApi {
37 void PluginRegistry::AddPlugin(const std::string& libraryName,
38                                Plugin& plugin)
39 {
40     LogDebug("Adding plugin for library: " << libraryName);
41
42     auto libraryIt = m_plugins.find(libraryName);
43     if (m_plugins.end() == libraryIt) {
44         m_plugins[libraryName] = &plugin;
45     }
46 }
47
48 Plugin* PluginRegistry::GetPlugin(const std::string& libraryName)
49 {
50     auto it = m_plugins.find(libraryName);
51     if (it == m_plugins.end()) {
52         if (!LoadFromFile(libraryName)) {
53             LogError("Failed to load lib" << libraryName);
54             ThrowMsg(PluginNotFound, "Failed to load plugin");
55         }
56
57         return m_plugins[libraryName];
58     }
59
60     return it->second;
61 }
62
63 void PluginRegistry::RemovePlugin(const std::string& libraryName,
64                                   Plugin& plugin)
65 {
66     auto it = m_plugins.find(libraryName);
67     if (it != m_plugins.end()) {
68         if (&plugin == it->second) {
69             m_plugins.erase(it);
70         }
71     }
72 }
73
74 void PluginRegistry::UnloadAll()
75 {
76     LogDebug("Unload all plugins");
77
78     typedef void (*UnregisterFunction)(PluginRegistration&, Plugin&);
79
80     FOREACH(libraryIt, m_libraries)
81     {
82         auto pluginIt = m_plugins.find(libraryIt->first);
83         if (m_plugins.end() != pluginIt) {
84             void* handle = dlopen(libraryIt->first.c_str(), RTLD_NOW);
85             if (!handle) {
86                 const char* error = (const char*)dlerror();
87                 LogError("Error: " << (error != NULL ? error : "unknown"));
88                 continue;
89             }
90
91             ExportedApi* entryPoint =
92                 static_cast<ExportedApi*>
93                 (dlsym(handle, GetExportedSymbolName()));
94             if (NULL == entryPoint) {
95                 const char* error = (const char*)dlerror();
96                 LogError("Error: " << (error != NULL ? error : "unknown"));
97                 continue;
98             }
99             if (entryPoint->Unregister == NULL) {
100                 LogError("Error Unregister function not set");
101                 continue;
102             }
103
104             PluginRegistration registration(
105                 new PluginRegistration::Impl(*this, libraryIt->first));
106
107             entryPoint->Unregister(registration, (pluginIt->second));
108
109             m_plugins.erase(pluginIt);
110         }
111         dlclose(libraryIt->second);
112     }
113 }
114
115 bool PluginRegistry::LoadFromFile(const std::string& libraryName)
116 {
117     void* handle = dlopen(libraryName.c_str(), RTLD_NOW);
118     if (!handle) {
119         const char* error = (const char*)dlerror();
120         LogError("Error: " << (error != NULL ? error : "unknown"));
121         return false;
122     }
123     m_libraries[libraryName] = handle;
124
125     ExportedApi* entryPoint =
126         static_cast<ExportedApi*>(dlsym(handle, GetExportedSymbolName()));
127     if (NULL == entryPoint) {
128         const char* error = (const char*)dlerror();
129         LogError("Error: " << (error != NULL ? error : "unknown"));
130         return false;
131     }
132
133     if (entryPoint->Register == NULL) {
134         LogError("Error Register function not set");
135         return false;
136     }
137     if (entryPoint->Unregister == NULL) {
138         LogError("Error Unregister function not set");
139         return false;
140     }
141
142     PluginRegistration registration(
143         new PluginRegistration::Impl(*this, libraryName));
144     entryPoint->Register(registration);
145
146     return true;
147 }
148
149 PluginRegistry::~PluginRegistry()
150 {
151     //TODO discuss ... when the unload should be called
152     //    UnloadAll();
153 }
154 }