Update change log and spec for wrt-plugins-tizen_0.4.13
[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
19 #include <dpl/log/log.h>
20 #include <dpl/shared_ptr.h>
21
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>
29
30 #include "NFCStaticController.h"
31 #include "EventNFCChangedPrivateData.h"
32 #include "JSNFCTag.h"
33 #include "JSNFCTarget.h"
34 #include "JSNdefMessage.h"
35 #include "NFCConverter.h"
36 #include "NFCAsyncCallbackManager.h"
37
38 using namespace DeviceAPI::Common;
39 using namespace WrtDeviceApis::CommonsJavaScript;
40 using namespace WrtDeviceApis::Commons;
41
42 namespace DeviceAPI {
43 namespace NFC {
44
45 NFCStaticController& NFCStaticController::getInstance() {
46         static NFCStaticController controller;
47         return controller;
48 }
49
50 NFCStaticController::NFCStaticController() :
51         EventNFCChangedListener(ThreadEnum::NULL_THREAD), 
52         setPoweredAnswerReceiver(ThreadEnum::NULL_THREAD) {
53 }
54
55 void NFCStaticController::onAnswerReceived(const EventNFCChangedPtr& event) {
56         LogDebug("onAnswerReceived Enter");
57
58         EventNFCChangedPrivateDataPtr privateData =
59         DPL::DynamicPointerCast<EventNFCChangedPrivateData>(event->getPrivateData());
60
61         void *nfcProp = event->getNFCProperties();
62
63         Try {
64                 /**
65                 *When received answer from platform, create an NFCTag JSObject and then call success function.
66                 */
67                 if (nfcProp != NULL) {
68                         JSObjectRef nfcObj;
69                         JSCallbackManagerPtr callbackManager = privateData->getCallbackManager();
70                         JSContextRef context = callbackManager->getContext();
71                         
72                         if (event->getNFCType() == NFC_TAG_TYPE)
73                                 nfcObj = JSNFCTag::createJSObject(context, nfcProp);
74                         else
75                                 nfcObj = JSNFCTarget::createJSObject(context, nfcProp);
76                         LogDebug("callOnSuccess");
77                         callbackManager->callOnSuccess(static_cast<JSValueRef>(nfcObj));
78                 } else {
79                         JSCallbackManagerPtr detachedCallbackManager = privateData->getDetachedCallbackManager();
80
81                         detachedCallbackManager->callOnSuccess();
82                 }
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");
91         }
92
93 }
94
95 void NFCStaticController::OnAnswerReceived(const EventNFCChangedSetPoweredPtr &event)
96 {
97         JSCallbackManagerPtr cbm =
98             DPL::StaticPointerCast<JSCallbackManager>(event->getPrivateData());
99
100         Try {
101                 if (!cbm) {
102                         LogDebug("no callback manager");
103                         return;
104                 }
105                 NFCAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(cbm);
106                 if (event->getResult()) {
107                         LogDebug("result success");
108                         cbm->callOnSuccess();
109                         return;
110                 }
111                 LogDebug("result fail");
112                 std::string error = event->getError();
113                 std::string errorMessage = event->getErrorMessage();
114                 JSValueRef errorObject;
115                 if (error != "") {
116                         if (errorMessage != "")
117                                 errorObject = JSTizenExceptionFactory::makeErrorObject(cbm->getContext(), error, errorMessage);
118                         else
119                                 errorObject = JSTizenExceptionFactory::makeErrorObject(cbm->getContext(), error, error);
120                 } else
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);  
135         }
136 }
137
138 } // NFC
139 } // DeviceAPI