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 JSClassRef JSStorageEvent::getClassRef()
110 m_jsClassRef = JSClassCreate(&m_classInfo);
115 const JSClassDefinition* JSStorageEvent::getClassInfo()
120 JSClassRef JSStorageEvent::m_jsClassRef = JSClassCreate(
121 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");
144 JSValueRef JSStorageEvent::getKey(
145 JSContextRef context,
147 JSStringRef /*propertyName*/,
148 JSValueRef* exception)
154 Converter converter(context);
156 return converter.toJSValueRef(getPrivateObject(object)->getKey());
158 CATCH_EXCEPTION_CONVERSION
159 CATCH_EXCEPTION_NULL_PTR
160 CATCH_EXCEPTION_PLATFORM_ERROR
161 CATCH_EXCEPTION_SECURITY
164 JSValueRef JSStorageEvent::getOldValue(
165 JSContextRef context,
167 JSStringRef /*propertyName*/,
168 JSValueRef* exception)
174 Converter converter(context);
176 DPL::OptionalString oldValue = getPrivateObject(object)->getOldValue();
179 return JSValueMakeNull(context);
181 return converter.toJSValueRef(DPL::ToUTF8String(*oldValue));
184 CATCH_EXCEPTION_CONVERSION
185 CATCH_EXCEPTION_NULL_PTR
186 CATCH_EXCEPTION_PLATFORM_ERROR
187 CATCH_EXCEPTION_SECURITY
190 JSValueRef JSStorageEvent::getNewValue(
191 JSContextRef context,
193 JSStringRef /*propertyName*/,
194 JSValueRef* exception)
200 Converter converter(context);
202 DPL::OptionalString newValue = getPrivateObject(object)->getNewValue();
205 return JSValueMakeNull(context);
207 return converter.toJSValueRef(DPL::ToUTF8String(*newValue));
210 CATCH_EXCEPTION_CONVERSION
211 CATCH_EXCEPTION_NULL_PTR
212 CATCH_EXCEPTION_PLATFORM_ERROR
213 CATCH_EXCEPTION_SECURITY
216 JSValueRef JSStorageEvent::getUrl(
217 JSContextRef context,
219 JSStringRef /*propertyName*/,
220 JSValueRef* exception)
226 Converter converter(context);
228 return converter.toJSValueRef(getPrivateObject(object)->getUrl());
230 CATCH_EXCEPTION_CONVERSION
231 CATCH_EXCEPTION_NULL_PTR
234 JSValueRef JSStorageEvent::getStorageArea(
235 JSContextRef context,
236 JSObjectRef /*object*/,
237 JSStringRef /*propertyName*/,
238 JSValueRef* exception)
244 LogError("Not implemented");
245 return JSValueMakeUndefined(context);
247 CATCH_EXCEPTION_CONVERSION
248 CATCH_EXCEPTION_NULL_PTR
249 CATCH_EXCEPTION_PLATFORM_ERROR
250 CATCH_EXCEPTION_SECURITY
253 IStorageEventPtr JSStorageEvent::getPrivateObject(JSObjectRef arg)
255 JSStorageEventPrivateObject* priv =
256 static_cast<JSStorageEventPrivateObject*>(JSObjectGetPrivate(arg));
259 LogError("Private object not initialized");
260 ThrowMsg(Commons::NullPointerException,
261 "Private object not initialized");
264 return priv->getObject();
267 #undef CATCH_EXCEPTION_CONVERSION
268 #undef CATCH_EXCEPTION_NULL_PTR
269 #undef CATCH_EXCEPTION_PLATFORM_ERROR
270 #undef CATCH_EXCEPTION_SECURITY