Update change log and spec for wrt-plugins-tizen_0.4.38
[framework/web/wrt-plugins-tizen.git] / src / Filesystem / JSStorage.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
19 #include <cassert>
20 #include <memory>
21 #include <CommonsJavaScript/JSUtils.h>
22 //#include <CommonsJavaScript/Converter.h>
23 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
24 #include <Export.h>
25 #include "StorageProperties.h"
26 #include "JSStorage.h"
27 #include "Converter.h"
28 #include <Logger.h>
29
30 namespace DeviceAPI {
31 namespace Filesystem {
32
33 using namespace WrtDeviceApis;
34 using namespace WrtDeviceApis::Commons;
35 using namespace WrtDeviceApis::CommonsJavaScript;
36
37 namespace {
38 const char* STR_STORAGE_LABEL = "label";
39 const char* STR_STORAGE_TYPE = "type";
40 const char* STR_STORAGE_STATE = "state";
41 /*const char* STR_TYPE_INTERNAL = "INTERNAL";
42 const char* STR_TYPE_EXTERNAL = "EXTERNAL";
43 const char* STR_STATE_MOUNTED = "MOUNTED";
44 const char* STR_STATE_REMOVED = "REMOVED";
45 const char* STR_STATE_UNMOUNTABLE = "UNMOUNTABLE";*/
46 } //private namespace
47
48 JSClassDefinition JSStorage::m_classInfo = {
49         0,
50         kJSClassAttributeNone,
51         "FileSystemStorage",
52         0,
53         m_property,
54         0,
55         initialize,
56         finalize,
57         NULL,     //HasProperty,
58         getProperty,
59         NULL,     //SetProperty,
60         NULL,     //DeleteProperty,
61         NULL,     //GetPropertyNames,
62         NULL,     //CallAsFunction,
63         NULL,     //CallAsConstructor,
64         hasInstance,
65         NULL,     //ConvertToType
66 };
67
68 JSStaticValue JSStorage::m_property[] = {
69         { STR_STORAGE_LABEL, getProperty, NULL, kJSPropertyAttributeReadOnly },
70         { STR_STORAGE_TYPE, getProperty, NULL, kJSPropertyAttributeReadOnly },
71         { STR_STORAGE_STATE, getProperty, NULL, kJSPropertyAttributeReadOnly },
72 /*      { STR_TYPE_INTERNAL, getProperty, NULL, kJSPropertyAttributeReadOnly },
73         { STR_TYPE_EXTERNAL, getProperty, NULL, kJSPropertyAttributeReadOnly },
74         { STR_STATE_MOUNTED, getProperty, NULL, kJSPropertyAttributeReadOnly },
75         { STR_STATE_REMOVED, getProperty, NULL, kJSPropertyAttributeReadOnly },
76         { STR_STATE_UNMOUNTABLE, getProperty, NULL, kJSPropertyAttributeReadOnly },*/
77         { 0, 0, 0, 0 }
78 };
79
80 const JSClassRef DLL_EXPORT JSStorage::getClassRef()
81 {
82         if (!m_jsClassRef) {
83                 m_jsClassRef = JSClassCreate(&m_classInfo);
84         }
85         return m_jsClassRef;
86 }
87
88 const JSClassDefinition* JSStorage::getClassInfo()
89 {
90         return &m_classInfo;
91 }
92
93 JSClassRef JSStorage::m_jsClassRef = JSClassCreate(
94                 JSStorage::getClassInfo());
95
96 JSObjectRef JSStorage::createJSObject(JSContextRef context,
97                 const StorageProperties &storages)
98 {
99         std::auto_ptr<StorageProperties> storageProps(new StorageProperties());
100
101         storageProps->setLabel(storages.getLabel());
102         storageProps->setType(storages.getType());
103         storageProps->setState(storages.getState());
104
105         JSStoragePriv *priv = new JSStoragePriv(context, storageProps.get());
106         storageProps.release();
107         if (!priv) {
108                 ThrowMsg(Commons::NullPointerException, "Can not new an object");
109         }
110         return JSObjectMake(context, getClassRef(), priv);
111 }
112
113 void JSStorage::initialize(JSContextRef context,
114                 JSObjectRef object)
115 {
116 }
117
118 void JSStorage::finalize(JSObjectRef object)
119 {
120         JSStoragePriv* priv = static_cast<JSStoragePriv*>(JSObjectGetPrivate(object));
121         JSObjectSetPrivate(object, NULL);
122         delete priv;
123 }
124
125 JSValueRef JSStorage::getProperty(JSContextRef context,
126                 JSObjectRef object,
127                 JSStringRef propertyName,
128                 JSValueRef* exception)
129 {
130         JSStoragePriv *priv = static_cast<JSStoragePriv*>(JSObjectGetPrivate(object));
131
132         Try {
133                 if (!priv) {
134                         ThrowMsg(Commons::PlatformException, "Can not new an object");
135                 }
136                 StorageProperties *storages = priv->getObject();
137                 Converter convert(context);
138
139                 if (JSStringIsEqualToUTF8CString(propertyName, STR_STORAGE_LABEL)) {
140                         return convert.toJSValueRef(storages->getLabel());
141                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_STORAGE_TYPE)) {
142                         return convert.toStorageType(storages->getType());
143                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_STORAGE_STATE)) {
144                         return convert.toStorageState(storages->getState());
145                 } 
146                 /*else if (JSStringIsEqualToUTF8CString(propertyName, STR_TYPE_INTERNAL)) {
147                         return convert.toJSValueRef(TYPE_INTERNAL);
148                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_TYPE_EXTERNAL)) {
149                         return convert.toJSValueRef(TYPE_EXTERNAL);
150                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_STATE_MOUNTED)) {
151                         return convert.toJSValueRef(STATE_MOUNTED);
152                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_STATE_REMOVED)) {
153                         return convert.toJSValueRef(STATE_REMOVED);
154                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_STATE_UNMOUNTABLE)) {
155                         return convert.toJSValueRef(STATE_UNMOUNTABLE);
156                 }*/
157         } Catch(Commons::Exception) {
158                 LoggerW("trying to get incorrect value");
159         }
160         return JSValueMakeUndefined(context);
161 }
162
163 bool JSStorage::hasInstance(JSContextRef context,
164                 JSObjectRef constructor,
165                 JSValueRef possibleInstance,
166                 JSValueRef* exception)
167 {
168         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
169 }
170 }
171 }
172