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/log.h>
35 #include <dpl/foreach.h>
41 const string GLOBAL_OBJECT_NAME = "GLOBAL_OBJECT";
42 const char* SEPARATOR = ".";
45 namespace WrtDeviceApis {
46 namespace PluginManager {
49 PluginManager::PluginManager(int widgetHandle,
50 const string &objectUri,
52 JSContextRef context) :
53 m_widgetHandle(widgetHandle),
54 m_objectInstance(object),
55 m_objectUri(GLOBAL_OBJECT_NAME),
56 m_shortUri(objectUri),
59 m_objectUri.append(SEPARATOR).append(objectUri);
60 WrtDB::WrtDatabase::attachToThreadRO();
63 PluginManager::~PluginManager()
65 ObjectList::iterator it = m_objectList.begin();
66 for (; it != m_objectList.end(); ++it)
68 JSValueUnprotect(m_context, it->second);
70 WrtDB::WrtDatabase::detachFromThread();
73 bool PluginManager::hasChild(const string &name) const
75 const PropertyList &prop = getProperties();
76 return prop.end() != find(prop.begin(), prop.end(), name);
79 bool PluginManager::loadChild(const string &name) const
81 LogInfo("loading " << name);
82 string localUri = m_objectUri;
83 localUri.append(SEPARATOR).append(name);
85 WrtDB::DbPluginHandle handle =
86 WrtDB::PluginDAOReadOnly::getPluginHandleForImplementedObject(localUri);
87 if (handle == WrtDB::INVALID_PLUGIN_HANDLE) {
88 LogError("Plugin not found");
92 WrtDB::DbWidgetFeatureSet features;
95 WrtDB::WidgetDAOReadOnly dao(m_widgetHandle);
96 features = dao.getFeaturesList();
98 Catch(WrtDB::WidgetDAOReadOnly::Exception::Base)
100 // Error while reading database - widget handle may
101 // be invalid or some data may be missing in database
102 LogError("Cannot get feature list");
106 //check does plugin with feature was requested
107 FOREACH (it, features)
109 if (it->pluginId == handle)
113 LogWarning("Feature rejected by ACE");
117 JavaScriptObject jsObject = {m_objectInstance,
120 return PluginLogicSingleton::Instance().loadPluginOnDemand(
121 handle, jsObject, const_cast<JSGlobalContextRef>(m_context));
124 LogError("Plugin not loaded");
128 JSValueRef PluginManager::getProperty(const string &name) const
130 LogDebug("getProperty " << name);
131 ObjectList::const_iterator it = m_objectList.find(name);
132 if (it != m_objectList.end()) {
133 //return already set value
137 if (!loadChild(name)) {
138 ThrowMsg(Commons::PlatformException, "Cannot load plugin");
141 it = m_objectList.find(name);
142 if (it != m_objectList.end()) {
147 ThrowMsg(Commons::PlatformException, "Cannot obtain property");
150 bool PluginManager::setProperty(const string &name,
153 LogDebug("setProperty " << name);
154 if (m_objectList.count(name) > 0) {
155 JSValueUnprotect(m_context, m_objectList[name]);
157 JSValueProtect(m_context, value);
158 m_objectList[name] = value;
162 bool PluginManager::deleteProperty(const string &name)
164 if (m_objectList.count(name) > 0) {
165 LogDebug("deleteProperty " << name);
166 JSValueUnprotect(m_context, m_objectList[name]);
167 m_objectList.erase(name);
174 Api::IPluginManager::PropertyList PluginManager::getProperties() const
176 if (!m_propertyCache.IsNull()) {
177 return *m_propertyCache;
180 m_propertyCache = PropertyList();
182 WrtDB::DbWidgetFeatureSet features;
185 WrtDB::WidgetDAOReadOnly dao(m_widgetHandle);
186 features = dao.getFeaturesList();
188 Catch(WrtDB::WidgetDAOReadOnly::Exception::Base)
190 LogError("Cannot get feature list");
191 ReThrow(Commons::PlatformException);
194 string localUri = m_objectUri + SEPARATOR;
195 WrtDB::DbWidgetFeatureSet::const_iterator feature = features.begin();
196 for (; feature != features.end(); ++feature) {
197 WrtDB::ImplementedObjectsList implObjs =
198 WrtDB::PluginDAOReadOnly::getImplementedObjectsForPluginHandle(
200 FOREACH(it, implObjs) {
201 //check if implemented object stats with localUri
202 if (it->find(localUri) == 0) {
203 string property = *it;
204 //remove local uri that predicts property name.
205 property.erase(0, localUri.size());
206 //check if property has its own properties.
207 size_t pos = property.find(SEPARATOR);
208 if (pos != string::npos) {
209 //if so then remove them.
212 m_propertyCache->push_back(property);
216 return *m_propertyCache;
219 void PluginManager::addPropertiesToList(
220 JSPropertyNameAccumulatorRef propertyNames) const
222 PropertyList properties = getProperties();
223 CommonsJavaScript::Converter converter(m_context);
224 FOREACH(it, properties) {
225 JSStringRef name = converter.toJSStringRef(*it);
226 JSPropertyNameAccumulatorAddName(propertyNames, name);
227 JSStringRelease(name);