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