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 <JSTizenExceptionFactory.h>
31 #include <JSTizenException.h>
32 #include "CompositeFilter.h"
33 #include "FilterConverter.h"
34 #include "JSAbstractFilterArray.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 constructor, //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");
122 void JSCompositeFilter::Initialize(JSContextRef context, JSObjectRef object)
124 if (!JSObjectGetPrivate(object))
126 CompositeFilterPtr filter(new CompositeFilter(UNION_FILTER, FilterArrayPtr(NULL)));
127 JSCompositeFilterPriv *priv = new JSCompositeFilterPriv(context, CompositeFilterPtr(filter));
128 if (!JSObjectSetPrivate(object, priv)) {
134 void JSCompositeFilter::Finalize(JSObjectRef object)
136 JSCompositeFilterPriv *priv = static_cast<JSCompositeFilterPriv*>(JSObjectGetPrivate(object));
146 CompositeFilterPtr JSCompositeFilter::getPrivData(JSObjectRef object)
149 JSCompositeFilterPriv *priv = static_cast<JSCompositeFilterPriv*>(JSObjectGetPrivate(object));
151 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
153 CompositeFilterPtr result = priv->getObject();
155 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
160 JSObjectRef JSCompositeFilter::constructor(JSContextRef context,
161 JSObjectRef constructor,
162 size_t argumentCount,
163 const JSValueRef arguments[],
164 JSValueRef* exception)
168 bool js2ndParamIsObject = false;
170 // AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_ADD);
171 // TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
173 JSCompositeFilterPriv *priv = static_cast<JSCompositeFilterPriv*>(JSObjectGetPrivate(constructor));
175 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
177 JSContextRef gContext = priv->getContext();
179 // JSContextRef gContext = JSGlobalContextFactory::getInstance()->get();
181 BasicValidator validator = BasicValidatorFactory::getValidator(gContext, exception);
183 if (argumentCount < 1)
184 ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
186 if (!JSValueIsString(gContext, arguments[0]))
187 ThrowMsg(InvalidArgumentException, "1st argument is not string.");
189 if (argumentCount >= 2)
191 if (JSValueIsObject(gContext, arguments[1]))
192 js2ndParamIsObject = true;
194 if (!js2ndParamIsObject &&
195 !JSValueIsNull(gContext, arguments[1]) &&
196 !JSValueIsUndefined(gContext, arguments[1]))
197 ThrowMsg(InvalidArgumentException, "2nd argument is not array.");
200 } Catch(Exception ) {
201 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
202 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
206 FilterConverterFactory::ConverterType converter = FilterConverterFactory::getConverter(gContext);
209 FilterArrayPtr filters(NULL);
212 type = converter->toCompositeFilterType(arguments[0]);
214 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
215 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
220 if(js2ndParamIsObject)
221 filters = converter->toFilterArray(arguments[1]);
223 filters = FilterArrayPtr(new FilterArray());
226 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
227 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
231 CompositeFilterPtr compositeFilter(new CompositeFilter(type, filters));
233 JSObjectRef jsobject;
236 jsobject = createJSObject(gContext, compositeFilter);
238 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
239 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
246 JSValueRef JSCompositeFilter::getType(JSContextRef context,
248 JSStringRef propertyName,
249 JSValueRef* exception)
254 FilterConverterFactory::ConverterType converter =
255 FilterConverterFactory::getConverter(context);
256 CompositeFilterPtr compositeFilter = getPrivData(object);
257 return converter->toJSValueRef(compositeFilter->getFilterType());
259 Catch(WrtDeviceApis::Commons::Exception)
261 LogWarning("trying to get incorrect value");
263 return JSValueMakeUndefined(context);
266 bool JSCompositeFilter::setType(JSContextRef context,
268 JSStringRef propertyName,
270 JSValueRef* exception)
274 CompositeFilterPtr compositeFilter = getPrivData(object);
275 FilterConverterFactory::ConverterType converter =
276 FilterConverterFactory::getConverter(context);
277 compositeFilter->setFilterType(converter->toCompositeFilterType(value));
280 Catch(WrtDeviceApis::Commons::Exception)
282 LogWarning("trying to set incorrect value");
285 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
289 JSValueRef JSCompositeFilter::getFilters(JSContextRef context,
291 JSStringRef propertyName,
292 JSValueRef* exception)
297 FilterConverterFactory::ConverterType converter =
298 FilterConverterFactory::getConverter(context);
299 CompositeFilterPtr compositeFilter = getPrivData(object);
300 return converter->toJSValueRef(compositeFilter->getFilters());
302 Catch(WrtDeviceApis::Commons::Exception)
304 LogWarning("trying to get incorrect value");
306 return JSValueMakeUndefined(context);
309 bool JSCompositeFilter::setFilters(JSContextRef context,
311 JSStringRef propertyName,
313 JSValueRef* exception)
317 CompositeFilterPtr compositeFilter = getPrivData(object);
318 FilterConverterFactory::ConverterType converter =
319 FilterConverterFactory::getConverter(context);
320 compositeFilter->setFilters(converter->toFilterArray(value));
323 Catch(WrtDeviceApis::Commons::Exception)
325 LogWarning("trying to set incorrect value");
328 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");