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