diff options
Diffstat (limited to 'mobile_src/NFC/NFCStaticController.cpp')
-rwxr-xr-x | mobile_src/NFC/NFCStaticController.cpp | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/mobile_src/NFC/NFCStaticController.cpp b/mobile_src/NFC/NFCStaticController.cpp new file mode 100755 index 0000000..38c6d98 --- /dev/null +++ b/mobile_src/NFC/NFCStaticController.cpp @@ -0,0 +1,136 @@ +// +// 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 <dpl/shared_ptr.h> + +#include <JavaScriptCore/JavaScript.h> +#include <CommonsJavaScript/PrivateObject.h> +#include <CommonsJavaScript/JSCallbackManager.h> +#include <CommonsJavaScript/ScopedJSStringRef.h> +#include <Commons/IEvent.h> +#include <JSWebAPIErrorFactory.h> + +#include "NFCStaticController.h" +#include "EventNFCChangedPrivateData.h" +#include "JSNFCTag.h" +#include "JSNFCTarget.h" +#include "NFCConverter.h" +#include "NFCAsyncCallbackManager.h" +#include <Logger.h> + +using namespace DeviceAPI::Common; +using namespace WrtDeviceApis::CommonsJavaScript; +using namespace WrtDeviceApis::Commons; + +namespace DeviceAPI { +namespace NFC { + +NFCStaticController& NFCStaticController::getInstance() { + static NFCStaticController controller; + return controller; +} + +NFCStaticController::NFCStaticController() : + EventNFCChangedListener(ThreadEnum::NULL_THREAD), + setPoweredAnswerReceiver(ThreadEnum::NULL_THREAD) { +} + +void NFCStaticController::onAnswerReceived(const EventNFCChangedPtr& event) { + LoggerD("onAnswerReceived Enter"); + + EventNFCChangedPrivateDataPtr privateData = + DPL::DynamicPointerCast<EventNFCChangedPrivateData>(event->getPrivateData()); + + void *nfcProp = event->getNFCProperties(); + + Try { + /** + *When received answer from platform, create an NFCTag JSObject and then call success function. + */ + if (nfcProp != NULL) { + JSObjectRef nfcObj; + JSCallbackManagerPtr callbackManager = privateData->getCallbackManager(); + JSContextRef context = callbackManager->getContext(); + + if (event->getNFCType() == NFC_TAG_TYPE) + nfcObj = JSNFCTag::createJSObject(context, nfcProp, privateData); + else + nfcObj = JSNFCTarget::createJSObject(context, nfcProp, privateData); + LoggerD("callOnSuccess"); + callbackManager->callOnSuccess(static_cast<JSValueRef>(nfcObj)); + } else { + JSCallbackManagerPtr detachedCallbackManager = privateData->getDetachedCallbackManager(); + + detachedCallbackManager->callOnSuccess(); + } + } Catch (ConversionException) { + LoggerE("Conversion exception while processing EventNFCChanged"); + } Catch (PlatformException) { + LoggerE("PlatformException:Platform can't create NFCTag" << _rethrown_exception.GetMessage()); + } Catch (WrtDeviceApis::Commons::UnknownException) { + LoggerE("UnknownException:Platform can't create NFCTag" << _rethrown_exception.GetMessage()); + } Catch (NullPointerException) { + LoggerE("NullPointer exception while processing EventNFCChanged"); + } + +} + +void NFCStaticController::OnAnswerReceived(const EventNFCChangedSetPoweredPtr &event) +{ + JSCallbackManagerPtr cbm = + DPL::StaticPointerCast<JSCallbackManager>(event->getPrivateData()); + + Try { + if (!cbm) { + LoggerD("no callback manager"); + return; + } + NFCAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(cbm); + if (event->getResult()) { + LoggerD("result success"); + cbm->callOnSuccess(); + return; + } + LoggerD("result fail"); + std::string error = event->getError(); + std::string errorMessage = event->getErrorMessage(); + JSValueRef errorObject; + if (error != "") { + if (errorMessage != "") + errorObject = JSWebAPIErrorFactory::makeErrorObject(cbm->getContext(), error, errorMessage); + else + errorObject = JSWebAPIErrorFactory::makeErrorObject(cbm->getContext(), error, error); + } else + errorObject = JSWebAPIErrorFactory::makeErrorObject(cbm->getContext(), JSWebAPIErrorFactory::UNKNOWN_ERROR,"Unknown Error"); + cbm->callOnError(errorObject); + } Catch (PlatformException) { + LoggerE("PlatformException" << _rethrown_exception.GetMessage()); + JSValueRef errorObject = JSWebAPIErrorFactory::makeErrorObject(cbm->getContext(), JSWebAPIErrorFactory::SERVICE_NOT_AVAILABLE_ERROR,"Service Not Available"); + cbm->callOnError(errorObject); + } Catch (WrtDeviceApis::Commons::UnknownException) { + LoggerE("UnknownException" << _rethrown_exception.GetMessage()); + JSValueRef errorObject = JSWebAPIErrorFactory::makeErrorObject(cbm->getContext(), JSWebAPIErrorFactory::UNKNOWN_ERROR,"Unknown Error"); + cbm->callOnError(errorObject); + } Catch (Exception) { + LoggerE("Exception: " << _rethrown_exception.GetMessage()); + JSValueRef errorObject = JSWebAPIErrorFactory::makeErrorObject(cbm->getContext(), JSWebAPIErrorFactory::UNKNOWN_ERROR,"Unknown Error"); + cbm->callOnError(errorObject); + } +} + +} // NFC +} // DeviceAPI |