OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef UI_BASE_EVENTS_EVENT_DISPATCHER_H_ | 5 #ifndef UI_BASE_EVENTS_EVENT_DISPATCHER_H_ |
6 #define UI_BASE_EVENTS_EVENT_DISPATCHER_H_ | 6 #define UI_BASE_EVENTS_EVENT_DISPATCHER_H_ |
7 | 7 |
8 #include "base/auto_reset.h" | |
9 #include "ui/base/events/event.h" | 8 #include "ui/base/events/event.h" |
10 #include "ui/base/events/event_constants.h" | 9 #include "ui/base/events/event_constants.h" |
11 #include "ui/base/events/event_target.h" | 10 #include "ui/base/events/event_target.h" |
12 #include "ui/base/ui_export.h" | 11 #include "ui/base/ui_export.h" |
13 | 12 |
14 namespace ui { | 13 namespace ui { |
15 | 14 |
16 // Dispatches events to appropriate targets. | 15 // Dispatches events to appropriate targets. |
17 class UI_EXPORT EventDispatcher { | 16 class UI_EXPORT EventDispatcher { |
18 public: | 17 public: |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 if (!CanDispatchToTarget(target)) | 54 if (!CanDispatchToTarget(target)) |
56 return result; | 55 return result; |
57 | 56 |
58 list.clear(); | 57 list.clear(); |
59 target->GetPostTargetHandlers(&list); | 58 target->GetPostTargetHandlers(&list); |
60 dispatch_helper.set_phase(EP_POSTTARGET); | 59 dispatch_helper.set_phase(EP_POSTTARGET); |
61 result |= DispatchEventToEventHandlers(list, event); | 60 result |= DispatchEventToEventHandlers(list, event); |
62 return result; | 61 return result; |
63 } | 62 } |
64 | 63 |
| 64 const Event* current_event() const { return current_event_; } |
| 65 Event* current_event() { return current_event_; } |
| 66 |
65 private: | 67 private: |
66 class UI_EXPORT ScopedDispatchHelper : public NON_EXPORTED_BASE( | 68 class UI_EXPORT ScopedDispatchHelper : public NON_EXPORTED_BASE( |
67 Event::DispatcherApi) { | 69 Event::DispatcherApi) { |
68 public: | 70 public: |
69 explicit ScopedDispatchHelper(Event* event); | 71 explicit ScopedDispatchHelper(Event* event); |
70 virtual ~ScopedDispatchHelper(); | 72 virtual ~ScopedDispatchHelper(); |
71 private: | 73 private: |
72 DISALLOW_COPY_AND_ASSIGN(ScopedDispatchHelper); | 74 DISALLOW_COPY_AND_ASSIGN(ScopedDispatchHelper); |
73 }; | 75 }; |
74 | 76 |
(...skipping 14 matching lines...) Expand all Loading... |
89 // Dispatches an event, and makes sure it sets ER_CONSUMED on the | 91 // Dispatches an event, and makes sure it sets ER_CONSUMED on the |
90 // event-handling result if the dispatcher itself has been destroyed during | 92 // event-handling result if the dispatcher itself has been destroyed during |
91 // dispatching the event to the event handler. | 93 // dispatching the event to the event handler. |
92 template<class T> | 94 template<class T> |
93 int DispatchEvent(EventHandler* handler, T* event) { | 95 int DispatchEvent(EventHandler* handler, T* event) { |
94 // If the target has been invalidated or deleted, don't dispatch the event. | 96 // If the target has been invalidated or deleted, don't dispatch the event. |
95 if (!CanDispatchToTarget(event->target())) | 97 if (!CanDispatchToTarget(event->target())) |
96 return ui::ER_CONSUMED; | 98 return ui::ER_CONSUMED; |
97 bool destroyed = false; | 99 bool destroyed = false; |
98 set_on_destroy_ = &destroyed; | 100 set_on_destroy_ = &destroyed; |
| 101 |
| 102 // Do not use base::AutoReset for |current_event_|. The EventDispatcher can |
| 103 // be destroyed by the event-handler during the event-dispatch. That would |
| 104 // cause invalid memory-write when AutoReset tries to restore the value. |
| 105 Event* old_event = current_event_; |
| 106 current_event_ = event; |
99 int result = DispatchEventToSingleHandler(handler, event); | 107 int result = DispatchEventToSingleHandler(handler, event); |
100 if (destroyed) | 108 if (destroyed) { |
101 result |= ui::ER_CONSUMED; | 109 result |= ui::ER_CONSUMED; |
102 else | 110 } else { |
| 111 current_event_ = old_event; |
103 set_on_destroy_ = NULL; | 112 set_on_destroy_ = NULL; |
| 113 } |
104 return result; | 114 return result; |
105 } | 115 } |
106 | 116 |
107 EventResult DispatchEventToSingleHandler(EventHandler* handler, Event* event); | 117 EventResult DispatchEventToSingleHandler(EventHandler* handler, Event* event); |
108 EventResult DispatchEventToSingleHandler(EventHandler* handler, | 118 EventResult DispatchEventToSingleHandler(EventHandler* handler, |
109 KeyEvent* event); | 119 KeyEvent* event); |
110 EventResult DispatchEventToSingleHandler(EventHandler* handler, | 120 EventResult DispatchEventToSingleHandler(EventHandler* handler, |
111 MouseEvent* event); | 121 MouseEvent* event); |
112 EventResult DispatchEventToSingleHandler(EventHandler* handler, | 122 EventResult DispatchEventToSingleHandler(EventHandler* handler, |
113 ScrollEvent* event); | 123 ScrollEvent* event); |
114 EventResult DispatchEventToSingleHandler(EventHandler* handler, | 124 EventResult DispatchEventToSingleHandler(EventHandler* handler, |
115 TouchEvent* event); | 125 TouchEvent* event); |
116 EventResult DispatchEventToSingleHandler(EventHandler* handler, | 126 EventResult DispatchEventToSingleHandler(EventHandler* handler, |
117 GestureEvent* event); | 127 GestureEvent* event); |
118 | 128 |
119 // This is used to track whether the dispatcher has been destroyed in the | 129 // This is used to track whether the dispatcher has been destroyed in the |
120 // middle of dispatching an event. | 130 // middle of dispatching an event. |
121 bool* set_on_destroy_; | 131 bool* set_on_destroy_; |
122 | 132 |
| 133 Event* current_event_; |
| 134 |
123 DISALLOW_COPY_AND_ASSIGN(EventDispatcher); | 135 DISALLOW_COPY_AND_ASSIGN(EventDispatcher); |
124 }; | 136 }; |
125 | 137 |
126 } // namespace ui | 138 } // namespace ui |
127 | 139 |
128 #endif // UI_BASE_EVENTS_EVENT_DISPATCHER_H_ | 140 #endif // UI_BASE_EVENTS_EVENT_DISPATCHER_H_ |
OLD | NEW |