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_impl.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, NFCManagerPrivObject *nfcManagerPriv) {
114 INFCAdapterPtr nfcAdapter(NFCFactory::getInstance().createNFCAdapterObject());
115 NFCAdapterPrivObject *priv = new NFCAdapterPrivObject(context, nfcAdapter);
118 ThrowMsg(NullPointerException, "Can not new a NFCTag object");
121 priv->copyAceCheckAccessFunction(nfcManagerPriv);
122 return JSObjectMake(context, getClassRef(), priv);
125 const JSClassRef JSNFCAdapter::getClassRef()
128 m_jsClassRef = JSClassCreate(&m_classInfo);
133 const JSClassDefinition* JSNFCAdapter::getClassInfo()
138 JSValueRef JSNFCAdapter::getProperty(JSContextRef context, JSObjectRef object,
139 JSStringRef propertyName, JSValueRef* exception)
144 NFCConverter convert(context);
146 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_NFCADAPTER_POWERED)) {
147 NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(object));
148 if (!privateObject) {
149 LoggerE("private object is null");
150 ThrowMsg(NullPointerException, "Private object not initialized");
152 INFCAdapterPtr nfcAdapter(privateObject->getObject());
153 return convert.toJSValueRef(nfcAdapter->getPowerState());
155 } Catch (NullPointerException) {
156 LoggerE("NullPointerException: " << _rethrown_exception.GetMessage());
157 } Catch (ConversionException) {
158 LoggerE("ConversionException: " << _rethrown_exception.GetMessage());
159 } Catch (UnsupportedException) {
160 LoggerE("UnsupportedException: " << _rethrown_exception.GetMessage());
161 } Catch (WrtDeviceApis::Commons::UnknownException) {
162 LoggerE("UnknownExceptionException: " << _rethrown_exception.GetMessage());
163 } Catch (PlatformException) {
164 LoggerE("PlatformExceptionException: " << _rethrown_exception.GetMessage());
165 } Catch (WrtDeviceApis::Commons::Exception) {
166 LoggerE("Exception: " << _rethrown_exception.GetMessage());
169 return JSValueMakeUndefined(context);
172 JSValueRef JSNFCAdapter::setTagListener (JSContextRef context, JSObjectRef object,
173 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
174 JSValueRef* exception) {
175 LoggerD("JSNFCAdapter::setTagListener Enter");
177 NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(thisObject));
178 if (!privateObject) {
179 LoggerE("private object is null");
180 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
183 TIZEN_CHECK_ACCESS(context, exception, privateObject,
184 NFC_FUNCTION_API_TAG_FUNCS);
187 ArgumentValidator validator(context, argumentCount, arguments);
188 NFCChangedCallback callbacks;
189 NFCConverter nfcConverter(context);
190 if (validator.toObject(0))
191 callbacks = nfcConverter.toNFCChangedCallback(arguments[0]);
193 std::vector<std::string> filterValue = validator.toStringVector(1, true);
195 JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(context);
196 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(global_context, callbacks.onattach, NULL, true, true);
197 JSCallbackManagerPtr detachedCallbackManager = JSCallbackManager::createObject(global_context, callbacks.ondetach, NULL);
198 TagFilterPtr tagFilter(new TagFilter());
199 for (unsigned int i=0; i<filterValue.size(); i++) {
200 nfcTagType filter = nfcConverter.toNfcTagType(filterValue.at(i));
202 tagFilter->addTagTypeValue(filter);
203 LoggerD("tag_types.at(i): " << filterValue.at(i));
206 INFCAdapterPtr nfcAdapter(privateObject->getObject());
208 EventNFCChangedPrivateDataPtr privateData(
209 new EventNFCChangedPrivateData(callbackManager,
210 detachedCallbackManager)
212 privateData->copyAceCheckAccessFunction(privateObject);
214 EventNFCChangedEmitterPtr emitter(new EventNFCChangedEmitter);
215 emitter->setListener(&NFCStaticController::getInstance());
216 emitter->setEventPrivateData(DPL::StaticPointerCast<EventNFCChanged::PrivateDataType>(privateData));
218 if (nfcAdapter->setTagListener(emitter, tagFilter) != 0)
219 Throw(WrtDeviceApis::Commons::UnknownException);
221 NFCListenerCancellerPtr canceller = NFCListenerCancellerPtr(new NFCListenerCanceller(global_context, thisObject, static_cast<long>(ID_NFCADAPTER_TAG_LISTENER)));
222 IListenerItemPtr listenerItem = DPL::StaticPointerCast<IListenerItem>(canceller);
223 NFCListenerManagerSingleton::Instance().registerListener(listenerItem, global_context);
225 return JSValueMakeUndefined(context);
226 } Catch (BasePlatformException) {
227 LoggerE(_rethrown_exception.getName() << ": " << _rethrown_exception.getMessage());
228 return JSWebAPIErrorFactory::postException(context, exception, _rethrown_exception);
229 } Catch (ConversionException) {
230 LoggerE("JSNFCAdapter::setTagListener : ConversionException");
231 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
232 } Catch (PlatformException) {
233 LoggerE("PlatformException: " << _rethrown_exception.GetMessage());
234 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::SERVICE_NOT_AVAILABLE_ERROR, "Service Not Available");
235 } Catch (WrtDeviceApis::Commons::UnknownException) {
236 LoggerE("UnknownException: " << _rethrown_exception.GetMessage());
237 } Catch (WrtDeviceApis::Commons::Exception) {
238 LoggerE("Exception: " << _rethrown_exception.GetMessage());
240 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error");
243 JSValueRef JSNFCAdapter::setPeerListener (JSContextRef context, JSObjectRef object,
244 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
245 JSValueRef* exception) {
246 LoggerD("JSNFCAdapter::setPeerListener Enter");
248 NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(thisObject));
249 if (!privateObject) {
250 LoggerE("private object is null");
251 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
254 TIZEN_CHECK_ACCESS(context, exception, privateObject,
255 NFC_FUNCTION_API_P2P_FUNCS);
258 ArgumentValidator validator(context, argumentCount, arguments);
260 NFCConverter nfcConverter(context);
261 NFCChangedCallback callbacks;
263 if (validator.toObject(0))
264 callbacks = nfcConverter.toNFCChangedCallback(arguments[0]);
266 JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(context);
267 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(global_context, callbacks.onattach, NULL, true, true);
268 JSCallbackManagerPtr detachedCallbackManager = JSCallbackManager::createObject(global_context, callbacks.ondetach, NULL);
270 INFCAdapterPtr nfcAdapter(privateObject->getObject());
272 EventNFCChangedPrivateDataPtr privateData(
273 new EventNFCChangedPrivateData(callbackManager,
274 detachedCallbackManager)
277 privateData->copyAceCheckAccessFunction(privateObject);
278 EventNFCChangedEmitterPtr emitter(new EventNFCChangedEmitter);
279 emitter->setListener(&NFCStaticController::getInstance());
280 emitter->setEventPrivateData(DPL::StaticPointerCast<EventNFCChanged::PrivateDataType>(privateData));
282 if (nfcAdapter->setPeerListener(emitter) != 0)
283 Throw(WrtDeviceApis::Commons::UnknownException);
285 NFCListenerCancellerPtr canceller = NFCListenerCancellerPtr(new NFCListenerCanceller(global_context, thisObject, static_cast<long>(ID_NFCADAPTER_PEER_LISTENER)));
286 IListenerItemPtr listenerItem = DPL::StaticPointerCast<IListenerItem>(canceller);
287 NFCListenerManagerSingleton::Instance().registerListener(listenerItem, global_context);
289 return JSValueMakeUndefined(context);
290 } Catch (BasePlatformException) {
291 LoggerE(_rethrown_exception.getName() << ": " << _rethrown_exception.getMessage());
292 return JSWebAPIErrorFactory::postException(context, exception, _rethrown_exception);
293 } Catch (ConversionException) {
294 LoggerE("JSNFCAdapter::setPeerListener : ConversionException");
295 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
296 } Catch (PlatformException) {
297 LoggerE("PlatformException: " << _rethrown_exception.GetMessage());
298 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::SERVICE_NOT_AVAILABLE_ERROR, "Service Not Available");
299 } Catch (WrtDeviceApis::Commons::UnknownException) {
300 LoggerE("UnknownException: " << _rethrown_exception.GetMessage());
301 } Catch (WrtDeviceApis::Commons::Exception) {
302 LoggerE("Exception: " << _rethrown_exception.GetMessage());
304 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error");
306 JSValueRef JSNFCAdapter::unsetTagListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
307 size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) {
308 LoggerD("JSNFCAdapter::unsetNFCTagListener Enter");
310 NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(thisObject));
311 if (!privateObject) {
312 LoggerE("private object is null");
313 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
316 TIZEN_CHECK_ACCESS(context, exception, privateObject,
317 NFC_FUNCTION_API_TAG_FUNCS);
320 INFCAdapterPtr nfcAdapter(privateObject->getObject());
322 nfcAdapter->unsetTagListener();
324 NFCListenerCancellerPtr canceller = NFCListenerCancellerPtr(new NFCListenerCanceller(privateObject->getContext(), thisObject, static_cast<long>(ID_NFCADAPTER_TAG_LISTENER)));
325 IListenerItemPtr listenerItem = DPL::StaticPointerCast<IListenerItem>(canceller);
326 NFCListenerManagerSingleton::Instance().unregisterListener(listenerItem);
328 return JSValueMakeUndefined(context);
329 } Catch (NullPointerException) {
330 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
331 } Catch (PlatformException) {
332 LoggerE("PlatformException: " << _rethrown_exception.GetMessage());
333 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::SERVICE_NOT_AVAILABLE_ERROR, "Service Not Available");
334 } Catch (WrtDeviceApis::Commons::Exception) {
335 LoggerE("Exception: " << _rethrown_exception.GetMessage());
337 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error");
340 JSValueRef JSNFCAdapter::unsetPeerListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
341 size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) {
342 LoggerD("JSNFCAdapter::unsetPeerListener Enter");
344 NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(thisObject));
345 if (!privateObject) {
346 LoggerE("private object is null");
347 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
350 TIZEN_CHECK_ACCESS(context, exception, privateObject,
351 NFC_FUNCTION_API_P2P_FUNCS);
353 INFCAdapterPtr nfcAdapter( privateObject->getObject());
355 nfcAdapter->unsetPeerListener();
357 NFCListenerCancellerPtr canceller = NFCListenerCancellerPtr(new NFCListenerCanceller(privateObject->getContext(), thisObject, static_cast<long>(ID_NFCADAPTER_PEER_LISTENER)));
358 IListenerItemPtr listenerItem = DPL::StaticPointerCast<IListenerItem>(canceller);
359 NFCListenerManagerSingleton::Instance().unregisterListener(listenerItem);
361 return JSValueMakeUndefined(context);
362 } Catch (NullPointerException) {
363 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
364 } Catch (PlatformException) {
365 LoggerE("PlatformException: " << _rethrown_exception.GetMessage());
366 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::SERVICE_NOT_AVAILABLE_ERROR, "Service Not Available");
367 } Catch (WrtDeviceApis::Commons::Exception) {
368 LoggerE("Exception: " << _rethrown_exception.GetMessage());
370 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error");
373 JSValueRef JSNFCAdapter::getCachedMessage (JSContextRef context, JSObjectRef object,
374 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
375 JSValueRef* exception) {
376 LoggerD("JSNFCAdapter::getCachedMessage Enter");
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 TIZEN_CHECK_ACCESS(context, exception, privateObject,
385 NFC_FUNCTION_API_COMMON_FUNCS);
388 INFCAdapterPtr nfcAdapter(privateObject->getObject());
389 void *cachedMessage = nfcAdapter->getCachedMessage();
390 if (cachedMessage != NULL) {
391 JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(context);
392 return JSNdefMessage::createJSObject(global_context, cachedMessage);
394 return JSValueMakeNull(context);
395 } Catch (WrtDeviceApis::Commons::UnknownException) {
396 LoggerE("Exception: " << _rethrown_exception.GetMessage());
397 } Catch (PlatformException) {
398 LoggerE("Exception: " << _rethrown_exception.GetMessage());
399 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::SERVICE_NOT_AVAILABLE_ERROR, "Service Not Available");
400 } Catch(NullPointerException) {
401 LoggerE("Exception: " << _rethrown_exception.GetMessage());
402 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
403 } Catch (WrtDeviceApis::Commons::Exception) {
404 LoggerE("Exception: " << _rethrown_exception.GetMessage());
406 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error");
409 JSValueRef JSNFCAdapter::setPowered (JSContextRef context, JSObjectRef object,
410 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
411 JSValueRef* exception) {
414 NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(thisObject));
415 if (!privateObject) {
416 LoggerE("private object is null");
417 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
420 TIZEN_CHECK_ACCESS(context, exception, privateObject,
421 NFC_FUNCTION_API_ADMIN_FUNCS);
423 JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(context);
424 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(global_context, NULL, NULL, true, true);
427 ArgumentValidator validator(context, argumentCount, arguments);
430 bool state = validator.toBool(0);
432 if (validator.toFunction(1, true))
433 callbackManager->setOnSuccess(arguments[1]);
435 if (validator.toFunction(2, true))
436 callbackManager->setOnError(arguments[2]);
438 EventNFCChangedSetPoweredPtr event(new EventNFCChangedSetPowered(state));
439 event->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManager) );
440 event->setForAsynchronousCall(&NFCStaticController::getInstance());
441 callbackManager->setObject(thisObject);
442 INFCAdapterPtr nfcAdapter(privateObject->getObject());
443 nfcAdapter->setPowered(event);
444 NFCAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, global_context);
445 return JSValueMakeUndefined(context);
446 } Catch (BasePlatformException) {
447 LoggerE(_rethrown_exception.getName() << ": " << _rethrown_exception.getMessage());
448 return JSWebAPIErrorFactory::postException(context, exception, _rethrown_exception);
449 } Catch (ConversionException) {
450 LoggerE("ConversionException");
451 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
452 } Catch (PlatformException) {
453 LoggerE("PlatformException: " << _rethrown_exception.GetMessage());
454 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::SERVICE_NOT_AVAILABLE_ERROR, "Service Not Available"));
455 return JSValueMakeUndefined(context);
456 } Catch (WrtDeviceApis::Commons::UnknownException) {
457 LoggerE("UnknownException: " << _rethrown_exception.GetMessage());
458 } Catch(NullPointerException) {
459 LoggerE("Exception: " << _rethrown_exception.GetMessage());
460 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
461 } Catch (WrtDeviceApis::Commons::Exception) {
462 LoggerE("Exception: " << _rethrown_exception.GetMessage());
464 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::UNKNOWN_ERROR,"Unknown Error"));
465 return JSValueMakeUndefined(context);