OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef UI_EVENTS_PLATFORM_PLATFORM_EVENT_SOURCE_H_ | 5 #ifndef UI_EVENTS_PLATFORM_PLATFORM_EVENT_SOURCE_H_ |
6 #define UI_EVENTS_PLATFORM_PLATFORM_EVENT_SOURCE_H_ | 6 #define UI_EVENTS_PLATFORM_PLATFORM_EVENT_SOURCE_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
(...skipping 15 matching lines...) Expand all Loading... |
26 namespace test { | 26 namespace test { |
27 class PlatformEventSourceTestAPI; | 27 class PlatformEventSourceTestAPI; |
28 } | 28 } |
29 | 29 |
30 // PlatformEventSource receives events from a source and dispatches the events | 30 // PlatformEventSource receives events from a source and dispatches the events |
31 // to the appropriate dispatchers. | 31 // to the appropriate dispatchers. |
32 class EVENTS_EXPORT PlatformEventSource { | 32 class EVENTS_EXPORT PlatformEventSource { |
33 public: | 33 public: |
34 virtual ~PlatformEventSource(); | 34 virtual ~PlatformEventSource(); |
35 | 35 |
| 36 // Returns the thread-local singleton. |
36 static PlatformEventSource* GetInstance(); | 37 static PlatformEventSource* GetInstance(); |
37 | 38 |
38 // Adds a dispatcher to the dispatcher list. If a dispatcher is added during | 39 // Adds a dispatcher to the dispatcher list. If a dispatcher is added during |
39 // dispatching an event, then the newly added dispatcher also receives that | 40 // dispatching an event, then the newly added dispatcher also receives that |
40 // event. | 41 // event. |
41 void AddPlatformEventDispatcher(PlatformEventDispatcher* dispatcher); | 42 void AddPlatformEventDispatcher(PlatformEventDispatcher* dispatcher); |
42 | 43 |
43 // Removes a dispatcher from the dispatcher list. Dispatchers can safely be | 44 // Removes a dispatcher from the dispatcher list. Dispatchers can safely be |
44 // removed from the dispatcher list during an event is being dispatched, | 45 // removed from the dispatcher list during an event is being dispatched, |
45 // without affecting the dispatch of the event to other existing dispatchers. | 46 // without affecting the dispatch of the event to other existing dispatchers. |
(...skipping 11 matching lines...) Expand all Loading... |
57 PlatformEventDispatcher* dispatcher); | 58 PlatformEventDispatcher* dispatcher); |
58 | 59 |
59 // Called to indicate that the source should stop dispatching the current | 60 // Called to indicate that the source should stop dispatching the current |
60 // stream of events and wait until the next iteration of the message-loop to | 61 // stream of events and wait until the next iteration of the message-loop to |
61 // dispatch the rest of the events. | 62 // dispatch the rest of the events. |
62 virtual void StopCurrentEventStream(); | 63 virtual void StopCurrentEventStream(); |
63 | 64 |
64 void AddPlatformEventObserver(PlatformEventObserver* observer); | 65 void AddPlatformEventObserver(PlatformEventObserver* observer); |
65 void RemovePlatformEventObserver(PlatformEventObserver* observer); | 66 void RemovePlatformEventObserver(PlatformEventObserver* observer); |
66 | 67 |
| 68 // Creates PlatformEventSource and sets it as a thread-local singleton. |
67 static std::unique_ptr<PlatformEventSource> CreateDefault(); | 69 static std::unique_ptr<PlatformEventSource> CreateDefault(); |
68 | 70 |
69 protected: | 71 protected: |
70 PlatformEventSource(); | 72 PlatformEventSource(); |
71 | 73 |
72 // Dispatches |platform_event| to the dispatchers. If there is an override | 74 // Dispatches |platform_event| to the dispatchers. If there is an override |
73 // dispatcher installed using |OverrideDispatcher()|, then that dispatcher | 75 // dispatcher installed using |OverrideDispatcher()|, then that dispatcher |
74 // receives the event first. |POST_DISPATCH_QUIT_LOOP| flag is set in the | 76 // receives the event first. |POST_DISPATCH_QUIT_LOOP| flag is set in the |
75 // returned value if the event-source should stop dispatching events at the | 77 // returned value if the event-source should stop dispatching events at the |
76 // current message-loop iteration. | 78 // current message-loop iteration. |
77 virtual uint32_t DispatchEvent(PlatformEvent platform_event); | 79 virtual uint32_t DispatchEvent(PlatformEvent platform_event); |
78 | 80 |
79 private: | 81 private: |
80 friend class ScopedEventDispatcher; | 82 friend class ScopedEventDispatcher; |
81 friend class test::PlatformEventSourceTestAPI; | 83 friend class test::PlatformEventSourceTestAPI; |
82 | 84 |
83 static PlatformEventSource* instance_; | |
84 | |
85 // This is invoked when the list of dispatchers changes (i.e. a new dispatcher | 85 // This is invoked when the list of dispatchers changes (i.e. a new dispatcher |
86 // is added, or a dispatcher is removed). | 86 // is added, or a dispatcher is removed). |
87 virtual void OnDispatcherListChanged(); | 87 virtual void OnDispatcherListChanged(); |
88 | 88 |
89 void OnOverriddenDispatcherRestored(); | 89 void OnOverriddenDispatcherRestored(); |
90 | 90 |
91 // Use an base::ObserverList<> instead of an std::vector<> to store the list | 91 // Use an base::ObserverList<> instead of an std::vector<> to store the list |
92 // of | 92 // of |
93 // dispatchers, so that adding/removing dispatchers during an event dispatch | 93 // dispatchers, so that adding/removing dispatchers during an event dispatch |
94 // is well-defined. | 94 // is well-defined. |
95 typedef base::ObserverList<PlatformEventDispatcher> | 95 typedef base::ObserverList<PlatformEventDispatcher> |
96 PlatformEventDispatcherList; | 96 PlatformEventDispatcherList; |
97 PlatformEventDispatcherList dispatchers_; | 97 PlatformEventDispatcherList dispatchers_; |
98 PlatformEventDispatcher* overridden_dispatcher_; | 98 PlatformEventDispatcher* overridden_dispatcher_; |
99 | 99 |
100 // Used to keep track of whether the current override-dispatcher has been | 100 // Used to keep track of whether the current override-dispatcher has been |
101 // reset and a previous override-dispatcher has been restored. | 101 // reset and a previous override-dispatcher has been restored. |
102 bool overridden_dispatcher_restored_; | 102 bool overridden_dispatcher_restored_; |
103 | 103 |
104 base::ObserverList<PlatformEventObserver> observers_; | 104 base::ObserverList<PlatformEventObserver> observers_; |
105 | 105 |
106 DISALLOW_COPY_AND_ASSIGN(PlatformEventSource); | 106 DISALLOW_COPY_AND_ASSIGN(PlatformEventSource); |
107 }; | 107 }; |
108 | 108 |
109 } // namespace ui | 109 } // namespace ui |
110 | 110 |
111 #endif // UI_EVENTS_PLATFORM_PLATFORM_EVENT_SOURCE_H_ | 111 #endif // UI_EVENTS_PLATFORM_PLATFORM_EVENT_SOURCE_H_ |
OLD | NEW |