2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file IListenerManager.h
20 * @author Kisub Song (kisubs.song@samsung.com)
25 #ifndef _TIZEN_COMMONS_ILISTENER_MANAGER_H_
26 #define _TIZEN_COMMONS_ILISTENER_MANAGER_H_
29 #include <JavaScriptCore/JavaScript.h>
30 #include <dpl/log/log.h>
31 #include <dpl/shared_ptr.h>
37 typedef DPL::SharedPtr<IListenerItem> IListenerItemPtr;
42 IListenerItem(JSContextRef context, JSObjectRef object, long watchId) :
49 virtual ~IListenerItem()
53 virtual void protectObject()
55 LogDebug("Protect object:" << m_object);
57 JSValueProtect(m_context, m_object);
60 virtual void unprotectObject()
62 LogDebug("Unprotect object:" << m_object);
64 JSValueUnprotect(m_context, m_object);
67 virtual void cancelListener()
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.");
73 virtual bool equal(const DeviceAPI::Common::IListenerItemPtr &other) const
78 if(m_object == other->m_object && m_watchId == other->m_watchId)
85 JSContextRef m_context;
90 class IListenerController
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;
102 virtual ~IListenerController()
106 virtual void registerListener(IListenerItemPtr &canceller, const JSContextRef context)
108 LogDebug("Registering a listener on context:" << context);
110 canceller->protectObject();
111 m_map.insert(ListenerPair(context, canceller));
114 virtual void unregisterContext(const JSContextRef context)
116 LogDebug("Unregistering all listeners on context:" << context);
118 ListenerMapIterPair iterPair = m_map.equal_range(context);
120 for(ListenerMapIter i=iterPair.first; i!=iterPair.second; i++)
122 LogDebug("Unregistering a listener");
123 i->second->cancelListener();
124 i->second->unprotectObject();
127 m_map.erase(context);
130 virtual void unregisterListener(const IListenerItemPtr &canceller)
132 LogDebug("Unregistering a listener");
134 for(ListenerMapIter i=m_map.begin(); i!=m_map.end(); i++)
136 if(i->second->equal(canceller))
138 LogDebug("Found object");
139 i->second->unprotectObject();
155 #endif // _TIZEN_COMMONS_ILISTENER_MANAGER_H_