diff options
Diffstat (limited to 'mobile_src/NFC/JSNFCManager.cpp')
-rwxr-xr-x | mobile_src/NFC/JSNFCManager.cpp | 260 |
1 files changed, 260 insertions, 0 deletions
diff --git a/mobile_src/NFC/JSNFCManager.cpp b/mobile_src/NFC/JSNFCManager.cpp new file mode 100755 index 0000000..1264ed0 --- /dev/null +++ b/mobile_src/NFC/JSNFCManager.cpp @@ -0,0 +1,260 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include <Commons/Exception.h> +#include <CommonsJavaScript/PrivateObject.h> +#include <JSWebAPIErrorFactory.h> +#include <SecurityExceptions.h> +#include <GlobalContextManager.h> +#include <ArgumentValidator.h> +#include <PlatformException.h> + +#include "JSNFCManager.h" +#include "JSNFCAdapter.h" +#include "NFCConverter.h" +#include "NFCFactory.h" +#include "plugin_config_impl.h" +#include "NFCDefaultAdapter.h" +#include <Logger.h> + +using namespace std; + +using namespace DeviceAPI::Common; +using namespace WrtDeviceApis::Commons; +using namespace WrtDeviceApis::CommonsJavaScript; + +#define TIZEN_NFCMANAGER_ATTRIBUTENAME "NFCManager" + +#define TIZEN_NFCMANAGER_TNF_EMPTY "NFC_RECORD_TNF_EMPTY" +#define TIZEN_NFCMANAGER_TNF_WELL_KNOWN "NFC_RECORD_TNF_WELL_KNOWN" +#define TIZEN_NFCMANAGER_TNF_MIME_MEDIA "NFC_RECORD_TNF_MIME_MEDIA" +#define TIZEN_NFCMANAGER_TNF_URI "NFC_RECORD_TNF_URI" +#define TIZEN_NFCMANAGER_TNF_EXTERNAL_RTD "NFC_RECORD_TNF_EXTERNAL_RTD" +#define TIZEN_NFCMANAGER_TNF_UNKNOWN "NFC_RECORD_TNF_UNKNOWN" +#define TIZEN_NFCMANAGER_TNF_UNCHANGED "NFC_RECORD_TNF_UNCHANGED" + +namespace DeviceAPI { +namespace NFC { + + JSClassDefinition JSNFCManager::m_classInfo = +{ + 0, + kJSClassAttributeNone, + TIZEN_NFCMANAGER_ATTRIBUTENAME, + 0, + m_property, + m_function, + initialize, + finalize, + NULL, //hasProperty, + NULL, + NULL, //setProperty, + NULL, //DeleteProperty, + NULL, //GetPropertyNames, + NULL, //CallAsFunction, + NULL, //CallAsConstructor, + NULL, //HasInstance, + NULL //ConvertToType +}; + +JSStaticValue JSNFCManager::m_property[] = +{ + //NFCManagerProperties + {TIZEN_NFCMANAGER_TNF_EMPTY, getProperty, NULL, kJSPropertyAttributeReadOnly}, + {TIZEN_NFCMANAGER_TNF_WELL_KNOWN, getProperty, NULL, kJSPropertyAttributeReadOnly}, + {TIZEN_NFCMANAGER_TNF_MIME_MEDIA, getProperty, NULL, kJSPropertyAttributeReadOnly}, + {TIZEN_NFCMANAGER_TNF_URI, getProperty, NULL, kJSPropertyAttributeReadOnly}, + {TIZEN_NFCMANAGER_TNF_EXTERNAL_RTD, getProperty, NULL, kJSPropertyAttributeReadOnly}, + {TIZEN_NFCMANAGER_TNF_UNKNOWN, getProperty, NULL, kJSPropertyAttributeReadOnly}, + {TIZEN_NFCMANAGER_TNF_UNCHANGED, getProperty, NULL, kJSPropertyAttributeReadOnly}, + { 0, 0, 0, 0 } +}; + +JSStaticFunction JSNFCManager::m_function[] = { + {"getDefaultAdapter", JSNFCManager::getDefaultAdapter, kJSPropertyAttributeNone }, + {"setExclusiveMode", JSNFCManager::setExclusiveMode, kJSPropertyAttributeNone }, + { 0, 0, 0} +}; + +JSClassRef JSNFCManager::m_jsClassRef = JSClassCreate(JSNFCManager::getClassInfo()); + +void JSNFCManager::initialize(JSContextRef context, JSObjectRef object) +{ + LoggerD("entered"); + NFCManagerPrivObject* priv = static_cast<NFCManagerPrivObject*>(JSObjectGetPrivate(object)); + if (!priv) { + Try { + priv = new NFCManagerPrivObject(context); + if(!JSObjectSetPrivate(object, static_cast<void*>(priv))) { + LoggerE("Object can't store private data."); + delete priv; + } + } Catch (UnsupportedException) { + LoggerE("UnsupportedException: " << _rethrown_exception.GetMessage()); + } Catch (WrtDeviceApis::Commons::UnknownException) { + LoggerE("UnknownExceptionException: " << _rethrown_exception.GetMessage()); + } Catch (PlatformException) { + LoggerE("PlatformExceptionException: " << _rethrown_exception.GetMessage()); + } Catch (WrtDeviceApis::Commons::Exception) { + LoggerE("Exception: " << _rethrown_exception.GetMessage()); + } + } +} + +void JSNFCManager::finalize(JSObjectRef object) +{ + LoggerD( "entered" ); + NFCManagerPrivObject *priv = static_cast<NFCManagerPrivObject*>(JSObjectGetPrivate(object)); + if (priv) { + JSObjectSetPrivate(object, NULL); + LoggerD("Deleting NFC Manager object"); + delete priv; + } +} + + +const JSClassRef JSNFCManager::getClassRef() +{ + if (!m_jsClassRef) { + m_jsClassRef = JSClassCreate(&m_classInfo); + } + return m_jsClassRef; +} + +const JSClassDefinition* JSNFCManager::getClassInfo() +{ + return &m_classInfo; +} + +JSValueRef JSNFCManager::getProperty(JSContextRef context, JSObjectRef object, + JSStringRef propertyName, JSValueRef* exception) +{ + LoggerD("Enter"); + + Try { + Converter convert(context); + + if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_NFCMANAGER_TNF_EMPTY)) { + return convert.toJSValueRef(NFC_TNF_EMPTY); + } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_NFCMANAGER_TNF_WELL_KNOWN)) { + return convert.toJSValueRef(NFC_TNF_WELL_KNOWN); + } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_NFCMANAGER_TNF_MIME_MEDIA)) { + return convert.toJSValueRef(NFC_TNF_MIME_MEDIA); + } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_NFCMANAGER_TNF_URI)) { + return convert.toJSValueRef(NFC_TNF_URI); + } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_NFCMANAGER_TNF_EXTERNAL_RTD)) { + return convert.toJSValueRef(NFC_TNF_EXTERNAL_RTD); + } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_NFCMANAGER_TNF_UNKNOWN)) { + return convert.toJSValueRef(NFC_TNF_UNKNOWN); + } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_NFCMANAGER_TNF_UNCHANGED)) { + return convert.toJSValueRef(NFC_TNF_UNCHANGED); + } + } Catch (ConversionException) { + LoggerE("ConversionException: " << _rethrown_exception.GetMessage()); + } Catch (UnsupportedException) { + LoggerE("UnsupportedException: " << _rethrown_exception.GetMessage()); + } Catch (WrtDeviceApis::Commons::UnknownException) { + LoggerE("UnknownExceptionException: " << _rethrown_exception.GetMessage()); + } Catch (PlatformException) { + LoggerE("PlatformExceptionException: " << _rethrown_exception.GetMessage()); + } Catch (WrtDeviceApis::Commons::Exception) { + LoggerE("Exception: " << _rethrown_exception.GetMessage()); + } + + return JSValueMakeUndefined(context); +} + +JSValueRef JSNFCManager::getDefaultAdapter (JSContextRef context, JSObjectRef object, + JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], + JSValueRef* exception) { + LoggerD("Enter"); + + Try { + NFCManagerPrivObject* privateObject = static_cast<NFCManagerPrivObject*>(JSObjectGetPrivate(thisObject)); + if (!privateObject) { + LoggerE("private object is null"); + ThrowMsg(UnsupportedException, "private object is null"); + } + + TIZEN_CHECK_ACCESS(context, exception, privateObject, + NFC_FUNCTION_API_COMMON_FUNCS); + + return JSNFCAdapter::createJSObject(GlobalContextManager::getInstance()->getGlobalContext(context), privateObject); + } Catch (UnsupportedException) { + LoggerE("UnsupportedException: " << _rethrown_exception.GetMessage()); + return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, "Not Supported"); + } Catch (ConversionException) { + LoggerE("ConversionException: " << _rethrown_exception.GetMessage()); + return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch"); + } Catch (NullPointerException) { + LoggerE("NullPointerException: " << _rethrown_exception.GetMessage()); + return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch"); + } Catch (InvalidArgumentException) { + LoggerE("InvalidArgumentException: " << _rethrown_exception.GetMessage()); + return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "Invalid Values"); + } Catch (PlatformException) { + LoggerE("PlatformException: " << _rethrown_exception.GetMessage()); + return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::SERVICE_NOT_AVAILABLE_ERROR, "Service Not Available"); + } Catch (WrtDeviceApis::Commons::UnknownException) { + LoggerE("UnknownException: " << _rethrown_exception.GetMessage()); + } Catch (WrtDeviceApis::Commons::Exception) { + LoggerE("Exception: " << _rethrown_exception.GetMessage()); + } + return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error"); +} + +JSValueRef JSNFCManager::setExclusiveMode (JSContextRef context, JSObjectRef object, + JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], + JSValueRef* exception) { + LoggerD("Enter"); + + Try { + NFCManagerPrivObject* privateObject = static_cast<NFCManagerPrivObject*>(JSObjectGetPrivate(thisObject)); + if (!privateObject) { + LoggerE("private object is null"); + throw TypeMismatchException("private object is null"); + } + + TIZEN_CHECK_ACCESS(context, exception, privateObject, + NFC_FUNCTION_API_COMMON_FUNCS); + + ArgumentValidator validator(context, argumentCount, arguments); + // mode + bool mode = validator.toBool(0); + LoggerD("mode : " << mode); + + NFCDefaultAdapterSingleton::Instance().setExclusiveMode(mode); + return JSValueMakeUndefined(context); + } Catch (BasePlatformException) { + LoggerE(_rethrown_exception.getName() << ": " << _rethrown_exception.getMessage()); + return JSWebAPIErrorFactory::postException(context, exception, _rethrown_exception); + } Catch (ConversionException) { + LoggerE("ConversionException: " << _rethrown_exception.GetMessage()); + return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch"); + } Catch (NullPointerException) { + LoggerE("NullPointerException: " << _rethrown_exception.GetMessage()); + return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch"); + } Catch (WrtDeviceApis::Commons::UnknownException) { + LoggerE("UnknownException: " << _rethrown_exception.GetMessage()); + } Catch (WrtDeviceApis::Commons::Exception) { + LoggerE("Exception: " << _rethrown_exception.GetMessage()); + } + return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error"); +} + +} +} |