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