[Release] wrt-plugins-common_0.3.89
[platform/framework/web/wrt-plugins-common.git] / src / js-overlay / JSClass / JSTizenServiceEvent.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        JSTizenServiceEvent.cpp
19  * @author      Yunchan Cho (yunchan.cho@samsung.com)
20  * @version     0.1
21  */
22
23 #include "JSTizenServiceEvent.h"
24 #include <dpl/log/log.h>
25 #include <dpl/assert.h>
26 #include <TizenServiceEvent/ITizenServiceEvent.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::TizenServiceEvent::Api;
34
35 #define WIDGET_PLUGIN_NAME "TizenServiceEvent"
36
37 #define TIZEN_SERVICE_EVENT_PROPERTY_SCALE      "scale"
38 #define TIZEN_SERVICE_EVENT_PROPERTY_BUNDLE     "__bundle"
39
40 #define CATCH_EXCEPTION_CONVERSION \
41     Catch(Commons::ConversionException) { \
42         LogError("Error on conversion"); \
43         return JSDOMExceptionFactory:: \
44                    UnknownException.make(context, exception); \
45     }
46
47 #define CATCH_EXCEPTION_NULL_PTR \
48     Catch(Commons::NullPointerException) { \
49         LogError("Error on pointer, null value"); \
50         return JSDOMExceptionFactory:: \
51                    UnknownException.make(context, exception); \
52     }
53
54 #define CATCH_EXCEPTION_PLATFORM_ERROR \
55     Catch(Commons::PlatformException){ \
56         LogError("PlatformException occured"); \
57         return JSDOMExceptionFactory:: \
58                    UnknownException.make(context, exception); \
59     }
60
61 #define CATCH_EXCEPTION_SECURITY \
62     Catch(Commons::SecurityException){ \
63         LogError("Security exception occured"); \
64         return JSDOMExceptionFactory:: \
65                    SecurityException.make(context, exception); \
66     }
67
68 namespace WrtPlugins {
69 namespace Tizen {
70 JSClassDefinition JSTizenServiceEvent::m_classInfo = {
71     0,
72     kJSClassAttributeNone,
73     WIDGET_PLUGIN_NAME,
74     0,
75     m_property,
76     NULL,
77     initialize,
78     finalize,
79     NULL, //HasProperty,
80     NULL, //GetProperty,
81     NULL, //SetProperty,
82     NULL, //DeleteProperty,
83     NULL, //GetPropertyNames,
84     NULL, //CallAsFunction,
85     NULL, //CallAsConstructor,
86     NULL, //HasInstance,
87     NULL, //ConvertToType,
88 };
89
90 JSStaticValue JSTizenServiceEvent::m_property[] = {
91     { TIZEN_SERVICE_EVENT_PROPERTY_SCALE, JSTizenServiceEvent::getScale,
92       0, kJSPropertyAttributeReadOnly },
93     { TIZEN_SERVICE_EVENT_PROPERTY_BUNDLE, JSTizenServiceEvent::getBundle,
94       0, kJSPropertyAttributeReadOnly },
95     { 0, 0, 0, 0 }
96 };
97
98 JSClassRef JSTizenServiceEvent::getClassRef()
99 {
100     if (!m_jsClassRef) {
101         m_jsClassRef = JSClassCreate(&m_classInfo);
102     }
103     return m_jsClassRef;
104 }
105
106 const JSClassDefinition* JSTizenServiceEvent::getClassInfo()
107 {
108     return &m_classInfo;
109 }
110
111 JSClassRef JSTizenServiceEvent::m_jsClassRef = JSClassCreate(
112         JSTizenServiceEvent::getClassInfo());
113
114 void JSTizenServiceEvent::initialize(JSContextRef /*context*/,
115                                      JSObjectRef object)
116 {
117     LogDebug("entered");
118
119     JSTizenServiceEventPrivateObject* priv =
120         static_cast<JSTizenServiceEventPrivateObject*>(JSObjectGetPrivate(
121                                                            object));
122
123     Assert(priv && "Missing private object");
124 }
125
126 void JSTizenServiceEvent::finalize(JSObjectRef object)
127 {
128     LogDebug("entered");
129     JSTizenServiceEventPrivateObject* priv =
130         static_cast<JSTizenServiceEventPrivateObject*>(JSObjectGetPrivate(
131                                                            object));
132
133     delete priv;
134     LogDebug("private object is realised");
135 }
136
137 JSValueRef JSTizenServiceEvent::getScale(
138     JSContextRef context,
139     JSObjectRef object,
140     JSStringRef /*propertyName*/,
141     JSValueRef* exception)
142 {
143     LogDebug("entered");
144
145     Try
146     {
147         Converter converter(context);
148         return converter.toJSValueRef(getPrivateObject(object)->getScale());
149     }
150     CATCH_EXCEPTION_CONVERSION
151     CATCH_EXCEPTION_NULL_PTR
152     CATCH_EXCEPTION_PLATFORM_ERROR
153         CATCH_EXCEPTION_SECURITY
154 }
155
156 JSValueRef JSTizenServiceEvent::getBundle(
157     JSContextRef context,
158     JSObjectRef object,
159     JSStringRef /*propertyName*/,
160     JSValueRef* exception)
161 {
162     LogDebug("entered");
163
164     Try
165     {
166         Converter converter(context);
167         return converter.toJSValueRef(getPrivateObject(object)->getBundle());
168     }
169     CATCH_EXCEPTION_CONVERSION
170     CATCH_EXCEPTION_NULL_PTR
171     CATCH_EXCEPTION_PLATFORM_ERROR
172         CATCH_EXCEPTION_SECURITY
173 }
174
175 ITizenServiceEventPtr JSTizenServiceEvent::getPrivateObject(JSObjectRef arg)
176 {
177     JSTizenServiceEventPrivateObject* priv =
178         static_cast<JSTizenServiceEventPrivateObject*>(JSObjectGetPrivate(arg));
179
180     if (!priv) {
181         LogError("Private object not initialized");
182         ThrowMsg(Commons::NullPointerException,
183                  "Private object not initialized");
184     }
185
186     return priv->getObject();
187 }
188
189 #undef CATCH_EXCEPTION_CONVERSION
190 #undef CATCH_EXCEPTION_NULL_PTR
191 #undef CATCH_EXCEPTION_PLATFORM_ERROR
192 #undef CATCH_EXCEPTION_SECURITY
193 }
194 }