2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FIo_MessagePortManagerImpl.cpp
19 * @brief This is the implementation file for the _MessagePortManagerImpl class.
25 #include <unique_ptr.h>
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"
36 using namespace Tizen::Base;
37 using namespace Tizen::App;
39 namespace Tizen { namespace Io
42 _MessagePortManagerImpl* _MessagePortManagerImpl::__pMessagePortMgrImplInst = null;
44 _MessagePortManagerImpl::_MessagePortManagerImpl(void)
48 _MessagePortManagerImpl::~_MessagePortManagerImpl(void)
53 _MessagePortManagerImpl::Construct(void)
57 static _StringHashProvider hashProvider;
58 static _StringComparer stringComparer;
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);
69 _MessagePortManagerImpl::InitSingleton(void)
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.");
74 result r = pInst->Construct();
75 SysTryReturnVoidResult(NID_IO, !IsFailed(r), r, "[%s] Propagating.", GetErrorMessage(r));
77 __pMessagePortMgrImplInst = pInst.release();
79 std::atexit(DestroySingleton);
84 _MessagePortManagerImpl::DestroySingleton(void)
86 delete __pMessagePortMgrImplInst;
89 _MessagePortManagerImpl*
90 _MessagePortManagerImpl::GetInstance(void)
92 static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
94 if (__pMessagePortMgrImplInst == null)
98 pthread_once(&onceBlock, InitSingleton);
99 result r = GetLastResult();
102 onceBlock = PTHREAD_ONCE_INIT;
103 SysPropagate(NID_IO, r);
107 return __pMessagePortMgrImplInst;
111 _MessagePortManagerImpl::RequestLocalMessagePort(const String& localPort)
113 SysTryReturn(NID_IO, !localPort.IsEmpty(), null, E_INVALID_ARG, "[E_INVALID_ARG] The port name is empty.");
115 LocalMessagePort* pMessagePort = null;
117 __localPorts.GetValue(localPort, pMessagePort);
118 if (pMessagePort == null)
120 pMessagePort = _LocalMessagePortImpl::GetMessagePort(localPort);
121 SysTryReturn(NID_IO, pMessagePort != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
123 __localPorts.Add(localPort, pMessagePort);
126 SetLastResult(E_SUCCESS);
131 _MessagePortManagerImpl::RequestRemoteMessagePort(const AppId& remoteAppId, const String& remotePort)
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.");
136 RemoteMessagePort* pMessagePort = null;
137 String remoteKey(remoteAppId + remotePort);
139 __remotePorts.GetValue(remoteKey, pMessagePort);
140 if (pMessagePort == null)
142 pMessagePort = _RemoteMessagePortImpl::GetMessagePort(remoteAppId, remotePort);
143 SysTryReturn(NID_IO, pMessagePort != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
145 __remotePorts.Add(remoteKey, pMessagePort);
148 SetLastResult(E_SUCCESS);
153 _MessagePortManagerImpl::RequestTrustedLocalMessagePort(const String& localPort)
155 SysTryReturn(NID_IO, !localPort.IsEmpty(), null, E_INVALID_ARG, "[E_INVALID_ARG] The port name is empty.");
157 LocalMessagePort* pMessagePort = null;
159 __trustLocalPorts.GetValue(localPort, pMessagePort);
160 if (pMessagePort == null)
162 pMessagePort = _LocalMessagePortImpl::GetMessagePort(localPort, true);
163 SysTryReturn(NID_IO, pMessagePort != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
165 __trustLocalPorts.Add(localPort, pMessagePort);
168 SetLastResult(E_SUCCESS);
173 _MessagePortManagerImpl::RequestTrustedRemoteMessagePort(const AppId& remoteAppId, const String& remotePort)
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.");
178 RemoteMessagePort* pMessagePort = null;
179 String remoteKey(remoteAppId + remotePort);
181 __trustRemotePorts.GetValue(remoteKey, pMessagePort);
182 if (pMessagePort == null)
184 pMessagePort = _RemoteMessagePortImpl::GetMessagePort(remoteAppId, remotePort, true);
185 SysTryReturn(NID_IO, pMessagePort != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
187 __trustRemotePorts.Add(remoteKey, pMessagePort);
190 SetLastResult(E_SUCCESS);
195 _MessagePortManagerImpl::GetRemoteMessagePort(const AppId& remoteAppId, const String& remotePort, bool isTrusted)
197 RemoteMessagePort* pMessagePort = null;
198 String remoteKey(remoteAppId + remotePort);
202 __remotePorts.GetValue(remoteKey, pMessagePort);
206 __trustRemotePorts.GetValue(remoteKey, pMessagePort);
209 if (pMessagePort == null)
211 pMessagePort = _RemoteMessagePortImpl::GetMessagePortOnly(remoteAppId, remotePort, isTrusted);
212 SysTryReturn(NID_IO, pMessagePort != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
216 __remotePorts.Add(remoteKey, pMessagePort);
220 __trustRemotePorts.Add(remoteKey, pMessagePort);
224 SetLastResult(E_SUCCESS);