Update wrt-plugins-common_0.3.53
[framework/web/wrt-plugins-common.git] / src / standards / W3C / Widget / JSWidget.cpp
index 3adba57..3f3f8c3 100644 (file)
@@ -29,9 +29,9 @@
 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
 #include "JSPreferences.h"
 #include <Widget/IWidget.h>
-#include "IFrameSupport.h"
 #include <LocalStorage/LocalStorageMgr.h>
 #include <Commons/WrtAccess/WrtAccess.h>
+#include <js-overlay/js_iframe_support.h>
 
 
 #define CATCH_EXCEPTION_NO_MODIFABLE \
@@ -197,7 +197,7 @@ JSClassDefinition JSWidget::m_classInfo = {
     NULL, //DeleteProperty,
     NULL, //GetPropertyNames,
     NULL, //CallAsFunction,
-    NULL, //CallAsConstructor,
+    callAsConstructor,
     NULL, //HasInstance,
     NULL, //ConvertToType,
 };
@@ -241,11 +241,13 @@ const JSClassDefinition* JSWidget::getClassInfo()
 
 JSClassRef JSWidget::m_jsClassRef = JSClassCreate(JSWidget::getClassInfo());
 
+JSContextRef JSWidget::m_globalContext = NULL;
+
 void JSWidget::initialize(JSContextRef context,
         JSObjectRef object)
 {
-    LogDebug("entered");
-    LogError("object " << object);
+    LogDebug("entered. Context : " << context);
+    LogDebug("Object: " << object);
 
     JSWidgetPrivateObject* priv =
         static_cast<JSWidgetPrivateObject*>(JSObjectGetPrivate(object));
@@ -290,10 +292,24 @@ void JSWidget::finalize(JSObjectRef object)
     JSWidgetPrivateObject* priv =
         static_cast<JSWidgetPrivateObject*>(JSObjectGetPrivate(object));
 
-    JSValueUnprotect(priv->getContext(), priv->getObject()->preferencesObject);
+    JSValueUnprotect(priv->getContext(),
+                     priv->getObject()->preferencesObject);
 
     delete priv;
-    LogDebug("private object is realised");
+    LogDebug("private object is released");
+}
+
+void JSWidget::acquireGlobalContext(java_script_context_t global_context,
+        js_object_instance_t iframe,
+        js_object_instance_t object)
+{
+    IFrameSupport::RegisterWidget(global_context, iframe, object);
+    if (!m_globalContext) {
+        m_globalContext = static_cast<JSContextRef>(global_context);
+        LogInfo("Global context acquired.");
+    } else {
+        LogInfo("Global context already set.");
+    }
 }
 
 JSValueRef JSWidget::getAuthor(JSContextRef context,
@@ -417,7 +433,11 @@ JSValueRef JSWidget::getHeight(JSContextRef context,
 {
     Try {
         Converter converter(context);
-        return converter.toJSValueRef(getIWidget(object)->getHeight());
+        unsigned int height = getIWidget(object)->getHeight();
+        if (0 == height) {
+            height = 1;
+        }
+        return converter.toJSValueRef(height);
     }
     CATCH_EXCEPTION_CONVERSION
     CATCH_EXCEPTION_NULL_PTR
@@ -431,7 +451,11 @@ JSValueRef JSWidget::getWidth(JSContextRef context,
 {
     Try {
         Converter converter(context);
-        return converter.toJSValueRef(getIWidget(object)->getWidth());
+        unsigned int width = getIWidget(object)->getWidth();
+        if (0 == width) {
+            width = 1;
+        }
+        return converter.toJSValueRef(width);
     }
     CATCH_EXCEPTION_CONVERSION
     CATCH_EXCEPTION_NULL_PTR
@@ -448,7 +472,7 @@ bool JSWidget::hasProperty(JSContextRef context,
         Converter converter(context);
 
         std::string key = converter.toString(propertyName);
-        if(key=="preferences"){
+        if (key == "preferences") {
             return true;
         }
     }
@@ -467,7 +491,6 @@ bool JSWidget::hasProperty(JSContextRef context,
     Catch(Commons::PlatformException){
         LogError("PlatformException occured");
     }
-
     return false;
 }
 
@@ -478,12 +501,12 @@ JSValueRef JSWidget::getProperty(JSContextRef context,
 {
     LogDebug("Object: " << object);
 
-    Try{
+    Try {
         Converter converter(context);
 
         std::string key = converter.toString(propertyName);
 
-        if(key=="preferences"){
+        if (key=="preferences") {
             Converter converter(context);
             JSObjectRef pref = getPreferences(object);
             if (!pref)
@@ -526,6 +549,21 @@ bool JSWidget::setProperty(JSContextRef context,
     return false;
 }
 
+JSObjectRef JSWidget::callAsConstructor(JSContextRef context,
+                                        JSObjectRef constructor,
+                                        size_t argumentCount,
+                                        const JSValueRef arguments[],
+                                        JSValueRef* exception)
+{
+    LogDebug("widget constructor");
+    if (!m_globalContext) {
+        LogError("Global context not set. Creating 'widget' object with "
+                "local context!");
+        return JSObjectMake(context, JSWidget::getClassRef(), NULL);
+    }
+    return JSObjectMake(m_globalContext, JSWidget::getClassRef(), NULL);
+}
+
 #undef CATCH_EXCEPTION_NO_MODIFABLE
 #undef CATCH_EXCEPTION_CONVERSION
 #undef CATCH_EXCEPTION_NULL_PTR