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 JSRemoteMessagePort.cpp
20 * @author Kisub Song (kisubs.song@samsung.com)
22 * @brief Implementation of the JSRemoteMessagePort class
25 #include "JSRemoteMessagePort.h"
27 #include <dpl/log/log.h>
28 #include <CommonsJavaScript/Validator.h>
29 #include <CommonsJavaScript/Converter.h>
30 #include <CommonsJavaScript/JSCallbackManager.h>
31 #include <CommonsJavaScript/JSUtils.h>
32 #include <CommonsJavaScript/JSPendingOperation.h>
33 #include <JSWebAPIException.h>
34 #include "EventRemoteMessagePortSendMessage.h"
35 #include "JSLocalMessagePort.h"
36 #include "MessagePortJSUtil.h"
38 #define TIZEN_REMOTE_MESSAGE_PORT "RemoteMessagePort"
40 #define TIZEN_REMOTE_MESSAGE_PORT_MESSAGE_PORT_NAME "messagePortName"
41 #define TIZEN_REMOTE_MESSAGE_PORT_APP_ID "appId"
42 #define TIZEN_REMOTE_MESSAGE_PORT_IS_TRUSTED "isTrusted"
44 #define TIZEN_REMOTE_MESSAGE_PORT_SEND_MESSAGE "sendMessage"
47 namespace MessagePort {
50 using namespace DeviceAPI::Common;
51 using namespace WrtDeviceApis::Commons;
52 using namespace WrtDeviceApis::CommonsJavaScript;
54 JSClassRef JSRemoteMessagePort::m_jsClassRef = NULL;
56 JSClassDefinition JSRemoteMessagePort::m_classInfo = {
58 kJSClassAttributeNone,
59 TIZEN_REMOTE_MESSAGE_PORT,
68 NULL, //DeleteProperty,
69 NULL, //GetPropertyNames,
70 NULL, //CallAsFunction,
71 NULL, //CallAsConstructor,
73 NULL, //ConvertToType,
76 JSStaticValue JSRemoteMessagePort::m_property[] = {
77 { TIZEN_REMOTE_MESSAGE_PORT_MESSAGE_PORT_NAME, getMessagePortName, NULL, kJSPropertyAttributeReadOnly },
78 { TIZEN_REMOTE_MESSAGE_PORT_APP_ID, getAppId, NULL, kJSPropertyAttributeReadOnly },
79 { TIZEN_REMOTE_MESSAGE_PORT_IS_TRUSTED, getIsTrusted, NULL, kJSPropertyAttributeReadOnly },
83 JSStaticFunction JSRemoteMessagePort::m_function[] = {
84 { TIZEN_REMOTE_MESSAGE_PORT_SEND_MESSAGE, sendMessage, kJSPropertyAttributeNone },
88 const JSClassDefinition* JSRemoteMessagePort::getClassInfo()
93 const JSClassRef JSRemoteMessagePort::getClassRef()
97 m_jsClassRef = JSClassCreate(&m_classInfo);
102 void JSRemoteMessagePort::Initialize(JSContextRef context,
107 void JSRemoteMessagePort::Finalize(JSObjectRef object)
109 JSRemoteMessagePortPriv *priv =
110 static_cast<JSRemoteMessagePortPriv*>(JSObjectGetPrivate(object));
115 bool JSRemoteMessagePort::isObjectOfClass(JSContextRef context, JSValueRef value)
117 return JSValueIsObjectOfClass(context, value, getClassRef());
120 RemoteMessagePortPtr JSRemoteMessagePort::getRemoteMessagePort(JSContextRef context, JSValueRef value)
122 if (!isObjectOfClass(context, value))
123 throw TypeMismatchException("value is not a RemoteMessagePort object");
125 JSObjectRef object = JSValueToObject(context, value, NULL);
127 throw TypeMismatchException("value is not a object");
129 JSRemoteMessagePortPriv *priv = static_cast<JSRemoteMessagePortPriv*>(JSObjectGetPrivate(object));
131 throw TypeMismatchException("cannot get priv object from RemoteMessagePort object");
133 return priv->getObject();
136 RemoteMessagePortPtr JSRemoteMessagePort::getPrivData(JSObjectRef object)
138 JSRemoteMessagePortPriv *priv =
139 static_cast<JSRemoteMessagePortPriv*>(JSObjectGetPrivate(object));
142 return priv->getObject();
144 throw TypeMismatchException("cannot get priv object from RemoteMessagePort object");
147 JSValueRef JSRemoteMessagePort::getMessagePortName(JSContextRef context,
149 JSStringRef propertyName,
150 JSValueRef* exception)
154 RemoteMessagePortPtr localMessagePort = getPrivData(object);
155 return JSUtil::toJSValueRef(context, localMessagePort->getMessagePortName());
157 catch(BasePlatformException &e)
159 LogWarning("trying to get incorrect value");
162 return JSValueMakeUndefined(context);
165 JSValueRef JSRemoteMessagePort::getAppId(JSContextRef context,
167 JSStringRef propertyName,
168 JSValueRef* exception)
172 RemoteMessagePortPtr localMessagePort = getPrivData(object);
173 return JSUtil::toJSValueRef(context, localMessagePort->getAppId());
175 catch(BasePlatformException &e)
177 LogWarning("trying to get incorrect value");
180 return JSValueMakeUndefined(context);
183 JSValueRef JSRemoteMessagePort::getIsTrusted(JSContextRef context,
185 JSStringRef propertyName,
186 JSValueRef* exception)
190 RemoteMessagePortPtr localMessagePort = getPrivData(object);
191 return JSUtil::toJSValueRef(context, localMessagePort->getIsTrusted());
193 catch(BasePlatformException &e)
195 LogWarning("trying to get incorrect value");
198 return JSValueMakeUndefined(context);
201 JSValueRef JSRemoteMessagePort::sendMessage(JSContextRef context,
203 JSObjectRef thisObject,
204 size_t argumentCount,
205 const JSValueRef arguments[],
206 JSValueRef* exception)
209 RemoteMessagePortPtr remoteMessagePort(NULL);
213 JSRemoteMessagePortPriv *priv =
214 static_cast<JSRemoteMessagePortPriv*>(JSObjectGetPrivate(thisObject));
217 throw TypeMismatchException("Wrong object");
219 remoteMessagePort = priv->getObject();
221 catch(BasePlatformException &e)
223 LogError("No private object");
224 return JSWebAPIException::throwException(context, exception, e);
227 MessagePortDataItemMapPtr data(NULL);
228 LocalMessagePortPtr localMessagePort(NULL);
232 if(argumentCount < 1)
233 throw TypeMismatchException("1st argument must be array of MessagePortDataItem");
235 data = MessagePortJSUtil::JSValueToMessagePortDataItemMap(context, arguments[0]);
237 catch(BasePlatformException &e)
239 return JSWebAPIException::throwException(context, exception, e);
244 if(argumentCount >= 2 &&
245 !JSValueIsUndefined(context, arguments[1]) &&
246 !JSValueIsNull(context, arguments[1]))
247 localMessagePort = MessagePortJSUtil::JSValueToLocalMessagePort(context, arguments[1]);
249 catch(BasePlatformException &e)
251 return JSWebAPIException::throwException(context, exception, e);
254 EventRemoteMessagePortSendMessagePtr dplEvent(new EventRemoteMessagePortSendMessage());
256 dplEvent->setData(data);
257 if(localMessagePort != NULL)
258 dplEvent->setLocalMessagePort(localMessagePort);
259 dplEvent->setForSynchronousCall();
263 remoteMessagePort->sendMessage(dplEvent);
267 LogError("Error on platform : " << _rethrown_exception.GetMessage());
268 return JSWebAPIException::throwException(context, exception,
269 UnknownException("Plugin's internal error."));
272 if (!dplEvent->getResult())
274 switch (dplEvent->getExceptionCode())
276 case ExceptionCodes::ConversionException:
277 return JSWebAPIException::throwException(context, exception,
278 TypeMismatchException("Wrong LocalMessagePort object."));
280 case ExceptionCodes::InvalidArgumentException:
281 return JSWebAPIException::throwException(context, exception,
282 InvalidValuesException("The data argument is wrong."));
284 case ExceptionCodes::NotFoundException:
285 return JSWebAPIException::throwException(context, exception,
286 UnknownException("The target application's port is not found."));
288 case ExceptionCodes::PlatformException:
289 return JSWebAPIException::throwException(context, exception,
290 UnknownException("The method cannot proceed due to a severe system error."));
292 case ExceptionCodes::OutOfRangeException:
293 return JSWebAPIException::throwException(context, exception,
294 UnknownException("The size of message has exceeded the maximum limit."));
297 return JSWebAPIException::throwException(context, exception,
298 UnknownException("Internal error"));
303 return JSValueMakeUndefined(context);