Update change log and spec for wrt-plugins-tizen_0.4.49
[framework/web/wrt-plugins-tizen.git] / src / Tizen / JSCompositeFilter.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        JSCompositeFilter.cpp
20  * @author      Kisub Song (kisubs.song@samsung.com)
21  * @version     0.1
22  * @brief       Implementation of the JSCompositeFilter class
23  */
24
25 #include "JSCompositeFilter.h"
26 #include <string>
27 #include <dpl/shared_ptr.h>
28 #include <CommonsJavaScript/ScopedJSStringRef.h>
29 #include <JSUtil.h>
30 #include <ArgumentValidator.h>
31 #include <JSWebAPIErrorFactory.h>
32 #include <Logger.h>
33
34 #define ATTRIBUTE_FILTER_CLASS_NAME "CompositeFilter"
35 #define ATTRIBUTE_FILTER_ATTR_TYPE "type"
36 #define ATTRIBUTE_FILTER_ATTR_FILTERS "filters"
37
38 namespace DeviceAPI {
39 namespace Tizen {
40
41 using namespace DeviceAPI::Common;
42 using namespace DeviceAPI::Tizen;
43 using namespace WrtDeviceApis::Commons;
44 using namespace WrtDeviceApis::CommonsJavaScript;
45
46 JSClassRef JSCompositeFilter::m_classRef = NULL;
47
48 JSClassDefinition JSCompositeFilter::m_classInfo =
49 {
50         0,
51         kJSClassAttributeNone,
52         ATTRIBUTE_FILTER_CLASS_NAME,
53         NULL,
54         m_property,
55         m_functions,
56         Initialize,
57         Finalize,
58         NULL,//hasProperty,
59         NULL,//getProperty,
60         setProperty,
61         NULL,//deleteProperty,
62         NULL,//getPropertyNames,
63         NULL, //CallAsFunction,
64         NULL, //CallAsConstructor,
65         NULL, //HasInstance,
66         NULL, //ConvertToType,
67 };
68
69 JSStaticValue JSCompositeFilter::m_property[] = {
70         { ATTRIBUTE_FILTER_ATTR_TYPE, getType, setType, kJSPropertyAttributeNone },
71         { 0, 0, 0, 0 }
72 };
73
74 JSStaticFunction JSCompositeFilter::m_functions[] =
75 {
76         { 0, 0, 0 }
77 };
78
79 JSClassRef JSCompositeFilter::getClassRef() {
80         if (!m_classRef) {
81                 m_classRef = JSClassCreate(&m_classInfo);
82         }
83         return m_classRef;
84 }
85
86 bool JSCompositeFilter::isObjectOfClass(JSContextRef context, JSValueRef value)
87 {
88         return JSValueIsObjectOfClass(context, value, getClassRef());
89 }
90
91 CompositeFilterPtr JSCompositeFilter::getCompositeFilter(JSContextRef context, JSValueRef value)
92 {
93         if (!isObjectOfClass(context, value))
94                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
95
96         JSObjectRef object = JSValueToObject(context, value, NULL);
97         if (!object)
98                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
99
100         JSCompositeFilterPriv *priv = static_cast<JSCompositeFilterPriv*>(JSObjectGetPrivate(object));
101         if (!priv)
102                 Throw(WrtDeviceApis::Commons::NullPointerException);
103
104         return priv->getObject();
105 }
106
107 JSObjectRef JSCompositeFilter::createJSObject(JSContextRef context,
108                 CompositeFilterPtr privateData,
109                 JSValueRef jsValueFilters)
110 {
111         JSCompositeFilterPriv *priv = new JSCompositeFilterPriv(context, privateData);
112         JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
113         if (NULL == jsObjectRef)
114         {
115                 LoggerE("object creation error");
116                 return NULL;
117         }
118
119         const ScopedJSStringRef jsStrFilters(JSStringCreateWithUTF8CString(ATTRIBUTE_FILTER_ATTR_FILTERS));
120
121         JSObjectSetProperty(context, jsObjectRef, jsStrFilters.get(), jsValueFilters, kJSPropertyAttributeNone, NULL);
122
123         return jsObjectRef;
124 }
125
126 JSObjectRef JSCompositeFilter::constructor(JSContextRef context,
127                 JSObjectRef constructor,
128                 size_t argumentCount,
129                 const JSValueRef arguments[],
130                 JSValueRef* exception)
131 {
132         LoggerD("entered");
133
134         FilterType type;
135         JSValueRef jsValueFilters = NULL;
136
137         ArgumentValidator validator(context, argumentCount, arguments);
138
139         try
140         {
141                 std::string typeStr = validator.toString(0, false);
142
143                 if(typeStr == "UNION")
144                         type = UNION_FILTER;
145                 else if(typeStr == "INTERSECTION")
146                         type = INTERSECTION_FILTER;
147                 else
148                         throw TypeMismatchException("Property is not valid CompositeFilterType value");
149         }
150         catch(BasePlatformException &e)
151         {
152                 LoggerW("Failed to convert 1st parameter to filter type. Using default value.");
153                 type = UNION_FILTER;
154         }
155
156         try
157         {
158                 if(argumentCount < 2 ||
159                                 JSValueIsUndefined(context, arguments[1]) ||
160                                 JSValueIsNull(context, arguments[1]))
161                         jsValueFilters = JSCreateArrayObject(context, 0, NULL);
162                 else if(JSIsArrayValue(context, arguments[1]))
163                         jsValueFilters = arguments[1];
164                 else
165                         throw TypeMismatchException("Property is not valid Array value");
166         }
167         catch(BasePlatformException &e)
168         {
169                 LoggerW("Failed to convert 2nd parameter to filter array. Using default value.");
170                 jsValueFilters = JSCreateArrayObject(context, 0, NULL);
171         }
172
173         CompositeFilterPtr compositeFilter(new CompositeFilter(type, FilterArrayPtr(new FilterArray())));
174
175         JSObjectRef jsobject = NULL;
176
177         Try
178         {
179                 jsobject = createJSObject(context, compositeFilter, jsValueFilters);
180         }
181         Catch(Exception)
182         {
183                 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
184         }
185
186         if(jsobject == NULL)
187                 return JSObjectMake(context, NULL, NULL);
188
189         return jsobject;
190 }
191
192 void JSCompositeFilter::Initialize(JSContextRef context, JSObjectRef object)
193 {
194         if (!JSObjectGetPrivate(object))
195         {
196                 CompositeFilterPtr filter(new CompositeFilter(UNION_FILTER, FilterArrayPtr(NULL)));
197                 JSCompositeFilterPriv *priv = new JSCompositeFilterPriv(context, CompositeFilterPtr(filter));
198                 if (!JSObjectSetPrivate(object, priv))
199                 {
200                         delete priv;
201                 }
202         }
203 }
204
205 void JSCompositeFilter::Finalize(JSObjectRef object)
206 {
207         JSCompositeFilterPriv *priv = static_cast<JSCompositeFilterPriv*>(JSObjectGetPrivate(object));
208
209         if (priv != NULL)
210         {
211                 delete (priv);
212         }
213
214         priv = NULL;
215 }
216
217 CompositeFilterPtr JSCompositeFilter::getPrivData(JSObjectRef object)
218 {
219         LoggerD("entered");
220         JSCompositeFilterPriv *priv = static_cast<JSCompositeFilterPriv*>(JSObjectGetPrivate(object));
221         if (!priv)
222                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
223
224         CompositeFilterPtr result = priv->getObject();
225         if (!result)
226                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
227
228         return result;
229 }
230
231 bool JSCompositeFilter::setProperty(JSContextRef context,
232                 JSObjectRef object,
233                 JSStringRef propertyName,
234                 JSValueRef value,
235                 JSValueRef* exception)
236 {
237         LoggerD("entered");
238
239         try
240         {
241                 CompositeFilterPtr compositeFilter = getPrivData(object);
242                 std::string name = JSUtil::JSStringToString(context, propertyName);
243                 if(name == ATTRIBUTE_FILTER_ATTR_FILTERS)
244                 {
245                         if(!JSIsArrayValue(context, value))
246                                 return true;
247
248                         return false;
249                 }
250         }
251         catch(BasePlatformException &e)
252         {
253                 LoggerW("trying to set incorrect value [" << e.getName() << "] " << e.getMessage());
254         }
255
256         return false;
257 }
258
259 JSValueRef JSCompositeFilter::getType(JSContextRef context,
260                 JSObjectRef object,
261                 JSStringRef propertyName,
262                 JSValueRef* exception)
263 {
264         LoggerD("entered");
265
266         try
267         {
268                 CompositeFilterPtr compositeFilter = getPrivData(object);
269                 FilterType type = compositeFilter->getFilterType();
270                 std::string typeStr;
271                 if(type == UNION_FILTER)
272                         typeStr = "UNION";
273                 else if(type == INTERSECTION_FILTER)
274                         typeStr = "INTERSECTION";
275                 else
276                         typeStr = "UNION";
277
278                 return JSUtil::toJSValueRef(context, typeStr);
279         }
280         catch(BasePlatformException &e)
281         {
282                 LoggerW("trying to get incorrect value [" << e.getName() << "] " << e.getMessage());
283         }
284
285         return JSValueMakeUndefined(context);
286 }
287
288 bool JSCompositeFilter::setType(JSContextRef context,
289                 JSObjectRef object,
290                 JSStringRef propertyName,
291                 JSValueRef value,
292                 JSValueRef* exception)
293 {
294         LoggerD("entered");
295
296         try
297         {
298                 CompositeFilterPtr compositeFilter = getPrivData(object);
299
300                 FilterType type;
301                 std::string typeStr = JSUtil::JSValueToString(context, value);
302
303                 if(typeStr == "UNION")
304                         type = UNION_FILTER;
305                 else if(typeStr == "INTERSECTION")
306                         type = INTERSECTION_FILTER;
307                 else
308                         throw TypeMismatchException("Property is not valid CompositeFilterType value");
309
310                 compositeFilter->setFilterType(type);
311         }
312         catch(BasePlatformException &e)
313         {
314                 LoggerW("trying to set incorrect value [" << e.getName() << "] " << e.getMessage());
315         }
316
317         return true;
318 }
319
320 } // Tizen
321 } // DeviceAPI