sync with tizen_2.0
[platform/framework/native/appfw.git] / src / io / FIo_LocalMessagePortImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 /**
19  * @file        FIo_LocalMessagePortImpl.cpp
20  * @brief       This is the implementation file for the _LocalMessagePortImpl class.
21  *
22  */
23
24 #include <unique_ptr.h>
25
26 #include <FBaseSysLog.h>
27 #include <FBaseColHashMap.h>
28 #include <FBaseRtIEventArg.h>
29 #include <FIoLocalMessagePort.h>
30 #include <FIoIMessagePortListener.h>
31
32 #include "FIo_LocalMessagePortImpl.h"
33 #include "FIo_RemoteMessagePortImpl.h"
34 #include "FIo_MessagePortManagerImpl.h"
35 #include "FIo_MessagePortProxy.h"
36
37 using namespace std;
38
39 using namespace Tizen::Base;
40 using namespace Tizen::Base::Runtime;
41 using namespace Tizen::Base::Collection;
42 using namespace Tizen::App;
43
44 namespace Tizen { namespace Io
45 {
46
47 class _MessagePortEventArg
48         : public IEventArg
49 {
50 public:
51         _MessagePortEventArg()
52                 : __pRemotePort(null)
53                 , __pMap(null)
54         {
55         }
56         RemoteMessagePort* __pRemotePort;
57         IMap* __pMap;
58 };
59
60 class _MessagePortEvent
61         : public Event
62 {
63 protected:
64         virtual void FireImpl(IEventListener& listener, const IEventArg& arg)
65         {
66                 IMessagePortListener* pListener = dynamic_cast<IMessagePortListener*> (&listener);
67                 if (pListener != null)
68                 {
69                         const _MessagePortEventArg* pArg = dynamic_cast<const _MessagePortEventArg*>(&arg);
70                         if (pArg != null)
71                         {
72                                 if (pArg->__pRemotePort == null)
73                                 {
74                                         pListener->OnMessageReceivedN(pArg->__pMap);
75                                 }
76                                 else
77                                 {
78                                         pListener->OnMessageReceivedN(pArg->__pRemotePort, pArg->__pMap);
79                                 }
80                         }
81                 }
82         }
83 };
84
85 _LocalMessagePortImpl::_LocalMessagePortImpl(void)
86         : __isTrusted(false)
87         , __pEvent(null)
88 {
89
90 }
91
92 _LocalMessagePortImpl::~_LocalMessagePortImpl(void)
93 {
94         delete __pEvent;
95 }
96
97 result
98 _LocalMessagePortImpl::Construct(const Tizen::Base::String& port, bool isTrusted)
99 {
100         result r = _MessagePortProxy::GetProxy()->RegisterMessagePort(port, isTrusted, *this);
101         SysTryReturnResult(NID_IO, r == E_SUCCESS, r, "Failed to initialize a message port.");
102
103         _MessagePortEvent* pEvent = new (std::nothrow) _MessagePortEvent();
104         SysTryReturnResult(NID_IO, pEvent != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
105
106         __pEvent = pEvent;
107         __port = port;
108         __isTrusted = isTrusted;
109
110         return E_SUCCESS;
111 }
112
113 result
114 _LocalMessagePortImpl::AddMessagePortListener(IMessagePortListener& listener)
115 {
116         return __pEvent->AddListener(listener);
117 }
118
119 result
120 _LocalMessagePortImpl::RemoveMessagePortListener(IMessagePortListener& listener)
121 {
122         return __pEvent->RemoveListener(listener);
123 }
124
125 String
126 _LocalMessagePortImpl::GetName(void) const
127 {
128         return __port;
129 }
130
131 bool
132 _LocalMessagePortImpl::IsTrusted(void) const
133 {
134         return __isTrusted;
135 }
136
137 void
138 _LocalMessagePortImpl::OnMessageReceivedN(IMap* pMap)
139 {
140         _MessagePortEventArg* pEventArg= new (std::nothrow) _MessagePortEventArg();
141         if(pEventArg != null)
142         {
143                 pEventArg->__pMap = pMap;
144                 __pEvent->Fire(*pEventArg);
145         }
146 }
147
148 void
149 _LocalMessagePortImpl::OnMessageReceivedN(const AppId& remoteAppId, const String& remotePort, bool isTrusted, IMap* pMap)
150 {
151         SysLog(NID_IO, "A message is received from remote port [%ls:%ls].", remoteAppId.GetPointer(), remotePort.GetPointer());
152
153         _MessagePortManagerImpl* pManager = _MessagePortManagerImpl::GetInstance();
154         SysTryReturnVoidResult(NID_IO, pManager != null, GetLastResult(), "Propagating.");
155
156         RemoteMessagePort* pRemotePort = pManager->GetRemoteMessagePort(remoteAppId, remotePort, isTrusted);
157
158         _MessagePortEventArg* pEventArg = new (std::nothrow) _MessagePortEventArg();
159         if(pEventArg != null)
160         {
161                 pEventArg->__pRemotePort = pRemotePort;
162                 pEventArg->__pMap = pMap;
163                 __pEvent->Fire(*pEventArg);
164         }
165 }
166
167 LocalMessagePort*
168 _LocalMessagePortImpl::GetMessagePort(const String& port, bool isTrusted)
169 {
170         result r = E_SUCCESS;
171
172         LocalMessagePort* pLocalMessagePort = new (std::nothrow) LocalMessagePort;
173         SysTryReturn(NID_IO, pLocalMessagePort != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
174
175         unique_ptr<_LocalMessagePortImpl> pImpl(new (std::nothrow) _LocalMessagePortImpl);
176         SysTryCatch(NID_IO, pImpl != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
177
178         r = pImpl->Construct(port, isTrusted);
179         SysTryCatch(NID_IO, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] Failed to initialize a message port.");
180
181         pLocalMessagePort->__pLocalMessagePortImpl = pImpl.release();
182
183         SetLastResult(r);
184         return pLocalMessagePort;
185
186 CATCH:
187         delete pLocalMessagePort;
188
189         return null;
190 }
191
192 } } // Tizen::Io