2 // Tizen Web Device API
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 MessagePortManagerProxy.cpp
20 * @author Kisub Song (kisubs.song@samsung.com)
26 #include "MessagePortManagerProxy.h"
29 #include <app_manager.h>
30 #include <dpl/singleton_impl.h>
32 #include "LocalMessagePort.h"
33 #include "RemoteMessagePort.h"
34 #include "MessagePortUtility.h"
37 IMPLEMENT_SINGLETON(DeviceAPI::MessagePort::MessagePortManagerProxy)
40 namespace MessagePort {
43 using namespace WrtDeviceApis::Commons;
45 MessagePortManagerProxy::MessagePortManagerProxy()
49 MessagePortManagerProxy::~MessagePortManagerProxy()
54 LocalMessagePortPtr MessagePortManagerProxy::requestLocalMessagePort(string &messagePortName)
56 LoggerD("request local (name : " << messagePortName << ")");
58 return getLocalMessagePort(messagePortName, false);
61 LocalMessagePortPtr MessagePortManagerProxy::requestTrustedLocalMessagePort(string &messagePortName)
63 LoggerD("request trusted local (name : " << messagePortName << ")");
65 return getLocalMessagePort(messagePortName, true);
68 RemoteMessagePortPtr MessagePortManagerProxy::requestRemoteMessagePort(string &appId, string &messagePortName)
70 LoggerD("request remote (appId : " << appId << " / name : " << messagePortName << ")");
72 return getRemoteMessagePort(appId, messagePortName, false);
75 RemoteMessagePortPtr MessagePortManagerProxy::requestTrustedRemoteMessagePort(string &appId, string &messagePortName)
77 LoggerD("request trusted remote (appId : " << appId << " / name : " << messagePortName << ")");
79 return getRemoteMessagePort(appId, messagePortName, true);
82 void MessagePortManagerProxy::sendMessage(string appId, string name, bool isTrusted,
83 MessagePortDataItemMapPtr &data, int localPortId)
85 bundle *b = MessagePortUtilitySingleton::Instance().getBundle(data);
91 if(localPortId == UNDEFINED_LOCAL_PORT_ID)
92 err = messageport_send_message(appId.c_str(), name.c_str(), b);
94 err = messageport_send_bidirectional_message(localPortId, appId.c_str(), name.c_str(), b);
98 if(localPortId == UNDEFINED_LOCAL_PORT_ID)
99 err = messageport_send_trusted_message(appId.c_str(), name.c_str(), b);
101 err = messageport_send_bidirectional_trusted_message(localPortId, appId.c_str(), name.c_str(), b);
108 case MESSAGEPORT_ERROR_INVALID_PARAMETER:
109 ThrowMsg(PlatformException, "The message argument is not a map of String key and String value pair.");
111 case MESSAGEPORT_ERROR_OUT_OF_MEMORY:
112 ThrowMsg(PlatformException, "The method cannot proceed due to a severe system error. (memory)");
114 case MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND:
115 ThrowMsg(NotFoundException, "The target application's port is not found.");
117 case MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH:
118 ThrowMsg(PlatformWrongStateException, "The target application is not signed with the same certificate.");
120 case MESSAGEPORT_ERROR_IO_ERROR:
121 ThrowMsg(PlatformException, "The method cannot proceed due to a severe system error. (I/O)");
123 case MESSAGEPORT_ERROR_MAX_EXCEEDED:
124 ThrowMsg(OutOfRangeException, "The size of message has exceeded the maximum limit.");
127 ThrowMsg(PlatformException, "The method cannot proceed due to a severe system error.");
133 LocalMessagePortPtr MessagePortManagerProxy::getLocalMessagePort(string &name, bool isTrusted)
135 LoggerD("getLocalMessagePort (name:" << name << ", isTrusted:" << isTrusted << ")");
136 LocalMessagePortPtr localMessagePort = getCachedLocalMessagePort(name, isTrusted);
138 if(localMessagePort == NULL)
142 id = messageport_register_trusted_local_port(name.c_str(), message_port_message_cb);
144 id = messageport_register_local_port(name.c_str(), message_port_message_cb);
151 case MESSAGEPORT_ERROR_INVALID_PARAMETER:
152 ThrowMsg(InvalidArgumentException, "The remote application ID or the remote message port name is empty.");
154 case MESSAGEPORT_ERROR_OUT_OF_MEMORY:
155 ThrowMsg(PlatformException, "The method cannot proceed due to a severe system error. (Memory)");
157 case MESSAGEPORT_ERROR_IO_ERROR:
158 ThrowMsg(PlatformException, "The method cannot proceed due to a severe system error. (I/O)");
161 ThrowMsg(PlatformException, "The method cannot proceed due to a severe system error. (Unknown code)");
166 localMessagePort = LocalMessagePortPtr(new LocalMessagePort(id, name, isTrusted));
168 cacheLocalMessagePort(id, name, isTrusted, localMessagePort);
171 return localMessagePort;
174 RemoteMessagePortPtr MessagePortManagerProxy::getRemoteMessagePort(string &appId, string &name, bool isTrusted)
176 LoggerD("getRemoteMessagePort (appId:" << appId << ", name:" << name << ", isTrusted:" << isTrusted << ")");
178 RemoteMessagePortPtr remoteMessagePort = getCachedRemoteMessagePort(appId, name, isTrusted);
180 if(remoteMessagePort == NULL)
185 errorCode = messageport_check_trusted_remote_port(appId.c_str(), name.c_str(), &exists);
187 errorCode = messageport_check_remote_port(appId.c_str(), name.c_str(), &exists);
189 if(!exists || errorCode < 0)
193 case MESSAGEPORT_ERROR_INVALID_PARAMETER:
194 ThrowMsg(InvalidArgumentException, "The remote application ID or the remote message port name is empty.");
196 case MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND:
197 ThrowMsg(NotFoundException, "The target application's port is not found.");
199 case MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH:
200 ThrowMsg(PlatformWrongStateException, "The target application is not signed with the same certificate.");
202 case MESSAGEPORT_ERROR_OUT_OF_MEMORY:
203 ThrowMsg(PlatformException, "The method cannot proceed due to a severe system error. (Memory)");
205 case MESSAGEPORT_ERROR_IO_ERROR:
206 ThrowMsg(PlatformException, "The method cannot proceed due to a severe system error. (I/O)");
209 ThrowMsg(NotFoundException, "The target application's port is not found. (Unknown code)");
214 remoteMessagePort = RemoteMessagePortPtr(new RemoteMessagePort(appId, name, isTrusted));
216 cacheRemoteMessagePort(appId, name, isTrusted, remoteMessagePort);
219 return remoteMessagePort;
222 void MessagePortManagerProxy::cacheLocalMessagePort(int id, std::string &name, bool isTrusted, LocalMessagePortPtr &localMessagePort)
224 LocalMessagePortKey key = { name, isTrusted };
226 LocalMessagePortPair keyPair(key, localMessagePort);
227 m_localMessagePortMap.insert(keyPair);
228 LocalMessagePortIdPair idPair(id, localMessagePort);
229 m_localMessagePortIdMap.insert(idPair);
232 LocalMessagePortPtr MessagePortManagerProxy::getCachedLocalMessagePort(string &name, bool isTrusted)
234 LocalMessagePortPtr localMessagePort(NULL);
236 LocalMessagePortKey key = { name, isTrusted };
237 LocalMessagePortMap::iterator iter = m_localMessagePortMap.find(key);
238 if(iter != m_localMessagePortMap.end())
240 localMessagePort = iter->second;
241 LoggerD("Found cached LocalMessagePort");
244 return localMessagePort;
247 LocalMessagePortPtr MessagePortManagerProxy::getCachedLocalMessagePort(int id)
249 LocalMessagePortPtr localMessagePort(NULL);
251 LocalMessagePortIdMap::iterator iter = m_localMessagePortIdMap.find(id);
252 if(iter != m_localMessagePortIdMap.end())
254 localMessagePort = iter->second;
255 LoggerD("Found cached LocalMessagePort");
258 return localMessagePort;
261 void MessagePortManagerProxy::cacheRemoteMessagePort(std::string &appId, std::string &name, bool isTrusted, RemoteMessagePortPtr &remoteMessagePort)
263 RemoteMessagePortKey key = { appId, name, isTrusted };
265 RemoteMessagePortPair keyPair(key, remoteMessagePort);
266 m_remoteMessagePortMap.insert(keyPair);
269 RemoteMessagePortPtr MessagePortManagerProxy::getCachedRemoteMessagePort(string &appId, string &name, bool isTrusted)
271 RemoteMessagePortPtr remoteMessagePort(NULL);
273 RemoteMessagePortKey key = { appId, name, isTrusted };
274 RemoteMessagePortMap::iterator iter = m_remoteMessagePortMap.find(key);
275 if(iter != m_remoteMessagePortMap.end())
277 remoteMessagePort = iter->second;
278 LoggerD("Found cached RemoteMessagePort");
281 return remoteMessagePort;
284 void MessagePortManagerProxy::messagePortMessageCb(int id, const char* remote_app_id,
285 const char* remote_port, bool trusted_message, bundle* data)
287 LoggerD("received message : (id:" << id << ", remote_appId : " << remote_app_id <<
288 ", remote_port:" << remote_port << ", trusted:" << trusted_message << ")");
290 LocalMessagePortPtr localMessagePort = getCachedLocalMessagePort(id);
291 if(localMessagePort == NULL)
293 LoggerW("There is no local message port id : " << id);
297 RemoteMessagePortPtr remoteMessagePort(NULL);
299 if(remote_app_id != NULL && remote_port != NULL)
301 string remoteAppId(remote_app_id);
302 string remotePortName(remote_port);
305 remoteMessagePort = getRemoteMessagePort(remoteAppId, remotePortName, trusted_message);
309 LoggerE("Error on getRemoteMessagePort : " << _rethrown_exception.GetMessage());
313 MessagePortDataItemMapPtr dataItemMap = MessagePortUtilitySingleton::Instance().getDataItemMap(data);
315 localMessagePort->OnMessageReceived(dataItemMap, remoteMessagePort);
319 void MessagePortManagerProxy::message_port_message_cb(int id, const char* remote_app_id,
320 const char* remote_port, bool trusted_message, bundle* data)
322 MessagePortManagerProxySingleton::Instance().messagePortMessageCb(id, remote_app_id,
323 remote_port, trusted_message, data);