upload tizen1.0 source
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Systeminfo / JSSysteminfo.cpp
index d41b654..fe0b251 100644 (file)
@@ -89,7 +89,6 @@ JSClassRef JSSysteminfo::m_jsClassRef = JSClassCreate(JSSysteminfo::getClassInfo
 void JSSysteminfo::initialize(JSContextRef context, JSObjectRef object)
 {
     JSSysteminfoPriv* priv = static_cast<JSSysteminfoPriv*>(JSObjectGetPrivate(object));
-    assert(!priv && "Invalid object creation.");
     ISysteminfoPtr Systeminfos(SysteminfoFactory::getInstance().getSysteminfos());
     priv = new JSSysteminfoPriv(context, Systeminfos);
     if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
@@ -114,35 +113,39 @@ JSValueRef JSSysteminfo::isSupported(JSContextRef context, JSObjectRef object, J
 {
     JSSysteminfoPriv *priv = static_cast<JSSysteminfoPriv*>(JSObjectGetPrivate(thisObject));
 
-       AceSecurityStatus status = SYSTEMINFO_CHECK_ACCESS(priv->getContext(),SYSTEMINFO_FUNCTION_API_IS_SUPPORTED);
-       TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
+    AceSecurityStatus status = SYSTEMINFO_CHECK_ACCESS(SYSTEMINFO_FUNCTION_API_IS_SUPPORTED);
+    TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
 
     Converter converter(context);
     Validator check(context, exception);
-    
-    if (argumentCount > 1 || argumentCount == 0) {
-        LogError("argument count is not correct");
-        return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
-    }
 
+    if (!priv) {
+        LogError("private object is null");
+        return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
+    }
     Try
     {
         ISysteminfoPtr Systeminfos(priv->getObject());
 
         bool retVal;
-        retVal = Systeminfos->isPropertyValid(priv->getContext(), arguments[0]);
+        
+        if (argumentCount == 0) {
+            retVal = Systeminfos->isPropertyValid(priv->getContext(), JSValueMakeUndefined(context));
+        } else {
+            retVal = Systeminfos->isPropertyValid(priv->getContext(), arguments[0]);        
+        }
 
         return converter.toJSValueRef(retVal);
     }
-
-    Catch(ConversionException) {
-        LogError("Error on conversion");
-        return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
+    Catch(WrtDeviceApis::Commons::ConversionException) {
+        return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error");
     }
-    Catch(InvalidArgumentException) {
-        LogError("JSSysteminfo::get InvalidArgumentException");
-        return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid parameter");
+    Catch(WrtDeviceApis::Commons::InvalidArgumentException){
+        return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error");
     }
+    Catch(WrtDeviceApis::Commons::Exception) {
+        return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown error");
+    }    
 
     return JSValueMakeUndefined(context);
 }
@@ -152,85 +155,65 @@ JSValueRef JSSysteminfo::getPropertyValue(JSContextRef context, JSObjectRef obje
 {
     JSSysteminfoPriv *priv = static_cast<JSSysteminfoPriv*>(JSObjectGetPrivate(thisObject));
 
-       AceSecurityStatus status = SYSTEMINFO_CHECK_ACCESS(priv->getContext(),SYSTEMINFO_FUNCTION_API_GET_PROPERTY_VALUE);
-       TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
-    
+    AceSecurityStatus status = SYSTEMINFO_CHECK_ACCESS(
+        SYSTEMINFO_FUNCTION_API_GET_PROPERTY_VALUE);
+    TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
+
     Converter converter(context);
     Validator check(context, exception);
+    std::string property;
+    JSValueRef errorCallback;
 
     if (!priv) {
         LogError("private object is null");
-        return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "unknown error");
+        return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Wrong Object");
     }
-
-    if (argumentCount == 0 || argumentCount > 4) {
-        LogError("argument count is not correct");
-        return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
+    if (argumentCount < 2) {
+        return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
     }
-
-    if (!JSValueIsNull(context, arguments[1]) && !JSValueIsUndefined(context, arguments[1]) && !check.isCallback(arguments[1])) {
-        LogError("wrong argument");
-        return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
-    }
-    
-    if (argumentCount > 2) {
-        if (!JSValueIsNull(context, arguments[2]) && !JSValueIsUndefined(context, arguments[2]) && !check.isCallback(arguments[2])) {
-            LogError("wrong argument");
-            return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
-        }
+    if (!check.isCallback(arguments[1])) {
+        return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
     }
 
     JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
-    if (check.isCallback(arguments[1])) {
-        onSuccessForCbm = arguments[1];
-    }
-    if (argumentCount > 2) {
+    onSuccessForCbm = arguments[1];
+    if (argumentCount == 3) {
         if (check.isCallback(arguments[2])) {
             onErrorForCbm = arguments[2];
+        } else if (!JSValueIsNull(context, arguments[2]) && !JSValueIsUndefined(context, arguments[2])) {
+            return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
         }
     }
     
     JSCallbackManagerPtr cbm(JSCallbackManager::createObject(priv->getContext(), onSuccessForCbm, onErrorForCbm, true, true));
-
+    JSValueProtect(priv->getContext(), thisObject);
 
     Try {
-        if (JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0])
-            || JSValueIsNull(context, arguments[1]) || JSValueIsUndefined(context, arguments[1])) {
-            LogError("successCallback parameter is JSNull/JSUndefined");
-            Throw(InvalidArgumentException);
-        }
-        
         ISysteminfoPtr Systeminfos(priv->getObject());
         
         EventGetSysteminfoPtr event(new EventGetSysteminfo());
-        
         event->setBasePropertyPtr(Systeminfos->getBasePropertyPtr(priv->getContext(), arguments[0]));
         event->setPrivateData(StaticPointerCast<IEventPrivateData>(cbm));
 
         SysteminfoListener& listener = SysteminfoListener::getInstance();
         event->setForAsynchronousCall(&listener);
 
-        JSObjectRef pendingOperation = makePendingOperation(context, event);
         Systeminfos->get(event);
-        
-        return pendingOperation;
+
+        return JSValueMakeUndefined(context);
     }
     
-    Catch(PendingOperationException) {
-        LogError("JSSysteminfo::get PendingOperationException");
-        cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, "invalid parameter"));
+    Catch(WrtDeviceApis::Commons::PendingOperationException) {
+        cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "Unknown error"));
     }
-    Catch(ConversionException) {
-        LogError("JSSysteminfo::get ConversionException");
-        cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error"));
+    Catch(WrtDeviceApis::Commons::ConversionException) {
+        cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error"));
     }
-    Catch(InvalidArgumentException) {
-        LogError("JSSysteminfo::get InvalidArgumentException");
-        cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, "invalid parameter"));
+    Catch(WrtDeviceApis::Commons::InvalidArgumentException){
+        cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error"));
     }
     Catch(WrtDeviceApis::Commons::Exception) {
-        LogError("JSSysteminfo::get Exception");
-        cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "unknown error"));
+        cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "Unknown error"));
     }
     return JSValueMakeUndefined(context);
 }
@@ -238,68 +221,56 @@ JSValueRef JSSysteminfo::getPropertyValue(JSContextRef context, JSObjectRef obje
 JSValueRef JSSysteminfo::addPropertyValueChangeListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
         size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
 {
-    LogDebug("enter");
-    JSValueRef property = NULL;
+    JSValueRef propertyId = NULL;
     WatchOption option;
-    int failId = -1;    
     JSSysteminfoPriv *priv = static_cast<JSSysteminfoPriv*>(JSObjectGetPrivate(thisObject));
 
-       AceSecurityStatus status = SYSTEMINFO_CHECK_ACCESS(priv->getContext(),SYSTEMINFO_FUNCTION_API_ADD_PROPERTY_VALUE_CHANGE_LISTENER);
-       TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
+    AceSecurityStatus status = SYSTEMINFO_CHECK_ACCESS(
+        SYSTEMINFO_FUNCTION_API_ADD_PROPERTY_VALUE_CHANGE_LISTENER);
+    TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
 
     Converter converter(context);
     Validator check(context, exception);
 
     if (!priv) {
         LogError("private object is null");
-        return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "unknown error");
+        return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Wrong Object");
     }
-
-    if (argumentCount == 0 || argumentCount > 4) {
-        LogError("wrong argument");
-        return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
+    if (argumentCount < 2) {
+        return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
     }
-
-    if (!JSValueIsNull(context, arguments[1]) && !JSValueIsUndefined(context, arguments[1]) && !check.isCallback(arguments[1])) {
-        LogError("wrong argument");
-        return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
+    if (!check.isCallback(arguments[1])) {
+        return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
     }
-
-    if (argumentCount > 2) {
-        if (!JSValueIsNull(context, arguments[2]) && !JSValueIsUndefined(context, arguments[2]) && !check.isCallback(arguments[2])) {
-            LogError("wrong argument");
-            return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
-        }
-    }
-
+    
     JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
-    if (check.isCallback(arguments[1])) {
-        onSuccessForCbm = arguments[1];
-    }
+    onSuccessForCbm = arguments[1];
     if (argumentCount > 2) {
         if (check.isCallback(arguments[2])) {
             onErrorForCbm = arguments[2];
+        } else if (!JSValueIsNull(context, arguments[2]) && !JSValueIsUndefined(context, arguments[2])) {
+            return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
         }
     }
+
     JSCallbackManagerPtr cbm(JSCallbackManager::createObject(priv->getContext(), onSuccessForCbm, onErrorForCbm, true, true));
+    JSValueProtect(priv->getContext(), thisObject);
 
     Try {
-        if (JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0])
-            || JSValueIsNull(context, arguments[1]) || JSValueIsUndefined(context, arguments[1])) {
-            LogError("successCallback parameter is JSNull/JSUndefined");
-            Throw(InvalidArgumentException);
-        }
-        property = arguments[0];
-        if (argumentCount > 3 && JSValueIsObject(context, arguments[3])) {
-            option.timeout = converter.toULong(JSUtils::getJSProperty(context, arguments[3], "timeout", exception));
-            option.highThreshold = converter.toDouble(JSUtils::getJSProperty(context, arguments[3], "highThreshold", exception));
-            option.lowThreshold = converter.toDouble(JSUtils::getJSProperty(context, arguments[3], "lowThreshold", exception));
+        if (argumentCount > 3) {
+            if (JSValueIsObject(context, arguments[3])) {
+                option.timeout = converter.toULong(JSUtils::getJSProperty(context, arguments[3], "timeout", exception));
+                option.highThreshold = converter.toDouble(JSUtils::getJSProperty(context, arguments[3], "highThreshold", exception));
+                option.lowThreshold = converter.toDouble(JSUtils::getJSProperty(context, arguments[3], "lowThreshold", exception));
+            } else if (!JSValueIsNull(context, arguments[3]) && !JSValueIsUndefined(context, arguments[3])) {
+                return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");        
+            }
         }
         ISysteminfoPtr Systeminfos(priv->getObject());
 
         EventWatchSysteminfoPtr event(new EventWatchSysteminfo);
         event->setWatchOption(option);
-        event->setBasePropertyPtr(Systeminfos->getBasePropertyPtr(priv->getContext(), property));
+        event->setBasePropertyPtr(Systeminfos->getBasePropertyPtr(priv->getContext(), arguments[0]));
         event->setPrivateData(StaticPointerCast<IEventPrivateData>(cbm));
 
         SysteminfoListener& listener = SysteminfoListener::getInstance();
@@ -308,64 +279,56 @@ JSValueRef JSSysteminfo::addPropertyValueChangeListener(JSContextRef context, JS
         Systeminfos->watch(event);
         LogDebug("event->getId()" << event->getId());
         return converter.toJSValueRef(event->getId());
-        }
-    
-    Catch(PendingOperationException) {
-        LogError("JSSysteminfo::get PendingOperationException");
-        cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, "invalid parameter"));
     }
-    Catch(ConversionException) {
-        LogError("JSSysteminfo::get ConversionException");
-        cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error"));
+
+    Catch(WrtDeviceApis::Commons::ConversionException) {
+        cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error"));
     }
-    Catch(InvalidArgumentException) {
-        LogError("JSSysteminfo::get InvalidArgumentException");
-        cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, "invalid parameter"));
+    Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
+        cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error"));
     }
     Catch(WrtDeviceApis::Commons::Exception) {
-        LogError("JSSysteminfo::get Exception");
-        cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "unknown error"));
+        cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "Unknown error"));
     }
+    
     return JSValueMakeUndefined(context);    
 }
 
 JSValueRef JSSysteminfo::removePropertyValueChangeListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
         size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
 {
-    int id = 0;
+    long id = 0;
     JSSysteminfoPriv *priv = static_cast<JSSysteminfoPriv*>(JSObjectGetPrivate(thisObject));    
 
-       AceSecurityStatus status = SYSTEMINFO_CHECK_ACCESS(priv->getContext(),SYSTEMINFO_FUNCTION_API_REMOVE_PROPERTY_VALUE_CHANGE_LISTENER);
-       TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
-    
-    Converter converter(context);
+    AceSecurityStatus status = SYSTEMINFO_CHECK_ACCESS(
+        SYSTEMINFO_FUNCTION_API_REMOVE_PROPERTY_VALUE_CHANGE_LISTENER);
+    TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
 
-    if (argumentCount == 0 || argumentCount > 1) {
-        LogError("argument count is not correct");
-        return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
-    } else if (!JSValueIsNumber(context, arguments[0])) {
-        LogError("wrong argument");
-        return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
+    Converter converter(context);
+    if (!priv) {
+        LogError("private object is null");
+        return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Wrong Object");
+    }
+    if (argumentCount < 1) {
+        return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
     }
 
     Try {
-               if (!priv) {
-                       ThrowMsg(NullPointerException, "No private object");
-               }
         ISysteminfoPtr Systeminfos(priv->getObject());
+        id = static_cast<long>(converter.toLong(arguments[0]));
 
-               if (argumentCount == 1) {
-                       id = static_cast<int>(converter.toInt(arguments[0]));
-               }
                LogDebug("clearWatch id = " << id );
                Systeminfos->clearWatch(id);
                return JSValueMakeUndefined(context);
        }
-       Catch (InvalidArgumentException) {
-       LogError("JSSysteminfo::get InvalidArgumentException");
-       }
-       Catch (WrtDeviceApis::Commons::Exception) {
-        LogError("JSSysteminfo::get Exception");
+    Catch(WrtDeviceApis::Commons::ConversionException) {
+        return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
+    }    
+    Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
+        return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error");
+    }
+    Catch(WrtDeviceApis::Commons::Exception) {
+        return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown error");        
     }
 
     return JSValueMakeUndefined(context);