merge with master
[platform/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 enum class AceSecurityStatus
30 {
31     AccessGranted,
32     AccessDenied,
33     InternalError
34 };
35
36 template <typename ... Args>
37 class DefaultArgsVerifier
38 {
39   public:
40     void operator()(WrtDeviceApis::Commons::AceFunction& aceFunction,
41                     Args && ... args) const
42     {
43         static_assert(
44             WrtDeviceApis::Commons::AlwaysFalse<Args ...>::value,
45             "Please provide a specialization for these argument types!");
46     }
47 };
48
49 template <>
50 class DefaultArgsVerifier<>
51 {
52   public:
53     void operator()(WrtDeviceApis::Commons::AceFunction& /*aceFunction*/) const
54     {}
55 };
56
57 template <typename ArgumentsVerifier,
58           typename ... Args>
59 AceSecurityStatus aceCheckAccess2(
60     WrtDeviceApis::Commons::AceFunction aceFunction,
61     Args && ... args)
62 {
63     using namespace WrtDeviceApis::Commons;
64
65     ArgumentsVerifier argsVerify;
66     argsVerify(aceFunction, args ...);
67
68     Try {
69         if (!(WrtAccessSingleton::Instance().checkAccessControl(aceFunction)))
70         {
71             LogDebug("Function is not allowed to run");
72             return AceSecurityStatus::AccessDenied;
73         }
74     }
75     Catch(WrtDeviceApis::Commons::OutOfRangeException) {
76         LogError("WrtAccess doesn't exist.");
77         return AceSecurityStatus::InternalError;
78     }
79
80     LogDebug("Function accepted!");
81
82     return AceSecurityStatus::AccessGranted;
83 }
84
85 //The simplest version
86 AceSecurityStatus aceCheckAccessSimple(
87     WrtDeviceApis::Commons::AceFunction aceFunction);
88 }
89 }
90
91 #endif // _FUNCTION_DECLARARION_