2 // Tizen Web Device API
3 // Copyright (c) 2013 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.
18 #include "MessagePortJSUtil.h"
20 #include <CommonsJavaScript/ScopedJSStringRef.h>
22 #include "JSLocalMessagePort.h"
23 #include "JSRemoteMessagePort.h"
26 #define MESSAGE_PORT_ATTRIBUTE_KEY "key"
27 #define MESSAGE_PORT_ATTRIBUTE_VALUE "value"
30 namespace MessagePort {
33 using namespace DeviceAPI::Common;
35 JSValueRef MessagePortJSUtil::toJSValueRef(JSContextRef context, const MessagePortDataItemMapPtr &arg)
37 JSObjectRef resultObject = JSCreateArrayObject(context, 0, NULL);
39 throw TypeMismatchException("Can not create array object");
42 MessagePortDataItemMap::iterator iter;
43 for(iter = arg->begin(); iter != arg->end(); iter++, i++)
45 const WrtDeviceApis::CommonsJavaScript::ScopedJSStringRef keyStr(
46 JSStringCreateWithUTF8CString(MESSAGE_PORT_ATTRIBUTE_KEY));
47 const WrtDeviceApis::CommonsJavaScript::ScopedJSStringRef valueStr(
48 JSStringCreateWithUTF8CString(MESSAGE_PORT_ATTRIBUTE_VALUE));
50 JSValueRef key = JSUtil::toJSValueRef(context, iter->first);
51 JSValueRef value = JSUtil::toJSValueRef(context, iter->second);
53 JSObjectRef jsobject = JSObjectMake(context, NULL, NULL);
54 JSObjectSetProperty(context, jsobject, keyStr.get(), key, kJSPropertyAttributeNone, NULL);
55 JSObjectSetProperty(context, jsobject, valueStr.get(), value, kJSPropertyAttributeNone, NULL);
57 JSValueRef jsvalue = jsobject;
59 if (!JSSetArrayElement(context, resultObject, i, jsvalue))
60 throw TypeMismatchException("Can not fill contact array");
63 return static_cast<JSValueRef>(resultObject);
66 JSValueRef MessagePortJSUtil::toJSValueRef(JSContextRef context, const LocalMessagePortPtr &arg)
68 LocalMessagePortController *priv = new LocalMessagePortController(context, arg);
69 JSObjectRef object = JSObjectMake(context, JSLocalMessagePort::getClassRef(), priv);
71 throw TypeMismatchException("Could not create JS object.");
76 JSValueRef MessagePortJSUtil::toJSValueRef(JSContextRef context, const RemoteMessagePortPtr &arg)
78 JSRemoteMessagePortPriv* priv = new JSRemoteMessagePortPriv(context, arg);
79 JSObjectRef object = JSObjectMake(context, JSRemoteMessagePort::getClassRef(), priv);
81 throw TypeMismatchException("Could not create JS object.");
86 MessagePortDataItemMapPtr MessagePortJSUtil::JSValueToMessagePortDataItemMap(JSContextRef context, JSValueRef jsValue)
88 if(!JSIsArrayValue(context, jsValue))
89 throw TypeMismatchException("MessagePortArray is not array.");
91 MessagePortDataItemMapPtr result = MessagePortDataItemMapPtr(new MessagePortDataItemMap());
93 const WrtDeviceApis::CommonsJavaScript::ScopedJSStringRef keyStr(
94 JSStringCreateWithUTF8CString(MESSAGE_PORT_ATTRIBUTE_KEY));
95 const WrtDeviceApis::CommonsJavaScript::ScopedJSStringRef valueStr(
96 JSStringCreateWithUTF8CString(MESSAGE_PORT_ATTRIBUTE_VALUE));
98 JSObjectRef jsObject = JSValueToObject(context, jsValue);
99 unsigned int length = JSGetArrayLength(context, jsObject);
100 for (std::size_t i = 0; i < length; i++)
102 JSValueRef element = JSGetArrayElement(context, jsObject, i);
104 if(!JSValueIsObject(context, element))
106 LoggerW("element is not object (" << i << ")");
110 JSObjectRef jsObject = JSValueToObject(context, element);
115 JSValueRef keyData = JSObjectGetProperty(context, jsObject, keyStr.get(), NULL);
118 key = JSUtil::JSValueToString(context, keyData);
120 catch(BasePlatformException &e)
122 LoggerW("key is not string");
126 JSValueRef valueData = JSObjectGetProperty(context, jsObject, valueStr.get(), NULL);
129 value = JSUtil::JSValueToString(context, valueData);
131 catch(BasePlatformException &e)
133 LoggerW("value is not string");
137 MessagePortDataItemPair pair(key, value);
138 result->insert(pair);
144 LocalMessagePortPtr MessagePortJSUtil::JSValueToLocalMessagePort(JSContextRef context, JSValueRef jsValue)
146 if(!JSLocalMessagePort::isObjectOfClass(context, jsValue))
148 LoggerE("Not a LocalMessagePort object");
149 throw TypeMismatchException("value is not a LocalMessagePort object");
152 return JSLocalMessagePort::getLocalMessagePort(context, jsValue);
155 RemoteMessagePortPtr MessagePortJSUtil::JSValueToRemoteMessagePort(JSContextRef context, JSValueRef jsValue)
157 if(!JSRemoteMessagePort::isObjectOfClass(context, jsValue))
159 LoggerE("Not a RemoteMessagePort object");
160 throw TypeMismatchException("value is not a RemoteMessagePort object");
163 return JSRemoteMessagePort::getRemoteMessagePort(context, jsValue);