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