tizen beta release
[framework/web/wrt-plugins-common.git] / src / CommonsJavaScript / Validator.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 #include "Validator.h"
17 #include <list>
18 #include <dpl/assert.h>
19 #include "ScopedJSStringRef.h"
20 #include "Converter.h"
21
22 namespace WrtDeviceApis {
23 namespace CommonsJavaScript {
24
25 Validator::Validator(JSContextRef context,
26         JSValueRef* exception) :
27     m_context(context),
28     m_exception(exception)
29 {
30     Assert(NULL != m_context && "Context cannot be NULL.");
31 }
32
33 Validator::~Validator()
34 {
35 }
36
37 bool Validator::isDate(const JSValueRef& arg)
38 {
39     if (JSValueIsNull(m_context,
40                       arg) ||
41         JSValueIsUndefined(m_context,
42                            arg) || !JSValueIsObject(m_context, arg)) {
43         return false;
44     }
45     Converter converter(m_context);
46     Try
47     {
48         converter.toDateTm(arg);
49     }
50     Catch(Commons::ConversionException)
51     {
52         return false;
53     }
54     return true;
55 }
56
57 bool
58 Validator::isCallback(const JSValueRef& arg)
59 {
60     Converter converter(m_context);
61     return !JSValueIsNull(m_context, arg) &&
62            !JSValueIsUndefined(m_context, arg) &&
63            JSObjectIsFunction(m_context, converter.toJSObjectRef(arg));
64 }
65
66 bool
67 Validator::checkArrayKeys(const std::vector<std::string> &allowed,
68         JSValueRef argument)
69 {
70     if (argument == NULL) {
71         return true;
72     }
73     if (!JSValueIsObject(m_context, argument)) {
74         return false;
75     }
76     JSObjectRef jsOptions = JSValueToObject(m_context, argument, NULL);
77     if (jsOptions == NULL) {
78         return false;
79     }
80     JSPropertyNameArrayRef jsProps = JSObjectCopyPropertyNames(m_context,
81                                                                jsOptions);
82     if (jsProps == NULL) {
83         // No properties found; and empty array.
84         return true;
85     }
86     size_t nCount = JSPropertyNameArrayGetCount(jsProps);
87     bool found;
88
89     std::list<JSStringRef> allowedJS;
90     for (size_t j = 0; j < allowed.size(); j++) {
91         allowedJS.push_back(JSStringCreateWithUTF8CString(allowed[j].c_str()));
92     }
93
94     for (size_t i = 0; i < nCount; i++) {
95         found = false;
96         for (std::list<JSStringRef>::const_iterator it = allowedJS.begin();
97              it != allowedJS.end();
98              it++) {
99             if (JSStringIsEqual(*it,
100                                 JSPropertyNameArrayGetNameAtIndex(jsProps,
101                                                                   i))) {
102                 found = true;
103                 break;
104             }
105         }
106         if (!found) {
107             for (size_t j = 0; j < allowed.size(); j++) {
108                 JSStringRelease(allowedJS.front());
109                 allowedJS.pop_front();
110             }
111             JSPropertyNameArrayRelease(jsProps);
112             return false;
113         }
114     }
115
116     for (size_t j = 0; j < allowed.size(); j++) {
117         JSStringRelease(allowedJS.front());
118         allowedJS.pop_front();
119     }
120     JSPropertyNameArrayRelease(jsProps);
121     return true;
122 }
123
124 bool Validator::isNullOrUndefined(const JSValueRef& arg)
125 {
126     return (JSValueIsNull(m_context, arg) ||
127             JSValueIsUndefined(m_context, arg));
128 }
129
130 } // CommonsJavaScript
131 } // WrtDeviceApis