upload tizen1.0 source
[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
24 #include <dpl/log/log.h>
25 #include <dpl/scoped_array.h>
26 #include <dpl/scoped_resource.h>
27 #include <dpl/assert.h>
28 #include <wrt_plugin_export.h>
29 #include <Commons/Exception.h>
30 #include "WrtAccess.h"
31 #include <ace-client/ace_client.h>
32 #include <dpl/singleton_impl.h>
33
34 IMPLEMENT_SINGLETON(WrtDeviceApis::Commons::WrtAccess)
35
36 namespace WrtDeviceApis {
37 namespace Commons {
38
39 struct DeviceCapParamPolicy
40 {
41     typedef AceClient::AceParamList* Type;
42     static Type NullValue()
43     {
44         return NULL;
45     }
46     static void Destroy(Type ptr)
47     {
48         if (ptr) {
49             delete[] ptr->param;
50         }
51         delete[] ptr;
52     }
53 };
54
55 /**
56  * Helper class - modified ScopedArray for ace_param_list_t
57  */
58 class ScopedDeviceCapArray : public DPL::ScopedResource<DeviceCapParamPolicy>
59 {
60   public:
61     explicit ScopedDeviceCapArray(AceClient::AceParamList *ptr =
62                 DeviceCapParamPolicy::NullValue()) :
63         DPL::ScopedResource<DeviceCapParamPolicy>(ptr)
64     {
65     }
66
67     AceClient::AceParamList & operator [](std::ptrdiff_t k) const
68     {
69         Assert(this->m_value != DeviceCapParamPolicy::NullValue() &&
70                "Dereference of scoped NULL array!");
71         Assert(k >= 0 && "Negative array index");
72
73         return this->m_value[k];
74     }
75 };
76
77 WrtAccess::WrtAccess()
78 {
79     DPL::Log::LogSystemSingleton::Instance().SetTag("WRT_PLUGINS");
80 }
81
82 WrtAccess::~WrtAccess()
83 {
84 }
85
86 void WrtAccess::initialize(int widgetId)
87 {
88     LogDebug("initialize");
89
90     if (widgetId < 0)
91     {
92         LogDebug("Invalid widget id");
93         Throw(Exception);
94     }
95
96     m_widgetId = widgetId;
97 }
98
99 void WrtAccess::deinitialize(int widgetId)
100 {
101     LogDebug("deinitialize");
102 }
103
104 int WrtAccess::getWidgetId() const
105 {
106     return m_widgetId;
107 }
108
109 bool WrtAccess::checkAccessControl(const AceFunction& aceFunction) const
110 {
111     size_t deviceCount = aceFunction.deviceCapabilities.size();
112
113     DPL::ScopedArray <const char *> deviceScopedArray;
114     ScopedDeviceCapArray paramsScopedArray;
115
116     if (deviceCount) {
117         deviceScopedArray.Reset(new const char*[deviceCount]);
118         paramsScopedArray.Reset(new AceClient::AceParamList[deviceCount]);
119
120         for (size_t i = 0; i < deviceCount; ++i) {
121             deviceScopedArray[i] =
122                 aceFunction.deviceCapabilities.at(i).devCapName.c_str();
123             paramsScopedArray[i].count =
124                 aceFunction.deviceCapabilities.at(i).devCapParams.size();
125
126             paramsScopedArray[i].param =
127                 new AceClient::AceParam[paramsScopedArray[i].count];
128
129             for (size_t j = 0; j < paramsScopedArray[i].count; ++j) {
130                 paramsScopedArray[i].param[j].name =
131                     aceFunction.deviceCapabilities.at(i).
132                         devCapParams[j].name.c_str();
133                 paramsScopedArray[i].param[j].value =
134                     aceFunction.deviceCapabilities.at(i).
135                         devCapParams[j].value.c_str();
136             }
137         }
138     }
139
140     size_t featuresCount = aceFunction.features.size();
141
142     DPL::ScopedArray <const char*> featureScopedArray;
143     if (featuresCount) {
144         featureScopedArray.Reset(new const char*[featuresCount]);
145
146         for (size_t i = 0; i < featuresCount; ++i) {
147             featureScopedArray[i] =
148                 aceFunction.features.at(i).name.c_str();
149         }
150     }
151
152     AceClient::AceRequest aceRequest;
153     aceRequest.sessionId = ""; // TODO for now empty session
154     aceRequest.widgetHandle = getWidgetId();
155     aceRequest.apiFeatures.count = featuresCount;
156     aceRequest.apiFeatures.apiFeature = featureScopedArray.Get();
157     aceRequest.functionName = aceFunction.name.c_str();
158     aceRequest.deviceCapabilities.devcapsCount = deviceCount;
159     aceRequest.deviceCapabilities.devCapNames = deviceScopedArray.Get();
160     aceRequest.deviceCapabilities.paramsCount = deviceCount;
161     aceRequest.deviceCapabilities.params = paramsScopedArray.Get();
162
163     return AceClient::AceThinClientSingleton::
164             Instance().checkFunctionCall(aceRequest);
165 }
166
167 }
168 } // WrtDeviceApisCommon