tizen 2.3.1 release
[framework/web/wearable/wrt-plugins-tizen.git] / src / SecureElement / JSReader.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 #include <Logger.h>
18 #include <SecurityExceptions.h>
19 #include <ArgumentValidator.h>
20 #include <GlobalContextManager.h>
21 #include <JSUtil.h>
22 #include <TimeTracer.h>
23
24 #include "JSReader.h"
25 #include "plugin_config.h"
26
27 namespace DeviceAPI {
28 namespace SecureElement {
29
30 using namespace WrtDeviceApis::Commons;
31 using namespace DeviceAPI::Common;
32
33 namespace{
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";
39 }
40
41 struct SEReaderHolder {
42     SEReaderPtr ptr;
43 };
44
45 JSClassDefinition JSReader::m_classInfo = {
46         0,                      // version
47         kJSClassAttributeNone,  // attributes
48         CLASS_NAME,             // class name
49         NULL,                   // parent class
50         m_property,             // static values
51         m_function,             // static functions
52         initialize,             // initialize
53         finalize,               // finalize
54         NULL,                   // hasProperty
55         NULL,                   // getProperty
56         NULL,                   // setProperty
57         NULL,                   // deleteProperty
58         NULL,                   // getPropertyNames
59         NULL,                   // callAsFunction
60         NULL,                   // constructor
61         NULL,                   // hasInstance,
62         NULL                    // convertToType
63 };
64
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},
69         { 0, 0, 0}
70 };
71
72 JSStaticValue JSReader::m_property[] =
73 {
74         {READER_ATTR_ISPRESENT, isPresent, NULL,
75                 kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly},
76         { 0, 0, 0, 0 }
77 };
78
79 JSClassRef JSReader::m_jsClassRef = JSClassCreate(JSReader::getClassInfo());
80
81 SEReaderPtr JSReader::getPrivateObject(JSContextRef context, JSValueRef value)
82 {
83     if (!JSValueIsObjectOfClass(context, value, getClassRef())) {
84         LOGW("Type mismatch");
85         throw TypeMismatchException("Type mismatch");
86     }
87
88     JSObjectRef object = JSUtil::JSValueToObject(context, value);
89     SEReaderHolder* priv = static_cast<SEReaderHolder*>(JSObjectGetPrivate(object));
90     if (!priv) {
91         LOGW("Holder is null");
92         throw UnknownException("Holder is null");
93     }
94     if (!priv->ptr) {
95         LOGW("Priv is null");
96         throw UnknownException("Priv is null");
97     }
98
99     return priv->ptr;
100 }
101
102 void JSReader::setPrivateObject(JSObjectRef object, SEReaderPtr native)
103 {
104     SEReaderHolder* priv = static_cast<SEReaderHolder*>(JSObjectGetPrivate(object));
105     if (!priv) {
106         LOGW("Priv is null");
107         throw UnknownException("Priv is null");
108     }
109     priv->ptr = native;
110 }
111
112 JSObjectRef JSReader::makeJSObject(JSContextRef context, SEReaderPtr native)
113 {
114     LOGD("Entered");
115     if (!native) {
116         LOGW("Native is null");
117         throw UnknownException("Native is null");
118     }
119
120     SEReaderHolder* priv = new(std::nothrow) SEReaderHolder();
121     if (!priv) {
122         LOGW("Priv is null");
123         throw UnknownException("Priv is null");
124     }
125     priv->ptr = native;
126
127     JSObjectRef obj = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
128     return obj;
129 }
130
131 void JSReader::initialize(JSContextRef context, JSObjectRef object)
132 {
133     LOGD("Entered");
134 }
135
136 void JSReader::finalize(JSObjectRef object)
137 {
138     LOGD("Entered");
139 }
140
141 const JSClassRef JSReader::getClassRef()
142 {
143     if (!m_jsClassRef) {
144         m_jsClassRef = JSClassCreate(&m_classInfo);
145     }
146     return m_jsClassRef;
147 }
148
149 const JSClassDefinition* JSReader::getClassInfo()
150 {
151     return &m_classInfo;
152 }
153
154 JSValueRef JSReader::isPresent(JSContextRef context,
155         JSObjectRef object,
156         JSStringRef propertyName,
157         JSValueRef* exception)
158 {
159     LOGD("Entered");
160     try {
161         SEReaderPtr priv = JSReader::getPrivateObject(context, object);
162         return JSUtil::toJSValueRef(context, priv->isPresent());
163     }
164     catch (const BasePlatformException &error) {
165         LOGE("Attribute get failed: %s", error.getMessage().c_str());
166     }
167     catch (...) {
168         LOGE("Attribute get failed");
169     }
170     return JSValueMakeUndefined(context);
171 }
172
173 JSValueRef JSReader::getName(JSContextRef context,
174         JSObjectRef object,
175         JSObjectRef thisObject,
176         size_t argumentCount,
177         const JSValueRef arguments[],
178         JSValueRef* exception)
179 {
180     SET_TIME_TRACER_ITEM(0);
181     LOGD("Entered");
182
183     ACE_ACCESS_CHECK(
184         AceSecurityStatus status = SECURE_ELEMENT_CHECK_ACCESS(SECUREELEMENT_FUNCTION_API_FUNCS);
185         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
186     );
187
188     try {
189         SEReaderPtr priv = JSReader::getPrivateObject(context, thisObject);
190         return JSUtil::toJSValueRef(context, priv->getName());
191     }
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);
196     }
197     catch (...) {
198         LOGE("getName fails");
199         return JSWebAPIErrorFactory::postException(context, exception,
200                 JSWebAPIErrorFactory::UNKNOWN_ERROR, "getName fails");
201     }
202 }
203
204 JSValueRef JSReader::openSession(JSContextRef context,
205         JSObjectRef object,
206         JSObjectRef thisObject,
207         size_t argumentCount,
208         const JSValueRef arguments[],
209         JSValueRef* exception)
210 {
211     SET_TIME_TRACER_ITEM(0);
212     LOGD("Entered");
213
214     ACE_ACCESS_CHECK(
215         AceSecurityStatus status = SECURE_ELEMENT_CHECK_ACCESS(SECUREELEMENT_FUNCTION_API_FUNCS);
216         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
217     );
218
219     OpenSessionCallbackData *callback = NULL;
220
221     try {
222         ArgumentValidator validator(context, argumentCount, arguments);
223
224         callback = new OpenSessionCallbackData(
225                 GlobalContextManager::getInstance()->getGlobalContext(context));
226         callback->setSuccessCallback(validator.toFunction(0, false));
227         callback->setErrorCallback(validator.toFunction(1, true));
228
229         SEReaderPtr priv = JSReader::getPrivateObject(context, thisObject);
230         priv->openSession(callback);
231     }
232     catch (const BasePlatformException &err) {
233         LOGE("openSession BasePlarformException caught: name: %s, msg: %s",
234                 err.getName().c_str(), err.getMessage().c_str());
235         delete callback;
236         return JSWebAPIErrorFactory::postException(context, exception, err);
237     }
238     catch (...) {
239         LOGE("openSession fails");
240         delete callback;
241         return JSWebAPIErrorFactory::postException(context, exception,
242                 JSWebAPIErrorFactory::UNKNOWN_ERROR, "openSession fails");
243     }
244
245     return JSValueMakeUndefined(context);
246 }
247
248 JSValueRef JSReader::closeSessions(JSContextRef context,
249         JSObjectRef object,
250         JSObjectRef thisObject,
251         size_t argumentCount,
252         const JSValueRef arguments[],
253         JSValueRef* exception)
254 {
255     SET_TIME_TRACER_ITEM(0);
256     LOGD("Entered");
257
258     ACE_ACCESS_CHECK(
259         AceSecurityStatus status = SECURE_ELEMENT_CHECK_ACCESS(SECUREELEMENT_FUNCTION_API_FUNCS);
260         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
261     );
262
263     try {
264         SEReaderPtr priv = JSReader::getPrivateObject(context, thisObject);
265         priv->closeSessions();
266     }
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);
271     }
272     catch (...) {
273         LOGE("closeSessions fails");
274         return JSWebAPIErrorFactory::postException(context, exception,
275                 JSWebAPIErrorFactory::UNKNOWN_ERROR, "closeSessions fails");
276     }
277
278     return JSValueMakeUndefined(context);
279 }
280
281 } // SecureElement
282 } // DeviceAPI