• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (C) 2010, 2011 Igalia S.L.
3  *
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.
8  *
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.
13  *
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
17  */
18 
19 #include "config.h"
20 #include "GObjectEventListener.h"
21 
22 #include "Event.h"
23 #include "EventListener.h"
24 #include "webkit/WebKitDOMEvent.h"
25 #include "webkit/WebKitDOMEventPrivate.h"
26 #include <glib-object.h>
27 #include <glib.h>
28 #include <wtf/HashMap.h>
29 
30 namespace WebCore {
31 
32 typedef void (*GObjectEventListenerCallback)(GObject*, WebKitDOMEvent*, void*);
33 
GObjectEventListener(GObject * object,EventTarget * target,const char * domEventName,GCallback handler,bool capture,void * userData)34 GObjectEventListener::GObjectEventListener(GObject* object, EventTarget* target, const char* domEventName, GCallback handler, bool capture, void* userData)
35     : EventListener(GObjectEventListenerType)
36     , m_object(object)
37     , m_coreTarget(target)
38     , m_domEventName(domEventName)
39     , m_handler(handler)
40     , m_capture(capture)
41     , m_userData(userData)
42 {
43     ASSERT(m_coreTarget);
44     g_object_weak_ref(object, reinterpret_cast<GWeakNotify>(GObjectEventListener::gobjectDestroyedCallback), this);
45 }
46 
~GObjectEventListener()47 GObjectEventListener::~GObjectEventListener()
48 {
49     if (!m_coreTarget)
50         return;
51     g_object_weak_unref(m_object, reinterpret_cast<GWeakNotify>(GObjectEventListener::gobjectDestroyedCallback), this);
52 }
53 
gobjectDestroyed()54 void GObjectEventListener::gobjectDestroyed()
55 {
56     ASSERT(m_coreTarget);
57 
58     // We must set m_coreTarget to null, because removeEventListener
59     // may call the destructor as a side effect and we must be in the
60     // proper state to prevent g_object_weak_unref.
61     EventTarget* target = m_coreTarget;
62     m_coreTarget = 0;
63     target->removeEventListener(m_domEventName.data(), this, m_capture);
64 }
65 
handleEvent(ScriptExecutionContext *,Event * event)66 void GObjectEventListener::handleEvent(ScriptExecutionContext*, Event* event)
67 {
68     WebKitDOMEvent* gobjectEvent = WEBKIT_DOM_EVENT(WebKit::kit(event));
69     reinterpret_cast<GObjectEventListenerCallback>(m_handler)(m_object, gobjectEvent, m_userData);
70     g_object_unref(gobjectEvent);
71 }
72 
operator ==(const EventListener & listener)73 bool GObjectEventListener::operator==(const EventListener& listener)
74 {
75     if (const GObjectEventListener* gobjectEventListener = GObjectEventListener::cast(&listener))
76         return m_object == gobjectEventListener->m_object && m_handler == gobjectEventListener->m_handler;
77 
78     return false;
79 }
80 
81 }
82