Home | History | Annotate | Download | only in gobject
      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 #ifndef GObjectEventListener_h
     20 #define GObjectEventListener_h
     21 
     22 #include "EventListener.h"
     23 #include "EventTarget.h"
     24 
     25 #include <wtf/RefPtr.h>
     26 #include <wtf/text/CString.h>
     27 
     28 typedef struct _GObject GObject;
     29 typedef void (*GCallback) (void);
     30 
     31 namespace WebCore {
     32 
     33 class GObjectEventListener : public EventListener {
     34 public:
     35 
     36     static bool addEventListener(GObject* object, EventTarget* target, const char* domEventName, GCallback handler, bool useCapture, void* userData)
     37     {
     38         RefPtr<GObjectEventListener> listener(adoptRef(new GObjectEventListener(object, target, domEventName, handler, useCapture, userData)));
     39         return target->addEventListener(domEventName, listener.release(), useCapture);
     40     }
     41 
     42     static bool removeEventListener(GObject* object, EventTarget* target, const char* domEventName, GCallback handler, bool useCapture)
     43     {
     44         GObjectEventListener key(object, target, domEventName, handler, useCapture, 0);
     45         return target->removeEventListener(domEventName, &key, useCapture);
     46     }
     47 
     48     static void gobjectDestroyedCallback(GObjectEventListener* listener, GObject*)
     49     {
     50         listener->gobjectDestroyed();
     51     }
     52 
     53     static const GObjectEventListener* cast(const EventListener* listener)
     54     {
     55         return listener->type() == GObjectEventListenerType
     56             ? static_cast<const GObjectEventListener*>(listener)
     57             : 0;
     58     }
     59 
     60     virtual bool operator==(const EventListener& other);
     61 
     62 private:
     63     GObjectEventListener(GObject*, EventTarget*, const char* domEventName, GCallback handler, bool capture, void* userData);
     64     ~GObjectEventListener();
     65     void gobjectDestroyed();
     66 
     67     virtual void handleEvent(ScriptExecutionContext*, Event*);
     68 
     69     GObject* m_object;
     70 
     71     // We do not need to keep a reference to the m_coreTarget, because
     72     // we only use it when the GObject and thus the m_coreTarget object is alive.
     73     EventTarget* m_coreTarget;
     74     CString m_domEventName;
     75     GCallback m_handler;
     76     bool m_capture;
     77     void* m_userData;
     78 };
     79 } // namespace WebCore
     80 
     81 #endif
     82