2 // Tizen Web Device API
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "JSSystemInfoProperty.h"
19 #include "JSSystemInfoStorageUnit.h"
26 namespace SystemInfo {
28 using namespace DeviceAPI::Common;
30 struct SystemInfoStorageUnitHolder {
31 SystemInfoStorageUnitPtr ptr;
35 const char* SYSTEMINFO_SYSTEMINFO_STORAGE_UNIT = "SystemInfoStorageUnit";
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";
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
56 NULL, //deleteProperty,
57 NULL, //getPropertyNames,
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 },
78 const JSClassDefinition* JSSystemInfoStorageUnit::getClassInfo()
83 JSClassRef JSSystemInfoStorageUnit::m_jsClassRef =
84 JSClassCreate(JSSystemInfoStorageUnit::getClassInfo());
86 JSClassRef DLL_EXPORT JSSystemInfoStorageUnit::getClassRef()
89 m_jsClassRef = JSClassCreate(&m_classInfo);
94 SystemInfoStorageUnitPtr JSSystemInfoStorageUnit::getPrivateObject(JSContextRef context,
97 if (!JSValueIsObjectOfClass(context, value, getClassRef())) {
98 LOGE("Type mismatch");
99 throw TypeMismatchException("Type mismatch");
102 JSObjectRef object = JSUtil::JSValueToObject(context, value);
103 SystemInfoStorageUnitHolder* priv =
104 static_cast<SystemInfoStorageUnitHolder*>(JSObjectGetPrivate(object));
106 LOGE("Holder is null");
107 throw UnknownException("Holder is null");
110 LOGE("Priv is null");
111 throw UnknownException("Priv is null");
116 void JSSystemInfoStorageUnit::setPrivateObject(JSObjectRef object,
117 SystemInfoStorageUnitPtr native)
119 SystemInfoStorageUnitHolder* priv =
120 static_cast<SystemInfoStorageUnitHolder*>(JSObjectGetPrivate(object));
122 LOGE("Holder is null");
123 throw UnknownException("Holder is null");
128 JSObjectRef JSSystemInfoStorageUnit::makeJSObject(JSContextRef context,
129 SystemInfoStorageUnitPtr native)
132 LOGE("NULL pointer to SystemInfoStorageUnit given");
133 throw UnknownException("NULL pointer to SystemInfoStorageUnit given");
136 SystemInfoStorageUnitHolder* priv = new(std::nothrow) SystemInfoStorageUnitHolder();
138 LOGE("Failed to allocate memory");
139 throw UnknownException("Failed to allocate memory");
143 JSObjectRef obj = JSObjectMake(context, getClassRef(),
144 static_cast<void*>(priv));
148 void JSSystemInfoStorageUnit::initialize(JSContextRef context, JSObjectRef object)
153 void JSSystemInfoStorageUnit::finalize(JSObjectRef object)
156 SystemInfoStorageUnitHolder* priv =
157 static_cast<SystemInfoStorageUnitHolder*>(JSObjectGetPrivate(object));
159 JSObjectSetPrivate(object, NULL);
165 JSObjectRef JSSystemInfoStorageUnit::unitsArrayToJSObjectArray(JSContextRef context,
166 const UnitsPtrVector& units) {
168 size_t count = units.size();
170 JSObjectRef array[count];
171 for (size_t i = 0; i < count; ++i) {
172 array[i] = JSSystemInfoStorageUnit::makeJSObject(context, units[i]);
174 JSObjectRef result = JSObjectMakeArray(context, count,
175 count > 0 ? array : NULL, NULL);
177 LOGW("Failed to create SystemInfoStorageUnit array");
178 throw UnknownException("SystemInfoStorageUnit array is null");
184 JSValueRef JSSystemInfoStorageUnit::getType(JSContextRef context,
186 JSStringRef property_name,
187 JSValueRef* exception)
191 SystemInfoStorageUnitPtr priv = getPrivateObject(context, object);
193 return JSUtil::toJSValueRef(context, SystemInfoStorageUnit::typeToString(priv->getType()));
195 catch (const BasePlatformException &err) {
196 LOGE("Exception caught: name: %s, msg: %s",
197 err.getName().c_str(), err.getMessage().c_str());
200 LOGE("getType failed");
202 return JSValueMakeUndefined(context);
205 JSValueRef JSSystemInfoStorageUnit::getCapacity(JSContextRef context,
207 JSStringRef property_name,
208 JSValueRef* exception)
212 SystemInfoStorageUnitPtr priv = getPrivateObject(context, object);
214 return JSUtil::toJSValueRef(context, priv->getCapacity());
216 catch (const BasePlatformException &err) {
217 LOGE("Exception caught: name: %s, msg: %s",
218 err.getName().c_str(), err.getMessage().c_str());
221 LOGE("getCapacity failed");
223 return JSValueMakeUndefined(context);
226 JSValueRef JSSystemInfoStorageUnit::getAvailableCapacity(JSContextRef context,
228 JSStringRef property_name,
229 JSValueRef* exception)
233 SystemInfoStorageUnitPtr priv = getPrivateObject(context, object);
235 return JSUtil::toJSValueRef(context, priv->getAvailableCapacity());
237 catch (const BasePlatformException &err) {
238 LOGE("Exception caught: name: %s, msg: %s",
239 err.getName().c_str(), err.getMessage().c_str());
242 LOGE("getAvailableCapacity failed");
244 return JSValueMakeUndefined(context);
247 JSValueRef JSSystemInfoStorageUnit::isRemovable(JSContextRef context,
249 JSStringRef property_name,
250 JSValueRef* exception)
254 SystemInfoStorageUnitPtr priv = getPrivateObject(context, object);
256 return JSUtil::toJSValueRef(context, priv->isRemovable());
258 catch (const BasePlatformException &err) {
259 LOGE("Exception caught: name: %s, msg: %s",
260 err.getName().c_str(), err.getMessage().c_str());
263 LOGE("isRemovable failed");
265 return JSValueMakeUndefined(context);