Revert "fixed PLM issue. Assert occured when an application was closed using back...
[platform/framework/native/appfw.git] / src / io / FIo_MessagePortManagerImpl.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_MessagePortManagerImpl.cpp
19  * @brief       This is the implementation file for the _MessagePortManagerImpl class.
20  *
21  */
22
23 #include <cstdlib>
24 #include <pthread.h>
25 #include <unique_ptr.h>
26
27 #include <FBaseSysLog.h>
28 #include <FIoLocalMessagePort.h>
29 #include <FIoRemoteMessagePort.h>
30 #include "FIo_MessagePortManagerImpl.h"
31 #include "FIo_LocalMessagePortImpl.h"
32 #include "FIo_RemoteMessagePortImpl.h"
33
34 using namespace std;
35
36 using namespace Tizen::Base;
37 using namespace Tizen::App;
38
39 namespace Tizen { namespace Io
40 {
41
42 _MessagePortManagerImpl* _MessagePortManagerImpl::__pMessagePortMgrImplInst = null;
43
44 _MessagePortManagerImpl::_MessagePortManagerImpl(void)
45 {
46 }
47
48 _MessagePortManagerImpl::~_MessagePortManagerImpl(void)
49 {
50 }
51
52 result
53 _MessagePortManagerImpl::Construct(void)
54 {
55         result r = E_SUCCESS;
56
57         static _StringHashProvider hashProvider;
58         static _StringComparer stringComparer;
59
60         r = __localPorts.Construct(0, 0, hashProvider, stringComparer);
61         r = __remotePorts.Construct(0, 0, hashProvider, stringComparer);
62         r = __trustLocalPorts.Construct(0, 0, hashProvider, stringComparer);
63         r = __trustRemotePorts.Construct(0, 0, hashProvider, stringComparer);
64
65         return E_SUCCESS;
66 }
67
68 void
69 _MessagePortManagerImpl::InitSingleton(void)
70 {
71         unique_ptr<_MessagePortManagerImpl> pInst(new (std::nothrow) _MessagePortManagerImpl);
72         SysTryReturnVoidResult(NID_IO, pInst != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
73
74         result r = pInst->Construct();
75         SysTryReturnVoidResult(NID_IO, !IsFailed(r), r, "[%s] Propagating.", GetErrorMessage(r));
76
77         __pMessagePortMgrImplInst = pInst.release();
78
79         std::atexit(DestroySingleton);
80         return;
81 }
82
83 void
84 _MessagePortManagerImpl::DestroySingleton(void)
85 {
86         delete __pMessagePortMgrImplInst;
87 }
88
89 _MessagePortManagerImpl*
90 _MessagePortManagerImpl::GetInstance(void)
91 {
92         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
93
94         if (__pMessagePortMgrImplInst == null)
95         {
96                 ClearLastResult();
97
98                 pthread_once(&onceBlock, InitSingleton);
99                 result r = GetLastResult();
100                 if (IsFailed(r))
101                 {
102                         onceBlock = PTHREAD_ONCE_INIT;
103                         SysPropagate(NID_IO, r);
104                 }
105         }
106
107         return __pMessagePortMgrImplInst;
108 }
109
110 LocalMessagePort*
111 _MessagePortManagerImpl::RequestLocalMessagePort(const String& localPort)
112 {
113         SysTryReturn(NID_IO, !localPort.IsEmpty(), null, E_INVALID_ARG, "[E_INVALID_ARG] The port name is empty.");
114
115         LocalMessagePort* pMessagePort = null;
116
117         __localPorts.GetValue(localPort, pMessagePort);
118         if (pMessagePort == null)
119         {
120                 pMessagePort = _LocalMessagePortImpl::GetMessagePort(localPort);
121                 SysTryReturn(NID_IO, pMessagePort != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
122
123                 __localPorts.Add(localPort, pMessagePort);
124         }
125
126         SetLastResult(E_SUCCESS);
127         return pMessagePort;
128 }
129
130 RemoteMessagePort*
131 _MessagePortManagerImpl::RequestRemoteMessagePort(const AppId& remoteAppId, const String& remotePort)
132 {
133         SysTryReturn(NID_IO, !remoteAppId.IsEmpty(), null, E_INVALID_ARG, "[E_INVALID_ARG] The application id is empty.");
134         SysTryReturn(NID_IO, !remotePort.IsEmpty(), null, E_INVALID_ARG, "[E_INVALID_ARG] The port name is empty.");
135
136         RemoteMessagePort* pMessagePort = null;
137         String remoteKey(remoteAppId + remotePort);
138
139         __remotePorts.GetValue(remoteKey, pMessagePort);
140         if (pMessagePort == null)
141         {
142                 pMessagePort = _RemoteMessagePortImpl::GetMessagePort(remoteAppId, remotePort);
143                 SysTryReturn(NID_IO, pMessagePort != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
144
145                 __remotePorts.Add(remoteKey, pMessagePort);
146         }
147
148         SetLastResult(E_SUCCESS);
149         return pMessagePort;
150 }
151
152 LocalMessagePort*
153 _MessagePortManagerImpl::RequestTrustedLocalMessagePort(const String& localPort)
154 {
155         SysTryReturn(NID_IO, !localPort.IsEmpty(), null, E_INVALID_ARG, "[E_INVALID_ARG] The port name is empty.");
156
157         LocalMessagePort* pMessagePort = null;
158
159         __trustLocalPorts.GetValue(localPort, pMessagePort);
160         if (pMessagePort == null)
161         {
162                 pMessagePort = _LocalMessagePortImpl::GetMessagePort(localPort, true);
163                 SysTryReturn(NID_IO, pMessagePort != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
164
165                 __trustLocalPorts.Add(localPort, pMessagePort);
166         }
167
168         SetLastResult(E_SUCCESS);
169         return pMessagePort;
170 }
171
172 RemoteMessagePort*
173 _MessagePortManagerImpl::RequestTrustedRemoteMessagePort(const AppId& remoteAppId, const String& remotePort)
174 {
175         SysTryReturn(NID_IO, !remoteAppId.IsEmpty(), null, E_INVALID_ARG, "[E_INVALID_ARG] The application id is empty.");
176         SysTryReturn(NID_IO, !remotePort.IsEmpty(), null, E_INVALID_ARG, "[E_INVALID_ARG] The port name is empty.");
177
178         RemoteMessagePort* pMessagePort = null;
179         String remoteKey(remoteAppId + remotePort);
180
181         __trustRemotePorts.GetValue(remoteKey, pMessagePort);
182         if (pMessagePort == null)
183         {
184                 pMessagePort = _RemoteMessagePortImpl::GetMessagePort(remoteAppId, remotePort, true);
185                 SysTryReturn(NID_IO, pMessagePort != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
186
187                 __trustRemotePorts.Add(remoteKey, pMessagePort);
188         }
189
190         SetLastResult(E_SUCCESS);
191         return pMessagePort;
192 }
193
194 RemoteMessagePort*
195  _MessagePortManagerImpl::GetRemoteMessagePort(const AppId& remoteAppId, const String& remotePort, bool isTrusted)
196 {
197         RemoteMessagePort* pMessagePort = null;
198         String remoteKey(remoteAppId + remotePort);
199
200         if (!isTrusted)
201         {
202                 __remotePorts.GetValue(remoteKey, pMessagePort);
203         }
204         else
205         {
206                 __trustRemotePorts.GetValue(remoteKey, pMessagePort);
207         }
208
209         if (pMessagePort == null)
210         {
211                 pMessagePort = _RemoteMessagePortImpl::GetMessagePortOnly(remoteAppId, remotePort, isTrusted);
212                 SysTryReturn(NID_IO, pMessagePort != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
213
214                 if (!isTrusted)
215                 {
216                         __remotePorts.Add(remoteKey, pMessagePort);
217                 }
218                 else
219                 {
220                         __trustRemotePorts.Add(remoteKey, pMessagePort);
221                 }
222         }
223
224         SetLastResult(E_SUCCESS);
225         return pMessagePort;
226 }
227
228 } } // Tizen::Io