MD Downloads: prevent search text from overlapping with the cancel search (X)
[chromium-blink-merge.git] / ui / events / platform / platform_event_source.h
blob5cd397e2809349c1a6e218474f50ecd9405db6f2
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 #ifndef UI_EVENTS_PLATFORM_PLATFORM_EVENT_SOURCE_H_
6 #define UI_EVENTS_PLATFORM_PLATFORM_EVENT_SOURCE_H_
8 #include <map>
9 #include <vector>
11 #include "base/auto_reset.h"
12 #include "base/macros.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/observer_list.h"
15 #include "ui/events/events_export.h"
16 #include "ui/events/platform/platform_event_types.h"
18 namespace ui {
20 class Event;
21 class PlatformEventDispatcher;
22 class PlatformEventObserver;
23 class ScopedEventDispatcher;
25 namespace test {
26 class PlatformEventSourceTestAPI;
29 // PlatformEventSource receives events from a source and dispatches the events
30 // to the appropriate dispatchers.
31 class EVENTS_EXPORT PlatformEventSource {
32 public:
33 virtual ~PlatformEventSource();
35 static PlatformEventSource* GetInstance();
37 // Adds a dispatcher to the dispatcher list. If a dispatcher is added during
38 // dispatching an event, then the newly added dispatcher also receives that
39 // event.
40 void AddPlatformEventDispatcher(PlatformEventDispatcher* dispatcher);
42 // Removes a dispatcher from the dispatcher list. Dispatchers can safely be
43 // removed from the dispatcher list during an event is being dispatched,
44 // without affecting the dispatch of the event to other existing dispatchers.
45 void RemovePlatformEventDispatcher(PlatformEventDispatcher* dispatcher);
47 // Installs a PlatformEventDispatcher that receives all the events. The
48 // dispatcher can process the event, or request that the default dispatchers
49 // be invoked by setting |POST_DISPATCH_PERFORM_DEFAULT| flag from the
50 // |DispatchEvent()| override.
51 // The returned |ScopedEventDispatcher| object is a handler for the overridden
52 // dispatcher. When this handler is destroyed, it removes the overridden
53 // dispatcher, and restores the previous override-dispatcher (or NULL if there
54 // wasn't any).
55 scoped_ptr<ScopedEventDispatcher> OverrideDispatcher(
56 PlatformEventDispatcher* dispatcher);
58 // Called to indicate that the source should stop dispatching the current
59 // stream of events and wait until the next iteration of the message-loop to
60 // dispatch the rest of the events.
61 virtual void StopCurrentEventStream();
63 void AddPlatformEventObserver(PlatformEventObserver* observer);
64 void RemovePlatformEventObserver(PlatformEventObserver* observer);
66 static scoped_ptr<PlatformEventSource> CreateDefault();
68 protected:
69 PlatformEventSource();
71 // Dispatches |platform_event| to the dispatchers. If there is an override
72 // dispatcher installed using |OverrideDispatcher()|, then that dispatcher
73 // receives the event first. |POST_DISPATCH_QUIT_LOOP| flag is set in the
74 // returned value if the event-source should stop dispatching events at the
75 // current message-loop iteration.
76 virtual uint32_t DispatchEvent(PlatformEvent platform_event);
78 private:
79 friend class ScopedEventDispatcher;
80 friend class test::PlatformEventSourceTestAPI;
82 static PlatformEventSource* instance_;
84 // This is invoked when the list of dispatchers changes (i.e. a new dispatcher
85 // is added, or a dispatcher is removed).
86 virtual void OnDispatcherListChanged();
88 void OnOverriddenDispatcherRestored();
90 // Use an base::ObserverList<> instead of an std::vector<> to store the list
91 // of
92 // dispatchers, so that adding/removing dispatchers during an event dispatch
93 // is well-defined.
94 typedef base::ObserverList<PlatformEventDispatcher>
95 PlatformEventDispatcherList;
96 PlatformEventDispatcherList dispatchers_;
97 PlatformEventDispatcher* overridden_dispatcher_;
99 // Used to keep track of whether the current override-dispatcher has been
100 // reset and a previous override-dispatcher has been restored.
101 bool overridden_dispatcher_restored_;
103 base::ObserverList<PlatformEventObserver> observers_;
105 DISALLOW_COPY_AND_ASSIGN(PlatformEventSource);
108 } // namespace ui
110 #endif // UI_EVENTS_PLATFORM_PLATFORM_EVENT_SOURCE_H_