sync with master
[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(IMap* pMap)
52                 : __pRemotePort(null)
53                 , __pMap(pMap)
54         {
55         }
56         _MessagePortEventArg(RemoteMessagePort* pRemotePort, IMap* pMap)
57                 : __pRemotePort(pRemotePort)
58                 , __pMap(pMap)
59         {
60         }
61         RemoteMessagePort* __pRemotePort;
62         IMap* __pMap;
63 };
64
65 class _MessagePortEvent
66         : public Event
67 {
68 protected:
69         virtual void FireImpl(IEventListener& listener, const IEventArg& arg)
70         {
71                 IMessagePortListener* pListener = dynamic_cast<IMessagePortListener*> (&listener);
72                 if (pListener != null)
73                 {
74                         const _MessagePortEventArg* pArg = dynamic_cast<const _MessagePortEventArg*>(&arg);
75                         if (pArg != null)
76                         {
77                                 if (pArg->__pRemotePort == null)
78                                 {
79                                         pListener->OnMessageReceivedN(pArg->__pMap);
80                                 }
81                                 else
82                                 {
83                                         pListener->OnMessageReceivedN(pArg->__pRemotePort, pArg->__pMap);
84                                 }
85                         }
86                 }
87         }
88 };
89
90 _LocalMessagePortImpl::_LocalMessagePortImpl(void)
91         : __isTrusted(false)
92         , __pEvent(null)
93 {
94
95 }
96
97 _LocalMessagePortImpl::~_LocalMessagePortImpl(void)
98 {
99         delete __pEvent;
100 }
101
102 result
103 _LocalMessagePortImpl::Construct(const Tizen::Base::String& port, bool isTrusted)
104 {
105         result r = _MessagePortProxy::GetProxy()->RegisterMessagePort(port, isTrusted, *this);
106         SysTryReturnResult(NID_IO, r == E_SUCCESS, r, "Failed to initialize a message port.");
107
108         _MessagePortEvent* pEvent = new (std::nothrow) _MessagePortEvent();
109         SysTryReturnResult(NID_IO, pEvent != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
110
111         __pEvent = pEvent;
112         __port = port;
113         __isTrusted = isTrusted;
114
115         return E_SUCCESS;
116 }
117
118 result
119 _LocalMessagePortImpl::AddMessagePortListener(IMessagePortListener& listener)
120 {
121         return __pEvent->AddListener(listener);
122 }
123
124 result
125 _LocalMessagePortImpl::RemoveMessagePortListener(IMessagePortListener& listener)
126 {
127         return __pEvent->RemoveListener(listener);
128 }
129
130 String
131 _LocalMessagePortImpl::GetName(void) const
132 {
133         return __port;
134 }
135
136 bool
137 _LocalMessagePortImpl::IsTrusted(void) const
138 {
139         return __isTrusted;
140 }
141
142 void
143 _LocalMessagePortImpl::OnMessageReceivedN(IMap* pMap)
144 {
145         SysLog(NID_IO, "A message is received.");
146
147         _MessagePortEventArg* pEventArg= new (std::nothrow) _MessagePortEventArg(pMap);
148         if(pEventArg != null)
149         {
150                 __pEvent->Fire(*pEventArg);
151         }
152 }
153
154 void
155 _LocalMessagePortImpl::OnMessageReceivedN(const AppId& remoteAppId, const String& remotePort, bool isTrusted, IMap* pMap)
156 {
157         SysLog(NID_IO, "A message is received from remote port [%ls:%ls].", remoteAppId.GetPointer(), remotePort.GetPointer());
158
159         _MessagePortManagerImpl* pManager = _MessagePortManagerImpl::GetInstance();
160         SysTryReturnVoidResult(NID_IO, pManager != null, GetLastResult(), "Propagating.");
161
162         RemoteMessagePort* pRemotePort = pManager->GetRemoteMessagePort(remoteAppId, remotePort, isTrusted);
163
164         _MessagePortEventArg* pEventArg = new (std::nothrow) _MessagePortEventArg(pRemotePort, pMap);
165         if(pEventArg != null)
166         {
167                 __pEvent->Fire(*pEventArg);
168         }
169 }
170
171 LocalMessagePort*
172 _LocalMessagePortImpl::GetMessagePort(const String& port, bool isTrusted)
173 {
174         result r = E_SUCCESS;
175
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.");
178
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.");
181
182         r = pImpl->Construct(port, isTrusted);
183         SysTryCatch(NID_IO, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
184
185         pLocalMessagePort->__pLocalMessagePortImpl = pImpl.release();
186
187         SetLastResult(r);
188         return pLocalMessagePort;
189
190 CATCH:
191         delete pLocalMessagePort;
192
193         return null;
194 }
195
196 } } // Tizen::Io