Update change log and spec for wrt-plugins-tizen_0.4.11
[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         NULL, //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 JSObjectRef JSAttributeFilter::constructor(JSContextRef context,
124                 JSObjectRef constructor,
125                 size_t argumentCount,
126                 const JSValueRef arguments[],
127                 JSValueRef* exception)
128 {
129         LogDebug("entered");
130
131         bool js2ndParamIsString = false;
132         bool js3rdParamIsValue = false;
133
134         BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
135         Try {
136                 if (argumentCount < 1)
137                         ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
138
139                 if (!JSValueIsString(context, arguments[0]))
140                         ThrowMsg(InvalidArgumentException, "1st argument is not string.");
141
142                 if (argumentCount >= 2)
143                 {
144                         if (JSValueIsString(context, arguments[1]))
145                                 js2ndParamIsString = true;
146
147                         if (!js2ndParamIsString &&
148                                         !JSValueIsNull(context, arguments[1]) &&
149                                         !JSValueIsUndefined(context, arguments[1]))
150                                 ThrowMsg(InvalidArgumentException, "2nd argument is not array.");
151                 }
152
153                 if (argumentCount >= 3)
154                 {
155                         if (!JSValueIsNull(context, arguments[2]) && !JSValueIsUndefined(context, arguments[2]))
156                                 js3rdParamIsValue = true;
157                 }
158
159
160         } Catch(Exception ) {
161                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
162                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
163                 return NULL;
164         }
165
166         FilterConverterFactory::ConverterType converter = FilterConverterFactory::getConverter(context);
167
168         std::string attributeName;
169         MatchFlag matchFlag;
170         AnyPtr matchValue(NULL);
171
172         Try {
173                 attributeName = converter->toString(arguments[0]);
174         } Catch(Exception) {
175                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
176                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
177                 return NULL;
178         }
179
180         Try {
181                 if(js2ndParamIsString)
182                         matchFlag = converter->toMatchFlag(arguments[1]);
183                 else
184                         matchFlag = MATCH_EXACTLY;
185
186         } Catch(Exception) {
187                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
188                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
189                 return NULL;
190         }
191
192         Try {
193                 if(js3rdParamIsValue)
194                         matchValue = converter->toAny(arguments[2]);
195                 else
196                         matchValue = AnyPtr(new Any());
197
198         } Catch(Exception) {
199                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
200                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
201                 return NULL;
202         }
203
204         AttributeFilterPtr attributeFilter(new AttributeFilter(attributeName, matchFlag, matchValue));
205
206         JSObjectRef jsobject;
207
208         Try {
209                 jsobject = createJSObject(context, attributeFilter);
210         } Catch(Exception) {
211                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
212                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
213                 return NULL;
214         }
215
216         return jsobject;
217 }
218
219 void JSAttributeFilter::Initialize(JSContextRef context, JSObjectRef object)
220 {
221         if (!JSObjectGetPrivate(object))
222         {
223                 AttributeFilterPtr filter(new AttributeFilter("", MATCH_NONE, AnyPtr(NULL)));
224                 JSAttributeFilterPriv *priv = new JSAttributeFilterPriv(context, AttributeFilterPtr(filter));
225                 if (!JSObjectSetPrivate(object, priv)) {
226                         delete priv;
227                 }
228         }
229 }
230
231 void JSAttributeFilter::Finalize(JSObjectRef object)
232 {
233         JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(JSObjectGetPrivate(object));
234
235         if (priv != NULL)
236         {
237                 delete (priv);
238         }
239
240         priv = NULL;
241 }
242
243 AttributeFilterPtr JSAttributeFilter::getPrivData(JSObjectRef object)
244 {
245         LogDebug("entered");
246         JSAttributeFilterPriv *priv = static_cast<JSAttributeFilterPriv*>(JSObjectGetPrivate(object));
247         if (!priv) {
248                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
249         }
250         AttributeFilterPtr result = priv->getObject();
251         if (!result) {
252                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
253         }
254         return result;
255 }
256
257 JSValueRef JSAttributeFilter::getAttributeName(JSContextRef context,
258                 JSObjectRef object,
259                 JSStringRef propertyName,
260                 JSValueRef* exception)
261 {
262         LogDebug("entered");
263         Try
264         {
265                 FilterConverterFactory::ConverterType converter =
266                                 FilterConverterFactory::getConverter(context);
267                 AttributeFilterPtr attributeFilter = getPrivData(object);
268                 return converter->toJSValueRef(attributeFilter->getAttributeName());
269         }
270         Catch(WrtDeviceApis::Commons::Exception)
271         {
272                 LogWarning("trying to get incorrect value");
273         }
274         return JSValueMakeUndefined(context);
275 }
276
277 bool JSAttributeFilter::setAttributeName(JSContextRef context,
278                 JSObjectRef object,
279                 JSStringRef propertyName,
280                 JSValueRef value,
281                 JSValueRef* exception)
282 {
283         Try
284         {
285                 AttributeFilterPtr attributeFilter = getPrivData(object);
286                 FilterConverterFactory::ConverterType converter =
287                                 FilterConverterFactory::getConverter(context);
288                 attributeFilter->setAttributeName(converter->toString(value));
289                 return true;
290         }
291         Catch(WrtDeviceApis::Commons::Exception)
292         {
293                 LogWarning("trying to set incorrect value");
294         }
295
296         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
297         return false;
298 }
299
300 JSValueRef JSAttributeFilter::getMatchFlag(JSContextRef context,
301                 JSObjectRef object,
302                 JSStringRef propertyName,
303                 JSValueRef* exception)
304 {
305         LogDebug("entered");
306         Try
307         {
308                 FilterConverterFactory::ConverterType converter =
309                                 FilterConverterFactory::getConverter(context);
310                 AttributeFilterPtr attributeFilter = getPrivData(object);
311                 return converter->toJSValueRef(attributeFilter->getMatchFlag());
312         }
313         Catch(WrtDeviceApis::Commons::Exception)
314         {
315                 LogWarning("trying to get incorrect value");
316         }
317         return JSValueMakeUndefined(context);
318 }
319
320 bool JSAttributeFilter::setMatchFlag(JSContextRef context,
321                 JSObjectRef object,
322                 JSStringRef propertyName,
323                 JSValueRef value,
324                 JSValueRef* exception)
325 {
326         Try
327         {
328                 AttributeFilterPtr attributeFilter = getPrivData(object);
329                 FilterConverterFactory::ConverterType converter =
330                                 FilterConverterFactory::getConverter(context);
331                 attributeFilter->setMatchFlag(converter->toMatchFlag(value));
332                 return true;
333         }
334         Catch(WrtDeviceApis::Commons::Exception)
335         {
336                 LogWarning("trying to set incorrect value");
337         }
338
339         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
340         return false;
341 }
342
343 JSValueRef JSAttributeFilter::getMatchValue(JSContextRef context,
344                 JSObjectRef object,
345                 JSStringRef propertyName,
346                 JSValueRef* exception)
347 {
348         LogDebug("entered");
349         Try
350         {
351                 FilterConverterFactory::ConverterType converter =
352                                 FilterConverterFactory::getConverter(context);
353                 AttributeFilterPtr attributeFilter = getPrivData(object);
354                 return converter->toJSValueRef(attributeFilter->getMatchValue());
355         }
356         Catch(WrtDeviceApis::Commons::Exception)
357         {
358                 LogWarning("trying to get incorrect value");
359         }
360
361         return JSValueMakeUndefined(context);
362 }
363
364 bool JSAttributeFilter::setMatchValue(JSContextRef context,
365                 JSObjectRef object,
366                 JSStringRef propertyName,
367                 JSValueRef value,
368                 JSValueRef* exception)
369 {
370         Try
371         {
372                 AttributeFilterPtr attributeFilter = getPrivData(object);
373                 FilterConverterFactory::ConverterType converter =
374                                 FilterConverterFactory::getConverter(context);
375                 attributeFilter->setMatchValue(converter->toAny(value));
376                 return true;
377         }
378         Catch(WrtDeviceApis::Commons::Exception)
379         {
380                 LogWarning("trying to set incorrect value");
381         }
382
383         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
384         return false;
385 }
386
387 } // Tizen
388 } // DeviceAPI
389