2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief This file is the implementation file of plugin
23 #include <dpl/log/log.h>
24 #include <dpl/assert.h>
27 Plugin::Plugin(const std::string &fileName,
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) :
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)
48 LogInfo("Unloading plugin library: " << m_fileName << "...");
51 if (dlclose(m_libHandle) != 0) {
52 LogError("Cannot close plugin handle");
54 LogDebug("Library is unloaded");
58 PluginPtr Plugin::LoadFromFile(const std::string& fileName)
60 LogDebug("LoadFromFile" << fileName);
62 LogDebug("Loading plugin: " << fileName);
65 dllHandle = dlopen(fileName.c_str(), RTLD_NOW);
67 if (dllHandle == NULL) {
69 "Failed to load plugin: " << fileName << ". Reason: " << dlerror());
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;
81 const js_entity_definition_t *rawClassList = NULL;
82 get_widget_entity_map_proc *getWidgetEntityMapProcPtr = NULL;
84 onWidgetStartProcPtr =
85 reinterpret_cast<on_widget_start_proc *>(
86 dlsym(dllHandle, PLUGIN_WIDGET_START_PROC_NAME));
88 reinterpret_cast<on_widget_init_proc *>(
89 dlsym(dllHandle, PLUGIN_WIDGET_INIT_PROC_NAME));
91 reinterpret_cast<on_widget_stop_proc *>(
92 dlsym(dllHandle, PLUGIN_WIDGET_STOP_PROC_NAME));
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));
103 if( getWidgetEntityMapProcPtr )
105 rawClassList = (*getWidgetEntityMapProcPtr)();
106 LogDebug("rawClassList : " << rawClassList << "by getWidgetClassMapProcPtr()");
111 static_cast<const js_entity_definition_t *>(dlsym(dllHandle,
112 PLUGIN_CLASS_MAP_NAME));
113 LogDebug("rawClassList : " << rawClassList );
117 if (NULL == onWidgetStartProcPtr || NULL == onWidgetStopProcPtr ||
118 /*NULL == onWidgetInitProcPtr ||*/ NULL == rawClassList) {
121 "##### Warning: The following plugin does not support new plugin API.");
123 "##### Old plugin API is deprecated. Please update it to new API");
126 "##### Plugin: " << fileName <<
127 " has got deprecated or invalid API");
130 // Will not load plugin
138 LogInfo("##### Plugin: " << fileName << " supports new plugin API");
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));
147 LogInfo("##### Class map:");
149 const js_entity_definition_t *rawEntityListIterator = rawClassList;
150 ClassPtrList classList(new Plugin::ClassList());
152 // Parse all class definitions
153 while (rawEntityListIterator->parent_name != NULL &&
154 rawEntityListIterator->object_name != NULL)
158 LogInfo("##### [" << rawEntityListIterator->object_name << "]: ");
159 LogInfo("##### Interface: " <<
160 rawEntityListIterator->interface_name);
161 LogInfo("##### Parent: " << rawEntityListIterator->parent_name);
164 classList->push_back(ClassPtr(new Class(rawEntityListIterator)));
167 ++rawEntityListIterator;
173 LogDebug("Plugin successfuly loaded");
175 // Insert to loaded modules list
177 PluginPtr instance(new Plugin(fileName,
179 onWidgetStartProcPtr,
183 onFrameUnloadProcPtr,
189 std::string Plugin::GetFileName() const
194 void Plugin::OnWidgetStart(int widgetId)
196 if (NULL != m_apiOnWidgetStart)
198 (*m_apiOnWidgetStart)(widgetId);
202 LogWarning("OnWidgetStart not set!");
206 void Plugin::OnWidgetInit(feature_mapping_interface_t* mapping)
208 Assert(NULL != mapping && "NULL mapping interface provided");
209 if (NULL != m_apiOnWidgetInit)
211 (*m_apiOnWidgetInit)(mapping);
215 LogWarning("OnWidgetInit not set!");
219 void Plugin::OnWidgetStop(int widgetId)
221 if (NULL != m_apiOnWidgetStop)
223 (*m_apiOnWidgetStop)(widgetId);
227 LogWarning("OnWidgetStop not set!");
231 void Plugin::OnFrameLoad(java_script_context_t context)
233 if (NULL != m_apiOnFrameLoad)
235 (*m_apiOnFrameLoad)(context);
239 LogWarning("OnFrameLoad not set!");
243 void Plugin::OnFrameUnload(java_script_context_t context)
245 if (NULL != m_apiOnFrameUnload)
247 (*m_apiOnFrameUnload)(context);
251 LogWarning("OnFrameUnload not set!");
255 const Plugin::ClassPtrList Plugin::GetClassList() const
257 return m_apiClassList;