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 JSAttributeFilter.cpp
19 * @author Kisub Song (kisubs.song@samsung.com)
21 * @brief Implementation of the JSAttributeFilter 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/AttributeFilter.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 "JSAttributeFilter.h"
36 #define ATTRIBUTE_FILTER_CLASS_NAME "AttributeFilter"
38 #define ATTRIBUTE_FILTER_ATTR_ATTRIBUTE_NAME "attributeName"
39 #define ATTRIBUTE_FILTER_ATTR_MATCH_FLAG "matchFlag"
40 #define ATTRIBUTE_FILTER_ATTR_MATCH_VALUES "matchValues"
46 using namespace TizenApis::Commons;
47 using namespace TizenApis::Api::Tizen;
48 using namespace WrtDeviceApis::Commons;
49 using namespace WrtDeviceApis::CommonsJavaScript;
51 JSClassRef JSAttributeFilter::m_classRef = NULL;
53 JSClassDefinition JSAttributeFilter::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 JSAttributeFilter::m_property[] = {
75 { ATTRIBUTE_FILTER_ATTR_ATTRIBUTE_NAME, getAttributeName, setAttributeName, kJSPropertyAttributeNone },
76 { ATTRIBUTE_FILTER_ATTR_MATCH_FLAG, getMatchFlag, setMatchFlag, kJSPropertyAttributeNone },
77 { ATTRIBUTE_FILTER_ATTR_MATCH_VALUES, getMatchValues, setMatchValues, kJSPropertyAttributeNone },
81 JSStaticFunction JSAttributeFilter::m_functions[] =
86 JSClassRef JSAttributeFilter::getClassRef() {
88 m_classRef = JSClassCreate(&m_classInfo);
93 bool JSAttributeFilter::isObjectOfClass(JSContextRef context, JSValueRef value)
95 return JSValueIsObjectOfClass(context, value, getClassRef());
98 AttributeFilterPtr JSAttributeFilter::getAttributeFilter(JSContextRef context, JSValueRef value)
100 if (!isObjectOfClass(context, value)) {
101 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
103 JSObjectRef object = JSValueToObject(context, value, NULL);
105 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
107 JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(JSObjectGetPrivate(object));
109 Throw(WrtDeviceApis::Commons::NullPointerException);
111 return priv->getObject();
114 void JSAttributeFilter::Initialize(JSContextRef context, JSObjectRef object)
116 // if (!JSObjectGetPrivate(object))
118 // AttributeFilterPtr coord(new AttributeFilter(""));
119 // JSAttributeFilterPriv *priv = new JSAttributeFilterPriv(context, AttributeFilterPtr(coord));
120 // if (!JSObjectSetPrivate(object, priv)) {
126 void JSAttributeFilter::Finalize(JSObjectRef object)
128 JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(JSObjectGetPrivate(object));
138 JSObjectRef JSAttributeFilter::createJSObject(JSContextRef context, AttributeFilterPtr privateData)
140 JSAttributeFilterPriv *priv = new JSAttributeFilterPriv(context, privateData);
141 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
142 if (NULL == jsObjectRef) {
143 LogError("object creation error");
149 AttributeFilterPtr JSAttributeFilter::getPrivData(JSObjectRef object)
152 JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(JSObjectGetPrivate(object));
154 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
156 AttributeFilterPtr result = priv->getObject();
158 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
163 JSObjectRef JSAttributeFilter::constructor(JSContextRef context,
164 JSObjectRef constructor,
165 size_t argumentCount,
166 const JSValueRef arguments[],
167 JSValueRef* exception)
171 // AceSecurityStatus status = CONTACT_CHECK_ACCESS(controller->getContext(), CONTACT_FUNCTION_API_ADD);
172 // TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
174 // JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(JSObjectGetPrivate(constructor));
176 // ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
178 // JSContextRef gContext = priv->getContext();
180 JSContextRef gContext = JSGlobalContextFactory::getInstance()->get();
182 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
184 if (argumentCount < 1 || argumentCount > 3)
185 ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
187 if (!JSValueIsString(gContext, arguments[0]))
188 ThrowMsg(InvalidArgumentException, "1st argument is not string.");
190 if (argumentCount >= 2)
192 if (!JSValueIsString(gContext, arguments[1]) && !JSValueIsNull(gContext, arguments[1]))
193 ThrowMsg(InvalidArgumentException, "2nd argument is not string.");
196 // 3rd argument can be any type.
198 } Catch(Exception ) {
199 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
200 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
204 FilterConverterFactory::ConverterType converter = FilterConverterFactory::getConverter(gContext);
206 std::string attributeName;
208 AnyArrayPtr matchValues;
211 attributeName = converter->toString(arguments[0]);
213 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
214 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
219 matchFlag = MATCH_EXACTLY;
220 if(argumentCount >= 2)
222 if(JSValueIsString(gContext, arguments[1]))
223 matchFlag = converter->toMatchFlag(arguments[1]);
226 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
227 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
232 if(argumentCount >= 3)
234 if(JSValueIsNull(gContext, arguments[2]))
235 matchValues = AnyArrayPtr(new AnyArray());
236 else if(JSIsArrayValue(gContext, arguments[2]))
237 matchValues = converter->toAnyArray(arguments[2]);
240 matchValues = AnyArrayPtr(new AnyArray());
241 matchValues->push_back(converter->toAny(arguments[2]));
246 matchValues = AnyArrayPtr(new AnyArray());
250 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
251 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
255 AttributeFilterPtr attributeFilter(new AttributeFilter(attributeName, matchFlag, matchValues));
257 JSObjectRef jsobject;
260 jsobject = createJSObject(gContext, attributeFilter);
262 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
263 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
270 JSValueRef JSAttributeFilter::getAttributeName(JSContextRef context,
272 JSStringRef propertyName,
273 JSValueRef* exception)
278 FilterConverterFactory::ConverterType converter =
279 FilterConverterFactory::getConverter(context);
280 AttributeFilterPtr attributeFilter = getPrivData(object);
281 return converter->toJSValueRef(attributeFilter->getAttributeName());
283 Catch(WrtDeviceApis::Commons::Exception)
285 LogWarning("trying to get incorrect value");
287 return JSValueMakeUndefined(context);
290 bool JSAttributeFilter::setAttributeName(JSContextRef context,
292 JSStringRef propertyName,
294 JSValueRef* exception)
298 AttributeFilterPtr attributeFilter = getPrivData(object);
299 FilterConverterFactory::ConverterType converter =
300 FilterConverterFactory::getConverter(context);
301 attributeFilter->setAttributeName(converter->toString(value));
304 Catch(WrtDeviceApis::Commons::Exception)
306 LogWarning("trying to set incorrect value");
309 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
313 JSValueRef JSAttributeFilter::getMatchFlag(JSContextRef context,
315 JSStringRef propertyName,
316 JSValueRef* exception)
321 FilterConverterFactory::ConverterType converter =
322 FilterConverterFactory::getConverter(context);
323 AttributeFilterPtr attributeFilter = getPrivData(object);
324 return converter->toJSValueRef(attributeFilter->getMatchFlag());
326 Catch(WrtDeviceApis::Commons::Exception)
328 LogWarning("trying to get incorrect value");
330 return JSValueMakeUndefined(context);
333 bool JSAttributeFilter::setMatchFlag(JSContextRef context,
335 JSStringRef propertyName,
337 JSValueRef* exception)
341 AttributeFilterPtr attributeFilter = getPrivData(object);
342 FilterConverterFactory::ConverterType converter =
343 FilterConverterFactory::getConverter(context);
344 attributeFilter->setMatchFlag(converter->toMatchFlag(value));
347 Catch(WrtDeviceApis::Commons::Exception)
349 LogWarning("trying to set incorrect value");
352 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
356 JSValueRef JSAttributeFilter::getMatchValues(JSContextRef context,
358 JSStringRef propertyName,
359 JSValueRef* exception)
364 FilterConverterFactory::ConverterType converter =
365 FilterConverterFactory::getConverter(context);
366 AttributeFilterPtr attributeFilter = getPrivData(object);
367 return converter->toJSValueRef(attributeFilter->getMatchValues());
369 Catch(WrtDeviceApis::Commons::Exception)
371 LogWarning("trying to get incorrect value");
374 return JSValueMakeUndefined(context);
377 bool JSAttributeFilter::setMatchValues(JSContextRef context,
379 JSStringRef propertyName,
381 JSValueRef* exception)
385 AttributeFilterPtr attributeFilter = getPrivData(object);
386 FilterConverterFactory::ConverterType converter =
387 FilterConverterFactory::getConverter(context);
388 attributeFilter->setMatchValues(converter->toAnyArray(value));
391 Catch(WrtDeviceApis::Commons::Exception)
393 LogWarning("trying to set incorrect value");
396 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");