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 <dpl/log/log.h>
20 #include <dpl/shared_ptr.h>
22 #include <JavaScriptCore/JavaScript.h>
23 #include <CommonsJavaScript/PrivateObject.h>
24 #include <CommonsJavaScript/JSCallbackManager.h>
25 #include <CommonsJavaScript/ScopedJSStringRef.h>
26 #include <Commons/IEvent.h>
27 #include <JSTizenExceptionFactory.h>
28 #include <JSTizenException.h>
30 #include "NFCStaticController.h"
31 #include "EventNFCChangedPrivateData.h"
33 #include "JSNFCTarget.h"
34 #include "JSNdefMessage.h"
35 #include "NFCConverter.h"
36 #include "NFCAsyncCallbackManager.h"
38 using namespace DeviceAPI::Common;
39 using namespace WrtDeviceApis::CommonsJavaScript;
40 using namespace WrtDeviceApis::Commons;
45 NFCStaticController& NFCStaticController::getInstance() {
46 static NFCStaticController controller;
50 NFCStaticController::NFCStaticController() :
51 EventNFCChangedListener(ThreadEnum::NULL_THREAD),
52 setPoweredAnswerReceiver(ThreadEnum::NULL_THREAD) {
55 void NFCStaticController::onAnswerReceived(const EventNFCChangedPtr& event) {
56 LogDebug("onAnswerReceived Enter");
58 EventNFCChangedPrivateDataPtr privateData =
59 DPL::DynamicPointerCast<EventNFCChangedPrivateData>(event->getPrivateData());
61 void *nfcProp = event->getNFCProperties();
65 *When received answer from platform, create an NFCTag JSObject and then call success function.
67 if (nfcProp != NULL) {
69 JSCallbackManagerPtr callbackManager = privateData->getCallbackManager();
70 JSContextRef context = callbackManager->getContext();
72 if (event->getNFCType() == NFC_TAG_TYPE)
73 nfcObj = JSNFCTag::createJSObject(context, nfcProp);
75 nfcObj = JSNFCTarget::createJSObject(context, nfcProp);
76 LogDebug("callOnSuccess");
77 callbackManager->callOnSuccess(static_cast<JSValueRef>(nfcObj));
79 JSCallbackManagerPtr detachedCallbackManager = privateData->getDetachedCallbackManager();
81 detachedCallbackManager->callOnSuccess();
83 } Catch (ConversionException) {
84 LogError("Conversion exception while processing EventNFCChanged");
85 } Catch (PlatformException) {
86 LogError("PlatformException:Platform can't create NFCTag" << _rethrown_exception.GetMessage());
87 } Catch (UnknownException) {
88 LogError("UnknownException:Platform can't create NFCTag" << _rethrown_exception.GetMessage());
89 } Catch (NullPointerException) {
90 LogError("NullPointer exception while processing EventNFCChanged");
95 void NFCStaticController::OnAnswerReceived(const EventNFCChangedSetPoweredPtr &event)
97 JSCallbackManagerPtr cbm =
98 DPL::StaticPointerCast<JSCallbackManager>(event->getPrivateData());
102 LogDebug("no callback manager");
105 NFCAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(cbm);
106 if (event->getResult()) {
107 LogDebug("result success");
108 cbm->callOnSuccess();
111 LogDebug("result fail");
112 std::string error = event->getError();
113 std::string errorMessage = event->getErrorMessage();
114 JSValueRef errorObject;
116 if (errorMessage != "")
117 errorObject = JSTizenExceptionFactory::makeErrorObject(cbm->getContext(), error, errorMessage);
119 errorObject = JSTizenExceptionFactory::makeErrorObject(cbm->getContext(), error, error);
121 errorObject = JSTizenExceptionFactory::makeErrorObject(cbm->getContext(), JSTizenException::UNKNOWN_ERROR,"Unknown Error");
122 cbm->callOnError(errorObject);
123 } Catch (PlatformException) {
124 LogError("PlatformException" << _rethrown_exception.GetMessage());
125 JSValueRef errorObject = JSTizenExceptionFactory::makeErrorObject(cbm->getContext(), JSTizenException::SERVICE_NOT_AVAILABLE,"Service Not Available");
126 cbm->callOnError(errorObject);
127 } Catch (UnknownException) {
128 LogError("UnknownException" << _rethrown_exception.GetMessage());
129 JSValueRef errorObject = JSTizenExceptionFactory::makeErrorObject(cbm->getContext(), JSTizenException::UNKNOWN_ERROR,"Unknown Error");
130 cbm->callOnError(errorObject);
131 } Catch (Exception) {
132 LogError("Exception: " << _rethrown_exception.GetMessage());
133 JSValueRef errorObject = JSTizenExceptionFactory::makeErrorObject(cbm->getContext(), JSTizenException::UNKNOWN_ERROR,"Unknown Error");
134 cbm->callOnError(errorObject);