Update change log and spec for wrt-plugins-tizen_0.4.21
[framework/web/wrt-plugins-tizen.git] / src / Common / ArgumentValidator.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2013 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 #include "ArgumentValidator.h"
20 #include "PlatformException.h"
21 #include "JSUtil.h"
22 #include <CommonsJavaScript/Converter.h>
23
24 #include <limits>
25 #include <dlog.h>
26 #include <math.h>
27 #include <stdarg.h>
28
29
30 #undef LOG_TAG
31 #define LOG_TAG "TIZEN_DEVICEAPI"
32
33 using namespace std;
34 using namespace WrtDeviceApis::CommonsJavaScript;
35 using namespace WrtDeviceApis::Commons;
36
37
38 namespace DeviceAPI {
39 namespace Common{
40
41 ArgumentValidator::ArgumentValidator(JSContextRef ctx, int argc, const JSValueRef* argv):mContext(ctx),mArgc(argc), mArgv(argv){
42 }
43
44 ArgumentValidator::~ArgumentValidator(){
45 }
46
47 JSValueRef ArgumentValidator::getArgument(int index, bool nullable) const{
48     if( index < mArgc ){
49         return mArgv[index];
50     }
51
52     JSValueRef value = NULL;
53     if( nullable )
54         value = JSValueMakeNull(mContext);
55     else
56         value = JSValueMakeUndefined(mContext);
57     return value;
58 }
59
60 bool ArgumentValidator::isOmitted(int index){
61     if( index < mArgc)
62         return false;
63     return true;
64 }
65 bool ArgumentValidator::isNull(int index){
66     if( !isOmitted(index) && JSValueIsNull(mContext, mArgv[index]) ){
67         return true;
68     }
69     return false;
70 }
71 bool ArgumentValidator::isUndefined(int index){
72     if( !isOmitted(index) && JSValueIsUndefined(mContext, mArgv[index]) ){
73         return true;
74     }
75     return false;
76 }
77
78 double ArgumentValidator::toNumber(int index, bool nullable, double defaultvalue) const{
79     JSValueRef value = getArgument(index, nullable);
80     if( JSValueIsNull(mContext, value) && nullable){
81         return defaultvalue;
82     }
83     return JSUtil::JSValueToNumber(mContext, value);
84 }
85
86 long ArgumentValidator::toLong(int index, bool nullable, long defaultvalue) const{
87     return static_cast<long>(toLongLong(index,nullable,defaultvalue));
88 }
89
90 signed char ArgumentValidator::toByte(int index, bool nullable, signed char defaultvalue) const{
91     return static_cast<signed char>(toLong(index,nullable,defaultvalue));
92 }
93
94 unsigned char ArgumentValidator::toOctet(int index, bool nullable, unsigned char defaultvalue) const{
95     return static_cast<unsigned char>(toULong(index,nullable,defaultvalue));
96 }
97
98 unsigned long ArgumentValidator::toULong(int index, bool nullable, unsigned long defaultvalue) const{
99     double number = toNumber(index,nullable,defaultvalue);
100     if( number < 0 )
101         return static_cast<unsigned long>(static_cast<long>(number));
102     return static_cast<unsigned long>(number);
103 }
104
105 long long ArgumentValidator::toLongLong(int index, bool nullable, long long defaultvalue) const{
106     return static_cast<long long>(toNumber(index,nullable,defaultvalue));
107 }
108
109 unsigned long long ArgumentValidator::toULongLong(int index, bool nullable, unsigned long long defaultvalue) const{
110     double number = toNumber(index,nullable,defaultvalue);
111     if( number < 0 )
112         return static_cast<unsigned long long>(static_cast<long long>(number));
113     return static_cast<unsigned long long>(number);
114 }
115
116
117 double ArgumentValidator::toDouble(int index, bool nullable, double defaultvalue) const{
118     JSValueRef value = getArgument(index, nullable);
119     if( JSValueIsNull(mContext, value) && nullable){
120         return defaultvalue;
121     }
122     return JSUtil::JSValueToDouble(mContext, value);
123 }
124
125 std::string ArgumentValidator::toString(int index, bool nullable, const  string & defaultvalue) const{
126     JSValueRef value = getArgument(index, nullable);
127     if( JSValueIsNull(mContext, value) && nullable){
128         return defaultvalue;
129     }
130
131     std::string result;
132
133     JSValueRef exception = NULL;
134     JSStringRef str = JSValueToStringCopy(mContext, value, &exception);
135     if (exception != NULL) {
136         throw TypeMismatchException(mContext, exception);
137     }
138     size_t jsSize = JSStringGetMaximumUTF8CStringSize(str);
139     {
140         char buffer[jsSize];
141         JSStringGetUTF8CString(str, buffer, jsSize);
142         result = buffer;
143     }
144     JSStringRelease(str);
145     return result;
146 }
147
148 bool ArgumentValidator::toBool(int index, bool nullable, bool defaultvalue) const{
149     JSValueRef value = getArgument(index, nullable);
150     if( JSValueIsNull(mContext, value) && nullable){
151         return defaultvalue;
152     }
153     bool boolvalue = JSValueToBoolean(mContext, value);
154     return boolvalue;
155 }
156
157 time_t ArgumentValidator::toTimeT(int index, bool nullable, time_t defaultvalue) const{
158     JSValueRef value = getArgument(index, nullable);
159     if( JSValueIsNull(mContext, value) && nullable){
160         return defaultvalue;
161     }
162     return JSUtil::JSValueToTimeT(mContext, value);
163 }
164
165 JSObjectRef ArgumentValidator::toObject(int index, bool nullable) const{
166     JSValueRef value = getArgument(index, nullable);
167     if( JSValueIsNull(mContext, value) && nullable){
168         return NULL;
169     }
170     if( !JSValueIsObject(mContext, value) ){
171         throw TypeMismatchException("Value is not Object");
172     }
173
174     JSValueRef exception = NULL;
175     JSObjectRef object = JSValueToObject(mContext, value, &exception);
176     if( exception ){
177         throw TypeMismatchException(mContext, exception);
178     }
179     return object;
180 }
181
182 JSObjectRef ArgumentValidator::toObject(int index, JSClassRef info, bool nullable) const{
183     JSObjectRef obj = toObject(index, nullable);
184     if( obj == NULL )
185         return NULL;
186     if( !JSValueIsObjectOfClass( mContext, obj, info) ){
187         throw TypeMismatchException("Value is not correct type");
188     }
189     return obj;
190 }
191
192 JSValueRef ArgumentValidator::toJSValueRef(int index, bool nullable) const{
193     return getArgument(index, nullable);
194 }
195
196 JSObjectRef ArgumentValidator::toFunction(int index, bool nullable) const{
197     JSObjectRef obj = toObject(index, nullable);
198     if( obj == NULL && nullable){
199         return NULL;
200     }
201     if( !JSObjectIsFunction( mContext, obj )){
202         throw TypeMismatchException("Value is not function");
203     }
204     return obj;
205 }
206
207 JSObjectRef ArgumentValidator::toArrayObject(int index, bool nullable) const{
208     JSValueRef value = getArgument(index, nullable);
209     if( JSValueIsNull(mContext, value) && nullable){
210         return NULL;
211     }
212
213     if( !JSIsArrayValue(mContext, value)){
214         throw TypeMismatchException("Type is not Array");
215     }
216
217     JSValueRef exception = NULL;
218     JSObjectRef obj = JSValueToObject(mContext, value, &exception);
219     if( exception != NULL )
220         throw TypeMismatchException(mContext, exception);
221     return obj;
222 }
223
224
225 std::vector<std::string> ArgumentValidator::toStringVector(int index, bool nullable) const{
226     JSObjectRef value = toArrayObject(index, nullable);
227     if( value == NULL || JSValueIsNull(mContext, value) ){
228         return std::vector<std::string>();
229     }
230     return JSUtil::JSArrayToStringVector(mContext, value);
231 }
232
233 std::vector<long> ArgumentValidator::toLongVector(int index, bool nullable) const{
234     JSObjectRef value = toArrayObject(index, nullable);
235     if( value == NULL || JSValueIsNull(mContext, value) ){
236         return std::vector<long>();
237     }
238     return JSUtil::JSArrayToLongVector(mContext, value);
239 }
240
241
242 std::vector<double> ArgumentValidator::toDoubleVector(int index, bool nullable) const{
243     JSObjectRef value = toArrayObject(index, nullable);
244     if( value == NULL || JSValueIsNull(mContext, value) ){
245         return std::vector<double>();
246     }
247     return JSUtil::JSArrayToDoubleVector(mContext, value);
248 }
249
250 std::vector<time_t> ArgumentValidator::toTimeTVector(int index, bool nullable) const{
251     JSObjectRef value = toArrayObject(index, nullable);
252     if( value == NULL || JSValueIsNull(mContext, value) ){
253         return std::vector<time_t>();
254     }
255     return JSUtil::JSArrayToTimeTVector(mContext, value);
256 }
257
258 std::vector<bool> ArgumentValidator::toBoolVector(int index, bool nullable) const{
259     JSObjectRef value = toArrayObject(index, nullable);
260     if( value == NULL || JSValueIsNull(mContext, value) ){
261         return std::vector<bool>();
262     }
263     return JSUtil::JSArrayToBoolVector(mContext, value);
264 }
265
266 std::vector<JSValueRef> ArgumentValidator::toJSValueRefVector(int index, bool nullable) const{
267     JSObjectRef value = toArrayObject(index, nullable);
268     if( value == NULL || JSValueIsNull(mContext, value) ){
269         return std::vector<JSValueRef>();
270     }
271
272     std::vector<JSValueRef> result;
273     for (std::size_t i = 0; i < JSGetArrayLength(mContext, value); ++i) {
274         JSValueRef element = JSGetArrayElement(mContext, value, i);
275         result.push_back(element);
276     }
277     return result;
278 }
279
280 std::map<std::string, std::string> ArgumentValidator::toStringMap(int index, bool nullable) const{
281     JSObjectRef value = toObject(index, nullable);
282     if( value == NULL || JSValueIsNull(mContext, value) ){
283         return std::map<std::string, std::string>();
284     }
285
286     return JSUtil::JSValueToStringMap(mContext, value);
287 }
288
289 JSObjectRef ArgumentValidator::toCallbackObject(int index, bool nullable, const char *callback, ...) const{
290     JSObjectRef obj = toObject(index, nullable);
291     if( obj == NULL && nullable){
292         return NULL;
293     }
294     va_list var_args;
295     va_start (var_args, callback);
296     const char * check = callback;
297     while( check != NULL ){
298         JSStringRef propertyName = JSStringCreateWithUTF8CString(check);
299         bool has = JSObjectHasProperty(mContext, obj, propertyName);
300         JSStringRelease(propertyName);
301         if( has ){
302             JSObjectRef o = JSUtil::JSValueToObject(mContext, JSUtil::getProperty(mContext, obj, check));
303             if( !JSObjectIsFunction(mContext, o) ){
304                 va_end(var_args);
305                 throw TypeMismatchException("Property is not function object");
306             }
307         }
308         check = static_cast<const char *>(va_arg(var_args, const char *));
309     }
310     va_end(var_args);
311     return obj;
312 }
313
314
315
316 }
317 }