Update wrt-plugins-common_0.3.60
[platform/framework/web/wrt-plugins-common.git] / src / Commons / WrtAccess / WrtAccess.cpp
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  * @author      Grzegorz Krawczyk (g.krawczyk@samsung.com)
18  * @version     0.1
19  * @brief
20  */
21
22 #include <memory>
23 #include <sstream>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <sys/types.h>
27
28 #include <dpl/log/log.h>
29 #include <dpl/scoped_array.h>
30 #include <dpl/scoped_resource.h>
31 #include <dpl/assert.h>
32 #include <Commons/Exception.h>
33 #include "WrtAccess.h"
34 #include <ace_api_client.h>
35 #include <dpl/singleton_safe_impl.h>
36
37 #include "popup-runner.h"
38 IMPLEMENT_SAFE_SINGLETON(WrtDeviceApis::Commons::WrtAccess)
39
40 namespace {
41
42 /**
43  * Helper class - single parameter and its value
44  */
45 struct AceParam
46 {
47     const char *name;
48     const char *value;
49
50     AceParam():
51         name(NULL), value(NULL)
52     {}
53
54     AceParam(const char *name, const char *value):
55         name(name), value(value)
56     {}
57 };
58
59 /**
60  * Helper class - list of params for single dev cap
61  */
62 struct AceParamList
63 {
64     size_t    count;
65     AceParam* param;
66     AceParamList():
67         count(0),
68         param(NULL)
69     {}
70 };
71
72 struct DeviceCapParamPolicy
73 {
74     typedef AceParamList* Type;
75     static Type NullValue()
76     {
77         return NULL;
78     }
79     static void Destroy(Type ptr)
80     {
81         if (ptr) {
82             delete[] ptr->param;
83         }
84         delete[] ptr;
85     }
86 };
87
88 /**
89  * Helper class - modified ScopedArray for ace_param_list_t
90  */
91 class ScopedDeviceCapArray : public DPL::ScopedResource<DeviceCapParamPolicy>
92 {
93   public:
94     explicit ScopedDeviceCapArray(AceParamList *ptr =
95                 DeviceCapParamPolicy::NullValue()) :
96         DPL::ScopedResource<DeviceCapParamPolicy>(ptr)
97     {
98     }
99
100     AceParamList & operator [](std::ptrdiff_t k) const
101     {
102         Assert(this->m_value != DeviceCapParamPolicy::NullValue() &&
103                "Dereference of scoped NULL array!");
104         Assert(k >= 0 && "Negative array index");
105
106         return this->m_value[k];
107     }
108 };
109 } // namespace
110
111 namespace WrtDeviceApis {
112 namespace Commons {
113
114 WrtAccess::WrtAccess() :
115         m_initialized(false),
116         m_sessionId(GenerateSessionId())
117 {
118 }
119
120 WrtAccess::~WrtAccess()
121 {
122 }
123
124 WrtAccess::SessionId WrtAccess::GenerateSessionId()
125 {
126     const size_t SESSION_ID_LENGTH = 32;
127
128     std::ostringstream pid;
129     pid << static_cast<int> (getpid());
130
131     std::string session_id = pid.str();
132
133     session_id.reserve(session_id.length() + SESSION_ID_LENGTH);
134
135     for (size_t i = 0; i < SESSION_ID_LENGTH; ++i)
136     {
137         int c = random() % 16;
138
139         session_id +=  (c < 10 ?
140                         static_cast<char>('0' + c) :
141                         static_cast<char>('A' + c - 10));
142     }
143     return session_id;
144 }
145
146 void WrtAccess::initialize(int widgetId)
147 {
148     LogDebug("initialize");
149     if (widgetId < 0)
150     {
151         LogDebug("Invalid widget id");
152         Throw(Exception);
153     }
154     ace_return_t ret = ace_client_initialize(Wrt::Popup::run_popup);
155     Assert(ACE_OK == ret);
156     m_initialized = true;
157     m_widgetId = widgetId;
158 }
159
160 void WrtAccess::deinitialize(int /*widgetId*/)
161 {
162     LogDebug("deinitialize");
163     m_initialized = false;
164     ace_return_t ret = ace_client_shutdown();
165     Assert(ACE_OK == ret);
166 }
167
168 int WrtAccess::getWidgetId() const
169 {
170     return m_widgetId;
171 }
172
173 bool WrtAccess::checkAccessControl(const AceFunction& aceFunction) const
174 {
175     Assert(m_initialized && "WrtAccessSingleton needs to be initialized with"
176             "WidgetId during on_widget_start_callback in each plugin");
177     size_t deviceCount = aceFunction.deviceCapabilities.size();
178
179     DPL::ScopedArray <const char *> deviceScopedArray;
180     ScopedDeviceCapArray paramsScopedArray;
181
182     if (deviceCount) {
183         deviceScopedArray.Reset(new const char*[deviceCount]);
184         paramsScopedArray.Reset(new AceParamList[deviceCount]);
185
186         for (size_t i = 0; i < deviceCount; ++i) {
187             deviceScopedArray[i] =
188                 aceFunction.deviceCapabilities.at(i).devCapName.c_str();
189             paramsScopedArray[i].count =
190                 aceFunction.deviceCapabilities.at(i).devCapParams.size();
191
192             paramsScopedArray[i].param =
193                 new AceParam[paramsScopedArray[i].count];
194
195             for (size_t j = 0; j < paramsScopedArray[i].count; ++j) {
196                 paramsScopedArray[i].param[j].name =
197                     aceFunction.deviceCapabilities.at(i).
198                         devCapParams[j].name.c_str();
199                 paramsScopedArray[i].param[j].value =
200                     aceFunction.deviceCapabilities.at(i).
201                         devCapParams[j].value.c_str();
202             }
203         }
204     }
205
206     size_t featuresCount = aceFunction.features.size();
207
208     DPL::ScopedArray <const char*> featureScopedArray;
209     if (featuresCount) {
210         featureScopedArray.Reset(new const char*[featuresCount]);
211
212         for (size_t i = 0; i < featuresCount; ++i) {
213             featureScopedArray[i] =
214                 aceFunction.features.at(i).name.c_str();
215         }
216     }
217
218     LogDebug("constructing ACE request");
219
220     ace_request_t aceRequest;
221     aceRequest.session_id = const_cast<const ace_session_id_t>(m_sessionId.c_str());
222     aceRequest.widget_handle = getWidgetId();
223     aceRequest.feature_list.count = featuresCount;
224     aceRequest.feature_list.items = const_cast<ace_string_t*>(featureScopedArray.Get());
225     aceRequest.dev_cap_list.count = deviceCount;
226     aceRequest.dev_cap_list.items = new ace_dev_cap_t[deviceCount];
227
228     const char**  devCapNames = deviceScopedArray.Get();
229     AceParamList* paramList = paramsScopedArray.Get();
230
231     unsigned int i;
232     for (i = 0; i < deviceCount; ++i) {
233         aceRequest.dev_cap_list.items[i].name =
234                 const_cast<const ace_string_t>(devCapNames[i]);
235         aceRequest.dev_cap_list.items[i].param_list.count = paramList[i].count;
236         aceRequest.dev_cap_list.items[i].param_list.items =
237                 new ace_param_t[paramList[i].count];
238         unsigned int j;
239         for (j = 0; j < paramList[i].count; ++j) {
240             aceRequest.dev_cap_list.items[i].param_list.items[j].name =
241                     const_cast<ace_string_t>(paramList[i].param[j].name);
242             aceRequest.dev_cap_list.items[i].param_list.items[j].value =
243                     const_cast<ace_string_t>(paramList[i].param[j].value);
244         }
245     }
246
247     ace_bool_t aceCheckResult = ACE_FALSE;
248     ace_return_t ret = ace_check_access(&aceRequest, &aceCheckResult);
249
250     for (i = 0; i < deviceCount; ++i) {
251         delete [] aceRequest.dev_cap_list.items[i].param_list.items;
252     }
253     delete [] aceRequest.dev_cap_list.items;
254
255     if (ACE_OK != ret) {
256         LogError("Error in ace check: " << static_cast<int>(ret));
257         return false;
258     }
259     return ACE_TRUE == aceCheckResult;
260 }
261
262 }
263 } // WrtDeviceApisCommon