wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / SecureElement / JSSEReader.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 <CommonsJavaScript/Converter.h>
19 #include <CommonsJavaScript/Validator.h>
20 #include <Commons/Exception.h>
21 #include <CommonsJavaScript/PrivateObject.h>
22 #include <CommonsJavaScript/JSUtils.h>
23 #include <CommonsJavaScript/JSCallbackManager.h>
24 #include <CommonsJavaScript/Utils.h>
25 #include <JSTizenExceptionFactory.h>
26 #include <JSTizenException.h>
27 #include <SecurityExceptions.h>
28 #include <ArgumentValidator.h>
29 #include <JSWebAPIException.h>
30 #include "SEFactory.h"
31 #include "SEResponseDispatcher.h"
32 #include "JSSEReader.h"
33 #include "SEAsyncCallbackManager.h"
34 #include "plugin_config.h"
35 #include <Logger.h>
36
37 using namespace DeviceAPI::Common;
38 using namespace WrtDeviceApis::Commons;
39 using namespace WrtDeviceApis::CommonsJavaScript;
40 using namespace DPL;
41
42 #define TIZEN_SEREADER_ATTRIBUTENAME "Reader"
43 #define TIZEN_SEREADER_ISPRESENT "isPresent"
44
45 namespace DeviceAPI {
46 namespace SecureElement {
47
48  JSClassDefinition JSSEReader::m_classInfo =
49 {
50     0,
51     kJSClassAttributeNone,
52     TIZEN_SEREADER_ATTRIBUTENAME,
53     0,
54     m_property,
55     m_function,
56     initialize,
57     finalize,
58     NULL, //hasProperty,
59     NULL,
60     NULL, //setProperty,
61     NULL, //DeleteProperty,
62     NULL, //GetPropertyNames,
63     NULL, //CallAsFunction,
64     NULL, //CallAsConstructor,
65     hasInstance, //HasInstance,
66     NULL  //ConvertToType
67 };
68
69 JSStaticFunction JSSEReader::m_function[] = {
70     {"getName", JSSEReader::getName, kJSPropertyAttributeNone},
71     {"openSession", JSSEReader::openSession, kJSPropertyAttributeNone},
72     {"closeSessions", JSSEReader::closeSessions, kJSPropertyAttributeNone},
73     { 0, 0, 0}
74 };
75
76 JSStaticValue JSSEReader::m_property[] =
77 {
78         {TIZEN_SEREADER_ISPRESENT,  getProperty, NULL, kJSPropertyAttributeReadOnly},
79         { 0, 0, 0, 0 }
80 };
81
82
83 JSClassRef JSSEReader::m_jsClassRef = JSClassCreate(JSSEReader::getClassInfo());
84
85 JSValueRef JSSEReader::getProperty(JSContextRef context, JSObjectRef object,
86         JSStringRef propertyName, JSValueRef* exception)
87 {
88         LoggerD("Enter");
89
90         Try     {
91                 Converter convert(context);
92
93                 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SEREADER_ISPRESENT)) {
94                         SEReaderPrivObject* privateObject = static_cast<SEReaderPrivObject*>(JSObjectGetPrivate(object));
95                         if (NULL == privateObject) {
96                                 ThrowMsg(NullPointerException, "Private object not set.");
97                         }
98                         ISEReaderPtr reader = privateObject->getObject();
99                         
100                         return convert.toJSValueRef(reader->isPresent());
101                 }
102         } Catch (ConversionException) {
103                 LoggerE("ConversionException: " << _rethrown_exception.GetMessage());
104         } Catch (NullPointerException) {
105                 LoggerE("NullPointerException: " << _rethrown_exception.GetMessage());
106         } Catch (WrtDeviceApis::Commons::UnknownException) {
107                 LoggerE("UnknownExceptionException: " << _rethrown_exception.GetMessage());
108         } Catch (PlatformException) {
109                 LoggerE("PlatformExceptionException: " << _rethrown_exception.GetMessage());
110         } Catch (WrtDeviceApis::Commons::Exception) {
111                 LoggerE("Exception: " << _rethrown_exception.GetMessage());
112         }
113
114     return JSValueMakeUndefined(context);
115 }
116
117 JSObjectRef JSSEReader::createJSObject(JSContextRef context, void *reader) {
118         LoggerD("entered");
119
120         ISEReaderPtr seReader = SEFactory::getInstance().createSEReaderObject(reader);
121         
122         SEReaderPrivObject *priv = new SEReaderPrivObject(context, seReader);
123
124         if (!priv) {
125                 ThrowMsg(NullPointerException, "Can not new a SecureElement object");
126         }
127
128         return JSObjectMake(context, getClassRef(), priv);
129 }
130
131 void JSSEReader::initialize(JSContextRef context, JSObjectRef object)
132 {
133 }
134
135 void JSSEReader::finalize(JSObjectRef object)
136 {
137         LoggerD( "entered" );
138         SEReaderPrivObject *priv = static_cast<SEReaderPrivObject*>( JSObjectGetPrivate( object ) ) ;
139         JSObjectSetPrivate(object, NULL);
140         LoggerD("Deleting SecureElement object");
141         delete priv;
142 }
143
144
145 const JSClassRef JSSEReader::getClassRef()
146 {
147         if (!m_jsClassRef) {
148                 m_jsClassRef = JSClassCreate(&m_classInfo);
149         }
150         return m_jsClassRef;
151 }
152
153 const JSClassDefinition* JSSEReader::getClassInfo()
154 {
155         return &m_classInfo;
156 }
157
158 bool JSSEReader::hasInstance(JSContextRef context,
159         JSObjectRef constructor,
160         JSValueRef possibleInstance,
161         JSValueRef* exception)
162 {
163     return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
164 }
165
166 JSValueRef JSSEReader::getName(JSContextRef context,
167         JSObjectRef object,
168         JSObjectRef thisObject,
169         size_t argumentCount,
170         const JSValueRef arguments[],
171         JSValueRef* exception)
172 {
173         LoggerD("Entered ");
174
175     AceSecurityStatus status = SECURE_ELEMENT_CHECK_ACCESS(SECUREELEMENT_FUNCTION_API_FUNCS);
176     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
177
178         SEReaderPrivObject* privateObject = static_cast<SEReaderPrivObject*>(JSObjectGetPrivate(thisObject));
179         if (NULL == privateObject) {
180                 LoggerE("private object is null");
181                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
182         }
183
184         ISEReaderPtr seReader(privateObject->getObject());
185         Converter convert(context);
186         return convert.toJSValueRef(seReader->getName());
187 }
188
189 JSValueRef JSSEReader::openSession(JSContextRef context,
190         JSObjectRef object,
191         JSObjectRef thisObject,
192         size_t argumentCount,
193         const JSValueRef arguments[],
194         JSValueRef* exception)
195 {
196         LoggerD("Entered ");
197
198     AceSecurityStatus status = SECURE_ELEMENT_CHECK_ACCESS(SECUREELEMENT_FUNCTION_API_FUNCS);
199     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
200
201         JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
202         try {
203                 ArgumentValidator validator(context, argumentCount, arguments);
204
205                 JSObjectRef successCallbackObj = validator.toFunction(0);
206                 if (successCallbackObj) {
207                         onSuccessForCbm = arguments[0];
208                 }
209
210                 JSObjectRef errorCallbackObj = validator.toFunction(1, true);
211                 if (errorCallbackObj) {
212                         onErrorForCbm = arguments[1];
213                 }
214         } catch (const BasePlatformException &err) {
215         LoggerE(err.getName() << ": " << err.getMessage());
216         return JSWebAPIException::throwException(context, exception, err);
217     }
218
219         SEReaderPrivObject* privateObject = static_cast<SEReaderPrivObject*>(JSObjectGetPrivate(thisObject));
220         if (NULL == privateObject) {
221                 LoggerE("private object is null");
222                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
223         }
224
225         ISEReaderPtr seReader(privateObject->getObject());
226         Converter convert(context);
227         JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(privateObject->getContext(), onSuccessForCbm, onErrorForCbm, true, true);
228         Try {
229                 EventSEOpenSessionPtr event(new EventSEOpenSession());
230                 event->setPrivateData( StaticPointerCast<IEventPrivateData>(callbackManager) );
231                 event->setForAsynchronousCall(&SEResponseDispatcher::getInstance());
232
233                 seReader->openSession(event);
234
235                 SEAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, privateObject->getContext());
236
237                 return JSValueMakeUndefined(context);
238         } Catch (ConversionException) {
239                 LoggerE("ConversionException");
240                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
241         } Catch (WrtDeviceApis::Commons::UnknownException) {
242                 LoggerE("UnknownException: " << _rethrown_exception.GetMessage());
243         } Catch (WrtDeviceApis::Commons::Exception) {
244                 LoggerE("Exception: " << _rethrown_exception.GetMessage());
245         }
246
247         callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
248         return JSValueMakeUndefined(context);
249 }
250
251 JSValueRef JSSEReader::closeSessions(JSContextRef context,
252         JSObjectRef object,
253         JSObjectRef thisObject,
254         size_t argumentCount,
255         const JSValueRef arguments[],
256         JSValueRef* exception)
257 {
258         LoggerD("Entered ");
259
260     AceSecurityStatus status = SECURE_ELEMENT_CHECK_ACCESS(SECUREELEMENT_FUNCTION_API_FUNCS);
261     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
262
263         SEReaderPrivObject* privateObject = static_cast<SEReaderPrivObject*>(JSObjectGetPrivate(thisObject));
264         if (NULL == privateObject) {
265                 LoggerE("private object is null");
266                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
267         }
268
269         ISEReaderPtr seReader(privateObject->getObject());
270         seReader->closeSessions();
271         return JSValueMakeUndefined(context);
272 }
273
274 }
275 }