Update wrt-plugins-common_0.3.53
[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 const 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(JSStorageEvent::getClassInfo());
121
122 void JSStorageEvent::initialize(JSContextRef context,
123                                 JSObjectRef object)
124 {
125     LogDebug("entered");
126
127     JSStorageEventPrivateObject* priv =
128         static_cast<JSStorageEventPrivateObject*>(JSObjectGetPrivate(object));
129
130     Assert(priv && "Missing private object");
131 }
132
133 void JSStorageEvent::finalize(JSObjectRef object)
134 {
135     LogDebug("entered");
136     JSStorageEventPrivateObject* priv =
137         static_cast<JSStorageEventPrivateObject*>(JSObjectGetPrivate(object));
138
139     delete priv;
140     LogDebug("private object is realised");
141
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     }
159     CATCH_EXCEPTION_CONVERSION
160     CATCH_EXCEPTION_NULL_PTR
161     CATCH_EXCEPTION_PLATFORM_ERROR
162     CATCH_EXCEPTION_SECURITY
163 }
164
165 JSValueRef JSStorageEvent::getOldValue(
166         JSContextRef context,
167         JSObjectRef object,
168         JSStringRef propertyName,
169         JSValueRef* exception)
170 {
171     LogDebug("entered");
172
173     Try
174     {
175         Converter converter(context);
176
177         DPL::OptionalString oldValue = getPrivateObject(object)->getOldValue();
178
179         if(!oldValue) {
180             return JSValueMakeNull(context);
181         } else {
182             return converter.toJSValueRef(DPL::ToUTF8String(*oldValue));
183         }
184
185     }
186     CATCH_EXCEPTION_CONVERSION
187     CATCH_EXCEPTION_NULL_PTR
188     CATCH_EXCEPTION_PLATFORM_ERROR
189     CATCH_EXCEPTION_SECURITY
190 }
191
192 JSValueRef JSStorageEvent::getNewValue(
193         JSContextRef context,
194         JSObjectRef object,
195         JSStringRef propertyName,
196         JSValueRef* exception)
197 {
198     LogDebug("entered");
199
200     Try
201     {
202         Converter converter(context);
203
204         DPL::OptionalString newValue = getPrivateObject(object)->getNewValue();
205
206         if(!newValue) {
207             return JSValueMakeNull(context);
208         } else {
209             return converter.toJSValueRef(DPL::ToUTF8String(*newValue));
210         }
211
212     }
213     CATCH_EXCEPTION_CONVERSION
214     CATCH_EXCEPTION_NULL_PTR
215     CATCH_EXCEPTION_PLATFORM_ERROR
216     CATCH_EXCEPTION_SECURITY
217 }
218
219 JSValueRef JSStorageEvent::getUrl(
220         JSContextRef context,
221         JSObjectRef object,
222         JSStringRef propertyName,
223         JSValueRef* exception)
224 {
225     LogDebug("entered");
226
227     Try
228     {
229         Converter converter(context);
230
231         return converter.toJSValueRef(getPrivateObject(object)->getUrl());
232
233     }
234     CATCH_EXCEPTION_CONVERSION
235     CATCH_EXCEPTION_NULL_PTR
236 }
237
238 JSValueRef JSStorageEvent::getStorageArea(
239         JSContextRef context,
240         JSObjectRef object,
241         JSStringRef propertyName,
242         JSValueRef* exception)
243 {
244     LogDebug("entered");
245
246     Try
247     {
248         LogError("Not implemented");
249         return JSValueMakeUndefined(context);
250     }
251     CATCH_EXCEPTION_CONVERSION
252     CATCH_EXCEPTION_NULL_PTR
253     CATCH_EXCEPTION_PLATFORM_ERROR
254     CATCH_EXCEPTION_SECURITY
255 }
256
257 IStorageEventPtr JSStorageEvent::getPrivateObject(JSObjectRef arg)
258 {
259     JSStorageEventPrivateObject* priv =
260         static_cast<JSStorageEventPrivateObject*>(JSObjectGetPrivate(arg));
261
262     if (!priv) {
263         LogError("Private object not initialized");
264         ThrowMsg(Commons::NullPointerException,
265                  "Private object not initialized");
266     }
267
268     return priv->getObject();
269 }
270
271 #undef CATCH_EXCEPTION_CONVERSION
272 #undef CATCH_EXCEPTION_NULL_PTR
273 #undef CATCH_EXCEPTION_PLATFORM_ERROR
274 #undef CATCH_EXCEPTION_SECURITY
275
276 }
277 }