tizen 2.4 release
[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/log/secure_log.h>
35 #include <dpl/foreach.h>
36 #include <dpl/scope_guard.h>
37
38 namespace WrtPluginsApi {
39 void PluginRegistry::AddPlugin(const std::string& libraryName,
40                                Plugin& plugin)
41 {
42     LogDebug("Adding plugin for library: " << libraryName);
43
44     auto libraryIt = m_plugins.find(libraryName);
45     if (m_plugins.end() == libraryIt) {
46         m_plugins[libraryName] = &plugin;
47     }
48 }
49
50 Plugin* PluginRegistry::GetPlugin(const std::string& libraryName)
51 {
52     auto it = m_plugins.find(libraryName);
53     if (it == m_plugins.end()) {
54         if (!LoadFromFile(libraryName)) {
55             LogError("Failed to load lib" << libraryName);
56             ThrowMsg(PluginNotFound, "Failed to load plugin");
57         }
58
59         return m_plugins[libraryName];
60     }
61
62     return it->second;
63 }
64
65 void PluginRegistry::RemovePlugin(const std::string& libraryName,
66                                   Plugin& plugin)
67 {
68     auto it = m_plugins.find(libraryName);
69     if (it != m_plugins.end()) {
70         if (&plugin == it->second) {
71             m_plugins.erase(it);
72         }
73     }
74 }
75
76 void PluginRegistry::UnloadAll()
77 {
78     LogDebug("Unload all plugins");
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             DPL_SCOPE_EXIT(handle) {
91                 if (dlclose(handle) != 0) {
92                     const char* error = dlerror();
93                     if (error != NULL)
94                     {
95                         std::string errstr{error};
96                         _E("%s", errstr.c_str());
97                     }
98                     else
99                     {
100                         _E("unknown error while closing plug-in library");
101                     }
102                 }
103             };
104
105             ExportedApi* entryPoint =
106                 static_cast<ExportedApi*>
107                 (dlsym(handle, GetExportedSymbolName()));
108             if (NULL == entryPoint) {
109                 const char* error = (const char*)dlerror();
110                 LogError("Error: " << (error != NULL ? error : "unknown"));
111                 continue;
112             }
113             if (entryPoint->Unregister == NULL) {
114                 LogError("Error Unregister function not set");
115                 continue;
116             }
117
118             PluginRegistration registration(
119                 new PluginRegistration::Impl(*this, libraryIt->first));
120
121             entryPoint->Unregister(registration, (pluginIt->second));
122
123             m_plugins.erase(pluginIt);
124         }
125         dlclose(libraryIt->second);
126     }
127 }
128
129 bool PluginRegistry::LoadFromFile(const std::string& libraryName)
130 {
131     void* handle = dlopen(libraryName.c_str(), RTLD_NOW);
132     if (!handle) {
133         const char* error = (const char*)dlerror();
134         LogError("Error: " << (error != NULL ? error : "unknown"));
135         return false;
136     }
137     m_libraries[libraryName] = handle;
138
139     ExportedApi* entryPoint =
140         static_cast<ExportedApi*>(dlsym(handle, GetExportedSymbolName()));
141     if (NULL == entryPoint) {
142         const char* error = (const char*)dlerror();
143         LogError("Error: " << (error != NULL ? error : "unknown"));
144         return false;
145     }
146
147     if (entryPoint->Register == NULL) {
148         LogError("Error Register function not set");
149         return false;
150     }
151     if (entryPoint->Unregister == NULL) {
152         LogError("Error Unregister function not set");
153         return false;
154     }
155
156     PluginRegistration registration(
157         new PluginRegistration::Impl(*this, libraryName));
158     entryPoint->Register(registration);
159
160     return true;
161 }
162
163 PluginRegistry::~PluginRegistry()
164 {
165     //TODO discuss ... when the unload should be called
166     //    UnloadAll();
167 }
168 }