2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @file JSCompositeFilter.cpp
19 * @author Kisub Song (kisubs.song@samsung.com)
21 * @brief Implementation of the JSCompositeFilter class
25 #include <dpl/log/log.h>
26 #include <dpl/shared_ptr.h>
27 #include <CommonsJavaScript/Converter.h>
28 #include <CommonsJavaScript/Validator.h>
29 #include <API/Filter/CompositeFilter.h>
30 #include <Tizen/Common/JSTizenExceptionFactory.h>
31 #include <Tizen/Common/JSTizenException.h>
32 #include <Tizen/Common/JSGlobalContextFactory.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"
46 using namespace TizenApis::Commons;
47 using namespace TizenApis::Api::Tizen;
48 using namespace WrtDeviceApis::Commons;
49 using namespace WrtDeviceApis::CommonsJavaScript;
51 JSClassRef JSCompositeFilter::m_classRef = NULL;
53 JSClassDefinition JSCompositeFilter::m_classInfo =
56 kJSClassAttributeNone,
57 ATTRIBUTE_FILTER_CLASS_NAME,
66 NULL, //DeleteProperty,
67 NULL, //getPropertyNames,
68 NULL, //CallAsFunction,
69 constructor, //CallAsConstructor,
71 NULL, //ConvertToType,
74 JSStaticValue JSCompositeFilter::m_property[] = {
75 { ATTRIBUTE_FILTER_ATTR_TYPE, getType, setType, kJSPropertyAttributeNone },
76 { ATTRIBUTE_FILTER_ATTR_FILTERS, getFilters, setFilters, kJSPropertyAttributeNone },
80 JSStaticFunction JSCompositeFilter::m_functions[] =
85 JSClassRef JSCompositeFilter::getClassRef() {
87 m_classRef = JSClassCreate(&m_classInfo);
92 bool JSCompositeFilter::isObjectOfClass(JSContextRef context, JSValueRef value)
94 return JSValueIsObjectOfClass(context, value, getClassRef());
97 CompositeFilterPtr JSCompositeFilter::getCompositeFilter(JSContextRef context, JSValueRef value)
99 if (!isObjectOfClass(context, value)) {
100 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
102 JSObjectRef object = JSValueToObject(context, value, NULL);
104 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
106 JSCompositeFilterPriv *priv = static_cast<JSCompositeFilterPriv*>(JSObjectGetPrivate(object));
108 Throw(WrtDeviceApis::Commons::NullPointerException);
110 return priv->getObject();
113 void JSCompositeFilter::Initialize(JSContextRef context, JSObjectRef object)
115 // if (!JSObjectGetPrivate(object))
117 // CompositeFilterPtr coord(new CompositeFilter(""));
118 // JSCompositeFilterPriv *priv = new JSCompositeFilterPriv(context, CompositeFilterPtr(coord));
119 // if (!JSObjectSetPrivate(object, priv)) {
125 void JSCompositeFilter::Finalize(JSObjectRef object)
127 JSCompositeFilterPriv *priv = static_cast<JSCompositeFilterPriv*>(JSObjectGetPrivate(object));
137 JSObjectRef JSCompositeFilter::createJSObject(JSContextRef context, CompositeFilterPtr privateData)
139 JSCompositeFilterPriv *priv = new JSCompositeFilterPriv(context, privateData);
140 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
141 if (NULL == jsObjectRef) {
142 LogError("object creation error");
148 CompositeFilterPtr JSCompositeFilter::getPrivData(JSObjectRef object)
151 JSCompositeFilterPriv *priv = static_cast<JSCompositeFilterPriv*>(JSObjectGetPrivate(object));
153 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
155 CompositeFilterPtr result = priv->getObject();
157 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
162 JSObjectRef JSCompositeFilter::constructor(JSContextRef context,
163 JSObjectRef constructor,
164 size_t argumentCount,
165 const JSValueRef arguments[],
166 JSValueRef* exception)
170 // AceSecurityStatus status = CONTACT_CHECK_ACCESS(controller->getContext(), 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 || argumentCount > 3)
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]) && !JSValueIsNull(gContext, arguments[1]))
192 ThrowMsg(InvalidArgumentException, "2nd argument is not object.");
195 // 3rd argument can be any type.
197 } Catch(Exception ) {
198 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
199 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
203 FilterConverterFactory::ConverterType converter = FilterConverterFactory::getConverter(gContext);
206 FilterArrayPtr filters;
209 type = converter->toCompositeFilterType(arguments[0]);
211 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
212 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
217 if(argumentCount >= 2)
219 if(!JSValueIsNull(gContext, arguments[1]))
220 filters = converter->toFilterArray(arguments[1]);
222 filters = FilterArrayPtr(new FilterArray());
225 filters = FilterArrayPtr(new FilterArray());
228 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
229 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
233 CompositeFilterPtr compositeFilter(new CompositeFilter(type, filters));
235 JSObjectRef jsobject;
238 jsobject = createJSObject(gContext, compositeFilter);
240 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
241 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
248 JSValueRef JSCompositeFilter::getType(JSContextRef context,
250 JSStringRef propertyName,
251 JSValueRef* exception)
256 FilterConverterFactory::ConverterType converter =
257 FilterConverterFactory::getConverter(context);
258 CompositeFilterPtr compositeFilter = getPrivData(object);
259 return converter->toJSValueRef(compositeFilter->getFilterType());
261 Catch(WrtDeviceApis::Commons::Exception)
263 LogWarning("trying to get incorrect value");
265 return JSValueMakeUndefined(context);
268 bool JSCompositeFilter::setType(JSContextRef context,
270 JSStringRef propertyName,
272 JSValueRef* exception)
276 CompositeFilterPtr compositeFilter = getPrivData(object);
277 FilterConverterFactory::ConverterType converter =
278 FilterConverterFactory::getConverter(context);
279 compositeFilter->setFilterType(converter->toCompositeFilterType(value));
282 Catch(WrtDeviceApis::Commons::Exception)
284 LogWarning("trying to set incorrect value");
287 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
291 JSValueRef JSCompositeFilter::getFilters(JSContextRef context,
293 JSStringRef propertyName,
294 JSValueRef* exception)
299 FilterConverterFactory::ConverterType converter =
300 FilterConverterFactory::getConverter(context);
301 CompositeFilterPtr compositeFilter = getPrivData(object);
302 return converter->toJSValueRef(compositeFilter->getFilters());
304 Catch(WrtDeviceApis::Commons::Exception)
306 LogWarning("trying to get incorrect value");
308 return JSValueMakeUndefined(context);
311 bool JSCompositeFilter::setFilters(JSContextRef context,
313 JSStringRef propertyName,
315 JSValueRef* exception)
319 CompositeFilterPtr compositeFilter = getPrivData(object);
320 FilterConverterFactory::ConverterType converter =
321 FilterConverterFactory::getConverter(context);
322 compositeFilter->setFilters(converter->toFilterArray(value));
325 Catch(WrtDeviceApis::Commons::Exception)
327 LogWarning("trying to set incorrect value");
330 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");