wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / NFC / NFCStaticController.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 #include <dpl/shared_ptr.h>
19
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>
27
28 #include "NFCStaticController.h"
29 #include "EventNFCChangedPrivateData.h"
30 #include "JSNFCTag.h"
31 #include "JSNFCTarget.h"
32 #include "NFCConverter.h"
33 #include "NFCAsyncCallbackManager.h"
34 #include <Logger.h>
35
36 using namespace DeviceAPI::Common;
37 using namespace WrtDeviceApis::CommonsJavaScript;
38 using namespace WrtDeviceApis::Commons;
39
40 namespace DeviceAPI {
41 namespace NFC {
42
43 NFCStaticController& NFCStaticController::getInstance() {
44         static NFCStaticController controller;
45         return controller;
46 }
47
48 NFCStaticController::NFCStaticController() :
49         EventNFCChangedListener(ThreadEnum::NULL_THREAD), 
50         setPoweredAnswerReceiver(ThreadEnum::NULL_THREAD) {
51 }
52
53 void NFCStaticController::onAnswerReceived(const EventNFCChangedPtr& event) {
54         LoggerD("onAnswerReceived Enter");
55
56         EventNFCChangedPrivateDataPtr privateData =
57         DPL::DynamicPointerCast<EventNFCChangedPrivateData>(event->getPrivateData());
58
59         void *nfcProp = event->getNFCProperties();
60
61         Try {
62                 /**
63                 *When received answer from platform, create an NFCTag JSObject and then call success function.
64                 */
65                 if (nfcProp != NULL) {
66                         JSObjectRef nfcObj;
67                         JSCallbackManagerPtr callbackManager = privateData->getCallbackManager();
68                         JSContextRef context = callbackManager->getContext();
69                         
70                         if (event->getNFCType() == NFC_TAG_TYPE)
71                                 nfcObj = JSNFCTag::createJSObject(context, nfcProp);
72                         else
73                                 nfcObj = JSNFCTarget::createJSObject(context, nfcProp);
74                         LoggerD("callOnSuccess");
75                         callbackManager->callOnSuccess(static_cast<JSValueRef>(nfcObj));
76                 } else {
77                         JSCallbackManagerPtr detachedCallbackManager = privateData->getDetachedCallbackManager();
78
79                         detachedCallbackManager->callOnSuccess();
80                 }
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");
89         }
90
91 }
92
93 void NFCStaticController::OnAnswerReceived(const EventNFCChangedSetPoweredPtr &event)
94 {
95         JSCallbackManagerPtr cbm =
96             DPL::StaticPointerCast<JSCallbackManager>(event->getPrivateData());
97
98         Try {
99                 if (!cbm) {
100                         LoggerD("no callback manager");
101                         return;
102                 }
103                 NFCAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(cbm);
104                 if (event->getResult()) {
105                         LoggerD("result success");
106                         cbm->callOnSuccess();
107                         return;
108                 }
109                 LoggerD("result fail");
110                 std::string error = event->getError();
111                 std::string errorMessage = event->getErrorMessage();
112                 JSValueRef errorObject;
113                 if (error != "") {
114                         if (errorMessage != "")
115                                 errorObject = JSTizenExceptionFactory::makeErrorObject(cbm->getContext(), error, errorMessage);
116                         else
117                                 errorObject = JSTizenExceptionFactory::makeErrorObject(cbm->getContext(), error, error);
118                 } else
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);  
133         }
134 }
135
136 } // NFC
137 } // DeviceAPI