Home | History | Annotate | Download | only in events
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef UI_EVENTS_EVENT_DISPATCHER_H_
      6 #define UI_EVENTS_EVENT_DISPATCHER_H_
      7 
      8 #include "base/auto_reset.h"
      9 #include "ui/events/event.h"
     10 #include "ui/events/event_constants.h"
     11 #include "ui/events/event_handler.h"
     12 #include "ui/events/events_export.h"
     13 
     14 namespace ui {
     15 
     16 class EventDispatcher;
     17 class EventTarget;
     18 class EventTargeter;
     19 
     20 struct EventDispatchDetails {
     21   EventDispatchDetails()
     22       : dispatcher_destroyed(false),
     23         target_destroyed(false) {}
     24   bool dispatcher_destroyed;
     25   bool target_destroyed;
     26 };
     27 
     28 class EVENTS_EXPORT EventDispatcherDelegate {
     29  public:
     30   EventDispatcherDelegate();
     31   virtual ~EventDispatcherDelegate();
     32 
     33   // Returns whether an event can still be dispatched to a target. (e.g. during
     34   // event dispatch, one of the handlers may have destroyed the target, in which
     35   // case the event can no longer be dispatched to the target).
     36   virtual bool CanDispatchToTarget(EventTarget* target) = 0;
     37 
     38   // Returns the event being dispatched (or NULL if no event is being
     39   // dispatched).
     40   Event* current_event();
     41 
     42   // Dispatches |event| to |target|. This calls |PreDispatchEvent()| before
     43   // dispatching the event, and |PostDispatchEvent()| after the event has been
     44   // dispatched.
     45   EventDispatchDetails DispatchEvent(EventTarget* target, Event* event)
     46       WARN_UNUSED_RESULT;
     47 
     48  protected:
     49   // This is called once a target has been determined for an event, right before
     50   // the event is dispatched to the target. This function may modify |event| to
     51   // prepare it for dispatch (e.g. update event flags, location etc.).
     52   virtual EventDispatchDetails PreDispatchEvent(
     53       EventTarget* target,
     54       Event* event) WARN_UNUSED_RESULT;
     55 
     56   // This is called right after the event dispatch is completed.
     57   virtual EventDispatchDetails PostDispatchEvent(
     58       EventTarget* target,
     59       const Event& event) WARN_UNUSED_RESULT;
     60 
     61  private:
     62   // Dispatches the event to the target.
     63   EventDispatchDetails DispatchEventToTarget(EventTarget* target,
     64                                              Event* event) WARN_UNUSED_RESULT;
     65 
     66   EventDispatcher* dispatcher_;
     67 
     68   DISALLOW_COPY_AND_ASSIGN(EventDispatcherDelegate);
     69 };
     70 
     71 // Dispatches events to appropriate targets.
     72 class EVENTS_EXPORT EventDispatcher {
     73  public:
     74   explicit EventDispatcher(EventDispatcherDelegate* delegate);
     75   virtual ~EventDispatcher();
     76 
     77   void ProcessEvent(EventTarget* target, Event* event);
     78 
     79   const Event* current_event() const { return current_event_; }
     80   Event* current_event() { return current_event_; }
     81 
     82   bool delegate_destroyed() const { return !delegate_; }
     83   const EventDispatchDetails& details() const { return details_; }
     84 
     85   void OnHandlerDestroyed(EventHandler* handler);
     86   void OnDispatcherDelegateDestroyed();
     87 
     88  private:
     89   void DispatchEventToEventHandlers(EventHandlerList* list, Event* event);
     90 
     91   // Dispatches an event, and makes sure it sets ER_CONSUMED on the
     92   // event-handling result if the dispatcher itself has been destroyed during
     93   // dispatching the event to the event handler.
     94   void DispatchEvent(EventHandler* handler, Event* event);
     95 
     96   EventDispatcherDelegate* delegate_;
     97 
     98   Event* current_event_;
     99 
    100   EventHandlerList handler_list_;
    101 
    102   EventDispatchDetails details_;
    103 
    104   DISALLOW_COPY_AND_ASSIGN(EventDispatcher);
    105 };
    106 
    107 }  // namespace ui
    108 
    109 #endif  // UI_EVENTS_EVENT_DISPATCHER_H_
    110