upload tizen1.0 source
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Tizen / JSTizen.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 /**
18  * @file        JSTizen.cpp
19  * @author
20  * @version     0.1
21  * @brief
22  */
23
24 #include <cassert>
25 #include <vector>
26 #include <dpl/log/log.h>
27 #include <CommonsJavaScript/PrivateObject.h>
28 #include <CommonsJavaScript/Converter.h>
29 #include <Commons/WrtAccess/WrtAccess.h>
30 #include <API/Filter/FilterTypes.h>
31 #include <API/Filter/AnyType.h>
32 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
33 #include "JSTizen.h"
34 #include "JSFeature.h"
35 #include "plugin_config.h"
36 #include <WidgetDB/WidgetDBMgr.h>
37 #include <WidgetDB/IWidgetDB.h>
38 #include <PluginManager/PluginManagerFactory.h>
39
40 #include <iostream>
41
42 #define PLUGIN_NAME "tizen"
43 #define FUNCTION_LIST_AVAILABLE_FEATURES "listAvailableFeatures"
44 #define FUNCTION_LIST_ACTIVATED_FEATURES "listActivatedFeatures"
45
46 namespace TizenApis {
47 namespace Tizen1_0 {
48 namespace Tizen {
49
50 using namespace WrtDeviceApis;
51 using namespace WrtDeviceApis::Commons;
52 using namespace TizenApis::Api::Tizen;
53 using namespace WrtDeviceApis::PluginManager::Api;
54
55 JSClassDefinition JSTizen::m_classInfo =
56 {
57     0,
58     kJSClassAttributeNone,
59     PLUGIN_NAME,
60     0,
61     m_properties,
62     m_functions,
63     initialize,
64     finalize,
65     hasProperty,
66     getProperty,
67     setProperty,
68     NULL, //deleteProperty,
69     getPropertyNames,
70     NULL,
71     NULL,
72     hasInstance,
73     convertToType
74 };
75
76 JSStaticValue JSTizen::m_properties[] = {
77     { 0, 0, 0 }
78 };
79
80 JSStaticFunction JSTizen::m_functions[] = {
81     { FUNCTION_LIST_AVAILABLE_FEATURES, listAvailableFeatures, kJSPropertyAttributeNone},
82     { FUNCTION_LIST_ACTIVATED_FEATURES, listActivatedFeatures, kJSPropertyAttributeNone},
83     { 0, 0, 0 }
84 };
85
86 const JSClassDefinition* JSTizen::getClassInfo()
87 {
88     return &m_classInfo;
89 }
90
91 JSClassRef JSTizen::m_classRef = JSClassCreate(JSTizen::getClassInfo());
92
93 void JSTizen::initialize(JSContextRef context, JSObjectRef object)
94 {
95     LogDebug("entered");
96     TizenPrivate* priv = static_cast<TizenPrivate*>(JSObjectGetPrivate(object));
97     JSObjectSetPrivate(object, NULL);
98     if (priv) {
99         delete priv;
100     }
101
102     int widgetId = WrtAccessSingleton::Instance().getWidgetId();
103     PluginOnDemandPrivPtr privObject(
104         new PluginOnDemandPriv(
105                         PluginManagerFactory::getInstance().getPluginManager(
106                             widgetId,
107                             PLUGIN_NAME,
108                             object,
109                             context)));
110
111     priv = new TizenPrivate(context, privObject);
112     if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
113         JSObjectSetPrivate(object, NULL);
114         delete priv;
115     }
116 }
117
118 void JSTizen::finalize(JSObjectRef object)
119 {
120     LogDebug("entered");
121     TizenPrivate* priv = static_cast<TizenPrivate*>(JSObjectGetPrivate(object));
122     JSObjectSetPrivate(object, NULL);
123     delete priv;
124 }
125
126 JSClassRef JSTizen::getClassRef()
127 {
128     if (!m_classRef) {
129         m_classRef = JSClassCreate(&m_classInfo);
130     }
131     return m_classRef;
132 }
133
134 //bool JSTizen::deleteProperty(JSContextRef context, JSObjectRef object,
135 //        JSStringRef propertyName, JSValueRef* exception)
136 //{
137 //    return true;
138 //}
139
140 void JSTizen::getPropertyNames(JSContextRef context, JSObjectRef object,
141         JSPropertyNameAccumulatorRef propertyNames)
142 {
143     Try
144     {
145         TizenPrivate* priv =
146             static_cast<TizenPrivate*>(JSObjectGetPrivate(object));
147         if (!priv || !priv->getObject()) {
148             LogError("No private obejct");
149             return;
150         }
151         PluginOnDemandPrivPtr privObj = priv->getObject();
152         privObj->getPluginManager()->addPropertiesToList(propertyNames);
153
154     }
155     Catch(Commons::Exception)
156     {
157         LogError("Error occured");
158     }
159 }
160
161 bool JSTizen::hasInstance(JSContextRef context, JSObjectRef constructor,
162         JSValueRef possibleInstance, JSValueRef* exception)
163 {
164     return true;
165 }
166
167 JSValueRef JSTizen::convertToType(JSContextRef context, JSObjectRef object,
168         JSType type, JSValueRef* exception)
169 {
170     return JSValueMakeUndefined(context);
171 }
172
173 bool JSTizen::hasProperty(JSContextRef context,
174         JSObjectRef object, JSStringRef propertyName)
175 {
176     LogDebug("entered");
177
178     Try
179     {
180         TizenPrivate* priv = static_cast<TizenPrivate*>(JSObjectGetPrivate(object));
181
182         if (!priv || !priv->getObject()) {
183             LogError("No private obejct");
184             return false;
185         }
186
187         return priv->getObject()->getPluginManager()->hasChild(
188             CommonsJavaScript::Converter(context).toString(propertyName));
189     }
190     Catch(WrtDeviceApis::Commons::Exception)
191     {
192         LogError("Error occured");
193     }
194
195     //let JSCore to handle this property
196     return false;
197 }
198
199 JSValueRef JSTizen::getProperty(JSContextRef context, JSObjectRef object,
200         JSStringRef propertyName, JSValueRef* exception)
201 {
202     LogDebug("entered");
203
204     Try
205     {
206         TizenPrivate* priv =
207                 static_cast<TizenPrivate*>(JSObjectGetPrivate(object));
208         if (!priv || !priv->getObject()) {
209             LogError("No private obejct");
210             return false;
211         }
212
213         return priv->getObject()->getPluginManager()->getProperty(
214             CommonsJavaScript::Converter(context).toString(propertyName));
215     }
216     Catch(WrtDeviceApis::Commons::Exception)
217     {
218         LogError("Error occured");
219     }
220
221     /* During normal operation flow should not reach here,
222        but wrt may fail with loading plugin. */
223   return JSValueMakeUndefined(context);
224 }
225
226 bool JSTizen::setProperty(JSContextRef context, JSObjectRef object,
227         JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
228 {
229     LogDebug("entered");
230     Try
231     {
232         TizenPrivate* priv =
233                 static_cast<TizenPrivate*>(JSObjectGetPrivate(object));
234         if (!priv || !priv->getObject()) {
235             LogError("No private obejct");
236             return false;
237         }
238         priv->getObject()->getPluginManager()->setProperty(
239             CommonsJavaScript::Converter(context).toString(propertyName),
240             value);
241     }
242     Catch(WrtDeviceApis::Commons::Exception)
243     {
244         LogError("Error occured");
245     }
246
247     //this code should not be reached. setProperty is not called when
248     //hasProperty returns false. Anyway, if we didn't set value then we
249     //return false.
250     return false;
251 }
252
253 JSValueRef JSTizen::listAvailableFeatures(JSContextRef context, JSObjectRef object,
254         JSObjectRef thisObject, size_t argumentCount,
255         const JSValueRef arguments[], JSValueRef* exception)
256 {
257     LogDebug("entered");
258
259     TizenPrivate* priv = static_cast<TizenPrivate*>(JSObjectGetPrivate(thisObject));
260     assert(priv);
261
262     /*
263      * Current Tizen spec assures that tizen is always available
264     AccessStatus status = DEVICEAPIS_CHECK_ACCESS(
265                                  DEVICEAPIS_FUNCTION_API_LIST_AVAIL_FEATURES);
266
267     SYNC_ACCESS_STATUS_HANDLER(status, context, exception);
268     */
269
270     try
271     {
272         if (!priv) {
273             ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "No private object");
274         }
275             PluginOnDemandPrivPtr privObj = priv->getObject();
276     }
277     catch(const WrtDeviceApis::Commons::Exception &ex)
278     {
279         return WrtDeviceApis::CommonsJavaScript::JSDOMExceptionFactory::UnknownException.make(context, exception);
280     }
281     JSObjectRef result = JSCreateArrayObject(context, 0, NULL);
282     int widgetId = WrtAccessSingleton::Instance().getWidgetId();
283     WidgetDB::Api::IWidgetDBPtr widgetDB =
284         WidgetDB::Api::getWidgetDB(widgetId);
285     WidgetDB::Api::Features features = widgetDB->getRegisteredFeatures();
286     for (size_t i=0; i<features.size(); ++i) {
287         JSFeaturePrivateObject *priv = new JSFeaturePrivateObject(context, features[i]);
288         JSObjectRef tempObj = JSObjectMake(context, JSFeature::getClassRef(), priv);
289         if (tempObj) {
290             if (!JSSetArrayElement(context, result, i, tempObj)) {
291                 delete priv;
292             }
293         }
294         else {
295             delete priv;
296         }
297     }
298     return result;
299 }
300
301 JSValueRef JSTizen::listActivatedFeatures(JSContextRef context, JSObjectRef object,
302         JSObjectRef thisObject, size_t argumentCount,
303         const JSValueRef arguments[], JSValueRef* exception)
304 {
305     LogDebug("entered");
306
307     TizenPrivate* priv = static_cast<TizenPrivate*>(JSObjectGetPrivate(thisObject));
308     assert(priv);
309
310     /*
311      * Current Tizen spec assures that tizen is always available
312      AccessStatus status = DEVICEAPIS_CHECK_ACCESS(
313                                  DEVICEAPIS_FUNCTION_API_LIST_ACTIV_FEATURES);
314
315     SYNC_ACCESS_STATUS_HANDLER(status, context, exception);*/
316     try
317     {
318         if (!priv) {
319             ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "No private object");
320         }
321         PluginOnDemandPrivPtr privObj = priv->getObject();
322     }
323     catch(const WrtDeviceApis::Commons::Exception &ex)
324     {
325         return WrtDeviceApis::CommonsJavaScript::JSDOMExceptionFactory::UnknownException.make(context, exception);
326     }
327     JSObjectRef result = JSCreateArrayObject(context, 0, NULL);
328     int widgetId = WrtAccessSingleton::Instance().getWidgetId();
329     WidgetDB::Api::IWidgetDBPtr widgetDB =
330         WidgetDB::Api::getWidgetDB(widgetId);
331     WidgetDB::Api::Features features = widgetDB->getWidgetFeatures();
332     for (size_t i=0; i<features.size(); ++i) {
333         JSFeaturePrivateObject *priv = new JSFeaturePrivateObject(context, features[i]);
334         JSObjectRef tempObj = JSObjectMake(context, JSFeature::getClassRef(), priv);
335         if (tempObj) {
336             if (!JSSetArrayElement(context, result, i, tempObj)) {
337                 delete priv;
338             }
339         }
340         else {
341             delete priv;
342         }
343     }
344     return result;
345 }
346
347 } // Tizen
348 } // Tizen1_0
349 } // TizenApis