Update change log and spec for wrt-plugins-tizen_0.4.11
[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 <CommonsJavaScript/ScopedJSStringRef.h>
31 #include <JSTizenExceptionFactory.h>
32 #include <JSTizenException.h>
33 #include "CompositeFilter.h"
34 #include "FilterConverter.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         setProperty,
64         NULL,//deleteProperty,
65         NULL,//getPropertyNames,
66         NULL, //CallAsFunction,
67         NULL, //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
120         FilterConverterFactory::ConverterType converter = FilterConverterFactory::getConverter(context);
121
122         const ScopedJSStringRef jsStrFilters(JSStringCreateWithUTF8CString(ATTRIBUTE_FILTER_ATTR_FILTERS));
123         JSValueRef jsValueFilters =     converter->toJSValueRef(privateData->getFilters());
124
125         JSObjectSetProperty(context, jsObjectRef, jsStrFilters.get(), jsValueFilters, kJSPropertyAttributeNone, NULL);
126
127         return jsObjectRef;
128 }
129
130 JSObjectRef JSCompositeFilter::constructor(JSContextRef context,
131                 JSObjectRef constructor,
132                 size_t argumentCount,
133                 const JSValueRef arguments[],
134                 JSValueRef* exception)
135 {
136         LogDebug("entered");
137
138         bool js2ndParamIsObject = false;
139
140         BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
141         Try {
142                 if (argumentCount < 1)
143                         ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
144
145                 if (!JSValueIsString(context, arguments[0]))
146                         ThrowMsg(InvalidArgumentException, "1st argument is not string.");
147
148                 if (argumentCount >= 2)
149                 {
150                         if (JSValueIsObject(context, arguments[1]))
151                                 js2ndParamIsObject = true;
152
153                         if (!js2ndParamIsObject &&
154                                         !JSValueIsNull(context, arguments[1]) &&
155                                         !JSValueIsUndefined(context, arguments[1]))
156                                 ThrowMsg(InvalidArgumentException, "2nd argument is not array.");
157                 }
158
159         } Catch(Exception ) {
160                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
161                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
162                 return NULL;
163         }
164
165         FilterConverterFactory::ConverterType converter = FilterConverterFactory::getConverter(context);
166
167         FilterType type;
168         FilterArrayPtr filters(NULL);
169
170         Try {
171                 type = converter->toCompositeFilterType(arguments[0]);
172         } Catch(Exception) {
173                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
174                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
175                 return NULL;
176         }
177
178         Try {
179                 if(js2ndParamIsObject)
180                         filters = converter->toFilterArray(arguments[1]);
181                 else
182                         filters = FilterArrayPtr(new FilterArray());
183
184         } Catch(Exception) {
185                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
186                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
187                 return NULL;
188         }
189
190         CompositeFilterPtr compositeFilter(new CompositeFilter(type, filters));
191
192         JSObjectRef jsobject;
193
194         Try {
195                 jsobject = createJSObject(context, compositeFilter);
196         } Catch(Exception) {
197                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
198                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
199                 return NULL;
200         }
201
202         return jsobject;
203 }
204
205 void JSCompositeFilter::Initialize(JSContextRef context, JSObjectRef object)
206 {
207         if (!JSObjectGetPrivate(object))
208         {
209                 CompositeFilterPtr filter(new CompositeFilter(UNION_FILTER, FilterArrayPtr(NULL)));
210                 JSCompositeFilterPriv *priv = new JSCompositeFilterPriv(context, CompositeFilterPtr(filter));
211                 if (!JSObjectSetPrivate(object, priv)) {
212                         delete priv;
213                 }
214         }
215 }
216
217 void JSCompositeFilter::Finalize(JSObjectRef object)
218 {
219         JSCompositeFilterPriv *priv = static_cast<JSCompositeFilterPriv*>(JSObjectGetPrivate(object));
220
221         if (priv != NULL)
222         {
223                 delete (priv);
224         }
225
226         priv = NULL;
227 }
228
229 CompositeFilterPtr JSCompositeFilter::getPrivData(JSObjectRef object)
230 {
231         LogDebug("entered");
232         JSCompositeFilterPriv *priv = static_cast<JSCompositeFilterPriv*>(JSObjectGetPrivate(object));
233         if (!priv) {
234                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
235         }
236         CompositeFilterPtr result = priv->getObject();
237         if (!result) {
238                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
239         }
240         return result;
241 }
242
243 bool JSCompositeFilter::hasProperty(JSContextRef context,
244                 JSObjectRef object,
245                 JSStringRef propertyName)
246 {
247         LogDebug("entered");
248         Try
249         {
250                 CompositeFilterPtr compositeFilter = getPrivData(object);
251
252                 FilterConverterFactory::ConverterType converter =
253                                 FilterConverterFactory::getConverter(context);
254                 std::string name = converter->toString(propertyName);
255
256                 if(name == ATTRIBUTE_FILTER_ATTR_FILTERS ||
257                                 name == ATTRIBUTE_FILTER_ATTR_TYPE )
258                         return true;
259         }
260         Catch(WrtDeviceApis::Commons::Exception)
261         {
262                 LogWarning("trying to get incorrect value");
263                 return false;
264         }
265
266         return false;
267 }
268
269 JSValueRef JSCompositeFilter::getProperty(JSContextRef context,
270                 JSObjectRef object,
271                 JSStringRef propertyName,
272                 JSValueRef* exception)
273 {
274         LogDebug("entered");
275         Try
276         {
277                 CompositeFilterPtr compositeFilter = getPrivData(object);
278
279                 FilterConverterFactory::ConverterType converter =
280                                 FilterConverterFactory::getConverter(context);
281                 std::string name = converter->toString(propertyName);
282                 if(name == ATTRIBUTE_FILTER_ATTR_FILTERS)
283                         return NULL;
284         }
285         Catch(WrtDeviceApis::Commons::Exception)
286         {
287                 LogWarning("trying to get incorrect value");
288         }
289
290         return JSValueMakeUndefined(context);
291 }
292
293 bool JSCompositeFilter::setProperty(JSContextRef context,
294                 JSObjectRef object,
295                 JSStringRef propertyName,
296                 JSValueRef value,
297                 JSValueRef* exception)
298 {
299         LogDebug("entered");
300         Try
301         {
302                 CompositeFilterPtr compositeFilter = getPrivData(object);
303                 FilterConverterFactory::ConverterType converter =
304                                 FilterConverterFactory::getConverter(context);
305                 std::string name = converter->toString(propertyName);
306                 if(name == ATTRIBUTE_FILTER_ATTR_FILTERS)
307                 {
308                         if(!JSIsArrayValue(context, value))
309                                 return true;
310
311                         return false;
312 //                      const ScopedJSStringRef jsStrFilters(
313 //                                      JSStringCreateWithUTF8CString(ATTRIBUTE_FILTER_ATTR_FILTERS));
314 //
315 //                      JSObjectSetProperty(context, object, jsStrFilters.get(),
316 //                                      value, kJSPropertyAttributeNone, NULL);
317 //
318 //                      return true;
319                 }
320         }
321         Catch(WrtDeviceApis::Commons::Exception)
322         {
323                 LogWarning("trying to set incorrect value");
324         }
325
326         return false;
327 }
328
329 bool JSCompositeFilter::deleteProperty(JSContextRef context,
330                 JSObjectRef object,
331                 JSStringRef propertyName,
332                 JSValueRef* exception)
333 {
334         LogDebug("entered");
335         Try
336         {
337                 CompositeFilterPtr compositeFilter = getPrivData(object);
338                 FilterConverterFactory::ConverterType converter =
339                                 FilterConverterFactory::getConverter(context);
340                 std::string name = converter->toString(propertyName);
341                 if(name == ATTRIBUTE_FILTER_ATTR_FILTERS)
342                         return true;
343         }
344         Catch(WrtDeviceApis::Commons::Exception)
345         {
346                 LogWarning("trying to set incorrect value");
347         }
348
349         return false;
350 }
351
352 void JSCompositeFilter::getPropertyNames(JSContextRef context,
353                 JSObjectRef object,
354                 JSPropertyNameAccumulatorRef propertyNames)
355 {
356         LogDebug("entered");
357
358         const ScopedJSStringRef jsStrFilters(JSStringCreateWithUTF8CString(ATTRIBUTE_FILTER_ATTR_FILTERS));
359         JSPropertyNameAccumulatorAddName(propertyNames, jsStrFilters.get());
360 }
361
362 JSValueRef JSCompositeFilter::getType(JSContextRef context,
363                 JSObjectRef object,
364                 JSStringRef propertyName,
365                 JSValueRef* exception)
366 {
367         LogDebug("entered");
368         Try
369         {
370                 FilterConverterFactory::ConverterType converter =
371                                 FilterConverterFactory::getConverter(context);
372                 CompositeFilterPtr compositeFilter = getPrivData(object);
373                 return converter->toJSValueRef(compositeFilter->getFilterType());
374         }
375         Catch(WrtDeviceApis::Commons::Exception)
376         {
377                 LogWarning("trying to get incorrect value");
378         }
379         return JSValueMakeUndefined(context);
380 }
381
382 bool JSCompositeFilter::setType(JSContextRef context,
383                 JSObjectRef object,
384                 JSStringRef propertyName,
385                 JSValueRef value,
386                 JSValueRef* exception)
387 {
388         Try
389         {
390                 CompositeFilterPtr compositeFilter = getPrivData(object);
391                 FilterConverterFactory::ConverterType converter =
392                                 FilterConverterFactory::getConverter(context);
393                 compositeFilter->setFilterType(converter->toCompositeFilterType(value));
394                 return true;
395         }
396         Catch(WrtDeviceApis::Commons::Exception)
397         {
398                 LogWarning("trying to set incorrect value");
399         }
400
401         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
402         return false;
403 }
404
405 //JSValueRef JSCompositeFilter::getFilters(JSContextRef context,
406 //              JSObjectRef object,
407 //              JSStringRef propertyName,
408 //              JSValueRef* exception)
409 //{
410 //      LogDebug("entered");
411 //      Try
412 //      {
413 //              FilterConverterFactory::ConverterType converter =
414 //                              FilterConverterFactory::getConverter(context);
415 //              CompositeFilterPtr compositeFilter = getPrivData(object);
416 //              return converter->toJSValueRef(compositeFilter->getFilters());
417 //      }
418 //      Catch(WrtDeviceApis::Commons::Exception)
419 //      {
420 //              LogWarning("trying to get incorrect value");
421 //      }
422 //      return JSValueMakeUndefined(context);
423 //}
424
425 //bool JSCompositeFilter::setFilters(JSContextRef context,
426 //              JSObjectRef object,
427 //              JSStringRef propertyName,
428 //              JSValueRef value,
429 //              JSValueRef* exception)
430 //{
431 //      Try
432 //      {
433 //              CompositeFilterPtr compositeFilter = getPrivData(object);
434 //              FilterConverterFactory::ConverterType converter =
435 //                              FilterConverterFactory::getConverter(context);
436 //              compositeFilter->setFilters(converter->toFilterArray(value));
437 //              return true;
438 //      }
439 //      Catch(WrtDeviceApis::Commons::Exception)
440 //      {
441 //              LogWarning("trying to set incorrect value");
442 //      }
443 //
444 //      JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
445 //      return false;
446 //}
447 //
448 } // Tizen
449 } // DeviceAPI
450