upload tizen1.0 source
[framework/web/wrt-plugins-common.git] / src / standards / W3C / Widget / JSStorageEvent.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  * @file        JSStorageEvent.cpp
19  * @author      Andrzej Surdej (a.surdej@samsung.com)
20  * @version     0.1
21  */
22
23 #include "JSStorageEvent.h"
24 #include <dpl/log/log.h>
25 #include <dpl/assert.h>
26 #include <StorageEvent/IStorageEvent.h>
27 #include <CommonsJavaScript/Converter.h>
28 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
29 #include "JSPreferences.h"
30
31 using namespace WrtDeviceApis;
32 using namespace WrtDeviceApis::Commons;
33 using namespace WrtDeviceApis::CommonsJavaScript;
34 using namespace WrtDeviceApis::StorageEvent::Api;
35
36 #define WIDGET_PLUGIN_NAME "StorageEvent"
37
38 #define WRT_STORAGE_EVENT_PROPERTY_KEY          "key"
39 #define WRT_STORAGE_EVENT_PROPERTY_OLD_VALUE    "oldValue"
40 #define WRT_STORAGE_EVENT_PROPERTY_NEW_VALUE    "newValue"
41 #define WRT_STORAGE_EVENT_PROPERTY_URL          "url"
42 #define WRT_STORAGE_EVENT_PROPERTY_STORAGE_AREA "storageArea"
43
44 #define CATCH_EXCEPTION_CONVERSION \
45     Catch(Commons::ConversionException) {\
46         LogError("Error on conversion");\
47         return JSDOMExceptionFactory::\
48             UnknownException.make(context, exception);\
49     }
50
51 #define CATCH_EXCEPTION_NULL_PTR \
52     Catch(Commons::NullPointerException) {\
53         LogError("Error on pointer, null value");\
54         return JSDOMExceptionFactory::\
55             UnknownException.make(context, exception);\
56     }
57
58 #define CATCH_EXCEPTION_PLATFORM_ERROR \
59     Catch(Commons::PlatformException){\
60         LogError("PlatformException occured");\
61         return JSDOMExceptionFactory::\
62             UnknownException.make(context, exception);\
63     }
64
65 #define CATCH_EXCEPTION_SECURITY \
66     Catch(Commons::SecurityException){\
67         LogError("Security exception occured");\
68         return JSDOMExceptionFactory::\
69             SecurityException.make(context, exception);\
70     }
71
72 namespace WrtPlugins {
73 namespace W3C {
74 JSClassDefinition JSStorageEvent::m_classInfo = {
75     0,
76     kJSClassAttributeNone,
77     WIDGET_PLUGIN_NAME,
78     0,
79     m_property,
80     NULL,
81     initialize,
82     finalize,
83     NULL, //HasProperty,
84     NULL, //GetProperty,
85     NULL, //SetProperty,
86     NULL, //DeleteProperty,
87     NULL, //GetPropertyNames,
88     NULL, //CallAsFunction,
89     NULL, //CallAsConstructor,
90     NULL, //HasInstance,
91     NULL, //ConvertToType,
92 };
93
94 JSStaticValue JSStorageEvent::m_property[] = {
95     { WRT_STORAGE_EVENT_PROPERTY_KEY, JSStorageEvent::getKey,
96       0, kJSPropertyAttributeReadOnly },
97     { WRT_STORAGE_EVENT_PROPERTY_OLD_VALUE, JSStorageEvent::getOldValue,
98       0, kJSPropertyAttributeReadOnly },
99     { WRT_STORAGE_EVENT_PROPERTY_NEW_VALUE, JSStorageEvent::getNewValue,
100       0, kJSPropertyAttributeReadOnly },
101     { WRT_STORAGE_EVENT_PROPERTY_URL, JSStorageEvent::getUrl,
102       0, kJSPropertyAttributeReadOnly },
103     { WRT_STORAGE_EVENT_PROPERTY_STORAGE_AREA, JSStorageEvent::getStorageArea,
104       0, kJSPropertyAttributeReadOnly },
105     { 0, 0, 0, 0 }
106 };
107
108 const JSClassRef JSStorageEvent::getClassRef()
109 {
110     if (!m_jsClassRef) {
111         m_jsClassRef = JSClassCreate(&m_classInfo);
112     }
113     return m_jsClassRef;
114 }
115
116 const JSClassDefinition* JSStorageEvent::getClassInfo()
117 {
118     return &m_classInfo;
119 }
120
121 JSClassRef JSStorageEvent::m_jsClassRef = JSClassCreate(JSStorageEvent::getClassInfo());
122
123 void JSStorageEvent::initialize(JSContextRef context,
124                                 JSObjectRef object)
125 {
126     LogDebug("entered");
127
128     JSStorageEventPrivateObject* priv =
129         static_cast<JSStorageEventPrivateObject*>(JSObjectGetPrivate(object));
130
131     Assert(priv && "Missing private object");
132 }
133
134 void JSStorageEvent::finalize(JSObjectRef object)
135 {
136     LogDebug("entered");
137     JSStorageEventPrivateObject* priv =
138         static_cast<JSStorageEventPrivateObject*>(JSObjectGetPrivate(object));
139
140     delete priv;
141     LogDebug("private object is realised");
142
143 }
144
145 JSValueRef JSStorageEvent::getKey(
146         JSContextRef context,
147         JSObjectRef object,
148         JSStringRef propertyName,
149         JSValueRef* exception)
150 {
151     LogDebug("entered");
152
153     Try
154     {
155         Converter converter(context);
156
157         return converter.toJSValueRef(getPrivateObject(object)->getKey());
158
159     }
160     CATCH_EXCEPTION_CONVERSION
161     CATCH_EXCEPTION_NULL_PTR
162     CATCH_EXCEPTION_PLATFORM_ERROR
163     CATCH_EXCEPTION_SECURITY
164 }
165
166 JSValueRef JSStorageEvent::getOldValue(
167         JSContextRef context,
168         JSObjectRef object,
169         JSStringRef propertyName,
170         JSValueRef* exception)
171 {
172     LogDebug("entered");
173
174     Try
175     {
176         Converter converter(context);
177
178         DPL::OptionalString oldValue = getPrivateObject(object)->getOldValue();
179
180         if(!oldValue) {
181             return JSValueMakeNull(context);
182         } else {
183             return converter.toJSValueRef(DPL::ToUTF8String(*oldValue));
184         }
185
186     }
187     CATCH_EXCEPTION_CONVERSION
188     CATCH_EXCEPTION_NULL_PTR
189     CATCH_EXCEPTION_PLATFORM_ERROR
190     CATCH_EXCEPTION_SECURITY
191 }
192
193 JSValueRef JSStorageEvent::getNewValue(
194         JSContextRef context,
195         JSObjectRef object,
196         JSStringRef propertyName,
197         JSValueRef* exception)
198 {
199     LogDebug("entered");
200
201     Try
202     {
203         Converter converter(context);
204
205         DPL::OptionalString newValue = getPrivateObject(object)->getNewValue();
206
207         if(!newValue) {
208             return JSValueMakeNull(context);
209         } else {
210             return converter.toJSValueRef(DPL::ToUTF8String(*newValue));
211         }
212
213     }
214     CATCH_EXCEPTION_CONVERSION
215     CATCH_EXCEPTION_NULL_PTR
216     CATCH_EXCEPTION_PLATFORM_ERROR
217     CATCH_EXCEPTION_SECURITY
218 }
219
220 JSValueRef JSStorageEvent::getUrl(
221         JSContextRef context,
222         JSObjectRef object,
223         JSStringRef propertyName,
224         JSValueRef* exception)
225 {
226     LogDebug("entered");
227
228     Try
229     {
230         Converter converter(context);
231
232         return converter.toJSValueRef(getPrivateObject(object)->getUrl());
233
234     }
235     CATCH_EXCEPTION_CONVERSION
236     CATCH_EXCEPTION_NULL_PTR
237 }
238
239 JSValueRef JSStorageEvent::getStorageArea(
240         JSContextRef context,
241         JSObjectRef object,
242         JSStringRef propertyName,
243         JSValueRef* exception)
244 {
245     LogDebug("entered");
246
247     Try
248     {
249         LogError("Not implemented");
250         return JSValueMakeUndefined(context);
251     }
252     CATCH_EXCEPTION_CONVERSION
253     CATCH_EXCEPTION_NULL_PTR
254     CATCH_EXCEPTION_PLATFORM_ERROR
255     CATCH_EXCEPTION_SECURITY
256 }
257
258 IStorageEventPtr JSStorageEvent::getPrivateObject(JSObjectRef arg)
259 {
260     JSStorageEventPrivateObject* priv =
261         static_cast<JSStorageEventPrivateObject*>(JSObjectGetPrivate(arg));
262
263     if (!priv) {
264         LogError("Private object not initialized");
265         ThrowMsg(Commons::NullPointerException,
266                  "Private object not initialized");
267     }
268
269     return priv->getObject();
270 }
271
272 #undef CATCH_EXCEPTION_CONVERSION
273 #undef CATCH_EXCEPTION_NULL_PTR
274 #undef CATCH_EXCEPTION_PLATFORM_ERROR
275 #undef CATCH_EXCEPTION_SECURITY
276
277 }
278 }