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