Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 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 #include "ui/events/platform/platform_event_source.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/message_loop/message_loop.h" | |
| 10 #include "ui/events/event.h" | |
| 11 #include "ui/events/platform/platform_event_dispatcher.h" | |
| 12 #include "ui/events/platform/platform_event_observer.h" | |
| 13 #include "ui/events/platform/scoped_event_dispatcher.h" | |
| 14 | |
| 15 namespace ui { | |
| 16 | |
| 17 // static | |
| 18 PlatformEventSource* PlatformEventSource::instance_ = NULL; | |
| 19 | |
| 20 PlatformEventSource::PlatformEventSource() | |
| 21 : overridden_dispatcher_(NULL), overridden_dispatcher_restored_(false) { | |
|
sky
2014/03/25 15:51:15
nit: when you wrap one param per line.
sadrul
2014/03/25 18:33:15
Done.
| |
| 22 CHECK(!instance_) << "Only one platform event source can be created."; | |
| 23 instance_ = this; | |
| 24 } | |
| 25 | |
| 26 PlatformEventSource::~PlatformEventSource() { | |
| 27 CHECK_EQ(this, instance_); | |
| 28 instance_ = NULL; | |
| 29 } | |
| 30 | |
| 31 PlatformEventSource* PlatformEventSource::GetInstance() { return instance_; } | |
| 32 | |
| 33 void PlatformEventSource::AddPlatformEventDispatcher( | |
| 34 PlatformEventDispatcher* dispatcher) { | |
| 35 CHECK(dispatcher); | |
|
sky
2014/03/25 15:51:15
DCHECK not already in dispatchers_?
sadrul
2014/03/25 18:33:15
Done.
| |
| 36 dispatchers_.push_back(dispatcher); | |
| 37 } | |
| 38 | |
| 39 void PlatformEventSource::RemovePlatformEventDispatcher( | |
| 40 PlatformEventDispatcher* dispatcher) { | |
| 41 PlatformEventDispatcherList::iterator remove = | |
| 42 std::remove(dispatchers_.begin(), dispatchers_.end(), dispatcher); | |
| 43 if (remove != dispatchers_.end()) | |
| 44 dispatchers_.erase(remove); | |
| 45 } | |
| 46 | |
| 47 scoped_ptr<ScopedEventDispatcher> PlatformEventSource::OverrideDispatcher( | |
| 48 PlatformEventDispatcher* dispatcher) { | |
| 49 CHECK(dispatcher); | |
| 50 overridden_dispatcher_restored_ = false; | |
| 51 return make_scoped_ptr( | |
| 52 new ScopedEventDispatcher(&overridden_dispatcher_, dispatcher)); | |
| 53 } | |
| 54 | |
| 55 void PlatformEventSource::AddPlatformEventObserver( | |
| 56 PlatformEventObserver* observer) { | |
| 57 CHECK(observer); | |
| 58 observers_.AddObserver(observer); | |
| 59 } | |
| 60 | |
| 61 void PlatformEventSource::RemovePlatformEventObserver( | |
| 62 PlatformEventObserver* observer) { | |
| 63 observers_.RemoveObserver(observer); | |
| 64 } | |
| 65 | |
| 66 uint32_t PlatformEventSource::DispatchEvent(PlatformEvent platform_event) { | |
| 67 uint32_t action = POST_DISPATCH_PERFORM_DEFAULT; | |
| 68 bool should_quit = false; | |
| 69 | |
| 70 if (!WillProcessEvent(platform_event)) { | |
| 71 // Give the overridden dispatcher a chance to dispatch the event first. | |
| 72 if (overridden_dispatcher_) | |
| 73 action = overridden_dispatcher_->DispatchEvent(platform_event); | |
| 74 should_quit = !!(action & POST_DISPATCH_QUIT_LOOP); | |
| 75 | |
| 76 if (action & POST_DISPATCH_PERFORM_DEFAULT) { | |
| 77 for (PlatformEventDispatcherList::iterator i = dispatchers_.begin(); | |
| 78 i != dispatchers_.end(); | |
| 79 ++i) { | |
| 80 PlatformEventDispatcher* dispatcher = *(i); | |
| 81 if (dispatcher->CanDispatchEvent(platform_event)) | |
| 82 action = dispatcher->DispatchEvent(platform_event); | |
| 83 if (action & POST_DISPATCH_QUIT_LOOP) | |
| 84 should_quit = true; | |
| 85 if (action & POST_DISPATCH_STOP_PROPAGATION) | |
| 86 break; | |
| 87 } | |
| 88 } | |
| 89 } | |
| 90 DidProcessEvent(platform_event); | |
| 91 if (should_quit || overridden_dispatcher_restored_) { | |
|
sky
2014/03/25 15:51:15
Why does overridden_dispatcher_restored_ == true m
sadrul
2014/03/25 18:33:15
Yeah. This code was a bit misleading. I have clari
| |
| 92 base::MessageLoop::current()->QuitNow(); | |
| 93 action |= POST_DISPATCH_QUIT_LOOP; | |
| 94 } | |
| 95 overridden_dispatcher_restored_ = false; | |
| 96 | |
| 97 return action; | |
| 98 } | |
| 99 | |
| 100 bool PlatformEventSource::WillProcessEvent(PlatformEvent event) { | |
| 101 if (observers_.might_have_observers()) { | |
| 102 ObserverListBase<PlatformEventObserver>::Iterator it(observers_); | |
| 103 PlatformEventObserver* obs; | |
| 104 while ((obs = it.GetNext()) != NULL) { | |
| 105 if (obs->WillProcessEvent(event)) | |
| 106 return true; | |
| 107 } | |
| 108 } | |
| 109 return false; | |
| 110 } | |
| 111 | |
| 112 void PlatformEventSource::DidProcessEvent(PlatformEvent event) { | |
| 113 FOR_EACH_OBSERVER(PlatformEventObserver, observers_, DidProcessEvent(event)); | |
| 114 } | |
| 115 | |
| 116 void PlatformEventSource::OnOverriddenDispatcherRestored() { | |
| 117 CHECK(overridden_dispatcher_); | |
| 118 overridden_dispatcher_restored_ = true; | |
| 119 } | |
| 120 | |
| 121 } // namespace ui | |
| OLD | NEW |