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/shared_ptr.h>
31 #include "Singleton.h"
39 typedef DPL::SharedPtr<IListenerItem> IListenerItemPtr;
44 IListenerItem(JSContextRef context, JSObjectRef object, long watchId) :
51 virtual ~IListenerItem()
55 virtual void protectObject()
57 LoggerD("Protect object:" << m_object);
59 JSValueProtect(m_context, m_object);
62 virtual void unprotectObject()
64 LoggerD("Unprotect object:" << m_object);
66 JSValueUnprotect(m_context, m_object);
69 virtual void cancelListener()
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.");
75 virtual bool equal(const DeviceAPI::Common::IListenerItemPtr &other) const
80 if(m_object == other->m_object && m_watchId == other->m_watchId)
87 JSContextRef m_context;
92 class IListenerController
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;
100 IListenerController()
104 virtual ~IListenerController()
108 virtual void registerListener(IListenerItemPtr &canceller, const JSContextRef context)
110 LoggerD("Registering a listener on context:" << context);
112 canceller->protectObject();
113 m_map.insert(ListenerPair(context, canceller));
116 virtual void unregisterContext(const JSContextRef context)
118 LoggerD("Unregistering all listeners on context:" << context);
120 ListenerMapIterPair iterPair = m_map.equal_range(context);
122 for(ListenerMapIter i=iterPair.first; i!=iterPair.second; i++)
124 LoggerD("Unregistering a listener");
125 i->second->cancelListener();
126 i->second->unprotectObject();
129 m_map.erase(context);
132 virtual void unregisterListener(const IListenerItemPtr &canceller)
134 LoggerD("Unregistering a listener");
136 for(ListenerMapIter i=m_map.begin(); i!=m_map.end(); i++)
138 if(i->second->equal(canceller))
140 LoggerD("Found object");
141 i->second->unprotectObject();
157 #endif // _TIZEN_COMMONS_ILISTENER_MANAGER_H_