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_MessagePortProxy.cpp
20 * @brief This is the implementation file for the _MessagePortProxy class.
25 #include <unique_ptr.h>
27 #include <package_manager.h>
29 #include <FBaseSysLog.h>
30 #include <FBase_StringConverter.h>
31 #include <FApp_AppInfo.h>
32 #include "FIo_IpcClient.h"
33 #include "FIo_MessagePortProxy.h"
34 #include "FIo_MessagePortMessages.h"
38 using namespace Tizen::App;
39 using namespace Tizen::Io;
40 using namespace Tizen::Base;
41 using namespace Tizen::Base::Collection;
42 using namespace Tizen::Base::Runtime;
44 namespace Tizen { namespace Io
47 _MessagePortProxy::_MessagePortProxy(void)
52 _MessagePortProxy::~_MessagePortProxy(void)
57 _MessagePortProxy::Construct(void)
59 SysAssertf(__pIpcClient == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
63 static _StringHashProvider hashProvider;
64 static _StringComparer stringComparer;
66 __listeners.Construct(0, 0, hashProvider, stringComparer);
67 __trustedListeners.Construct(0, 0, hashProvider, stringComparer);
69 unique_ptr<_IpcClient> pIpcClient(new (std::nothrow) _IpcClient);
70 SysTryReturnResult(NID_IO, pIpcClient != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
72 r = pIpcClient->Construct("osp.io.ipcserver.messageportmanager", this);
73 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] Failed to connect to IPC server.", GetErrorMessage(r));
75 __pIpcClient = pIpcClient.release();
77 const String& appId = _AppInfo::GetAppId();
78 const String& appExecutableName = _AppInfo::GetAppExecutableName();
80 __appId.Append(appId);
82 __appId.Append(appExecutableName);
89 _MessagePortProxy::OnIpcResponseReceived(_IpcClient& client, const IPC::Message& message)
91 IPC_BEGIN_MESSAGE_MAP(_MessagePortProxy, message)
92 IPC_MESSAGE_HANDLER_EX(MessagePortService_sendMessageAsync, &client, OnSendMessage)
93 IPC_MESSAGE_HANDLER_EX(MessagePortService_sendBidirMessageAsync, &client, OnSendBidirMessage)
94 IPC_END_MESSAGE_MAP_EX()
98 _MessagePortProxy::RegisterMessagePort(const String& localPort, bool isTrusted,
99 const _IMessagePortListener& listener)
101 SysAssertf(__pIpcClient != null, "Not yet constructed. Construct() should be called before use.\n");
103 SysLog(NID_IO, "Register a message port : [%ls:%ls]", __appId.GetPointer(), localPort.GetPointer());
105 result r = E_SUCCESS;
107 bool contain = false;
108 String key = localPort;
112 __listeners.ContainsKey(key, contain);
116 __listeners.Add(key, const_cast<_IMessagePortListener*>(&listener));
121 __listeners.SetValue(key, const_cast<_IMessagePortListener*>(&listener));
126 __trustedListeners.ContainsKey(key, contain);
130 __trustedListeners.Add(key, const_cast<_IMessagePortListener*>(&listener));
135 __trustedListeners.SetValue(key, const_cast<_IMessagePortListener*>(&listener));
140 unique_ptr<IPC::Message> pMsg(new (std::nothrow) MessagePortService_register(__appId, localPort, isTrusted, &ret));
141 SysTryReturnResult(NID_IO, pMsg != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
143 r = __pIpcClient->SendRequest(pMsg.get());
144 SysTryReturnResult(NID_IO, r == E_SUCCESS, r, "Failed to register a message port.", GetErrorMessage(r));
145 SysTryReturnResult(NID_IO, ret == E_SUCCESS, ret, "Failed to register a message port.", GetErrorMessage(ret));
151 _MessagePortProxy::RequestRemotePort(const AppId& remoteAppId,
152 const String& remotePort,
155 SysAssertf(__pIpcClient != null, "Not yet constructed. Construct() should be called before use.\n");
157 SysLog(NID_IO, "Request a remote message port [%ls:%ls]", remoteAppId.GetPointer(), remotePort.GetPointer());
159 result r = E_SUCCESS;
164 package_manager_compare_result_type_e res;
166 unique_ptr<char[]> pLocal(_StringConverter::CopyToCharArrayN(__appId));
167 unique_ptr<char[]> pRemote(_StringConverter::CopyToCharArrayN(remoteAppId));
169 if (package_manager_compare_app_cert_info(pLocal.get(), pRemote.get(), &res) == 0)
171 if (res != PACAKGE_MANAGER_COMPARE_MATCH)
173 SysLogException(NID_IO, E_CERTIFICATE_VERIFICATION_FAILED, "[E_CERTIFICATE_VERIFICATION_FAILED] The target application is not signed with the same certificate.");
174 return E_CERTIFICATE_VERIFICATION_FAILED;
179 SysLogException(NID_IO, E_SYSTEM, "[E_SYSTEM] Failed to compare the cerificate infomation.");
184 unique_ptr<IPC::Message> pMsg(new (std::nothrow) MessagePortService_requestRemotePort(remoteAppId, remotePort, isTrusted, &ret));
185 SysTryReturnResult(NID_IO, pMsg != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
187 r = __pIpcClient->SendRequest(pMsg.get());
188 SysTryReturnResult(NID_IO, r == E_SUCCESS, r, "Failed to request a message port.", GetErrorMessage(r));
189 SysTryReturnResult(NID_IO, ret == E_SUCCESS, ret, "Failed to request a message port.", GetErrorMessage(ret));
195 _MessagePortProxy::SendMessage(const AppId& remoteAppId, const String& remotePort, bool isTrusted, const HashMap* pMap)
197 SysAssertf(__pIpcClient != null, "Not yet constructed. Construct() should be called before use.\n");
199 SysLog(NID_IO, "Send a unidirectional message to remote port [%ls:%ls]", remoteAppId.GetPointer(), remotePort.GetPointer());
201 result r = E_SUCCESS;
206 unique_ptr<IPC::Message> pMsg(new (std::nothrow) MessagePortService_sendMessage(remoteAppId, remotePort, isTrusted, *pMap, &ret));
207 SysTryReturnResult(NID_IO, pMsg != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
208 r = __pIpcClient->SendRequest(pMsg.get() );
211 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] Failed to send a message.", GetErrorMessage(r));
212 SysTryReturn(NID_IO, ret == E_SUCCESS, ret, ret, "[%s] Failed to send a message.", GetErrorMessage(ret));
218 _MessagePortProxy::SendMessage(const String& localPort, bool isTrustedLocal, const AppId& remoteAppId, const String& remotePort, bool isTrustedRemote, const HashMap* pMap)
220 SysAssertf(__pIpcClient != null, "Not yet constructed. Construct() should be called before use.\n");
222 result r = E_SUCCESS;
225 SysLog(NID_IO, "Send a bidirectional message from [%ls:%ls] to [%ls:%ls]", __appId.GetPointer(), localPort.GetPointer(), remoteAppId.GetPointer(), remotePort.GetPointer());
229 if (!isTrustedRemote)
231 unique_ptr<IPC::Message> pMsg(new (std::nothrow) MessagePortService_sendBidirMessage(__appId, localPort, isTrustedLocal, remoteAppId, remotePort, *pMap, &ret));
232 SysTryReturnResult(NID_IO, pMsg != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
233 r = __pIpcClient->SendRequest(pMsg.get());
237 unique_ptr<IPC::Message> pMsg(new (std::nothrow) MessagePortService_sendTrustedBidirMessage(__appId, localPort, isTrustedLocal, remoteAppId, remotePort, *pMap, &ret));
238 SysTryReturnResult(NID_IO, pMsg != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
239 r = __pIpcClient->SendRequest(pMsg.get());
243 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] Failed to send a message.", GetErrorMessage(r));
244 SysTryReturn(NID_IO, ret == E_SUCCESS, ret, ret, "[%s] Failed to send a message.", GetErrorMessage(ret));
250 _MessagePortProxy::OnSendMessage(const String& localPort, bool isTrusted, const HashMap& map)
252 SysLog(NID_IO, "A message is received to local port [%ls:%ls].", __appId.GetPointer(), localPort.GetPointer());
254 _IMessagePortListener* pListener = null;
256 String key = localPort;
260 __listeners.GetValue(key, pListener);
264 __trustedListeners.GetValue(key, pListener);
267 HashMap* pMap = new (std::nothrow) HashMap(SingleObjectDeleter);
268 SysTryReturn(NID_IO, pMap != null, false, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
270 pMap->Construct(map);
274 pListener->OnMessageReceivedN(pMap);
287 _MessagePortProxy::OnSendBidirMessage(const String& localPort, bool isTrustedLocal, const AppId& remoteAppId, const String& remotePort, bool isTrustedRemote, const HashMap& map)
289 SysLog(NID_IO, "A message is received to local port [%ls:%ls], from remote port [%ls:%ls].", __appId.GetPointer(), localPort.GetPointer(), remoteAppId.GetPointer(), remotePort.GetPointer());
291 _IMessagePortListener* pListener = null;
293 String key = localPort;
297 __listeners.GetValue(key, pListener);
301 __trustedListeners.GetValue(key, pListener);
304 HashMap* pMap = new (std::nothrow) HashMap(SingleObjectDeleter);
305 SysTryReturn(NID_IO, pMap != null, false, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
307 pMap->Construct(map);
311 pListener->OnMessageReceivedN(remoteAppId, remotePort, isTrustedRemote, pMap);
323 _MessagePortProxy::GetProxy(void)
325 static _MessagePortProxy* pProxy = null;
329 unique_ptr<_MessagePortProxy> p(new (std::nothrow) _MessagePortProxy);
330 SysTryReturn(NID_IO, p != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
332 result r = p->Construct();
333 SysTryReturn(NID_IO, r == E_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] Failed to initialize a message port proxy.");
335 pProxy = p.release();
338 SetLastResult(E_SUCCESS);