[email protected] | 868869a | 2012-09-10 14:56:26 | [diff] [blame] | 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 | |
[email protected] | 86ccbd4 | 2013-09-18 18:11:54 | [diff] [blame] | 5 | #include "ui/events/event_dispatcher.h" |
[email protected] | 868869a | 2012-09-10 14:56:26 | [diff] [blame] | 6 | |
[email protected] | 5f0b5d01 | 2012-12-05 22:36:20 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
[email protected] | 7407bf3 | 2013-12-05 22:00:41 | [diff] [blame] | 9 | #include "ui/events/event_target.h" |
| 10 | #include "ui/events/event_targeter.h" |
| 11 | |
[email protected] | 868869a | 2012-09-10 14:56:26 | [diff] [blame] | 12 | namespace ui { |
| 13 | |
[email protected] | 4d8784a7 | 2012-12-06 22:45:41 | [diff] [blame] | 14 | namespace { |
| 15 | |
[email protected] | e5ae465 | 2013-09-25 00:36:27 | [diff] [blame] | 16 | class ScopedDispatchHelper : public Event::DispatcherApi { |
[email protected] | 4d8784a7 | 2012-12-06 22:45:41 | [diff] [blame] | 17 | public: |
| 18 | explicit ScopedDispatchHelper(Event* event) |
| 19 | : Event::DispatcherApi(event) { |
| 20 | set_result(ui::ER_UNHANDLED); |
| 21 | } |
| 22 | |
| 23 | virtual ~ScopedDispatchHelper() { |
| 24 | set_phase(EP_POSTDISPATCH); |
| 25 | } |
| 26 | |
| 27 | private: |
| 28 | DISALLOW_COPY_AND_ASSIGN(ScopedDispatchHelper); |
| 29 | }; |
| 30 | |
| 31 | } // namespace |
| 32 | |
| 33 | EventDispatcherDelegate::EventDispatcherDelegate() |
| 34 | : dispatcher_(NULL) { |
| 35 | } |
| 36 | |
| 37 | EventDispatcherDelegate::~EventDispatcherDelegate() { |
| 38 | if (dispatcher_) |
| 39 | dispatcher_->OnDispatcherDelegateDestroyed(); |
| 40 | } |
| 41 | |
| 42 | Event* EventDispatcherDelegate::current_event() { |
| 43 | return dispatcher_ ? dispatcher_->current_event() : NULL; |
| 44 | } |
| 45 | |
[email protected] | ddb5ec5 | 2013-11-08 05:22:03 | [diff] [blame] | 46 | EventDispatchDetails EventDispatcherDelegate::DispatchEvent(EventTarget* target, |
| 47 | Event* event) { |
[email protected] | 7407bf3 | 2013-12-05 22:00:41 | [diff] [blame] | 48 | CHECK(target); |
[email protected] | 4c5d7c9 | 2013-12-13 17:17:39 | [diff] [blame] | 49 | Event::DispatcherApi dispatch_helper(event); |
| 50 | dispatch_helper.set_phase(EP_PREDISPATCH); |
| 51 | dispatch_helper.set_result(ER_UNHANDLED); |
| 52 | |
[email protected] | 7407bf3 | 2013-12-05 22:00:41 | [diff] [blame] | 53 | EventDispatchDetails details = PreDispatchEvent(target, event); |
[email protected] | b75fcca0 | 2014-02-26 15:07:59 | [diff] [blame] | 54 | if (!event->handled() && |
| 55 | !details.dispatcher_destroyed && |
| 56 | !details.target_destroyed) { |
[email protected] | 7407bf3 | 2013-12-05 22:00:41 | [diff] [blame] | 57 | details = DispatchEventToTarget(target, event); |
[email protected] | b75fcca0 | 2014-02-26 15:07:59 | [diff] [blame] | 58 | } |
| 59 | bool target_destroyed_during_dispatch = details.target_destroyed; |
| 60 | if (!details.dispatcher_destroyed) { |
| 61 | details = PostDispatchEvent(target_destroyed_during_dispatch ? |
| 62 | NULL : target, *event); |
| 63 | } |
[email protected] | 7407bf3 | 2013-12-05 22:00:41 | [diff] [blame] | 64 | |
[email protected] | b75fcca0 | 2014-02-26 15:07:59 | [diff] [blame] | 65 | details.target_destroyed |= target_destroyed_during_dispatch; |
[email protected] | 7407bf3 | 2013-12-05 22:00:41 | [diff] [blame] | 66 | return details; |
| 67 | } |
| 68 | |
| 69 | EventDispatchDetails EventDispatcherDelegate::PreDispatchEvent( |
| 70 | EventTarget* target, Event* event) { |
| 71 | return EventDispatchDetails(); |
| 72 | } |
| 73 | |
| 74 | EventDispatchDetails EventDispatcherDelegate::PostDispatchEvent( |
| 75 | EventTarget* target, const Event& event) { |
| 76 | return EventDispatchDetails(); |
| 77 | } |
| 78 | |
| 79 | EventDispatchDetails EventDispatcherDelegate::DispatchEventToTarget( |
| 80 | EventTarget* target, |
| 81 | Event* event) { |
[email protected] | 4d8784a7 | 2012-12-06 22:45:41 | [diff] [blame] | 82 | EventDispatcher* old_dispatcher = dispatcher_; |
| 83 | EventDispatcher dispatcher(this); |
| 84 | dispatcher_ = &dispatcher; |
| 85 | dispatcher.ProcessEvent(target, event); |
| 86 | if (!dispatcher.delegate_destroyed()) |
| 87 | dispatcher_ = old_dispatcher; |
| 88 | else if (old_dispatcher) |
| 89 | old_dispatcher->OnDispatcherDelegateDestroyed(); |
| 90 | |
[email protected] | b75fcca0 | 2014-02-26 15:07:59 | [diff] [blame] | 91 | EventDispatchDetails details; |
| 92 | details.dispatcher_destroyed = dispatcher.delegate_destroyed(); |
| 93 | details.target_destroyed = |
| 94 | (!details.dispatcher_destroyed && !CanDispatchToTarget(target)); |
| 95 | return details; |
[email protected] | 4d8784a7 | 2012-12-06 22:45:41 | [diff] [blame] | 96 | } |
| 97 | |
[email protected] | 7407bf3 | 2013-12-05 22:00:41 | [diff] [blame] | 98 | //////////////////////////////////////////////////////////////////////////////// |
| 99 | // EventDispatcher: |
| 100 | |
[email protected] | 4d8784a7 | 2012-12-06 22:45:41 | [diff] [blame] | 101 | EventDispatcher::EventDispatcher(EventDispatcherDelegate* delegate) |
| 102 | : delegate_(delegate), |
| 103 | current_event_(NULL) { |
[email protected] | 868869a | 2012-09-10 14:56:26 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | EventDispatcher::~EventDispatcher() { |
| 107 | } |
| 108 | |
[email protected] | 5f0b5d01 | 2012-12-05 22:36:20 | [diff] [blame] | 109 | void EventDispatcher::OnHandlerDestroyed(EventHandler* handler) { |
[email protected] | 4d8784a7 | 2012-12-06 22:45:41 | [diff] [blame] | 110 | handler_list_.erase(std::find(handler_list_.begin(), |
| 111 | handler_list_.end(), |
| 112 | handler)); |
| 113 | } |
| 114 | |
| 115 | void EventDispatcher::ProcessEvent(EventTarget* target, Event* event) { |
| 116 | if (!target || !target->CanAcceptEvent(*event)) |
[email protected] | 5f0b5d01 | 2012-12-05 22:36:20 | [diff] [blame] | 117 | return; |
[email protected] | 4d8784a7 | 2012-12-06 22:45:41 | [diff] [blame] | 118 | |
| 119 | ScopedDispatchHelper dispatch_helper(event); |
| 120 | dispatch_helper.set_target(target); |
| 121 | |
| 122 | handler_list_.clear(); |
| 123 | target->GetPreTargetHandlers(&handler_list_); |
| 124 | |
| 125 | dispatch_helper.set_phase(EP_PRETARGET); |
[email protected] | b432dd4 | 2013-10-29 00:01:31 | [diff] [blame] | 126 | DispatchEventToEventHandlers(&handler_list_, event); |
[email protected] | 1bd9288 | 2012-12-10 17:41:27 | [diff] [blame] | 127 | if (event->handled()) |
[email protected] | 4d8784a7 | 2012-12-06 22:45:41 | [diff] [blame] | 128 | return; |
| 129 | |
| 130 | // If the event hasn't been consumed, trigger the default handler. Note that |
| 131 | // even if the event has already been handled (i.e. return result has |
| 132 | // ER_HANDLED set), that means that the event should still be processed at |
| 133 | // this layer, however it should not be processed in the next layer of |
| 134 | // abstraction. |
| 135 | if (delegate_ && delegate_->CanDispatchToTarget(target)) { |
| 136 | dispatch_helper.set_phase(EP_TARGET); |
| 137 | DispatchEvent(target, event); |
[email protected] | 1bd9288 | 2012-12-10 17:41:27 | [diff] [blame] | 138 | if (event->handled()) |
[email protected] | 4d8784a7 | 2012-12-06 22:45:41 | [diff] [blame] | 139 | return; |
| 140 | } |
| 141 | |
[email protected] | b75fcca0 | 2014-02-26 15:07:59 | [diff] [blame] | 142 | if (!delegate_ || !delegate_->CanDispatchToTarget(target)) |
[email protected] | 4d8784a7 | 2012-12-06 22:45:41 | [diff] [blame] | 143 | return; |
| 144 | |
| 145 | handler_list_.clear(); |
| 146 | target->GetPostTargetHandlers(&handler_list_); |
| 147 | dispatch_helper.set_phase(EP_POSTTARGET); |
[email protected] | b432dd4 | 2013-10-29 00:01:31 | [diff] [blame] | 148 | DispatchEventToEventHandlers(&handler_list_, event); |
[email protected] | 4d8784a7 | 2012-12-06 22:45:41 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void EventDispatcher::OnDispatcherDelegateDestroyed() { |
| 152 | delegate_ = NULL; |
[email protected] | 5f0b5d01 | 2012-12-05 22:36:20 | [diff] [blame] | 153 | } |
| 154 | |
[email protected] | 868869a | 2012-09-10 14:56:26 | [diff] [blame] | 155 | //////////////////////////////////////////////////////////////////////////////// |
| 156 | // EventDispatcher, private: |
| 157 | |
[email protected] | b432dd4 | 2013-10-29 00:01:31 | [diff] [blame] | 158 | void EventDispatcher::DispatchEventToEventHandlers(EventHandlerList* list, |
[email protected] | 4d8784a7 | 2012-12-06 22:45:41 | [diff] [blame] | 159 | Event* event) { |
[email protected] | b432dd4 | 2013-10-29 00:01:31 | [diff] [blame] | 160 | for (EventHandlerList::const_iterator it = list->begin(), |
| 161 | end = list->end(); it != end; ++it) { |
[email protected] | 16bdf059 | 2012-12-10 23:49:12 | [diff] [blame] | 162 | (*it)->dispatchers_.push(this); |
[email protected] | 4d8784a7 | 2012-12-06 22:45:41 | [diff] [blame] | 163 | } |
| 164 | |
[email protected] | b432dd4 | 2013-10-29 00:01:31 | [diff] [blame] | 165 | while (!list->empty()) { |
| 166 | EventHandler* handler = (*list->begin()); |
[email protected] | 4d8784a7 | 2012-12-06 22:45:41 | [diff] [blame] | 167 | if (delegate_ && !event->stopped_propagation()) |
| 168 | DispatchEvent(handler, event); |
| 169 | |
[email protected] | b432dd4 | 2013-10-29 00:01:31 | [diff] [blame] | 170 | if (!list->empty() && *list->begin() == handler) { |
[email protected] | 4d8784a7 | 2012-12-06 22:45:41 | [diff] [blame] | 171 | // The handler has not been destroyed (because if it were, then it would |
| 172 | // have been removed from the list). |
[email protected] | 16bdf059 | 2012-12-10 23:49:12 | [diff] [blame] | 173 | CHECK(handler->dispatchers_.top() == this); |
| 174 | handler->dispatchers_.pop(); |
[email protected] | b432dd4 | 2013-10-29 00:01:31 | [diff] [blame] | 175 | list->erase(list->begin()); |
[email protected] | 4d8784a7 | 2012-12-06 22:45:41 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void EventDispatcher::DispatchEvent(EventHandler* handler, Event* event) { |
| 181 | // If the target has been invalidated or deleted, don't dispatch the event. |
| 182 | if (!delegate_->CanDispatchToTarget(event->target())) { |
[email protected] | f44db2a | 2013-04-15 19:59:49 | [diff] [blame] | 183 | if (event->cancelable()) |
| 184 | event->StopPropagation(); |
[email protected] | 4d8784a7 | 2012-12-06 22:45:41 | [diff] [blame] | 185 | return; |
| 186 | } |
| 187 | |
| 188 | base::AutoReset<Event*> event_reset(¤t_event_, event); |
[email protected] | bdb6d25c | 2012-12-03 20:29:46 | [diff] [blame] | 189 | handler->OnEvent(event); |
[email protected] | f44db2a | 2013-04-15 19:59:49 | [diff] [blame] | 190 | if (!delegate_ && event->cancelable()) |
| 191 | event->StopPropagation(); |
[email protected] | 4fda7de9 | 2012-11-21 16:58:50 | [diff] [blame] | 192 | } |
| 193 | |
[email protected] | 868869a | 2012-09-10 14:56:26 | [diff] [blame] | 194 | } // namespace ui |