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.
17 * @file PluginManager.cpp
18 * @author Lukasz Marek (l.marek@samsung.com)
23 #include "PluginManager.h"
25 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
26 #include <dpl/wrt-dao-ro/plugin_dao_read_only.h>
27 #include <dpl/wrt-dao-ro/WrtDatabase.h>
28 #include <plugin_logic.h>
30 #include <dpl/wrt-dao-ro/common_dao_types.h>
31 #include <Commons/Exception.h>
32 #include <CommonsJavaScript/Converter.h>
33 #include <dpl/exception.h>
34 #include <dpl/log/secure_log.h>
35 #include <dpl/foreach.h>
40 const string GLOBAL_OBJECT_NAME = "GLOBAL_OBJECT";
41 const char* SEPARATOR = ".";
44 namespace WrtDeviceApis {
45 namespace PluginManager {
46 PluginManager::PluginManager(int widgetHandle,
47 const string &objectUri,
49 JSContextRef context) :
50 m_widgetHandle(widgetHandle),
51 m_objectInstance(object),
52 m_objectUri(GLOBAL_OBJECT_NAME),
53 m_shortUri(objectUri),
56 m_objectUri.append(SEPARATOR).append(objectUri);
57 WrtDB::WrtDatabase::attachToThreadRO();
60 PluginManager::~PluginManager()
62 ObjectList::iterator it = m_objectList.begin();
63 for (; it != m_objectList.end(); ++it) {
64 JSValueUnprotect(m_context, it->second);
66 WrtDB::WrtDatabase::detachFromThread();
69 bool PluginManager::hasChild(const string &name) const
71 const PropertyList &prop = getProperties();
72 return prop.end() != find(prop.begin(), prop.end(), name);
75 bool PluginManager::loadChild(const string &name) const
77 _D("loading %s", name.c_str());
78 string localUri = m_objectUri;
79 localUri.append(SEPARATOR).append(name);
81 WrtDB::DbPluginHandle handle =
82 WrtDB::PluginDAOReadOnly::getPluginHandleForImplementedObject(localUri);
83 if (handle == WrtDB::INVALID_PLUGIN_HANDLE) {
84 _E("Plugin not found");
88 WrtDB::DbWidgetFeatureSet features;
89 WrtDB::WidgetType widgetType;
92 WrtDB::WidgetDAOReadOnly dao(m_widgetHandle);
93 features = dao.getFeaturesList();
94 widgetType = dao.getWidgetType();
96 Catch(WrtDB::WidgetDAOReadOnly::Exception::Base)
98 // Error while reading database - widget handle may
99 // be invalid or some data may be missing in database
100 _E("Cannot get feature list");
104 if (widgetType == WrtDB::APP_TYPE_TIZENWEBAPP) {
105 JavaScriptObject jsObject = { m_objectInstance,
107 return PluginLogicSingleton::Instance().loadPluginOnDemand(
108 handle, jsObject, const_cast<JSGlobalContextRef>(m_context));
111 //check does plugin with feature was requested
112 FOREACH(it, features)
114 if (it->pluginId == handle) {
116 _E("Feature rejected by ACE");
120 JavaScriptObject jsObject = { m_objectInstance,
123 return PluginLogicSingleton::Instance().loadPluginOnDemand(
125 const_cast<JSGlobalContextRef>(m_context));
128 _E("Plugin not loaded");
132 JSValueRef PluginManager::getProperty(const string &name) const
134 _D("getProperty %s", name.c_str());
135 ObjectList::const_iterator it = m_objectList.find(name);
136 if (it != m_objectList.end()) {
137 //return already set value
141 if (!loadChild(name)) {
142 ThrowMsg(Commons::PlatformException, "Cannot load plugin");
145 it = m_objectList.find(name);
146 if (it != m_objectList.end()) {
151 ThrowMsg(Commons::PlatformException, "Cannot obtain property");
154 bool PluginManager::setProperty(const string &name,
157 _D("setProperty %s", name.c_str());
158 if (m_objectList.count(name) > 0) {
159 JSValueUnprotect(m_context, m_objectList[name]);
161 JSValueProtect(m_context, value);
162 m_objectList[name] = value;
166 bool PluginManager::deleteProperty(const string &name)
168 if (m_objectList.count(name) > 0) {
169 _D("deleteProperty %s", name.c_str());
170 JSValueUnprotect(m_context, m_objectList[name]);
171 m_objectList.erase(name);
177 Api::IPluginManager::PropertyList PluginManager::getProperties() const
179 using namespace WrtDB;
181 if (!m_propertyCache.IsNull()) {
182 return *m_propertyCache;
185 m_propertyCache = PropertyList();
187 WidgetDAOReadOnly dao(m_widgetHandle);
188 string localUri = m_objectUri + SEPARATOR;
190 if (dao.getWidgetType() == APP_TYPE_TIZENWEBAPP)
192 static ImplementedObjectsList implObjectList =
193 PluginDAOReadOnly::getImplementedObjects();
195 FOREACH(it_implObject, implObjectList)
197 //check if implemented object stats with localUri
198 if (it_implObject->find(localUri) == 0) {
199 string property = *it_implObject;
200 //remove local uri that predicts property name.
201 property.erase(0, localUri.size());
202 //check if property has its own properties.
203 size_t pos = property.find(SEPARATOR);
204 if (pos != string::npos) {
205 //if so then remove them.
209 m_propertyCache->push_back(property);
215 DbWidgetFeatureSet features;
219 features = dao.getFeaturesList();
221 Catch(WidgetDAOReadOnly::Exception::Base)
223 _E("Cannot get feature list");
224 ReThrow(Commons::PlatformException);
227 DbWidgetFeatureSet::const_iterator feature = features.begin();
228 for (; feature != features.end(); ++feature) {
229 ImplementedObjectsList implObjs =
230 PluginDAOReadOnly::getImplementedObjectsForPluginHandle(
232 FOREACH(it, implObjs) {
233 //check if implemented object stats with localUri
234 if (it->find(localUri) == 0) {
235 string property = *it;
236 //remove local uri that predicts property name.
237 property.erase(0, localUri.size());
238 //check if property has its own properties.
239 size_t pos = property.find(SEPARATOR);
240 if (pos != string::npos) {
241 //if so then remove them.
244 m_propertyCache->push_back(property);
250 return *m_propertyCache;
253 void PluginManager::addPropertiesToList(
254 JSPropertyNameAccumulatorRef propertyNames) const
256 PropertyList properties = getProperties();
257 CommonsJavaScript::Converter converter(m_context);
258 FOREACH(it, properties) {
259 JSStringRef name = converter.toJSStringRef(*it);
260 JSPropertyNameAccumulatorAddName(propertyNames, name);
261 JSStringRelease(name);