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 "FilterConverter.h"
33 #include "JSAttributeFilter.h"
35 #define ATTRIBUTE_FILTER_CLASS_NAME "AttributeFilter"
37 #define ATTRIBUTE_FILTER_ATTR_ATTRIBUTE_NAME "attributeName"
38 #define ATTRIBUTE_FILTER_ATTR_MATCH_FLAG "matchFlag"
39 #define ATTRIBUTE_FILTER_ATTR_MATCH_VALUE "matchValue"
45 using namespace TizenApis::Commons;
46 using namespace TizenApis::Api::Tizen;
47 using namespace WrtDeviceApis::Commons;
48 using namespace WrtDeviceApis::CommonsJavaScript;
50 JSClassRef JSAttributeFilter::m_classRef = NULL;
52 JSClassDefinition JSAttributeFilter::m_classInfo =
55 kJSClassAttributeNone,
56 ATTRIBUTE_FILTER_CLASS_NAME,
65 NULL, //DeleteProperty,
66 NULL, //getPropertyNames,
67 NULL, //CallAsFunction,
68 constructor, //CallAsConstructor,
70 NULL, //ConvertToType,
73 JSStaticValue JSAttributeFilter::m_property[] = {
74 { ATTRIBUTE_FILTER_ATTR_ATTRIBUTE_NAME, getAttributeName, setAttributeName, kJSPropertyAttributeNone },
75 { ATTRIBUTE_FILTER_ATTR_MATCH_FLAG, getMatchFlag, setMatchFlag, kJSPropertyAttributeNone },
76 { ATTRIBUTE_FILTER_ATTR_MATCH_VALUE, getMatchValue, setMatchValue, kJSPropertyAttributeNone },
80 JSStaticFunction JSAttributeFilter::m_functions[] =
85 JSClassRef JSAttributeFilter::getClassRef() {
87 m_classRef = JSClassCreate(&m_classInfo);
92 bool JSAttributeFilter::isObjectOfClass(JSContextRef context, JSValueRef value)
94 return JSValueIsObjectOfClass(context, value, getClassRef());
97 AttributeFilterPtr JSAttributeFilter::getAttributeFilter(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 JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(JSObjectGetPrivate(object));
108 Throw(WrtDeviceApis::Commons::NullPointerException);
110 return priv->getObject();
113 JSObjectRef JSAttributeFilter::createJSObject(JSContextRef context, AttributeFilterPtr privateData)
115 JSAttributeFilterPriv *priv = new JSAttributeFilterPriv(context, privateData);
116 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
117 if (NULL == jsObjectRef) {
118 LogError("object creation error");
124 void JSAttributeFilter::Initialize(JSContextRef context, JSObjectRef object)
126 if (!JSObjectGetPrivate(object))
128 AttributeFilterPtr filter(new AttributeFilter("", MATCH_NONE, AnyPtr(NULL)));
129 JSAttributeFilterPriv *priv = new JSAttributeFilterPriv(context, AttributeFilterPtr(filter));
130 if (!JSObjectSetPrivate(object, priv)) {
136 void JSAttributeFilter::Finalize(JSObjectRef object)
138 JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(JSObjectGetPrivate(object));
148 AttributeFilterPtr JSAttributeFilter::getPrivData(JSObjectRef object)
151 JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(JSObjectGetPrivate(object));
153 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
155 AttributeFilterPtr result = priv->getObject();
157 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
162 JSObjectRef JSAttributeFilter::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 JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(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(context, 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 (!JSValueIsString(gContext, arguments[1]) && !JSValueIsNull(gContext, arguments[1]))
192 ThrowMsg(InvalidArgumentException, "2nd argument is not string.");
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);
205 std::string attributeName;
210 attributeName = converter->toString(arguments[0]);
212 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
213 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
218 matchFlag = MATCH_EXACTLY;
219 if(argumentCount >= 2)
221 if(JSValueIsString(gContext, arguments[1]))
222 matchFlag = converter->toMatchFlag(arguments[1]);
225 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
226 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
231 if(argumentCount >= 3)
233 if(JSValueIsNull(gContext, arguments[2]))
234 matchValue = AnyPtr(new Any());
236 matchValue = converter->toAny(arguments[2]);
240 matchValue = AnyPtr(new Any());
244 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
245 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
249 AttributeFilterPtr attributeFilter(new AttributeFilter(attributeName, matchFlag, matchValue));
251 JSObjectRef jsobject;
254 jsobject = createJSObject(gContext, attributeFilter);
256 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
257 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
264 JSValueRef JSAttributeFilter::getAttributeName(JSContextRef context,
266 JSStringRef propertyName,
267 JSValueRef* exception)
272 FilterConverterFactory::ConverterType converter =
273 FilterConverterFactory::getConverter(context);
274 AttributeFilterPtr attributeFilter = getPrivData(object);
275 return converter->toJSValueRef(attributeFilter->getAttributeName());
277 Catch(WrtDeviceApis::Commons::Exception)
279 LogWarning("trying to get incorrect value");
281 return JSValueMakeUndefined(context);
284 bool JSAttributeFilter::setAttributeName(JSContextRef context,
286 JSStringRef propertyName,
288 JSValueRef* exception)
292 AttributeFilterPtr attributeFilter = getPrivData(object);
293 FilterConverterFactory::ConverterType converter =
294 FilterConverterFactory::getConverter(context);
295 attributeFilter->setAttributeName(converter->toString(value));
298 Catch(WrtDeviceApis::Commons::Exception)
300 LogWarning("trying to set incorrect value");
303 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
307 JSValueRef JSAttributeFilter::getMatchFlag(JSContextRef context,
309 JSStringRef propertyName,
310 JSValueRef* exception)
315 FilterConverterFactory::ConverterType converter =
316 FilterConverterFactory::getConverter(context);
317 AttributeFilterPtr attributeFilter = getPrivData(object);
318 return converter->toJSValueRef(attributeFilter->getMatchFlag());
320 Catch(WrtDeviceApis::Commons::Exception)
322 LogWarning("trying to get incorrect value");
324 return JSValueMakeUndefined(context);
327 bool JSAttributeFilter::setMatchFlag(JSContextRef context,
329 JSStringRef propertyName,
331 JSValueRef* exception)
335 AttributeFilterPtr attributeFilter = getPrivData(object);
336 FilterConverterFactory::ConverterType converter =
337 FilterConverterFactory::getConverter(context);
338 attributeFilter->setMatchFlag(converter->toMatchFlag(value));
341 Catch(WrtDeviceApis::Commons::Exception)
343 LogWarning("trying to set incorrect value");
346 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
350 JSValueRef JSAttributeFilter::getMatchValue(JSContextRef context,
352 JSStringRef propertyName,
353 JSValueRef* exception)
358 FilterConverterFactory::ConverterType converter =
359 FilterConverterFactory::getConverter(context);
360 AttributeFilterPtr attributeFilter = getPrivData(object);
361 return converter->toJSValueRef(attributeFilter->getMatchValue());
363 Catch(WrtDeviceApis::Commons::Exception)
365 LogWarning("trying to get incorrect value");
368 return JSValueMakeUndefined(context);
371 bool JSAttributeFilter::setMatchValue(JSContextRef context,
373 JSStringRef propertyName,
375 JSValueRef* exception)
379 AttributeFilterPtr attributeFilter = getPrivData(object);
380 FilterConverterFactory::ConverterType converter =
381 FilterConverterFactory::getConverter(context);
382 attributeFilter->setMatchValue(converter->toAny(value));
385 Catch(WrtDeviceApis::Commons::Exception)
387 LogWarning("trying to set incorrect value");
390 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");