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 JSAttributeFilter.cpp
20 * @author Kisub Song (kisubs.song@samsung.com)
22 * @brief Implementation of the JSAttributeFilter 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 "AttributeFilter.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_VALUE "matchValue"
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 JSAttributeFilter::m_classRef = NULL;
51 JSClassDefinition JSAttributeFilter::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 JSAttributeFilter::m_property[] = {
73 { ATTRIBUTE_FILTER_ATTR_ATTRIBUTE_NAME, getAttributeName, setAttributeName, kJSPropertyAttributeNone },
74 { ATTRIBUTE_FILTER_ATTR_MATCH_FLAG, getMatchFlag, setMatchFlag, kJSPropertyAttributeNone },
75 { ATTRIBUTE_FILTER_ATTR_MATCH_VALUE, getMatchValue, setMatchValue, kJSPropertyAttributeNone },
79 JSStaticFunction JSAttributeFilter::m_functions[] =
84 JSClassRef JSAttributeFilter::getClassRef() {
86 m_classRef = JSClassCreate(&m_classInfo);
91 bool JSAttributeFilter::isObjectOfClass(JSContextRef context, JSValueRef value)
93 return JSValueIsObjectOfClass(context, value, getClassRef());
96 AttributeFilterPtr JSAttributeFilter::getAttributeFilter(JSContextRef context, JSValueRef value)
98 if (!isObjectOfClass(context, value)) {
99 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
101 JSObjectRef object = JSValueToObject(context, value, NULL);
103 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
105 JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(JSObjectGetPrivate(object));
107 Throw(WrtDeviceApis::Commons::NullPointerException);
109 return priv->getObject();
112 JSObjectRef JSAttributeFilter::createJSObject(JSContextRef context, AttributeFilterPtr privateData)
114 JSAttributeFilterPriv *priv = new JSAttributeFilterPriv(context, privateData);
115 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
116 if (NULL == jsObjectRef) {
117 LogError("object creation error");
123 void JSAttributeFilter::Initialize(JSContextRef context, JSObjectRef object)
125 if (!JSObjectGetPrivate(object))
127 AttributeFilterPtr filter(new AttributeFilter("", MATCH_NONE, AnyPtr(NULL)));
128 JSAttributeFilterPriv *priv = new JSAttributeFilterPriv(context, AttributeFilterPtr(filter));
129 if (!JSObjectSetPrivate(object, priv)) {
135 void JSAttributeFilter::Finalize(JSObjectRef object)
137 JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(JSObjectGetPrivate(object));
147 AttributeFilterPtr JSAttributeFilter::getPrivData(JSObjectRef object)
150 JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(JSObjectGetPrivate(object));
152 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
154 AttributeFilterPtr result = priv->getObject();
156 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
161 JSObjectRef JSAttributeFilter::constructor(JSContextRef context,
162 JSObjectRef constructor,
163 size_t argumentCount,
164 const JSValueRef arguments[],
165 JSValueRef* exception)
169 bool js2ndParamIsString = false;
170 bool js3rdParamIsValue = false;
172 // AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_ADD);
173 // TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
175 JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(JSObjectGetPrivate(constructor));
177 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
179 JSContextRef gContext = priv->getContext();
181 // JSContextRef gContext = JSGlobalContextFactory::getInstance()->get();
183 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
185 if (argumentCount < 1)
186 ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
188 if (!JSValueIsString(gContext, arguments[0]))
189 ThrowMsg(InvalidArgumentException, "1st argument is not string.");
191 if (argumentCount >= 2)
193 if (JSValueIsString(gContext, arguments[1]))
194 js2ndParamIsString = true;
196 if (!js2ndParamIsString &&
197 !JSValueIsNull(gContext, arguments[1]) &&
198 !JSValueIsUndefined(gContext, arguments[1]))
199 ThrowMsg(InvalidArgumentException, "2nd argument is not array.");
202 if (argumentCount >= 3)
204 if (!JSValueIsNull(gContext, arguments[2]) && !JSValueIsUndefined(gContext, arguments[2]))
205 js3rdParamIsValue = true;
209 } Catch(Exception ) {
210 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
211 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
215 FilterConverterFactory::ConverterType converter = FilterConverterFactory::getConverter(gContext);
217 std::string attributeName;
219 AnyPtr matchValue(NULL);
222 attributeName = converter->toString(arguments[0]);
224 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
225 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
230 if(js2ndParamIsString)
231 matchFlag = converter->toMatchFlag(arguments[1]);
233 matchFlag = MATCH_EXACTLY;
236 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
237 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
242 if(js3rdParamIsValue)
243 matchValue = converter->toAny(arguments[2]);
245 matchValue = AnyPtr(new Any());
248 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
249 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
253 AttributeFilterPtr attributeFilter(new AttributeFilter(attributeName, matchFlag, matchValue));
255 JSObjectRef jsobject;
258 jsobject = createJSObject(gContext, attributeFilter);
260 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
261 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
268 JSValueRef JSAttributeFilter::getAttributeName(JSContextRef context,
270 JSStringRef propertyName,
271 JSValueRef* exception)
276 FilterConverterFactory::ConverterType converter =
277 FilterConverterFactory::getConverter(context);
278 AttributeFilterPtr attributeFilter = getPrivData(object);
279 return converter->toJSValueRef(attributeFilter->getAttributeName());
281 Catch(WrtDeviceApis::Commons::Exception)
283 LogWarning("trying to get incorrect value");
285 return JSValueMakeUndefined(context);
288 bool JSAttributeFilter::setAttributeName(JSContextRef context,
290 JSStringRef propertyName,
292 JSValueRef* exception)
296 AttributeFilterPtr attributeFilter = getPrivData(object);
297 FilterConverterFactory::ConverterType converter =
298 FilterConverterFactory::getConverter(context);
299 attributeFilter->setAttributeName(converter->toString(value));
302 Catch(WrtDeviceApis::Commons::Exception)
304 LogWarning("trying to set incorrect value");
307 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
311 JSValueRef JSAttributeFilter::getMatchFlag(JSContextRef context,
313 JSStringRef propertyName,
314 JSValueRef* exception)
319 FilterConverterFactory::ConverterType converter =
320 FilterConverterFactory::getConverter(context);
321 AttributeFilterPtr attributeFilter = getPrivData(object);
322 return converter->toJSValueRef(attributeFilter->getMatchFlag());
324 Catch(WrtDeviceApis::Commons::Exception)
326 LogWarning("trying to get incorrect value");
328 return JSValueMakeUndefined(context);
331 bool JSAttributeFilter::setMatchFlag(JSContextRef context,
333 JSStringRef propertyName,
335 JSValueRef* exception)
339 AttributeFilterPtr attributeFilter = getPrivData(object);
340 FilterConverterFactory::ConverterType converter =
341 FilterConverterFactory::getConverter(context);
342 attributeFilter->setMatchFlag(converter->toMatchFlag(value));
345 Catch(WrtDeviceApis::Commons::Exception)
347 LogWarning("trying to set incorrect value");
350 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
354 JSValueRef JSAttributeFilter::getMatchValue(JSContextRef context,
356 JSStringRef propertyName,
357 JSValueRef* exception)
362 FilterConverterFactory::ConverterType converter =
363 FilterConverterFactory::getConverter(context);
364 AttributeFilterPtr attributeFilter = getPrivData(object);
365 return converter->toJSValueRef(attributeFilter->getMatchValue());
367 Catch(WrtDeviceApis::Commons::Exception)
369 LogWarning("trying to get incorrect value");
372 return JSValueMakeUndefined(context);
375 bool JSAttributeFilter::setMatchValue(JSContextRef context,
377 JSStringRef propertyName,
379 JSValueRef* exception)
383 AttributeFilterPtr attributeFilter = getPrivData(object);
384 FilterConverterFactory::ConverterType converter =
385 FilterConverterFactory::getConverter(context);
386 attributeFilter->setMatchValue(converter->toAny(value));
389 Catch(WrtDeviceApis::Commons::Exception)
391 LogWarning("trying to set incorrect value");
394 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");