event_dispatcher.h revision d0247b1b59f9c528cb6df88b4f2b9afaf80d181e
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/base/ui_export.h"
10#include "ui/events/event.h"
11#include "ui/events/event_constants.h"
12#include "ui/events/event_target.h"
13
14namespace ui {
15
16class EventDispatcher;
17
18class UI_EXPORT EventDispatcherDelegate {
19 public:
20  EventDispatcherDelegate();
21  virtual ~EventDispatcherDelegate();
22
23  // Returns whether an event can still be dispatched to a target. (e.g. during
24  // event dispatch, one of the handlers may have destroyed the target, in which
25  // case the event can no longer be dispatched to the target).
26  virtual bool CanDispatchToTarget(EventTarget* target) = 0;
27
28  // Returns the event being dispatched (or NULL if no event is being
29  // dispatched).
30  Event* current_event();
31
32 protected:
33  // Dispatches the event to the target. Returns true if the delegate is still
34  // alive after dispatching event, and false if the delegate was destroyed
35  // during the event dispatch.
36  bool DispatchEvent(EventTarget* target, Event* event);
37
38 private:
39  EventDispatcher* dispatcher_;
40
41  DISALLOW_COPY_AND_ASSIGN(EventDispatcherDelegate);
42};
43
44// Dispatches events to appropriate targets.
45class UI_EXPORT EventDispatcher {
46 public:
47  explicit EventDispatcher(EventDispatcherDelegate* delegate);
48  virtual ~EventDispatcher();
49
50  void ProcessEvent(EventTarget* target, Event* event);
51
52  const Event* current_event() const { return current_event_; }
53  Event* current_event() { return current_event_; }
54
55  bool delegate_destroyed() const { return !delegate_; }
56
57  void OnHandlerDestroyed(EventHandler* handler);
58  void OnDispatcherDelegateDestroyed();
59
60 private:
61  void DispatchEventToEventHandlers(EventHandlerList& list,
62                                    Event* event);
63
64  // Dispatches an event, and makes sure it sets ER_CONSUMED on the
65  // event-handling result if the dispatcher itself has been destroyed during
66  // dispatching the event to the event handler.
67  void DispatchEvent(EventHandler* handler, Event* event);
68
69  EventDispatcherDelegate* delegate_;
70
71  Event* current_event_;
72
73  EventHandlerList handler_list_;
74
75  DISALLOW_COPY_AND_ASSIGN(EventDispatcher);
76};
77
78}  // namespace ui
79
80#endif  // UI_EVENTS_EVENT_DISPATCHER_H_
81