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
25 #include "JSCompositeFilter.h"
27 #include <dpl/shared_ptr.h>
28 #include <CommonsJavaScript/ScopedJSStringRef.h>
30 #include <ArgumentValidator.h>
31 #include <JSWebAPIErrorFactory.h>
34 #define ATTRIBUTE_FILTER_CLASS_NAME "CompositeFilter"
35 #define ATTRIBUTE_FILTER_ATTR_TYPE "type"
36 #define ATTRIBUTE_FILTER_ATTR_FILTERS "filters"
41 using namespace DeviceAPI::Common;
42 using namespace DeviceAPI::Tizen;
43 using namespace WrtDeviceApis::Commons;
44 using namespace WrtDeviceApis::CommonsJavaScript;
46 JSClassRef JSCompositeFilter::m_classRef = NULL;
48 JSClassDefinition JSCompositeFilter::m_classInfo =
51 kJSClassAttributeNone,
52 ATTRIBUTE_FILTER_CLASS_NAME,
61 NULL,//deleteProperty,
62 NULL,//getPropertyNames,
63 NULL, //CallAsFunction,
64 NULL, //CallAsConstructor,
66 NULL, //ConvertToType,
69 JSStaticValue JSCompositeFilter::m_property[] = {
70 { ATTRIBUTE_FILTER_ATTR_TYPE, getType, setType, kJSPropertyAttributeNone },
74 JSStaticFunction JSCompositeFilter::m_functions[] =
79 JSClassRef JSCompositeFilter::getClassRef() {
81 m_classRef = JSClassCreate(&m_classInfo);
86 bool JSCompositeFilter::isObjectOfClass(JSContextRef context, JSValueRef value)
88 return JSValueIsObjectOfClass(context, value, getClassRef());
91 CompositeFilterPtr JSCompositeFilter::getCompositeFilter(JSContextRef context, JSValueRef value)
93 if (!isObjectOfClass(context, value))
94 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
96 JSObjectRef object = JSValueToObject(context, value, NULL);
98 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
100 JSCompositeFilterPriv *priv = static_cast<JSCompositeFilterPriv*>(JSObjectGetPrivate(object));
102 Throw(WrtDeviceApis::Commons::NullPointerException);
104 return priv->getObject();
107 JSObjectRef JSCompositeFilter::createJSObject(JSContextRef context,
108 CompositeFilterPtr privateData,
109 JSValueRef jsValueFilters)
111 JSCompositeFilterPriv *priv = new JSCompositeFilterPriv(context, privateData);
112 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
113 if (NULL == jsObjectRef)
115 LoggerE("object creation error");
119 const ScopedJSStringRef jsStrFilters(JSStringCreateWithUTF8CString(ATTRIBUTE_FILTER_ATTR_FILTERS));
121 JSObjectSetProperty(context, jsObjectRef, jsStrFilters.get(), jsValueFilters, kJSPropertyAttributeNone, NULL);
126 JSObjectRef JSCompositeFilter::constructor(JSContextRef context,
127 JSObjectRef constructor,
128 size_t argumentCount,
129 const JSValueRef arguments[],
130 JSValueRef* exception)
135 JSValueRef jsValueFilters = NULL;
137 ArgumentValidator validator(context, argumentCount, arguments);
141 std::string typeStr = validator.toString(0, false);
143 if(typeStr == "UNION")
145 else if(typeStr == "INTERSECTION")
146 type = INTERSECTION_FILTER;
148 throw TypeMismatchException("Property is not valid CompositeFilterType value");
150 catch(BasePlatformException &e)
152 return JSWebAPIErrorFactory::postException(context, exception, e);
157 if(argumentCount < 2 ||
158 JSValueIsUndefined(context, arguments[1]) ||
159 JSValueIsNull(context, arguments[1]))
160 jsValueFilters = JSCreateArrayObject(context, 0, NULL);
161 else if(JSIsArrayValue(context, arguments[1]))
162 jsValueFilters = arguments[1];
164 throw TypeMismatchException("Property is not valid Array value");
166 catch(BasePlatformException &e)
168 return JSWebAPIErrorFactory::postException(context, exception, e);
171 CompositeFilterPtr compositeFilter(new CompositeFilter(type, FilterArrayPtr(new FilterArray())));
173 JSObjectRef jsobject;
177 jsobject = createJSObject(context, compositeFilter, jsValueFilters);
181 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
182 return JSWebAPIErrorFactory::postException(context, exception,
183 TypeMismatchException("Error occurred while creating object"));
189 void JSCompositeFilter::Initialize(JSContextRef context, JSObjectRef object)
191 if (!JSObjectGetPrivate(object))
193 CompositeFilterPtr filter(new CompositeFilter(UNION_FILTER, FilterArrayPtr(NULL)));
194 JSCompositeFilterPriv *priv = new JSCompositeFilterPriv(context, CompositeFilterPtr(filter));
195 if (!JSObjectSetPrivate(object, priv))
202 void JSCompositeFilter::Finalize(JSObjectRef object)
204 JSCompositeFilterPriv *priv = static_cast<JSCompositeFilterPriv*>(JSObjectGetPrivate(object));
214 CompositeFilterPtr JSCompositeFilter::getPrivData(JSObjectRef object)
217 JSCompositeFilterPriv *priv = static_cast<JSCompositeFilterPriv*>(JSObjectGetPrivate(object));
219 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
221 CompositeFilterPtr result = priv->getObject();
223 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
228 bool JSCompositeFilter::setProperty(JSContextRef context,
230 JSStringRef propertyName,
232 JSValueRef* exception)
238 CompositeFilterPtr compositeFilter = getPrivData(object);
239 std::string name = JSUtil::JSStringToString(context, propertyName);
240 if(name == ATTRIBUTE_FILTER_ATTR_FILTERS)
242 if(!JSIsArrayValue(context, value))
248 catch(BasePlatformException &e)
250 LoggerW("trying to set incorrect value [" << e.getName() << "] " << e.getMessage());
256 JSValueRef JSCompositeFilter::getType(JSContextRef context,
258 JSStringRef propertyName,
259 JSValueRef* exception)
265 CompositeFilterPtr compositeFilter = getPrivData(object);
266 FilterType type = compositeFilter->getFilterType();
268 if(type == UNION_FILTER)
270 else if(type == INTERSECTION_FILTER)
271 typeStr = "INTERSECTION";
275 return JSUtil::toJSValueRef(context, typeStr);
277 catch(BasePlatformException &e)
279 LoggerW("trying to get incorrect value [" << e.getName() << "] " << e.getMessage());
282 return JSValueMakeUndefined(context);
285 bool JSCompositeFilter::setType(JSContextRef context,
287 JSStringRef propertyName,
289 JSValueRef* exception)
295 CompositeFilterPtr compositeFilter = getPrivData(object);
298 std::string typeStr = JSUtil::JSValueToString(context, value);
300 if(typeStr == "UNION")
302 else if(typeStr == "INTERSECTION")
303 type = INTERSECTION_FILTER;
305 throw TypeMismatchException("Property is not valid CompositeFilterType value");
307 compositeFilter->setFilterType(type);
309 catch(BasePlatformException &e)
311 LoggerW("trying to set incorrect value [" << e.getName() << "] " << e.getMessage());