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 LoggerW("Failed to convert 1st parameter to filter type. Using default value.");
158 if(argumentCount < 2 ||
159 JSValueIsUndefined(context, arguments[1]) ||
160 JSValueIsNull(context, arguments[1]))
161 jsValueFilters = JSCreateArrayObject(context, 0, NULL);
162 else if(JSIsArrayValue(context, arguments[1]))
163 jsValueFilters = arguments[1];
165 throw TypeMismatchException("Property is not valid Array value");
167 catch(BasePlatformException &e)
169 LoggerW("Failed to convert 2nd parameter to filter array. Using default value.");
170 jsValueFilters = JSCreateArrayObject(context, 0, NULL);
173 CompositeFilterPtr compositeFilter(new CompositeFilter(type, FilterArrayPtr(new FilterArray())));
175 JSObjectRef jsobject = NULL;
179 jsobject = createJSObject(context, compositeFilter, jsValueFilters);
183 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
187 return JSObjectMake(context, NULL, NULL);
192 void JSCompositeFilter::Initialize(JSContextRef context, JSObjectRef object)
194 if (!JSObjectGetPrivate(object))
196 CompositeFilterPtr filter(new CompositeFilter(UNION_FILTER, FilterArrayPtr(NULL)));
197 JSCompositeFilterPriv *priv = new JSCompositeFilterPriv(context, CompositeFilterPtr(filter));
198 if (!JSObjectSetPrivate(object, priv))
205 void JSCompositeFilter::Finalize(JSObjectRef object)
207 JSCompositeFilterPriv *priv = static_cast<JSCompositeFilterPriv*>(JSObjectGetPrivate(object));
217 CompositeFilterPtr JSCompositeFilter::getPrivData(JSObjectRef object)
220 JSCompositeFilterPriv *priv = static_cast<JSCompositeFilterPriv*>(JSObjectGetPrivate(object));
222 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
224 CompositeFilterPtr result = priv->getObject();
226 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
231 bool JSCompositeFilter::setProperty(JSContextRef context,
233 JSStringRef propertyName,
235 JSValueRef* exception)
241 CompositeFilterPtr compositeFilter = getPrivData(object);
242 std::string name = JSUtil::JSStringToString(context, propertyName);
243 if(name == ATTRIBUTE_FILTER_ATTR_FILTERS)
245 if(!JSIsArrayValue(context, value))
251 catch(BasePlatformException &e)
253 LoggerW("trying to set incorrect value [" << e.getName() << "] " << e.getMessage());
259 JSValueRef JSCompositeFilter::getType(JSContextRef context,
261 JSStringRef propertyName,
262 JSValueRef* exception)
268 CompositeFilterPtr compositeFilter = getPrivData(object);
269 FilterType type = compositeFilter->getFilterType();
271 if(type == UNION_FILTER)
273 else if(type == INTERSECTION_FILTER)
274 typeStr = "INTERSECTION";
278 return JSUtil::toJSValueRef(context, typeStr);
280 catch(BasePlatformException &e)
282 LoggerW("trying to get incorrect value [" << e.getName() << "] " << e.getMessage());
285 return JSValueMakeUndefined(context);
288 bool JSCompositeFilter::setType(JSContextRef context,
290 JSStringRef propertyName,
292 JSValueRef* exception)
298 CompositeFilterPtr compositeFilter = getPrivData(object);
301 std::string typeStr = JSUtil::JSValueToString(context, value);
303 if(typeStr == "UNION")
305 else if(typeStr == "INTERSECTION")
306 type = INTERSECTION_FILTER;
308 throw TypeMismatchException("Property is not valid CompositeFilterType value");
310 compositeFilter->setFilterType(type);
312 catch(BasePlatformException &e)
314 LoggerW("trying to set incorrect value [" << e.getName() << "] " << e.getMessage());