tizen 2.3.1 release
[framework/web/wearable/wrt-plugins-tizen.git] / src / SecureElement / SEReader.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2014 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 "SEReader.h"
19 #include "JSSession.h"
20
21 #include <PlatformException.h>
22 #include <Logger.h>
23 #include <GlobalContextManager.h>
24 #include <JSWebAPIErrorFactory.h>
25 #include <JSUtil.h>
26
27 namespace DeviceAPI {
28 namespace SecureElement {
29
30 using namespace smartcard_service_api;
31 using namespace Common;
32
33 SEReader::SEReader(void* reader)
34 {
35     if (!reader) {
36         LOGE("Reader is null");
37         throw UnknownException("Reader is null");
38     }
39
40     m_reader = static_cast<Reader*>(reader);
41 }
42
43 SEReader::~SEReader()
44 {
45     try {
46         closeSessions();
47     }
48     catch (ExceptionBase& e) {
49         LOGE("Exception caught: %d, message: %s", e.getErrorCode(), e.what());
50     }
51 }
52
53 bool SEReader::isPresent() const
54 {
55     return m_reader->isSecureElementPresent();
56 }
57
58 std::string SEReader::getName() const
59 {
60     return m_reader->getName();
61 }
62
63 void SEReader::openSession(OpenSessionCallbackData* callback)
64 {
65     callback->setReader(m_reader);
66     pthread_t thread;
67     if(pthread_create(&thread, NULL, openSessionThread,
68             static_cast<void*>(callback))) {
69         LOGE("Thread creation failed");
70         throw UnknownException("Thread creation failed");
71     }
72     if(pthread_detach(thread)) {
73         LOGE("Thread detachment failed");
74     }
75 }
76
77 void* SEReader::openSessionThread(void* data)
78 {
79     auto callback = static_cast<OpenSessionCallbackData*>(data);
80     if (!callback) {
81         LOGE("callback is null");
82         return NULL;
83     }
84
85     Reader* reader = callback->getReader();
86     if (!reader) {
87         LOGE("reader is null");
88         delete callback;
89         callback = NULL;
90         return NULL;
91     }
92
93     try {
94         SessionHelper* session = reader->openSessionSync();
95         SessionPtr session_ptr(new Session(
96                 static_cast<smartcard_service_api::Session*>(session)));
97         callback->setSession(session_ptr);
98     }
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());
103     }
104     catch (...) {
105         LOGE("openSessionThread fails");
106         callback->setError("UnknownError", "openSessionThread fails");
107     }
108
109     guint id = g_idle_add(openSessionCallback, data);
110     if (!id) {
111         LOGE("g_idle_add fails");
112         delete callback;
113         callback = NULL;
114     }
115     return NULL;
116 }
117
118 gboolean SEReader::openSessionCallback(void *data)
119 {
120     auto callback = static_cast<OpenSessionCallbackData*>(data);
121     if (!callback) {
122         LOGE("callback is null");
123         return false;
124     }
125
126     JSContextRef context = callback->getContext();
127     if (!GlobalContextManager::getInstance()->isAliveGlobalContext(context)) {
128         LOGE("context was closed");
129         delete callback;
130         callback = NULL;
131         return false;
132     }
133     try {
134         if (callback->isError()) {
135             JSObjectRef error = JSWebAPIErrorFactory::makeErrorObject(context,
136                     callback->getErrorName(),
137                     callback->getErrorMessage());
138             callback->callErrorCallback(error);
139         }
140         else {
141             auto session = callback->getSession();
142             if (!session) {
143                 LOGE("session is null");
144                 delete callback;
145                 callback = NULL;
146                 return false;
147             }
148
149             JSObjectRef obj = JSSession::makeJSObject(context, session);
150             callback->callSuccessCallback(obj);
151         }
152     }
153     catch (const BasePlatformException &err) {
154         LOGE("openSessionCallback fails, %s: %s", err.getName().c_str(),
155                 err.getMessage().c_str());
156     }
157     catch (...) {
158         LOGE("openSessionCallback fails");
159     }
160
161     delete callback;
162     callback = NULL;
163
164     return false;
165 }
166
167 void SEReader::closeSessions()
168 {
169     m_reader->closeSessions();
170 }
171
172 }
173 }