wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / MessagePort / MessagePortJSUtil.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 #include "MessagePortJSUtil.h"
19
20 #include <CommonsJavaScript/ScopedJSStringRef.h>
21
22 #include "JSLocalMessagePort.h"
23 #include "JSRemoteMessagePort.h"
24 #include <Logger.h>
25
26 #define MESSAGE_PORT_ATTRIBUTE_KEY "key"
27 #define MESSAGE_PORT_ATTRIBUTE_VALUE "value"
28
29 namespace DeviceAPI {
30 namespace MessagePort {
31
32 using namespace std;
33 using namespace DeviceAPI::Common;
34
35 JSValueRef MessagePortJSUtil::toJSValueRef(JSContextRef context, const MessagePortDataItemMapPtr &arg)
36 {
37         JSObjectRef resultObject = JSCreateArrayObject(context, 0, NULL);
38         if (!resultObject)
39                 throw TypeMismatchException("Can not create array object");
40
41         int i = 0;
42         MessagePortDataItemMap::iterator iter;
43         for(iter = arg->begin(); iter != arg->end(); iter++, i++)
44         {
45                 const WrtDeviceApis::CommonsJavaScript::ScopedJSStringRef keyStr(
46                                 JSStringCreateWithUTF8CString(MESSAGE_PORT_ATTRIBUTE_KEY));
47                 const WrtDeviceApis::CommonsJavaScript::ScopedJSStringRef valueStr(
48                                 JSStringCreateWithUTF8CString(MESSAGE_PORT_ATTRIBUTE_VALUE));
49
50                 JSValueRef key = JSUtil::toJSValueRef(context, iter->first);
51                 JSValueRef value = JSUtil::toJSValueRef(context, iter->second);
52
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);
56
57                 JSValueRef jsvalue = jsobject;
58
59                 if (!JSSetArrayElement(context, resultObject, i, jsvalue))
60                         throw TypeMismatchException("Can not fill contact array");
61         }
62
63         return static_cast<JSValueRef>(resultObject);
64 }
65
66 JSValueRef MessagePortJSUtil::toJSValueRef(JSContextRef context, const LocalMessagePortPtr &arg)
67 {
68         LocalMessagePortController *priv = new LocalMessagePortController(context, arg);
69         JSObjectRef object = JSObjectMake(context, JSLocalMessagePort::getClassRef(), priv);
70         if (!object)
71                 throw TypeMismatchException("Could not create JS object.");
72
73         return object;
74 }
75
76 JSValueRef MessagePortJSUtil::toJSValueRef(JSContextRef context, const RemoteMessagePortPtr &arg)
77 {
78         JSRemoteMessagePortPriv* priv = new JSRemoteMessagePortPriv(context, arg);
79         JSObjectRef object = JSObjectMake(context, JSRemoteMessagePort::getClassRef(), priv);
80         if (!object)
81                 throw TypeMismatchException("Could not create JS object.");
82
83         return object;
84 }
85
86 MessagePortDataItemMapPtr MessagePortJSUtil::JSValueToMessagePortDataItemMap(JSContextRef context, JSValueRef jsValue)
87 {
88         if(!JSIsArrayValue(context, jsValue))
89                 throw TypeMismatchException("MessagePortArray is not array.");
90
91         MessagePortDataItemMapPtr result = MessagePortDataItemMapPtr(new MessagePortDataItemMap());
92
93         const WrtDeviceApis::CommonsJavaScript::ScopedJSStringRef keyStr(
94                         JSStringCreateWithUTF8CString(MESSAGE_PORT_ATTRIBUTE_KEY));
95         const WrtDeviceApis::CommonsJavaScript::ScopedJSStringRef valueStr(
96                         JSStringCreateWithUTF8CString(MESSAGE_PORT_ATTRIBUTE_VALUE));
97
98         JSObjectRef jsObject = JSValueToObject(context, jsValue);
99         unsigned int length = JSGetArrayLength(context, jsObject);
100         for (std::size_t i = 0; i < length; i++)
101         {
102                 JSValueRef element = JSGetArrayElement(context, jsObject, i);
103
104                 if(!JSValueIsObject(context, element))
105                 {
106                         LoggerW("element is not object (" << i << ")");
107                         continue;
108                 }
109
110                 JSObjectRef jsObject = JSValueToObject(context, element);
111
112                 std::string key;
113                 std::string value;
114
115                 JSValueRef keyData = JSObjectGetProperty(context, jsObject, keyStr.get(), NULL);
116                 try
117                 {
118                         key = JSUtil::JSValueToString(context, keyData);
119                 }
120                 catch(BasePlatformException &e)
121                 {
122                         LoggerW("key is not string");
123                         continue;
124                 }
125
126                 JSValueRef valueData = JSObjectGetProperty(context, jsObject, valueStr.get(), NULL);
127                 try
128                 {
129                         value = JSUtil::JSValueToString(context, valueData);
130                 }
131                 catch(BasePlatformException &e)
132                 {
133                         LoggerW("value is not string");
134                         continue;
135                 }
136
137                 MessagePortDataItemPair pair(key, value);
138                 result->insert(pair);
139         }
140
141         return result;
142 }
143
144 LocalMessagePortPtr MessagePortJSUtil::JSValueToLocalMessagePort(JSContextRef context, JSValueRef jsValue)
145 {
146         if(!JSLocalMessagePort::isObjectOfClass(context, jsValue))
147         {
148                 LoggerE("Not a LocalMessagePort object");
149                 throw TypeMismatchException("value is not a LocalMessagePort object");
150         }
151
152         return JSLocalMessagePort::getLocalMessagePort(context, jsValue);
153 }
154
155 RemoteMessagePortPtr MessagePortJSUtil::JSValueToRemoteMessagePort(JSContextRef context, JSValueRef jsValue)
156 {
157         if(!JSRemoteMessagePort::isObjectOfClass(context, jsValue))
158         {
159                 LoggerE("Not a RemoteMessagePort object");
160                 throw TypeMismatchException("value is not a RemoteMessagePort object");
161         }
162
163         return JSRemoteMessagePort::getRemoteMessagePort(context, jsValue);
164 }
165
166 } // MessagePort
167 } // DeviceAPI