platform_event_source.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #include "ui/events/platform/platform_event_source.h"
  5. #include <algorithm>
  6. #include <ostream>
  7. #include "base/lazy_instance.h"
  8. #include "base/observer_list.h"
  9. #include "base/threading/thread_local.h"
  10. #include "ui/events/platform/platform_event_dispatcher.h"
  11. #include "ui/events/platform/platform_event_observer.h"
  12. #include "ui/events/platform/scoped_event_dispatcher.h"
  13. namespace ui {
  14. namespace {
  15. // PlatformEventSource singleton is thread local so that different instances
  16. // can be used on different threads (e.g. browser thread should be able to
  17. // access PlatformEventSource owned by the UI Service's thread).
  18. base::LazyInstance<base::ThreadLocalPointer<PlatformEventSource>>::Leaky
  19. lazy_tls_ptr = LAZY_INSTANCE_INITIALIZER;
  20. } // namespace
  21. bool PlatformEventSource::ignore_native_platform_events_ = false;
  22. PlatformEventSource::PlatformEventSource()
  23. : overridden_dispatcher_(nullptr), overridden_dispatcher_restored_(false) {
  24. CHECK(!lazy_tls_ptr.Pointer()->Get())
  25. << "Only one platform event source can be created.";
  26. lazy_tls_ptr.Pointer()->Set(this);
  27. }
  28. PlatformEventSource::~PlatformEventSource() {
  29. CHECK_EQ(this, lazy_tls_ptr.Pointer()->Get());
  30. lazy_tls_ptr.Pointer()->Set(nullptr);
  31. }
  32. PlatformEventSource* PlatformEventSource::GetInstance() {
  33. return lazy_tls_ptr.Pointer()->Get();
  34. }
  35. bool PlatformEventSource::ShouldIgnoreNativePlatformEvents() {
  36. return ignore_native_platform_events_;
  37. }
  38. void PlatformEventSource::SetIgnoreNativePlatformEvents(bool ignore_events) {
  39. ignore_native_platform_events_ = ignore_events;
  40. }
  41. void PlatformEventSource::AddPlatformEventDispatcher(
  42. PlatformEventDispatcher* dispatcher) {
  43. CHECK(dispatcher);
  44. dispatchers_.AddObserver(dispatcher);
  45. OnDispatcherListChanged();
  46. }
  47. void PlatformEventSource::RemovePlatformEventDispatcher(
  48. PlatformEventDispatcher* dispatcher) {
  49. dispatchers_.RemoveObserver(dispatcher);
  50. OnDispatcherListChanged();
  51. }
  52. std::unique_ptr<ScopedEventDispatcher> PlatformEventSource::OverrideDispatcher(
  53. PlatformEventDispatcher* dispatcher) {
  54. CHECK(dispatcher);
  55. overridden_dispatcher_restored_ = false;
  56. return std::make_unique<ScopedEventDispatcher>(&overridden_dispatcher_,
  57. dispatcher);
  58. }
  59. void PlatformEventSource::AddPlatformEventObserver(
  60. PlatformEventObserver* observer) {
  61. CHECK(observer);
  62. observers_.AddObserver(observer);
  63. }
  64. void PlatformEventSource::RemovePlatformEventObserver(
  65. PlatformEventObserver* observer) {
  66. observers_.RemoveObserver(observer);
  67. }
  68. uint32_t PlatformEventSource::DispatchEvent(PlatformEvent platform_event) {
  69. uint32_t action = POST_DISPATCH_PERFORM_DEFAULT;
  70. for (PlatformEventObserver& observer : observers_)
  71. observer.WillProcessEvent(platform_event);
  72. // Give the overridden dispatcher a chance to dispatch the event first.
  73. if (overridden_dispatcher_)
  74. action = overridden_dispatcher_->DispatchEvent(platform_event);
  75. if (action & POST_DISPATCH_PERFORM_DEFAULT) {
  76. for (PlatformEventDispatcher& dispatcher : dispatchers_) {
  77. if (dispatcher.CanDispatchEvent(platform_event))
  78. action = dispatcher.DispatchEvent(platform_event);
  79. if (action & POST_DISPATCH_STOP_PROPAGATION)
  80. break;
  81. }
  82. }
  83. for (PlatformEventObserver& observer : observers_)
  84. observer.DidProcessEvent(platform_event);
  85. overridden_dispatcher_restored_ = false;
  86. return action;
  87. }
  88. void PlatformEventSource::OnDispatcherListChanged() {
  89. }
  90. void PlatformEventSource::OnOverriddenDispatcherRestored() {
  91. CHECK(overridden_dispatcher_);
  92. overridden_dispatcher_restored_ = true;
  93. }
  94. } // namespace ui