upload tizen1.0 source
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / NFC / JSNdefMessage.cpp
index db7e698..308c2fa 100755 (executable)
@@ -193,40 +193,42 @@ JSObjectRef JSNdefMessage::constructor(JSContextRef ctx, JSObjectRef constructor
        LogDebug("entered");
 
        NdefMessagePrivObject* mainPriv = static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(constructor));
-       JSContextRef global_context = mainPriv ? mainPriv->getContext() : ctx;
+
        Try {
-               if (argumentCount == 0)
-                       return createJSObject(global_context);
-               else if (argumentCount == 1) {
-                       if (JSValueIsNull(ctx, arguments[0]) || JSValueIsUndefined(ctx, arguments[0]) || !(JSIsArrayValue(ctx, arguments[0]) || JSNdefRecordArray::isObjectOfClass(ctx, arguments[0])))
+               if ((argumentCount == 0) || JSValueIsNull(ctx, arguments[0]) || JSValueIsUndefined(ctx, arguments[0]))
+                       return createJSObject(ctx);
+               else {
+                       if (!(JSIsArrayValue(ctx, arguments[0]) || JSNdefRecordArray::isObjectOfClass(ctx, arguments[0])))
                                ThrowMsg(ConversionException, "Parameter's type is not matched");
 
                        NFCConverter convert(ctx);
 
                        if (JSNdefRecordArray::isObjectOfClass(ctx, arguments[0])) {
-                               return createJSObject(global_context, JSNdefRecordArray::getNdefRecordArray(ctx, arguments[0]));
+                               return createJSObject(ctx, JSNdefRecordArray::getNdefRecordArray(ctx, arguments[0]));
                        }
 
                        JSObjectRef objArg = convert.toJSObjectRef(arguments[0]);
                        if (JSGetArrayLength(ctx, objArg) > 0) {
-                               JSValueRef element;
+                               bool isRecordArray = false;
+                               bool isByteArray = false;
                                for (std::size_t i = 0; i < JSGetArrayLength(ctx, objArg); ++i) {
-                                       element = JSGetArrayElement(ctx, objArg, i);
-                                       if (!JSValueIsNull(ctx, element))
+                                       JSValueRef element = JSGetArrayElement(ctx, objArg, i);
+                                       if (convert.isNdefRecord(element))
+                                               isRecordArray = true;
+                                       else
+                                               isByteArray = true;
+                                       if (isRecordArray && isByteArray)
                                                break;
                                }
-                               if (JSValueIsNull(ctx, element))
-                                       return createJSObject(global_context);
-                               else if (convert.isNdefRecord(element))
-                                       return createJSObject(global_context, convert.toVectorOfRecordHandles(arguments[0]));
+                               if (isRecordArray && isByteArray)
+                                       ThrowMsg(ConversionException, "Parameter's type is not matched");
+                               else if (isRecordArray)
+                                       return createJSObject(ctx, convert.toVectorOfRecordHandles(arguments[0]));
                                else
-                                       return createJSObject(global_context, convert.toVectorOfUChars(arguments[0]));
+                                       return createJSObject(ctx, convert.toVectorOfUChars(arguments[0]));
                        } else
-                               return createJSObject(global_context);
+                               return createJSObject(ctx);
                }
-               else
-                       ThrowMsg(InvalidArgumentException, "ArgumentCount is not correct");
-
        } Catch(ConversionException) {
                LogError("ConversionException: " << _rethrown_exception.GetMessage());
                *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
@@ -237,6 +239,10 @@ JSObjectRef JSNdefMessage::constructor(JSContextRef ctx, JSObjectRef constructor
                return NULL;
        } Catch (PlatformException) {
                LogError("PlatformException: " << _rethrown_exception.GetMessage());
+               *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::SERVICE_NOT_AVAILABLE, "Service Not Available");
+               return NULL;
+       } Catch (UnknownException) {
+               LogError("PlatformException: " << _rethrown_exception.GetMessage());
                *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
                return NULL;
        } Catch (WrtDeviceApis::Commons::Exception) {
@@ -267,12 +273,16 @@ JSValueRef JSNdefMessage::getProperty(JSContextRef context, JSObjectRef object,
                if (JSStringIsEqualToUTF8CString(propertyName, TIZEN10_NDEFMESSAGE_RECORDCOUNT)) {
                        return convert.toJSValueRefLong(NdefMessage->getRecordCount());
                } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN10_NDEFMESSAGE_RECORDS)) {
-                       return JSNdefRecordArray::createArray(privateObject->getContext(), NdefMessage);
+                       return JSNdefRecordArray::createArray(context, NdefMessage);
                }
        } Catch (ConversionException) {
                LogError("ConversionException: " << _rethrown_exception.GetMessage());
        } Catch (NullPointerException) {
                LogError("NullPointerException: " << _rethrown_exception.GetMessage());
+       } Catch (UnknownException) {
+               LogError("UnknownExceptionException: " << _rethrown_exception.GetMessage());
+       } Catch (PlatformException) {
+               LogError("PlatformExceptionException: " << _rethrown_exception.GetMessage());
        } Catch (WrtDeviceApis::Commons::Exception) {
                LogError("Exception: " << _rethrown_exception.GetMessage());
        }
@@ -292,7 +302,6 @@ bool JSNdefMessage::setProperty(JSContextRef context, JSObjectRef object,
                                ThrowMsg(NullPointerException, "Private object not initialized");
                        }
 
-                       INdefMessagePtr NdefMessage(privateObject->getObject());
                        NFCConverter convert(context);
 
                        if (JSValueIsNull(context, value) || JSValueIsUndefined(context, value) || !(JSIsArrayValue(context, value) || JSNdefRecordArray::isObjectOfClass(context, value))) {
@@ -316,13 +325,19 @@ bool JSNdefMessage::setProperty(JSContextRef context, JSObjectRef object,
                 }
        } Catch (NullPointerException) {
                LogError("NullPointerException: " << _rethrown_exception.GetMessage());
-               *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
+               *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
        } Catch (ConversionException) {
                LogError("ConversionException: " << _rethrown_exception.GetMessage());
                *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
        } Catch (InvalidArgumentException) {
                LogError("InvalidArgumentException: " << _rethrown_exception.GetMessage());
                *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
+       } Catch (PlatformException) {
+               LogError("PlatformException: " << _rethrown_exception.GetMessage());
+               *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::SERVICE_NOT_AVAILABLE, "Service Not Available");
+       } Catch (UnknownException) {
+               LogError("PlatformException: " << _rethrown_exception.GetMessage());
+               *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
        } Catch (WrtDeviceApis::Commons::Exception) {
                LogError("Exception: " << _rethrown_exception.GetMessage());
                *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
@@ -348,30 +363,28 @@ JSValueRef JSNdefMessage::toByte(JSContextRef context,
 {
        LogDebug("Entered ");
        Try {
-               if (argumentCount != 0) {
-                       LogError("JSNdefMessage::toByte TypeMismatchException");
-                       return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
-               }
-
+               AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_TAG_P2P_FUNCS);
+               TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
+       
                NdefMessagePrivObject* privateObject = static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(thisObject));
                if (NULL == privateObject) {
                        LogError("private object is null");
-                       return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
+                       return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
                }
 
-               AceSecurityStatus status = NFC_CHECK_ACCESS(privateObject->getContext(),NFC_FUNCTION_API_TAG_P2P_FUNCS);
-               TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
-
                INdefMessagePtr NdefMessage(privateObject->getObject());
                NFCConverter convert(context);
 
                return convert.toJSValueRef(NdefMessage->toByte());
-       } Catch (PlatformException) {
+       } Catch (UnknownException) {
                LogError("Exception: " << _rethrown_exception.GetMessage());
                return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
+       } Catch (PlatformException) {
+               LogError("Exception: " << _rethrown_exception.GetMessage());
+               return JSTizenExceptionFactory::postException(context, exception, JSTizenException::SERVICE_NOT_AVAILABLE, "Service Not Available");
        } Catch(NullPointerException) {
                LogError("Exception: " << _rethrown_exception.GetMessage());
-               return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
+               return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
        } Catch (WrtDeviceApis::Commons::Exception) {
                LogError("Exception: " << _rethrown_exception.GetMessage());
                return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");