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