2 // Tizen Web Device API
3 // Copyright (c) 2014 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.
19 #include "JSSession.h"
21 #include <PlatformException.h>
23 #include <GlobalContextManager.h>
24 #include <JSWebAPIErrorFactory.h>
28 namespace SecureElement {
30 using namespace smartcard_service_api;
31 using namespace Common;
33 SEReader::SEReader(void* reader)
36 LOGE("Reader is null");
37 throw UnknownException("Reader is null");
40 m_reader = static_cast<Reader*>(reader);
48 catch (ExceptionBase& e) {
49 LOGE("Exception caught: %d, message: %s", e.getErrorCode(), e.what());
53 bool SEReader::isPresent() const
55 return m_reader->isSecureElementPresent();
58 std::string SEReader::getName() const
60 return m_reader->getName();
63 void SEReader::openSession(OpenSessionCallbackData* callback)
65 callback->setReader(m_reader);
67 if(pthread_create(&thread, NULL, openSessionThread,
68 static_cast<void*>(callback))) {
69 LOGE("Thread creation failed");
70 throw UnknownException("Thread creation failed");
72 if(pthread_detach(thread)) {
73 LOGE("Thread detachment failed");
77 void* SEReader::openSessionThread(void* data)
79 auto callback = static_cast<OpenSessionCallbackData*>(data);
81 LOGE("callback is null");
85 Reader* reader = callback->getReader();
87 LOGE("reader is null");
94 SessionHelper* session = reader->openSessionSync();
95 SessionPtr session_ptr(new Session(
96 static_cast<smartcard_service_api::Session*>(session)));
97 callback->setSession(session_ptr);
99 catch (const BasePlatformException &err) {
100 LOGE("openSessionThread fails, %s: %s", err.getName().c_str(),
101 err.getMessage().c_str());
102 callback->setError(err.getName(), err.getMessage());
105 LOGE("openSessionThread fails");
106 callback->setError("UnknownError", "openSessionThread fails");
109 guint id = g_idle_add(openSessionCallback, data);
111 LOGE("g_idle_add fails");
118 gboolean SEReader::openSessionCallback(void *data)
120 auto callback = static_cast<OpenSessionCallbackData*>(data);
122 LOGE("callback is null");
126 JSContextRef context = callback->getContext();
127 if (!GlobalContextManager::getInstance()->isAliveGlobalContext(context)) {
128 LOGE("context was closed");
134 if (callback->isError()) {
135 JSObjectRef error = JSWebAPIErrorFactory::makeErrorObject(context,
136 callback->getErrorName(),
137 callback->getErrorMessage());
138 callback->callErrorCallback(error);
141 auto session = callback->getSession();
143 LOGE("session is null");
149 JSObjectRef obj = JSSession::makeJSObject(context, session);
150 callback->callSuccessCallback(obj);
153 catch (const BasePlatformException &err) {
154 LOGE("openSessionCallback fails, %s: %s", err.getName().c_str(),
155 err.getMessage().c_str());
158 LOGE("openSessionCallback fails");
167 void SEReader::closeSessions()
169 m_reader->closeSessions();