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.
19 #include <dpl/log/log.h>
20 #include <CommonsJavaScript/Converter.h>
21 #include <CommonsJavaScript/Validator.h>
22 #include <Commons/Exception.h>
23 #include <CommonsJavaScript/PrivateObject.h>
24 #include <CommonsJavaScript/JSUtils.h>
25 #include <CommonsJavaScript/JSCallbackManager.h>
26 #include <CommonsJavaScript/Utils.h>
27 #include <JSTizenExceptionFactory.h>
28 #include <JSTizenException.h>
29 #include <SecurityExceptions.h>
30 #include "SEFactory.h"
31 #include "SEResponseDispatcher.h"
32 #include "JSSESession.h"
33 #include "SEConverter.h"
34 #include "SEAsyncCallbackManager.h"
38 using namespace DeviceAPI::Common;
39 using namespace WrtDeviceApis::Commons;
40 using namespace WrtDeviceApis::CommonsJavaScript;
43 #define TIZEN_SESESSION_ATTRIBUTENAME "Session"
44 #define TIZEN_SESESSION_ISCLOSED "isClosed"
47 namespace SecureElement {
49 JSClassDefinition JSSESession::m_classInfo =
52 kJSClassAttributeNone,
53 TIZEN_SESESSION_ATTRIBUTENAME,
62 NULL, //DeleteProperty,
63 NULL, //GetPropertyNames,
64 NULL, //CallAsFunction,
65 NULL, //CallAsConstructor,
66 hasInstance, //HasInstance,
70 JSStaticFunction JSSESession::m_function[] = {
71 {"openBasicChannel", JSSESession::openBasicChannel, kJSPropertyAttributeNone},
72 {"openLogicalChannel", JSSESession::openLogicalChannel, kJSPropertyAttributeNone},
73 {"getATR", JSSESession::getATR, kJSPropertyAttributeNone},
74 {"close", JSSESession::close, kJSPropertyAttributeNone},
75 {"closeChannels", JSSESession::closeChannels, kJSPropertyAttributeNone},
79 JSStaticValue JSSESession::m_property[] =
81 {TIZEN_SESESSION_ISCLOSED, getProperty, NULL, kJSPropertyAttributeReadOnly},
84 JSClassRef JSSESession::m_jsClassRef = JSClassCreate(JSSESession::getClassInfo());
86 JSValueRef JSSESession::getProperty(JSContextRef context, JSObjectRef object,
87 JSStringRef propertyName, JSValueRef* exception)
92 Converter convert(context);
94 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SESESSION_ISCLOSED)) {
95 SESessionPrivObject* privateObject = static_cast<SESessionPrivObject*>(JSObjectGetPrivate(object));
96 if (NULL == privateObject) {
97 ThrowMsg(NullPointerException, "Private object not set.");
99 ISESessionPtr seSession = privateObject->getObject();
101 return convert.toJSValueRef(seSession->isClosed());
103 } Catch (ConversionException) {
104 LogError("ConversionException: " << _rethrown_exception.GetMessage());
105 } Catch (NullPointerException) {
106 LogError("NullPointerException: " << _rethrown_exception.GetMessage());
107 } Catch (UnknownException) {
108 LogError("UnknownExceptionException: " << _rethrown_exception.GetMessage());
109 } Catch (PlatformException) {
110 LogError("PlatformExceptionException: " << _rethrown_exception.GetMessage());
111 } Catch (WrtDeviceApis::Commons::Exception) {
112 LogError("Exception: " << _rethrown_exception.GetMessage());
115 return JSValueMakeUndefined(context);
118 JSObjectRef JSSESession::createJSObject(JSContextRef context, void *session) {
121 ISESessionPtr seSession = SEFactory::getInstance().createSESessionObject(session);
123 SESessionPrivObject *priv = new SESessionPrivObject(context, seSession);
126 ThrowMsg(NullPointerException, "Can not new a SecureElement object");
129 return JSObjectMake(context, getClassRef(), priv);
132 void JSSESession::initialize(JSContextRef context, JSObjectRef object)
136 void JSSESession::finalize(JSObjectRef object)
138 LogDebug( "entered" );
139 SESessionPrivObject *priv = static_cast<SESessionPrivObject*>( JSObjectGetPrivate( object ) ) ;
140 JSObjectSetPrivate(object, NULL);
141 LogDebug("Deleting SecureElement object");
146 const JSClassRef JSSESession::getClassRef()
149 m_jsClassRef = JSClassCreate(&m_classInfo);
154 const JSClassDefinition* JSSESession::getClassInfo()
159 bool JSSESession::hasInstance(JSContextRef context,
160 JSObjectRef constructor,
161 JSValueRef possibleInstance,
162 JSValueRef* exception)
164 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
167 JSValueRef JSSESession::getATR(JSContextRef context,
169 JSObjectRef thisObject,
170 size_t argumentCount,
171 const JSValueRef arguments[],
172 JSValueRef* exception)
174 SESessionPrivObject* privateObject = static_cast<SESessionPrivObject*>(JSObjectGetPrivate(thisObject));
175 if (NULL == privateObject) {
176 LogError("private object is null");
177 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
180 ISESessionPtr seSession(privateObject->getObject());
181 SEConverter convert(context);
182 return convert.toJSValueRef(seSession->getATR());
185 JSValueRef JSSESession::close(JSContextRef context,
187 JSObjectRef thisObject,
188 size_t argumentCount,
189 const JSValueRef arguments[],
190 JSValueRef* exception)
192 SESessionPrivObject* privateObject = static_cast<SESessionPrivObject*>(JSObjectGetPrivate(thisObject));
193 if (NULL == privateObject) {
194 LogError("private object is null");
195 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
198 ISESessionPtr seSession(privateObject->getObject());
200 return JSValueMakeUndefined(context);
203 JSValueRef JSSESession::closeChannels(JSContextRef context,
205 JSObjectRef thisObject,
206 size_t argumentCount,
207 const JSValueRef arguments[],
208 JSValueRef* exception)
210 SESessionPrivObject* privateObject = static_cast<SESessionPrivObject*>(JSObjectGetPrivate(thisObject));
211 if (NULL == privateObject) {
212 LogError("private object is null");
213 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
216 ISESessionPtr seSession(privateObject->getObject());
217 seSession->closeChannels();
218 return JSValueMakeUndefined(context);
221 JSValueRef JSSESession::openBasicChannel(JSContextRef context,
223 JSObjectRef thisObject,
224 size_t argumentCount,
225 const JSValueRef arguments[],
226 JSValueRef* exception)
228 LogDebug("Entered ");
230 Validator validator(context, exception);
232 if ((argumentCount < 2) || JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0]) || !JSIsArrayValue(context, arguments[0])) {
233 /* 1st argument is mandatory. And 1st argument must be Array. */
234 LogError("AID TypeMismatchException!");
235 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
238 if (JSValueIsNull(context, arguments[1]) || JSValueIsUndefined(context, arguments[1]) || !validator.isCallback(arguments[1])) {
239 /* 2st argument is mandatory. And 1st argument must be Array. */
240 LogError("SEChannelSuccessCallback TypeMismatchException!");
241 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
244 if ((argumentCount > 2) && !JSValueIsNull(context, arguments[2]) && !JSValueIsUndefined(context, arguments[2])) {
245 if (!validator.isCallback(arguments[2])) {
246 /* 3nd argument must be Callback. */
247 LogError("ErrorCallback TypeMismatchException!");
248 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
252 JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
253 if (validator.isCallback(arguments[1])) {
254 onSuccessForCbm = arguments[1];
257 if ((argumentCount > 2) && validator.isCallback(arguments[2])) {
258 onErrorForCbm = arguments[2];
261 SESessionPrivObject* privateObject = static_cast<SESessionPrivObject*>(JSObjectGetPrivate(thisObject));
262 if (NULL == privateObject) {
263 LogError("private object is null");
264 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
267 ISESessionPtr seSession(privateObject->getObject());
268 Converter convert(context);
269 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(privateObject->getContext(), onSuccessForCbm, onErrorForCbm, true, true);
271 EventSEOpenChannelPtr event(new EventSEOpenChannel(convert.toVectorOfUChars(arguments[0]), true));
272 event->setPrivateData( StaticPointerCast<IEventPrivateData>(callbackManager) );
273 event->setForAsynchronousCall(&SEResponseDispatcher::getInstance());
275 seSession->openChannel(event);
277 SEAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, privateObject->getContext());
279 return JSValueMakeUndefined(context);
280 } Catch (ConversionException) {
281 LogError("ConversionException");
282 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
283 } Catch (InvalidArgumentException) {
284 LogError("InvalidArgumentException");
285 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values"));
286 return JSValueMakeUndefined(context);
287 } Catch (PlatformException) {
288 LogError("PlatformException: " << _rethrown_exception.GetMessage());
289 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::SERVICE_NOT_AVAILABLE, "Service Not Available"));
290 return JSValueMakeUndefined(context);
291 } Catch (UnknownException) {
292 LogError("UnknownException: " << _rethrown_exception.GetMessage());
293 } Catch (WrtDeviceApis::Commons::Exception) {
294 LogError("Exception: " << _rethrown_exception.GetMessage());
297 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
298 return JSValueMakeUndefined(context);
301 JSValueRef JSSESession::openLogicalChannel(JSContextRef context,
303 JSObjectRef thisObject,
304 size_t argumentCount,
305 const JSValueRef arguments[],
306 JSValueRef* exception)
308 LogDebug("Entered ");
310 Validator validator(context, exception);
312 if ((argumentCount < 2) || JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0]) || !JSIsArrayValue(context, arguments[0])) {
313 /* 1st argument is mandatory. And 1st argument must be Array. */
314 LogError("AID TypeMismatchException!");
315 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
318 if (JSValueIsNull(context, arguments[1]) || JSValueIsUndefined(context, arguments[1]) || !validator.isCallback(arguments[1])) {
319 /* 2st argument is mandatory. And 1st argument must be Array. */
320 LogError("SEChannelSuccessCallback TypeMismatchException!");
321 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
324 if ((argumentCount > 2) && !JSValueIsNull(context, arguments[2]) && !JSValueIsUndefined(context, arguments[2])) {
325 if (!validator.isCallback(arguments[2])) {
326 /* 3nd argument must be Callback. */
327 LogError("ErrorCallback TypeMismatchException!");
328 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
332 JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
333 if (validator.isCallback(arguments[1])) {
334 onSuccessForCbm = arguments[1];
337 if ((argumentCount > 2) && validator.isCallback(arguments[2])) {
338 onErrorForCbm = arguments[2];
341 SESessionPrivObject* privateObject = static_cast<SESessionPrivObject*>(JSObjectGetPrivate(thisObject));
342 if (NULL == privateObject) {
343 LogError("private object is null");
344 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
347 ISESessionPtr seSession(privateObject->getObject());
348 Converter convert(context);
349 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(privateObject->getContext(), onSuccessForCbm, onErrorForCbm, true, true);
351 EventSEOpenChannelPtr event(new EventSEOpenChannel(convert.toVectorOfUChars(arguments[0]), false));
352 event->setPrivateData( StaticPointerCast<IEventPrivateData>(callbackManager) );
353 event->setForAsynchronousCall(&SEResponseDispatcher::getInstance());
355 seSession->openChannel(event);
357 SEAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, privateObject->getContext());
359 return JSValueMakeUndefined(context);
360 } Catch (ConversionException) {
361 LogError("ConversionException");
362 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
363 } Catch (InvalidArgumentException) {
364 LogError("InvalidArgumentException");
365 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values"));
366 return JSValueMakeUndefined(context);
367 } Catch (PlatformException) {
368 LogError("PlatformException: " << _rethrown_exception.GetMessage());
369 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::SERVICE_NOT_AVAILABLE, "Service Not Available"));
370 return JSValueMakeUndefined(context);
371 } Catch (UnknownException) {
372 LogError("UnknownException: " << _rethrown_exception.GetMessage());
373 } Catch (WrtDeviceApis::Commons::Exception) {
374 LogError("Exception: " << _rethrown_exception.GetMessage());
377 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
378 return JSValueMakeUndefined(context);