tizen 2.3.1 release
[framework/web/wearable/wrt-plugins-tizen.git] / src / NFC / JSHCEEventData.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include <SecurityExceptions.h>
19
20 #include <JSUtil.h>
21 #include <JSWebAPIErrorFactory.h>
22 #include <ArgumentValidator.h>
23 #include <GlobalContextManager.h>
24 #include <MultiCallbackUserData.h>
25 #include <PlatformException.h>
26
27 #include "plugin_config.h"
28
29 #include "JSHCEEventData.h"
30
31 using namespace WrtDeviceApis::Commons;
32 using namespace DeviceAPI::Common;
33
34 namespace DeviceAPI {
35 namespace NFC {
36
37 namespace {
38     const char* HCEAPDU_DATA_EVENT_TYPE ="eventType";
39     const char* HCEAPDU_DATA_APDU ="apdu";
40     const char* HCEAPDU_DATA_LENGTH ="length";
41 }
42
43 JSClassDefinition JSHCEEventData::m_classInfo = {
44     0,
45     kJSClassAttributeNone,
46     "HCEEventData",
47     NULL, //ParentClass
48     m_property, //StaticValues
49     NULL, //StaticFunctions
50     initialize, //Initialize
51     finalize, //Finalize
52     NULL, //HasProperty,
53     NULL, //GetProperty,
54     NULL, //SetProperty,
55     NULL, //DeleteProperty,
56     NULL, //GetPropertyNames,
57     NULL, //CallAsFunction,
58     NULL, //CallAsConstructor,
59     NULL, //HasInstance,
60     NULL //ConvertToType
61 };
62
63 JSStaticValue JSHCEEventData::m_property[] = {
64         { HCEAPDU_DATA_EVENT_TYPE, getEventType, NULL, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
65         { HCEAPDU_DATA_APDU, getApdu, NULL, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
66         { HCEAPDU_DATA_LENGTH, getLength, NULL, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
67         { 0, 0, 0, 0 }
68 };
69
70
71 JSClassRef JSHCEEventData::m_jsClassRef = JSClassCreate(JSHCEEventData::getClassInfo());
72
73 const JSClassRef JSHCEEventData::getClassRef()
74 {
75     LOGD("Entered");
76
77     if (!m_jsClassRef) {
78         m_jsClassRef = JSClassCreate(&m_classInfo);
79     }
80     return m_jsClassRef;
81 }
82
83 const JSClassDefinition* JSHCEEventData::getClassInfo()
84 {
85     LOGD("Entered");
86     return &m_classInfo;
87 }
88
89 void JSHCEEventData::initialize(JSContextRef context, JSObjectRef object)
90 {
91     LOGD("Entered");
92 }
93
94 void JSHCEEventData::finalize(JSObjectRef object)
95 {
96     LOGD("Entered");
97 }
98
99 HceEventDataPtr JSHCEEventData::getPrivateObject(JSContextRef context, JSObjectRef object)
100 {
101     LOGD("Entered");
102
103     if (!JSValueIsObjectOfClass(context, object, getClassRef())) {
104         LOGE("Type mismatch");
105         throw TypeMismatchException("Type mismatch");
106     }
107
108     HceEventDataHolder * holder = static_cast<HceEventDataHolder *>(JSObjectGetPrivate(object));
109     if (!holder) {
110         LOGE("No holder data exist");
111         throw TypeMismatchException("No holder data exist");
112     }
113
114     if (!holder->ptr) {
115         LOGE("holder ptr is null");
116         throw TypeMismatchException("holder ptr is null");
117     }
118
119     return holder->ptr;
120 }
121
122 void JSHCEEventData::setPrivateObject(JSContextRef context, JSObjectRef object, HceEventDataPtr priv)
123 {
124     LOGD("Entered");
125 }
126
127 JSObjectRef JSHCEEventData::makeJSObject(JSContextRef context, HceEventDataPtr native)
128 {
129     LOGD("Entered");
130
131     if (!native) {
132         LOGE("Native is null");
133         throw Common::UnknownException("Native is null");
134     }
135     HceEventDataHolder* holder = new(std::nothrow) HceEventDataHolder();
136     if (!holder) {
137         LOGE("No holder data exist");
138         throw TypeMismatchException("No holder data exist");
139     }
140     holder->ptr = native;
141
142     JSObjectRef obj = JSObjectMake(context, getClassRef(), static_cast<void*>(holder));
143     if (!holder) {
144         LOGE("Object creation failed");
145         delete holder;
146         holder = NULL;
147         throw Common::UnknownException("Object creation failed");
148     }
149
150     return obj;
151 }
152
153 JSValueRef  JSHCEEventData::getEventType(JSContextRef context,
154         JSObjectRef object,
155         JSStringRef propertyName,
156         JSValueRef* exception)
157 {
158     LOGD("Entered");
159     try {
160         HceEventDataPtr priv = getPrivateObject(context, object);
161
162         return JSUtil::toJSValueRef(context, priv->getEventType());
163     }
164     catch (const BasePlatformException &err) {
165         LOGE("Exception caught: name: %s, msg: %s",
166                 err.getName().c_str(), err.getMessage().c_str());
167     }
168     catch (...) {
169         LOGE("getEventType fails");
170     }
171     return JSValueMakeUndefined(context);
172 }
173
174 JSValueRef  JSHCEEventData::getApdu(JSContextRef context,
175         JSObjectRef object,
176         JSStringRef propertyName,
177         JSValueRef* exception)
178 {
179     LOGD("Entered");
180     try {
181         HceEventDataPtr priv = getPrivateObject(context, object);
182
183         return JSUtil::toJSValueRef_(context, priv->getApdu());
184     }
185     catch (const BasePlatformException &err) {
186         LOGE("Exception caught: name: %s, msg: %s",
187                 err.getName().c_str(), err.getMessage().c_str());
188     }
189     catch (...) {
190         LOGE("getApdu fails");
191     }
192     return JSValueMakeUndefined(context);
193 }
194
195 JSValueRef  JSHCEEventData::getLength(JSContextRef context,
196         JSObjectRef object,
197         JSStringRef propertyName,
198         JSValueRef* exception)
199 {
200     LOGD("Entered");
201     try {
202         HceEventDataPtr priv = getPrivateObject(context, object);
203
204         return JSUtil::toJSValueRef(context, priv->getLength());
205     }
206     catch (const BasePlatformException &err) {
207         LOGE("Exception caught: name: %s, msg: %s",
208                 err.getName().c_str(), err.getMessage().c_str());
209     }
210     catch (...) {
211         LOGE("getLength fails");
212     }
213     return JSValueMakeUndefined(context);
214 }
215
216 } // NFC
217 } // DeviceAPI