summaryrefslogtreecommitdiffstats
path: root/Source/WebCore/bindings/gobject/GObjectEventListener.cpp
blob: 27432b91a39dd8e28699b48bb96f58242f5ae74d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
 *  Copyright (C) 2010 Igalia S.L.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "config.h"
#include "GObjectEventListener.h"

#include "Event.h"
#include "EventListener.h"
#include "webkit/WebKitDOMEvent.h"
#include "webkit/WebKitDOMEventPrivate.h"
#include <glib-object.h>
#include <glib.h>
#include <wtf/HashMap.h>

namespace WebCore {

GObjectEventListener::GObjectEventListener(GObject* object, DOMWindow* window, Node* node, const char* domEventName, const char* signalName)
    : EventListener(GObjectEventListenerType)
    , m_object(object)
    , m_coreNode(node)
    , m_coreWindow(window)
    , m_domEventName(domEventName)
    , m_signalName(signalName)
{
    ASSERT(!m_coreWindow || !m_coreNode);

    g_object_weak_ref(object, reinterpret_cast<GWeakNotify>(GObjectEventListener::gobjectDestroyedCallback), this);
}

GObjectEventListener::~GObjectEventListener()
{
    if (!m_coreWindow && !m_coreNode)
        return;
    g_object_weak_unref(m_object, reinterpret_cast<GWeakNotify>(GObjectEventListener::gobjectDestroyedCallback), this);
}

void GObjectEventListener::gobjectDestroyed()
{
    ASSERT(!m_coreWindow || !m_coreNode);

    // We must set m_coreWindow and m_coreNode to null, because removeEventListener may call the
    // destructor as a side effect and we must be in the proper state to prevent g_object_weak_unref.
    if (DOMWindow* window = m_coreWindow) {
        m_coreWindow = 0;
        window->removeEventListener(m_domEventName.data(), this, false);
        return;
    }

    Node* node = m_coreNode;
    m_coreNode = 0; // See above.
    node->removeEventListener(m_domEventName.data(), this, false);
}

void GObjectEventListener::handleEvent(ScriptExecutionContext*, Event* event)
{
    gboolean handled = FALSE;
    WebKitDOMEvent* gobjectEvent = WEBKIT_DOM_EVENT(WebKit::kit(event));
    g_signal_emit_by_name(m_object, m_signalName.data(), gobjectEvent, &handled);
    g_object_unref(gobjectEvent);
}

bool GObjectEventListener::operator==(const EventListener& listener)
{
    if (const GObjectEventListener* gobjectEventListener = GObjectEventListener::cast(&listener))
        return m_signalName == gobjectEventListener->m_signalName && m_object == gobjectEventListener->m_object;

    return false;
}

}