2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file FIo_MessagePortProxy.cpp
19 * @brief This is the implementation file for the _MessagePortProxy class.
25 #include <unique_ptr.h>
27 #include <message-port.h>
29 #include <FBaseSysLog.h>
30 #include <FBaseByteBuffer.h>
32 #include <FBase_StringConverter.h>
33 #include <FApp_AppInfo.h>
35 #include "FIo_MessagePortProxy.h"
39 using namespace Tizen::App;
40 using namespace Tizen::Io;
41 using namespace Tizen::Base;
42 using namespace Tizen::Base::Collection;
43 using namespace Tizen::Base::Runtime;
45 namespace Tizen { namespace Io
48 //static const int MAX_MESSAGE_SIZE = 16 * 1024;
51 ConvertBundleToMap(const char *pKey, const int type, const bundle_keyval_t *pVal, void *pData)
53 //SysLog(NID_IO, "CB key = %s", pKey);
55 HashMap* pMap = static_cast<HashMap*>(pData);
57 if (pKey && pVal && pMap)
64 bundle_keyval_get_basic_val(const_cast<bundle_keyval_t*>(pVal), reinterpret_cast<void**>(&pStr), &size);
67 pMap->Add(new (std::nothrow) String(pKey), new (std::nothrow) String(pStr));
71 case BUNDLE_TYPE_BYTE:
72 bundle_keyval_get_basic_val(const_cast<bundle_keyval_t*>(pVal), reinterpret_cast<void**>(&pStr), &size);
74 //SysLog(NID_IO, "Bundle byte value = %s, size = %d", pStr, size);
78 ByteBuffer* pBuffer = new (std::nothrow) ByteBuffer();
79 SysTryReturnVoidResult(NID_IO, pMap != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
80 result r = pBuffer->Construct(size);
81 SysTryCatch(NID_IO, r == E_SUCCESS, delete pBuffer, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
83 r = pBuffer->SetArray((const byte*)pStr, 0, size);
84 SysTryCatch(NID_IO, r == E_SUCCESS, delete pBuffer, E_INVALID_ARG, "[E_INVALID_ARG] The buffer argument is invalid.");
88 pMap->Add(new (std::nothrow) String(pKey), pBuffer);
93 SysLog(NID_APP, "Invalid type for %s : %d", pKey, type);
103 OnMessageReceived(int id, const char* remote_app_id, const char* remote_port, bool trusted_port, bundle* data)
105 SysLog(NID_IO, "Message received");
107 _MessagePortProxy* p = _MessagePortProxy::GetProxy();
110 char* pLocalPort = null;
112 ret = messageport_get_local_port_name(id, &pLocalPort);
113 if (pLocalPort == null)
115 SysSecureLog(NID_IO, "No local port for id: %d", id);
121 //SysLog(NID_IO, "local port name: %s", pLocalPort);
123 _IMessagePortListener* pListener = null;
124 p->__listeners.GetValue(pLocalPort, pListener);
128 if (pListener != null)
130 HashMap* pMap = new (std::nothrow) HashMap(SingleObjectDeleter);
131 SysTryCatch(NID_IO, pMap != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
133 result r = pMap->Construct();
134 SysTryCatch(NID_IO, r == E_SUCCESS, delete pMap, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
136 bundle_foreach(data, ConvertBundleToMap, pMap);
138 if (remote_app_id == null) // Uni-directional
140 pListener->OnMessageReceivedN(pMap);
142 else // Bi-directional
144 SysSecureLog(NID_IO, "Message received from [%s:%s], trusted: %d", remote_app_id, remote_port, trusted_port);
146 pListener->OnMessageReceivedN(remote_app_id, remote_port, trusted_port, pMap);
155 OnTrustedMessageReceived(int id, const char* remote_app_id, const char* remote_port, bool trusted_port, bundle* data)
157 SysLog(NID_IO, "Trusted message received");
159 _MessagePortProxy* p = _MessagePortProxy::GetProxy();
162 char* pLocalPort = null;
163 ret = messageport_get_local_port_name(id, &pLocalPort);
164 if (pLocalPort == null)
166 SysSecureLog(NID_IO, "No local port for id: %d", id);
172 _IMessagePortListener* pListener = null;
173 p->__trustedListeners.GetValue(pLocalPort, pListener);
177 if (pListener != null)
179 HashMap* pMap = new (std::nothrow) HashMap(SingleObjectDeleter);
180 SysTryCatch(NID_IO, pMap != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
182 result r = pMap->Construct();
183 SysTryCatch(NID_IO, r == E_SUCCESS, delete pMap, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
185 bundle_foreach(data, ConvertBundleToMap, pMap);
187 if (remote_app_id == null) // Uni-directional
189 pListener->OnMessageReceivedN(pMap);
191 else // Bi-directional
193 SysSecureLog(NID_IO, "Trusted message received from [%s:%s], trusted: %d", remote_app_id, remote_port, trusted_port);
195 pListener->OnMessageReceivedN(remote_app_id, remote_port, trusted_port, pMap);
203 _MessagePortProxy::_MessagePortProxy(void)
207 _MessagePortProxy::~_MessagePortProxy(void)
212 _MessagePortProxy::Construct(void)
214 static _StringHashProvider hashProvider;
215 static _StringComparer stringComparer;
217 __listeners.Construct(0, 0, hashProvider, stringComparer);
218 __trustedListeners.Construct(0, 0, hashProvider, stringComparer);
219 __ids.Construct(0, 0, hashProvider, stringComparer);
220 __trustedIds.Construct(0, 0, hashProvider, stringComparer);
222 __appId = _AppInfo::GetApplicationId();
229 _MessagePortProxy::RegisterMessagePort(const String& localPort, bool isTrusted,
230 const _IMessagePortListener& listener)
232 SysSecureLog(NID_IO, "Register a message port : [%ls:%ls]", __appId.GetPointer(), localPort.GetPointer());
235 bool contain = false;
237 unique_ptr<char[]> pLocal(_StringConverter::CopyToCharArrayN(localPort));
241 ret = messageport_register_local_port(pLocal.get(), OnMessageReceived);
242 SysTryReturnResult(NID_IO, ret >= 0, E_SYSTEM, "Failed to register the local message port. %d", ret);
244 __listeners.ContainsKey(localPort, contain);
248 __listeners.Add(localPort, const_cast<_IMessagePortListener*>(&listener));
249 __ids.Add(localPort, ret);
253 __listeners.SetValue(localPort, const_cast<_IMessagePortListener*>(&listener));
254 __ids.SetValue(localPort, ret);
260 ret = messageport_register_trusted_local_port(pLocal.get(), OnTrustedMessageReceived);
261 SysTryReturnResult(NID_IO, ret >= 0, E_SYSTEM, "Failed to register the trusted local message port. %d", ret);
263 __trustedListeners.ContainsKey(localPort, contain);
267 __trustedListeners.Add(localPort, const_cast<_IMessagePortListener*>(&listener));
268 __trustedIds.Add(localPort, ret);
272 __trustedListeners.SetValue(localPort, const_cast<_IMessagePortListener*>(&listener));
273 __trustedIds.SetValue(localPort, ret);
282 _MessagePortProxy::RequestRemotePort(const AppId& remoteAppId,
283 const String& remotePort,
286 SysSecureLog(NID_IO, "Request a remote message port [%ls:%ls]", remoteAppId.GetPointer(), remotePort.GetPointer());
288 result r = E_SUCCESS;
292 unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(remoteAppId));
293 unique_ptr<char[]> pPort(_StringConverter::CopyToCharArrayN(remotePort));
297 ret = messageport_check_remote_port(pAppId.get(), pPort.get(), &exist);
301 ret = messageport_check_trusted_remote_port(pAppId.get(), pPort.get(), &exist);
306 r = ConvertToResult(ret);
308 SysTryReturnResult(NID_IO, r != E_CERTIFICATE_VERIFICATION_FAILED, E_CERTIFICATE_VERIFICATION_FAILED, "The target application is not signed with the same certificate.");
310 SysLogException(NID_IO, E_SYSTEM, "[E_SYSTEM] Failed to request the remote message port.");
315 SysTryReturnResult(NID_IO, exist, E_OBJ_NOT_FOUND, "The remote message port is not found.");
321 _MessagePortProxy::SendMessage(const AppId& remoteAppId, const String& remotePort, bool isTrusted, const HashMap* pMap)
323 SysSecureLog(NID_IO, "Send a unidirectional message to remote port [%ls:%ls]", remoteAppId.GetPointer(), remotePort.GetPointer());
328 // Convert Map to bundle
329 bundle* b = ConvertMapToBundleN(pMap, &size);
330 SysTryReturnResult(NID_IO, b != null, E_INVALID_ARG, "The argument is invalid.");
331 //SysTryReturnResult(NID_IO, size <= MAX_MESSAGE_SIZE, E_MAX_EXCEEDED, "The size of the message has exceeded the maximum limit.");
333 unique_ptr<char[]> pRemoteAppId(_StringConverter::CopyToCharArrayN(remoteAppId));
334 unique_ptr<char[]> pRemotePort(_StringConverter::CopyToCharArrayN(remotePort));
338 ret = messageport_send_message(pRemoteAppId.get(), pRemotePort.get(), b);
342 ret = messageport_send_trusted_message(pRemoteAppId.get(), pRemotePort.get(), b);
349 result r = ConvertToResult(ret);
351 SysTryReturnResult(NID_IO, r != E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "The remote message port is not found.");
352 SysTryReturnResult(NID_IO, r != E_CERTIFICATE_VERIFICATION_FAILED, E_CERTIFICATE_VERIFICATION_FAILED, "The target application is not signed with the same certificate.");
353 SysTryReturnResult(NID_IO, r != E_MAX_EXCEEDED, E_MAX_EXCEEDED, "The size of the message has exceeded the maximum limit.");
355 SysLogException(NID_IO, E_SYSTEM, "[E_SYSTEM] Failed to request the remote message port.");
364 _MessagePortProxy::SendMessage(const String& localPort, bool isTrustedLocal, const AppId& remoteAppId, const String& remotePort, bool isTrustedRemote, const HashMap* pMap)
366 SysSecureLog(NID_IO, "Send a bidirectional message from [%ls:%ls] to [%ls:%ls]", __appId.GetPointer(), localPort.GetPointer(), remoteAppId.GetPointer(), remotePort.GetPointer());
368 result r = E_SUCCESS;
373 r = __ids.GetValue(localPort, id);
377 r = __trustedIds.GetValue(localPort, id);
380 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_SYSTEM, "Internal system errors.");
384 // Convert Map to bundle
385 bundle* b = ConvertMapToBundleN(pMap, &size);
386 SysTryReturnResult(NID_IO, b != null, E_INVALID_ARG, "The argument is invalid.");
387 //SysTryReturnResult(NID_IO, size <= MAX_MESSAGE_SIZE, E_MAX_EXCEEDED, "The size of the message has exceeded the maximum limit.");
389 unique_ptr<char[]> pRemoteAppId(_StringConverter::CopyToCharArrayN(remoteAppId));
390 unique_ptr<char[]> pRemotePort(_StringConverter::CopyToCharArrayN(remotePort));
393 if (!isTrustedRemote)
395 ret = messageport_send_bidirectional_message(id, pRemoteAppId.get(), pRemotePort.get(), b);
399 ret = messageport_send_bidirectional_trusted_message(id, pRemoteAppId.get(), pRemotePort.get(), b);
406 result r = ConvertToResult(ret);
408 SysTryReturnResult(NID_IO, r != E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "The remote message port is not found.");
409 SysTryReturnResult(NID_IO, r != E_CERTIFICATE_VERIFICATION_FAILED, E_CERTIFICATE_VERIFICATION_FAILED, "The target application is not signed with the same certificate.");
410 SysTryReturnResult(NID_IO, r != E_MAX_EXCEEDED, E_MAX_EXCEEDED, "The size of the message has exceeded the maximum limit.");
412 SysLogException(NID_IO, E_SYSTEM, "[E_SYSTEM] Failed to request the remote message port.");
421 _MessagePortProxy::GetProxy(void)
423 static _MessagePortProxy* pProxy = null;
427 unique_ptr<_MessagePortProxy> p(new (std::nothrow) _MessagePortProxy);
428 SysTryReturn(NID_IO, p != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
430 result r = p->Construct();
431 SysTryReturn(NID_IO, r == E_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] Failed to initialize a message port proxy.");
433 pProxy = p.release();
436 SetLastResult(E_SUCCESS);
441 _MessagePortProxy::ConvertToResult(int error)
443 SysLog(NID_IO, "Native error = %d", error);
447 case MESSAGEPORT_ERROR_IO_ERROR:
450 case MESSAGEPORT_ERROR_OUT_OF_MEMORY:
451 return E_OUT_OF_MEMORY;
453 case MESSAGEPORT_ERROR_INVALID_PARAMETER:
454 return E_INVALID_ARG;
456 case MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND:
457 return E_OBJ_NOT_FOUND;
459 case MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH:
460 return E_CERTIFICATE_VERIFICATION_FAILED;
462 case MESSAGEPORT_ERROR_MAX_EXCEEDED:
463 return E_MAX_EXCEEDED;
473 _MessagePortProxy::ConvertMapToBundleN(const HashMap* pMap, int* pSize)
475 bundle *b = bundle_create();
478 std::unique_ptr<IMapEnumerator> pEnum (pMap->GetMapEnumeratorN());
479 while(pEnum->MoveNext() == E_SUCCESS)
481 const String* pKey = dynamic_cast<const String*>(pEnum->GetKey());
482 const Object* pValue = pEnum->GetValue();
486 unique_ptr<char[]> pKeyData(_StringConverter::CopyToCharArrayN(*pKey));
488 if (typeid(*pValue) == typeid(const String)) // String
490 const String* pStr = static_cast<const String*>(pValue);
491 size += pStr->GetLength() * sizeof(wchar_t);
493 unique_ptr<char[]> pValueData(_StringConverter::CopyToCharArrayN(*pStr));
495 //SysLog(NID_IO, "key: %s, value: %s", pKeyData.get(), pValueData.get());
497 bundle_add(b, pKeyData.get(), pValueData.get());
501 if (typeid(*pValue) == typeid(const ByteBuffer)) // ByteBuffer
503 const ByteBuffer* pBuffer = static_cast<const ByteBuffer*>(pValue);
504 size += pBuffer->GetLimit();
506 //SysLog(NID_IO, "Add a string key: %s, byte value: %s", pKeyData.get(), pBuffer->GetPointer());
508 bundle_add_byte(b, pKeyData.get(), pBuffer->GetPointer(), pBuffer->GetLimit());
512 SysLog(NID_IO, "The value type is not supported.");
521 SysLog(NID_IO, "The key type is not supported.");