e6bd4db98dbc88c1c9efd3ed8bd01b393fc75ffa
[framework/web/wrt-plugins-tizen.git] / src / Tizen / JSCompositeFilter.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 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 /**
19  * @file        JSCompositeFilter.cpp
20  * @author      Kisub Song (kisubs.song@samsung.com)
21  * @version     0.1
22  * @brief       Implementation of the JSCompositeFilter class
23  */
24
25 #include <string>
26 #include <dpl/log/log.h>
27 #include <dpl/shared_ptr.h>
28 #include <CommonsJavaScript/Converter.h>
29 #include <CommonsJavaScript/Validator.h>
30 #include <JSTizenExceptionFactory.h>
31 #include <JSTizenException.h>
32 #include "CompositeFilter.h"
33 #include "FilterConverter.h"
34 #include "JSAbstractFilterArray.h"
35 #include "JSCompositeFilter.h"
36
37 #define ATTRIBUTE_FILTER_CLASS_NAME "CompositeFilter"
38
39 #define ATTRIBUTE_FILTER_ATTR_TYPE "type"
40 #define ATTRIBUTE_FILTER_ATTR_FILTERS "filters"
41
42 namespace DeviceAPI {\rnamespace Tizen {
43
44 using namespace DeviceAPI::Common;
45 using namespace DeviceAPI::Tizen;
46 using namespace WrtDeviceApis::Commons;
47 using namespace WrtDeviceApis::CommonsJavaScript;
48
49 JSClassRef JSCompositeFilter::m_classRef = NULL;
50
51 JSClassDefinition JSCompositeFilter::m_classInfo =
52 {
53         0,
54         kJSClassAttributeNone,
55         ATTRIBUTE_FILTER_CLASS_NAME,
56         NULL,
57         m_property,
58         m_functions,
59         Initialize,
60         Finalize,
61         NULL, //hasProperty,
62         NULL, //GetProperty,
63         NULL, //SetProperty,
64         NULL, //DeleteProperty,
65         NULL, //getPropertyNames,
66         NULL, //CallAsFunction,
67         constructor, //CallAsConstructor,
68         NULL, //HasInstance,
69         NULL, //ConvertToType,
70 };
71
72 JSStaticValue JSCompositeFilter::m_property[] = {
73         { ATTRIBUTE_FILTER_ATTR_TYPE, getType, setType, kJSPropertyAttributeNone },
74         { ATTRIBUTE_FILTER_ATTR_FILTERS, getFilters, setFilters, kJSPropertyAttributeNone },
75         { 0, 0, 0, 0 }
76 };
77
78 JSStaticFunction JSCompositeFilter::m_functions[] =
79 {
80         { 0, 0, 0 }
81 };
82
83 JSClassRef JSCompositeFilter::getClassRef() {
84         if (!m_classRef) {
85                 m_classRef = JSClassCreate(&m_classInfo);
86         }
87         return m_classRef;
88 }
89
90 bool JSCompositeFilter::isObjectOfClass(JSContextRef context, JSValueRef value)
91 {
92         return JSValueIsObjectOfClass(context, value, getClassRef());
93 }
94
95 CompositeFilterPtr JSCompositeFilter::getCompositeFilter(JSContextRef context, JSValueRef value)
96 {
97         if (!isObjectOfClass(context, value)) {
98                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
99         }
100         JSObjectRef object = JSValueToObject(context, value, NULL);
101         if (!object) {
102                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
103         }
104         JSCompositeFilterPriv *priv = static_cast<JSCompositeFilterPriv*>(JSObjectGetPrivate(object));
105         if (!priv) {
106                 Throw(WrtDeviceApis::Commons::NullPointerException);
107         }
108         return priv->getObject();
109 }
110
111 JSObjectRef JSCompositeFilter::createJSObject(JSContextRef context, CompositeFilterPtr privateData)
112 {
113         JSCompositeFilterPriv *priv = new JSCompositeFilterPriv(context, privateData);
114         JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
115         if (NULL == jsObjectRef) {
116                 LogError("object creation error");
117                 return NULL;
118         }
119         return jsObjectRef;
120 }
121
122 void JSCompositeFilter::Initialize(JSContextRef context, JSObjectRef object)
123 {
124         if (!JSObjectGetPrivate(object))
125         {
126                 CompositeFilterPtr filter(new CompositeFilter(UNION_FILTER, FilterArrayPtr(NULL)));
127                 JSCompositeFilterPriv *priv = new JSCompositeFilterPriv(context, CompositeFilterPtr(filter));
128                 if (!JSObjectSetPrivate(object, priv)) {
129                         delete priv;
130                 }
131         }
132 }
133
134 void JSCompositeFilter::Finalize(JSObjectRef object)
135 {
136         JSCompositeFilterPriv *priv = static_cast<JSCompositeFilterPriv*>(JSObjectGetPrivate(object));
137
138         if (priv != NULL)
139         {
140                 delete (priv);
141         }
142
143         priv = NULL;
144 }
145
146 CompositeFilterPtr JSCompositeFilter::getPrivData(JSObjectRef object)
147 {
148         LogDebug("entered");
149         JSCompositeFilterPriv *priv = static_cast<JSCompositeFilterPriv*>(JSObjectGetPrivate(object));
150         if (!priv) {
151                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
152         }
153         CompositeFilterPtr result = priv->getObject();
154         if (!result) {
155                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
156         }
157         return result;
158 }
159
160 JSObjectRef JSCompositeFilter::constructor(JSContextRef context,
161                 JSObjectRef constructor,
162                 size_t argumentCount,
163                 const JSValueRef arguments[],
164                 JSValueRef* exception)
165 {
166         LogDebug("entered");
167
168         bool js2ndParamIsObject = false;
169
170 //      AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_ADD);
171 //      TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
172
173         JSCompositeFilterPriv *priv = static_cast<JSCompositeFilterPriv*>(JSObjectGetPrivate(constructor));
174         if (!priv) {
175                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
176         }
177         JSContextRef gContext = priv->getContext();
178
179 //      JSContextRef gContext = JSGlobalContextFactory::getInstance()->get();
180
181         BasicValidator validator = BasicValidatorFactory::getValidator(gContext, exception);
182         Try {
183                 if (argumentCount < 1)
184                         ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
185
186                 if (!JSValueIsString(gContext, arguments[0]))
187                         ThrowMsg(InvalidArgumentException, "1st argument is not string.");
188
189                 if (argumentCount >= 2)
190                 {
191                         if (JSValueIsObject(gContext, arguments[1]))
192                                 js2ndParamIsObject = true;
193
194                         if (!js2ndParamIsObject &&
195                                         !JSValueIsNull(gContext, arguments[1]) &&
196                                         !JSValueIsUndefined(gContext, arguments[1]))
197                                 ThrowMsg(InvalidArgumentException, "2nd argument is not array.");
198                 }
199
200         } Catch(Exception ) {
201                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
202                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
203                 return NULL;
204         }
205
206         FilterConverterFactory::ConverterType converter = FilterConverterFactory::getConverter(gContext);
207
208         FilterType type;
209         FilterArrayPtr filters(NULL);
210
211         Try {
212                 type = converter->toCompositeFilterType(arguments[0]);
213         } Catch(Exception) {
214                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
215                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
216                 return NULL;
217         }
218
219         Try {
220                 if(js2ndParamIsObject)
221                         filters = converter->toFilterArray(arguments[1]);
222                 else
223                         filters = FilterArrayPtr(new FilterArray());
224
225         } Catch(Exception) {
226                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
227                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
228                 return NULL;
229         }
230
231         CompositeFilterPtr compositeFilter(new CompositeFilter(type, filters));
232
233         JSObjectRef jsobject;
234
235         Try {
236                 jsobject = createJSObject(gContext, compositeFilter);
237         } Catch(Exception) {
238                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
239                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
240                 return NULL;
241         }
242
243         return jsobject;
244 }
245
246 JSValueRef JSCompositeFilter::getType(JSContextRef context,
247                 JSObjectRef object,
248                 JSStringRef propertyName,
249                 JSValueRef* exception)
250 {
251         LogDebug("entered");
252         Try
253         {
254                 FilterConverterFactory::ConverterType converter =
255                                 FilterConverterFactory::getConverter(context);
256                 CompositeFilterPtr compositeFilter = getPrivData(object);
257                 return converter->toJSValueRef(compositeFilter->getFilterType());
258         }
259         Catch(WrtDeviceApis::Commons::Exception)
260         {
261                 LogWarning("trying to get incorrect value");
262         }
263         return JSValueMakeUndefined(context);
264 }
265
266 bool JSCompositeFilter::setType(JSContextRef context,
267                 JSObjectRef object,
268                 JSStringRef propertyName,
269                 JSValueRef value,
270                 JSValueRef* exception)
271 {
272         Try
273         {
274                 CompositeFilterPtr compositeFilter = getPrivData(object);
275                 FilterConverterFactory::ConverterType converter =
276                                 FilterConverterFactory::getConverter(context);
277                 compositeFilter->setFilterType(converter->toCompositeFilterType(value));
278                 return true;
279         }
280         Catch(WrtDeviceApis::Commons::Exception)
281         {
282                 LogWarning("trying to set incorrect value");
283         }
284
285         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
286         return false;
287 }
288
289 JSValueRef JSCompositeFilter::getFilters(JSContextRef context,
290                 JSObjectRef object,
291                 JSStringRef propertyName,
292                 JSValueRef* exception)
293 {
294         LogDebug("entered");
295         Try
296         {
297                 FilterConverterFactory::ConverterType converter =
298                                 FilterConverterFactory::getConverter(context);
299                 CompositeFilterPtr compositeFilter = getPrivData(object);
300                 return converter->toJSValueRef(compositeFilter->getFilters());
301         }
302         Catch(WrtDeviceApis::Commons::Exception)
303         {
304                 LogWarning("trying to get incorrect value");
305         }
306         return JSValueMakeUndefined(context);
307 }
308
309 bool JSCompositeFilter::setFilters(JSContextRef context,
310                 JSObjectRef object,
311                 JSStringRef propertyName,
312                 JSValueRef value,
313                 JSValueRef* exception)
314 {
315         Try
316         {
317                 CompositeFilterPtr compositeFilter = getPrivData(object);
318                 FilterConverterFactory::ConverterType converter =
319                                 FilterConverterFactory::getConverter(context);
320                 compositeFilter->setFilters(converter->toFilterArray(value));
321                 return true;
322         }
323         Catch(WrtDeviceApis::Commons::Exception)
324         {
325                 LogWarning("trying to set incorrect value");
326         }
327
328         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
329         return false;
330 }
331
332 } // Tizen
333 } // DeviceAPI
334