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/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>
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);\
44 #define CATCH_EXCEPTION_CONVERSION \
45 Catch(Commons::ConversionException) {\
46 LogError("Error on conversion");\
47 return JSDOMExceptionFactory::\
48 UnknownException.make(context, exception);\
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);\
58 #define CATCH_EXCEPTION_PLATFORM_ERROR \
59 Catch(Commons::PlatformException){\
60 LogError("PlatformException occured");\
61 return JSDOMExceptionFactory::\
62 UnknownException.make(context, exception);\
65 #define CATCH_EXCEPTION_SECURITY \
66 Catch(Commons::SecurityException){\
67 LogError("Security exception occured");\
68 return JSDOMExceptionFactory::\
69 SecurityException.make(context, exception);\
72 #define CATCH_EXCEPTION_OUT_OF_RANGE \
73 Catch(Commons::OutOfRangeException) {\
74 LogError("OutOfRangeException");\
75 return JSDOMExceptionFactory::\
76 QuotaExceededException.make(context, exception);\
79 #define CATCH_EXCEPTION_INVALID_ARG \
80 Catch(Commons::InvalidArgumentException) {\
81 LogError("Pair for given key doesnt exist");\
82 return JSValueMakeNull(context);\
85 #define WIDGET_PLUGIN_NAME "Widget"
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"
98 namespace WrtPlugins {
101 using namespace WrtDeviceApis;
102 using namespace WrtDeviceApis::Commons;
103 using namespace WrtDeviceApis::CommonsJavaScript;
104 using namespace WrtDeviceApis::Widget;
107 struct WidgetPrivateObject
109 Widget::Api::IWidgetPtr iwidget;
110 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;
135 LocalStorage::Api::ILocalStoragePtr getLocalStorage(int widgetId)
137 static int id = widgetId;
138 Assert(id == widgetId && "SPA is not longer supported");
140 static LocalStorage::Api::ILocalStoragePtr storage(
141 LocalStorage::Api::getLocalStorage(widgetId));
146 JSObjectRef getPreferences(JSObjectRef arg)
148 JSWidgetPrivateObject* priv =
149 static_cast<JSWidgetPrivateObject*>(JSObjectGetPrivate(arg));
152 LogError("Private object not initialized");
156 return priv->getObject()->preferencesObject;
159 JSObjectRef createPreferencesObject(JSContextRef context,
160 JSObjectRef widgetObject,
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;
170 JSObjectRef preferences = JSObjectMake(context,
171 JSPreferences::getClassRef(),
175 LogError("Preferences object is null");
178 //Unprotect is called in JSWidget::finalize
179 JSValueProtect(context, preferences);
185 JSClassDefinition JSWidget::m_classInfo = {
187 kJSClassAttributeNone,
197 NULL, //DeleteProperty,
198 NULL, //GetPropertyNames,
199 NULL, //CallAsFunction,
200 NULL, //CallAsConstructor,
202 NULL, //ConvertToType,
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 },
229 const JSClassRef JSWidget::getClassRef()
232 m_jsClassRef = JSClassCreate(&m_classInfo);
237 const JSClassDefinition* JSWidget::getClassInfo()
242 JSClassRef JSWidget::m_jsClassRef = JSClassCreate(JSWidget::getClassInfo());
244 void JSWidget::initialize(JSContextRef context,
248 LogError("object " << object);
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 int 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);
276 LogDebug("private object created");
279 Catch(Commons::InvalidArgumentException){
280 LogError("You should register widget id in ON_WIDGET_START");
287 void JSWidget::finalize(JSObjectRef object)
290 JSWidgetPrivateObject* priv =
291 static_cast<JSWidgetPrivateObject*>(JSObjectGetPrivate(object));
293 JSValueUnprotect(priv->getContext(), priv->getObject()->preferencesObject);
296 LogDebug("private object is realised");
299 JSValueRef JSWidget::getAuthor(JSContextRef context,
301 JSStringRef propertyName,
302 JSValueRef* exception)
307 Converter converter(context);
308 return converter.toJSValueRef(getIWidget(object)->getAuthor());
310 CATCH_EXCEPTION_CONVERSION
311 CATCH_EXCEPTION_NULL_PTR
312 CATCH_EXCEPTION_PLATFORM_ERROR
315 JSValueRef JSWidget::getAuthorEmail(JSContextRef context,
317 JSStringRef propertyName,
318 JSValueRef* exception)
321 Converter converter(context);
322 return converter.toJSValueRef(getIWidget(object)->getAuthorEmail());
324 CATCH_EXCEPTION_CONVERSION
325 CATCH_EXCEPTION_NULL_PTR
326 CATCH_EXCEPTION_PLATFORM_ERROR
329 JSValueRef JSWidget::getAuthorHref(JSContextRef context,
331 JSStringRef propertyName,
332 JSValueRef* exception)
335 Converter converter(context);
336 return converter.toJSValueRef(getIWidget(object)->getAuthorHref());
338 CATCH_EXCEPTION_CONVERSION
339 CATCH_EXCEPTION_NULL_PTR
340 CATCH_EXCEPTION_PLATFORM_ERROR
343 JSValueRef JSWidget::getDescription(JSContextRef context,
345 JSStringRef propertyName,
346 JSValueRef* exception)
349 Converter converter(context);
350 return converter.toJSValueRef(getIWidget(object)->getDescription());
352 CATCH_EXCEPTION_CONVERSION
353 CATCH_EXCEPTION_NULL_PTR
354 CATCH_EXCEPTION_PLATFORM_ERROR
357 JSValueRef JSWidget::getId(JSContextRef context,
359 JSStringRef propertyName,
360 JSValueRef* exception)
363 Converter converter(context);
364 return converter.toJSValueRef(getIWidget(object)->getId());
366 CATCH_EXCEPTION_CONVERSION
367 CATCH_EXCEPTION_NULL_PTR
368 CATCH_EXCEPTION_PLATFORM_ERROR
371 JSValueRef JSWidget::getName(JSContextRef context,
373 JSStringRef propertyName,
374 JSValueRef* exception)
377 Converter converter(context);
378 return converter.toJSValueRef(getIWidget(object)->getName());
380 CATCH_EXCEPTION_CONVERSION
381 CATCH_EXCEPTION_NULL_PTR
382 CATCH_EXCEPTION_PLATFORM_ERROR
385 JSValueRef JSWidget::getShortName(JSContextRef context,
387 JSStringRef propertyName,
388 JSValueRef* exception)
391 Converter converter(context);
392 return converter.toJSValueRef(getIWidget(object)->getShortName());
394 CATCH_EXCEPTION_CONVERSION
395 CATCH_EXCEPTION_NULL_PTR
396 CATCH_EXCEPTION_PLATFORM_ERROR
399 JSValueRef JSWidget::getVersion(JSContextRef context,
401 JSStringRef propertyName,
402 JSValueRef* exception)
405 Converter converter(context);
406 return converter.toJSValueRef(getIWidget(object)->getVersion());
408 CATCH_EXCEPTION_CONVERSION
409 CATCH_EXCEPTION_NULL_PTR
410 CATCH_EXCEPTION_PLATFORM_ERROR
413 JSValueRef JSWidget::getHeight(JSContextRef context,
415 JSStringRef propertyName,
416 JSValueRef* exception)
419 Converter converter(context);
420 return converter.toJSValueRef(getIWidget(object)->getHeight());
422 CATCH_EXCEPTION_CONVERSION
423 CATCH_EXCEPTION_NULL_PTR
424 CATCH_EXCEPTION_PLATFORM_ERROR
427 JSValueRef JSWidget::getWidth(JSContextRef context,
429 JSStringRef propertyName,
430 JSValueRef* exception)
433 Converter converter(context);
434 return converter.toJSValueRef(getIWidget(object)->getWidth());
436 CATCH_EXCEPTION_CONVERSION
437 CATCH_EXCEPTION_NULL_PTR
438 CATCH_EXCEPTION_PLATFORM_ERROR
441 bool JSWidget::hasProperty(JSContextRef context,
443 JSStringRef propertyName)
448 Converter converter(context);
450 std::string key = converter.toString(propertyName);
451 if(key=="preferences"){
455 Catch(Commons::InvalidArgumentException) {
456 LogDebug("Pair for given key doesnt exist");
459 Catch(Commons::ConversionException) {
460 LogError("Error on conversion");
463 Catch(Commons::NullPointerException) {
464 LogError("Error on pointer, null value");
467 Catch(Commons::PlatformException){
468 LogError("PlatformException occured");
474 JSValueRef JSWidget::getProperty(JSContextRef context,
476 JSStringRef propertyName,
477 JSValueRef* exception)
479 LogDebug("Object: " << object);
482 Converter converter(context);
484 std::string key = converter.toString(propertyName);
486 if(key=="preferences"){
487 Converter converter(context);
488 JSObjectRef pref = getPreferences(object);
491 LogError("Preferences object is NULL");
492 return JSValueMakeUndefined(context);
496 LogError("Property NOT supported: " << propertyName);
497 return JSValueMakeUndefined(context);
500 CATCH_EXCEPTION_CONVERSION
501 CATCH_EXCEPTION_NULL_PTR
502 CATCH_EXCEPTION_INVALID_ARG
505 bool JSWidget::setProperty(JSContextRef context,
507 JSStringRef propertyName,
509 JSValueRef* exception)
514 Converter converter(context);
516 std::string key = converter.toString(propertyName);
517 if (key == "preferences"){
518 LogError("Object is read only");
522 CATCH_EXCEPTION_INVALID_ARG
523 CATCH_EXCEPTION_CONVERSION
524 CATCH_EXCEPTION_NULL_PTR
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