2
* Copyright (C) 2010, 2011 Igalia S.L.
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Lesser General Public
6
* License as published by the Free Software Foundation; either
7
* version 2 of the License, or (at your option) any later version.
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Lesser General Public License for more details.
14
* You should have received a copy of the GNU Lesser General Public
15
* License along with this library; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
#include "GObjectEventListener.h"
23
#include "WebKitDOMEvent.h"
24
#include "WebKitDOMEventPrivate.h"
25
#include <glib-object.h>
26
#include <wtf/HashMap.h>
30
typedef void (*GObjectEventListenerCallback)(GObject*, WebKitDOMEvent*, void*);
32
GObjectEventListener::GObjectEventListener(GObject* object, EventTarget* target, const char* domEventName, GCallback handler, bool capture, void* userData)
33
: EventListener(GObjectEventListenerType)
35
, m_coreTarget(target)
36
, m_domEventName(domEventName)
39
, m_userData(userData)
42
g_object_weak_ref(object, reinterpret_cast<GWeakNotify>(GObjectEventListener::gobjectDestroyedCallback), this);
45
GObjectEventListener::~GObjectEventListener()
49
g_object_weak_unref(m_object, reinterpret_cast<GWeakNotify>(GObjectEventListener::gobjectDestroyedCallback), this);
52
void GObjectEventListener::gobjectDestroyed()
56
// We must set m_coreTarget to null, because removeEventListener
57
// may call the destructor as a side effect and we must be in the
58
// proper state to prevent g_object_weak_unref.
59
EventTarget* target = m_coreTarget;
61
target->removeEventListener(m_domEventName.data(), this, m_capture);
64
void GObjectEventListener::handleEvent(ScriptExecutionContext*, Event* event)
66
WebKitDOMEvent* gobjectEvent = WEBKIT_DOM_EVENT(WebKit::kit(event));
67
reinterpret_cast<GObjectEventListenerCallback>(m_handler)(m_object, gobjectEvent, m_userData);
68
g_object_unref(gobjectEvent);
71
bool GObjectEventListener::operator==(const EventListener& listener)
73
if (const GObjectEventListener* gobjectEventListener = GObjectEventListener::cast(&listener))
74
return m_object == gobjectEventListener->m_object && m_handler == gobjectEventListener->m_handler;