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/WrtWrapper/WrtWrappersMgr.h>
23 #include <Commons/Exception.h>
24 #include <Commons/TypesDeclaration.h>
25 #include <Commons/TypeTraits.h>
27 #define ACE_DECLARE_FUNCTION(function_definition) \
28 extern WrtDeviceApis::Commons::AceFunction ace_ ## function_definition
30 #define ACE_DECLARE_PARAM(param_definition) \
31 extern WrtDeviceApis::Commons::AceDeviceCapParam ace_param_ ## \
37 explicit DevCapFinder(const std::string& devcap) : m_searchFor(devcap)
40 explicit DevCapFinder(const char* devcap) : m_searchFor(devcap)
43 bool operator()(const WrtDeviceApis::Commons::AceDeviceCapability& dc) const
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 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.
79 #define DECLARE_FUNCTION_GETTER(Plugin_Module) \
80 WrtDeviceApis::Commons::AceFunction get ## Plugin_Module ## FunctionData( \
81 const std::string & functionId);
83 #define DEFINE_FUNCTION_GETTER(Plugin_Module, FunctionMap) \
84 WrtDeviceApis::Commons::AceFunction get ## Plugin_Module ## FunctionData( \
85 const std::string & functionId) \
87 WrtDeviceApis::Commons::FunctionMapping::const_iterator it = \
88 FunctionMap.find(functionId); \
89 if (it == FunctionMap.end()) \
91 std::string errorMsg(); \
92 ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, \
93 "Function with id " << functionId << "not found"); \
98 typedef WrtDeviceApis::Commons::AceFunction (&AceFunctionGetter)(const std::string&);
100 namespace WrtDeviceApis {
103 enum class AceSecurityStatus
110 template <typename ... Args>
111 class DefaultArgsVerifier
114 void operator()(AceFunction& aceFunction, Args && ... args) const
117 WrtDeviceApis::Commons::AlwaysFalse<Args ...>::value,
118 "Please provide a specialization for these argument types!");
123 class DefaultArgsVerifier<>
126 void operator()(AceFunction& /*aceFunction*/) const
131 template <typename FunctionGetter,
132 typename ArgumentsVerifier,
134 AceSecurityStatus aceCheckAccess(
135 JavaScriptContext globalContext,
136 const FunctionGetter& f,
137 const char* functionName,
140 AceFunction aceFunction = f(functionName);
142 ArgumentsVerifier argsVerify;
143 argsVerify(aceFunction, args ...);
145 IWrtWrapperPtr wrapper =
146 WrtWrappersMgr::getInstance().getWrtWrapper(globalContext);
148 LogError("Wrapper doesn't exist.");
149 return AceSecurityStatus::InternalError;
151 if (!(wrapper->checkAccess(aceFunction))) {
152 LogDebug("Function is not allowed to run");
153 return AceSecurityStatus::AccessDenied;
155 LogDebug("Function accepted!");
157 return AceSecurityStatus::AccessGranted;
161 } // WrtDeviceApisCommon
163 #endif // WRTDEVICEAPIS_COMMONS_FUNCTION_DECLARARION_