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