2 // Tizen Web Device API
3 // Copyright (c) 2012 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.
19 * @file JSCompositeFilter.cpp
20 * @author Kisub Song (kisubs.song@samsung.com)
22 * @brief Implementation of the JSCompositeFilter class
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"
37 #define ATTRIBUTE_FILTER_CLASS_NAME "CompositeFilter"
39 #define ATTRIBUTE_FILTER_ATTR_TYPE "type"
40 #define ATTRIBUTE_FILTER_ATTR_FILTERS "filters"
42 namespace DeviceAPI {
\rnamespace Tizen {
44 using namespace DeviceAPI::Common;
45 using namespace DeviceAPI::Tizen;
46 using namespace WrtDeviceApis::Commons;
47 using namespace WrtDeviceApis::CommonsJavaScript;
49 JSClassRef JSCompositeFilter::m_classRef = NULL;
51 JSClassDefinition JSCompositeFilter::m_classInfo =
54 kJSClassAttributeNone,
55 ATTRIBUTE_FILTER_CLASS_NAME,
64 NULL,//deleteProperty,
65 NULL,//getPropertyNames,
66 NULL, //CallAsFunction,
67 NULL, //CallAsConstructor,
69 NULL, //ConvertToType,
72 JSStaticValue JSCompositeFilter::m_property[] = {
73 { ATTRIBUTE_FILTER_ATTR_TYPE, getType, setType, kJSPropertyAttributeNone },
74 //{ ATTRIBUTE_FILTER_ATTR_FILTERS, getFilters, setFilters, kJSPropertyAttributeNone },
78 JSStaticFunction JSCompositeFilter::m_functions[] =
83 JSClassRef JSCompositeFilter::getClassRef() {
85 m_classRef = JSClassCreate(&m_classInfo);
90 bool JSCompositeFilter::isObjectOfClass(JSContextRef context, JSValueRef value)
92 return JSValueIsObjectOfClass(context, value, getClassRef());
95 CompositeFilterPtr JSCompositeFilter::getCompositeFilter(JSContextRef context, JSValueRef value)
97 if (!isObjectOfClass(context, value)) {
98 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
100 JSObjectRef object = JSValueToObject(context, value, NULL);
102 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
104 JSCompositeFilterPriv *priv = static_cast<JSCompositeFilterPriv*>(JSObjectGetPrivate(object));
106 Throw(WrtDeviceApis::Commons::NullPointerException);
108 return priv->getObject();
111 JSObjectRef JSCompositeFilter::createJSObject(JSContextRef context, CompositeFilterPtr privateData)
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");
120 FilterConverterFactory::ConverterType converter = FilterConverterFactory::getConverter(context);
122 const ScopedJSStringRef jsStrFilters(JSStringCreateWithUTF8CString(ATTRIBUTE_FILTER_ATTR_FILTERS));
123 JSValueRef jsValueFilters = converter->toJSValueRef(privateData->getFilters());
125 JSObjectSetProperty(context, jsObjectRef, jsStrFilters.get(), jsValueFilters, kJSPropertyAttributeNone, NULL);
130 JSObjectRef JSCompositeFilter::constructor(JSContextRef context,
131 JSObjectRef constructor,
132 size_t argumentCount,
133 const JSValueRef arguments[],
134 JSValueRef* exception)
138 bool js2ndParamIsObject = false;
140 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
142 if (argumentCount < 1)
143 ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
145 if (!JSValueIsString(context, arguments[0]))
146 ThrowMsg(InvalidArgumentException, "1st argument is not string.");
148 if (argumentCount >= 2)
150 if (JSValueIsObject(context, arguments[1]))
151 js2ndParamIsObject = true;
153 if (!js2ndParamIsObject &&
154 !JSValueIsNull(context, arguments[1]) &&
155 !JSValueIsUndefined(context, arguments[1]))
156 ThrowMsg(InvalidArgumentException, "2nd argument is not array.");
159 } Catch(Exception ) {
160 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
161 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
165 FilterConverterFactory::ConverterType converter = FilterConverterFactory::getConverter(context);
168 FilterArrayPtr filters(NULL);
171 type = converter->toCompositeFilterType(arguments[0]);
173 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
174 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
179 if(js2ndParamIsObject)
180 filters = converter->toFilterArray(arguments[1]);
182 filters = FilterArrayPtr(new FilterArray());
185 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
186 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
190 CompositeFilterPtr compositeFilter(new CompositeFilter(type, filters));
192 JSObjectRef jsobject;
195 jsobject = createJSObject(context, compositeFilter);
197 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
198 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
205 void JSCompositeFilter::Initialize(JSContextRef context, JSObjectRef object)
207 if (!JSObjectGetPrivate(object))
209 CompositeFilterPtr filter(new CompositeFilter(UNION_FILTER, FilterArrayPtr(NULL)));
210 JSCompositeFilterPriv *priv = new JSCompositeFilterPriv(context, CompositeFilterPtr(filter));
211 if (!JSObjectSetPrivate(object, priv)) {
217 void JSCompositeFilter::Finalize(JSObjectRef object)
219 JSCompositeFilterPriv *priv = static_cast<JSCompositeFilterPriv*>(JSObjectGetPrivate(object));
229 CompositeFilterPtr JSCompositeFilter::getPrivData(JSObjectRef object)
232 JSCompositeFilterPriv *priv = static_cast<JSCompositeFilterPriv*>(JSObjectGetPrivate(object));
234 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
236 CompositeFilterPtr result = priv->getObject();
238 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
243 bool JSCompositeFilter::hasProperty(JSContextRef context,
245 JSStringRef propertyName)
250 CompositeFilterPtr compositeFilter = getPrivData(object);
252 FilterConverterFactory::ConverterType converter =
253 FilterConverterFactory::getConverter(context);
254 std::string name = converter->toString(propertyName);
256 if(name == ATTRIBUTE_FILTER_ATTR_FILTERS ||
257 name == ATTRIBUTE_FILTER_ATTR_TYPE )
260 Catch(WrtDeviceApis::Commons::Exception)
262 LogWarning("trying to get incorrect value");
269 JSValueRef JSCompositeFilter::getProperty(JSContextRef context,
271 JSStringRef propertyName,
272 JSValueRef* exception)
277 CompositeFilterPtr compositeFilter = getPrivData(object);
279 FilterConverterFactory::ConverterType converter =
280 FilterConverterFactory::getConverter(context);
281 std::string name = converter->toString(propertyName);
282 if(name == ATTRIBUTE_FILTER_ATTR_FILTERS)
285 Catch(WrtDeviceApis::Commons::Exception)
287 LogWarning("trying to get incorrect value");
290 return JSValueMakeUndefined(context);
293 bool JSCompositeFilter::setProperty(JSContextRef context,
295 JSStringRef propertyName,
297 JSValueRef* exception)
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)
308 if(!JSIsArrayValue(context, value))
312 // const ScopedJSStringRef jsStrFilters(
313 // JSStringCreateWithUTF8CString(ATTRIBUTE_FILTER_ATTR_FILTERS));
315 // JSObjectSetProperty(context, object, jsStrFilters.get(),
316 // value, kJSPropertyAttributeNone, NULL);
321 Catch(WrtDeviceApis::Commons::Exception)
323 LogWarning("trying to set incorrect value");
329 bool JSCompositeFilter::deleteProperty(JSContextRef context,
331 JSStringRef propertyName,
332 JSValueRef* exception)
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)
344 Catch(WrtDeviceApis::Commons::Exception)
346 LogWarning("trying to set incorrect value");
352 void JSCompositeFilter::getPropertyNames(JSContextRef context,
354 JSPropertyNameAccumulatorRef propertyNames)
358 const ScopedJSStringRef jsStrFilters(JSStringCreateWithUTF8CString(ATTRIBUTE_FILTER_ATTR_FILTERS));
359 JSPropertyNameAccumulatorAddName(propertyNames, jsStrFilters.get());
362 JSValueRef JSCompositeFilter::getType(JSContextRef context,
364 JSStringRef propertyName,
365 JSValueRef* exception)
370 FilterConverterFactory::ConverterType converter =
371 FilterConverterFactory::getConverter(context);
372 CompositeFilterPtr compositeFilter = getPrivData(object);
373 return converter->toJSValueRef(compositeFilter->getFilterType());
375 Catch(WrtDeviceApis::Commons::Exception)
377 LogWarning("trying to get incorrect value");
379 return JSValueMakeUndefined(context);
382 bool JSCompositeFilter::setType(JSContextRef context,
384 JSStringRef propertyName,
386 JSValueRef* exception)
390 CompositeFilterPtr compositeFilter = getPrivData(object);
391 FilterConverterFactory::ConverterType converter =
392 FilterConverterFactory::getConverter(context);
393 compositeFilter->setFilterType(converter->toCompositeFilterType(value));
396 Catch(WrtDeviceApis::Commons::Exception)
398 LogWarning("trying to set incorrect value");
401 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
405 //JSValueRef JSCompositeFilter::getFilters(JSContextRef context,
406 // JSObjectRef object,
407 // JSStringRef propertyName,
408 // JSValueRef* exception)
410 // LogDebug("entered");
413 // FilterConverterFactory::ConverterType converter =
414 // FilterConverterFactory::getConverter(context);
415 // CompositeFilterPtr compositeFilter = getPrivData(object);
416 // return converter->toJSValueRef(compositeFilter->getFilters());
418 // Catch(WrtDeviceApis::Commons::Exception)
420 // LogWarning("trying to get incorrect value");
422 // return JSValueMakeUndefined(context);
425 //bool JSCompositeFilter::setFilters(JSContextRef context,
426 // JSObjectRef object,
427 // JSStringRef propertyName,
429 // JSValueRef* exception)
433 // CompositeFilterPtr compositeFilter = getPrivData(object);
434 // FilterConverterFactory::ConverterType converter =
435 // FilterConverterFactory::getConverter(context);
436 // compositeFilter->setFilters(converter->toFilterArray(value));
439 // Catch(WrtDeviceApis::Commons::Exception)
441 // LogWarning("trying to set incorrect value");
444 // JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");