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>
25 #include "JSLocalMessagePort.h"
26 #include "JSRemoteMessagePort.h"
29 #define MESSAGE_PORT_ATTRIBUTE_KEY "key"
30 #define MESSAGE_PORT_ATTRIBUTE_VALUE "value"
33 namespace MessagePort {
36 using namespace DeviceAPI::Common;
38 JSValueRef MessagePortJSUtil::toJSValueRef(JSContextRef context, const MessagePortDataItemMapPtr &arg)
40 JSObjectRef resultObject = JSCreateArrayObject(context, 0, NULL);
42 throw TypeMismatchException("Can not create array object");
45 MessagePortDataItemMap::iterator iter;
46 for(iter = arg->begin(); iter != arg->end(); iter++, i++)
48 const WrtDeviceApis::CommonsJavaScript::ScopedJSStringRef keyStr(
49 JSStringCreateWithUTF8CString(MESSAGE_PORT_ATTRIBUTE_KEY));
50 const WrtDeviceApis::CommonsJavaScript::ScopedJSStringRef valueStr(
51 JSStringCreateWithUTF8CString(MESSAGE_PORT_ATTRIBUTE_VALUE));
53 JSValueRef key = JSUtil::toJSValueRef(context, iter->first);
54 JSValueRef value = JSUtil::toJSValueRef(context, iter->second);
56 JSObjectRef jsobject = JSObjectMake(context, NULL, NULL);
57 JSObjectSetProperty(context, jsobject, keyStr.get(), key, kJSPropertyAttributeNone, NULL);
58 JSObjectSetProperty(context, jsobject, valueStr.get(), value, kJSPropertyAttributeNone, NULL);
60 JSValueRef jsvalue = jsobject;
62 if (!JSSetArrayElement(context, resultObject, i, jsvalue))
63 throw TypeMismatchException("Can not fill contact array");
66 return static_cast<JSValueRef>(resultObject);
69 JSValueRef MessagePortJSUtil::toJSValueRef(JSContextRef context, const LocalMessagePortPtr &arg)
71 LocalMessagePortController *priv = new LocalMessagePortController(context, arg);
72 JSObjectRef object = JSObjectMake(context, JSLocalMessagePort::getClassRef(), priv);
74 throw TypeMismatchException("Could not create JS object.");
79 JSValueRef MessagePortJSUtil::toJSValueRef(JSContextRef context, const RemoteMessagePortPtr &arg)
81 JSRemoteMessagePortPriv* priv = new JSRemoteMessagePortPriv(context, arg);
82 JSObjectRef object = JSObjectMake(context, JSRemoteMessagePort::getClassRef(), priv);
84 throw TypeMismatchException("Could not create JS object.");
89 MessagePortDataItemMapPtr MessagePortJSUtil::JSValueToMessagePortDataItemMap(JSContextRef context, JSValueRef jsValue)
91 if(!JSIsArrayValue(context, jsValue))
92 throw TypeMismatchException("MessagePortArray is not array.");
94 MessagePortDataItemMapPtr result = MessagePortDataItemMapPtr(new MessagePortDataItemMap());
96 const WrtDeviceApis::CommonsJavaScript::ScopedJSStringRef keyStr(
97 JSStringCreateWithUTF8CString(MESSAGE_PORT_ATTRIBUTE_KEY));
98 const WrtDeviceApis::CommonsJavaScript::ScopedJSStringRef valueStr(
99 JSStringCreateWithUTF8CString(MESSAGE_PORT_ATTRIBUTE_VALUE));
101 set<string> keyDupChecker;
102 JSObjectRef jsObject = JSValueToObject(context, jsValue);
103 unsigned int length = JSGetArrayLength(context, jsObject);
104 for (std::size_t i = 0; i < length; i++)
106 JSValueRef element = JSGetArrayElement(context, jsObject, i);
108 if(!JSValueIsObject(context, element))
111 ss << "data[" << i << "] is not object type";
112 throw TypeMismatchException(ss.str().c_str());
115 JSObjectRef jsObject = JSValueToObject(context, element);
120 JSValueRef keyData = JSObjectGetProperty(context, jsObject, keyStr.get(), NULL);
123 key = JSUtil::JSValueToString(context, keyData);
125 catch(BasePlatformException &e)
128 ss << "data[" << i << "].key is not string type";
129 throw TypeMismatchException(ss.str().c_str());
135 ss << "data[" << i << "].key is empty";
136 throw InvalidValuesException(ss.str().c_str());
139 if(keyDupChecker.find(key) != keyDupChecker.end())
142 ss << "data[" << i << "].key \"" << key << "\" is duplicated";
143 throw InvalidValuesException(ss.str().c_str());
146 keyDupChecker.insert(key);
148 JSValueRef valueData = JSObjectGetProperty(context, jsObject, valueStr.get(), NULL);
151 value = JSUtil::JSValueToString(context, valueData);
153 catch(BasePlatformException &e)
156 ss << "data[" << i << "].value is not string type";
157 throw TypeMismatchException(ss.str().c_str());
160 MessagePortDataItemPair pair(key, value);
161 result->insert(pair);
167 LocalMessagePortPtr MessagePortJSUtil::JSValueToLocalMessagePort(JSContextRef context, JSValueRef jsValue)
169 if(!JSLocalMessagePort::isObjectOfClass(context, jsValue))
171 LoggerE("Not a LocalMessagePort object");
172 throw TypeMismatchException("value is not a LocalMessagePort object");
175 return JSLocalMessagePort::getLocalMessagePort(context, jsValue);
178 RemoteMessagePortPtr MessagePortJSUtil::JSValueToRemoteMessagePort(JSContextRef context, JSValueRef jsValue)
180 if(!JSRemoteMessagePort::isObjectOfClass(context, jsValue))
182 LoggerE("Not a RemoteMessagePort object");
183 throw TypeMismatchException("value is not a RemoteMessagePort object");
186 return JSRemoteMessagePort::getRemoteMessagePort(context, jsValue);