47323499d918a1cdc31580e20f524fdd4d4c9b6c
[framework/web/wrt-plugins-tizen.git] / src / Tizen / JSAttributeFilter.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 /**
19  * @file        JSAttributeFilter.cpp
20  * @author      Kisub Song (kisubs.song@samsung.com)
21  * @version     0.1
22  * @brief       Implementation of the JSAttributeFilter class
23  */
24
25 #include <string>
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"
35
36 #define ATTRIBUTE_FILTER_CLASS_NAME "AttributeFilter"
37
38 #define ATTRIBUTE_FILTER_ATTR_ATTRIBUTE_NAME "attributeName"
39 #define ATTRIBUTE_FILTER_ATTR_MATCH_FLAG "matchFlag"
40 #define ATTRIBUTE_FILTER_ATTR_MATCH_VALUE "matchValue"
41
42 namespace DeviceAPI {\rnamespace Tizen {
43
44 using namespace DeviceAPI::Common;
45 using namespace DeviceAPI::Tizen;
46 using namespace WrtDeviceApis::Commons;
47 using namespace WrtDeviceApis::CommonsJavaScript;
48
49 JSClassRef JSAttributeFilter::m_classRef = NULL;
50
51 JSClassDefinition JSAttributeFilter::m_classInfo =
52 {
53         0,
54         kJSClassAttributeNone,
55         ATTRIBUTE_FILTER_CLASS_NAME,
56         NULL,
57         m_property,
58         m_functions,
59         Initialize,
60         Finalize,
61         NULL, //hasProperty,
62         NULL, //GetProperty,
63         NULL, //SetProperty,
64         NULL, //DeleteProperty,
65         NULL, //getPropertyNames,
66         NULL, //CallAsFunction,
67         constructor, //CallAsConstructor,
68         NULL, //HasInstance,
69         NULL, //ConvertToType,
70 };
71
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 },
76         { 0, 0, 0, 0 }
77 };
78
79 JSStaticFunction JSAttributeFilter::m_functions[] =
80 {
81         { 0, 0, 0 }
82 };
83
84 JSClassRef JSAttributeFilter::getClassRef() {
85         if (!m_classRef) {
86                 m_classRef = JSClassCreate(&m_classInfo);
87         }
88         return m_classRef;
89 }
90
91 bool JSAttributeFilter::isObjectOfClass(JSContextRef context, JSValueRef value)
92 {
93         return JSValueIsObjectOfClass(context, value, getClassRef());
94 }
95
96 AttributeFilterPtr JSAttributeFilter::getAttributeFilter(JSContextRef context, JSValueRef value)
97 {
98         if (!isObjectOfClass(context, value)) {
99                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
100         }
101         JSObjectRef object = JSValueToObject(context, value, NULL);
102         if (!object) {
103                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
104         }
105         JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(JSObjectGetPrivate(object));
106         if (!priv) {
107                 Throw(WrtDeviceApis::Commons::NullPointerException);
108         }
109         return priv->getObject();
110 }
111
112 JSObjectRef JSAttributeFilter::createJSObject(JSContextRef context, AttributeFilterPtr privateData)
113 {
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");
118                 return NULL;
119         }
120         return jsObjectRef;
121 }
122
123 void JSAttributeFilter::Initialize(JSContextRef context, JSObjectRef object)
124 {
125         if (!JSObjectGetPrivate(object))
126         {
127                 AttributeFilterPtr filter(new AttributeFilter("", MATCH_NONE, AnyPtr(NULL)));
128                 JSAttributeFilterPriv *priv = new JSAttributeFilterPriv(context, AttributeFilterPtr(filter));
129                 if (!JSObjectSetPrivate(object, priv)) {
130                         delete priv;
131                 }
132         }
133 }
134
135 void JSAttributeFilter::Finalize(JSObjectRef object)
136 {
137         JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(JSObjectGetPrivate(object));
138
139         if (priv != NULL)
140         {
141                 delete (priv);
142         }
143
144         priv = NULL;
145 }
146
147 AttributeFilterPtr JSAttributeFilter::getPrivData(JSObjectRef object)
148 {
149         LogDebug("entered");
150         JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(JSObjectGetPrivate(object));
151         if (!priv) {
152                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
153         }
154         AttributeFilterPtr result = priv->getObject();
155         if (!result) {
156                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
157         }
158         return result;
159 }
160
161 JSObjectRef JSAttributeFilter::constructor(JSContextRef context,
162                 JSObjectRef constructor,
163                 size_t argumentCount,
164                 const JSValueRef arguments[],
165                 JSValueRef* exception)
166 {
167         LogDebug("entered");
168
169         bool js2ndParamIsString = false;
170         bool js3rdParamIsValue = false;
171
172 //      AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_ADD);
173 //      TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
174
175         JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(JSObjectGetPrivate(constructor));
176         if (!priv) {
177                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
178         }
179         JSContextRef gContext = priv->getContext();
180
181 //      JSContextRef gContext = JSGlobalContextFactory::getInstance()->get();
182
183         BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
184         Try {
185                 if (argumentCount < 1)
186                         ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
187
188                 if (!JSValueIsString(gContext, arguments[0]))
189                         ThrowMsg(InvalidArgumentException, "1st argument is not string.");
190
191                 if (argumentCount >= 2)
192                 {
193                         if (JSValueIsString(gContext, arguments[1]))
194                                 js2ndParamIsString = true;
195
196                         if (!js2ndParamIsString &&
197                                         !JSValueIsNull(gContext, arguments[1]) &&
198                                         !JSValueIsUndefined(gContext, arguments[1]))
199                                 ThrowMsg(InvalidArgumentException, "2nd argument is not array.");
200                 }
201
202                 if (argumentCount >= 3)
203                 {
204                         if (!JSValueIsNull(gContext, arguments[2]) && !JSValueIsUndefined(gContext, arguments[2]))
205                                 js3rdParamIsValue = true;
206                 }
207
208
209         } Catch(Exception ) {
210                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
211                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
212                 return NULL;
213         }
214
215         FilterConverterFactory::ConverterType converter = FilterConverterFactory::getConverter(gContext);
216
217         std::string attributeName;
218         MatchFlag matchFlag;
219         AnyPtr matchValue(NULL);
220
221         Try {
222                 attributeName = converter->toString(arguments[0]);
223         } Catch(Exception) {
224                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
225                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
226                 return NULL;
227         }
228
229         Try {
230                 if(js2ndParamIsString)
231                         matchFlag = converter->toMatchFlag(arguments[1]);
232                 else
233                         matchFlag = MATCH_EXACTLY;
234
235         } Catch(Exception) {
236                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
237                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
238                 return NULL;
239         }
240
241         Try {
242                 if(js3rdParamIsValue)
243                         matchValue = converter->toAny(arguments[2]);
244                 else
245                         matchValue = AnyPtr(new Any());
246
247         } Catch(Exception) {
248                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
249                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
250                 return NULL;
251         }
252
253         AttributeFilterPtr attributeFilter(new AttributeFilter(attributeName, matchFlag, matchValue));
254
255         JSObjectRef jsobject;
256
257         Try {
258                 jsobject = createJSObject(gContext, attributeFilter);
259         } Catch(Exception) {
260                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
261                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
262                 return NULL;
263         }
264
265         return jsobject;
266 }
267
268 JSValueRef JSAttributeFilter::getAttributeName(JSContextRef context,
269                 JSObjectRef object,
270                 JSStringRef propertyName,
271                 JSValueRef* exception)
272 {
273         LogDebug("entered");
274         Try
275         {
276                 FilterConverterFactory::ConverterType converter =
277                                 FilterConverterFactory::getConverter(context);
278                 AttributeFilterPtr attributeFilter = getPrivData(object);
279                 return converter->toJSValueRef(attributeFilter->getAttributeName());
280         }
281         Catch(WrtDeviceApis::Commons::Exception)
282         {
283                 LogWarning("trying to get incorrect value");
284         }
285         return JSValueMakeUndefined(context);
286 }
287
288 bool JSAttributeFilter::setAttributeName(JSContextRef context,
289                 JSObjectRef object,
290                 JSStringRef propertyName,
291                 JSValueRef value,
292                 JSValueRef* exception)
293 {
294         Try
295         {
296                 AttributeFilterPtr attributeFilter = getPrivData(object);
297                 FilterConverterFactory::ConverterType converter =
298                                 FilterConverterFactory::getConverter(context);
299                 attributeFilter->setAttributeName(converter->toString(value));
300                 return true;
301         }
302         Catch(WrtDeviceApis::Commons::Exception)
303         {
304                 LogWarning("trying to set incorrect value");
305         }
306
307         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
308         return false;
309 }
310
311 JSValueRef JSAttributeFilter::getMatchFlag(JSContextRef context,
312                 JSObjectRef object,
313                 JSStringRef propertyName,
314                 JSValueRef* exception)
315 {
316         LogDebug("entered");
317         Try
318         {
319                 FilterConverterFactory::ConverterType converter =
320                                 FilterConverterFactory::getConverter(context);
321                 AttributeFilterPtr attributeFilter = getPrivData(object);
322                 return converter->toJSValueRef(attributeFilter->getMatchFlag());
323         }
324         Catch(WrtDeviceApis::Commons::Exception)
325         {
326                 LogWarning("trying to get incorrect value");
327         }
328         return JSValueMakeUndefined(context);
329 }
330
331 bool JSAttributeFilter::setMatchFlag(JSContextRef context,
332                 JSObjectRef object,
333                 JSStringRef propertyName,
334                 JSValueRef value,
335                 JSValueRef* exception)
336 {
337         Try
338         {
339                 AttributeFilterPtr attributeFilter = getPrivData(object);
340                 FilterConverterFactory::ConverterType converter =
341                                 FilterConverterFactory::getConverter(context);
342                 attributeFilter->setMatchFlag(converter->toMatchFlag(value));
343                 return true;
344         }
345         Catch(WrtDeviceApis::Commons::Exception)
346         {
347                 LogWarning("trying to set incorrect value");
348         }
349
350         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
351         return false;
352 }
353
354 JSValueRef JSAttributeFilter::getMatchValue(JSContextRef context,
355                 JSObjectRef object,
356                 JSStringRef propertyName,
357                 JSValueRef* exception)
358 {
359         LogDebug("entered");
360         Try
361         {
362                 FilterConverterFactory::ConverterType converter =
363                                 FilterConverterFactory::getConverter(context);
364                 AttributeFilterPtr attributeFilter = getPrivData(object);
365                 return converter->toJSValueRef(attributeFilter->getMatchValue());
366         }
367         Catch(WrtDeviceApis::Commons::Exception)
368         {
369                 LogWarning("trying to get incorrect value");
370         }
371
372         return JSValueMakeUndefined(context);
373 }
374
375 bool JSAttributeFilter::setMatchValue(JSContextRef context,
376                 JSObjectRef object,
377                 JSStringRef propertyName,
378                 JSValueRef value,
379                 JSValueRef* exception)
380 {
381         Try
382         {
383                 AttributeFilterPtr attributeFilter = getPrivData(object);
384                 FilterConverterFactory::ConverterType converter =
385                                 FilterConverterFactory::getConverter(context);
386                 attributeFilter->setMatchValue(converter->toAny(value));
387                 return true;
388         }
389         Catch(WrtDeviceApis::Commons::Exception)
390         {
391                 LogWarning("trying to set incorrect value");
392         }
393
394         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
395         return false;
396 }
397
398 } // Tizen
399 } // DeviceAPI
400