Update change log and spec for wrt-plugins-tizen_0.2.84
[framework/web/wrt-plugins-tizen.git] / src / standards / Tizen / LBS / JSLocationServiceProvider.cpp
index d04d14b..0a1231f 100755 (executable)
@@ -126,7 +126,7 @@ JSValueRef JSLocationServiceProvider::getProperty(JSContextRef ctx, JSObjectRef
                        JSObjectSetProperty(ctx, metaobject , key , valueRef ,kJSPropertyAttributeReadOnly, NULL );
                        JSStringRelease(key);
                        JSStringRelease(value);
-               }               
+               }
                return metaobject;
        }
        else if(JSStringIsEqualToUTF8CString(propertyName, "attribution")) {
@@ -148,7 +148,7 @@ JSValueRef JSLocationServiceProvider::getProperty(JSContextRef ctx, JSObjectRef
                delete []supportedOptionValues;
                return supportedOptionArray;
        }
-       else if(JSStringIsEqualToUTF8CString(propertyName, "connectivity")) {           
+       else if(JSStringIsEqualToUTF8CString(propertyName, "connectivity")) {
                JSStringRef conn = JSStringCreateWithUTF8CString(priv->getConnectivity().c_str());
                JSValueRef jsv = JSValueMakeString(ctx, conn);
                JSStringRelease(conn);
@@ -195,69 +195,113 @@ JSValueRef JSLocationServiceProvider::setOptions(JSContextRef ctx, JSObjectRef o
                return Commons::JSTizenExceptionFactory::postException(ctx,exception,  Commons::JSTizenException::TYPE_MISMATCH_ERROR, "The object is not LocationServiceProvider instance");
        }
 
-       JSValueRef convertedArguments[3];
-       JSObjectRef convertedArgumentObjects[3];
-       for( unsigned int i = 0 ; i < 3; i++ ){
-               if( i < argumentCount ){
-                       convertedArguments[i] = arguments[i];
-                       if( JSValueIsObject(ctx, arguments[i]) )
-                               convertedArgumentObjects[i] = JSValueToObject(ctx, arguments[i] , NULL );
-                       else
-                               convertedArgumentObjects[i] = NULL;
-               }else{
-                       convertedArguments[i] = JSValueMakeUndefined(ctx);
-                       convertedArgumentObjects[i] = NULL;
+       JSValueRef convertedArg[3];
+       JSObjectRef convertedArgObjects[3];
+       for( unsigned int i = 0 ; i < 3; i++ )
+       {
+               if(i<argumentCount)
+               {
+                       convertedArg[i] = arguments[i];
+                       convertedArgObjects[i] = JSValueToObject(ctx, arguments[i] , NULL );
+               }
+               else
+               {
+                       convertedArg[i] = JSValueMakeUndefined(ctx);
+                       convertedArgObjects[i] = NULL;
                }
        }
 
+       //Check the first argument
+       if(JSValueIsNull(ctx, convertedArg[0]) || JSValueIsUndefined(ctx, convertedArg[0]) || !JSValueIsObject(ctx, convertedArg[0]))
+       {
+               return Commons::JSTizenExceptionFactory::postException(ctx,exception,  Commons::JSTizenException::TYPE_MISMATCH_ERROR, "The first argument is invalid (null, undefined or not an object)");
+       }
+
+       JSObjectRef onsuccess;
+       JSObjectRef onfail;
+
        // The second argument is null or undefined or function object
-       if(  !JSValueIsNull(ctx, convertedArguments[1]) &&
-                 !JSValueIsUndefined(ctx, convertedArguments[1]) &&
-                 ( convertedArgumentObjects[1] == NULL || !JSObjectIsFunction(ctx, convertedArgumentObjects[1]) )
-         )
-               return Commons::JSTizenExceptionFactory::postException(ctx,exception,  Commons::JSTizenException::TYPE_MISMATCH_ERROR, "The first argument must be function or null");
+       bool isFunctionOnSuccess = false;
+       if(JSValueIsNull(ctx, convertedArg[1]) || JSValueIsUndefined(ctx, convertedArg[1]))
+       {
+               isFunctionOnSuccess = false;
+               LOGD("%s - 2rd argument is OK (Either null or undefined) ", __func__);
+       }
+       else if (JSObjectIsFunction(ctx, convertedArgObjects[1]))
+       {
+               isFunctionOnSuccess = true;
+               onsuccess = convertedArgObjects[1];
+               LOGD("%s - 2rd argument is OK (function) ", __func__);
+       }
+       else
+       {
+               return Commons::JSTizenExceptionFactory::postException(ctx,exception,  Commons::JSTizenException::TYPE_MISMATCH_ERROR, "The second argument must be a function");
+       }
 
        // The third argument is null or undefined or function object
-       if(  !JSValueIsNull(ctx, convertedArguments[2]) &&
-                 !JSValueIsUndefined(ctx, convertedArguments[2]) &&
-                 ( convertedArgumentObjects[2] == NULL || !JSObjectIsFunction(ctx, convertedArgumentObjects[2]))
-          )
-               return Commons::JSTizenExceptionFactory::postException(ctx,exception,  Commons::JSTizenException::TYPE_MISMATCH_ERROR, "The first argument must be function or null");
-
-       // The first argument is option object
-       JSObjectRef jsOptions = convertedArgumentObjects[0];
-       if( jsOptions == NULL ){
-               return Commons::JSTizenExceptionFactory::postException(ctx, exception,  Commons::JSTizenException::TYPE_MISMATCH_ERROR, "The option arguments is not Object");
+       bool isFunctionOnFail = false;
+       if(JSValueIsNull(ctx, convertedArg[2]) || JSValueIsUndefined(ctx, convertedArg[2]))
+       {
+               isFunctionOnFail = false;
+               LOGD("%s - 3rd argument is OK (Either null or undefined) ", __func__);
+       }
+       else if (JSObjectIsFunction(ctx, convertedArgObjects[2]))
+       {
+               isFunctionOnFail = true;
+               onfail = convertedArgObjects[2];
+               LOGD("%s - 3rd argument is OK (function) ", __func__);
+       }
+       else
+       {
+               return Commons::JSTizenExceptionFactory::postException(ctx,exception,  Commons::JSTizenException::TYPE_MISMATCH_ERROR, "The third argument must be a function");
        }
 
        // Initialize a CallbackData
        CallbackData * userdata = NULL;
+       userdata = new CallbackData();
 
-       JSObjectRef onsuccess = convertedArgumentObjects[1];
-       JSObjectRef onfail = convertedArgumentObjects[2];
-       if( onsuccess != NULL || onfail != NULL ){
-               userdata = new CallbackData();
+       //set the success callback
+       if(isFunctionOnSuccess)
+       {
+               userdata->onSuccess = onsuccess;
+               JSValueProtect(ctx, onsuccess);
+       }
+       else
+       {
                userdata->onSuccess = NULL;
+       }
+
+       //set the error callback
+       if(isFunctionOnFail)
+       {
+               userdata->onFail = onfail;
+               JSValueProtect(ctx, onfail);
+       }
+       else
+       {
                userdata->onFail = NULL;
+       }
 
-               if( onsuccess ){
-                       userdata->onSuccess = onsuccess;
-                       JSValueProtect(ctx, onsuccess);
-               }
-               if( onfail ){
-                       userdata->onFail = onfail;
-                       JSValueProtect(ctx, onfail);
-               }
+       // The first argument is option object
+       JSObjectRef jsOptions = convertedArgObjects[0];
+       if( jsOptions == NULL ){
+               return Commons::JSTizenExceptionFactory::postException(ctx, exception,  Commons::JSTizenException::TYPE_MISMATCH_ERROR, "The option arguments is not Object");
        }
 
        // Convert  Object to vector
-       std::vector<std::pair<std::string, std::string>> options = LBSUtil::convertObject(ctx, jsOptions);      
+       std::vector<std::pair<std::string, std::string>> options = LBSUtil::convertObject(ctx, jsOptions);
 
        // Call native setOption function
        int ret = priv->setOption(options , _setOptionCb , userdata, NULL);
+       LOGD("ret : %d ", ret);
 
        // Invalid options values
        if( ret != 0 ){
+               JSObjectRef error = Commons::JSTizenExceptionFactory::makeErrorObject(JSLBS::getGlobalContext(), Commons::JSTizenException::INVALID_VALUES_ERROR , "invalid option value");
+               JSValueRef args[1] = {error};
+               if( userdata->onFail )
+                       JSObjectCallAsFunction(JSLBS::getGlobalContext(), userdata->onFail  , NULL, 1, args, NULL);
+
                //remove callback values
                if( userdata != NULL ){
                        if( userdata->onSuccess )
@@ -266,10 +310,6 @@ JSValueRef JSLocationServiceProvider::setOptions(JSContextRef ctx, JSObjectRef o
                                JSValueUnprotect(ctx, userdata->onFail);
                        delete userdata;
                }
-
-               JSObjectRef error = Commons::JSTizenExceptionFactory::makeErrorObject(JSLBS::getGlobalContext(), Commons::JSTizenException::INVALID_VALUES_ERROR , "invalid option value");
-               JSValueRef args[1] = {error};
-               JSObjectCallAsFunction(JSLBS::getGlobalContext(), onfail  , NULL, 1, args, NULL);
        }
 
        return JSValueMakeUndefined(ctx);