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