2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FIo_MessagePortProxy.cpp
20 * @brief This is the implementation file for the _MessagePortProxy class.
26 #include <unique_ptr.h>
28 #include <package_manager.h>
30 #include <message-port.h>
32 #include <FBaseSysLog.h>
33 #include <FBase_StringConverter.h>
34 #include <FApp_AppInfo.h>
36 #include "FIo_MessagePortProxy.h"
40 using namespace Tizen::App;
41 using namespace Tizen::Io;
42 using namespace Tizen::Base;
43 using namespace Tizen::Base::Collection;
44 using namespace Tizen::Base::Runtime;
46 namespace Tizen { namespace Io
49 static const int MAX_MESSAGE_SIZE = 16 * 1024;
52 ConvertBundleToMap(const char *pKey, const int type, const bundle_keyval_t *pVal, void *pData)
54 //SysLog(NID_IO, "CB key = %s", pKey);
56 HashMap* pMap = static_cast<HashMap*>(pData);
58 if (pKey && pVal && pMap)
65 bundle_keyval_get_basic_val(const_cast<bundle_keyval_t*>(pVal), reinterpret_cast<void**>(&pStr), &size);
68 pMap->Add(new (std::nothrow) String(pKey), new (std::nothrow) String(pStr));
72 case BUNDLE_TYPE_BYTE:
73 bundle_keyval_get_basic_val(const_cast<bundle_keyval_t*>(pVal), reinterpret_cast<void**>(&pStr), &size);
75 //SysLog(NID_IO, "Bundle byte value = %s, size = %d", pStr, size);
79 ByteBuffer* pBuffer = new (std::nothrow) ByteBuffer();
80 SysTryReturnVoidResult(NID_IO, pMap != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
81 result r = pBuffer->Construct(size);
82 SysTryCatch(NID_IO, r == E_SUCCESS, delete pBuffer, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
84 r = pBuffer->SetArray((const byte*)pStr, 0, size);
85 SysTryCatch(NID_IO, r == E_SUCCESS, delete pBuffer, E_INVALID_ARG, "[E_INVALID_ARG] The buffer argument is invalid.");
89 pMap->Add(new (std::nothrow) String(pKey), pBuffer);
94 SysLog(NID_APP, "Invalid type for %s : %d", pKey, type);
104 OnMessageReceived(int id, const char* remote_app_id, const char* remote_port, bool trusted_port, bundle* data)
106 SysLog(NID_IO, "Message received");
108 _MessagePortProxy* p = _MessagePortProxy::GetProxy();
111 char* pLocalPort = null;
113 ret = messageport_get_local_port_name(id, &pLocalPort);
114 if (pLocalPort == null)
116 SysSecureLog(NID_IO, "No local port for id: %d", id);
122 //SysLog(NID_IO, "local port name: %s", pLocalPort);
124 _IMessagePortListener* pListener = null;
125 p->__listeners.GetValue(pLocalPort, pListener);
129 if (pListener != null)
131 HashMap* pMap = new (std::nothrow) HashMap(SingleObjectDeleter);
132 SysTryCatch(NID_IO, pMap != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
134 result r = pMap->Construct();
135 SysTryCatch(NID_IO, r == E_SUCCESS, delete pMap, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
137 bundle_foreach(data, ConvertBundleToMap, pMap);
139 if (remote_app_id == null) // Uni-directional
141 pListener->OnMessageReceivedN(pMap);
143 else // Bi-directional
145 SysSecureLog(NID_IO, "Message received from [%s:%s], trusted: %d", remote_app_id, remote_port, trusted_port);
147 pListener->OnMessageReceivedN(remote_app_id, remote_port, trusted_port, pMap);
156 OnTrustedMessageReceived(int id, const char* remote_app_id, const char* remote_port, bool trusted_port, bundle* data)
158 SysLog(NID_IO, "Trusted message received");
160 _MessagePortProxy* p = _MessagePortProxy::GetProxy();
163 char* pLocalPort = null;
164 ret = messageport_get_local_port_name(id, &pLocalPort);
165 if (pLocalPort == null)
167 SysSecureLog(NID_IO, "No local port for id: %d", id);
173 _IMessagePortListener* pListener = null;
174 p->__trustedListeners.GetValue(pLocalPort, pListener);
178 if (pListener != null)
180 HashMap* pMap = new (std::nothrow) HashMap(SingleObjectDeleter);
181 SysTryCatch(NID_IO, pMap != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
183 result r = pMap->Construct();
184 SysTryCatch(NID_IO, r == E_SUCCESS, delete pMap, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
186 bundle_foreach(data, ConvertBundleToMap, pMap);
188 if (remote_app_id == null) // Uni-directional
190 pListener->OnMessageReceivedN(pMap);
192 else // Bi-directional
194 SysSecureLog(NID_IO, "Trusted message received from [%s:%s], trusted: %d", remote_app_id, remote_port, trusted_port);
196 pListener->OnMessageReceivedN(remote_app_id, remote_port, trusted_port, pMap);
204 _MessagePortProxy::_MessagePortProxy(void)
208 _MessagePortProxy::~_MessagePortProxy(void)
213 _MessagePortProxy::Construct(void)
215 static _StringHashProvider hashProvider;
216 static _StringComparer stringComparer;
218 __listeners.Construct(0, 0, hashProvider, stringComparer);
219 __trustedListeners.Construct(0, 0, hashProvider, stringComparer);
220 __ids.Construct(0, 0, hashProvider, stringComparer);
221 __trustedIds.Construct(0, 0, hashProvider, stringComparer);
223 __appId = _AppInfo::GetApplicationId();
230 _MessagePortProxy::RegisterMessagePort(const String& localPort, bool isTrusted,
231 const _IMessagePortListener& listener)
233 SysSecureLog(NID_IO, "Register a message port : [%ls:%ls]", __appId.GetPointer(), localPort.GetPointer());
235 result r = E_SUCCESS;
237 bool contain = false;
239 unique_ptr<char[]> pLocal(_StringConverter::CopyToCharArrayN(localPort));
243 ret = messageport_register_local_port(pLocal.get(), OnMessageReceived);
244 SysTryReturnResult(NID_IO, ret >= 0, E_SYSTEM, "Failed to register the local message port. %d", ret);
246 __listeners.ContainsKey(localPort, contain);
250 __listeners.Add(localPort, const_cast<_IMessagePortListener*>(&listener));
251 __ids.Add(localPort, ret);
255 __listeners.SetValue(localPort, const_cast<_IMessagePortListener*>(&listener));
256 __ids.SetValue(localPort, ret);
262 ret = messageport_register_trusted_local_port(pLocal.get(), OnTrustedMessageReceived);
263 SysTryReturnResult(NID_IO, ret >= 0, E_SYSTEM, "Failed to register the trusted local message port. %d", ret);
265 __trustedListeners.ContainsKey(localPort, contain);
269 __trustedListeners.Add(localPort, const_cast<_IMessagePortListener*>(&listener));
270 __trustedIds.Add(localPort, ret);
274 __trustedListeners.SetValue(localPort, const_cast<_IMessagePortListener*>(&listener));
275 __trustedIds.SetValue(localPort, ret);
284 _MessagePortProxy::RequestRemotePort(const AppId& remoteAppId,
285 const String& remotePort,
288 SysSecureLog(NID_IO, "Request a remote message port [%ls:%ls]", remoteAppId.GetPointer(), remotePort.GetPointer());
290 result r = E_SUCCESS;
294 unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(remoteAppId));
295 unique_ptr<char[]> pPort(_StringConverter::CopyToCharArrayN(remotePort));
299 ret = messageport_check_remote_port(pAppId.get(), pPort.get(), &exist);
303 ret = messageport_check_trusted_remote_port(pAppId.get(), pPort.get(), &exist);
308 r = ConvertToResult(ret);
310 SysTryReturnResult(NID_IO, r != E_CERTIFICATE_VERIFICATION_FAILED, E_CERTIFICATE_VERIFICATION_FAILED, "The target application is not signed with the same certificate.");
312 SysLogException(NID_IO, E_SYSTEM, "[E_SYSTEM] Failed to request the remote message port.");
317 SysTryReturnResult(NID_IO, exist, E_OBJ_NOT_FOUND, "The remote message port is not found.");
323 _MessagePortProxy::SendMessage(const AppId& remoteAppId, const String& remotePort, bool isTrusted, const HashMap* pMap)
325 SysSecureLog(NID_IO, "Send a unidirectional message to remote port [%ls:%ls]", remoteAppId.GetPointer(), remotePort.GetPointer());
330 // Convert Map to bundle
331 bundle* b = ConvertMapToBundleN(pMap, &size);
332 SysTryReturnResult(NID_IO, b != null, E_INVALID_ARG, "The argument is invalid.");
333 //SysTryReturnResult(NID_IO, size <= MAX_MESSAGE_SIZE, E_MAX_EXCEEDED, "The size of the message has exceeded the maximum limit.");
335 unique_ptr<char[]> pRemoteAppId(_StringConverter::CopyToCharArrayN(remoteAppId));
336 unique_ptr<char[]> pRemotePort(_StringConverter::CopyToCharArrayN(remotePort));
340 ret = messageport_send_message(pRemoteAppId.get(), pRemotePort.get(), b);
344 ret = messageport_send_trusted_message(pRemoteAppId.get(), pRemotePort.get(), b);
351 result r = ConvertToResult(ret);
353 SysTryReturnResult(NID_IO, r != E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "The remote message port is not found.");
354 SysTryReturnResult(NID_IO, r != E_CERTIFICATE_VERIFICATION_FAILED, E_CERTIFICATE_VERIFICATION_FAILED, "The target application is not signed with the same certificate.");
355 SysTryReturnResult(NID_IO, r != E_MAX_EXCEEDED, E_MAX_EXCEEDED, "The size of the message has exceeded the maximum limit.");
357 SysLogException(NID_IO, E_SYSTEM, "[E_SYSTEM] Failed to request the remote message port.");
366 _MessagePortProxy::SendMessage(const String& localPort, bool isTrustedLocal, const AppId& remoteAppId, const String& remotePort, bool isTrustedRemote, const HashMap* pMap)
368 SysSecureLog(NID_IO, "Send a bidirectional message from [%ls:%ls] to [%ls:%ls]", __appId.GetPointer(), localPort.GetPointer(), remoteAppId.GetPointer(), remotePort.GetPointer());
370 result r = E_SUCCESS;
375 r = __ids.GetValue(localPort, id);
379 r = __trustedIds.GetValue(localPort, id);
382 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_SYSTEM, "Internal system errors.");
386 // Convert Map to bundle
387 bundle* b = ConvertMapToBundleN(pMap, &size);
388 SysTryReturnResult(NID_IO, b != null, E_INVALID_ARG, "The argument is invalid.");
389 //SysTryReturnResult(NID_IO, size <= MAX_MESSAGE_SIZE, E_MAX_EXCEEDED, "The size of the message has exceeded the maximum limit.");
391 unique_ptr<char[]> pRemoteAppId(_StringConverter::CopyToCharArrayN(remoteAppId));
392 unique_ptr<char[]> pRemotePort(_StringConverter::CopyToCharArrayN(remotePort));
395 if (!isTrustedRemote)
397 ret = messageport_send_bidirectional_message(id, pRemoteAppId.get(), pRemotePort.get(), b);
401 ret = messageport_send_bidirectional_trusted_message(id, pRemoteAppId.get(), pRemotePort.get(), b);
408 result r = ConvertToResult(ret);
410 SysTryReturnResult(NID_IO, r != E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "The remote message port is not found.");
411 SysTryReturnResult(NID_IO, r != E_CERTIFICATE_VERIFICATION_FAILED, E_CERTIFICATE_VERIFICATION_FAILED, "The target application is not signed with the same certificate.");
412 SysTryReturnResult(NID_IO, r != E_MAX_EXCEEDED, E_MAX_EXCEEDED, "The size of the message has exceeded the maximum limit.");
414 SysLogException(NID_IO, E_SYSTEM, "[E_SYSTEM] Failed to request the remote message port.");
423 _MessagePortProxy::GetProxy(void)
425 static _MessagePortProxy* pProxy = null;
429 unique_ptr<_MessagePortProxy> p(new (std::nothrow) _MessagePortProxy);
430 SysTryReturn(NID_IO, p != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
432 result r = p->Construct();
433 SysTryReturn(NID_IO, r == E_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] Failed to initialize a message port proxy.");
435 pProxy = p.release();
438 SetLastResult(E_SUCCESS);
443 _MessagePortProxy::ConvertToResult(int error)
445 SysLog(NID_IO, "Native error = %d", error);
449 case MESSAGEPORT_ERROR_IO_ERROR:
452 case MESSAGEPORT_ERROR_OUT_OF_MEMORY:
453 return E_OUT_OF_MEMORY;
455 case MESSAGEPORT_ERROR_INVALID_PARAMETER:
456 return E_INVALID_ARG;
458 case MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND:
459 return E_OBJ_NOT_FOUND;
461 case MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH:
462 return E_CERTIFICATE_VERIFICATION_FAILED;
464 case MESSAGEPORT_ERROR_MAX_EXCEEDED:
465 return E_MAX_EXCEEDED;
475 _MessagePortProxy::ConvertMapToBundleN(const HashMap* pMap, int* pSize)
477 bundle *b = bundle_create();
480 std::unique_ptr<IMapEnumerator> pEnum (pMap->GetMapEnumeratorN());
481 while(pEnum->MoveNext() == E_SUCCESS)
483 const String* pKey = dynamic_cast<const String*>(pEnum->GetKey());
484 const Object* pValue = pEnum->GetValue();
488 unique_ptr<char[]> pKeyData(_StringConverter::CopyToCharArrayN(*pKey));
490 if (typeid(*pValue) == typeid(const String)) // String
492 const String* pStr = static_cast<const String*>(pValue);
493 size += pStr->GetLength() * sizeof(wchar_t);
495 unique_ptr<char[]> pValueData(_StringConverter::CopyToCharArrayN(*pStr));
497 //SysLog(NID_IO, "key: %s, value: %s", pKeyData.get(), pValueData.get());
499 bundle_add(b, pKeyData.get(), pValueData.get());
503 if (typeid(*pValue) == typeid(const ByteBuffer)) // ByteBuffer
505 const ByteBuffer* pBuffer = static_cast<const ByteBuffer*>(pValue);
506 size += pBuffer->GetLimit();
508 //SysLog(NID_IO, "Add a string key: %s, byte value: %s", pKeyData.get(), pBuffer->GetPointer());
510 bundle_add_byte(b, pKeyData.get(), pBuffer->GetPointer(), pBuffer->GetLimit());
514 SysLog(NID_IO, "The value type is not supported.");
523 SysLog(NID_IO, "The key type is not supported.");