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.
18 #include <CommonsJavaScript/Validator.h>
19 #include <Commons/Exception.h>
20 #include <CommonsJavaScript/PrivateObject.h>
21 #include <CommonsJavaScript/JSUtils.h>
22 #include <CommonsJavaScript/JSCallbackManager.h>
23 #include <CommonsJavaScript/Utils.h>
24 #include <JSWebAPIErrorFactory.h>
25 #include <ArgumentValidator.h>
26 #include <SecurityExceptions.h>
27 #include "SEFactory.h"
28 #include "SEResponseDispatcher.h"
29 #include "JSSEChannel.h"
30 #include "SEAsyncCallbackManager.h"
31 #include "SEConverter.h"
32 #include "plugin_config.h"
35 using namespace DeviceAPI::Common;
36 using namespace WrtDeviceApis::Commons;
37 using namespace WrtDeviceApis::CommonsJavaScript;
40 #define TIZEN_SECHANNEL_ATTRIBUTENAME "Channel"
41 #define TIZEN_SECHANNEL_ISBASICCHANNEL "isBasicChannel"
45 namespace SecureElement {
47 JSClassDefinition JSSEChannel::m_classInfo =
50 kJSClassAttributeNone,
51 TIZEN_SECHANNEL_ATTRIBUTENAME,
60 NULL, //DeleteProperty,
61 NULL, //GetPropertyNames,
62 NULL, //CallAsFunction,
63 NULL, //CallAsConstructor,
64 hasInstance, //HasInstance,
68 JSStaticFunction JSSEChannel::m_function[] = {
69 {"close", JSSEChannel::close, kJSPropertyAttributeNone},
70 {"transmit", JSSEChannel::transmit, kJSPropertyAttributeNone},
74 JSStaticValue JSSEChannel::m_property[] =
76 {TIZEN_SECHANNEL_ISBASICCHANNEL, getProperty, NULL, kJSPropertyAttributeReadOnly},
79 JSClassRef JSSEChannel::m_jsClassRef = JSClassCreate(JSSEChannel::getClassInfo());
81 JSValueRef JSSEChannel::getProperty(JSContextRef context, JSObjectRef object,
82 JSStringRef propertyName, JSValueRef* exception)
87 SEChannelPrivObject* privateObject = static_cast<SEChannelPrivObject*>(JSObjectGetPrivate(object));
88 if (NULL == privateObject) {
89 ThrowMsg(NullPointerException, "Private object not set.");
91 ISEChannelPtr channel = privateObject->getObject();
92 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SECHANNEL_ISBASICCHANNEL)) {
93 Converter convert(context);
94 return convert.toJSValueRef(channel->isBasicChannel());
96 } Catch (ConversionException) {
97 LoggerE("ConversionException: " << _rethrown_exception.GetMessage());
98 } Catch (NullPointerException) {
99 LoggerE("NullPointerException: " << _rethrown_exception.GetMessage());
100 } Catch (WrtDeviceApis::Commons::UnknownException) {
101 LoggerE("UnknownExceptionException: " << _rethrown_exception.GetMessage());
102 } Catch (PlatformException) {
103 LoggerE("PlatformExceptionException: " << _rethrown_exception.GetMessage());
104 } Catch (WrtDeviceApis::Commons::Exception) {
105 LoggerE("Exception: " << _rethrown_exception.GetMessage());
108 return JSValueMakeUndefined(context);
111 JSObjectRef JSSEChannel::createJSObject(JSContextRef context, void *channel) {
114 ISEChannelPtr seChannel = SEFactory::getInstance().createSEChannelObject(channel);
116 SEChannelPrivObject *priv = new SEChannelPrivObject(context, seChannel);
119 ThrowMsg(NullPointerException, "Can not new a SecureElement object");
122 return JSObjectMake(context, getClassRef(), priv);
125 void JSSEChannel::initialize(JSContextRef context, JSObjectRef object)
127 LoggerD( "entered" );
130 void JSSEChannel::finalize(JSObjectRef object)
132 LoggerD( "entered" );
133 SEChannelPrivObject *priv = static_cast<SEChannelPrivObject*>( JSObjectGetPrivate( object ) ) ;
134 JSObjectSetPrivate(object, NULL);
135 LoggerD("Deleting SecureElement object");
140 const JSClassRef JSSEChannel::getClassRef()
143 m_jsClassRef = JSClassCreate(&m_classInfo);
148 const JSClassDefinition* JSSEChannel::getClassInfo()
153 bool JSSEChannel::hasInstance(JSContextRef context,
154 JSObjectRef constructor,
155 JSValueRef possibleInstance,
156 JSValueRef* exception)
158 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
161 JSValueRef JSSEChannel::close(JSContextRef context,
163 JSObjectRef thisObject,
164 size_t argumentCount,
165 const JSValueRef arguments[],
166 JSValueRef* exception)
170 AceSecurityStatus status = SECURE_ELEMENT_CHECK_ACCESS(SECUREELEMENT_FUNCTION_API_FUNCS);
171 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
173 SEChannelPrivObject* privateObject = static_cast<SEChannelPrivObject*>(JSObjectGetPrivate(thisObject));
174 if (NULL == privateObject) {
175 LoggerE("private object is null");
176 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
179 ISEChannelPtr seChannel(privateObject->getObject());
182 return JSValueMakeUndefined(context);
183 } Catch (WrtDeviceApis::Commons::UnknownException) {
184 LoggerE("Exception: " << _rethrown_exception.GetMessage());
185 } Catch (WrtDeviceApis::Commons::Exception) {
186 LoggerE("Exception: " << _rethrown_exception.GetMessage());
188 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error");
191 JSValueRef JSSEChannel::transmit(JSContextRef context,
193 JSObjectRef thisObject,
194 size_t argumentCount,
195 const JSValueRef arguments[],
196 JSValueRef* exception)
200 AceSecurityStatus status = SECURE_ELEMENT_CHECK_ACCESS(SECUREELEMENT_FUNCTION_API_FUNCS);
201 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
203 if ((argumentCount < 2) || JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0]) || !JSIsArrayValue(context, arguments[0])) {
204 /* 1st argument is mandatory. And 1st argument must be Array */
205 LoggerE("data TypeMismatchException!");
206 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
209 JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
211 ArgumentValidator validator(context, argumentCount, arguments);
213 JSObjectRef successCallbackObj = validator.toFunction(1);
214 if (successCallbackObj) {
215 onSuccessForCbm = arguments[1];
218 JSObjectRef errorCallbackObj = validator.toFunction(2, true);
219 if (errorCallbackObj) {
220 onErrorForCbm = arguments[2];
222 } catch (const BasePlatformException &err) {
223 LoggerE(err.getName() << ": " << err.getMessage());
224 return JSWebAPIErrorFactory::postException(context, exception, err);
228 SEChannelPrivObject* privateObject = static_cast<SEChannelPrivObject*>(JSObjectGetPrivate(thisObject));
229 if (NULL == privateObject) {
230 LoggerE("private object is null");
231 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
234 ISEChannelPtr seChannel(privateObject->getObject());
235 Converter convert(context);
236 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(privateObject->getContext(), onSuccessForCbm, onErrorForCbm, true, true);
239 EventSEChannelTransmitPtr event(new EventSEChannelTransmit(convert.toVectorOfUChars(arguments[0])));
240 event->setPrivateData( DPL::StaticPointerCast<IEventPrivateData>(callbackManager) );
241 event->setForAsynchronousCall(&SEResponseDispatcher::getInstance());
242 callbackManager->setObject(thisObject);
243 seChannel->transmit(event);
245 SEAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, privateObject->getContext());
247 return JSValueMakeUndefined(context);
248 } Catch (ConversionException) {
249 LoggerE("ConversionException: " << _rethrown_exception.GetMessage());
250 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
251 } Catch (WrtDeviceApis::Commons::UnknownException) {
252 LoggerE("UnknownException: " << _rethrown_exception.GetMessage());
253 } Catch (WrtDeviceApis::Commons::Exception) {
254 LoggerE("Exception: " << _rethrown_exception.GetMessage());
257 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::UNKNOWN_ERROR,"Unknown Error"));
258 return JSValueMakeUndefined(context);