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_LocalMessagePortImpl.cpp
19 * @brief This is the implementation file for the _LocalMessagePortImpl class.
23 #include <unique_ptr.h>
25 #include <FBaseSysLog.h>
26 #include <FBaseColHashMap.h>
27 #include <FBaseRtIEventArg.h>
28 #include <FIoLocalMessagePort.h>
29 #include <FIoIMessagePortListener.h>
31 #include "FIo_LocalMessagePortImpl.h"
32 #include "FIo_RemoteMessagePortImpl.h"
33 #include "FIo_MessagePortManagerImpl.h"
34 #include "FIo_MessagePortProxy.h"
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Runtime;
40 using namespace Tizen::Base::Collection;
41 using namespace Tizen::App;
43 namespace Tizen { namespace Io
46 class _MessagePortEventArg
50 _MessagePortEventArg(IMap* pMap)
55 _MessagePortEventArg(RemoteMessagePort* pRemotePort, IMap* pMap)
56 : __pRemotePort(pRemotePort)
60 RemoteMessagePort* __pRemotePort;
64 class _MessagePortEvent
68 virtual void FireImpl(IEventListener& listener, const IEventArg& arg)
70 IMessagePortListener* pListener = dynamic_cast<IMessagePortListener*> (&listener);
71 if (pListener != null)
73 const _MessagePortEventArg* pArg = dynamic_cast<const _MessagePortEventArg*>(&arg);
76 if (pArg->__pRemotePort == null)
78 pListener->OnMessageReceivedN(pArg->__pMap);
82 pListener->OnMessageReceivedN(pArg->__pRemotePort, pArg->__pMap);
89 _LocalMessagePortImpl::_LocalMessagePortImpl(void)
96 _LocalMessagePortImpl::~_LocalMessagePortImpl(void)
102 _LocalMessagePortImpl::Construct(const Tizen::Base::String& port, bool isTrusted)
104 result r = _MessagePortProxy::GetProxy()->RegisterMessagePort(port, isTrusted, *this);
105 SysTryReturnResult(NID_IO, r == E_SUCCESS, r, "Failed to initialize a message port.");
107 _MessagePortEvent* pEvent = new (std::nothrow) _MessagePortEvent();
108 SysTryReturnResult(NID_IO, pEvent != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
112 __isTrusted = isTrusted;
118 _LocalMessagePortImpl::AddMessagePortListener(IMessagePortListener& listener)
120 return __pEvent->AddListener(listener);
124 _LocalMessagePortImpl::RemoveMessagePortListener(IMessagePortListener& listener)
126 return __pEvent->RemoveListener(listener);
130 _LocalMessagePortImpl::GetName(void) const
136 _LocalMessagePortImpl::IsTrusted(void) const
142 _LocalMessagePortImpl::OnMessageReceivedN(IMap* pMap)
144 SysLog(NID_IO, "A message is received.");
146 _MessagePortEventArg* pEventArg= new (std::nothrow) _MessagePortEventArg(pMap);
147 if(pEventArg != null)
149 __pEvent->Fire(*pEventArg);
154 _LocalMessagePortImpl::OnMessageReceivedN(const AppId& remoteAppId, const String& remotePort, bool isTrusted, IMap* pMap)
156 SysLog(NID_IO, "A message is received from remote port [%ls:%ls].", remoteAppId.GetPointer(), remotePort.GetPointer());
158 _MessagePortManagerImpl* pManager = _MessagePortManagerImpl::GetInstance();
159 SysTryReturnVoidResult(NID_IO, pManager != null, GetLastResult(), "Propagating.");
161 RemoteMessagePort* pRemotePort = pManager->GetRemoteMessagePort(remoteAppId, remotePort, isTrusted);
163 _MessagePortEventArg* pEventArg = new (std::nothrow) _MessagePortEventArg(pRemotePort, pMap);
164 if(pEventArg != null)
166 __pEvent->Fire(*pEventArg);
171 _LocalMessagePortImpl::GetMessagePort(const String& port, bool isTrusted)
173 result r = E_SUCCESS;
175 LocalMessagePort* pLocalMessagePort = new (std::nothrow) LocalMessagePort;
176 SysTryReturn(NID_IO, pLocalMessagePort != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
178 unique_ptr<_LocalMessagePortImpl> pImpl(new (std::nothrow) _LocalMessagePortImpl);
179 SysTryCatch(NID_IO, pImpl != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
181 r = pImpl->Construct(port, isTrusted);
182 SysTryCatch(NID_IO, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
184 pLocalMessagePort->__pLocalMessagePortImpl = pImpl.release();
187 return pLocalMessagePort;
190 delete pLocalMessagePort;