upload tizen1.0 source
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Tizen / JSTizen.cpp
index ad92400..2745cd1 100755 (executable)
 
 #include <cassert>
 #include <vector>
-#include <dpl/log.h>
+#include <dpl/log/log.h>
 #include <CommonsJavaScript/PrivateObject.h>
 #include <CommonsJavaScript/Converter.h>
-#include <Commons/WrtWrapper/IWrtWrapper.h>
-#include <Commons/WrtWrapper/WrtWrappersMgr.h>
+#include <Commons/WrtAccess/WrtAccess.h>
 #include <API/Filter/FilterTypes.h>
 #include <API/Filter/AnyType.h>
 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
 #include "JSTizen.h"
 #include "JSFeature.h"
 #include "plugin_config.h"
+#include <WidgetDB/WidgetDBMgr.h>
+#include <WidgetDB/IWidgetDB.h>
+#include <PluginManager/PluginManagerFactory.h>
 
 #include <iostream>
 
@@ -45,8 +47,10 @@ namespace TizenApis {
 namespace Tizen1_0 {
 namespace Tizen {
 
+using namespace WrtDeviceApis;
 using namespace WrtDeviceApis::Commons;
 using namespace TizenApis::Api::Tizen;
+using namespace WrtDeviceApis::PluginManager::Api;
 
 JSClassDefinition JSTizen::m_classInfo =
 {
@@ -61,7 +65,7 @@ JSClassDefinition JSTizen::m_classInfo =
     hasProperty,
     getProperty,
     setProperty,
-    deleteProperty,
+    NULL, //deleteProperty,
     getPropertyNames,
     NULL,
     NULL,
@@ -94,19 +98,15 @@ void JSTizen::initialize(JSContextRef context, JSObjectRef object)
     if (priv) {
         delete priv;
     }
-    IWrtWrapperPtr wrapper;
-    Try
-    {
-        wrapper = WrtWrappersMgr::getInstance().getWrtWrapper(context);
-    }
-    Catch(WrtDeviceApis::Commons::InvalidArgumentException)
-    {
-        LogError("Cannot get WRT wrapper.");
-        LogError("You should register your wrapper in ON_WIDGET_START");
-        return;
-    }
 
-    PluginOnDemandPrivPtr privObject(new PluginOnDemandPriv(wrapper));
+    int widgetId = WrtAccessSingleton::Instance().getWidgetId();
+    PluginOnDemandPrivPtr privObject(
+        new PluginOnDemandPriv(
+                       PluginManagerFactory::getInstance().getPluginManager(
+                           widgetId,
+                           PLUGIN_NAME,
+                           object,
+                           context)));
 
     priv = new TizenPrivate(context, privObject);
     if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
@@ -131,15 +131,31 @@ JSClassRef JSTizen::getClassRef()
     return m_classRef;
 }
 
-bool JSTizen::deleteProperty(JSContextRef context, JSObjectRef object,
-        JSStringRef propertyName, JSValueRef* exception)
-{
-    return true;
-}
+//bool JSTizen::deleteProperty(JSContextRef context, JSObjectRef object,
+//        JSStringRef propertyName, JSValueRef* exception)
+//{
+//    return true;
+//}
 
 void JSTizen::getPropertyNames(JSContextRef context, JSObjectRef object,
         JSPropertyNameAccumulatorRef propertyNames)
 {
+    Try
+    {
+        TizenPrivate* priv =
+            static_cast<TizenPrivate*>(JSObjectGetPrivate(object));
+        if (!priv || !priv->getObject()) {
+            LogError("No private obejct");
+            return;
+        }
+        PluginOnDemandPrivPtr privObj = priv->getObject();
+        privObj->getPluginManager()->addPropertiesToList(propertyNames);
+
+    }
+    Catch(Commons::Exception)
+    {
+        LogError("Error occured");
+    }
 }
 
 bool JSTizen::hasInstance(JSContextRef context, JSObjectRef constructor,
@@ -161,31 +177,15 @@ bool JSTizen::hasProperty(JSContextRef context,
 
     Try
     {
-        WrtDeviceApis::CommonsJavaScript::Converter converter(context);
-        std::string propName = converter.toString(propertyName);
-               LogDebug("checking property:" + propName);
-
-               TizenPrivate* priv = static_cast<TizenPrivate*>(JSObjectGetPrivate(object));
+        TizenPrivate* priv = static_cast<TizenPrivate*>(JSObjectGetPrivate(object));
 
         if (!priv || !priv->getObject()) {
             LogError("No private obejct");
             return false;
         }
-        PluginOnDemandPrivPtr privObj = priv->getObject();
-        PluginOnDemandPriv::ObjectListPtr objList = privObj->getObjectList();
 
-        if (objList->find(propName) != objList->end()) {
-            //already set so property exists
-            return true;
-        }
-
-        std::string pluginUri(PLUGIN_NAME);
-        pluginUri.append(".").append(propName);
-        if (privObj->getWrtWrapper()->isJSObjectAvailable(pluginUri)) {
-                       LogDebug(propName + " is available");
-            //property can be set
-            return true;
-        }
+        return priv->getObject()->getPluginManager()->hasChild(
+            CommonsJavaScript::Converter(context).toString(propertyName));
     }
     Catch(WrtDeviceApis::Commons::Exception)
     {
@@ -203,36 +203,15 @@ JSValueRef JSTizen::getProperty(JSContextRef context, JSObjectRef object,
 
     Try
     {
-        WrtDeviceApis::CommonsJavaScript::Converter converter(context);
-        std::string propName = converter.toString(propertyName);
-
         TizenPrivate* priv =
                 static_cast<TizenPrivate*>(JSObjectGetPrivate(object));
         if (!priv || !priv->getObject()) {
             LogError("No private obejct");
             return false;
         }
-        PluginOnDemandPrivPtr privObj = priv->getObject();
-        PluginOnDemandPriv::ObjectListPtr objList = privObj->getObjectList();
-
-        PluginOnDemandPriv::ObjectList::iterator it = objList->find(propName);
-        if (it != objList->end()) {
-            //return already set value
-            return it->second;
-        }
 
-        std::string pluginUri(PLUGIN_NAME);
-        pluginUri.append(".").append(propName);
-           LogDebug("load new object : " << pluginUri);
-        //load new plugin
-        privObj->setAllowSettingProperty(true);
-        privObj->getWrtWrapper()->loadJSObject(pluginUri);
-        privObj->setAllowSettingProperty(false);
-        it = objList->find(propName);
-        if (it != objList->end()) {
-            //return value
-            return it->second;
-        }
+        return priv->getObject()->getPluginManager()->getProperty(
+            CommonsJavaScript::Converter(context).toString(propertyName));
     }
     Catch(WrtDeviceApis::Commons::Exception)
     {
@@ -250,24 +229,15 @@ bool JSTizen::setProperty(JSContextRef context, JSObjectRef object,
     LogDebug("entered");
     Try
     {
-        WrtDeviceApis::CommonsJavaScript::Converter converter(context);
-        std::string propName = converter.toString(propertyName);
-
         TizenPrivate* priv =
                 static_cast<TizenPrivate*>(JSObjectGetPrivate(object));
         if (!priv || !priv->getObject()) {
             LogError("No private obejct");
             return false;
         }
-        PluginOnDemandPrivPtr privObj = priv->getObject();
-
-        std::string pluginUri(PLUGIN_NAME);
-        pluginUri.append(".").append(propName);
-        if (privObj->getAllowSettingProperty()) {
-                JSValueProtect(priv->getContext(), value);
-                (*privObj->getObjectList())[propName] = value;
-            return true;
-        }
+        priv->getObject()->getPluginManager()->setProperty(
+            CommonsJavaScript::Converter(context).toString(propertyName),
+            value);
     }
     Catch(WrtDeviceApis::Commons::Exception)
     {
@@ -285,14 +255,13 @@ JSValueRef JSTizen::listAvailableFeatures(JSContextRef context, JSObjectRef obje
         const JSValueRef arguments[], JSValueRef* exception)
 {
     LogDebug("entered");
-    IWrtWrapperPtr wrapper;
 
     TizenPrivate* priv = static_cast<TizenPrivate*>(JSObjectGetPrivate(thisObject));
     assert(priv);
 
     /*
      * Current Tizen spec assures that tizen is always available
-    AccessStatus status = DEVICEAPIS_CHECK_ACCESS(priv->getContext(),
+    AccessStatus status = DEVICEAPIS_CHECK_ACCESS(
                                  DEVICEAPIS_FUNCTION_API_LIST_AVAIL_FEATURES);
 
     SYNC_ACCESS_STATUS_HANDLER(status, context, exception);
@@ -303,17 +272,17 @@ JSValueRef JSTizen::listAvailableFeatures(JSContextRef context, JSObjectRef obje
         if (!priv) {
             ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "No private object");
         }
-//        wrapper = priv->getObject();
            PluginOnDemandPrivPtr privObj = priv->getObject();
-           wrapper = privObj->getWrtWrapper();
     }
     catch(const WrtDeviceApis::Commons::Exception &ex)
     {
-        LogError("couldn't get wrapper, " << ex.GetMessage());
         return WrtDeviceApis::CommonsJavaScript::JSDOMExceptionFactory::UnknownException.make(context, exception);
     }
     JSObjectRef result = JSCreateArrayObject(context, 0, NULL);
-    IWrtWrapper::Features features = wrapper->getRegisteredFeatures();
+    int widgetId = WrtAccessSingleton::Instance().getWidgetId();
+    WidgetDB::Api::IWidgetDBPtr widgetDB =
+        WidgetDB::Api::getWidgetDB(widgetId);
+    WidgetDB::Api::Features features = widgetDB->getRegisteredFeatures();
     for (size_t i=0; i<features.size(); ++i) {
         JSFeaturePrivateObject *priv = new JSFeaturePrivateObject(context, features[i]);
         JSObjectRef tempObj = JSObjectMake(context, JSFeature::getClassRef(), priv);
@@ -340,26 +309,26 @@ JSValueRef JSTizen::listActivatedFeatures(JSContextRef context, JSObjectRef obje
 
     /*
      * Current Tizen spec assures that tizen is always available
-     AccessStatus status = DEVICEAPIS_CHECK_ACCESS(priv->getContext(),
+     AccessStatus status = DEVICEAPIS_CHECK_ACCESS(
                                  DEVICEAPIS_FUNCTION_API_LIST_ACTIV_FEATURES);
 
     SYNC_ACCESS_STATUS_HANDLER(status, context, exception);*/
-    IWrtWrapperPtr wrapper;
     try
     {
         if (!priv) {
             ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "No private object");
         }
         PluginOnDemandPrivPtr privObj = priv->getObject();
-        wrapper = privObj->getWrtWrapper();
     }
     catch(const WrtDeviceApis::Commons::Exception &ex)
     {
-        LogError("couldn't get wrapper, " << ex.GetMessage());
         return WrtDeviceApis::CommonsJavaScript::JSDOMExceptionFactory::UnknownException.make(context, exception);
     }
     JSObjectRef result = JSCreateArrayObject(context, 0, NULL);
-    IWrtWrapper::Features features = wrapper->getWidgetFeatures();
+    int widgetId = WrtAccessSingleton::Instance().getWidgetId();
+    WidgetDB::Api::IWidgetDBPtr widgetDB =
+        WidgetDB::Api::getWidgetDB(widgetId);
+    WidgetDB::Api::Features features = widgetDB->getWidgetFeatures();
     for (size_t i=0; i<features.size(); ++i) {
         JSFeaturePrivateObject *priv = new JSFeaturePrivateObject(context, features[i]);
         JSObjectRef tempObj = JSObjectMake(context, JSFeature::getClassRef(), priv);