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_
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"
21 class PlatformEventDispatcher
;
22 class PlatformEventObserver
;
23 class ScopedEventDispatcher
;
25 // PlatformEventSource receives events from a source and dispatches the events
26 // to the appropriate dispatchers.
27 class EVENTS_EXPORT PlatformEventSource
{
29 virtual ~PlatformEventSource();
31 static PlatformEventSource
* GetInstance();
33 // Adds a dispatcher to the dispatcher list. If a dispatcher is added during
34 // dispatching an event, then the newly added dispatcher also receives that
36 void AddPlatformEventDispatcher(PlatformEventDispatcher
* dispatcher
);
38 // Removes a dispatcher from the dispatcher list. Dispatchers can safely be
39 // removed from the dispatcher list during an event is being dispatched,
40 // without affecting the dispatch of the event to other existing dispatchers.
41 void RemovePlatformEventDispatcher(PlatformEventDispatcher
* dispatcher
);
43 // Installs a PlatformEventDispatcher that receives all the events. The
44 // dispatcher can process the event, or request that the default dispatchers
45 // be invoked by setting |POST_DISPATCH_PERFORM_DEFAULT| flag from the
46 // |DispatchEvent()| override.
47 // The returned |ScopedEventDispatcher| object is a handler for the overridden
48 // dispatcher. When this handler is destroyed, it removes the overridden
49 // dispatcher, and restores the previous override-dispatcher (or NULL if there
51 scoped_ptr
<ScopedEventDispatcher
> OverrideDispatcher(
52 PlatformEventDispatcher
* dispatcher
);
54 // Called to indicate that the source should stop dispatching the current
55 // stream of events and wait until the next iteration of the message-loop to
56 // dispatch the rest of the events.
57 virtual void StopCurrentEventStream();
59 void AddPlatformEventObserver(PlatformEventObserver
* observer
);
60 void RemovePlatformEventObserver(PlatformEventObserver
* observer
);
62 static scoped_ptr
<PlatformEventSource
> CreateDefault();
65 PlatformEventSource();
67 // Dispatches |platform_event| to the dispatchers. If there is an override
68 // dispatcher installed using |OverrideDispatcher()|, then that dispatcher
69 // receives the event first. |POST_DISPATCH_QUIT_LOOP| flag is set in the
70 // returned value if the event-source should stop dispatching events at the
71 // current message-loop iteration.
72 virtual uint32_t DispatchEvent(PlatformEvent platform_event
);
75 friend class ScopedEventDispatcher
;
76 static PlatformEventSource
* instance_
;
78 // This is invoked when the list of dispatchers changes (i.e. a new dispatcher
79 // is added, or a dispatcher is removed).
80 virtual void OnDispatcherListChanged();
82 void OnOverriddenDispatcherRestored();
84 // Use an ObserverList<> instead of an std::vector<> to store the list of
85 // dispatchers, so that adding/removing dispatchers during an event dispatch
87 typedef ObserverList
<PlatformEventDispatcher
> PlatformEventDispatcherList
;
88 PlatformEventDispatcherList dispatchers_
;
89 PlatformEventDispatcher
* overridden_dispatcher_
;
91 // Used to keep track of whether the current override-dispatcher has been
92 // reset and a previous override-dispatcher has been restored.
93 bool overridden_dispatcher_restored_
;
95 ObserverList
<PlatformEventObserver
> observers_
;
97 DISALLOW_COPY_AND_ASSIGN(PlatformEventSource
);
102 #endif // UI_EVENTS_PLATFORM_PLATFORM_EVENT_SOURCE_H_