Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: ui/base/events/event_dispatcher.h

Issue 11308322: events: Start changing EventHandler interface to not return a value. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/base/events/event.cc ('k') | ui/base/events/event_dispatcher.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "ui/base/events/event.h" 8 #include "ui/base/events/event.h"
9 #include "ui/base/events/event_constants.h" 9 #include "ui/base/events/event_constants.h"
10 #include "ui/base/events/event_target.h" 10 #include "ui/base/events/event_target.h"
11 #include "ui/base/ui_export.h" 11 #include "ui/base/ui_export.h"
12 12
13 namespace ui { 13 namespace ui {
14 14
15 // Dispatches events to appropriate targets. 15 // Dispatches events to appropriate targets.
16 class UI_EXPORT EventDispatcher { 16 class UI_EXPORT EventDispatcher {
17 public: 17 public:
18 EventDispatcher(); 18 EventDispatcher();
19 virtual ~EventDispatcher(); 19 virtual ~EventDispatcher();
20 20
21 // Returns whether an event can still be dispatched to a target. (e.g. during 21 // Returns whether an event can still be dispatched to a target. (e.g. during
22 // event dispatch, one of the handlers may have destroyed the target, in which 22 // event dispatch, one of the handlers may have destroyed the target, in which
23 // case the event can no longer be dispatched to the target). 23 // case the event can no longer be dispatched to the target).
24 virtual bool CanDispatchToTarget(EventTarget* target) = 0; 24 virtual bool CanDispatchToTarget(EventTarget* target) = 0;
25 25
26 template<class T> 26 template<class T>
27 int ProcessEvent(EventTarget* target, T* event) { 27 void ProcessEvent(EventTarget* target, T* event) {
28 if (!target || !target->CanAcceptEvents()) 28 if (!target || !target->CanAcceptEvents())
29 return ER_UNHANDLED; 29 return;
30 30
31 ScopedDispatchHelper dispatch_helper(event); 31 ScopedDispatchHelper dispatch_helper(event);
32 dispatch_helper.set_target(target); 32 dispatch_helper.set_target(target);
33 33
34 EventHandlerList list; 34 EventHandlerList list;
35 target->GetPreTargetHandlers(&list); 35 target->GetPreTargetHandlers(&list);
36 dispatch_helper.set_phase(EP_PRETARGET); 36 dispatch_helper.set_phase(EP_PRETARGET);
37 int result = DispatchEventToEventHandlers(list, event); 37 DispatchEventToEventHandlers(list, event);
38 if (result & ER_CONSUMED) 38 if (event->stopped_propagation())
39 return result; 39 return;
40 40
41 // If the event hasn't been consumed, trigger the default handler. Note that 41 // If the event hasn't been consumed, trigger the default handler. Note that
42 // even if the event has already been handled (i.e. return result has 42 // even if the event has already been handled (i.e. return result has
43 // ER_HANDLED set), that means that the event should still be processed at 43 // ER_HANDLED set), that means that the event should still be processed at
44 // this layer, however it should not be processed in the next layer of 44 // this layer, however it should not be processed in the next layer of
45 // abstraction. 45 // abstraction.
46 if (CanDispatchToTarget(target)) { 46 if (CanDispatchToTarget(target)) {
47 dispatch_helper.set_phase(EP_TARGET); 47 dispatch_helper.set_phase(EP_TARGET);
48 result |= DispatchEvent(target, event); 48 DispatchEvent(target, event);
49 dispatch_helper.set_result(event->result() | result); 49 if (event->stopped_propagation())
50 if (result & ER_CONSUMED) 50 return;
51 return result;
52 } 51 }
53 52
54 if (!CanDispatchToTarget(target)) 53 if (!CanDispatchToTarget(target))
55 return result; 54 return;
56 55
57 list.clear(); 56 list.clear();
58 target->GetPostTargetHandlers(&list); 57 target->GetPostTargetHandlers(&list);
59 dispatch_helper.set_phase(EP_POSTTARGET); 58 dispatch_helper.set_phase(EP_POSTTARGET);
60 result |= DispatchEventToEventHandlers(list, event); 59 DispatchEventToEventHandlers(list, event);
61 return result;
62 } 60 }
63 61
64 const Event* current_event() const { return current_event_; } 62 const Event* current_event() const { return current_event_; }
65 Event* current_event() { return current_event_; } 63 Event* current_event() { return current_event_; }
66 64
67 private: 65 private:
68 class UI_EXPORT ScopedDispatchHelper : public NON_EXPORTED_BASE( 66 class UI_EXPORT ScopedDispatchHelper : public NON_EXPORTED_BASE(
69 Event::DispatcherApi) { 67 Event::DispatcherApi) {
70 public: 68 public:
71 explicit ScopedDispatchHelper(Event* event); 69 explicit ScopedDispatchHelper(Event* event);
72 virtual ~ScopedDispatchHelper(); 70 virtual ~ScopedDispatchHelper();
73 private: 71 private:
74 DISALLOW_COPY_AND_ASSIGN(ScopedDispatchHelper); 72 DISALLOW_COPY_AND_ASSIGN(ScopedDispatchHelper);
75 }; 73 };
76 74
77 template<class T> 75 template<class T>
78 int DispatchEventToEventHandlers(EventHandlerList& list, T* event) { 76 void DispatchEventToEventHandlers(EventHandlerList& list, T* event) {
79 int result = ER_UNHANDLED;
80 Event::DispatcherApi dispatch_helper(event);
81 for (EventHandlerList::const_iterator it = list.begin(), 77 for (EventHandlerList::const_iterator it = list.begin(),
82 end = list.end(); it != end; ++it) { 78 end = list.end(); it != end; ++it) {
83 result |= DispatchEvent((*it), event); 79 DispatchEvent((*it), event);
84 dispatch_helper.set_result(event->result() | result); 80 if (event->stopped_propagation())
85 if (result & ER_CONSUMED) 81 return;
86 return result;
87 } 82 }
88 return result;
89 } 83 }
90 84
91 // Dispatches an event, and makes sure it sets ER_CONSUMED on the 85 // Dispatches an event, and makes sure it sets ER_CONSUMED on the
92 // event-handling result if the dispatcher itself has been destroyed during 86 // event-handling result if the dispatcher itself has been destroyed during
93 // dispatching the event to the event handler. 87 // dispatching the event to the event handler.
94 template<class T> 88 template<class T>
95 int DispatchEvent(EventHandler* handler, T* event) { 89 void DispatchEvent(EventHandler* handler, T* event) {
96 // If the target has been invalidated or deleted, don't dispatch the event. 90 // If the target has been invalidated or deleted, don't dispatch the event.
97 if (!CanDispatchToTarget(event->target())) 91 if (!CanDispatchToTarget(event->target())) {
98 return ui::ER_CONSUMED; 92 event->StopPropagation();
93 return;
94 }
99 bool destroyed = false; 95 bool destroyed = false;
100 set_on_destroy_ = &destroyed; 96 set_on_destroy_ = &destroyed;
101 97
102 // Do not use base::AutoReset for |current_event_|. The EventDispatcher can 98 // Do not use base::AutoReset for |current_event_|. The EventDispatcher can
103 // be destroyed by the event-handler during the event-dispatch. That would 99 // be destroyed by the event-handler during the event-dispatch. That would
104 // cause invalid memory-write when AutoReset tries to restore the value. 100 // cause invalid memory-write when AutoReset tries to restore the value.
105 Event* old_event = current_event_; 101 Event* old_event = current_event_;
106 current_event_ = event; 102 current_event_ = event;
107 int result = DispatchEventToSingleHandler(handler, event); 103 DispatchEventToSingleHandler(handler, event);
108 if (destroyed) { 104 if (destroyed) {
109 result |= ui::ER_CONSUMED; 105 event->StopPropagation();
110 } else { 106 } else {
111 current_event_ = old_event; 107 current_event_ = old_event;
112 set_on_destroy_ = NULL; 108 set_on_destroy_ = NULL;
113 } 109 }
114 return result;
115 } 110 }
116 111
117 EventResult DispatchEventToSingleHandler(EventHandler* handler, Event* event); 112 void DispatchEventToSingleHandler(EventHandler* handler, Event* event);
118 113
119 // This is used to track whether the dispatcher has been destroyed in the 114 // This is used to track whether the dispatcher has been destroyed in the
120 // middle of dispatching an event. 115 // middle of dispatching an event.
121 bool* set_on_destroy_; 116 bool* set_on_destroy_;
122 117
123 Event* current_event_; 118 Event* current_event_;
124 119
125 DISALLOW_COPY_AND_ASSIGN(EventDispatcher); 120 DISALLOW_COPY_AND_ASSIGN(EventDispatcher);
126 }; 121 };
127 122
128 } // namespace ui 123 } // namespace ui
129 124
130 #endif // UI_BASE_EVENTS_EVENT_DISPATCHER_H_ 125 #endif // UI_BASE_EVENTS_EVENT_DISPATCHER_H_
OLDNEW
« no previous file with comments | « ui/base/events/event.cc ('k') | ui/base/events/event_dispatcher.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698