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.
19 * @author Grzegorz Krawczyk (g.krawczyk@samsung.com)
25 #include <CommonsJavaScript/Converter.h>
26 #include <dpl/log/log.h>
27 #include <dpl/log/secure_log.h>
28 #include <dpl/assert.h>
29 #include <Widget/WidgetFactory.h>
30 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
31 #include "JSPreferences.h"
32 #include <Widget/IWidget.h>
33 #include <LocalStorage/LocalStorageMgr.h>
34 #include <Commons/WrtAccess/WrtAccess.h>
35 #include <dpl/wrt-dao-ro/wrt_db_types.h>
36 #include <CommonsJavaScript/JSLifeManager.h>
39 extern "C" JSGlobalContextRef JSContextGetGlobalContext(JSContextRef ctx);
41 #define CATCH_EXCEPTION_NO_MODIFABLE \
42 Catch(Commons::LocalStorageValueNoModifableException) { \
43 _E("The item is read only"); \
44 return JSDOMExceptionFactory:: \
45 NoModificationAllowedException.make(context, exception); \
48 #define CATCH_EXCEPTION_CONVERSION \
49 Catch(Commons::ConversionException) { \
50 _E("Error on conversion"); \
51 return JSDOMExceptionFactory:: \
52 UnknownException.make(context, exception); \
55 #define CATCH_EXCEPTION_NULL_PTR \
56 Catch(Commons::NullPointerException) { \
57 _E("Error on pointer, null value"); \
58 return JSDOMExceptionFactory:: \
59 UnknownException.make(context, exception); \
62 #define CATCH_EXCEPTION_PLATFORM_ERROR \
63 Catch(Commons::PlatformException){ \
64 _E("PlatformException occured"); \
65 return JSDOMExceptionFactory:: \
66 UnknownException.make(context, exception); \
69 #define CATCH_EXCEPTION_SECURITY \
70 Catch(Commons::SecurityException){ \
71 _E("Security exception occured"); \
72 return JSDOMExceptionFactory:: \
73 SecurityException.make(context, exception); \
76 #define CATCH_EXCEPTION_OUT_OF_RANGE \
77 Catch(Commons::OutOfRangeException) { \
78 _E("OutOfRangeException"); \
79 return JSDOMExceptionFactory:: \
80 QuotaExceededException.make(context, exception); \
83 #define CATCH_EXCEPTION_INVALID_ARG \
84 Catch(Commons::InvalidArgumentException) { \
85 _E("Pair for given key doesnt exist"); \
86 return JSValueMakeNull(context); \
89 #define WIDGET_PLUGIN_NAME "Widget"
91 #define WRT_WIDGET_PROPERTY_AUTHOR "author"
92 #define WRT_WIDGET_PROPERTY_AUTHOR_EMAIL "authorEmail"
93 #define WRT_WIDGET_PROPERTY_AUTHOR_HREF "authorHref"
94 #define WRT_WIDGET_PROPERTY_DESCRIPTION "description"
95 #define WRT_WIDGET_PROPERTY_ID "id"
96 #define WRT_WIDGET_PROPERTY_NAME "name"
97 #define WRT_WIDGET_PROPERTY_SHORT_NAME "shortName"
98 #define WRT_WIDGET_PROPERTY_VERSION "version"
99 #define WRT_WIDGET_PROPERTY_HEIGHT "height"
100 #define WRT_WIDGET_PROPERTY_WIDTH "width"
102 namespace WrtPlugins {
104 using namespace WrtDeviceApis;
105 using namespace WrtDeviceApis::Commons;
106 using namespace WrtDeviceApis::CommonsJavaScript;
107 using namespace WrtDeviceApis::Widget;
109 struct WidgetPrivateObject
111 Widget::Api::IWidgetPtr iwidget;
112 JSObjectRef preferencesObject;
113 JSObjectRef widgetObject;
115 typedef std::shared_ptr<WidgetPrivateObject> WidgetPrivateObjectPtr;
117 typedef WrtDeviceApis::CommonsJavaScript::PrivateObjectT
118 <WidgetPrivateObjectPtr>::Type JSWidgetPrivateObject;
120 WrtDeviceApis::Widget::Api::IWidgetPtr getIWidget(JSObjectRef arg)
122 JSWidgetPrivateObject* priv =
123 static_cast<JSWidgetPrivateObject*>(JSObjectGetPrivate(arg));
126 LogError("Private object not initialized");
127 ThrowMsg(Commons::NullPointerException,
128 "Private object not initialized");
131 return priv->getObject()->iwidget;
134 LocalStorage::Api::ILocalStoragePtr getLocalStorage(WidgetHandle widgetId)
136 LocalStorage::Api::ILocalStoragePtr storage(
137 LocalStorage::Api::getLocalStorage(widgetId));
142 JSObjectRef getPreferences(JSObjectRef arg)
144 JSWidgetPrivateObject* priv =
145 static_cast<JSWidgetPrivateObject*>(JSObjectGetPrivate(arg));
148 LogError("Private object not initialized");
152 return priv->getObject()->preferencesObject;
155 JSObjectRef createPreferencesObject(JSContextRef context,
156 JSObjectRef widgetObject,
157 WidgetHandle widgetId)
159 AssertMsg(widgetObject, "Widget Object can'n be null");
160 //delete is invoked in JSPreferences::finalize
161 LocalStoragePrivateData* priv = new LocalStoragePrivateData;
162 AssertMsg(priv, "Private data is null");
163 priv->istorage = getLocalStorage(widgetId);
164 priv->widgetObject = widgetObject;
166 JSObjectRef preferences = JSObjectMake(context,
167 JSPreferences::getClassRef(),
171 LogError("Preferences object is null");
174 //Unprotect is called in JSWidget::finalize
175 JSValueSafeProtect(context, preferences);
180 JSClassDefinition JSWidget::m_classInfo = {
182 kJSClassAttributeNone,
192 NULL, //DeleteProperty,
193 NULL, //GetPropertyNames,
194 NULL, //CallAsFunction,
197 NULL, //ConvertToType,
200 JSStaticValue JSWidget::m_property[] = {
201 { WRT_WIDGET_PROPERTY_AUTHOR, JSWidget::getAuthor,
202 0, kJSPropertyAttributeReadOnly },
203 { WRT_WIDGET_PROPERTY_AUTHOR_EMAIL, JSWidget::getAuthorEmail,
204 0, kJSPropertyAttributeReadOnly },
205 { WRT_WIDGET_PROPERTY_AUTHOR_HREF, JSWidget::getAuthorHref,
206 0, kJSPropertyAttributeReadOnly },
207 { WRT_WIDGET_PROPERTY_DESCRIPTION, JSWidget::getDescription,
208 0, kJSPropertyAttributeReadOnly },
209 { WRT_WIDGET_PROPERTY_ID, JSWidget::getId,
210 0, kJSPropertyAttributeReadOnly },
211 { WRT_WIDGET_PROPERTY_NAME, JSWidget::getName,
212 0, kJSPropertyAttributeReadOnly },
213 { WRT_WIDGET_PROPERTY_SHORT_NAME, JSWidget::getShortName,
214 0, kJSPropertyAttributeReadOnly },
215 { WRT_WIDGET_PROPERTY_VERSION, JSWidget::getVersion,
216 0, kJSPropertyAttributeReadOnly },
217 { WRT_WIDGET_PROPERTY_HEIGHT, JSWidget::getHeight,
218 0, kJSPropertyAttributeReadOnly },
219 { WRT_WIDGET_PROPERTY_WIDTH, JSWidget::getWidth,
220 0, kJSPropertyAttributeReadOnly },
224 JSClassRef JSWidget::getClassRef()
227 m_jsClassRef = JSClassCreate(&m_classInfo);
232 const JSClassDefinition* JSWidget::getClassInfo()
237 JSClassRef JSWidget::m_jsClassRef = JSClassCreate(JSWidget::getClassInfo());
239 void JSWidget::initialize(JSContextRef context,
242 JSGlobalContextRef g_context = JSContextGetGlobalContext(context);
244 LogDebug("entered. Context : " << context << "(global:" << g_context << "), Object: " << object);
246 if (context != g_context) {
250 JSWidgetPrivateObject* priv =
251 static_cast<JSWidgetPrivateObject*>(JSObjectGetPrivate(object));
254 LogDebug("creation private object");
257 using namespace WrtDeviceApis::Commons;
259 Widget::Api::IWidgetPtr widget =
260 Api::WidgetFactory::createWidget();
261 WidgetHandle widgetId = WrtAccessSingleton::Instance().getWidgetId();
262 JSObjectRef preferences =
263 createPreferencesObject(context,
267 LogError("Failed to create preferences object");
270 WidgetPrivateObjectPtr widgetPriv(new WidgetPrivateObject);
271 widgetPriv->iwidget = widget;
272 widgetPriv->preferencesObject = preferences;
274 priv = new JSWidgetPrivateObject(context, widgetPriv);
275 JSObjectSetPrivate(object, priv);
277 Catch(Commons::InvalidArgumentException){
278 LogError("You should register widget id in ON_WIDGET_START");
281 Catch(DPL::Exception) {
282 LogError("Failed to create private object for JSWidget");
288 void JSWidget::finalize(JSObjectRef object)
291 JSWidgetPrivateObject* priv =
292 static_cast<JSWidgetPrivateObject*>(JSObjectGetPrivate(object));
295 JSValueSafeUnprotect(priv->getContext(),
296 priv->getObject()->preferencesObject);
299 LogDebug("private object is released");
301 LogDebug("private object wasn't created");
305 JSValueRef JSWidget::getAuthor(JSContextRef context,
307 JSStringRef /*propertyName*/,
308 JSValueRef* exception)
313 Converter converter(context);
314 return converter.toJSValueRef(getIWidget(object)->getAuthor());
316 CATCH_EXCEPTION_CONVERSION
317 CATCH_EXCEPTION_NULL_PTR
318 CATCH_EXCEPTION_PLATFORM_ERROR
321 JSValueRef JSWidget::getAuthorEmail(JSContextRef context,
323 JSStringRef /*propertyName*/,
324 JSValueRef* exception)
327 Converter converter(context);
328 return converter.toJSValueRef(getIWidget(object)->getAuthorEmail());
330 CATCH_EXCEPTION_CONVERSION
331 CATCH_EXCEPTION_NULL_PTR
332 CATCH_EXCEPTION_PLATFORM_ERROR
335 JSValueRef JSWidget::getAuthorHref(JSContextRef context,
337 JSStringRef /*propertyName*/,
338 JSValueRef* exception)
341 Converter converter(context);
342 return converter.toJSValueRef(getIWidget(object)->getAuthorHref());
344 CATCH_EXCEPTION_CONVERSION
345 CATCH_EXCEPTION_NULL_PTR
346 CATCH_EXCEPTION_PLATFORM_ERROR
349 JSValueRef JSWidget::getDescription(JSContextRef context,
351 JSStringRef /*propertyName*/,
352 JSValueRef* exception)
355 Converter converter(context);
356 return converter.toJSValueRef(getIWidget(object)->getDescription());
358 CATCH_EXCEPTION_CONVERSION
359 CATCH_EXCEPTION_NULL_PTR
360 CATCH_EXCEPTION_PLATFORM_ERROR
363 JSValueRef JSWidget::getId(JSContextRef context,
365 JSStringRef /*propertyName*/,
366 JSValueRef* exception)
369 Converter converter(context);
370 return converter.toJSValueRef(getIWidget(object)->getId());
372 CATCH_EXCEPTION_CONVERSION
373 CATCH_EXCEPTION_NULL_PTR
374 CATCH_EXCEPTION_PLATFORM_ERROR
377 JSValueRef JSWidget::getName(JSContextRef context,
379 JSStringRef /*propertyName*/,
380 JSValueRef* exception)
383 Converter converter(context);
384 return converter.toJSValueRef(getIWidget(object)->getName());
386 CATCH_EXCEPTION_CONVERSION
387 CATCH_EXCEPTION_NULL_PTR
388 CATCH_EXCEPTION_PLATFORM_ERROR
391 JSValueRef JSWidget::getShortName(JSContextRef context,
393 JSStringRef /*propertyName*/,
394 JSValueRef* exception)
397 Converter converter(context);
398 return converter.toJSValueRef(getIWidget(object)->getShortName());
400 CATCH_EXCEPTION_CONVERSION
401 CATCH_EXCEPTION_NULL_PTR
402 CATCH_EXCEPTION_PLATFORM_ERROR
405 JSValueRef JSWidget::getVersion(JSContextRef context,
407 JSStringRef /*propertyName*/,
408 JSValueRef* exception)
411 Converter converter(context);
412 return converter.toJSValueRef(getIWidget(object)->getVersion());
414 CATCH_EXCEPTION_CONVERSION
415 CATCH_EXCEPTION_NULL_PTR
416 CATCH_EXCEPTION_PLATFORM_ERROR
419 JSValueRef JSWidget::getHeight(JSContextRef context,
421 JSStringRef /*propertyName*/,
422 JSValueRef* exception)
425 Converter converter(context);
426 unsigned int height = getIWidget(object)->getHeight();
430 return converter.toJSValueRef(height);
432 CATCH_EXCEPTION_CONVERSION
433 CATCH_EXCEPTION_NULL_PTR
434 CATCH_EXCEPTION_PLATFORM_ERROR
437 JSValueRef JSWidget::getWidth(JSContextRef context,
439 JSStringRef /*propertyName*/,
440 JSValueRef* exception)
443 Converter converter(context);
444 unsigned int width = getIWidget(object)->getWidth();
448 return converter.toJSValueRef(width);
450 CATCH_EXCEPTION_CONVERSION
451 CATCH_EXCEPTION_NULL_PTR
452 CATCH_EXCEPTION_PLATFORM_ERROR
455 bool JSWidget::hasProperty(JSContextRef context,
456 JSObjectRef /*object*/,
457 JSStringRef propertyName)
462 Converter converter(context);
464 std::string key = converter.toString(propertyName);
465 if (key == "preferences") {
469 Catch(Commons::InvalidArgumentException) {
470 LogDebug("Pair for given key doesnt exist");
473 Catch(Commons::ConversionException) {
474 LogError("Error on conversion");
477 Catch(Commons::NullPointerException) {
478 LogError("Error on pointer, null value");
481 Catch(Commons::PlatformException){
482 LogError("PlatformException occured");
487 JSValueRef JSWidget::getProperty(JSContextRef context,
489 JSStringRef propertyName,
490 JSValueRef* exception)
493 Converter converter(context);
495 std::string key = converter.toString(propertyName);
497 if (key == "preferences") {
498 Converter converter(context);
499 JSObjectRef pref = getPreferences(object);
501 LogError("Preferences object is NULL");
502 return JSValueMakeUndefined(context);
506 LogError("Property NOT supported: " << propertyName);
507 return JSValueMakeUndefined(context);
510 CATCH_EXCEPTION_CONVERSION
511 CATCH_EXCEPTION_NULL_PTR
512 CATCH_EXCEPTION_INVALID_ARG
515 bool JSWidget::setProperty(JSContextRef context,
516 JSObjectRef /*object*/,
517 JSStringRef propertyName,
518 JSValueRef /*jvalue*/,
519 JSValueRef* exception)
524 Converter converter(context);
526 std::string key = converter.toString(propertyName);
527 if (key == "preferences") {
528 LogError("Object is read only");
532 CATCH_EXCEPTION_INVALID_ARG
533 CATCH_EXCEPTION_CONVERSION
534 CATCH_EXCEPTION_NULL_PTR
539 JSObjectRef JSWidget::callAsConstructor(JSContextRef context,
540 JSObjectRef /*constructor*/,
541 size_t /*argumentCount*/,
542 const JSValueRef /*arguments*/[],
543 JSValueRef* /*exception*/)
545 LogDebug("widget constructor");
546 return JSObjectMake(context, JSWidget::getClassRef(), NULL);
549 #undef CATCH_EXCEPTION_NO_MODIFABLE
550 #undef CATCH_EXCEPTION_CONVERSION
551 #undef CATCH_EXCEPTION_NULL_PTR
552 #undef CATCH_EXCEPTION_PLATFORM_ERROR
553 #undef CATCH_EXCEPTION_SECURITY
554 #undef CATCH_EXCEPTION_OUT_OF_RANGE
555 #undef CATCH_EXCEPTION_INVALID_ARG