upload tizen1.0 source
[framework/web/wrt-plugins-common.git] / src / CommonsJavaScript / Security / SecurityFunctionDeclaration.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 _FUNCTION_DECLARATION_
17 #define _FUNCTION_DECLARATION_
18
19 #include <string>
20 #include <Commons/WrtAccess/WrtAccess.h>
21 #include <Commons/Exception.h>
22 #include <JavaScriptCore/JavaScript.h>
23 #include <Commons/TypesDeclaration.h>
24 #include <Commons/TypeTraits.h>
25 #include <dpl/log/log.h>
26
27 namespace WrtDeviceApis {
28 namespace CommonsJavaScript {
29
30 enum class AceSecurityStatus
31 {
32     AccessGranted,
33     AccessDenied,
34     InternalError
35 };
36
37 template <typename ... Args>
38 class DefaultArgsVerifier
39 {
40   public:
41     void operator()(WrtDeviceApis::Commons::AceFunction& aceFunction,
42             Args && ... args) const
43     {
44         static_assert(
45             WrtDeviceApis::Commons::AlwaysFalse<Args ...>::value,
46             "Please provide a specialization for these argument types!");
47     }
48 };
49
50 template <>
51 class DefaultArgsVerifier<>
52 {
53   public:
54     void operator()(WrtDeviceApis::Commons::AceFunction& /*aceFunction*/) const
55     {
56     }
57 };
58
59 template <typename ArgumentsVerifier,
60           typename ... Args>
61 AceSecurityStatus aceCheckAccess2(
62         WrtDeviceApis::Commons::AceFunction aceFunction,
63         Args && ... args)
64 {
65     using namespace WrtDeviceApis::Commons;
66
67     ArgumentsVerifier argsVerify;
68     argsVerify(aceFunction, args ...);
69
70     Try {
71         if (!(WrtAccessSingleton::Instance().checkAccessControl(aceFunction))) {
72         LogDebug("Function is not allowed to run");
73         return AceSecurityStatus::AccessDenied;
74         }
75     }
76     Catch(WrtDeviceApis::Commons::OutOfRangeException) {
77         LogError("WrtAccess doesn't exist.");
78         return AceSecurityStatus::InternalError;
79     }
80
81     LogDebug("Function accepted!");
82
83     return AceSecurityStatus::AccessGranted;
84 }
85
86
87
88 //The simplest version
89 AceSecurityStatus aceCheckAccessSimple(
90         WrtDeviceApis::Commons::AceFunction aceFunction);
91
92 }
93 }
94
95 #endif // _FUNCTION_DECLARARION_