platform_event_source.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #ifndef UI_EVENTS_PLATFORM_PLATFORM_EVENT_SOURCE_H_
  5. #define UI_EVENTS_PLATFORM_PLATFORM_EVENT_SOURCE_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include "base/auto_reset.h"
  9. #include "base/observer_list.h"
  10. #include "ui/events/events_export.h"
  11. #include "ui/events/platform_event.h"
  12. namespace ui {
  13. class PlatformEventDispatcher;
  14. class PlatformEventObserver;
  15. class ScopedEventDispatcher;
  16. namespace test {
  17. class PlatformEventSourceTestAPI;
  18. }
  19. // PlatformEventSource receives events from a source and dispatches the events
  20. // to the appropriate dispatchers.
  21. class EVENTS_EXPORT PlatformEventSource {
  22. public:
  23. PlatformEventSource(const PlatformEventSource&) = delete;
  24. PlatformEventSource& operator=(const PlatformEventSource&) = delete;
  25. virtual ~PlatformEventSource();
  26. // Returns the thread-local singleton.
  27. static PlatformEventSource* GetInstance();
  28. // Returns true when platform events should not be sent to the rest of the
  29. // pipeline. Mainly when Chrome is run in a test environment and it doesn't
  30. // expect any events from the platform and all events are synthesized by the
  31. // test environment.
  32. static bool ShouldIgnoreNativePlatformEvents();
  33. // Sets whether to ignore platform events and drop them or to forward them to
  34. // the rest of the input pipeline.
  35. static void SetIgnoreNativePlatformEvents(bool ignore_events);
  36. // Adds a dispatcher to the dispatcher list. If a dispatcher is added during
  37. // dispatching an event, then the newly added dispatcher also receives that
  38. // event.
  39. void AddPlatformEventDispatcher(PlatformEventDispatcher* dispatcher);
  40. // Removes a dispatcher from the dispatcher list. Dispatchers can safely be
  41. // removed from the dispatcher list during an event is being dispatched,
  42. // without affecting the dispatch of the event to other existing dispatchers.
  43. void RemovePlatformEventDispatcher(PlatformEventDispatcher* dispatcher);
  44. // Installs a PlatformEventDispatcher that receives all the events. The
  45. // dispatcher can process the event, or request that the default dispatchers
  46. // be invoked by setting |POST_DISPATCH_PERFORM_DEFAULT| flag from the
  47. // |DispatchEvent()| override.
  48. // The returned |ScopedEventDispatcher| object is a handler for the overridden
  49. // dispatcher. When this handler is destroyed, it removes the overridden
  50. // dispatcher, and restores the previous override-dispatcher (or NULL if there
  51. // wasn't any).
  52. std::unique_ptr<ScopedEventDispatcher> OverrideDispatcher(
  53. PlatformEventDispatcher* dispatcher);
  54. void AddPlatformEventObserver(PlatformEventObserver* observer);
  55. void RemovePlatformEventObserver(PlatformEventObserver* observer);
  56. // Creates PlatformEventSource and sets it as a thread-local singleton.
  57. static std::unique_ptr<PlatformEventSource> CreateDefault();
  58. protected:
  59. typedef base::ObserverList<PlatformEventObserver>::Unchecked
  60. PlatformEventObserverList;
  61. PlatformEventSource();
  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. PlatformEventObserverList& observers() { return observers_; }
  69. private:
  70. friend class ScopedEventDispatcher;
  71. friend class test::PlatformEventSourceTestAPI;
  72. // This is invoked when the list of dispatchers changes (i.e. a new dispatcher
  73. // is added, or a dispatcher is removed).
  74. virtual void OnDispatcherListChanged();
  75. void OnOverriddenDispatcherRestored();
  76. // Use an base::ObserverList<> instead of an std::vector<> to store the list
  77. // of
  78. // dispatchers, so that adding/removing dispatchers during an event dispatch
  79. // is well-defined.
  80. typedef base::ObserverList<PlatformEventDispatcher>::Unchecked
  81. PlatformEventDispatcherList;
  82. PlatformEventDispatcherList dispatchers_;
  83. PlatformEventDispatcher* overridden_dispatcher_;
  84. // Used to keep track of whether the current override-dispatcher has been
  85. // reset and a previous override-dispatcher has been restored.
  86. bool overridden_dispatcher_restored_;
  87. static bool ignore_native_platform_events_;
  88. PlatformEventObserverList observers_;
  89. };
  90. } // namespace ui
  91. #endif // UI_EVENTS_PLATFORM_PLATFORM_EVENT_SOURCE_H_