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