2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef WRTDEVICEAPIS_COMMONS_FUNCTION_DECLARATION_
17 #define WRTDEVICEAPIS_COMMONS_FUNCTION_DECLARATION_
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>
28 #define ACE_DECLARE_FUNCTION(function_definition) \
29 extern WrtDeviceApis::Commons::AceFunction ace_##function_definition
31 #define ACE_DECLARE_PARAM(param_definition) \
32 extern WrtDeviceApis::Commons::AceDeviceCapParam ace_param_## \
38 explicit DevCapFinder(const std::string& devcap) : m_searchFor(devcap)
40 explicit DevCapFinder(const char* devcap) : m_searchFor(devcap)
42 bool operator()(const WrtDeviceApis::Commons::AceDeviceCapability& dc)
45 return m_searchFor == dc.devCapName;
49 std::string m_searchFor;
52 #define ACE_ADD_DEV_CAP_PARAM(dev_caps_list, dev_cap_name, param) \
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()) \
61 WrtDeviceApis::Commons::InvalidArgumentException, \
62 "Trying to set a param that doesn't exist: " << \
67 LogInfo("Setting dev cap " << dev_cap_name << " param: " << \
68 param.name << " to value " << param.value); \
69 devcapit->devCapParams.push_back(param); \
74 * Macro must be run inside plugin function. Plugin function must follow this
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
83 #define DECLARE_FUNCTION_GETTER(Plugin_Module) \
84 WrtDeviceApis::Commons::AceFunction get##Plugin_Module##FunctionData( \
85 const std::string & functionId);
87 #define DEFINE_FUNCTION_GETTER(Plugin_Module, FunctionMap) \
88 WrtDeviceApis::Commons::AceFunction get##Plugin_Module##FunctionData( \
89 const std::string & functionId) \
91 WrtDeviceApis::Commons::FunctionMapping::const_iterator it = \
92 FunctionMap.find(functionId); \
93 if (it == FunctionMap.end()) \
95 std::string errorMsg(); \
96 ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, \
97 "Function with id " << functionId << "not found"); \
102 typedef WrtDeviceApis::Commons::AceFunction (&AceFunctionGetter)(const std::
105 namespace WrtDeviceApis {
107 enum class AceSecurityStatus
114 template <typename ... Args>
115 class DefaultArgsVerifier
118 void operator()(AceFunction& aceFunction, Args && ... args) const
121 WrtDeviceApis::Commons::AlwaysFalse<Args ...>::value,
122 "Please provide a specialization for these argument types!");
127 class DefaultArgsVerifier<>
130 void operator()(AceFunction& /*aceFunction*/) const
134 template <typename FunctionGetter,
135 typename ArgumentsVerifier,
137 AceSecurityStatus aceCheckAccess(
138 const FunctionGetter& f,
139 const char* functionName,
142 using namespace WrtDeviceApis::Commons;
144 AceFunction aceFunction = f(functionName);
146 ArgumentsVerifier argsVerify;
147 argsVerify(aceFunction, args ...);
149 if (!(WrtAccessSingleton::Instance().checkAccessControl(aceFunction))) {
150 LogDebug("Function is not allowed to run");
151 return AceSecurityStatus::AccessDenied;
153 LogDebug("Function accepted!");
155 return AceSecurityStatus::AccessGranted;
158 } // WrtDeviceApisCommon
160 #endif // WRTDEVICEAPIS_COMMONS_FUNCTION_DECLARARION_