Remove unused static constant
[platform/framework/native/appfw.git] / src / io / FIo_MessagePortProxy.cpp
index 57a7217..23ad6ea 100644 (file)
@@ -1,5 +1,4 @@
 //
-// Open Service Platform
 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
 //
 // Licensed under the Apache License, Version 2.0 (the License);
 #include <typeinfo>
 #include <unique_ptr.h>
 
-#include <package_manager.h>
-
 #include <message-port.h>
 
 #include <FBaseSysLog.h>
+#include <FBaseByteBuffer.h>
+
 #include <FBase_StringConverter.h>
 #include <FApp_AppInfo.h>
 
@@ -46,10 +45,12 @@ using namespace Tizen::Base::Runtime;
 namespace Tizen { namespace Io
 {
 
+//static const int MAX_MESSAGE_SIZE = 16 * 1024;
+
 static void
 ConvertBundleToMap(const char *pKey, const int type, const bundle_keyval_t *pVal, void *pData)
 {
-       SysLog(NID_IO, "CB key = %s", pKey);
+       //SysLog(NID_IO, "CB key = %s", pKey);
 
        HashMap* pMap = static_cast<HashMap*>(pData);
 
@@ -70,16 +71,18 @@ ConvertBundleToMap(const char *pKey, const int type, const bundle_keyval_t *pVal
                        case BUNDLE_TYPE_BYTE:
                                bundle_keyval_get_basic_val(const_cast<bundle_keyval_t*>(pVal), reinterpret_cast<void**>(&pStr), &size);
 
-                               SysLog(NID_IO, "Bundle byte value = %s, size = %d", pStr, size);
+                               //SysLog(NID_IO, "Bundle byte value = %s, size = %d", pStr, size);
 
                                if (pStr)
                                {
                                        ByteBuffer* pBuffer = new (std::nothrow) ByteBuffer();
-                                       SysTryReturn(NID_IO, pMap != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
+                                       SysTryReturnVoidResult(NID_IO, pMap != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
                                        result r = pBuffer->Construct(size);
-                                       SysTryReturn(NID_IO, r == E_SUCCESS, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
+                                       SysTryCatch(NID_IO, r == E_SUCCESS, delete pBuffer, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
+
+                                       r = pBuffer->SetArray((const byte*)pStr, 0, size);
+                                       SysTryCatch(NID_IO, r == E_SUCCESS, delete pBuffer, E_INVALID_ARG, "[E_INVALID_ARG] The buffer argument is invalid.");
 
-                                       pBuffer->SetArray((const byte*)pStr, 0, size);
                                        pBuffer->Flip();
 
                                        pMap->Add(new (std::nothrow) String(pKey), pBuffer);
@@ -91,6 +94,9 @@ ConvertBundleToMap(const char *pKey, const int type, const bundle_keyval_t *pVal
                                break;
                }
        }
+
+CATCH:
+       return;
 }
 
 static void
@@ -106,13 +112,13 @@ OnMessageReceived(int id, const char* remote_app_id, const char* remote_port, bo
        ret = messageport_get_local_port_name(id, &pLocalPort);
        if (pLocalPort == null)
        {
-               SysLog(NID_IO, "No local port for id: %d", id);
+               SysSecureLog(NID_IO, "No local port for id: %d", id);
 
                bundle_free(data);
                return;
        }
 
-       SysLog(NID_IO, "local port name: %s", pLocalPort);
+       //SysLog(NID_IO, "local port name: %s", pLocalPort);
 
        _IMessagePortListener* pListener = null;
        p->__listeners.GetValue(pLocalPort, pListener);
@@ -127,8 +133,6 @@ OnMessageReceived(int id, const char* remote_app_id, const char* remote_port, bo
                result r = pMap->Construct();
                SysTryCatch(NID_IO, r == E_SUCCESS, delete pMap, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
 
-               SysLog(NID_IO, "bundle to map");
-
                bundle_foreach(data, ConvertBundleToMap, pMap);
 
                if (remote_app_id == null) // Uni-directional
@@ -137,7 +141,7 @@ OnMessageReceived(int id, const char* remote_app_id, const char* remote_port, bo
                }
                else // Bi-directional
                {
-                       SysLog(NID_IO, "Message received from [%s:%s], trusted: %d", remote_app_id, remote_port, trusted_port);
+                       SysSecureLog(NID_IO, "Message received from [%s:%s], trusted: %d", remote_app_id, remote_port, trusted_port);
 
                        pListener->OnMessageReceivedN(remote_app_id, remote_port, trusted_port, pMap);
                }
@@ -159,7 +163,7 @@ OnTrustedMessageReceived(int id, const char* remote_app_id, const char* remote_p
        ret = messageport_get_local_port_name(id, &pLocalPort);
        if (pLocalPort == null)
        {
-               SysLog(NID_IO, "No local port for id: %d", id);
+               SysSecureLog(NID_IO, "No local port for id: %d", id);
 
                bundle_free(data);
                return;
@@ -186,7 +190,8 @@ OnTrustedMessageReceived(int id, const char* remote_app_id, const char* remote_p
                }
                else // Bi-directional
                {
-                       SysLog(NID_IO, "Trusted message received from [%s:%s], trusted: %d", remote_app_id, remote_port, trusted_port);
+                       SysSecureLog(NID_IO, "Trusted message received from [%s:%s], trusted: %d", remote_app_id, remote_port, trusted_port);
+
                        pListener->OnMessageReceivedN(remote_app_id, remote_port, trusted_port, pMap);
                }
        }
@@ -224,9 +229,8 @@ result
 _MessagePortProxy::RegisterMessagePort(const String& localPort,        bool isTrusted,
                                                                        const _IMessagePortListener& listener)
 {
-       SysLog(NID_IO, "Register a message port : [%ls:%ls]", __appId.GetPointer(), localPort.GetPointer());
+       SysSecureLog(NID_IO, "Register a message port : [%ls:%ls]", __appId.GetPointer(), localPort.GetPointer());
 
-       result r = E_SUCCESS;
        int ret = 0;
        bool contain = false;
 
@@ -279,7 +283,7 @@ _MessagePortProxy::RequestRemotePort(const AppId& remoteAppId,
                                                                        const String& remotePort,
                                                                        bool isTrusted)
 {
-       SysLog(NID_IO, "Request a remote message port [%ls:%ls]", remoteAppId.GetPointer(), remotePort.GetPointer());
+       SysSecureLog(NID_IO, "Request a remote message port [%ls:%ls]", remoteAppId.GetPointer(), remotePort.GetPointer());
 
        result r = E_SUCCESS;
        int ret = 0;
@@ -316,13 +320,15 @@ _MessagePortProxy::RequestRemotePort(const AppId& remoteAppId,
 result
 _MessagePortProxy::SendMessage(const AppId& remoteAppId, const String& remotePort, bool isTrusted, const HashMap* pMap)
 {
-       SysLog(NID_IO, "Send a unidirectional message to remote port [%ls:%ls]", remoteAppId.GetPointer(), remotePort.GetPointer());
+       SysSecureLog(NID_IO, "Send a unidirectional message to remote port [%ls:%ls]", remoteAppId.GetPointer(), remotePort.GetPointer());
 
        int ret = 0;
+       int size = 0;
 
        // Convert Map to bundle
-       bundle* b = ConvertMapToBundleN(pMap);
-       SysTryReturnResult(NID_IO, b != null, E_SYSTEM, "Internal system errors.");
+       bundle* b = ConvertMapToBundleN(pMap, &size);
+       SysTryReturnResult(NID_IO, b != null, E_INVALID_ARG, "The argument is invalid.");
+       //SysTryReturnResult(NID_IO, size <= MAX_MESSAGE_SIZE, E_MAX_EXCEEDED, "The size of the message has exceeded the maximum limit.");
 
        unique_ptr<char[]> pRemoteAppId(_StringConverter::CopyToCharArrayN(remoteAppId));
        unique_ptr<char[]> pRemotePort(_StringConverter::CopyToCharArrayN(remotePort));
@@ -357,7 +363,7 @@ _MessagePortProxy::SendMessage(const AppId& remoteAppId, const String& remotePor
 result
 _MessagePortProxy::SendMessage(const String& localPort, bool isTrustedLocal, const AppId& remoteAppId, const String& remotePort, bool isTrustedRemote, const HashMap* pMap)
 {
-       SysLog(NID_IO, "Send a bidirectional message from [%ls:%ls] to [%ls:%ls]", __appId.GetPointer(), localPort.GetPointer(), remoteAppId.GetPointer(), remotePort.GetPointer());
+       SysSecureLog(NID_IO, "Send a bidirectional message from [%ls:%ls] to [%ls:%ls]", __appId.GetPointer(), localPort.GetPointer(), remoteAppId.GetPointer(), remotePort.GetPointer());
 
        result r = E_SUCCESS;
        int id = 0;
@@ -373,9 +379,12 @@ _MessagePortProxy::SendMessage(const String& localPort, bool isTrustedLocal, con
 
        SysTryReturnResult(NID_IO, r == E_SUCCESS, E_SYSTEM, "Internal system errors.");
 
+       int size = 0;
+
        // Convert Map to bundle
-       bundle* b = ConvertMapToBundleN(pMap);
-       SysTryReturnResult(NID_IO, b != null, E_SYSTEM, "Internal system errors.");
+       bundle* b = ConvertMapToBundleN(pMap, &size);
+       SysTryReturnResult(NID_IO, b != null, E_INVALID_ARG, "The argument is invalid.");
+       //SysTryReturnResult(NID_IO, size <= MAX_MESSAGE_SIZE, E_MAX_EXCEEDED, "The size of the message has exceeded the maximum limit.");
 
        unique_ptr<char[]> pRemoteAppId(_StringConverter::CopyToCharArrayN(remoteAppId));
        unique_ptr<char[]> pRemotePort(_StringConverter::CopyToCharArrayN(remotePort));
@@ -461,9 +470,10 @@ _MessagePortProxy::ConvertToResult(int error)
 }
 
 bundle*
-_MessagePortProxy::ConvertMapToBundleN(const HashMap* pMap)
+_MessagePortProxy::ConvertMapToBundleN(const HashMap* pMap, int* pSize)
 {
        bundle *b = bundle_create();
+       int size = 0;
 
        std::unique_ptr<IMapEnumerator> pEnum (pMap->GetMapEnumeratorN());
        while(pEnum->MoveNext() == E_SUCCESS)
@@ -477,7 +487,10 @@ _MessagePortProxy::ConvertMapToBundleN(const HashMap* pMap)
 
                        if (typeid(*pValue) == typeid(const String)) // String
                        {
-                               unique_ptr<char[]> pValueData(_StringConverter::CopyToCharArrayN(*(static_cast<const String*>(pValue))));
+                               const String* pStr = static_cast<const String*>(pValue);
+                               size += pStr->GetLength() * sizeof(wchar_t);
+
+                               unique_ptr<char[]> pValueData(_StringConverter::CopyToCharArrayN(*pStr));
 
                                //SysLog(NID_IO, "key: %s, value: %s", pKeyData.get(), pValueData.get());
 
@@ -488,15 +501,15 @@ _MessagePortProxy::ConvertMapToBundleN(const HashMap* pMap)
                                if (typeid(*pValue) == typeid(const ByteBuffer)) // ByteBuffer
                                {
                                        const ByteBuffer* pBuffer = static_cast<const ByteBuffer*>(pValue);
+                                       size += pBuffer->GetLimit();
 
-                                       // Add a byte data to bundle
-                                       SysLog(NID_IO, "Add a string key: %s, byte value: %s", pKeyData.get(), pBuffer->GetPointer());
+                                       //SysLog(NID_IO, "Add a string key: %s, byte value: %s", pKeyData.get(), pBuffer->GetPointer());
 
                                        bundle_add_byte(b, pKeyData.get(), pBuffer->GetPointer(), pBuffer->GetLimit());
                                }
                                else
                                {
-                                       SysLog(NID_IO, "Not supported");
+                                       SysLog(NID_IO, "The value type is not supported.");
 
                                        bundle_free(b);
                                        return null;
@@ -505,13 +518,14 @@ _MessagePortProxy::ConvertMapToBundleN(const HashMap* pMap)
                }
                else
                {
-                       SysLog(NID_IO, "Not supported");
+                       SysLog(NID_IO, "The key type is not supported.");
 
                        bundle_free(b);
                        return null;
                }
        }
 
+       *pSize = size;
        return b;
 }