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 <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"
35 using namespace DeviceAPI::Common;
36 using namespace WrtDeviceApis::Commons;
37 using namespace WrtDeviceApis::CommonsJavaScript;
40 #define TIZEN_SEREADER_ATTRIBUTENAME "Reader"
41 #define TIZEN_SEREADER_ISPRESENT "isPresent"
44 namespace SecureElement {
46 JSClassDefinition JSSEReader::m_classInfo =
49 kJSClassAttributeNone,
50 TIZEN_SEREADER_ATTRIBUTENAME,
59 NULL, //DeleteProperty,
60 NULL, //GetPropertyNames,
61 NULL, //CallAsFunction,
62 NULL, //CallAsConstructor,
63 hasInstance, //HasInstance,
67 JSStaticFunction JSSEReader::m_function[] = {
68 {"getName", JSSEReader::getName, kJSPropertyAttributeNone},
69 {"openSession", JSSEReader::openSession, kJSPropertyAttributeNone},
70 {"closeSessions", JSSEReader::closeSessions, kJSPropertyAttributeNone},
74 JSStaticValue JSSEReader::m_property[] =
76 {TIZEN_SEREADER_ISPRESENT, getProperty, NULL, kJSPropertyAttributeReadOnly},
81 JSClassRef JSSEReader::m_jsClassRef = JSClassCreate(JSSEReader::getClassInfo());
83 JSValueRef JSSEReader::getProperty(JSContextRef context, JSObjectRef object,
84 JSStringRef propertyName, JSValueRef* exception)
89 Converter convert(context);
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.");
96 ISEReaderPtr reader = privateObject->getObject();
98 return convert.toJSValueRef(reader->isPresent());
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());
112 return JSValueMakeUndefined(context);
115 JSObjectRef JSSEReader::createJSObject(JSContextRef context, void *reader) {
118 ISEReaderPtr seReader = SEFactory::getInstance().createSEReaderObject(reader);
120 SEReaderPrivObject *priv = new SEReaderPrivObject(context, seReader);
123 ThrowMsg(NullPointerException, "Can not new a SecureElement object");
126 return JSObjectMake(context, getClassRef(), priv);
129 void JSSEReader::initialize(JSContextRef context, JSObjectRef object)
133 void JSSEReader::finalize(JSObjectRef object)
135 LoggerD( "entered" );
136 SEReaderPrivObject *priv = static_cast<SEReaderPrivObject*>( JSObjectGetPrivate( object ) ) ;
137 JSObjectSetPrivate(object, NULL);
138 LoggerD("Deleting SecureElement object");
143 const JSClassRef JSSEReader::getClassRef()
146 m_jsClassRef = JSClassCreate(&m_classInfo);
151 const JSClassDefinition* JSSEReader::getClassInfo()
156 bool JSSEReader::hasInstance(JSContextRef context,
157 JSObjectRef constructor,
158 JSValueRef possibleInstance,
159 JSValueRef* exception)
161 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
164 JSValueRef JSSEReader::getName(JSContextRef context,
166 JSObjectRef thisObject,
167 size_t argumentCount,
168 const JSValueRef arguments[],
169 JSValueRef* exception)
173 AceSecurityStatus status = SECURE_ELEMENT_CHECK_ACCESS(SECUREELEMENT_FUNCTION_API_FUNCS);
174 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
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");
182 ISEReaderPtr seReader(privateObject->getObject());
183 Converter convert(context);
184 return convert.toJSValueRef(seReader->getName());
187 JSValueRef JSSEReader::openSession(JSContextRef context,
189 JSObjectRef thisObject,
190 size_t argumentCount,
191 const JSValueRef arguments[],
192 JSValueRef* exception)
196 AceSecurityStatus status = SECURE_ELEMENT_CHECK_ACCESS(SECUREELEMENT_FUNCTION_API_FUNCS);
197 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
199 JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
201 ArgumentValidator validator(context, argumentCount, arguments);
203 JSObjectRef successCallbackObj = validator.toFunction(0);
204 if (successCallbackObj) {
205 onSuccessForCbm = arguments[0];
208 JSObjectRef errorCallbackObj = validator.toFunction(1, true);
209 if (errorCallbackObj) {
210 onErrorForCbm = arguments[1];
212 } catch (const BasePlatformException &err) {
213 LoggerE(err.getName() << ": " << err.getMessage());
214 return JSWebAPIErrorFactory::postException(context, exception, err);
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");
223 ISEReaderPtr seReader(privateObject->getObject());
224 Converter convert(context);
225 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(privateObject->getContext(), onSuccessForCbm, onErrorForCbm, true, true);
227 EventSEOpenSessionPtr event(new EventSEOpenSession());
228 event->setPrivateData( DPL::StaticPointerCast<IEventPrivateData>(callbackManager) );
229 event->setForAsynchronousCall(&SEResponseDispatcher::getInstance());
230 callbackManager->setObject(thisObject);
231 seReader->openSession(event);
233 SEAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, privateObject->getContext());
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());
245 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::UNKNOWN_ERROR,"Unknown Error"));
246 return JSValueMakeUndefined(context);
249 JSValueRef JSSEReader::closeSessions(JSContextRef context,
251 JSObjectRef thisObject,
252 size_t argumentCount,
253 const JSValueRef arguments[],
254 JSValueRef* exception)
258 AceSecurityStatus status = SECURE_ELEMENT_CHECK_ACCESS(SECUREELEMENT_FUNCTION_API_FUNCS);
259 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
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");
267 ISEReaderPtr seReader(privateObject->getObject());
268 seReader->closeSessions();
269 return JSValueMakeUndefined(context);