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