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 <dpl/shared_ptr.h>
20 #include <JavaScriptCore/JavaScript.h>
21 #include <CommonsJavaScript/PrivateObject.h>
22 #include <CommonsJavaScript/JSCallbackManager.h>
23 #include <CommonsJavaScript/ScopedJSStringRef.h>
24 #include <Commons/IEvent.h>
25 #include <JSTizenExceptionFactory.h>
26 #include <JSTizenException.h>
28 #include "NFCStaticController.h"
29 #include "EventNFCChangedPrivateData.h"
31 #include "JSNFCTarget.h"
32 #include "NFCConverter.h"
33 #include "NFCAsyncCallbackManager.h"
36 using namespace DeviceAPI::Common;
37 using namespace WrtDeviceApis::CommonsJavaScript;
38 using namespace WrtDeviceApis::Commons;
43 NFCStaticController& NFCStaticController::getInstance() {
44 static NFCStaticController controller;
48 NFCStaticController::NFCStaticController() :
49 EventNFCChangedListener(ThreadEnum::NULL_THREAD),
50 setPoweredAnswerReceiver(ThreadEnum::NULL_THREAD) {
53 void NFCStaticController::onAnswerReceived(const EventNFCChangedPtr& event) {
54 LoggerD("onAnswerReceived Enter");
56 EventNFCChangedPrivateDataPtr privateData =
57 DPL::DynamicPointerCast<EventNFCChangedPrivateData>(event->getPrivateData());
59 void *nfcProp = event->getNFCProperties();
63 *When received answer from platform, create an NFCTag JSObject and then call success function.
65 if (nfcProp != NULL) {
67 JSCallbackManagerPtr callbackManager = privateData->getCallbackManager();
68 JSContextRef context = callbackManager->getContext();
70 if (event->getNFCType() == NFC_TAG_TYPE)
71 nfcObj = JSNFCTag::createJSObject(context, nfcProp);
73 nfcObj = JSNFCTarget::createJSObject(context, nfcProp);
74 LoggerD("callOnSuccess");
75 callbackManager->callOnSuccess(static_cast<JSValueRef>(nfcObj));
77 JSCallbackManagerPtr detachedCallbackManager = privateData->getDetachedCallbackManager();
79 detachedCallbackManager->callOnSuccess();
81 } Catch (ConversionException) {
82 LoggerE("Conversion exception while processing EventNFCChanged");
83 } Catch (PlatformException) {
84 LoggerE("PlatformException:Platform can't create NFCTag" << _rethrown_exception.GetMessage());
85 } Catch (UnknownException) {
86 LoggerE("UnknownException:Platform can't create NFCTag" << _rethrown_exception.GetMessage());
87 } Catch (NullPointerException) {
88 LoggerE("NullPointer exception while processing EventNFCChanged");
93 void NFCStaticController::OnAnswerReceived(const EventNFCChangedSetPoweredPtr &event)
95 JSCallbackManagerPtr cbm =
96 DPL::StaticPointerCast<JSCallbackManager>(event->getPrivateData());
100 LoggerD("no callback manager");
103 NFCAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(cbm);
104 if (event->getResult()) {
105 LoggerD("result success");
106 cbm->callOnSuccess();
109 LoggerD("result fail");
110 std::string error = event->getError();
111 std::string errorMessage = event->getErrorMessage();
112 JSValueRef errorObject;
114 if (errorMessage != "")
115 errorObject = JSTizenExceptionFactory::makeErrorObject(cbm->getContext(), error, errorMessage);
117 errorObject = JSTizenExceptionFactory::makeErrorObject(cbm->getContext(), error, error);
119 errorObject = JSTizenExceptionFactory::makeErrorObject(cbm->getContext(), JSTizenException::UNKNOWN_ERROR,"Unknown Error");
120 cbm->callOnError(errorObject);
121 } Catch (PlatformException) {
122 LoggerE("PlatformException" << _rethrown_exception.GetMessage());
123 JSValueRef errorObject = JSTizenExceptionFactory::makeErrorObject(cbm->getContext(), JSTizenException::SERVICE_NOT_AVAILABLE,"Service Not Available");
124 cbm->callOnError(errorObject);
125 } Catch (UnknownException) {
126 LoggerE("UnknownException" << _rethrown_exception.GetMessage());
127 JSValueRef errorObject = JSTizenExceptionFactory::makeErrorObject(cbm->getContext(), JSTizenException::UNKNOWN_ERROR,"Unknown Error");
128 cbm->callOnError(errorObject);
129 } Catch (Exception) {
130 LoggerE("Exception: " << _rethrown_exception.GetMessage());
131 JSValueRef errorObject = JSTizenExceptionFactory::makeErrorObject(cbm->getContext(), JSTizenException::UNKNOWN_ERROR,"Unknown Error");
132 cbm->callOnError(errorObject);