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