Update change log and spec for wrt-plugins-tizen_0.4.13
[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) const{
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) const{
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) const{
71     return static_cast<long>(toLongLong(index,nullable,defaultvalue));
72 }
73
74 unsigned long ArgumentValidator::toULong(int index, bool nullable, long defaultvalue) const{
75     return static_cast<unsigned long>(toULongLong(index,nullable,defaultvalue));
76 }
77
78 long long ArgumentValidator::toLongLong(int index, bool nullable, long long defaultvalue) const{
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) const{
83     return static_cast<unsigned long long>(toNumber(index,nullable,defaultvalue));
84 }
85
86
87 double ArgumentValidator::toDouble(int index, bool nullable, double defaultvalue) const{
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) const{
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) const{
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) const{
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) const{
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) const{
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) const{
163     return getArgument(index, nullable);
164 }
165
166 JSObjectRef ArgumentValidator::toFunction(int index, bool nullable) const{
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) const{
178     JSValueRef value = getArgument(index, nullable);
179     if( JSValueIsNull(mContext, value) && nullable){
180         return NULL;
181     }
182
183     if( !JSIsArrayValue(mContext, value)){
184         throw TypeMismatchException("Type is not Array");
185     }
186
187     JSValueRef exception = NULL;
188     JSObjectRef obj = JSValueToObject(mContext, value, &exception);
189     if( exception != NULL )
190         throw TypeMismatchException(mContext, exception);
191     return obj;
192 }
193
194
195 std::vector<std::string> ArgumentValidator::toStringVector(int index, bool nullable) const{
196     JSObjectRef value = toArrayObject(index, nullable);
197     if( value == NULL || JSValueIsNull(mContext, value) ){
198         return std::vector<std::string>();
199     }
200     return JSUtil::JSArrayToStringVector(mContext, value);
201 }
202
203 std::vector<long> ArgumentValidator::toLongVector(int index, bool nullable) const{
204     JSObjectRef value = toArrayObject(index, nullable);
205     if( value == NULL || JSValueIsNull(mContext, value) ){
206         return std::vector<long>();
207     }
208     return JSUtil::JSArrayToLongVector(mContext, value);
209 }
210
211
212 std::vector<double> ArgumentValidator::toDoubleVector(int index, bool nullable) const{
213     JSObjectRef value = toArrayObject(index, nullable);
214     if( value == NULL || JSValueIsNull(mContext, value) ){
215         return std::vector<double>();
216     }
217     return JSUtil::JSArrayToDoubleVector(mContext, value);
218 }
219
220 std::vector<time_t> ArgumentValidator::toTimeTVector(int index, bool nullable) const{
221     JSObjectRef value = toArrayObject(index, nullable);
222     if( value == NULL || JSValueIsNull(mContext, value) ){
223         return std::vector<time_t>();
224     }
225     return JSUtil::JSArrayToTimeTVector(mContext, value);
226 }
227
228 std::vector<bool> ArgumentValidator::toBoolVector(int index, bool nullable) const{
229     JSObjectRef value = toArrayObject(index, nullable);
230     if( value == NULL || JSValueIsNull(mContext, value) ){
231         return std::vector<bool>();
232     }
233     return JSUtil::JSArrayToBoolVector(mContext, value);
234 }
235
236 std::vector<JSValueRef> ArgumentValidator::toJSValueRefVector(int index, bool nullable) const{
237     JSObjectRef value = toArrayObject(index, nullable);
238     if( value == NULL || JSValueIsNull(mContext, value) ){
239         return std::vector<JSValueRef>();
240     }
241
242     std::vector<JSValueRef> result;
243     for (std::size_t i = 0; i < JSGetArrayLength(mContext, value); ++i) {
244         JSValueRef element = JSGetArrayElement(mContext, value, i);
245         result.push_back(element);
246     }
247     return result;
248 }
249
250 std::map<std::string, std::string> ArgumentValidator::toStringMap(int index, bool nullable) const{
251     JSObjectRef value = toObject(index, nullable);
252     if( value == NULL || JSValueIsNull(mContext, value) ){
253         return std::map<std::string, std::string>();
254     }
255
256     return JSUtil::JSValueToStringMap(mContext, value);
257 }
258
259
260 }
261 }