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>
30 using namespace WrtDeviceApis;
31 using namespace WrtDeviceApis::Commons;
32 using namespace WrtDeviceApis::CommonsJavaScript;
33 using namespace WrtDeviceApis::StorageEvent::Api;
35 #define WIDGET_PLUGIN_NAME "StorageEvent"
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"
43 #define CATCH_EXCEPTION_CONVERSION \
44 Catch(Commons::ConversionException) {\
45 LogError("Error on conversion");\
46 return JSDOMExceptionFactory::\
47 UnknownException.make(context, exception);\
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);\
57 #define CATCH_EXCEPTION_PLATFORM_ERROR \
58 Catch(Commons::PlatformException){\
59 LogError("PlatformException occured");\
60 return JSDOMExceptionFactory::\
61 UnknownException.make(context, exception);\
64 #define CATCH_EXCEPTION_SECURITY \
65 Catch(Commons::SecurityException){\
66 LogError("Security exception occured");\
67 return JSDOMExceptionFactory::\
68 SecurityException.make(context, exception);\
71 namespace WrtPlugins {
73 JSClassDefinition JSStorageEvent::m_classInfo = {
75 kJSClassAttributeNone,
85 NULL, //DeleteProperty,
86 NULL, //GetPropertyNames,
87 NULL, //CallAsFunction,
88 NULL, //CallAsConstructor,
90 NULL, //ConvertToType,
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 },
107 const JSClassRef JSStorageEvent::getClassRef()
110 m_jsClassRef = JSClassCreate(&m_classInfo);
115 const JSClassDefinition* JSStorageEvent::getClassInfo()
120 JSClassRef JSStorageEvent::m_jsClassRef = JSClassCreate(JSStorageEvent::getClassInfo());
122 void JSStorageEvent::initialize(JSContextRef context,
127 JSStorageEventPrivateObject* priv =
128 static_cast<JSStorageEventPrivateObject*>(JSObjectGetPrivate(object));
130 Assert(priv && "Missing private object");
133 void JSStorageEvent::finalize(JSObjectRef object)
136 JSStorageEventPrivateObject* priv =
137 static_cast<JSStorageEventPrivateObject*>(JSObjectGetPrivate(object));
140 LogDebug("private object is realised");
144 JSValueRef JSStorageEvent::getKey(
145 JSContextRef context,
147 JSStringRef propertyName,
148 JSValueRef* exception)
154 Converter converter(context);
156 return converter.toJSValueRef(getPrivateObject(object)->getKey());
159 CATCH_EXCEPTION_CONVERSION
160 CATCH_EXCEPTION_NULL_PTR
161 CATCH_EXCEPTION_PLATFORM_ERROR
162 CATCH_EXCEPTION_SECURITY
165 JSValueRef JSStorageEvent::getOldValue(
166 JSContextRef context,
168 JSStringRef propertyName,
169 JSValueRef* exception)
175 Converter converter(context);
177 DPL::OptionalString oldValue = getPrivateObject(object)->getOldValue();
180 return JSValueMakeNull(context);
182 return converter.toJSValueRef(DPL::ToUTF8String(*oldValue));
186 CATCH_EXCEPTION_CONVERSION
187 CATCH_EXCEPTION_NULL_PTR
188 CATCH_EXCEPTION_PLATFORM_ERROR
189 CATCH_EXCEPTION_SECURITY
192 JSValueRef JSStorageEvent::getNewValue(
193 JSContextRef context,
195 JSStringRef propertyName,
196 JSValueRef* exception)
202 Converter converter(context);
204 DPL::OptionalString newValue = getPrivateObject(object)->getNewValue();
207 return JSValueMakeNull(context);
209 return converter.toJSValueRef(DPL::ToUTF8String(*newValue));
213 CATCH_EXCEPTION_CONVERSION
214 CATCH_EXCEPTION_NULL_PTR
215 CATCH_EXCEPTION_PLATFORM_ERROR
216 CATCH_EXCEPTION_SECURITY
219 JSValueRef JSStorageEvent::getUrl(
220 JSContextRef context,
222 JSStringRef propertyName,
223 JSValueRef* exception)
229 Converter converter(context);
231 return converter.toJSValueRef(getPrivateObject(object)->getUrl());
234 CATCH_EXCEPTION_CONVERSION
235 CATCH_EXCEPTION_NULL_PTR
238 JSValueRef JSStorageEvent::getStorageArea(
239 JSContextRef context,
241 JSStringRef propertyName,
242 JSValueRef* exception)
248 LogError("Not implemented");
249 return JSValueMakeUndefined(context);
251 CATCH_EXCEPTION_CONVERSION
252 CATCH_EXCEPTION_NULL_PTR
253 CATCH_EXCEPTION_PLATFORM_ERROR
254 CATCH_EXCEPTION_SECURITY
257 IStorageEventPtr JSStorageEvent::getPrivateObject(JSObjectRef arg)
259 JSStorageEventPrivateObject* priv =
260 static_cast<JSStorageEventPrivateObject*>(JSObjectGetPrivate(arg));
263 LogError("Private object not initialized");
264 ThrowMsg(Commons::NullPointerException,
265 "Private object not initialized");
268 return priv->getObject();
271 #undef CATCH_EXCEPTION_CONVERSION
272 #undef CATCH_EXCEPTION_NULL_PTR
273 #undef CATCH_EXCEPTION_PLATFORM_ERROR
274 #undef CATCH_EXCEPTION_SECURITY