merge wrt-plugins-tizen_0.2.0-2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Application / JSApplicationServiceData.cpp
index 89fc7ca..1182fb7 100755 (executable)
 #include <dpl/shared_ptr.h>
 #include <CommonsJavaScript/Converter.h>
 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
+#include <Tizen/Common/JSTizenExceptionFactory.h>
+#include <Tizen/Common/JSTizenException.h>
+
 #include "JSApplicationServiceData.h"
 #include "ApplicationConverter.h"
+#include "ApplicationUtil.h"
+
+using namespace TizenApis::Api::Application;
+using namespace WrtDeviceApis::Commons;
+using namespace WrtDeviceApis::CommonsJavaScript;
+using namespace TizenApis::Commons;
 
 namespace TizenApis {
 namespace Tizen1_0 {
 namespace Application {
 
-using namespace Api::Application;
-using namespace WrtDeviceApis;
-using namespace WrtDeviceApis::CommonsJavaScript;
-
-
-//namespace {
-//     const char* APPLICATION_SERVICE_DATA_KEY = "key";
-//     const char* APPLICATION_SERVICE_DATA_VALUE = "value";
-//}
+namespace {
+const char* APPLICATION_SERVICE_DATA_KEY = "key";
+const char* APPLICATION_SERVICE_DATA_VALUE = "value";
+}
 
 JSClassRef JSApplicationServiceData::m_classRef = NULL;
 
@@ -48,8 +52,8 @@ JSClassDefinition JSApplicationServiceData::m_classInfo =
        initialize,
        finalize,
        NULL, //hasProperty,
-       NULL, //GetProperty,
-       NULL, //SetProperty,
+       getProperty, //GetProperty,
+       setProperty, //SetProperty,
        NULL, //DeleteProperty,
        NULL, //getPropertyNames,
        NULL,
@@ -59,8 +63,10 @@ JSClassDefinition JSApplicationServiceData::m_classInfo =
 };
 
 JSStaticValue JSApplicationServiceData::m_property[] = {
-       { "key", getDataKey, setDataKey, kJSPropertyAttributeNone },
-       { "value", getDataValue, setDataValue, kJSPropertyAttributeNone },
+//     { APPLICATION_SERVICE_DATA_KEY, getDataKey, setDataKey, kJSPropertyAttributeNone },
+//     { APPLICATION_SERVICE_DATA_VALUE, getDataValue, setDataValue, kJSPropertyAttributeNone },
+       { APPLICATION_SERVICE_DATA_KEY, getProperty, setProperty, kJSPropertyAttributeNone },
+       { APPLICATION_SERVICE_DATA_VALUE, getProperty, setProperty, kJSPropertyAttributeNone },
        { 0, 0, 0, 0 }
 };
 
@@ -85,35 +91,43 @@ JSValueRef JSApplicationServiceData::createJSObject(JSContextRef context,
        privateData->setKey(key);
        privateData->setValue(value);
        JSApplicationServiceDataPriv *priv = new JSApplicationServiceDataPriv(context, privateData);
+       
        JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
        if (NULL == jsValueRef) {
                LogError("object creation error");
                return JSValueMakeUndefined(context);
        }
+       
        return jsValueRef;
 }
 
 JSObjectRef JSApplicationServiceData::constructor(JSContextRef context, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
 {
-       ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
-
-       if ((argumentCount == 2) && JSValueIsString(context, arguments[0]) && JSIsArrayValue(context, arguments[1])) {
-               return JSValueToObject(context, createJSObject(context, converter->toString(arguments[0]), converter->toVectorOfStrings(arguments[1])), exception);
-       } else {
-               // throw exception
+       ApplicationUtil util(context, exception);
+       
+       if (argumentCount != 2 ||
+               !util.isString(arguments[0]) || !util.isArray(arguments[1])) {
+               LogError("Wrong parameters");
+               *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
                return NULL;
        }
+
+       ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
+       return JSValueToObject(context, createJSObject(context, converter->toString(arguments[0]), converter->toVectorOfStrings(arguments[1])), exception);
 }
 
 void JSApplicationServiceData::initialize(JSContextRef context, JSObjectRef object)
 {
-       //assert(NULL != JSObjectGetPrivate(object));
+       LogInfo(">> initialize");
 }
 
 void JSApplicationServiceData::finalize(JSObjectRef object)
 {
-       // TODO : check
-       //delete (JSObjectGetPrivate(object));
+    LogInfo(">> finalize");
+    JSApplicationServiceDataPriv* priv = static_cast<JSApplicationServiceDataPriv*>(JSObjectGetPrivate(object));
+    JSObjectSetPrivate(object, NULL);
+    LogDebug("Deleting ApplicationService object");
+    delete priv;       
 }
 
 bool JSApplicationServiceData::isObjectOfClass(JSContextRef context, JSValueRef value)
@@ -126,11 +140,11 @@ ApplicationServiceDataPtr JSApplicationServiceData::getPrivData(JSObjectRef obje
        LogDebug("entered");
        JSApplicationServiceDataPriv *priv = static_cast<JSApplicationServiceDataPriv*>(JSObjectGetPrivate(object));
        if (!priv) {
-               ThrowMsg(Commons::NullPointerException, "Private object is null");
+               ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
        }
        ApplicationServiceDataPtr result = priv->getObject();
        if (!result) {
-               ThrowMsg(Commons::NullPointerException, "Private object is null");
+               ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
        }
        return result;
 }
@@ -138,95 +152,69 @@ ApplicationServiceDataPtr JSApplicationServiceData::getPrivData(JSObjectRef obje
 ApplicationServiceDataPtr JSApplicationServiceData::getApplicationServiceData(JSContextRef context, JSValueRef value)
 {
        if (!isObjectOfClass(context, value)) {
-               Throw(Commons::InvalidArgumentException);
+               Throw(WrtDeviceApis::Commons::InvalidArgumentException);
        }
        JSObjectRef object = JSValueToObject(context, value, NULL);
        if (!object) {
-               Throw(Commons::InvalidArgumentException);
+               Throw(WrtDeviceApis::Commons::InvalidArgumentException);
        }
        JSApplicationServiceDataPriv *priv = static_cast<JSApplicationServiceDataPriv*>(JSObjectGetPrivate(object));
        if (!priv) {
-               Throw(Commons::NullPointerException);
+               Throw(WrtDeviceApis::Commons::NullPointerException);
        }
        return priv->getObject();
 }
 
-JSValueRef JSApplicationServiceData::getDataKey(JSContextRef context,
-               JSObjectRef object,
-               JSStringRef propertyName,
-               JSValueRef* exception)
-{
-       Try
-       {
-               CommonsJavaScript::Converter converter(context);
-               ApplicationServiceDataPtr privateData = getPrivData(object);
-               return converter.toJSValueRef(privateData->getKey());
-       }
-       Catch(Commons::Exception)
-       {
-               LogWarning("trying to get incorrect value");
-       }
-       return JSValueMakeUndefined(context);
-}
 
-bool JSApplicationServiceData::setDataKey(JSContextRef context,
+JSValueRef JSApplicationServiceData::getProperty(JSContextRef context,
                JSObjectRef object,
                JSStringRef propertyName,
-               JSValueRef key,
                JSValueRef* exception)
 {
-       Try
-       {
-               ApplicationServiceDataPtr privateData = getPrivData(object);
-               CommonsJavaScript::Converter converter(context);
-               privateData->setKey(converter.toString(key));
-               return true;
-       }
-       Catch(Commons::Exception)
-       {
-               LogWarning("trying to set incorrect value");
-       }
-       JSDOMExceptionFactory::TypeMismatchException.make(context, exception);
-       return false;
-}
-
-JSValueRef JSApplicationServiceData::getDataValue(JSContextRef context,
-               JSObjectRef object,
-               JSStringRef propertyName,
-               JSValueRef* exception)
-{
-       Try
-       {
+       Try     {
                WrtDeviceApis::CommonsJavaScript::Converter converter(context);
                ApplicationServiceDataPtr privateData = getPrivData(object);
-
-               return converter.toJSValueRef(privateData->getValue());
-       }
-       Catch(WrtDeviceApis::Commons::Exception)
-       {
-               LogWarning("trying to get incorrect value");
-       }
+               
+               if (JSStringIsEqualToUTF8CString(propertyName, APPLICATION_SERVICE_DATA_KEY)) {
+                       //LogDebug("JSApplicationServiceData::getProperty::key " << privateData->getKey());
+                       return converter.toJSValueRef(privateData->getKey());
+               } else if (JSStringIsEqualToUTF8CString(propertyName, APPLICATION_SERVICE_DATA_VALUE)) {
+                       //LogDebug("JSApplicationServiceData::getProperty::value " << privateData->getValue());
+                       return converter.toJSValueRef(privateData->getValue());
+               } 
+       } Catch(WrtDeviceApis::Commons::Exception) {
+        LogError("Exception: " << _rethrown_exception.GetMessage());
+               return JSDOMExceptionFactory::UnknownException.make(context, exception);
+    }
+       
        return JSValueMakeUndefined(context);
 }
 
-bool JSApplicationServiceData::setDataValue(JSContextRef context,
-               JSObjectRef object,
-               JSStringRef propertyName,
-               JSValueRef value,
-               JSValueRef* exception)
+
+bool JSApplicationServiceData::setProperty(JSContextRef context,
+                       JSObjectRef object,
+                       JSStringRef propertyName,
+                       JSValueRef value,
+                       JSValueRef* exception)
 {
-       Try
-       {
-               ApplicationServiceDataPtr privateData = getPrivData(object);
+       Try     {
                WrtDeviceApis::CommonsJavaScript::Converter converter(context);
-               privateData->setValue(converter.toVectorOfStrings(value));
-               return true;
-       }
-       Catch(WrtDeviceApis::Commons::Exception)
-       {
-               LogWarning("trying to set incorrect value");
-       }
-       JSDOMExceptionFactory::TypeMismatchException.make(context, exception);
+               ApplicationServiceDataPtr privateData = getPrivData(object);
+               
+               if (JSStringIsEqualToUTF8CString(propertyName, APPLICATION_SERVICE_DATA_KEY)) {
+                       //LogDebug("JSApplicationServiceData::getProperty::key " << privateData->getKey());
+                       privateData->setKey(converter.toString(value));
+                       return true;
+               } else if (JSStringIsEqualToUTF8CString(propertyName, APPLICATION_SERVICE_DATA_VALUE)) {
+                       //LogDebug("JSApplicationServiceData::getProperty::value " << privateData->getValue());
+                       privateData->setValue(converter.toVectorOfStrings(value));
+                       return true;                    
+               } 
+       } Catch(WrtDeviceApis::Commons::Exception) {
+               LogError("Exception: " << _rethrown_exception.GetMessage());
+        JSDOMExceptionFactory::UnknownException.make(context, exception);
+    }
+       
        return false;
 }