tizen beta release
[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/WrtWrapper/WrtWrappersMgr.h>
23 #include <Commons/Exception.h>
24 #include <Commons/TypesDeclaration.h>
25 #include <Commons/TypeTraits.h>
26
27 #define ACE_DECLARE_FUNCTION(function_definition) \
28     extern WrtDeviceApis::Commons::AceFunction ace_ ## function_definition
29
30 #define ACE_DECLARE_PARAM(param_definition) \
31     extern WrtDeviceApis::Commons::AceDeviceCapParam ace_param_ ## \
32     param_definition
33
34 class DevCapFinder
35 {
36   public:
37     explicit DevCapFinder(const std::string& devcap) : m_searchFor(devcap)
38     {
39     }
40     explicit DevCapFinder(const char* devcap) : m_searchFor(devcap)
41     {
42     }
43     bool operator()(const WrtDeviceApis::Commons::AceDeviceCapability& dc) 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             LogInfo("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 declaration:
75  * plugin_example_function(JScontextRef cotext, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef *exception);
76  * where context, arguments, argumentsCount, exception are fixed and could not be change.
77  * */
78
79 #define DECLARE_FUNCTION_GETTER(Plugin_Module)                                 \
80     WrtDeviceApis::Commons::AceFunction get ## Plugin_Module ## FunctionData(     \
81         const std::string & functionId);
82
83 #define DEFINE_FUNCTION_GETTER(Plugin_Module, FunctionMap)                     \
84     WrtDeviceApis::Commons::AceFunction get ## Plugin_Module ## FunctionData(     \
85         const std::string & functionId)                                        \
86     {                                                                          \
87         WrtDeviceApis::Commons::FunctionMapping::const_iterator it =              \
88             FunctionMap.find(functionId);                                      \
89         if (it == FunctionMap.end())                                           \
90         {                                                                      \
91             std::string errorMsg();                                            \
92             ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException,            \
93                      "Function with id " << functionId << "not found");        \
94         }                                                                      \
95         return it->second;                                                     \
96     }
97
98 typedef WrtDeviceApis::Commons::AceFunction (&AceFunctionGetter)(const std::string&);
99
100 namespace WrtDeviceApis {
101 namespace Commons {
102
103 enum class AceSecurityStatus
104 {
105     AccessGranted,
106     AccessDenied,
107     InternalError
108 };
109
110 template <typename ... Args>
111 class DefaultArgsVerifier
112 {
113   public:
114     void operator()(AceFunction& aceFunction, Args && ... args) const
115     {
116         static_assert(
117             WrtDeviceApis::Commons::AlwaysFalse<Args ...>::value,
118             "Please provide a specialization for these argument types!");
119     }
120 };
121
122 template <>
123 class DefaultArgsVerifier<>
124 {
125   public:
126     void operator()(AceFunction& /*aceFunction*/) const
127     {
128     }
129 };
130
131 template <typename FunctionGetter,
132           typename ArgumentsVerifier,
133           typename ... Args>
134 AceSecurityStatus aceCheckAccess(
135         JavaScriptContext globalContext,
136         const FunctionGetter& f,
137         const char* functionName,
138         Args && ... args)
139 {
140     AceFunction aceFunction = f(functionName);
141
142     ArgumentsVerifier argsVerify;
143     argsVerify(aceFunction, args ...);
144
145     IWrtWrapperPtr wrapper =
146         WrtWrappersMgr::getInstance().getWrtWrapper(globalContext);
147     if (!wrapper) {
148         LogError("Wrapper doesn't exist.");
149         return AceSecurityStatus::InternalError;
150     }
151     if (!(wrapper->checkAccess(aceFunction))) {
152         LogDebug("Function is not allowed to run");
153         return AceSecurityStatus::AccessDenied;
154     }
155     LogDebug("Function accepted!");
156
157     return AceSecurityStatus::AccessGranted;
158 }
159
160 }
161 } // WrtDeviceApisCommon
162
163 #endif // WRTDEVICEAPIS_COMMONS_FUNCTION_DECLARARION_