d29d83a4221d7dadf7f7a9d6aa6aac030c1b4566
[framework/web/wrt-plugins-tizen.git] / src / Common / IListenerManager.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  * @file        IListenerManager.h
20  * @author      Kisub Song (kisubs.song@samsung.com)
21  * @version     0.1
22  * @brief
23  */
24
25 #ifndef _TIZEN_COMMONS_ILISTENER_MANAGER_H_
26 #define _TIZEN_COMMONS_ILISTENER_MANAGER_H_
27
28 #include <map>
29 #include <JavaScriptCore/JavaScript.h>
30 #include <dpl/log/log.h>
31 #include <dpl/shared_ptr.h>
32
33 namespace DeviceAPI {
34 namespace Common {
35
36 class IListenerItem;
37 typedef DPL::SharedPtr<IListenerItem> IListenerItemPtr;
38
39 class IListenerItem
40 {
41 public:
42         IListenerItem(JSContextRef context, JSObjectRef object, long watchId) :
43                 m_context(context),
44                 m_object(object),
45                 m_watchId(watchId)
46         {
47         }
48
49         virtual ~IListenerItem()
50         {
51         }
52
53         virtual void protectObject()
54         {
55                 LogDebug("Protect object:" << m_object);
56
57                 JSValueProtect(m_context, m_object);
58         }
59
60         virtual void unprotectObject()
61         {
62                 LogDebug("Unprotect object:" << m_object);
63
64                 JSValueUnprotect(m_context, m_object);
65         }
66
67         virtual void cancelListener()
68         {
69                 LogWarning("IListenerItem MUST be used as an inherited shape.");
70                 LogWarning("If this log has been printed, it must be used with wrong usage.");
71         }
72
73         virtual bool equal(const DeviceAPI::Common::IListenerItemPtr &other) const
74         {
75                 if(!other)
76                         return false;
77
78                 if(m_object == other->m_object && m_watchId == other->m_watchId)
79                         return true;
80
81                 return false;
82         }
83
84 protected:
85         JSContextRef    m_context;
86         JSObjectRef             m_object;
87         long                    m_watchId;
88 };
89
90 class IListenerController
91 {
92 public:
93         typedef std::multimap<JSContextRef, IListenerItemPtr>   ListenerMap;
94         typedef typename ListenerMap::iterator                  ListenerMapIter;
95         typedef std::pair<JSContextRef, IListenerItemPtr>       ListenerPair;
96         typedef std::pair<ListenerMapIter, ListenerMapIter>     ListenerMapIterPair;
97
98         IListenerController()
99         {
100         }
101
102         virtual ~IListenerController()
103         {
104         }
105
106         virtual void registerListener(IListenerItemPtr &canceller, const JSContextRef context)
107         {
108                 LogDebug("Registering a listener on context:" << context);
109
110                 canceller->protectObject();
111                 m_map.insert(ListenerPair(context, canceller));
112         }
113
114         virtual void unregisterContext(const JSContextRef context)
115         {
116                 LogDebug("Unregistering all listeners on context:" << context);
117
118                 ListenerMapIterPair iterPair = m_map.equal_range(context);
119
120                 for(ListenerMapIter i=iterPair.first; i!=iterPair.second; i++)
121                 {
122                         LogDebug("Unregistering a listener");
123                         i->second->cancelListener();
124                         i->second->unprotectObject();
125                 }
126
127                 m_map.erase(context);
128         }
129
130         virtual void unregisterListener(const IListenerItemPtr &canceller)
131         {
132                 LogDebug("Unregistering a listener");
133
134                 for(ListenerMapIter i=m_map.begin(); i!=m_map.end(); i++)
135                 {
136                         if(i->second->equal(canceller))
137                         {
138                                 LogDebug("Found object");
139                                 i->second->unprotectObject();
140
141                                 m_map.erase(i);
142
143                                 break;
144                         }
145                 }
146         }
147
148 private:
149         ListenerMap m_map;
150 };
151
152 } // Common
153 }// DeviceAPI
154
155 #endif // _TIZEN_COMMONS_ILISTENER_MANAGER_H_