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#ifndef UI_EVENTS_PLATFORM_PLATFORM_EVENT_SOURCE_H_
6#define UI_EVENTS_PLATFORM_PLATFORM_EVENT_SOURCE_H_
7
8#include <map>
9#include <vector>
10
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"
17
18namespace ui {
19
20class Event;
21class PlatformEventDispatcher;
22class PlatformEventObserver;
23class ScopedEventDispatcher;
24
25// PlatformEventSource receives events from a source and dispatches the events
26// to the appropriate dispatchers.
27class EVENTS_EXPORT PlatformEventSource {
28 public:
29  virtual ~PlatformEventSource();
30
31  static PlatformEventSource* GetInstance();
32
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
35  // event.
36  void AddPlatformEventDispatcher(PlatformEventDispatcher* dispatcher);
37
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);
42
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
50  // wasn't any).
51  scoped_ptr<ScopedEventDispatcher> OverrideDispatcher(
52      PlatformEventDispatcher* dispatcher);
53
54  void AddPlatformEventObserver(PlatformEventObserver* observer);
55  void RemovePlatformEventObserver(PlatformEventObserver* observer);
56
57  static scoped_ptr<PlatformEventSource> CreateDefault();
58
59 protected:
60  PlatformEventSource();
61
62  // Dispatches |platform_event| to the dispatchers. If there is an override
63  // dispatcher installed using |OverrideDispatcher()|, then that dispatcher
64  // receives the event first. |POST_DISPATCH_QUIT_LOOP| flag is set in the
65  // returned value if the event-source should stop dispatching events at the
66  // current message-loop iteration.
67  virtual uint32_t DispatchEvent(PlatformEvent platform_event);
68
69 private:
70  friend class ScopedEventDispatcher;
71  static PlatformEventSource* instance_;
72
73  // Called to indicate that the source should stop dispatching the current
74  // stream of events and wait until the next iteration of the message-loop to
75  // dispatch the rest of the events.
76  virtual void StopCurrentEventStream();
77
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();
81
82  void OnOverriddenDispatcherRestored();
83
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
86  // is well-defined.
87  typedef ObserverList<PlatformEventDispatcher> PlatformEventDispatcherList;
88  PlatformEventDispatcherList dispatchers_;
89  PlatformEventDispatcher* overridden_dispatcher_;
90
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_;
94
95  ObserverList<PlatformEventObserver> observers_;
96
97  DISALLOW_COPY_AND_ASSIGN(PlatformEventSource);
98};
99
100}  // namespace ui
101
102#endif  // UI_EVENTS_PLATFORM_PLATFORM_EVENT_SOURCE_H_
103