tizen 2.3.1 release
[framework/web/wearable/wrt-plugins-tizen.git] / src / Systeminfo / JSSystemInfoStorageUnit.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include "JSSystemInfoProperty.h"
19 #include "JSSystemInfoStorageUnit.h"
20
21 #include <Logger.h>
22 #include <Export.h>
23 #include <JSUtil.h>
24
25 namespace DeviceAPI {
26 namespace SystemInfo {
27
28 using namespace DeviceAPI::Common;
29
30 struct SystemInfoStorageUnitHolder {
31     SystemInfoStorageUnitPtr ptr;
32 };
33
34 namespace {
35 const char* SYSTEMINFO_SYSTEMINFO_STORAGE_UNIT = "SystemInfoStorageUnit";
36
37 const char* SYSTEMINFO_STORAGE_UNIT_TYPE = "type";
38 const char* SYSTEMINFO_STORAGE_UNIT_CAPACITY = "capacity";
39 const char* SYSTEMINFO_STORAGE_UNIT_AVAILABLE_CAPACITY = "availableCapacity";
40 const char* SYSTEMINFO_STORAGE_UNIT_IS_REMOVABLE = "isRemovable";
41 const char* SYSTEMINFO_STORAGE_UNIT_IS_REMOVEABLE = "isRemoveable";
42 }
43
44 JSClassDefinition JSSystemInfoStorageUnit::m_classInfo = {
45         0, // current (and only) version is 0
46         kJSClassAttributeNone, //attributes
47         SYSTEMINFO_SYSTEMINFO_STORAGE_UNIT, //class name
48         JSSystemInfoProperty::getClassRef(), // parent class
49         m_property,
50         NULL, //m_function,
51         initialize,
52         finalize,
53         NULL, //hasProperty,
54         NULL, //getProperty,
55         NULL, //setProperty,
56         NULL, //deleteProperty,
57         NULL, //getPropertyNames,
58         NULL, //function,
59         NULL, //constructor,
60         NULL, //hasInstance,
61         NULL  //convertToType,
62 };
63
64 JSStaticValue JSSystemInfoStorageUnit::m_property[] = {
65         { SYSTEMINFO_STORAGE_UNIT_TYPE, getType, NULL,
66                 kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
67         { SYSTEMINFO_STORAGE_UNIT_CAPACITY, getCapacity, NULL,
68                  kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
69         { SYSTEMINFO_STORAGE_UNIT_AVAILABLE_CAPACITY, getAvailableCapacity, NULL,
70                   kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
71         { SYSTEMINFO_STORAGE_UNIT_IS_REMOVABLE, isRemovable, NULL,
72                   kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
73         { SYSTEMINFO_STORAGE_UNIT_IS_REMOVEABLE, isRemovable, NULL,
74                   kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
75         { 0, 0, 0, 0 }
76 };
77
78 const JSClassDefinition* JSSystemInfoStorageUnit::getClassInfo()
79 {
80     return &m_classInfo;
81 }
82
83 JSClassRef JSSystemInfoStorageUnit::m_jsClassRef =
84         JSClassCreate(JSSystemInfoStorageUnit::getClassInfo());
85
86 JSClassRef DLL_EXPORT JSSystemInfoStorageUnit::getClassRef()
87 {
88     if (!m_jsClassRef) {
89         m_jsClassRef = JSClassCreate(&m_classInfo);
90     }
91     return m_jsClassRef;
92 }
93
94 SystemInfoStorageUnitPtr JSSystemInfoStorageUnit::getPrivateObject(JSContextRef context,
95         JSValueRef value)
96 {
97     if (!JSValueIsObjectOfClass(context, value, getClassRef())) {
98         LOGE("Type mismatch");
99         throw TypeMismatchException("Type mismatch");
100     }
101
102     JSObjectRef object = JSUtil::JSValueToObject(context, value);
103     SystemInfoStorageUnitHolder* priv =
104             static_cast<SystemInfoStorageUnitHolder*>(JSObjectGetPrivate(object));
105     if (!priv) {
106         LOGE("Holder is null");
107         throw UnknownException("Holder is null");
108     }
109     if (!priv->ptr) {
110         LOGE("Priv is null");
111         throw UnknownException("Priv is null");
112     }
113     return priv->ptr;
114 }
115
116 void JSSystemInfoStorageUnit::setPrivateObject(JSObjectRef object,
117         SystemInfoStorageUnitPtr native)
118 {
119     SystemInfoStorageUnitHolder* priv =
120             static_cast<SystemInfoStorageUnitHolder*>(JSObjectGetPrivate(object));
121     if (!priv) {
122         LOGE("Holder is null");
123         throw UnknownException("Holder is null");
124     }
125     priv->ptr = native;
126 }
127
128 JSObjectRef JSSystemInfoStorageUnit::makeJSObject(JSContextRef context,
129         SystemInfoStorageUnitPtr native)
130 {
131     if (!native) {
132         LOGE("NULL pointer to SystemInfoStorageUnit given");
133         throw UnknownException("NULL pointer to SystemInfoStorageUnit given");
134     }
135
136     SystemInfoStorageUnitHolder* priv = new(std::nothrow) SystemInfoStorageUnitHolder();
137     if (!priv) {
138         LOGE("Failed to allocate memory");
139         throw UnknownException("Failed to allocate memory");
140     }
141     priv->ptr = native;
142
143     JSObjectRef obj = JSObjectMake(context, getClassRef(),
144             static_cast<void*>(priv));
145     return obj;
146 }
147
148 void JSSystemInfoStorageUnit::initialize(JSContextRef context, JSObjectRef object)
149 {
150     LOGD("Entered");
151 }
152
153 void JSSystemInfoStorageUnit::finalize(JSObjectRef object)
154 {
155     LOGD("Entered");
156     SystemInfoStorageUnitHolder* priv =
157             static_cast<SystemInfoStorageUnitHolder*>(JSObjectGetPrivate(object));
158     if (priv) {
159         JSObjectSetPrivate(object, NULL);
160         delete priv;
161         priv = NULL;
162     }
163 }
164
165 JSObjectRef JSSystemInfoStorageUnit::unitsArrayToJSObjectArray(JSContextRef context,
166             const UnitsPtrVector& units) {
167     LOGD("Entered");
168     size_t count = units.size();
169
170     JSObjectRef array[count];
171     for (size_t i = 0; i < count; ++i) {
172         array[i] = JSSystemInfoStorageUnit::makeJSObject(context, units[i]);
173     }
174     JSObjectRef result = JSObjectMakeArray(context, count,
175             count > 0 ? array : NULL, NULL);
176     if (!result) {
177         LOGW("Failed to create SystemInfoStorageUnit array");
178         throw UnknownException("SystemInfoStorageUnit array is null");
179     }
180     LOGD("return");
181     return result;
182 }
183
184 JSValueRef JSSystemInfoStorageUnit::getType(JSContextRef context,
185         JSObjectRef object,
186         JSStringRef property_name,
187         JSValueRef* exception)
188 {
189     LOGD("Entered");
190     try {
191         SystemInfoStorageUnitPtr priv = getPrivateObject(context, object);
192
193         return JSUtil::toJSValueRef(context, SystemInfoStorageUnit::typeToString(priv->getType()));
194     }
195     catch (const BasePlatformException &err) {
196         LOGE("Exception caught: name: %s, msg: %s",
197                 err.getName().c_str(), err.getMessage().c_str());
198     }
199     catch (...) {
200         LOGE("getType failed");
201     }
202     return JSValueMakeUndefined(context);
203 }
204
205 JSValueRef JSSystemInfoStorageUnit::getCapacity(JSContextRef context,
206         JSObjectRef object,
207         JSStringRef property_name,
208         JSValueRef* exception)
209 {
210     LOGD("Entered");
211     try {
212         SystemInfoStorageUnitPtr priv = getPrivateObject(context, object);
213
214         return JSUtil::toJSValueRef(context, priv->getCapacity());
215     }
216     catch (const BasePlatformException &err) {
217         LOGE("Exception caught: name: %s, msg: %s",
218                 err.getName().c_str(), err.getMessage().c_str());
219     }
220     catch (...) {
221         LOGE("getCapacity failed");
222     }
223     return JSValueMakeUndefined(context);
224 }
225
226 JSValueRef JSSystemInfoStorageUnit::getAvailableCapacity(JSContextRef context,
227         JSObjectRef object,
228         JSStringRef property_name,
229         JSValueRef* exception)
230 {
231     LOGD("Entered");
232     try {
233         SystemInfoStorageUnitPtr priv = getPrivateObject(context, object);
234
235         return JSUtil::toJSValueRef(context, priv->getAvailableCapacity());
236     }
237     catch (const BasePlatformException &err) {
238         LOGE("Exception caught: name: %s, msg: %s",
239                 err.getName().c_str(), err.getMessage().c_str());
240     }
241     catch (...) {
242         LOGE("getAvailableCapacity failed");
243     }
244     return JSValueMakeUndefined(context);
245 }
246
247 JSValueRef JSSystemInfoStorageUnit::isRemovable(JSContextRef context,
248         JSObjectRef object,
249         JSStringRef property_name,
250         JSValueRef* exception)
251 {
252     LOGD("Entered");
253     try {
254         SystemInfoStorageUnitPtr priv = getPrivateObject(context, object);
255
256         return JSUtil::toJSValueRef(context, priv->isRemovable());
257     }
258     catch (const BasePlatformException &err) {
259         LOGE("Exception caught: name: %s, msg: %s",
260                 err.getName().c_str(), err.getMessage().c_str());
261     }
262     catch (...) {
263         LOGE("isRemovable failed");
264     }
265     return JSValueMakeUndefined(context);
266 }
267
268 } // SystemInfo
269 } // DeviceAPI