wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Systeminfo / JSStorageInfo.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 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 <memory>
19 #include "JSStorageInfo.h"
20 #include <Logger.h>
21
22 namespace DeviceAPI {
23 namespace Systeminfo {
24 using namespace WrtDeviceApis::CommonsJavaScript;
25 using namespace WrtDeviceApis::Commons;
26
27 namespace {
28 const char* STORAGE_TYPE_PROPERTY = "type";
29 const char* STORAGE_CAPACITY_PROPERTY = "capacity";
30 const char* STORAGE_AVAILCAPA_PROPERTY = "availableCapacity";
31 const char* STORAGE_REMOVEABLE_PROPERTY = "isRemoveable";
32 const char* STORAGE_REMOVABLE_PROPERTY = "isRemovable";
33
34
35 JSClassRef JSStorageInfo::m_classRef = NULL;
36
37 JSClassDefinition JSStorageInfo::m_classInfo = {
38     0,
39     kJSClassAttributeNone,
40     "storageinfo",
41     0,
42     m_properties,
43     NULL,
44     Initialize,
45     Finalize,
46     hasProperty,
47     getProperty,
48     NULL,
49     NULL,
50     NULL,
51     NULL,
52     NULL,
53     NULL,
54     NULL
55 };
56
57 JSStaticValue JSStorageInfo::m_properties[] = {
58     { STORAGE_TYPE_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },
59     { STORAGE_CAPACITY_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },
60     { STORAGE_AVAILCAPA_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },
61     { STORAGE_REMOVEABLE_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },
62     { STORAGE_REMOVABLE_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },
63     { 0, 0, 0, 0 }
64 };
65
66 const JSClassRef JSStorageInfo::getClassRef()
67 {
68     if (!m_classRef) {
69         m_classRef = JSClassCreate(&m_classInfo);
70     }
71     return m_classRef;
72 }
73
74 const JSClassDefinition* JSStorageInfo::getClassInfo()
75 {
76     return &m_classInfo;
77 }
78
79 void JSStorageInfo::Initialize(JSContextRef context, JSObjectRef object)
80 {
81 }
82
83 void JSStorageInfo::Finalize(JSObjectRef object)
84 {
85     LoggerD("Entered");
86     JSStoragePriv* priv = static_cast<JSStoragePriv*>(JSObjectGetPrivate(object));
87     JSObjectSetPrivate(object, NULL);
88     LoggerD("Deleting StorageInfo object");
89     delete priv;
90 }
91
92 bool JSStorageInfo::hasProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName)
93 {
94     return JSUtils::hasProperty(m_properties, propertyName);
95 }
96
97 JSObjectRef JSStorageInfo::createJSObject(JSContextRef context, const StoragePropertiesPtr storageInfo)
98 {
99     JSStoragePriv *priv = new JSStoragePriv(context, storageInfo);
100     return JSObjectMake(context, getClassRef(), priv);
101 }
102
103 JSValueRef JSStorageInfo::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
104 {
105     LoggerD("Enter");
106     JSStoragePriv *priv = static_cast<JSStoragePriv*>(JSObjectGetPrivate(object));
107     if (NULL == priv) {
108         LoggerE("Private object not set.");
109         return JSValueMakeUndefined(context);
110     }
111
112     Try
113     {
114         StoragePropertiesPtr storageInfo = priv->getObject();
115         Converter convert(context);
116         if (JSStringIsEqualToUTF8CString(propertyName, STORAGE_TYPE_PROPERTY)) {
117             return convert.toJSValueRef(storageInfo->type);
118         } else if (JSStringIsEqualToUTF8CString(propertyName, STORAGE_CAPACITY_PROPERTY)) {
119             return JSValueMakeNumber(context, storageInfo->capacity);
120         } else if (JSStringIsEqualToUTF8CString(propertyName, STORAGE_AVAILCAPA_PROPERTY)) {
121             return JSValueMakeNumber(context, storageInfo->availableCapacity);            
122         } else if (JSStringIsEqualToUTF8CString(propertyName, STORAGE_REMOVEABLE_PROPERTY)) {
123             return convert.toJSValueRef(storageInfo->isRemoveable);
124         } else if (JSStringIsEqualToUTF8CString(propertyName, STORAGE_REMOVABLE_PROPERTY)) {
125             return convert.toJSValueRef(storageInfo->isRemovable);
126         }
127     }
128     Catch(Exception)
129     {
130         LoggerE("Exception: " << _rethrown_exception.GetMessage());
131     }
132     return JSValueMakeUndefined(context);
133 }
134 }
135 }