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.
18 #include <SecurityExceptions.h>
19 #include <ArgumentValidator.h>
20 #include <GlobalContextManager.h>
22 #include <TimeTracer.h>
25 #include "plugin_config.h"
28 namespace SecureElement {
30 using namespace WrtDeviceApis::Commons;
31 using namespace DeviceAPI::Common;
34 const char* CLASS_NAME = "Reader";
35 const char* READER_ATTR_ISPRESENT = "isPresent";
36 const char* READER_FUN_GET_NAME = "getName";
37 const char* READER_FUN_OPEN_SESSION = "openSession";
38 const char* READER_FUN_CLOSE_SESSIONS = "closeSessions";
41 struct SEReaderHolder {
45 JSClassDefinition JSReader::m_classInfo = {
47 kJSClassAttributeNone, // attributes
48 CLASS_NAME, // class name
50 m_property, // static values
51 m_function, // static functions
52 initialize, // initialize
57 NULL, // deleteProperty
58 NULL, // getPropertyNames
59 NULL, // callAsFunction
65 JSStaticFunction JSReader::m_function[] = {
66 {READER_FUN_GET_NAME, getName, kJSPropertyAttributeNone},
67 {READER_FUN_OPEN_SESSION, openSession, kJSPropertyAttributeNone},
68 {READER_FUN_CLOSE_SESSIONS, closeSessions, kJSPropertyAttributeNone},
72 JSStaticValue JSReader::m_property[] =
74 {READER_ATTR_ISPRESENT, isPresent, NULL,
75 kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly},
79 JSClassRef JSReader::m_jsClassRef = JSClassCreate(JSReader::getClassInfo());
81 SEReaderPtr JSReader::getPrivateObject(JSContextRef context, JSValueRef value)
83 if (!JSValueIsObjectOfClass(context, value, getClassRef())) {
84 LOGW("Type mismatch");
85 throw TypeMismatchException("Type mismatch");
88 JSObjectRef object = JSUtil::JSValueToObject(context, value);
89 SEReaderHolder* priv = static_cast<SEReaderHolder*>(JSObjectGetPrivate(object));
91 LOGW("Holder is null");
92 throw UnknownException("Holder is null");
96 throw UnknownException("Priv is null");
102 void JSReader::setPrivateObject(JSObjectRef object, SEReaderPtr native)
104 SEReaderHolder* priv = static_cast<SEReaderHolder*>(JSObjectGetPrivate(object));
106 LOGW("Priv is null");
107 throw UnknownException("Priv is null");
112 JSObjectRef JSReader::makeJSObject(JSContextRef context, SEReaderPtr native)
116 LOGW("Native is null");
117 throw UnknownException("Native is null");
120 SEReaderHolder* priv = new(std::nothrow) SEReaderHolder();
122 LOGW("Priv is null");
123 throw UnknownException("Priv is null");
127 JSObjectRef obj = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
131 void JSReader::initialize(JSContextRef context, JSObjectRef object)
136 void JSReader::finalize(JSObjectRef object)
141 const JSClassRef JSReader::getClassRef()
144 m_jsClassRef = JSClassCreate(&m_classInfo);
149 const JSClassDefinition* JSReader::getClassInfo()
154 JSValueRef JSReader::isPresent(JSContextRef context,
156 JSStringRef propertyName,
157 JSValueRef* exception)
161 SEReaderPtr priv = JSReader::getPrivateObject(context, object);
162 return JSUtil::toJSValueRef(context, priv->isPresent());
164 catch (const BasePlatformException &error) {
165 LOGE("Attribute get failed: %s", error.getMessage().c_str());
168 LOGE("Attribute get failed");
170 return JSValueMakeUndefined(context);
173 JSValueRef JSReader::getName(JSContextRef context,
175 JSObjectRef thisObject,
176 size_t argumentCount,
177 const JSValueRef arguments[],
178 JSValueRef* exception)
180 SET_TIME_TRACER_ITEM(0);
184 AceSecurityStatus status = SECURE_ELEMENT_CHECK_ACCESS(SECUREELEMENT_FUNCTION_API_FUNCS);
185 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
189 SEReaderPtr priv = JSReader::getPrivateObject(context, thisObject);
190 return JSUtil::toJSValueRef(context, priv->getName());
192 catch (const BasePlatformException &err) {
193 LOGE("getName BasePlarformException caught: name: %s, msg: %s",
194 err.getName().c_str(), err.getMessage().c_str());
195 return JSWebAPIErrorFactory::postException(context, exception, err);
198 LOGE("getName fails");
199 return JSWebAPIErrorFactory::postException(context, exception,
200 JSWebAPIErrorFactory::UNKNOWN_ERROR, "getName fails");
204 JSValueRef JSReader::openSession(JSContextRef context,
206 JSObjectRef thisObject,
207 size_t argumentCount,
208 const JSValueRef arguments[],
209 JSValueRef* exception)
211 SET_TIME_TRACER_ITEM(0);
215 AceSecurityStatus status = SECURE_ELEMENT_CHECK_ACCESS(SECUREELEMENT_FUNCTION_API_FUNCS);
216 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
219 OpenSessionCallbackData *callback = NULL;
222 ArgumentValidator validator(context, argumentCount, arguments);
224 callback = new OpenSessionCallbackData(
225 GlobalContextManager::getInstance()->getGlobalContext(context));
226 callback->setSuccessCallback(validator.toFunction(0, false));
227 callback->setErrorCallback(validator.toFunction(1, true));
229 SEReaderPtr priv = JSReader::getPrivateObject(context, thisObject);
230 priv->openSession(callback);
232 catch (const BasePlatformException &err) {
233 LOGE("openSession BasePlarformException caught: name: %s, msg: %s",
234 err.getName().c_str(), err.getMessage().c_str());
236 return JSWebAPIErrorFactory::postException(context, exception, err);
239 LOGE("openSession fails");
241 return JSWebAPIErrorFactory::postException(context, exception,
242 JSWebAPIErrorFactory::UNKNOWN_ERROR, "openSession fails");
245 return JSValueMakeUndefined(context);
248 JSValueRef JSReader::closeSessions(JSContextRef context,
250 JSObjectRef thisObject,
251 size_t argumentCount,
252 const JSValueRef arguments[],
253 JSValueRef* exception)
255 SET_TIME_TRACER_ITEM(0);
259 AceSecurityStatus status = SECURE_ELEMENT_CHECK_ACCESS(SECUREELEMENT_FUNCTION_API_FUNCS);
260 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
264 SEReaderPtr priv = JSReader::getPrivateObject(context, thisObject);
265 priv->closeSessions();
267 catch (const BasePlatformException &err) {
268 LOGE("closeSessions BasePlarformException caught: name: %s, msg: %s",
269 err.getName().c_str(), err.getMessage().c_str());
270 return JSWebAPIErrorFactory::postException(context, exception, err);
273 LOGE("closeSessions fails");
274 return JSWebAPIErrorFactory::postException(context, exception,
275 JSWebAPIErrorFactory::UNKNOWN_ERROR, "closeSessions fails");
278 return JSValueMakeUndefined(context);