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 <CommonsJavaScript/Converter.h>
20 #include <Commons/Exception.h>
21 #include <CommonsJavaScript/PrivateObject.h>
22 #include <CommonsJavaScript/JSUtils.h>
23 #include <CommonsJavaScript/JSCallbackManager.h>
24 #include <CommonsJavaScript/Utils.h>
25 #include <JSTizenExceptionFactory.h>
26 #include <JSTizenException.h>
27 #include <SecurityExceptions.h>
28 #include <ArgumentValidator.h>
29 #include <JSWebAPIException.h>
30 #include "SEFactory.h"
31 #include "SEResponseDispatcher.h"
32 #include "JSSEService.h"
33 #include "SEAsyncCallbackManager.h"
34 #include "SEListenerManager.h"
35 #include "SEConverter.h"
36 #include "plugin_config.h"
39 using namespace DeviceAPI::Common;
40 using namespace WrtDeviceApis::Commons;
41 using namespace WrtDeviceApis::CommonsJavaScript;
44 #define TIZEN_SESERVICE_ATTRIBUTENAME "SEService"
47 namespace SecureElement {
49 JSClassDefinition JSSEService::m_classInfo =
52 kJSClassAttributeNone,
53 TIZEN_SESERVICE_ATTRIBUTENAME,
62 NULL, //DeleteProperty,
63 NULL, //GetPropertyNames,
64 NULL, //CallAsFunction,
65 NULL, //CallAsConstructor,
66 hasInstance, //HasInstance,
70 JSStaticFunction JSSEService::m_function[] = {
71 {"getReaders", JSSEService::getReaders, kJSPropertyAttributeNone},
72 {"registerSEListener", JSSEService::registerSEListener, kJSPropertyAttributeNone},
73 {"unregisterSEListener", JSSEService::unregisterSEListener, kJSPropertyAttributeNone},
74 {"shutdown", JSSEService::shutdown, kJSPropertyAttributeNone},
78 JSClassRef JSSEService::m_jsClassRef = JSClassCreate(JSSEService::getClassInfo());
80 void JSSEService::initialize(JSContextRef context, JSObjectRef object)
82 SEServicePrivObject* priv = static_cast<SEServicePrivObject*>(JSObjectGetPrivate(object));
85 ISEServicePtr seService( SEFactory::getInstance().createSEServiceObject() );
86 priv = new SEServicePrivObject(context, seService);
87 if(!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
88 LoggerE("Object can't store private data.");
91 } Catch (UnsupportedException) {
92 LoggerE("UnsupportedException: " << _rethrown_exception.GetMessage());
93 } Catch (WrtDeviceApis::Commons::UnknownException) {
94 LoggerE("UnknownExceptionException: " << _rethrown_exception.GetMessage());
95 } Catch (PlatformException) {
96 LoggerE("PlatformExceptionException: " << _rethrown_exception.GetMessage());
97 } Catch (WrtDeviceApis::Commons::Exception) {
98 LoggerE("Exception: " << _rethrown_exception.GetMessage());
103 void JSSEService::finalize(JSObjectRef object)
105 LoggerD( "entered" );
106 SEServicePrivObject *priv = static_cast<SEServicePrivObject*>( JSObjectGetPrivate( object ) ) ;
107 JSObjectSetPrivate(object, NULL);
108 LoggerD("Deleting SEService object");
113 const JSClassRef JSSEService::getClassRef()
116 m_jsClassRef = JSClassCreate(&m_classInfo);
121 const JSClassDefinition* JSSEService::getClassInfo()
126 bool JSSEService::hasInstance(JSContextRef context,
127 JSObjectRef constructor,
128 JSValueRef possibleInstance,
129 JSValueRef* exception)
131 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
134 JSValueRef JSSEService::getReaders(JSContextRef context,
136 JSObjectRef thisObject,
137 size_t argumentCount,
138 const JSValueRef arguments[],
139 JSValueRef* exception)
143 AceSecurityStatus status = SECURE_ELEMENT_CHECK_ACCESS(SECUREELEMENT_FUNCTION_API_FUNCS);
144 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
146 JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
148 ArgumentValidator validator(context, argumentCount, arguments);
150 JSObjectRef successCallbackObj = validator.toFunction(0);
151 if (successCallbackObj) {
152 onSuccessForCbm = arguments[0];
155 JSObjectRef errorCallbackObj = validator.toFunction(1, true);
156 if (errorCallbackObj) {
157 onErrorForCbm = arguments[1];
159 } catch (const BasePlatformException &err) {
160 LoggerE(err.getName() << ": " << err.getMessage());
161 return JSWebAPIException::throwException(context, exception, err);
164 SEServicePrivObject* privateObject = static_cast<SEServicePrivObject*>(JSObjectGetPrivate(thisObject));
165 if (NULL == privateObject) {
166 LoggerE("private object is null");
167 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
170 ISEServicePtr seService(privateObject->getObject());
171 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(privateObject->getContext(), onSuccessForCbm, onErrorForCbm, true, true);
173 EventListSEsPtr event(new EventListSEs());
174 event->setPrivateData( StaticPointerCast<IEventPrivateData>(callbackManager) );
175 event->setForAsynchronousCall(&SEResponseDispatcher::getInstance());
177 seService->getReaders(event);
179 SEAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, privateObject->getContext());
180 return JSValueMakeUndefined(context);
181 } Catch (ConversionException) {
182 LoggerE("ConversionException");
183 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
184 } Catch (WrtDeviceApis::Commons::UnknownException) {
185 LoggerE("UnknownException: " << _rethrown_exception.GetMessage());
186 } Catch (WrtDeviceApis::Commons::Exception) {
187 LoggerE("Exception: " << _rethrown_exception.GetMessage());
190 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
191 return JSValueMakeUndefined(context);
194 JSValueRef JSSEService::registerSEListener(JSContextRef context,
196 JSObjectRef thisObject,
197 size_t argumentCount,
198 const JSValueRef arguments[],
199 JSValueRef* exception)
203 AceSecurityStatus status = SECURE_ELEMENT_CHECK_ACCESS(SECUREELEMENT_FUNCTION_API_FUNCS);
204 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
206 Validator validator(context, exception);
208 ArgumentValidator argValidator(context, argumentCount, arguments);
209 if (!argValidator.toObject(0))
210 throw TypeMismatchException("Parameter is not Object");
211 }catch(const BasePlatformException& err){
212 return JSWebAPIException::throwException(context, exception, err);
215 SEConverter convert(context);
216 SEListener seListener;
217 if (!validator.isCallback(arguments[0])) {
218 seListener = convert.toSEListener(arguments[0]);
220 /* 1st argument must be SEListener. */
221 LoggerE("SEListener must has onSEReady and onSENotReady");
222 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
226 SEServicePrivObject* privateObject = static_cast<SEServicePrivObject*>(JSObjectGetPrivate(thisObject));
227 if (NULL == privateObject) {
228 LoggerE("private object is null");
229 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
232 ISEServicePtr seService(privateObject->getObject());
233 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(privateObject->getContext(), seListener.onSEReady, seListener.onSENotReady, true, true);
234 JSValueProtect(privateObject->getContext(), thisObject);
236 EventSEStateChangedEmitterPtr emitter(new EventSEStateChangedEmitter);
237 emitter->setListener(&SEResponseDispatcher::getInstance());
238 emitter->setEventPrivateData(StaticPointerCast<EventSEStateChanged::PrivateDataType>(callbackManager));
239 seService->registerSEListener(emitter);
241 SEListenerCancellerPtr canceller = SEListenerCancellerPtr(new SEListenerCanceller(privateObject->getContext(), thisObject, static_cast<long>(emitter->getId())));
242 IListenerItemPtr listenerItem = StaticPointerCast<IListenerItem>(canceller);
243 SEListenerManagerSingleton::Instance().registerListener(listenerItem, privateObject->getContext());
245 return convert.toJSValueRefLong(static_cast<long>(emitter->getId()));
246 } Catch (ConversionException) {
247 LoggerE("ConversionException");
248 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
249 } Catch (WrtDeviceApis::Commons::UnknownException) {
250 LoggerE("UnknownException: " << _rethrown_exception.GetMessage());
251 } Catch (WrtDeviceApis::Commons::Exception) {
252 LoggerE("Exception: " << _rethrown_exception.GetMessage());
255 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR,"Unknown Error");
258 JSValueRef JSSEService::unregisterSEListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
259 size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) {
262 AceSecurityStatus status = SECURE_ELEMENT_CHECK_ACCESS(SECUREELEMENT_FUNCTION_API_FUNCS);
263 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
267 ArgumentValidator validator(context, argumentCount, arguments);
268 value = validator.toULong(0);
269 }catch(const BasePlatformException& err){
270 return JSWebAPIException::throwException(context, exception, err);
273 SEServicePrivObject* privateObject = static_cast<SEServicePrivObject*>(JSObjectGetPrivate(thisObject));
274 if (NULL == privateObject) {
275 LoggerE("private object is null");
276 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
280 ISEServicePtr seService(privateObject->getObject());
282 Converter convert(context);
283 seService->unregisterSEListener(value);
285 SEListenerCancellerPtr canceller = SEListenerCancellerPtr(new SEListenerCanceller(privateObject->getContext(), thisObject, value));
286 IListenerItemPtr listenerItem = StaticPointerCast<IListenerItem>(canceller);
287 SEListenerManagerSingleton::Instance().unregisterListener(listenerItem);
289 return JSValueMakeUndefined(context);
290 } Catch (ConversionException) {
291 LoggerE("ConversionException");
292 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
293 } Catch (NullPointerException) {
294 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
295 } Catch (WrtDeviceApis::Commons::NotFoundException) {
296 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Not Found ID");
297 } Catch (WrtDeviceApis::Commons::Exception) {
298 LoggerE("Exception: " << _rethrown_exception.GetMessage());
300 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
303 JSValueRef JSSEService::shutdown(JSContextRef context,
305 JSObjectRef thisObject,
306 size_t argumentCount,
307 const JSValueRef arguments[],
308 JSValueRef* exception)
312 AceSecurityStatus status = SECURE_ELEMENT_CHECK_ACCESS(SECUREELEMENT_FUNCTION_API_FUNCS);
313 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
315 SEServicePrivObject* privateObject = static_cast<SEServicePrivObject*>(JSObjectGetPrivate(thisObject));
316 if (NULL == privateObject) {
317 LoggerE("private object is null");
318 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
321 ISEServicePtr seService(privateObject->getObject());
323 seService->shutdown();
324 return JSValueMakeUndefined(context);
325 } Catch (WrtDeviceApis::Commons::UnknownException) {
326 LoggerE("Exception: " << _rethrown_exception.GetMessage());
327 } Catch (WrtDeviceApis::Commons::Exception) {
328 LoggerE("Exception: " << _rethrown_exception.GetMessage());
330 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");