15e5dcf4a11de0f4ebbdf7106775f56725219240
[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 <dlog.h>
25 #include <math.h>
26
27 #undef LOG_TAG
28 #define LOG_TAG "TIZEN_DEVICEAPI"
29
30 using namespace std;
31 using namespace WrtDeviceApis::CommonsJavaScript;
32 using namespace WrtDeviceApis::Commons;
33
34
35 namespace DeviceAPI {
36 namespace Common{
37
38 ArgumentValidator::ArgumentValidator(JSContextRef ctx, int argc, const JSValueRef* argv):mContext(ctx),mArgc(argc), mArgv(argv){
39 }
40
41 ArgumentValidator::~ArgumentValidator(){
42 }
43
44 JSValueRef ArgumentValidator::getArgument(int index, bool nullable){
45     if( index < mArgc ){
46         return mArgv[index];
47     }
48
49     JSValueRef value = NULL;
50     if( nullable )
51         value = JSValueMakeNull(mContext);
52     else
53         value = JSValueMakeUndefined(mContext);
54     return value;
55 }
56
57 double ArgumentValidator::toNumber(int index, bool nullable, double defaultvalue){
58     JSValueRef value = getArgument(index, nullable);
59     JSValueRef exception = NULL;
60     if( JSValueIsNull(mContext, value) && nullable){
61         return defaultvalue;
62     }
63     double numberValue = JSValueToNumber(mContext, value, &exception);
64     if(exception != NULL){
65         throw TypeMismatchException(mContext, exception);
66     }
67     return numberValue;
68 }
69
70 long ArgumentValidator::toLong(int index, bool nullable, long defaultvalue){
71     return static_cast<long>(toLongLong(index,nullable,defaultvalue));
72 }
73
74 unsigned long ArgumentValidator::toULong(int index, bool nullable, long defaultvalue){
75     return static_cast<unsigned long>(toULongLong(index,nullable,defaultvalue));
76 }
77
78 long long ArgumentValidator::toLongLong(int index, bool nullable, long long defaultvalue){
79     return static_cast<long long>(toNumber(index,nullable,defaultvalue));
80 }
81
82 unsigned long long ArgumentValidator::toULongLong(int index, bool nullable, long long defaultvalue){
83     return static_cast<unsigned long long>(toNumber(index,nullable,defaultvalue));
84 }
85
86
87 double ArgumentValidator::toDouble(int index, bool nullable, double defaultvalue){
88     double doublevalue = toNumber(index,nullable,defaultvalue);
89     if( isnan(doublevalue)){
90         throw TypeMismatchException("Value is not number");
91     }
92     return doublevalue;
93 }
94
95 std::string ArgumentValidator::toString(int index, bool nullable, const  string & defaultvalue){
96     JSValueRef value = getArgument(index, nullable);
97     if( JSValueIsNull(mContext, value) && nullable){
98         return defaultvalue;
99     }
100
101     std::string result;
102
103     JSValueRef exception = NULL;
104     JSStringRef str = JSValueToStringCopy(mContext, value, &exception);
105     if (exception != NULL) {
106         throw TypeMismatchException(mContext, exception);
107     }
108     size_t jsSize = JSStringGetMaximumUTF8CStringSize(str);
109     {
110         char buffer[jsSize];
111         JSStringGetUTF8CString(str, buffer, jsSize);
112         result = buffer;
113     }
114     JSStringRelease(str);
115     return result;
116 }
117
118 bool ArgumentValidator::toBool(int index, bool nullable, bool defaultvalue){
119     JSValueRef value = getArgument(index, nullable);
120     if( JSValueIsNull(mContext, value) && nullable){
121         return defaultvalue;
122     }
123     bool boolvalue = JSValueToBoolean(mContext, value);
124     return boolvalue;
125 }
126
127 time_t ArgumentValidator::toTimeT(int index, bool nullable, time_t defaultvalue){
128     JSValueRef value = getArgument(index, nullable);
129     if( JSValueIsNull(mContext, value) && nullable){
130         return defaultvalue;
131     }
132     return JSUtil::JSValueToTimeT(mContext, value);
133 }
134
135 JSObjectRef ArgumentValidator::toObject(int index, bool nullable){
136     JSValueRef value = getArgument(index, nullable);
137     if( JSValueIsNull(mContext, value) && nullable){
138         return NULL;
139     }
140     if( !JSValueIsObject(mContext, value) ){
141         throw TypeMismatchException("Value is not Object");
142     }
143
144     JSValueRef exception = NULL;
145     JSObjectRef object = JSValueToObject(mContext, value, &exception);
146     if( exception ){
147         throw TypeMismatchException(mContext, exception);
148     }
149     return object;
150 }
151
152 JSObjectRef ArgumentValidator::toObject(int index, JSClassRef info, bool nullable){
153     JSObjectRef obj = toObject(index, nullable);
154     if( obj == NULL )
155         return NULL;
156     if( !JSValueIsObjectOfClass( mContext, obj, info) ){
157         throw TypeMismatchException("Value is not correct type");
158     }
159     return obj;
160 }
161
162 JSValueRef ArgumentValidator::toJSValueRef(int index, bool nullable){
163     return getArgument(index, nullable);
164 }
165
166 JSObjectRef ArgumentValidator::toFunction(int index, bool nullable){
167     JSObjectRef obj = toObject(index, nullable);
168     if( obj == NULL && nullable){
169         return NULL;
170     }
171     if( !JSObjectIsFunction( mContext, obj )){
172         throw TypeMismatchException("Value is not function");
173     }
174     return obj;
175 }
176
177 JSObjectRef ArgumentValidator::toArrayObject(int index, bool nullable){
178     JSValueRef value = getArgument(index, nullable);
179     if( JSValueIsNull(mContext, value) && nullable){
180         return NULL;
181     }
182
183     if( !JSIsArrayValue(mContext, value)){
184         JSObjectRef jsArray = JSCreateArrayObject(mContext, 0, NULL);
185         if( !JSValueIsNull(mContext,value) && !JSValueIsUndefined(mContext,value) )
186             JSSetArrayElement(mContext, jsArray, 0, value);
187         return jsArray;
188     }
189     JSValueRef exception = NULL;
190     JSObjectRef obj = JSValueToObject(mContext, value, &exception);
191     if( exception != NULL )
192         throw TypeMismatchException(mContext, exception);
193     return obj;
194 }
195
196
197 std::vector<std::string> ArgumentValidator::toStringVector(int index, bool nullable){
198     JSObjectRef value = toArrayObject(index, nullable);
199     if( value == NULL || JSValueIsNull(mContext, value) ){
200         return std::vector<std::string>();
201     }
202     return JSUtil::JSArrayToStringVector(mContext, value);
203 }
204
205 std::vector<long> ArgumentValidator::toLongVector(int index, bool nullable){
206     JSObjectRef value = toArrayObject(index, nullable);
207     if( value == NULL || JSValueIsNull(mContext, value) ){
208         return std::vector<long>();
209     }
210     return JSUtil::JSArrayToLongVector(mContext, value);
211 }
212
213
214 std::vector<double> ArgumentValidator::toDoubleVector(int index, bool nullable){
215     JSObjectRef value = toArrayObject(index, nullable);
216     if( value == NULL || JSValueIsNull(mContext, value) ){
217         return std::vector<double>();
218     }
219     return JSUtil::JSArrayToDoubleVector(mContext, value);
220 }
221
222 std::vector<time_t> ArgumentValidator::toTimeTVector(int index, bool nullable){
223     JSObjectRef value = toArrayObject(index, nullable);
224     if( value == NULL || JSValueIsNull(mContext, value) ){
225         return std::vector<time_t>();
226     }
227     return JSUtil::JSArrayToTimeTVector(mContext, value);
228 }
229
230 std::vector<bool> ArgumentValidator::toBoolVector(int index, bool nullable){
231     JSObjectRef value = toArrayObject(index, nullable);
232     if( value == NULL || JSValueIsNull(mContext, value) ){
233         return std::vector<bool>();
234     }
235     return JSUtil::JSArrayToBoolVector(mContext, value);
236 }
237
238 std::vector<JSValueRef> ArgumentValidator::toJSValueRefVector(int index, bool nullable){
239     JSObjectRef value = toArrayObject(index, nullable);
240     if( value == NULL || JSValueIsNull(mContext, value) ){
241         return std::vector<JSValueRef>();
242     }
243
244     std::vector<JSValueRef> result;
245     for (std::size_t i = 0; i < JSGetArrayLength(mContext, value); ++i) {
246         JSValueRef element = JSGetArrayElement(mContext, value, i);
247         result.push_back(element);
248     }
249     return result;
250 }
251
252 std::map<std::string, std::string> ArgumentValidator::toStringMap(int index, bool nullable){
253     JSObjectRef value = toObject(index, nullable);
254     if( value == NULL || JSValueIsNull(mContext, value) ){
255         return std::map<std::string, std::string>();
256     }
257
258     return JSUtil::JSValueToStringMap(mContext, value);
259 }
260
261
262 }
263 }