2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
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"
37 using namespace DeviceAPI::Common;
38 using namespace WrtDeviceApis::Commons;
39 using namespace WrtDeviceApis::CommonsJavaScript;
42 #define TIZEN_SEREADER_ATTRIBUTENAME "Reader"
43 #define TIZEN_SEREADER_ISPRESENT "isPresent"
46 namespace SecureElement {
48 JSClassDefinition JSSEReader::m_classInfo =
51 kJSClassAttributeNone,
52 TIZEN_SEREADER_ATTRIBUTENAME,
61 NULL, //DeleteProperty,
62 NULL, //GetPropertyNames,
63 NULL, //CallAsFunction,
64 NULL, //CallAsConstructor,
65 hasInstance, //HasInstance,
69 JSStaticFunction JSSEReader::m_function[] = {
70 {"getName", JSSEReader::getName, kJSPropertyAttributeNone},
71 {"openSession", JSSEReader::openSession, kJSPropertyAttributeNone},
72 {"closeSessions", JSSEReader::closeSessions, kJSPropertyAttributeNone},
76 JSStaticValue JSSEReader::m_property[] =
78 {TIZEN_SEREADER_ISPRESENT, getProperty, NULL, kJSPropertyAttributeReadOnly},
83 JSClassRef JSSEReader::m_jsClassRef = JSClassCreate(JSSEReader::getClassInfo());
85 JSValueRef JSSEReader::getProperty(JSContextRef context, JSObjectRef object,
86 JSStringRef propertyName, JSValueRef* exception)
91 Converter convert(context);
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.");
98 ISEReaderPtr reader = privateObject->getObject();
100 return convert.toJSValueRef(reader->isPresent());
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());
114 return JSValueMakeUndefined(context);
117 JSObjectRef JSSEReader::createJSObject(JSContextRef context, void *reader) {
120 ISEReaderPtr seReader = SEFactory::getInstance().createSEReaderObject(reader);
122 SEReaderPrivObject *priv = new SEReaderPrivObject(context, seReader);
125 ThrowMsg(NullPointerException, "Can not new a SecureElement object");
128 return JSObjectMake(context, getClassRef(), priv);
131 void JSSEReader::initialize(JSContextRef context, JSObjectRef object)
135 void JSSEReader::finalize(JSObjectRef object)
137 LoggerD( "entered" );
138 SEReaderPrivObject *priv = static_cast<SEReaderPrivObject*>( JSObjectGetPrivate( object ) ) ;
139 JSObjectSetPrivate(object, NULL);
140 LoggerD("Deleting SecureElement object");
145 const JSClassRef JSSEReader::getClassRef()
148 m_jsClassRef = JSClassCreate(&m_classInfo);
153 const JSClassDefinition* JSSEReader::getClassInfo()
158 bool JSSEReader::hasInstance(JSContextRef context,
159 JSObjectRef constructor,
160 JSValueRef possibleInstance,
161 JSValueRef* exception)
163 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
166 JSValueRef JSSEReader::getName(JSContextRef context,
168 JSObjectRef thisObject,
169 size_t argumentCount,
170 const JSValueRef arguments[],
171 JSValueRef* exception)
175 AceSecurityStatus status = SECURE_ELEMENT_CHECK_ACCESS(SECUREELEMENT_FUNCTION_API_FUNCS);
176 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
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");
184 ISEReaderPtr seReader(privateObject->getObject());
185 Converter convert(context);
186 return convert.toJSValueRef(seReader->getName());
189 JSValueRef JSSEReader::openSession(JSContextRef context,
191 JSObjectRef thisObject,
192 size_t argumentCount,
193 const JSValueRef arguments[],
194 JSValueRef* exception)
198 AceSecurityStatus status = SECURE_ELEMENT_CHECK_ACCESS(SECUREELEMENT_FUNCTION_API_FUNCS);
199 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
201 JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
203 ArgumentValidator validator(context, argumentCount, arguments);
205 JSObjectRef successCallbackObj = validator.toFunction(0);
206 if (successCallbackObj) {
207 onSuccessForCbm = arguments[0];
210 JSObjectRef errorCallbackObj = validator.toFunction(1, true);
211 if (errorCallbackObj) {
212 onErrorForCbm = arguments[1];
214 } catch (const BasePlatformException &err) {
215 LoggerE(err.getName() << ": " << err.getMessage());
216 return JSWebAPIException::throwException(context, exception, err);
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");
225 ISEReaderPtr seReader(privateObject->getObject());
226 Converter convert(context);
227 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(privateObject->getContext(), onSuccessForCbm, onErrorForCbm, true, true);
229 EventSEOpenSessionPtr event(new EventSEOpenSession());
230 event->setPrivateData( StaticPointerCast<IEventPrivateData>(callbackManager) );
231 event->setForAsynchronousCall(&SEResponseDispatcher::getInstance());
233 seReader->openSession(event);
235 SEAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, privateObject->getContext());
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());
247 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
248 return JSValueMakeUndefined(context);
251 JSValueRef JSSEReader::closeSessions(JSContextRef context,
253 JSObjectRef thisObject,
254 size_t argumentCount,
255 const JSValueRef arguments[],
256 JSValueRef* exception)
260 AceSecurityStatus status = SECURE_ELEMENT_CHECK_ACCESS(SECUREELEMENT_FUNCTION_API_FUNCS);
261 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
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");
269 ISEReaderPtr seReader(privateObject->getObject());
270 seReader->closeSessions();
271 return JSValueMakeUndefined(context);