7252822929887e8fd96ac67fd00395ea9c5927dc
[framework/web/wrt-plugins-tizen.git] / src / NFC / NFCListenerManager.h
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 #ifndef _TIZEN_NFC_LISTENER_MANAGER_H_
20 #define _TIZEN_NFC_LISTENER_MANAGER_H_
21
22 #include <map>
23 #include <JavaScriptCore/JavaScript.h>
24 #include <dpl/log/log.h>
25 #include <dpl/shared_ptr.h>
26 #include <dpl/singleton.h>
27 #include <IListenerManager.h>
28 #include "JSNFCAdapter.h"
29 #include "JSNFCTarget.h"
30
31 namespace DeviceAPI {
32 namespace NFC {
33
34 enum NFCListenerWatchID {
35         ID_NFCADAPTER_TAG_LISTENER = 1,
36         ID_NFCADAPTER_PEER_LISTENER,
37         ID_NFCPEER_RECEIVENDEF_LISTENER,
38         ID_NFCADAPTER_CARDEMULATION_LISTENER
39 };
40
41 class NFCListenerManager : public DeviceAPI::Common::IListenerController
42 {
43 public:
44         NFCListenerManager()
45         {
46         }
47
48         virtual ~NFCListenerManager()
49         {
50         }
51
52 };
53 typedef DPL::Singleton<NFCListenerManager> NFCListenerManagerSingleton;
54
55 class NFCListenerCanceller : public DeviceAPI::Common::IListenerItem
56 {
57 public:
58         NFCListenerCanceller(JSContextRef context, JSObjectRef object, long watchId) :
59                 DeviceAPI::Common::IListenerItem(context, object, watchId)
60         {
61         }
62
63         virtual ~NFCListenerCanceller()
64         {
65         }
66
67         virtual void cancelListener()
68         {
69                 LogDebug("context : " << m_context);
70                 LogDebug("object : " << m_object);
71                 switch(m_watchId) {
72                         case ID_NFCPEER_RECEIVENDEF_LISTENER:
73                         {
74                                 NFCTargetPrivObject* privateObject = static_cast<NFCTargetPrivObject*>(JSObjectGetPrivate(m_object));
75                                 if (NULL == privateObject) {
76                                         LogWarning("Object has no privateObject");
77                                         return;
78                                 }
79
80                                 INFCTargetPtr nfcTarget(privateObject->getObject());
81                                 nfcTarget->unsetReceiveNDEFListener();
82                                 break;
83                         }
84                         case ID_NFCADAPTER_TAG_LISTENER:
85                         case ID_NFCADAPTER_PEER_LISTENER:
86                         {
87                                 NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(m_object));
88                                 if (NULL == privateObject) {
89                                         LogError("Object has no privateObject");
90                                         return;
91                                 }
92                                 Try {
93                                         INFCAdapterPtr nfcAdapter( privateObject->getObject());
94                                         if (ID_NFCADAPTER_TAG_LISTENER == m_watchId)
95                                                 nfcAdapter->unsetTagListener();
96                                         else
97                                                 nfcAdapter->unsetPeerListener();
98                                 } Catch(WrtDeviceApis::Commons::Exception) {
99                                         LogError("Error on platform : " << _rethrown_exception.GetMessage());
100                                 }
101                                 break;
102                         }
103                         case ID_NFCADAPTER_CARDEMULATION_LISTENER:
104                         {
105                                 NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(m_object));
106                                 if (NULL == privateObject) {
107                                         LogError("Object has no privateObject");
108                                         return;
109                                 }
110                                 Try {
111                                         INFCAdapterPtr nfcAdapter( privateObject->getObject());
112                                         nfcAdapter->unsetCardEmulationChangeListener();
113                                 } Catch(WrtDeviceApis::Commons::Exception) {
114                                         LogError("Error on platform : " << _rethrown_exception.GetMessage());
115                                 }
116                                 break;                          
117                         }
118                         default:
119                                 LogError("Wrong ID");
120                 }
121         }
122 };
123 typedef DPL::SharedPtr<NFCListenerCanceller> NFCListenerCancellerPtr;
124
125 } // NFC
126 } // DeviceAPI
127
128 #endif // _TIZEN_NFC_LISTENER_MANAGER_H_