2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file JSStorageEvent.cpp
19 * @author Andrzej Surdej (a.surdej@samsung.com)
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"
31 using namespace WrtDeviceApis;
32 using namespace WrtDeviceApis::Commons;
33 using namespace WrtDeviceApis::CommonsJavaScript;
34 using namespace WrtDeviceApis::StorageEvent::Api;
36 #define WIDGET_PLUGIN_NAME "StorageEvent"
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"
44 #define CATCH_EXCEPTION_CONVERSION \
45 Catch(Commons::ConversionException) {\
46 LogError("Error on conversion");\
47 return JSDOMExceptionFactory::\
48 UnknownException.make(context, exception);\
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);\
58 #define CATCH_EXCEPTION_PLATFORM_ERROR \
59 Catch(Commons::PlatformException){\
60 LogError("PlatformException occured");\
61 return JSDOMExceptionFactory::\
62 UnknownException.make(context, exception);\
65 #define CATCH_EXCEPTION_SECURITY \
66 Catch(Commons::SecurityException){\
67 LogError("Security exception occured");\
68 return JSDOMExceptionFactory::\
69 SecurityException.make(context, exception);\
72 namespace WrtPlugins {
74 JSClassDefinition JSStorageEvent::m_classInfo = {
76 kJSClassAttributeNone,
86 NULL, //DeleteProperty,
87 NULL, //GetPropertyNames,
88 NULL, //CallAsFunction,
89 NULL, //CallAsConstructor,
91 NULL, //ConvertToType,
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 },
108 const JSClassRef JSStorageEvent::getClassRef()
111 m_jsClassRef = JSClassCreate(&m_classInfo);
116 const JSClassDefinition* JSStorageEvent::getClassInfo()
121 JSClassRef JSStorageEvent::m_jsClassRef = JSClassCreate(JSStorageEvent::getClassInfo());
123 void JSStorageEvent::initialize(JSContextRef context,
128 JSStorageEventPrivateObject* priv =
129 static_cast<JSStorageEventPrivateObject*>(JSObjectGetPrivate(object));
131 Assert(priv && "Missing private object");
134 void JSStorageEvent::finalize(JSObjectRef object)
137 JSStorageEventPrivateObject* priv =
138 static_cast<JSStorageEventPrivateObject*>(JSObjectGetPrivate(object));
141 LogDebug("private object is realised");
145 JSValueRef JSStorageEvent::getKey(
146 JSContextRef context,
148 JSStringRef propertyName,
149 JSValueRef* exception)
155 Converter converter(context);
157 return converter.toJSValueRef(getPrivateObject(object)->getKey());
160 CATCH_EXCEPTION_CONVERSION
161 CATCH_EXCEPTION_NULL_PTR
162 CATCH_EXCEPTION_PLATFORM_ERROR
163 CATCH_EXCEPTION_SECURITY
166 JSValueRef JSStorageEvent::getOldValue(
167 JSContextRef context,
169 JSStringRef propertyName,
170 JSValueRef* exception)
176 Converter converter(context);
178 DPL::OptionalString oldValue = getPrivateObject(object)->getOldValue();
181 return JSValueMakeNull(context);
183 return converter.toJSValueRef(DPL::ToUTF8String(*oldValue));
187 CATCH_EXCEPTION_CONVERSION
188 CATCH_EXCEPTION_NULL_PTR
189 CATCH_EXCEPTION_PLATFORM_ERROR
190 CATCH_EXCEPTION_SECURITY
193 JSValueRef JSStorageEvent::getNewValue(
194 JSContextRef context,
196 JSStringRef propertyName,
197 JSValueRef* exception)
203 Converter converter(context);
205 DPL::OptionalString newValue = getPrivateObject(object)->getNewValue();
208 return JSValueMakeNull(context);
210 return converter.toJSValueRef(DPL::ToUTF8String(*newValue));
214 CATCH_EXCEPTION_CONVERSION
215 CATCH_EXCEPTION_NULL_PTR
216 CATCH_EXCEPTION_PLATFORM_ERROR
217 CATCH_EXCEPTION_SECURITY
220 JSValueRef JSStorageEvent::getUrl(
221 JSContextRef context,
223 JSStringRef propertyName,
224 JSValueRef* exception)
230 Converter converter(context);
232 return converter.toJSValueRef(getPrivateObject(object)->getUrl());
235 CATCH_EXCEPTION_CONVERSION
236 CATCH_EXCEPTION_NULL_PTR
239 JSValueRef JSStorageEvent::getStorageArea(
240 JSContextRef context,
242 JSStringRef propertyName,
243 JSValueRef* exception)
249 LogError("Not implemented");
250 return JSValueMakeUndefined(context);
252 CATCH_EXCEPTION_CONVERSION
253 CATCH_EXCEPTION_NULL_PTR
254 CATCH_EXCEPTION_PLATFORM_ERROR
255 CATCH_EXCEPTION_SECURITY
258 IStorageEventPtr JSStorageEvent::getPrivateObject(JSObjectRef arg)
260 JSStorageEventPrivateObject* priv =
261 static_cast<JSStorageEventPrivateObject*>(JSObjectGetPrivate(arg));
264 LogError("Private object not initialized");
265 ThrowMsg(Commons::NullPointerException,
266 "Private object not initialized");
269 return priv->getObject();
272 #undef CATCH_EXCEPTION_CONVERSION
273 #undef CATCH_EXCEPTION_NULL_PTR
274 #undef CATCH_EXCEPTION_PLATFORM_ERROR
275 #undef CATCH_EXCEPTION_SECURITY