tizen beta release
[framework/web/wrt-plugins-common.git] / src / CommonsJavaScript / Validator.h
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 #ifndef WRTDEVICEAPIS_COMMONSJAVASCRIPT_VALIDATOR_H_
17 #define WRTDEVICEAPIS_COMMONSJAVASCRIPT_VALIDATOR_H_
18
19 #include <ctime>
20 #include <string>
21 #include <vector>
22 #include <JavaScriptCore/JavaScript.h>
23 #include <dpl/noncopyable.h>
24 #include <dpl/shared_ptr.h>
25 #include <Commons/Exception.h>
26
27 namespace WrtDeviceApis {
28 namespace CommonsJavaScript {
29
30 class Validator : private DPL::Noncopyable
31 {
32   public:
33     explicit Validator(JSContextRef context,
34             JSValueRef* exception = NULL);
35     virtual ~Validator();
36
37     /**
38      * Checks if arg is a Date.
39      * @param arg JSValueRef to check.
40      * @return true when object is a date, false otherwise
41      */
42     bool isDate(const JSValueRef& arg);
43
44     /**
45      * Checks if arg is a callback function.
46      * @param arg JSValueRef to check.
47      * @return true when object is a callback function, false otherwise
48      */
49     bool isCallback(const JSValueRef& arg);
50
51     /**
52      * Checks if arg have allowed properties.
53      * @param allowed allowed property names.
54      * @param arg JSValueRef to check.
55      * @return true when object's properties are subset of allowed
56      */
57     bool checkArrayKeys(const std::vector<std::string> &allowed,
58             JSValueRef argument);
59
60     bool isNullOrUndefined(const JSValueRef& arg);
61
62   protected:
63     JSContextRef m_context;
64     JSValueRef* m_exception;
65 };
66
67 template<class C>
68 class ValidatorFactory : private DPL::Noncopyable
69 {
70   public:
71     /**
72      * Validator type which deletes itself when gets out of scope.
73      */
74     typedef DPL::SharedPtr<C> ValidatorType;
75
76   public:
77     /**
78      * Gets converter object.
79      * @param context JavaScript context the conversion will be performed in.
80      * @param[out] exception JavaScript value for storing exception.
81      * @return Converter object.
82      */
83     static ValidatorType getValidator(JSContextRef context,
84             JSValueRef* exception = NULL)
85     {
86         C* convert = new C(context, exception);
87         return ValidatorType(convert);
88     }
89
90   private:
91     ValidatorFactory();
92 };
93
94 typedef ValidatorFactory<Validator> BasicValidatorFactory;
95 typedef BasicValidatorFactory::ValidatorType BasicValidator;
96
97 } // CommonsJavaScript
98 } // WrtDeviceApis
99
100 #endif /* _VALIDATOR_H_ */