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