Update wrt-plugins-common_0.3.53
[framework/web/wrt-plugins-common.git] / src / modules / tizen / PluginManager / PluginManager.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        PluginManager.cpp
18  * @author      Lukasz Marek (l.marek@samsung.com)
19  * @version     0.1
20  * @brief
21  */
22
23 #include "PluginManager.h"
24 #include <algorithm>
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>
29 #include <js_types.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>
36
37
38 using namespace std;
39
40 namespace {
41 const string GLOBAL_OBJECT_NAME = "GLOBAL_OBJECT";
42 const char* SEPARATOR = ".";
43 }
44
45 namespace WrtDeviceApis {
46 namespace PluginManager {
47
48
49 PluginManager::PluginManager(int widgetHandle,
50                              const string &objectUri,
51                              JSObjectRef object,
52                              JSContextRef context) :
53     m_widgetHandle(widgetHandle),
54     m_objectInstance(object),
55     m_objectUri(GLOBAL_OBJECT_NAME),
56     m_shortUri(objectUri),
57     m_context(context)
58 {
59     m_objectUri.append(SEPARATOR).append(objectUri);
60     WrtDB::WrtDatabase::attachToThreadRO();
61 }
62
63 PluginManager::~PluginManager()
64 {
65     ObjectList::iterator it = m_objectList.begin();
66     for (; it != m_objectList.end(); ++it)
67     {
68         JSValueUnprotect(m_context, it->second);
69     }
70     WrtDB::WrtDatabase::detachFromThread();
71 }
72
73 bool PluginManager::hasChild(const string &name) const
74 {
75     const PropertyList &prop = getProperties();
76     return prop.end() != find(prop.begin(), prop.end(), name);
77 }
78
79 bool PluginManager::loadChild(const string &name) const
80 {
81     LogInfo("loading " << name);
82     string localUri = m_objectUri;
83     localUri.append(SEPARATOR).append(name);
84
85     WrtDB::DbPluginHandle handle =
86         WrtDB::PluginDAOReadOnly::getPluginHandleForImplementedObject(localUri);
87     if (handle == WrtDB::INVALID_PLUGIN_HANDLE) {
88         LogError("Plugin not found");
89         return false;
90     }
91
92     WrtDB::DbWidgetFeatureSet features;
93     Try
94     {
95         WrtDB::WidgetDAOReadOnly dao(m_widgetHandle);
96         features = dao.getFeaturesList();
97     }
98     Catch(WrtDB::WidgetDAOReadOnly::Exception::Base)
99     {
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");
103         return false;
104     }
105
106     //check does plugin with feature was requested
107     FOREACH (it, features)
108     {
109         if (it->pluginId == handle)
110         {
111             if(it->rejected)
112             {
113                 LogWarning("Feature rejected by ACE");
114                 continue;
115             }
116
117             JavaScriptObject jsObject = {m_objectInstance,
118                                                       m_shortUri};
119
120             return PluginLogicSingleton::Instance().loadPluginOnDemand(
121                 handle, jsObject, const_cast<JSGlobalContextRef>(m_context));
122         }
123     }
124     LogError("Plugin not loaded");
125     return false;
126 }
127
128 JSValueRef PluginManager::getProperty(const string &name) const
129 {
130     LogDebug("getProperty " << name);
131     ObjectList::const_iterator it = m_objectList.find(name);
132     if (it != m_objectList.end()) {
133         //return already set value
134         return it->second;
135     }
136
137     if (!loadChild(name)) {
138         ThrowMsg(Commons::PlatformException, "Cannot load plugin");
139     }
140
141     it = m_objectList.find(name);
142     if (it != m_objectList.end()) {
143         //return set value
144         return it->second;
145     }
146
147     ThrowMsg(Commons::PlatformException, "Cannot obtain property");
148 }
149
150 bool PluginManager::setProperty(const string &name,
151                                 JSValueRef value)
152 {
153     LogDebug("setProperty " << name);
154     if (m_objectList.count(name) > 0) {
155         JSValueUnprotect(m_context, m_objectList[name]);
156     }
157     JSValueProtect(m_context, value);
158     m_objectList[name] = value;
159     return true;
160 }
161
162 bool PluginManager::deleteProperty(const string &name)
163 {
164     if (m_objectList.count(name) > 0) {
165         LogDebug("deleteProperty " << name);
166         JSValueUnprotect(m_context, m_objectList[name]);
167         m_objectList.erase(name);
168         return true;
169     }
170     return false;
171 }
172
173
174 Api::IPluginManager::PropertyList PluginManager::getProperties() const
175 {
176     if (!m_propertyCache.IsNull()) {
177         return *m_propertyCache;
178     }
179
180     m_propertyCache = PropertyList();
181
182     WrtDB::DbWidgetFeatureSet features;
183     Try
184     {
185         WrtDB::WidgetDAOReadOnly dao(m_widgetHandle);
186         features = dao.getFeaturesList();
187     }
188     Catch(WrtDB::WidgetDAOReadOnly::Exception::Base)
189     {
190         LogError("Cannot get feature list");
191         ReThrow(Commons::PlatformException);
192     }
193
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(
199                 feature->pluginId);
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.
210                     property.erase(pos);
211                 }
212                 m_propertyCache->push_back(property);
213             }
214         }
215     }
216     return *m_propertyCache;
217 }
218
219 void PluginManager::addPropertiesToList(
220         JSPropertyNameAccumulatorRef propertyNames) const
221 {
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);
228     }
229
230 }
231
232 }
233 }