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 JSAttributeRangeFilter.cpp
20 * @author Kisub Song (kisubs.song@samsung.com)
22 * @brief Implementation of the JSAttributeRangeFilter class
25 #include "JSAttributeRangeFilter.h"
26 #include <dpl/shared_ptr.h>
27 #include <CommonsJavaScript/ScopedJSStringRef.h>
29 #include <ArgumentValidator.h>
30 #include <JSWebAPIErrorFactory.h>
31 #include "AnyFactory.h"
34 #define ATTRIBUTE_RANGE_FILTER_CLASS_NAME "AttributeRangeFilter"
36 #define ATTRIBUTE_FILTER_ATTR_ATTRIBUTE_NAME "attributeName"
37 #define ATTRIBUTE_FILTER_ATTR_INITIAL_VALUE "initialValue"
38 #define ATTRIBUTE_FILTER_ATTR_END_VALUE "endValue"
43 using namespace DeviceAPI::Common;
44 using namespace DeviceAPI::Tizen;
45 using namespace WrtDeviceApis::Commons;
46 using namespace WrtDeviceApis::CommonsJavaScript;
48 JSClassRef JSAttributeRangeFilter::m_classRef = NULL;
50 JSClassDefinition JSAttributeRangeFilter::m_classInfo =
53 kJSClassAttributeNone,
54 ATTRIBUTE_RANGE_FILTER_CLASS_NAME,
63 NULL, //DeleteProperty,
64 NULL, //getPropertyNames,
65 NULL, //CallAsFunction,
66 NULL, //CallAsConstructor,
68 NULL, //ConvertToType,
71 JSStaticValue JSAttributeRangeFilter::m_property[] = {
72 { ATTRIBUTE_FILTER_ATTR_ATTRIBUTE_NAME, getAttributeName, setAttributeName, kJSPropertyAttributeNone },
76 JSStaticFunction JSAttributeRangeFilter::m_functions[] =
81 JSClassRef JSAttributeRangeFilter::getClassRef() {
83 m_classRef = JSClassCreate(&m_classInfo);
88 bool JSAttributeRangeFilter::isObjectOfClass(JSContextRef context, JSValueRef value)
90 return JSValueIsObjectOfClass(context, value, getClassRef());
93 AttributeRangeFilterPtr JSAttributeRangeFilter::getAttributeRangeFilter(JSContextRef context, JSValueRef value)
95 if (!isObjectOfClass(context, value))
96 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
98 JSObjectRef object = JSValueToObject(context, value, NULL);
100 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
102 JSAttributeRangeFilterPriv *priv = static_cast<JSAttributeRangeFilterPriv*>(JSObjectGetPrivate(object));
104 Throw(WrtDeviceApis::Commons::NullPointerException);
106 return priv->getObject();
109 JSObjectRef JSAttributeRangeFilter::createJSObject(JSContextRef context,
110 AttributeRangeFilterPtr privateData,
111 JSValueRef jsValueInitialValue,
112 JSValueRef jsValueEndValue)
114 JSAttributeRangeFilterPriv *priv = new JSAttributeRangeFilterPriv(context, privateData);
115 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
116 if (NULL == jsObjectRef)
118 LoggerE("object creation error");
122 const ScopedJSStringRef jsStrInitialValue(JSStringCreateWithUTF8CString(ATTRIBUTE_FILTER_ATTR_INITIAL_VALUE));
123 const ScopedJSStringRef jsStrEndValue(JSStringCreateWithUTF8CString(ATTRIBUTE_FILTER_ATTR_END_VALUE));
125 JSObjectSetProperty(context, jsObjectRef, jsStrInitialValue.get(), jsValueInitialValue, kJSPropertyAttributeNone, NULL);
126 JSObjectSetProperty(context, jsObjectRef, jsStrEndValue.get(), jsValueEndValue, kJSPropertyAttributeNone, NULL);
131 JSObjectRef JSAttributeRangeFilter::constructor(JSContextRef context,
132 JSObjectRef constructor,
133 size_t argumentCount,
134 const JSValueRef arguments[],
135 JSValueRef* exception)
139 std::string attributeName;
140 AnyPtr initialValue(NULL);
141 JSValueRef jsValueInitialValue = NULL;
142 AnyPtr endValue(NULL);
143 JSValueRef jsValueEndValue = NULL;
145 ArgumentValidator validator(context, argumentCount, arguments);
149 attributeName = validator.toString(0, false);
151 catch(BasePlatformException &e)
153 return JSWebAPIErrorFactory::postException(context, exception, e);
156 initialValue = AnyFactory::createAnyEmpty(context);
158 if(argumentCount >= 2)
159 jsValueInitialValue = arguments[1];
161 jsValueInitialValue = JSValueMakeUndefined(context);
163 endValue = AnyFactory::createAnyEmpty(context);
165 if(argumentCount >= 3)
166 jsValueEndValue = arguments[2];
168 jsValueEndValue = JSValueMakeUndefined(context);
170 AttributeRangeFilterPtr attributeRangeFilter(new AttributeRangeFilter(attributeName, initialValue, endValue));
172 JSObjectRef jsobject;
176 jsobject = createJSObject(context, attributeRangeFilter, jsValueInitialValue, jsValueEndValue);
180 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
181 return JSWebAPIErrorFactory::postException(context, exception,
182 TypeMismatchException("Error occurred while creating object"));
188 void JSAttributeRangeFilter::Initialize(JSContextRef context, JSObjectRef object)
190 if (!JSObjectGetPrivate(object))
192 AttributeRangeFilterPtr filter(new AttributeRangeFilter("", AnyPtr(NULL), AnyPtr(NULL)));
193 JSAttributeRangeFilterPriv *priv =
194 new JSAttributeRangeFilterPriv(context, AttributeRangeFilterPtr(filter));
195 if (!JSObjectSetPrivate(object, priv))
202 void JSAttributeRangeFilter::Finalize(JSObjectRef object)
204 JSAttributeRangeFilterPriv *priv = static_cast<JSAttributeRangeFilterPriv*>(JSObjectGetPrivate(object));
214 AttributeRangeFilterPtr JSAttributeRangeFilter::getPrivData(JSObjectRef object)
217 JSAttributeRangeFilterPriv *priv = static_cast<JSAttributeRangeFilterPriv*>(JSObjectGetPrivate(object));
219 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
221 AttributeRangeFilterPtr result = priv->getObject();
223 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
228 JSValueRef JSAttributeRangeFilter::getAttributeName(JSContextRef context,
230 JSStringRef propertyName,
231 JSValueRef* exception)
237 AttributeRangeFilterPtr attributeRangeFilter = getPrivData(object);
238 return JSUtil::toJSValueRef(context, attributeRangeFilter->getAttributeName());
240 catch(BasePlatformException &e)
242 LoggerW("trying to get incorrect value [" << e.getName() << "] " << e.getMessage());
245 return JSValueMakeUndefined(context);
248 bool JSAttributeRangeFilter::setAttributeName(JSContextRef context,
250 JSStringRef propertyName,
252 JSValueRef* exception)
258 AttributeRangeFilterPtr attributeRangeFilter = getPrivData(object);
259 attributeRangeFilter->setAttributeName(JSUtil::JSValueToString(context, value));
261 catch(BasePlatformException &e)
263 LoggerW("trying to set incorrect value [" << e.getName() << "] " << e.getMessage());