• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
EventDispatchDetailsEventDispatchDetails21   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   // |target| is NULL if the target was deleted during dispatch.
58   virtual EventDispatchDetails PostDispatchEvent(
59       EventTarget* target,
60       const Event& event) WARN_UNUSED_RESULT;
61 
62  private:
63   // Dispatches the event to the target.
64   EventDispatchDetails DispatchEventToTarget(EventTarget* target,
65                                              Event* event) WARN_UNUSED_RESULT;
66 
67   EventDispatcher* dispatcher_;
68 
69   DISALLOW_COPY_AND_ASSIGN(EventDispatcherDelegate);
70 };
71 
72 // Dispatches events to appropriate targets.
73 class EVENTS_EXPORT EventDispatcher {
74  public:
75   explicit EventDispatcher(EventDispatcherDelegate* delegate);
76   virtual ~EventDispatcher();
77 
78   void ProcessEvent(EventTarget* target, Event* event);
79 
current_event()80   const Event* current_event() const { return current_event_; }
current_event()81   Event* current_event() { return current_event_; }
82 
delegate_destroyed()83   bool delegate_destroyed() const { return !delegate_; }
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   DISALLOW_COPY_AND_ASSIGN(EventDispatcher);
103 };
104 
105 }  // namespace ui
106 
107 #endif  // UI_EVENTS_EVENT_DISPATCHER_H_
108