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)
41 explicit DevCapFinder(const char* devcap) : m_searchFor(devcap)
44 bool operator()(const WrtDeviceApis::Commons::AceDeviceCapability& dc) const
46 return m_searchFor == dc.devCapName;
50 std::string m_searchFor;
53 #define ACE_ADD_DEV_CAP_PARAM(dev_caps_list, dev_cap_name, param) \
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()) \
62 WrtDeviceApis::Commons::InvalidArgumentException, \
63 "Trying to set a param that doesn't exist: " << \
68 LogInfo("Setting dev cap " << dev_cap_name << " param: " << \
69 param.name << " to value " << param.value); \
70 devcapit->devCapParams.push_back(param); \
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.
80 #define DECLARE_FUNCTION_GETTER(Plugin_Module) \
81 WrtDeviceApis::Commons::AceFunction get ## Plugin_Module ## FunctionData( \
82 const std::string & functionId);
84 #define DEFINE_FUNCTION_GETTER(Plugin_Module, FunctionMap) \
85 WrtDeviceApis::Commons::AceFunction get ## Plugin_Module ## FunctionData( \
86 const std::string & functionId) \
88 WrtDeviceApis::Commons::FunctionMapping::const_iterator it = \
89 FunctionMap.find(functionId); \
90 if (it == FunctionMap.end()) \
92 std::string errorMsg(); \
93 ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, \
94 "Function with id " << functionId << "not found"); \
99 typedef WrtDeviceApis::Commons::AceFunction (&AceFunctionGetter)(const std::string&);
101 namespace WrtDeviceApis {
104 enum class AceSecurityStatus
111 template <typename ... Args>
112 class DefaultArgsVerifier
115 void operator()(AceFunction& aceFunction, Args && ... args) const
118 WrtDeviceApis::Commons::AlwaysFalse<Args ...>::value,
119 "Please provide a specialization for these argument types!");
124 class DefaultArgsVerifier<>
127 void operator()(AceFunction& /*aceFunction*/) const
132 template <typename FunctionGetter,
133 typename ArgumentsVerifier,
135 AceSecurityStatus aceCheckAccess(
136 const FunctionGetter& f,
137 const char* functionName,
140 using namespace WrtDeviceApis::Commons;
142 AceFunction aceFunction = f(functionName);
144 ArgumentsVerifier argsVerify;
145 argsVerify(aceFunction, args ...);
147 if (!(WrtAccessSingleton::Instance().checkAccessControl(aceFunction))) {
148 LogDebug("Function is not allowed to run");
149 return AceSecurityStatus::AccessDenied;
151 LogDebug("Function accepted!");
153 return AceSecurityStatus::AccessGranted;
157 } // WrtDeviceApisCommon
159 #endif // WRTDEVICEAPIS_COMMONS_FUNCTION_DECLARARION_