Update wrt-plugins-common_0.3.53
[framework/web/wrt-plugins-common.git] / src / CommonsJavaScript / Security / StaticDeclaration.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
17 #ifndef _WRTPLUGINS_COMMONS_SRC_COMMONS_STATICDECLARATIOS_H_
18 #define _WRTPLUGINS_COMMONS_SRC_COMMONS_STATICDECLARATIOS_H_
19
20 #include <map>
21 #include <vector>
22 #include <string>
23 #include <string.h>
24 #include <dpl/noncopyable.h>
25 #include <dpl/assert.h>
26 #include <dpl/foreach.h>
27 #include <Commons/TypesDeclaration.h>
28 #include <wrt-commons/wrt_plugin_export.h>
29
30 namespace WrtDeviceApis {
31 namespace CommonsJavaScript {
32
33 template<typename ParamType,
34          typename DeviceCapType,
35          typename FunctionType>
36 class StaticDeclarations  : public DPL::Noncopyable
37 {
38     struct FunctionTriplet {
39         const char* name;
40         std::vector<DeviceCapType> devCaps;
41         std::vector<const char*> features;
42     };
43   public:
44
45
46     typedef typename std::map<ParamType, const char*>  ParamsMap;
47
48     typedef std::map<DeviceCapType,
49                std::pair<const char*,
50                          std::vector<ParamType> > >  DeviceCapsMaps;
51
52     typedef std::map<FunctionType, FunctionTriplet >  FunctionsMap;
53
54     typedef std::map<FunctionType, WrtDeviceApis::Commons::AceFunction>
55         AceFunctionsMap;
56
57     typedef std::map<std::string, std::vector<DeviceCapType> > FeaturesMap;
58
59     static const std::string getParamName(const ParamType& paramId)
60     {
61         auto it = m_params.find(paramId);
62
63         Assert(it != m_params.end() && "No such paramId");
64
65         return it->second;
66     }
67
68     /**
69      * Returns set of device capabilities WITHOUT params
70      * for given device capability id
71      * */
72     static WrtDeviceApis::Commons::AceDeviceCapability
73                 getDeviceCapabilityWithoutParams(const DeviceCapType& devCapsId)
74     {
75         WrtDeviceApis::Commons::AceDeviceCapability deviceCap;
76         auto it = m_deviceCaps.find(devCapsId);
77
78         Assert(it != m_deviceCaps.end() && "No such device cap");
79
80         deviceCap.devCapName = it->second.first;
81
82         return deviceCap;
83     }
84
85     /**
86      * Returns set of device capabilities with set params
87      * for given device capability id
88      * */
89     static WrtDeviceApis::Commons::AceDeviceCapability
90                 getDeviceCapability(const DeviceCapType& devCapsId)
91     {
92         auto it = m_deviceCaps.find(devCapsId);
93
94         Assert(it != m_deviceCaps.end() && "No such dev-cap found");
95
96         WrtDeviceApis::Commons::AceDeviceCapability deviceCap;
97         deviceCap.devCapName = it->second.first;
98
99         FOREACH(paramIt, it->second.second)
100         {
101             WrtDeviceApis::Commons::AceDeviceCapParam param(
102                 getParamName(*paramIt),
103                 std::string());
104
105             deviceCap.devCapParams.push_back(param);
106         }
107
108         return deviceCap;
109     }
110
111     static void addDeviceCapabilty(
112                 const DeviceCapType& devCapsId,
113                 WrtDeviceApis::Commons::AceFunction& aceFunction)
114     {
115         aceFunction.deviceCapabilities.push_back(
116                 getDeviceCapability(devCapsId));
117     }
118
119     /**
120      * Returns names of device-capabilities base on capability id
121      */
122     static std::string getDevCapNameById(DeviceCapType devCapId)
123     {
124         auto it = m_deviceCaps.find(devCapId);
125         Assert(it != m_deviceCaps.end() && "No such devcapid found!");
126         return it->second.first;
127     }
128
129     /**
130      * Sets parameter value for given paramId
131      */
132     static bool setParamValue(WrtDeviceApis::Commons::AceFunction& function,
133                               ParamType paramId,
134                               DeviceCapType devCapId,
135                               const std::string& value)
136     {
137         //get name of the deviceCaps
138         std::string devCapName =  getDevCapNameById(devCapId) ;
139         std::string paramName = getParamName(paramId);
140
141         //search throw all the device capabilities
142         FOREACH(devCapIt, function.deviceCapabilities) {
143             if(devCapIt->devCapName == devCapName) {
144                 //device capability has been found
145                 //check params
146                 FOREACH(devParamIt, devCapIt->devCapParams) {
147                     if(devParamIt->name == paramName) {
148                         devParamIt->value = value;
149                         return true;
150                     }
151                 }
152             }
153         }
154         return false;
155     }
156     /**
157      * Return struct Commons::AceFunction with set function name
158      *
159      * To set device capabilities you may use setDeviceCap function
160      * To set param value function you may use setParamValue Function
161      * */
162     static WrtDeviceApis::Commons::AceFunction getEmptyFunction(
163                 const FunctionType& functionId)
164     {
165         WrtDeviceApis::Commons::AceFunction function;
166         auto it = m_functions.find(functionId);
167         Assert(it != m_functions.end() && "No such a function");
168         function.name = it->second.first;
169
170         return function;
171     };
172
173     /**
174      * The most useful Function
175      * Return Commons::AceFunction with filled all required fields:
176      * name, device caps and proper param namespace
177      *
178      * To set param value function you may use setParamValue function
179      * */
180     static WrtDeviceApis::Commons::AceFunction getSecurityFunction(
181                 const FunctionType& functionId)
182     {
183         WrtDeviceApis::Commons::AceFunction function;
184         auto it = m_functions.find(functionId);
185         Assert(it != m_functions.end() && "No such function found!");
186
187         function.name = it->second.name;
188
189         FOREACH (featIt, it->second.features)
190             function.features.push_back(std::string(*featIt));
191
192         FOREACH(devCapIt, it->second.devCaps) {
193             function.deviceCapabilities.push_back(
194                         getDeviceCapability(*devCapIt));
195         }
196         return function;
197     };
198
199     /**
200      * To create static map
201      * */
202     static void createStaticAceFunctions()
203     {
204         FOREACH(functionIt, m_functions)
205         {
206             m_aceFunctions[functionIt->first] =
207                     getSecurityFunction(functionIt->first);
208         }
209     }
210
211     static feature_mapping_t* getFeaturesToDevCapMapping()
212     {
213         feature_mapping_t* mapping = new feature_mapping_t;
214
215         mapping->featuresCount = m_features.size();
216         mapping->features = new feature_devcaps_t[mapping->featuresCount];
217
218         size_t i = 0;
219
220         FOREACH(featureIt, m_features)
221         {
222             mapping->features[i].feature_name =
223                 strndup(featureIt->first.c_str(), featureIt->first.size() + 1);
224
225             mapping->features[i].devCaps.devCapsCount =
226                     featureIt->second.size();
227
228             mapping->features[i].devCaps.deviceCaps =
229                     new char*[mapping->features[i].devCaps.devCapsCount];
230
231             for (size_t j = 0;
232                  j < mapping->features[i].devCaps.devCapsCount;
233                  ++j)
234             {
235                 std::string dc = getDevCapNameById(featureIt->second[j]);
236
237                 mapping->features[i].devCaps.deviceCaps[j] =
238                     strndup(dc.c_str(), dc.size() + 1);
239             }
240
241             ++i;
242         }
243
244         return mapping;
245     }
246
247     static const devcaps_t* devcapsGetter(pfeature_mapping_t feats,
248                                           const char* featureName)
249     {
250         Assert(featureName && "Trying to extract info about NULL api feature");
251
252         std::string feature(featureName);
253
254         devcaps_t* ret = NULL;
255
256         for (size_t i = 0; i < feats->featuresCount ; ++i)
257         {
258             Assert(feats->features &&
259                    feats->features[i].feature_name &&
260                    "NULL api feature in feature mapping");
261
262             std::string feat(feats->features[i].feature_name);
263
264             if (feature == feat)
265             {
266                 ret = &(feats->features[i].devCaps);
267                 break;
268             }
269         }
270
271         return ret;
272     }
273
274     static void featuresDeinitializer(feature_mapping_t* mapping)
275     {
276         if (mapping)
277         {
278             if (mapping->features)
279             {
280                 for (size_t i = 0; i < mapping->featuresCount; ++i)
281                 {
282                     free(mapping->features[i].feature_name);
283
284                     devcaps_t& dc = mapping->features[i].devCaps;
285
286                     if (dc.deviceCaps)
287                     {
288                         for (size_t j = 0; j < dc.devCapsCount; ++j)
289                         {
290                             free(dc.deviceCaps[j]);
291                         }
292
293                         delete []dc.deviceCaps;
294                     }
295                 }
296                 delete []mapping->features;
297             }
298             delete mapping;
299         }
300     }
301
302     static void getMappingInterface(feature_mapping_interface_t *mapping)
303     {
304         if (mapping)
305         {
306             mapping->featGetter =
307                     StaticDeclarations::getFeaturesToDevCapMapping;
308             mapping->dcGetter = StaticDeclarations::devcapsGetter;
309             mapping->release = StaticDeclarations::featuresDeinitializer;
310         }
311     }
312
313 private:
314     static ParamsMap m_params;
315     static DeviceCapsMaps m_deviceCaps;
316     static FunctionsMap m_functions;
317     static AceFunctionsMap m_aceFunctions;
318
319     static FeaturesMap m_features;
320 };
321
322 }
323 }
324
325 #endif