tizen beta release
[platform/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 <dpl/noncopyable.h>
24 #include <dpl/assert.h>
25 #include <dpl/foreach.h>
26 #include <Commons/TypesDeclaration.h>
27
28
29 namespace WrtDeviceApis {
30 namespace CommonsJavaScript {
31
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   public:
43
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 private:
211     static ParamsMap m_params;
212     static DeviceCapsMaps m_deviceCaps;
213     static FunctionsMap m_functions;
214     static AceFunctionsMap m_aceFunctions;
215
216     static FeaturesMap m_features;
217 };
218
219 }
220 }
221
222 #endif