Update change log and spec for wrt-plugins-tizen_0.4.70
[framework/web/wrt-plugins-tizen.git] / src / NFC / JSNFCAdapter.cpp
index 5805a85..8f4cf70 100755 (executable)
@@ -40,7 +40,7 @@
 #include "NFCListenerManager.h"
 #include "NFCFactory.h"
 #include "EventNFCChanged.h"
-#include "plugin_config.h"
+#include "plugin_config_impl.h"
 #include <Logger.h>
 
 using namespace std;
@@ -108,7 +108,7 @@ void JSNFCAdapter::finalize(JSObjectRef object)
        JSObjectSetPrivate(object, NULL);
 }
 
-JSObjectRef JSNFCAdapter::createJSObject(JSContextRef context) {
+JSObjectRef JSNFCAdapter::createJSObject(JSContextRef context, NFCManagerPrivObject *nfcManagerPriv) {
        LoggerD("entered");
 
        INFCAdapterPtr nfcAdapter(NFCFactory::getInstance().createNFCAdapterObject());
@@ -118,6 +118,7 @@ JSObjectRef JSNFCAdapter::createJSObject(JSContextRef context) {
                ThrowMsg(NullPointerException, "Can not new a NFCTag object");
        }
 
+       priv->copyAceCheckAccessFunction(nfcManagerPriv);
        return JSObjectMake(context, getClassRef(), priv);
 }
 
@@ -173,9 +174,15 @@ JSValueRef JSNFCAdapter::setTagListener (JSContextRef context, JSObjectRef objec
                JSValueRef* exception) {
        LoggerD("JSNFCAdapter::setTagListener Enter");
 
-       AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_TAG_FUNCS);
-       TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
-       JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(context);
+       NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(thisObject));
+       if (!privateObject) {
+               LoggerE("private object is null");
+               return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
+       }
+
+       TIZEN_CHECK_ACCESS(context, exception, privateObject,
+        NFC_FUNCTION_API_TAG_FUNCS);
+
        Try {
                ArgumentValidator validator(context, argumentCount, arguments);
                NFCChangedCallback callbacks;
@@ -184,7 +191,8 @@ JSValueRef JSNFCAdapter::setTagListener (JSContextRef context, JSObjectRef objec
                        callbacks = nfcConverter.toNFCChangedCallback(arguments[0]);
 
                std::vector<std::string> filterValue = validator.toStringVector(1, true);
-       
+
+               JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(context);
                JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(global_context, callbacks.onattach, NULL, true, true);
                JSCallbackManagerPtr detachedCallbackManager = JSCallbackManager::createObject(global_context, callbacks.ondetach, NULL);
                TagFilterPtr tagFilter(new TagFilter());
@@ -195,18 +203,14 @@ JSValueRef JSNFCAdapter::setTagListener (JSContextRef context, JSObjectRef objec
                        LoggerD("tag_types.at(i): " << filterValue.at(i));
                }
 
-               NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(thisObject));
-               if (!privateObject) {
-                       LoggerE("private object is null");
-                       return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
-               }
-
                INFCAdapterPtr nfcAdapter(privateObject->getObject());
 
                EventNFCChangedPrivateDataPtr privateData(
                    new EventNFCChangedPrivateData(callbackManager,
                                                    detachedCallbackManager)
                    );
+               privateData->copyAceCheckAccessFunction(privateObject);
+       
                EventNFCChangedEmitterPtr emitter(new EventNFCChangedEmitter);
                emitter->setListener(&NFCStaticController::getInstance());
                emitter->setEventPrivateData(DPL::StaticPointerCast<EventNFCChanged::PrivateDataType>(privateData));
@@ -241,10 +245,14 @@ JSValueRef JSNFCAdapter::setPeerListener (JSContextRef context, JSObjectRef obje
                JSValueRef* exception) {
        LoggerD("JSNFCAdapter::setPeerListener Enter");
 
-       AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_P2P_FUNCS);
-       TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
+       NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(thisObject));
+       if (!privateObject) {
+               LoggerE("private object is null");
+               return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
+       }
 
-       JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(context);
+       TIZEN_CHECK_ACCESS(context, exception, privateObject,
+        NFC_FUNCTION_API_P2P_FUNCS);
 
        Try {
                ArgumentValidator validator(context, argumentCount, arguments);
@@ -254,16 +262,11 @@ JSValueRef JSNFCAdapter::setPeerListener (JSContextRef context, JSObjectRef obje
 
                if (validator.toObject(0))
                        callbacks = nfcConverter.toNFCChangedCallback(arguments[0]);
-       
+
+               JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(context);
                JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(global_context, callbacks.onattach, NULL, true, true);
                JSCallbackManagerPtr detachedCallbackManager = JSCallbackManager::createObject(global_context, callbacks.ondetach, NULL);
 
-               NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(thisObject));
-               if (!privateObject) {
-                       LoggerE("private object is null");
-                       return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
-               }
-
                INFCAdapterPtr nfcAdapter(privateObject->getObject());
 
                EventNFCChangedPrivateDataPtr privateData(
@@ -271,6 +274,7 @@ JSValueRef JSNFCAdapter::setPeerListener (JSContextRef context, JSObjectRef obje
                                                    detachedCallbackManager)
                    );
 
+               privateData->copyAceCheckAccessFunction(privateObject);
                EventNFCChangedEmitterPtr emitter(new EventNFCChangedEmitter);
                emitter->setListener(&NFCStaticController::getInstance());
                emitter->setEventPrivateData(DPL::StaticPointerCast<EventNFCChanged::PrivateDataType>(privateData));
@@ -303,15 +307,15 @@ JSValueRef JSNFCAdapter::unsetTagListener(JSContextRef context, JSObjectRef obje
                size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) {
        LoggerD("JSNFCAdapter::unsetNFCTagListener Enter");
 
-       AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_TAG_FUNCS);
-       TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
-
        NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(thisObject));
        if (!privateObject) {
                LoggerE("private object is null");
                return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
        }
 
+       TIZEN_CHECK_ACCESS(context, exception, privateObject,
+        NFC_FUNCTION_API_TAG_FUNCS);
+
        Try     {
                INFCAdapterPtr nfcAdapter(privateObject->getObject());
 
@@ -337,17 +341,16 @@ JSValueRef JSNFCAdapter::unsetPeerListener(JSContextRef context, JSObjectRef obj
                size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) {
        LoggerD("JSNFCAdapter::unsetPeerListener Enter");
 
-       AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_P2P_FUNCS);
-       TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
-
        NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(thisObject));
        if (!privateObject) {
                LoggerE("private object is null");
                return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
        }
 
+       TIZEN_CHECK_ACCESS(context, exception, privateObject,
+        NFC_FUNCTION_API_P2P_FUNCS);
        Try {
-               INFCAdapterPtr nfcAdapter( privateObject->getObject() );
+               INFCAdapterPtr nfcAdapter( privateObject->getObject());
 
                nfcAdapter->unsetPeerListener();
 
@@ -372,21 +375,22 @@ JSValueRef JSNFCAdapter::getCachedMessage (JSContextRef context, JSObjectRef obj
                JSValueRef* exception) {
        LoggerD("JSNFCAdapter::getCachedMessage Enter");
 
-       AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_COMMON_FUNCS);
-       TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
-
        NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(thisObject));
        if (!privateObject) {
                LoggerE("private object is null");
                return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
        }
 
-       JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(context);
+       TIZEN_CHECK_ACCESS(context, exception, privateObject,
+        NFC_FUNCTION_API_COMMON_FUNCS);
+
        Try {
                INFCAdapterPtr nfcAdapter(privateObject->getObject());
                void *cachedMessage = nfcAdapter->getCachedMessage();
-               if (cachedMessage != NULL)
+               if (cachedMessage != NULL) {
+                       JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(context);
                        return JSNdefMessage::createJSObject(global_context, cachedMessage);
+               }
                return JSValueMakeNull(context);
        } Catch (WrtDeviceApis::Commons::UnknownException) {
                LoggerE("Exception: " << _rethrown_exception.GetMessage());
@@ -407,18 +411,16 @@ JSValueRef JSNFCAdapter::setPowered (JSContextRef context, JSObjectRef object,
                JSValueRef* exception) {
        LoggerD("Enter");
 
-       AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_ADMIN_FUNCS);
-       TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
-
-       JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(context);
-
        NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(thisObject));
        if (!privateObject) {
                LoggerE("private object is null");
                return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
        }
 
-       INFCAdapterPtr nfcAdapter(privateObject->getObject());
+       TIZEN_CHECK_ACCESS(context, exception, privateObject,
+        NFC_FUNCTION_API_ADMIN_FUNCS);
+
+       JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(context);
        JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(global_context, NULL, NULL, true, true);
 
        Try {
@@ -433,12 +435,11 @@ JSValueRef JSNFCAdapter::setPowered (JSContextRef context, JSObjectRef object,
                if (validator.toFunction(2, true))
                        callbackManager->setOnError(arguments[2]);
 
-               NFCConverter nfcConverter(context);
-
                EventNFCChangedSetPoweredPtr event(new EventNFCChangedSetPowered(state));
-               event->setPrivateData( DPL::StaticPointerCast<IEventPrivateData>(callbackManager) );
+               event->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManager) );
                event->setForAsynchronousCall(&NFCStaticController::getInstance());
                callbackManager->setObject(thisObject);
+               INFCAdapterPtr nfcAdapter(privateObject->getObject());
                nfcAdapter->setPowered(event);
                NFCAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, global_context);
                return JSValueMakeUndefined(context);