tizen 2.4 release
[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/log/secure_log.h>
30 #include <dpl/scoped_resource.h>
31 #include <dpl/assert.h>
32 #include <Commons/Exception.h>
33 #include "WrtAccess.h"
34 #include <dpl/singleton_safe_impl.h>
35
36 IMPLEMENT_SAFE_SINGLETON(WrtDeviceApis::Commons::WrtAccess)
37
38 namespace {
39 /**
40  * Helper class - single parameter and its value
41  */
42 struct AceParam
43 {
44     const char *name;
45     const char *value;
46
47     AceParam() :
48         name(NULL), value(NULL)
49     {}
50
51     AceParam(const char *name, const char *value) :
52         name(name), value(value)
53     {}
54 };
55
56 /**
57  * Helper class - list of params for single dev cap
58  */
59 struct AceParamList
60 {
61     size_t count;
62     AceParam* param;
63     AceParamList() :
64         count(0),
65         param(NULL)
66     {}
67 };
68
69 struct DeviceCapParamPolicy
70 {
71     typedef AceParamList* Type;
72     static Type NullValue()
73     {
74         return NULL;
75     }
76     static void Destroy(Type ptr)
77     {
78         if (ptr) {
79             delete[] ptr->param;
80         }
81         delete[] ptr;
82     }
83 };
84
85 /**
86  * Helper class - modified ScopedArray for ace_param_list_t
87  */
88 class ScopedDeviceCapArray : public DPL::ScopedResource<DeviceCapParamPolicy>
89 {
90   public:
91     explicit ScopedDeviceCapArray(AceParamList *ptr =
92                                       DeviceCapParamPolicy::NullValue()) :
93         DPL::ScopedResource<DeviceCapParamPolicy>(ptr)
94     {}
95
96     AceParamList & operator [](std::ptrdiff_t k) const
97     {
98         AssertMsg(this->m_value != DeviceCapParamPolicy::NullValue(),
99                "Dereference of scoped NULL array!");
100         AssertMsg(k >= 0, "Negative array index");
101
102         return this->m_value[k];
103     }
104 };
105 } // namespace
106
107 namespace WrtDeviceApis {
108 namespace Commons {
109 WrtAccess::WrtAccess() :
110     m_widgetId(-1),
111     m_sessionId(GenerateSessionId())
112 {}
113
114 WrtAccess::~WrtAccess()
115 {}
116
117 WrtAccess::SessionId WrtAccess::GenerateSessionId()
118 {
119     const size_t SESSION_ID_LENGTH = 32;
120
121     std::ostringstream pid;
122     pid << static_cast<int>(getpid());
123
124     std::string session_id = pid.str();
125
126     session_id.reserve(session_id.length() + SESSION_ID_LENGTH);
127
128     for (size_t i = 0; i < SESSION_ID_LENGTH; ++i) {
129         int c = random() % 16;
130
131         session_id += (c < 10 ?
132                        static_cast<char>('0' + c) :
133                        static_cast<char>('A' + c - 10));
134     }
135     return session_id;
136 }
137
138 void WrtAccess::initialize(WidgetHandle widgetId)
139 {
140     _D("initialize");
141     if (widgetId < 0) {
142         _E("Invalid widget id");
143         Throw(Exception);
144     }
145
146     m_widgetId = widgetId;
147 }
148
149 void WrtAccess::deinitialize(WidgetHandle /*widgetId*/)
150 {
151     _D("deinitialize");
152 }
153
154 WidgetHandle WrtAccess::getWidgetId() const
155 {
156     return m_widgetId;
157 }
158 }
159 } // WrtDeviceApisCommon