Update change log and spec for wrt-plugins-tizen_0.4.13
[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 };
39
40 class NFCListenerManager : public DeviceAPI::Common::IListenerController
41 {
42 public:
43         NFCListenerManager()
44         {
45         }
46
47         virtual ~NFCListenerManager()
48         {
49         }
50
51 };
52 typedef DPL::Singleton<NFCListenerManager> NFCListenerManagerSingleton;
53
54 class NFCListenerCanceller : public DeviceAPI::Common::IListenerItem
55 {
56 public:
57         NFCListenerCanceller(JSContextRef context, JSObjectRef object, long watchId) :
58                 DeviceAPI::Common::IListenerItem(context, object, watchId)
59         {
60         }
61
62         virtual ~NFCListenerCanceller()
63         {
64         }
65
66         virtual void cancelListener()
67         {
68                 LogDebug("context : " << m_context);
69                 LogDebug("object : " << m_object);
70                 switch(m_watchId) {
71                         case ID_NFCPEER_RECEIVENDEF_LISTENER:
72                         {
73                                 NFCTargetPrivObject* privateObject = static_cast<NFCTargetPrivObject*>(JSObjectGetPrivate(m_object));
74                                 if (NULL == privateObject) {
75                                         LogWarning("Object has no privateObject");
76                                         return;
77                                 }
78
79                                 INFCTargetPtr nfcTarget(privateObject->getObject());
80                                 nfcTarget->unsetReceiveNDEFListener();
81                                 break;
82                         }
83                         case ID_NFCADAPTER_TAG_LISTENER:
84                         case ID_NFCADAPTER_PEER_LISTENER:
85                         {
86                                 NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(m_object));
87                                 if (NULL == privateObject) {
88                                         LogError("Object has no privateObject");
89                                         return;
90                                 }
91                                 Try {
92                                         INFCAdapterPtr nfcAdapter( privateObject->getObject());
93                                         if (ID_NFCADAPTER_TAG_LISTENER == m_watchId)
94                                                 nfcAdapter->unsetTagListener();
95                                         else
96                                                 nfcAdapter->unsetPeerListener();
97                                 } Catch(WrtDeviceApis::Commons::Exception) {
98                                         LogError("Error on platform : " << _rethrown_exception.GetMessage());
99                                 }
100                                 break;
101                         }
102
103                         default:
104                                 LogError("Wrong ID");
105                 }
106         }
107 };
108 typedef DPL::SharedPtr<NFCListenerCanceller> NFCListenerCancellerPtr;
109
110 } // NFC
111 } // DeviceAPI
112
113 #endif // _TIZEN_NFC_LISTENER_MANAGER_H_