907526872f95e3bf95a875ce78f2b9dc8fa0ffcc
[platform/framework/web/wrt-plugins-common.git] / src / Commons / FunctionDeclaration.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_COMMONS_FUNCTION_DECLARATION_
17 #define WRTDEVICEAPIS_COMMONS_FUNCTION_DECLARATION_
18
19 #include <string>
20 #include <algorithm>
21 #include <dpl/log/log.h>
22 #include <Commons/WrtAccess/WrtAccess.h>
23 #include <Commons/Exception.h>
24 #include <Commons/TypesDeclaration.h>
25 #include <Commons/TypeTraits.h>
26 #include <Commons/plugin_initializer_def.h>
27
28 #define ACE_DECLARE_FUNCTION(function_definition) \
29     extern WrtDeviceApis::Commons::AceFunction ace_##function_definition
30
31 #define ACE_DECLARE_PARAM(param_definition) \
32     extern WrtDeviceApis::Commons::AceDeviceCapParam ace_param_## \
33     param_definition
34
35 class DevCapFinder
36 {
37   public:
38     explicit DevCapFinder(const std::string& devcap) : m_searchFor(devcap)
39     {}
40     explicit DevCapFinder(const char* devcap) : m_searchFor(devcap)
41     {}
42     bool operator()(const WrtDeviceApis::Commons::AceDeviceCapability& dc)
43     const
44     {
45         return m_searchFor == dc.devCapName;
46     }
47
48   private:
49     std::string m_searchFor;
50 };
51
52 #define ACE_ADD_DEV_CAP_PARAM(dev_caps_list, dev_cap_name, param)              \
53     do {                                                                       \
54         WrtDeviceApis::Commons::AceDeviceCaps::iterator devcapit =                \
55             std::find_if(dev_caps_list.begin(),                                \
56                          dev_caps_list.end(),                                  \
57                          DevCapFinder(dev_cap_name));                          \
58         if (devcapit == dev_caps_list.end())                                   \
59         {                                                                      \
60             ThrowMsg(                                                          \
61                 WrtDeviceApis::Commons::InvalidArgumentException,                 \
62                 "Trying to set a param that doesn't exist: " <<                \
63                 dev_cap_name);                                                 \
64         }                                                                      \
65         else                                                                   \
66         {                                                                      \
67             LogDebug("Setting dev cap " << dev_cap_name << " param: " <<        \
68                     param.name << " to value " << param.value);                \
69             devcapit->devCapParams.push_back(param);                           \
70         }                                                                      \
71     } while (0)
72
73 /*
74  * Macro must be run inside plugin function. Plugin function must follow this
75  * declaration:
76  * plugin_example_function(JScontextRef cotext, JSObjectRef function,
77  * JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
78  * JSValueRef *exception);
79  * where context, arguments, argumentsCount, exception are fixed and could not
80  * be change.
81  * */
82
83 #define DECLARE_FUNCTION_GETTER(Plugin_Module)                                 \
84     WrtDeviceApis::Commons::AceFunction get##Plugin_Module##FunctionData(     \
85         const std::string & functionId);
86
87 #define DEFINE_FUNCTION_GETTER(Plugin_Module, FunctionMap)                     \
88     WrtDeviceApis::Commons::AceFunction get##Plugin_Module##FunctionData(     \
89         const std::string & functionId)                                        \
90     {                                                                          \
91         WrtDeviceApis::Commons::FunctionMapping::const_iterator it =              \
92             FunctionMap.find(functionId);                                      \
93         if (it == FunctionMap.end())                                           \
94         {                                                                      \
95             std::string errorMsg();                                            \
96             ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException,            \
97                      "Function with id " << functionId << "not found");        \
98         }                                                                      \
99         return it->second;                                                     \
100     }
101
102 typedef WrtDeviceApis::Commons::AceFunction (&AceFunctionGetter)(const std::
103                                                                      string&);
104
105 namespace WrtDeviceApis {
106 namespace Commons {
107 enum class AceSecurityStatus
108 {
109     AccessGranted,
110     AccessDenied,
111     InternalError
112 };
113
114 template <typename ... Args>
115 class DefaultArgsVerifier
116 {
117   public:
118     void operator()(AceFunction& aceFunction, Args && ... args) const
119     {
120         static_assert(
121             WrtDeviceApis::Commons::AlwaysFalse<Args ...>::value,
122             "Please provide a specialization for these argument types!");
123     }
124 };
125
126 template <>
127 class DefaultArgsVerifier<>
128 {
129   public:
130     void operator()(AceFunction& /*aceFunction*/) const
131     {}
132 };
133
134 template <typename FunctionGetter,
135           typename ArgumentsVerifier,
136           typename ... Args>
137 AceSecurityStatus aceCheckAccess(
138     const FunctionGetter& f,
139     const char* functionName,
140     Args && ... args)
141 {
142     using namespace WrtDeviceApis::Commons;
143
144     AceFunction aceFunction = f(functionName);
145
146     ArgumentsVerifier argsVerify;
147     argsVerify(aceFunction, args ...);
148
149     if (!(WrtAccessSingleton::Instance().checkAccessControl(aceFunction))) {
150             return AceSecurityStatus::AccessDenied;
151     }
152
153     return AceSecurityStatus::AccessGranted;
154 }
155 }
156 } // WrtDeviceApisCommon
157
158 #endif // WRTDEVICEAPIS_COMMONS_FUNCTION_DECLARARION_