upload tizen1.0 source
[platform/framework/web/wrt-plugins-common.git] / src / standards / W3C / Widget / JSWidget.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        JSWidget.cpp
19  * @author      Grzegorz Krawczyk (g.krawczyk@samsung.com)
20  * @version     0.1
21  */
22
23 #include "JSWidget.h"
24 #include <memory>
25 #include <CommonsJavaScript/Converter.h>
26 #include <dpl/log/log.h>
27 #include <dpl/assert.h>
28 #include <Widget/WidgetFactory.h>
29 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
30 #include "JSPreferences.h"
31 #include <Widget/IWidget.h>
32 #include "IFrameSupport.h"
33 #include <LocalStorage/LocalStorageMgr.h>
34 #include <Commons/WrtAccess/WrtAccess.h>
35
36
37 #define CATCH_EXCEPTION_NO_MODIFABLE \
38     Catch(Commons::LocalStorageValueNoModifableException) {\
39         LogError("The item is read only");\
40         return JSDOMExceptionFactory::\
41             NoModificationAllowedException.make(context, exception);\
42     }
43
44 #define CATCH_EXCEPTION_CONVERSION \
45     Catch(Commons::ConversionException) {\
46         LogError("Error on conversion");\
47         return JSDOMExceptionFactory::\
48             UnknownException.make(context, exception);\
49     }
50
51 #define CATCH_EXCEPTION_NULL_PTR \
52     Catch(Commons::NullPointerException) {\
53         LogError("Error on pointer, null value");\
54         return JSDOMExceptionFactory::\
55             UnknownException.make(context, exception);\
56     }
57
58 #define CATCH_EXCEPTION_PLATFORM_ERROR \
59     Catch(Commons::PlatformException){\
60         LogError("PlatformException occured");\
61         return JSDOMExceptionFactory::\
62             UnknownException.make(context, exception);\
63     }
64
65 #define CATCH_EXCEPTION_SECURITY \
66     Catch(Commons::SecurityException){\
67         LogError("Security exception occured");\
68         return JSDOMExceptionFactory::\
69             SecurityException.make(context, exception);\
70     }
71
72 #define CATCH_EXCEPTION_OUT_OF_RANGE \
73     Catch(Commons::OutOfRangeException) {\
74         LogError("OutOfRangeException");\
75         return JSDOMExceptionFactory::\
76             QuotaExceededException.make(context, exception);\
77     }
78
79 #define CATCH_EXCEPTION_INVALID_ARG \
80     Catch(Commons::InvalidArgumentException) {\
81         LogError("Pair for given key doesnt exist");\
82         return JSValueMakeNull(context);\
83     }
84
85 #define WIDGET_PLUGIN_NAME "Widget"
86
87 #define WRT_WIDGET_PROPERTY_AUTHOR       "author"
88 #define WRT_WIDGET_PROPERTY_AUTHOR_EMAIL "authorEmail"
89 #define WRT_WIDGET_PROPERTY_AUTHOR_HREF  "authorHref"
90 #define WRT_WIDGET_PROPERTY_DESCRIPTION  "description"
91 #define WRT_WIDGET_PROPERTY_ID           "id"
92 #define WRT_WIDGET_PROPERTY_NAME         "name"
93 #define WRT_WIDGET_PROPERTY_SHORT_NAME   "shortName"
94 #define WRT_WIDGET_PROPERTY_VERSION      "version"
95 #define WRT_WIDGET_PROPERTY_HEIGHT       "height"
96 #define WRT_WIDGET_PROPERTY_WIDTH        "width"
97
98 namespace WrtPlugins {
99 namespace W3C {
100
101 using namespace WrtDeviceApis;
102 using namespace WrtDeviceApis::Commons;
103 using namespace WrtDeviceApis::CommonsJavaScript;
104 using namespace WrtDeviceApis::Widget;
105
106
107 struct WidgetPrivateObject
108 {
109     Widget::Api::IWidgetPtr iwidget;
110     JSObjectRef preferencesObject;
111     //TEMP
112     int widgetId;
113     JSObjectRef widgetObject;
114 };
115 typedef std::shared_ptr<WidgetPrivateObject> WidgetPrivateObjectPtr;
116
117 typedef WrtDeviceApis::CommonsJavaScript::PrivateObjectT
118     <WidgetPrivateObjectPtr>::Type JSWidgetPrivateObject;
119
120 WrtDeviceApis::Widget::Api::IWidgetPtr getIWidget(JSObjectRef arg)
121 {
122     JSWidgetPrivateObject* priv =
123         static_cast<JSWidgetPrivateObject*>(JSObjectGetPrivate(arg));
124
125     if (!priv) {
126         LogError("Private object not initialized");
127         ThrowMsg(Commons::NullPointerException,
128                  "Private object not initialized");
129     }
130
131     return priv->getObject()->iwidget;
132 }
133
134
135 LocalStorage::Api::ILocalStoragePtr getLocalStorage(int widgetId)
136 {
137     static int id = widgetId;
138     Assert(id == widgetId && "SPA is not longer supported");
139
140     static LocalStorage::Api::ILocalStoragePtr storage(
141         LocalStorage::Api::getLocalStorage(widgetId));
142
143     return storage;
144 }
145
146 JSObjectRef getPreferences(JSObjectRef arg)
147 {
148     JSWidgetPrivateObject* priv =
149         static_cast<JSWidgetPrivateObject*>(JSObjectGetPrivate(arg));
150
151     if (!priv) {
152         LogError("Private object not initialized");
153         return NULL;
154     }
155
156     return priv->getObject()->preferencesObject;
157 }
158
159 JSObjectRef createPreferencesObject(JSContextRef context,
160                                     JSObjectRef widgetObject,
161                                     int widgetId)
162 {
163     Assert(widgetObject && "Widget Object can'n be null");
164     //delete is invoked in JSPreferences::finalize
165     LocalStoragePrivateData* priv = new LocalStoragePrivateData;
166     Assert(priv && "Private data is null");
167     priv->istorage = getLocalStorage(widgetId);
168     priv->widgetObject = widgetObject;
169
170     JSObjectRef preferences = JSObjectMake(context,
171                                            JSPreferences::getClassRef(),
172                                            priv);
173
174     if(!preferences){
175         LogError("Preferences object is null");
176         delete priv;
177     }
178     //Unprotect is called in JSWidget::finalize
179     JSValueProtect(context, preferences);
180
181     return preferences;
182 };
183
184
185 JSClassDefinition JSWidget::m_classInfo = {
186     0,
187     kJSClassAttributeNone,
188     WIDGET_PLUGIN_NAME,
189     0,
190     m_property,
191     NULL,
192     initialize,
193     finalize,
194     hasProperty,
195     getProperty,
196     setProperty,
197     NULL, //DeleteProperty,
198     NULL, //GetPropertyNames,
199     NULL, //CallAsFunction,
200     NULL, //CallAsConstructor,
201     NULL, //HasInstance,
202     NULL, //ConvertToType,
203 };
204
205 JSStaticValue JSWidget::m_property[] = {
206     { WRT_WIDGET_PROPERTY_AUTHOR, JSWidget::getAuthor,
207       0, kJSPropertyAttributeReadOnly },
208     { WRT_WIDGET_PROPERTY_AUTHOR_EMAIL, JSWidget::getAuthorEmail,
209       0, kJSPropertyAttributeReadOnly },
210     { WRT_WIDGET_PROPERTY_AUTHOR_HREF, JSWidget::getAuthorHref,
211       0, kJSPropertyAttributeReadOnly },
212     { WRT_WIDGET_PROPERTY_DESCRIPTION, JSWidget::getDescription,
213       0, kJSPropertyAttributeReadOnly },
214     { WRT_WIDGET_PROPERTY_ID, JSWidget::getId,
215       0, kJSPropertyAttributeReadOnly },
216     { WRT_WIDGET_PROPERTY_NAME, JSWidget::getName,
217       0, kJSPropertyAttributeReadOnly },
218     { WRT_WIDGET_PROPERTY_SHORT_NAME, JSWidget::getShortName,
219       0, kJSPropertyAttributeReadOnly },
220     { WRT_WIDGET_PROPERTY_VERSION, JSWidget::getVersion,
221       0, kJSPropertyAttributeReadOnly },
222     { WRT_WIDGET_PROPERTY_HEIGHT, JSWidget::getHeight,
223       0, kJSPropertyAttributeReadOnly },
224     { WRT_WIDGET_PROPERTY_WIDTH, JSWidget::getWidth,
225       0, kJSPropertyAttributeReadOnly },
226     { 0, 0, 0, 0 }
227 };
228
229 const JSClassRef JSWidget::getClassRef()
230 {
231     if (!m_jsClassRef) {
232         m_jsClassRef = JSClassCreate(&m_classInfo);
233     }
234     return m_jsClassRef;
235 }
236
237 const JSClassDefinition* JSWidget::getClassInfo()
238 {
239     return &m_classInfo;
240 }
241
242 JSClassRef JSWidget::m_jsClassRef = JSClassCreate(JSWidget::getClassInfo());
243
244 void JSWidget::initialize(JSContextRef context,
245         JSObjectRef object)
246 {
247     LogDebug("entered");
248     LogError("object " << object);
249
250     JSWidgetPrivateObject* priv =
251         static_cast<JSWidgetPrivateObject*>(JSObjectGetPrivate(object));
252
253     if (!priv) {
254         LogDebug("creation private object");
255
256         Try {
257             using namespace WrtDeviceApis::Commons;
258
259             Widget::Api::IWidgetPtr widget =
260                 Api::WidgetFactory::createWidget();
261             int widgetId = WrtAccessSingleton::Instance().getWidgetId();
262             JSObjectRef preferences =
263                 createPreferencesObject(context,
264                                         object,
265                                         widgetId);
266             if(!preferences){
267                 LogError("Failed to create preferences object");
268             }
269
270             WidgetPrivateObjectPtr widgetPriv(new WidgetPrivateObject);
271             widgetPriv->iwidget = widget;
272             widgetPriv->preferencesObject = preferences;
273
274             priv = new JSWidgetPrivateObject(context, widgetPriv);
275             JSObjectSetPrivate(object, priv);
276             LogDebug("private object created");
277
278         }
279         Catch(Commons::InvalidArgumentException){
280             LogError("You should register widget id in ON_WIDGET_START");
281             return;
282         }
283
284     }
285 }
286
287 void JSWidget::finalize(JSObjectRef object)
288 {
289     LogDebug("entered");
290     JSWidgetPrivateObject* priv =
291         static_cast<JSWidgetPrivateObject*>(JSObjectGetPrivate(object));
292
293     JSValueUnprotect(priv->getContext(), priv->getObject()->preferencesObject);
294
295     delete priv;
296     LogDebug("private object is realised");
297 }
298
299 JSValueRef JSWidget::getAuthor(JSContextRef context,
300         JSObjectRef object,
301         JSStringRef propertyName,
302         JSValueRef* exception)
303 {
304     LogDebug("entered");
305
306     Try {
307         Converter converter(context);
308         return converter.toJSValueRef(getIWidget(object)->getAuthor());
309     }
310     CATCH_EXCEPTION_CONVERSION
311     CATCH_EXCEPTION_NULL_PTR
312     CATCH_EXCEPTION_PLATFORM_ERROR
313 }
314
315 JSValueRef JSWidget::getAuthorEmail(JSContextRef context,
316         JSObjectRef object,
317         JSStringRef propertyName,
318         JSValueRef* exception)
319 {
320     Try {
321         Converter converter(context);
322         return converter.toJSValueRef(getIWidget(object)->getAuthorEmail());
323     }
324     CATCH_EXCEPTION_CONVERSION
325     CATCH_EXCEPTION_NULL_PTR
326     CATCH_EXCEPTION_PLATFORM_ERROR
327 }
328
329 JSValueRef JSWidget::getAuthorHref(JSContextRef context,
330         JSObjectRef object,
331         JSStringRef propertyName,
332         JSValueRef* exception)
333 {
334     Try {
335         Converter converter(context);
336         return converter.toJSValueRef(getIWidget(object)->getAuthorHref());
337     }
338     CATCH_EXCEPTION_CONVERSION
339     CATCH_EXCEPTION_NULL_PTR
340     CATCH_EXCEPTION_PLATFORM_ERROR
341 }
342
343 JSValueRef JSWidget::getDescription(JSContextRef context,
344         JSObjectRef object,
345         JSStringRef propertyName,
346         JSValueRef* exception)
347 {
348     Try {
349         Converter converter(context);
350         return converter.toJSValueRef(getIWidget(object)->getDescription());
351     }
352     CATCH_EXCEPTION_CONVERSION
353     CATCH_EXCEPTION_NULL_PTR
354     CATCH_EXCEPTION_PLATFORM_ERROR
355 }
356
357 JSValueRef JSWidget::getId(JSContextRef context,
358         JSObjectRef object,
359         JSStringRef propertyName,
360         JSValueRef* exception)
361 {
362     Try {
363         Converter converter(context);
364         return converter.toJSValueRef(getIWidget(object)->getId());
365     }
366     CATCH_EXCEPTION_CONVERSION
367     CATCH_EXCEPTION_NULL_PTR
368     CATCH_EXCEPTION_PLATFORM_ERROR
369 }
370
371 JSValueRef JSWidget::getName(JSContextRef context,
372         JSObjectRef object,
373         JSStringRef propertyName,
374         JSValueRef* exception)
375 {
376     Try {
377         Converter converter(context);
378         return converter.toJSValueRef(getIWidget(object)->getName());
379     }
380     CATCH_EXCEPTION_CONVERSION
381     CATCH_EXCEPTION_NULL_PTR
382     CATCH_EXCEPTION_PLATFORM_ERROR
383 }
384
385 JSValueRef JSWidget::getShortName(JSContextRef context,
386         JSObjectRef object,
387         JSStringRef propertyName,
388         JSValueRef* exception)
389 {
390     Try {
391         Converter converter(context);
392         return converter.toJSValueRef(getIWidget(object)->getShortName());
393     }
394     CATCH_EXCEPTION_CONVERSION
395     CATCH_EXCEPTION_NULL_PTR
396     CATCH_EXCEPTION_PLATFORM_ERROR
397 }
398
399 JSValueRef JSWidget::getVersion(JSContextRef context,
400         JSObjectRef object,
401         JSStringRef propertyName,
402         JSValueRef* exception)
403 {
404     Try {
405         Converter converter(context);
406         return converter.toJSValueRef(getIWidget(object)->getVersion());
407     }
408     CATCH_EXCEPTION_CONVERSION
409     CATCH_EXCEPTION_NULL_PTR
410     CATCH_EXCEPTION_PLATFORM_ERROR
411 }
412
413 JSValueRef JSWidget::getHeight(JSContextRef context,
414         JSObjectRef object,
415         JSStringRef propertyName,
416         JSValueRef* exception)
417 {
418     Try {
419         Converter converter(context);
420         return converter.toJSValueRef(getIWidget(object)->getHeight());
421     }
422     CATCH_EXCEPTION_CONVERSION
423     CATCH_EXCEPTION_NULL_PTR
424     CATCH_EXCEPTION_PLATFORM_ERROR
425 }
426
427 JSValueRef JSWidget::getWidth(JSContextRef context,
428         JSObjectRef object,
429         JSStringRef propertyName,
430         JSValueRef* exception)
431 {
432     Try {
433         Converter converter(context);
434         return converter.toJSValueRef(getIWidget(object)->getWidth());
435     }
436     CATCH_EXCEPTION_CONVERSION
437     CATCH_EXCEPTION_NULL_PTR
438     CATCH_EXCEPTION_PLATFORM_ERROR
439 }
440
441 bool JSWidget::hasProperty(JSContextRef context,
442                            JSObjectRef object,
443                            JSStringRef propertyName)
444 {
445     LogDebug("enter");
446
447     Try{
448         Converter converter(context);
449
450         std::string key = converter.toString(propertyName);
451         if(key=="preferences"){
452             return true;
453         }
454     }
455     Catch(Commons::InvalidArgumentException) {
456         LogDebug("Pair for given key doesnt exist");
457     }
458
459     Catch(Commons::ConversionException) {
460         LogError("Error on conversion");
461     }
462
463     Catch(Commons::NullPointerException) {
464         LogError("Error on pointer, null value");
465     }
466
467     Catch(Commons::PlatformException){
468         LogError("PlatformException occured");
469     }
470
471     return false;
472 }
473
474 JSValueRef JSWidget::getProperty(JSContextRef context,
475                                  JSObjectRef object,
476                                  JSStringRef propertyName,
477                                  JSValueRef* exception)
478 {
479     LogDebug("Object: " << object);
480
481     Try{
482         Converter converter(context);
483
484         std::string key = converter.toString(propertyName);
485
486         if(key=="preferences"){
487             Converter converter(context);
488             JSObjectRef pref = getPreferences(object);
489             if (!pref)
490             {
491                 LogError("Preferences object is NULL");
492                 return JSValueMakeUndefined(context);
493             }
494             return pref;
495         }
496         LogError("Property NOT supported: " << propertyName);
497         return JSValueMakeUndefined(context);
498     }
499
500     CATCH_EXCEPTION_CONVERSION
501     CATCH_EXCEPTION_NULL_PTR
502     CATCH_EXCEPTION_INVALID_ARG
503 }
504
505 bool JSWidget::setProperty(JSContextRef context,
506                            JSObjectRef object,
507                            JSStringRef propertyName,
508                            JSValueRef jvalue,
509                            JSValueRef* exception)
510 {
511     LogDebug("enter");
512
513     Try{
514         Converter converter(context);
515
516         std::string key = converter.toString(propertyName);
517         if (key == "preferences"){
518             LogError("Object is read only");
519             return true;
520         }
521     }
522     CATCH_EXCEPTION_INVALID_ARG
523     CATCH_EXCEPTION_CONVERSION
524     CATCH_EXCEPTION_NULL_PTR
525
526     return false;
527 }
528
529 #undef CATCH_EXCEPTION_NO_MODIFABLE
530 #undef CATCH_EXCEPTION_CONVERSION
531 #undef CATCH_EXCEPTION_NULL_PTR
532 #undef CATCH_EXCEPTION_PLATFORM_ERROR
533 #undef CATCH_EXCEPTION_SECURITY
534 #undef CATCH_EXCEPTION_OUT_OF_RANGE
535 #undef CATCH_EXCEPTION_INVALID_ARG
536
537 }
538 }