5fa776d2344a72a513f56d4ba07256b1f27b239a
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Filesystem / JSStorage.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. 
15  */
16
17
18 #include <cassert>
19 #include <memory>
20 #include <dpl/log/log.h>
21 #include <CommonsJavaScript/JSUtils.h>
22 #include <CommonsJavaScript/Converter.h>
23 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
24 #include <API/Filesystem/StorageProperties.h>
25 #include "JSStorage.h"
26
27 namespace TizenApis {
28 namespace Tizen1_0 {
29
30 using namespace WrtDeviceApis;
31 using namespace WrtDeviceApis::Commons;
32 using namespace WrtDeviceApis::CommonsJavaScript;
33 using namespace Api::Filesystem;
34
35 namespace {
36 const char* STR_STORAGE_LABEL = "label";
37 const char* STR_STORAGE_TYPE = "type";
38 const char* STR_STORAGE_STATE = "state";
39 const char* STR_TYPE_INTERNAL = "TYPE_INTERNAL";
40 const char* STR_TYPE_EXTERNAL = "TYPE_EXTERNAL";
41 const char* STR_STATE_MOUNTED = "STATE_MOUNTED";
42 const char* STR_STATE_REMOVED = "STATE_REMOVED";
43 const char* STR_STATE_UNMOUNTABLE = "STATE_UNMOUNTABLE";
44 } //private namespace
45
46 JSClassDefinition JSStorage::m_classInfo = {
47         0,
48         kJSClassAttributeNone,
49         "FileSystemStorage",
50         0,
51         m_property,
52         0,
53         initialize,
54         finalize,
55         NULL,     //HasProperty,
56         getProperty,
57         NULL,     //SetProperty,
58         NULL,     //DeleteProperty,
59         NULL,     //GetPropertyNames,
60         NULL,     //CallAsFunction,
61         NULL,     //CallAsConstructor,
62         hasInstance,
63         NULL,     //ConvertToType
64 };
65
66 JSStaticValue JSStorage::m_property[] = {
67         { STR_STORAGE_LABEL, getProperty, NULL, kJSPropertyAttributeReadOnly },
68         { STR_STORAGE_TYPE, getProperty, NULL, kJSPropertyAttributeReadOnly },
69         { STR_STORAGE_STATE, getProperty, NULL, kJSPropertyAttributeReadOnly },
70         { STR_TYPE_INTERNAL, getProperty, NULL, kJSPropertyAttributeReadOnly },
71         { STR_TYPE_EXTERNAL, getProperty, NULL, kJSPropertyAttributeReadOnly },
72         { STR_STATE_MOUNTED, getProperty, NULL, kJSPropertyAttributeReadOnly },
73         { STR_STATE_REMOVED, getProperty, NULL, kJSPropertyAttributeReadOnly },
74         { STR_STATE_UNMOUNTABLE, getProperty, NULL, kJSPropertyAttributeReadOnly },
75         { 0, 0, 0, 0 }
76 };
77
78 const JSClassRef JSStorage::getClassRef()
79 {
80         if (!m_jsClassRef) {
81                 m_jsClassRef = JSClassCreate(&m_classInfo);
82         }
83         return m_jsClassRef;
84 }
85
86 const JSClassDefinition* JSStorage::getClassInfo()
87 {
88         return &m_classInfo;
89 }
90
91 JSClassRef JSStorage::m_jsClassRef = JSClassCreate(
92                 JSStorage::getClassInfo());
93
94 JSObjectRef JSStorage::createJSObject(JSContextRef context,
95                 const StorageProperties &storages)
96 {
97         std::auto_ptr<StorageProperties> storageProps(new StorageProperties());
98
99         storageProps->setLabel(storages.getLabel());
100         storageProps->setType(storages.getType());
101         storageProps->setState(storages.getState());
102
103         JSStoragePriv *priv = new JSStoragePriv(context, storageProps.get());
104         storageProps.release();
105         if (!priv) {
106                 ThrowMsg(Commons::NullPointerException, "Can not new an object");
107         }
108         return JSObjectMake(context, getClassRef(), priv);
109 }
110
111 void JSStorage::initialize(JSContextRef context,
112                 JSObjectRef object)
113 {
114 }
115
116 void JSStorage::finalize(JSObjectRef object)
117 {
118         JSStoragePriv* priv = static_cast<JSStoragePriv*>(JSObjectGetPrivate(object));
119         JSObjectSetPrivate(object, NULL);
120         delete priv;
121 }
122
123 JSValueRef JSStorage::getProperty(JSContextRef context,
124                 JSObjectRef object,
125                 JSStringRef propertyName,
126                 JSValueRef* exception)
127 {
128         JSStoragePriv *priv = static_cast<JSStoragePriv*>(JSObjectGetPrivate(object));
129         assert(priv && "Private object not set.");
130
131         Try {
132                 StorageProperties *storages = priv->getObject();
133                 Converter convert(context);
134
135                 if (JSStringIsEqualToUTF8CString(propertyName, STR_STORAGE_LABEL)) {
136                         return convert.toJSValueRef(storages->getLabel());
137                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_STORAGE_TYPE)) {
138                         return convert.toJSValueRef(storages->getType());
139                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_STORAGE_STATE)) {
140                         return convert.toJSValueRef(storages->getState());
141                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_TYPE_INTERNAL)) {
142                         return convert.toJSValueRef(TYPE_INTERNAL);
143                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_TYPE_EXTERNAL)) {
144                         return convert.toJSValueRef(TYPE_EXTERNAL);
145                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_STATE_MOUNTED)) {
146                         return convert.toJSValueRef(STATE_MOUNTED);
147                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_STATE_REMOVED)) {
148                         return convert.toJSValueRef(STATE_REMOVED);
149                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_STATE_UNMOUNTABLE)) {
150                         return convert.toJSValueRef(STATE_UNMOUNTABLE);
151                 }
152         } Catch(Commons::Exception) {
153                 LogWarning("trying to get incorrect value");
154         }
155         return JSValueMakeUndefined(context);
156 }
157
158 bool JSStorage::hasInstance(JSContextRef context,
159                 JSObjectRef constructor,
160                 JSValueRef possibleInstance,
161                 JSValueRef* exception)
162 {
163         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
164 }
165 }
166 }
167