Add ICU message format support
[chromium-blink-merge.git] / ui / events / platform / platform_event_source.cc
blob99f69ac37390acd646819bbe8f5c8b1427bf2b71
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.
5 #include "ui/events/platform/platform_event_source.h"
7 #include <algorithm>
9 #include "base/message_loop/message_loop.h"
10 #include "ui/events/platform/platform_event_dispatcher.h"
11 #include "ui/events/platform/platform_event_observer.h"
12 #include "ui/events/platform/scoped_event_dispatcher.h"
14 namespace ui {
16 // static
17 PlatformEventSource* PlatformEventSource::instance_ = NULL;
19 PlatformEventSource::PlatformEventSource()
20 : overridden_dispatcher_(NULL),
21 overridden_dispatcher_restored_(false) {
22 CHECK(!instance_) << "Only one platform event source can be created.";
23 instance_ = this;
26 PlatformEventSource::~PlatformEventSource() {
27 CHECK_EQ(this, instance_);
28 instance_ = NULL;
31 PlatformEventSource* PlatformEventSource::GetInstance() { return instance_; }
33 void PlatformEventSource::AddPlatformEventDispatcher(
34 PlatformEventDispatcher* dispatcher) {
35 CHECK(dispatcher);
36 dispatchers_.AddObserver(dispatcher);
37 OnDispatcherListChanged();
40 void PlatformEventSource::RemovePlatformEventDispatcher(
41 PlatformEventDispatcher* dispatcher) {
42 dispatchers_.RemoveObserver(dispatcher);
43 OnDispatcherListChanged();
46 scoped_ptr<ScopedEventDispatcher> PlatformEventSource::OverrideDispatcher(
47 PlatformEventDispatcher* dispatcher) {
48 CHECK(dispatcher);
49 overridden_dispatcher_restored_ = false;
50 return make_scoped_ptr(
51 new ScopedEventDispatcher(&overridden_dispatcher_, dispatcher));
54 void PlatformEventSource::StopCurrentEventStream() {
57 void PlatformEventSource::AddPlatformEventObserver(
58 PlatformEventObserver* observer) {
59 CHECK(observer);
60 observers_.AddObserver(observer);
63 void PlatformEventSource::RemovePlatformEventObserver(
64 PlatformEventObserver* observer) {
65 observers_.RemoveObserver(observer);
68 uint32_t PlatformEventSource::DispatchEvent(PlatformEvent platform_event) {
69 uint32_t action = POST_DISPATCH_PERFORM_DEFAULT;
71 FOR_EACH_OBSERVER(PlatformEventObserver, observers_,
72 WillProcessEvent(platform_event));
73 // Give the overridden dispatcher a chance to dispatch the event first.
74 if (overridden_dispatcher_)
75 action = overridden_dispatcher_->DispatchEvent(platform_event);
77 if ((action & POST_DISPATCH_PERFORM_DEFAULT) &&
78 dispatchers_.might_have_observers()) {
79 base::ObserverList<PlatformEventDispatcher>::Iterator iter(&dispatchers_);
80 while (PlatformEventDispatcher* dispatcher = iter.GetNext()) {
81 if (dispatcher->CanDispatchEvent(platform_event))
82 action = dispatcher->DispatchEvent(platform_event);
83 if (action & POST_DISPATCH_STOP_PROPAGATION)
84 break;
87 FOR_EACH_OBSERVER(PlatformEventObserver, observers_,
88 DidProcessEvent(platform_event));
90 // If an overridden dispatcher has been destroyed, then the platform
91 // event-source should halt dispatching the current stream of events, and wait
92 // until the next message-loop iteration for dispatching events. This lets any
93 // nested message-loop to unwind correctly and any new dispatchers to receive
94 // the correct sequence of events.
95 if (overridden_dispatcher_restored_)
96 StopCurrentEventStream();
98 overridden_dispatcher_restored_ = false;
100 return action;
103 void PlatformEventSource::OnDispatcherListChanged() {
106 void PlatformEventSource::OnOverriddenDispatcherRestored() {
107 CHECK(overridden_dispatcher_);
108 overridden_dispatcher_restored_ = true;
111 } // namespace ui