2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FIo_MessagePortManagerImpl.cpp
20 * @brief This is the implementation file for the _MessagePortManagerImpl class.
26 #include <unique_ptr.h>
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"
37 using namespace Tizen::Base;
38 using namespace Tizen::App;
40 namespace Tizen { namespace Io
43 _MessagePortManagerImpl* _MessagePortManagerImpl::__pMessagePortMgrImplInst = null;
45 _MessagePortManagerImpl::_MessagePortManagerImpl(void)
49 _MessagePortManagerImpl::~_MessagePortManagerImpl(void)
54 _MessagePortManagerImpl::Construct(void)
58 static _StringHashProvider hashProvider;
59 static _StringComparer stringComparer;
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);
70 _MessagePortManagerImpl::InitSingleton(void)
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.");
75 result r = pInst->Construct();
76 SysTryReturnVoidResult(NID_IO, !IsFailed(r), r, "[%s] Propagating.", GetErrorMessage(r));
78 __pMessagePortMgrImplInst = pInst.release();
80 std::atexit(DestroySingleton);
85 _MessagePortManagerImpl::DestroySingleton(void)
87 delete __pMessagePortMgrImplInst;
90 _MessagePortManagerImpl*
91 _MessagePortManagerImpl::GetInstance(void)
93 static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
95 if (__pMessagePortMgrImplInst == null)
99 pthread_once(&onceBlock, InitSingleton);
100 result r = GetLastResult();
103 onceBlock = PTHREAD_ONCE_INIT;
104 SysPropagate(NID_IO, r);
108 return __pMessagePortMgrImplInst;
112 _MessagePortManagerImpl::RequestLocalMessagePort(const String& localPort)
114 SysTryReturn(NID_IO, !localPort.IsEmpty(), null, E_INVALID_ARG, "[E_INVALID_ARG] The port name is empty.");
116 LocalMessagePort* pMessagePort = null;
118 __localPorts.GetValue(localPort, pMessagePort);
119 if (pMessagePort == null)
121 pMessagePort = _LocalMessagePortImpl::GetMessagePort(localPort);
122 SysTryReturn(NID_IO, pMessagePort != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
124 __localPorts.Add(localPort, pMessagePort);
127 SetLastResult(E_SUCCESS);
132 _MessagePortManagerImpl::RequestRemoteMessagePort(const AppId& remoteAppId, const String& remotePort)
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.");
137 RemoteMessagePort* pMessagePort = null;
138 String remoteKey(remoteAppId + remotePort);
140 __remotePorts.GetValue(remoteKey, pMessagePort);
141 if (pMessagePort == null)
143 pMessagePort = _RemoteMessagePortImpl::GetMessagePort(remoteAppId, remotePort);
144 SysTryReturn(NID_IO, pMessagePort != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
146 __remotePorts.Add(remoteKey, pMessagePort);
149 SetLastResult(E_SUCCESS);
154 _MessagePortManagerImpl::RequestTrustedLocalMessagePort(const String& localPort)
156 SysTryReturn(NID_IO, !localPort.IsEmpty(), null, E_INVALID_ARG, "[E_INVALID_ARG] The port name is empty.");
158 LocalMessagePort* pMessagePort = null;
160 __trustLocalPorts.GetValue(localPort, pMessagePort);
161 if (pMessagePort == null)
163 pMessagePort = _LocalMessagePortImpl::GetMessagePort(localPort, true);
164 SysTryReturn(NID_IO, pMessagePort != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
166 __trustLocalPorts.Add(localPort, pMessagePort);
169 SetLastResult(E_SUCCESS);
174 _MessagePortManagerImpl::RequestTrustedRemoteMessagePort(const AppId& remoteAppId, const String& remotePort)
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.");
179 RemoteMessagePort* pMessagePort = null;
180 String remoteKey(remoteAppId + remotePort);
182 __trustRemotePorts.GetValue(remoteKey, pMessagePort);
183 if (pMessagePort == null)
185 pMessagePort = _RemoteMessagePortImpl::GetMessagePort(remoteAppId, remotePort, true);
186 SysTryReturn(NID_IO, pMessagePort != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
188 __trustRemotePorts.Add(remoteKey, pMessagePort);
191 SetLastResult(E_SUCCESS);
196 _MessagePortManagerImpl::GetRemoteMessagePort(const AppId& remoteAppId, const String& remotePort, bool isTrusted)
198 RemoteMessagePort* pMessagePort = null;
199 String remoteKey(remoteAppId + remotePort);
203 __remotePorts.GetValue(remoteKey, pMessagePort);
207 __trustRemotePorts.GetValue(remoteKey, pMessagePort);
210 if (pMessagePort == null)
212 pMessagePort = _RemoteMessagePortImpl::GetMessagePortOnly(remoteAppId, remotePort, isTrusted);
213 SysTryReturn(NID_IO, pMessagePort != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
217 __remotePorts.Add(remoteKey, pMessagePort);
221 __trustRemotePorts.Add(remoteKey, pMessagePort);
225 SetLastResult(E_SUCCESS);