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 <CommonsJavaScript/Validator.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 <JSWebAPIErrorFactory.h>
26 #include <SecurityExceptions.h>
27 #include <GlobalContextManager.h>
28 #include <ArgumentValidator.h>
30 #include <PlatformException.h>
32 #include "JSNFCManager.h"
33 #include "JSNFCAdapter.h"
34 #include "JSNdefMessage.h"
35 #include "JSNdefRecord.h"
36 #include "NFCConverter.h"
37 #include "NFCStaticController.h"
38 #include "EventNFCChangedPrivateData.h"
39 #include "NFCAsyncCallbackManager.h"
40 #include "NFCListenerManager.h"
41 #include "NFCFactory.h"
42 #include "EventNFCChanged.h"
43 #include "plugin_config.h"
48 using namespace DeviceAPI::Common;
49 using namespace WrtDeviceApis::Commons;
50 using namespace WrtDeviceApis::CommonsJavaScript;
52 #define TIZEN_NFCADAPTER_ATTRIBUTENAME "NFCAdapter"
53 #define TIZEN_NFCADAPTER_POWERED "powered"
58 JSClassDefinition JSNFCAdapter::m_classInfo =
61 kJSClassAttributeNone,
62 TIZEN_NFCADAPTER_ATTRIBUTENAME,
71 NULL, //DeleteProperty,
72 NULL, //GetPropertyNames,
73 NULL, //CallAsFunction,
74 NULL, //CallAsConstructor,
79 JSStaticValue JSNFCAdapter::m_property[] =
81 //NFCAdapterProperties
82 {TIZEN_NFCADAPTER_POWERED, getProperty, NULL, kJSPropertyAttributeReadOnly},
86 JSStaticFunction JSNFCAdapter::m_function[] = {
87 {"setTagListener", JSNFCAdapter::setTagListener, kJSPropertyAttributeNone },
88 {"unsetTagListener", JSNFCAdapter::unsetTagListener, kJSPropertyAttributeNone},
89 {"setPeerListener", JSNFCAdapter::setPeerListener, kJSPropertyAttributeNone },
90 {"unsetPeerListener", JSNFCAdapter::unsetPeerListener, kJSPropertyAttributeNone},
91 {"getCachedMessage", JSNFCAdapter::getCachedMessage, kJSPropertyAttributeNone},
92 {"setPowered", JSNFCAdapter::setPowered, kJSPropertyAttributeNone },
96 JSClassRef JSNFCAdapter::m_jsClassRef = JSClassCreate(JSNFCAdapter::getClassInfo());
98 void JSNFCAdapter::initialize(JSContextRef context, JSObjectRef object)
100 LoggerD("entered. Nothing to do");
103 void JSNFCAdapter::finalize(JSObjectRef object)
105 NFCAdapterPrivObject* priv =
106 static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(object));
108 JSObjectSetPrivate(object, NULL);
111 JSObjectRef JSNFCAdapter::createJSObject(JSContextRef context) {
114 INFCAdapterPtr nfcAdapter(NFCFactory::getInstance().createNFCAdapterObject());
115 NFCAdapterPrivObject *priv = new NFCAdapterPrivObject(context, nfcAdapter);
118 ThrowMsg(NullPointerException, "Can not new a NFCTag object");
121 return JSObjectMake(context, getClassRef(), priv);
124 const JSClassRef JSNFCAdapter::getClassRef()
127 m_jsClassRef = JSClassCreate(&m_classInfo);
132 const JSClassDefinition* JSNFCAdapter::getClassInfo()
137 JSValueRef JSNFCAdapter::getProperty(JSContextRef context, JSObjectRef object,
138 JSStringRef propertyName, JSValueRef* exception)
143 NFCConverter convert(context);
145 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_NFCADAPTER_POWERED)) {
146 NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(object));
147 if (!privateObject) {
148 LoggerE("private object is null");
149 ThrowMsg(NullPointerException, "Private object not initialized");
151 INFCAdapterPtr nfcAdapter(privateObject->getObject());
152 return convert.toJSValueRef(nfcAdapter->getPowerState());
154 } Catch (NullPointerException) {
155 LoggerE("NullPointerException: " << _rethrown_exception.GetMessage());
156 } Catch (ConversionException) {
157 LoggerE("ConversionException: " << _rethrown_exception.GetMessage());
158 } Catch (UnsupportedException) {
159 LoggerE("UnsupportedException: " << _rethrown_exception.GetMessage());
160 } Catch (WrtDeviceApis::Commons::UnknownException) {
161 LoggerE("UnknownExceptionException: " << _rethrown_exception.GetMessage());
162 } Catch (PlatformException) {
163 LoggerE("PlatformExceptionException: " << _rethrown_exception.GetMessage());
164 } Catch (WrtDeviceApis::Commons::Exception) {
165 LoggerE("Exception: " << _rethrown_exception.GetMessage());
168 return JSValueMakeUndefined(context);
171 JSValueRef JSNFCAdapter::setTagListener (JSContextRef context, JSObjectRef object,
172 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
173 JSValueRef* exception) {
174 LoggerD("JSNFCAdapter::setTagListener Enter");
176 AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_TAG_FUNCS);
177 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
178 JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(context);
180 ArgumentValidator validator(context, argumentCount, arguments);
181 NFCChangedCallback callbacks;
182 NFCConverter nfcConverter(context);
183 if (validator.toObject(0))
184 callbacks = nfcConverter.toNFCChangedCallback(arguments[0]);
186 std::vector<std::string> filterValue = validator.toStringVector(1, true);
188 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(global_context, callbacks.onattach, NULL, true, true);
189 JSCallbackManagerPtr detachedCallbackManager = JSCallbackManager::createObject(global_context, callbacks.ondetach, NULL);
190 TagFilterPtr tagFilter(new TagFilter());
191 for (unsigned int i=0; i<filterValue.size(); i++) {
192 nfcTagType filter = nfcConverter.toNfcTagType(filterValue.at(i));
194 tagFilter->addTagTypeValue(filter);
195 LoggerD("tag_types.at(i): " << filterValue.at(i));
198 NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(thisObject));
199 if (!privateObject) {
200 LoggerE("private object is null");
201 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
204 INFCAdapterPtr nfcAdapter(privateObject->getObject());
206 EventNFCChangedPrivateDataPtr privateData(
207 new EventNFCChangedPrivateData(callbackManager,
208 detachedCallbackManager)
210 EventNFCChangedEmitterPtr emitter(new EventNFCChangedEmitter);
211 emitter->setListener(&NFCStaticController::getInstance());
212 emitter->setEventPrivateData(DPL::StaticPointerCast<EventNFCChanged::PrivateDataType>(privateData));
214 if (nfcAdapter->setTagListener(emitter, tagFilter) != 0)
215 Throw(WrtDeviceApis::Commons::UnknownException);
217 NFCListenerCancellerPtr canceller = NFCListenerCancellerPtr(new NFCListenerCanceller(global_context, thisObject, static_cast<long>(ID_NFCADAPTER_TAG_LISTENER)));
218 IListenerItemPtr listenerItem = DPL::StaticPointerCast<IListenerItem>(canceller);
219 NFCListenerManagerSingleton::Instance().registerListener(listenerItem, global_context);
221 return JSValueMakeUndefined(context);
222 } Catch (BasePlatformException) {
223 LoggerE(_rethrown_exception.getName() << ": " << _rethrown_exception.getMessage());
224 return JSWebAPIErrorFactory::postException(context, exception, _rethrown_exception);
225 } Catch (ConversionException) {
226 LoggerE("JSNFCAdapter::setTagListener : ConversionException");
227 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
228 } Catch (PlatformException) {
229 LoggerE("PlatformException: " << _rethrown_exception.GetMessage());
230 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::SERVICE_NOT_AVAILABLE_ERROR, "Service Not Available");
231 } Catch (WrtDeviceApis::Commons::UnknownException) {
232 LoggerE("UnknownException: " << _rethrown_exception.GetMessage());
233 } Catch (WrtDeviceApis::Commons::Exception) {
234 LoggerE("Exception: " << _rethrown_exception.GetMessage());
236 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error");
239 JSValueRef JSNFCAdapter::setPeerListener (JSContextRef context, JSObjectRef object,
240 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
241 JSValueRef* exception) {
242 LoggerD("JSNFCAdapter::setPeerListener Enter");
244 AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_P2P_FUNCS);
245 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
247 JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(context);
250 ArgumentValidator validator(context, argumentCount, arguments);
252 NFCConverter nfcConverter(context);
253 NFCChangedCallback callbacks;
255 if (validator.toObject(0))
256 callbacks = nfcConverter.toNFCChangedCallback(arguments[0]);
258 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(global_context, callbacks.onattach, NULL, true, true);
259 JSCallbackManagerPtr detachedCallbackManager = JSCallbackManager::createObject(global_context, callbacks.ondetach, NULL);
261 NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(thisObject));
262 if (!privateObject) {
263 LoggerE("private object is null");
264 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
267 INFCAdapterPtr nfcAdapter(privateObject->getObject());
269 EventNFCChangedPrivateDataPtr privateData(
270 new EventNFCChangedPrivateData(callbackManager,
271 detachedCallbackManager)
274 EventNFCChangedEmitterPtr emitter(new EventNFCChangedEmitter);
275 emitter->setListener(&NFCStaticController::getInstance());
276 emitter->setEventPrivateData(DPL::StaticPointerCast<EventNFCChanged::PrivateDataType>(privateData));
278 if (nfcAdapter->setPeerListener(emitter) != 0)
279 Throw(WrtDeviceApis::Commons::UnknownException);
281 NFCListenerCancellerPtr canceller = NFCListenerCancellerPtr(new NFCListenerCanceller(global_context, thisObject, static_cast<long>(ID_NFCADAPTER_PEER_LISTENER)));
282 IListenerItemPtr listenerItem = DPL::StaticPointerCast<IListenerItem>(canceller);
283 NFCListenerManagerSingleton::Instance().registerListener(listenerItem, global_context);
285 return JSValueMakeUndefined(context);
286 } Catch (BasePlatformException) {
287 LoggerE(_rethrown_exception.getName() << ": " << _rethrown_exception.getMessage());
288 return JSWebAPIErrorFactory::postException(context, exception, _rethrown_exception);
289 } Catch (ConversionException) {
290 LoggerE("JSNFCAdapter::setPeerListener : ConversionException");
291 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
292 } Catch (PlatformException) {
293 LoggerE("PlatformException: " << _rethrown_exception.GetMessage());
294 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::SERVICE_NOT_AVAILABLE_ERROR, "Service Not Available");
295 } Catch (WrtDeviceApis::Commons::UnknownException) {
296 LoggerE("UnknownException: " << _rethrown_exception.GetMessage());
297 } Catch (WrtDeviceApis::Commons::Exception) {
298 LoggerE("Exception: " << _rethrown_exception.GetMessage());
300 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error");
302 JSValueRef JSNFCAdapter::unsetTagListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
303 size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) {
304 LoggerD("JSNFCAdapter::unsetNFCTagListener Enter");
306 AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_TAG_FUNCS);
307 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
309 NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(thisObject));
310 if (!privateObject) {
311 LoggerE("private object is null");
312 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
316 INFCAdapterPtr nfcAdapter(privateObject->getObject());
318 nfcAdapter->unsetTagListener();
320 NFCListenerCancellerPtr canceller = NFCListenerCancellerPtr(new NFCListenerCanceller(privateObject->getContext(), thisObject, static_cast<long>(ID_NFCADAPTER_TAG_LISTENER)));
321 IListenerItemPtr listenerItem = DPL::StaticPointerCast<IListenerItem>(canceller);
322 NFCListenerManagerSingleton::Instance().unregisterListener(listenerItem);
324 return JSValueMakeUndefined(context);
325 } Catch (NullPointerException) {
326 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
327 } Catch (PlatformException) {
328 LoggerE("PlatformException: " << _rethrown_exception.GetMessage());
329 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::SERVICE_NOT_AVAILABLE_ERROR, "Service Not Available");
330 } Catch (WrtDeviceApis::Commons::Exception) {
331 LoggerE("Exception: " << _rethrown_exception.GetMessage());
333 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error");
336 JSValueRef JSNFCAdapter::unsetPeerListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
337 size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) {
338 LoggerD("JSNFCAdapter::unsetPeerListener Enter");
340 AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_P2P_FUNCS);
341 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
343 NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(thisObject));
344 if (!privateObject) {
345 LoggerE("private object is null");
346 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
350 INFCAdapterPtr nfcAdapter( privateObject->getObject() );
352 nfcAdapter->unsetPeerListener();
354 NFCListenerCancellerPtr canceller = NFCListenerCancellerPtr(new NFCListenerCanceller(privateObject->getContext(), thisObject, static_cast<long>(ID_NFCADAPTER_PEER_LISTENER)));
355 IListenerItemPtr listenerItem = DPL::StaticPointerCast<IListenerItem>(canceller);
356 NFCListenerManagerSingleton::Instance().unregisterListener(listenerItem);
358 return JSValueMakeUndefined(context);
359 } Catch (NullPointerException) {
360 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
361 } Catch (PlatformException) {
362 LoggerE("PlatformException: " << _rethrown_exception.GetMessage());
363 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::SERVICE_NOT_AVAILABLE_ERROR, "Service Not Available");
364 } Catch (WrtDeviceApis::Commons::Exception) {
365 LoggerE("Exception: " << _rethrown_exception.GetMessage());
367 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error");
370 JSValueRef JSNFCAdapter::getCachedMessage (JSContextRef context, JSObjectRef object,
371 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
372 JSValueRef* exception) {
373 LoggerD("JSNFCAdapter::getCachedMessage Enter");
375 AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_COMMON_FUNCS);
376 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
378 NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(thisObject));
379 if (!privateObject) {
380 LoggerE("private object is null");
381 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
384 JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(context);
386 INFCAdapterPtr nfcAdapter(privateObject->getObject());
387 void *cachedMessage = nfcAdapter->getCachedMessage();
388 if (cachedMessage != NULL)
389 return JSNdefMessage::createJSObject(global_context, cachedMessage);
390 return JSValueMakeNull(context);
391 } Catch (WrtDeviceApis::Commons::UnknownException) {
392 LoggerE("Exception: " << _rethrown_exception.GetMessage());
393 } Catch (PlatformException) {
394 LoggerE("Exception: " << _rethrown_exception.GetMessage());
395 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::SERVICE_NOT_AVAILABLE_ERROR, "Service Not Available");
396 } Catch(NullPointerException) {
397 LoggerE("Exception: " << _rethrown_exception.GetMessage());
398 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
399 } Catch (WrtDeviceApis::Commons::Exception) {
400 LoggerE("Exception: " << _rethrown_exception.GetMessage());
402 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error");
405 JSValueRef JSNFCAdapter::setPowered (JSContextRef context, JSObjectRef object,
406 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
407 JSValueRef* exception) {
410 AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_ADMIN_FUNCS);
411 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
413 JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(context);
415 NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(thisObject));
416 if (!privateObject) {
417 LoggerE("private object is null");
418 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
421 INFCAdapterPtr nfcAdapter(privateObject->getObject());
422 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(global_context, NULL, NULL, true, true);
425 ArgumentValidator validator(context, argumentCount, arguments);
428 bool state = validator.toBool(0);
430 if (validator.toFunction(1, true))
431 callbackManager->setOnSuccess(arguments[1]);
433 if (validator.toFunction(2, true))
434 callbackManager->setOnError(arguments[2]);
436 NFCConverter nfcConverter(context);
438 EventNFCChangedSetPoweredPtr event(new EventNFCChangedSetPowered(state));
439 event->setPrivateData( DPL::StaticPointerCast<IEventPrivateData>(callbackManager) );
440 event->setForAsynchronousCall(&NFCStaticController::getInstance());
441 nfcAdapter->setPowered(event);
442 NFCAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, global_context);
443 return JSValueMakeUndefined(context);
444 } Catch (BasePlatformException) {
445 LoggerE(_rethrown_exception.getName() << ": " << _rethrown_exception.getMessage());
446 return JSWebAPIErrorFactory::postException(context, exception, _rethrown_exception);
447 } Catch (ConversionException) {
448 LoggerE("ConversionException");
449 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
450 } Catch (PlatformException) {
451 LoggerE("PlatformException: " << _rethrown_exception.GetMessage());
452 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::SERVICE_NOT_AVAILABLE_ERROR, "Service Not Available"));
453 return JSValueMakeUndefined(context);
454 } Catch (WrtDeviceApis::Commons::UnknownException) {
455 LoggerE("UnknownException: " << _rethrown_exception.GetMessage());
456 } Catch(NullPointerException) {
457 LoggerE("Exception: " << _rethrown_exception.GetMessage());
458 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
459 } Catch (WrtDeviceApis::Commons::Exception) {
460 LoggerE("Exception: " << _rethrown_exception.GetMessage());
462 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::UNKNOWN_ERROR,"Unknown Error"));
463 return JSValueMakeUndefined(context);