fe30b08a9cd2ab3f3b6d0688e74c98c481ff465b
[framework/web/wrt-plugins-common.git] / src_wearable / 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 Validator::Validator(JSContextRef context,
25                      JSValueRef* exception) :
26     m_context(context),
27     m_exception(exception)
28 {
29     AssertMsg(NULL != m_context, "Context cannot be NULL.");
30 }
31
32 Validator::~Validator()
33 {}
34
35 bool Validator::isDate(const JSValueRef& arg)
36 {
37     if (JSValueIsNull(m_context,
38                       arg) ||
39         JSValueIsUndefined(m_context,
40                            arg) || !JSValueIsObject(m_context, arg))
41     {
42         return false;
43     }
44     Converter converter(m_context);
45     Try
46     {
47         converter.toDateTm(arg);
48     }
49     Catch(Commons::ConversionException)
50     {
51         return false;
52     }
53     return true;
54 }
55
56 bool
57 Validator::isCallback(const JSValueRef& arg)
58 {
59     Converter converter(m_context);
60     return !JSValueIsNull(m_context, arg) &&
61            !JSValueIsUndefined(m_context, arg) &&
62            JSObjectIsFunction(m_context, converter.toJSObjectRef(arg));
63 }
64
65 bool
66 Validator::checkArrayKeys(const std::vector<std::string> &allowed,
67                           JSValueRef argument)
68 {
69     if (argument == NULL) {
70         return true;
71     }
72     if (!JSValueIsObject(m_context, argument)) {
73         return false;
74     }
75     JSObjectRef jsOptions = JSValueToObject(m_context, argument, NULL);
76     if (jsOptions == NULL) {
77         return false;
78     }
79     JSPropertyNameArrayRef jsProps = JSObjectCopyPropertyNames(m_context,
80                                                                jsOptions);
81     if (jsProps == NULL) {
82         // No properties found; and empty array.
83         return true;
84     }
85     size_t nCount = JSPropertyNameArrayGetCount(jsProps);
86     bool found;
87
88     std::list<JSStringRef> allowedJS;
89     for (size_t j = 0; j < allowed.size(); j++) {
90         allowedJS.push_back(JSStringCreateWithUTF8CString(allowed[j].c_str()));
91     }
92
93     for (size_t i = 0; i < nCount; i++) {
94         found = false;
95         for (std::list<JSStringRef>::const_iterator it = allowedJS.begin();
96              it != allowedJS.end();
97              ++it)
98         {
99             if (JSStringIsEqual(*it,
100                                 JSPropertyNameArrayGetNameAtIndex(jsProps,
101                                                                   i)))
102             {
103                 found = true;
104                 break;
105             }
106         }
107         if (!found) {
108             for (size_t j = 0; j < allowed.size(); j++) {
109                 JSStringRelease(allowedJS.front());
110                 allowedJS.pop_front();
111             }
112             JSPropertyNameArrayRelease(jsProps);
113             return false;
114         }
115     }
116
117     for (size_t j = 0; j < allowed.size(); j++) {
118         JSStringRelease(allowedJS.front());
119         allowedJS.pop_front();
120     }
121     JSPropertyNameArrayRelease(jsProps);
122     return true;
123 }
124
125 bool Validator::isNullOrUndefined(const JSValueRef& arg)
126 {
127     return (JSValueIsNull(m_context, arg) ||
128             JSValueIsUndefined(m_context, arg));
129 }
130 } // CommonsJavaScript
131 } // WrtDeviceApis