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_LocalMessagePortImpl.cpp
20 * @brief This is the implementation file for the _LocalMessagePortImpl class.
24 #include <unique_ptr.h>
26 #include <FBaseSysLog.h>
27 #include <FBaseColHashMap.h>
28 #include <FBaseRtIEventArg.h>
29 #include <FIoLocalMessagePort.h>
30 #include <FIoIMessagePortListener.h>
32 #include "FIo_LocalMessagePortImpl.h"
33 #include "FIo_RemoteMessagePortImpl.h"
34 #include "FIo_MessagePortManagerImpl.h"
35 #include "FIo_MessagePortProxy.h"
39 using namespace Tizen::Base;
40 using namespace Tizen::Base::Runtime;
41 using namespace Tizen::Base::Collection;
42 using namespace Tizen::App;
44 namespace Tizen { namespace Io
47 class _MessagePortEventArg
51 _MessagePortEventArg(IMap* pMap)
56 _MessagePortEventArg(RemoteMessagePort* pRemotePort, IMap* pMap)
57 : __pRemotePort(pRemotePort)
61 RemoteMessagePort* __pRemotePort;
65 class _MessagePortEvent
69 virtual void FireImpl(IEventListener& listener, const IEventArg& arg)
71 IMessagePortListener* pListener = dynamic_cast<IMessagePortListener*> (&listener);
72 if (pListener != null)
74 const _MessagePortEventArg* pArg = dynamic_cast<const _MessagePortEventArg*>(&arg);
77 if (pArg->__pRemotePort == null)
79 pListener->OnMessageReceivedN(pArg->__pMap);
83 pListener->OnMessageReceivedN(pArg->__pRemotePort, pArg->__pMap);
90 _LocalMessagePortImpl::_LocalMessagePortImpl(void)
97 _LocalMessagePortImpl::~_LocalMessagePortImpl(void)
103 _LocalMessagePortImpl::Construct(const Tizen::Base::String& port, bool isTrusted)
105 result r = _MessagePortProxy::GetProxy()->RegisterMessagePort(port, isTrusted, *this);
106 SysTryReturnResult(NID_IO, r == E_SUCCESS, r, "Failed to initialize a message port.");
108 _MessagePortEvent* pEvent = new (std::nothrow) _MessagePortEvent();
109 SysTryReturnResult(NID_IO, pEvent != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
113 __isTrusted = isTrusted;
119 _LocalMessagePortImpl::AddMessagePortListener(IMessagePortListener& listener)
121 return __pEvent->AddListener(listener);
125 _LocalMessagePortImpl::RemoveMessagePortListener(IMessagePortListener& listener)
127 return __pEvent->RemoveListener(listener);
131 _LocalMessagePortImpl::GetName(void) const
137 _LocalMessagePortImpl::IsTrusted(void) const
143 _LocalMessagePortImpl::OnMessageReceivedN(IMap* pMap)
145 SysLog(NID_IO, "A message is received.");
147 _MessagePortEventArg* pEventArg= new (std::nothrow) _MessagePortEventArg(pMap);
148 if(pEventArg != null)
150 __pEvent->Fire(*pEventArg);
155 _LocalMessagePortImpl::OnMessageReceivedN(const AppId& remoteAppId, const String& remotePort, bool isTrusted, IMap* pMap)
157 SysLog(NID_IO, "A message is received from remote port [%ls:%ls].", remoteAppId.GetPointer(), remotePort.GetPointer());
159 _MessagePortManagerImpl* pManager = _MessagePortManagerImpl::GetInstance();
160 SysTryReturnVoidResult(NID_IO, pManager != null, GetLastResult(), "Propagating.");
162 RemoteMessagePort* pRemotePort = pManager->GetRemoteMessagePort(remoteAppId, remotePort, isTrusted);
164 _MessagePortEventArg* pEventArg = new (std::nothrow) _MessagePortEventArg(pRemotePort, pMap);
165 if(pEventArg != null)
167 __pEvent->Fire(*pEventArg);
172 _LocalMessagePortImpl::GetMessagePort(const String& port, bool isTrusted)
174 result r = E_SUCCESS;
176 LocalMessagePort* pLocalMessagePort = new (std::nothrow) LocalMessagePort;
177 SysTryReturn(NID_IO, pLocalMessagePort != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
179 unique_ptr<_LocalMessagePortImpl> pImpl(new (std::nothrow) _LocalMessagePortImpl);
180 SysTryCatch(NID_IO, pImpl != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
182 r = pImpl->Construct(port, isTrusted);
183 SysTryCatch(NID_IO, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
185 pLocalMessagePort->__pLocalMessagePortImpl = pImpl.release();
188 return pLocalMessagePort;
191 delete pLocalMessagePort;