Update wrt-plugins-common_0.3.53
[framework/web/wrt-plugins-common.git] / src / plugin-loading / plugin.cpp
1 /*
2  * Copyright (c) 2011 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        plugin.cpp
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of plugin
21  */
22 #include "plugin.h"
23 #include <dpl/log/log.h>
24 #include <dpl/assert.h>
25 #include <dlfcn.h>
26
27 Plugin::Plugin(const std::string &fileName,
28         void *libHandle,
29         on_widget_start_proc *apiOnWidgetStart,
30         on_widget_init_proc *apiOnWidgetInit,
31         on_widget_stop_proc *apiOnWidgetStop,
32         on_frame_load_proc* apiOnFrameLoad,
33         on_frame_unload_proc* apiOnFrameUnload,
34         const ClassPtrList &apiClassList) :
35     m_fileName(fileName),
36     m_libHandle(libHandle),
37     m_apiOnWidgetStart(apiOnWidgetStart),
38     m_apiOnWidgetInit(apiOnWidgetInit),
39     m_apiOnWidgetStop(apiOnWidgetStop),
40     m_apiOnFrameLoad(apiOnFrameLoad),
41     m_apiOnFrameUnload(apiOnFrameUnload),
42     m_apiClassList(apiClassList)
43 {
44 }
45
46 Plugin::~Plugin()
47 {
48     LogInfo("Unloading plugin library: " << m_fileName << "...");
49
50     // Unload library
51     if (dlclose(m_libHandle) != 0) {
52         LogError("Cannot close plugin handle");
53     } else {
54         LogDebug("Library is unloaded");
55     }
56 }
57
58 PluginPtr Plugin::LoadFromFile(const std::string& fileName)
59 {
60     LogDebug("LoadFromFile" << fileName);
61
62     LogDebug("Loading plugin: " << fileName);
63
64     void *dllHandle;
65     dllHandle = dlopen(fileName.c_str(), RTLD_NOW);
66
67     if (dllHandle == NULL) {
68         LogError(
69             "Failed to load plugin: " << fileName << ". Reason: " << dlerror());
70         PluginPtr empty;
71         return empty;
72     }
73
74     // Load new plugin API
75     on_widget_start_proc *onWidgetStartProcPtr = NULL;
76     on_widget_stop_proc *onWidgetStopProcPtr = NULL;
77     on_widget_init_proc *onWidgetInitProcPtr = NULL;
78     on_frame_load_proc *onFrameLoadProcPtr = NULL;
79     on_frame_unload_proc *onFrameUnloadProcPtr = NULL;
80
81     const js_entity_definition_t *rawClassList = NULL;
82     get_widget_entity_map_proc *getWidgetEntityMapProcPtr = NULL;
83
84     onWidgetStartProcPtr =
85         reinterpret_cast<on_widget_start_proc *>(
86             dlsym(dllHandle, PLUGIN_WIDGET_START_PROC_NAME));
87     onWidgetInitProcPtr =
88         reinterpret_cast<on_widget_init_proc *>(
89             dlsym(dllHandle, PLUGIN_WIDGET_INIT_PROC_NAME));
90     onWidgetStopProcPtr =
91         reinterpret_cast<on_widget_stop_proc *>(
92             dlsym(dllHandle, PLUGIN_WIDGET_STOP_PROC_NAME));
93     onFrameLoadProcPtr =
94         reinterpret_cast<on_frame_load_proc *>(
95             dlsym(dllHandle, PLUGIN_FRAME_LOAD_PROC_NAME));
96     onFrameUnloadProcPtr =
97         reinterpret_cast<on_frame_unload_proc *>(
98             dlsym(dllHandle, PLUGIN_FRAME_UNLOAD_PROC_NAME));
99     getWidgetEntityMapProcPtr =
100         reinterpret_cast<get_widget_entity_map_proc *>(
101             dlsym(dllHandle, PLUGIN_GET_CLASS_MAP_PROC_NAME));
102
103     if( getWidgetEntityMapProcPtr )
104     {
105         rawClassList = (*getWidgetEntityMapProcPtr)();
106         LogDebug("rawClassList : " << rawClassList  << "by getWidgetClassMapProcPtr()");
107     }
108     else
109     {
110         rawClassList =
111             static_cast<const js_entity_definition_t *>(dlsym(dllHandle,
112                                                         PLUGIN_CLASS_MAP_NAME));
113         LogDebug("rawClassList : " << rawClassList );
114     }
115
116
117     if (NULL == onWidgetStartProcPtr || NULL == onWidgetStopProcPtr ||
118         /*NULL == onWidgetInitProcPtr ||*/ NULL == rawClassList) {
119         LogWarning("#####");
120         LogWarning(
121             "##### Warning: The following plugin does not support new plugin API.");
122         LogWarning(
123             "##### Old plugin API is deprecated. Please update it to new API");
124         LogWarning("#####");
125         LogWarning(
126             "##### Plugin: " << fileName <<
127             " has got deprecated or invalid API");
128         LogWarning("#####");
129
130         // Will not load plugin
131         dlclose(dllHandle);
132
133         PluginPtr empty;
134         return empty;
135     }
136
137     LogInfo("#####");
138     LogInfo("##### Plugin: " << fileName << " supports new plugin API");
139     LogInfo("#####");
140     LogInfo("##### $onWidgetStartProc: " << onWidgetStartProcPtr);
141     LogInfo("##### $onWidgetInitProc: " << onWidgetInitProcPtr);
142     LogInfo("##### $onWidgetStopProc " << onWidgetStopProcPtr);
143     LogInfo("##### $onFrameLoadProc " << onFrameLoadProcPtr);
144     LogInfo("##### $onFrameUnloadProc " << onFrameUnloadProcPtr);
145     LogInfo("##### $classMap: " << reinterpret_cast<const void *>(rawClassList));
146     LogInfo("##### ");
147     LogInfo("##### Class map:");
148
149     const js_entity_definition_t *rawEntityListIterator = rawClassList;
150     ClassPtrList classList(new Plugin::ClassList());
151
152     // Parse all class definitions
153     while (rawEntityListIterator->parent_name != NULL &&
154             rawEntityListIterator->object_name != NULL)
155     {
156         // Logging
157         LogInfo("#####");
158         LogInfo("#####     [" << rawEntityListIterator->object_name << "]: ");
159         LogInfo("#####     Interface: " <<
160                 rawEntityListIterator->interface_name);
161         LogInfo("#####     Parent: " << rawEntityListIterator->parent_name);
162
163         // Register class
164         classList->push_back(ClassPtr(new Class(rawEntityListIterator)));
165
166         // Go to next class
167         ++rawEntityListIterator;
168     }
169
170     LogInfo("#####");
171
172     // Load export table
173     LogDebug("Plugin successfuly loaded");
174
175     // Insert to loaded modules list
176
177     PluginPtr instance(new Plugin(fileName,
178                                   dllHandle,
179                                   onWidgetStartProcPtr,
180                                   onWidgetInitProcPtr,
181                                   onWidgetStopProcPtr,
182                                   onFrameLoadProcPtr,
183                                   onFrameUnloadProcPtr,
184                                   classList));
185
186     return instance;
187 }
188
189 std::string Plugin::GetFileName() const
190 {
191     return m_fileName;
192 }
193
194 void Plugin::OnWidgetStart(int widgetId)
195 {
196     if (NULL != m_apiOnWidgetStart)
197     {
198         (*m_apiOnWidgetStart)(widgetId);
199     }
200     else
201     {
202         LogWarning("OnWidgetStart not set!");
203     }
204 }
205
206 void Plugin::OnWidgetInit(feature_mapping_interface_t* mapping)
207 {
208     Assert(NULL != mapping && "NULL mapping interface provided");
209     if (NULL != m_apiOnWidgetInit)
210     {
211         (*m_apiOnWidgetInit)(mapping);
212     }
213     else
214     {
215         LogWarning("OnWidgetInit not set!");
216     }
217 }
218
219 void Plugin::OnWidgetStop(int widgetId)
220 {
221     if (NULL != m_apiOnWidgetStop)
222     {
223         (*m_apiOnWidgetStop)(widgetId);
224     }
225     else
226     {
227         LogWarning("OnWidgetStop not set!");
228     }
229 }
230
231 void Plugin::OnFrameLoad(java_script_context_t context)
232 {
233     if (NULL != m_apiOnFrameLoad)
234     {
235         (*m_apiOnFrameLoad)(context);
236     }
237     else
238     {
239         LogWarning("OnFrameLoad not set!");
240     }
241 }
242
243 void Plugin::OnFrameUnload(java_script_context_t context)
244 {
245     if (NULL != m_apiOnFrameUnload)
246     {
247         (*m_apiOnFrameUnload)(context);
248     }
249     else
250     {
251         LogWarning("OnFrameUnload not set!");
252     }
253 }
254
255 const Plugin::ClassPtrList Plugin::GetClassList() const
256 {
257     return m_apiClassList;
258 }