event_rewriter.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2018 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/event_rewriter.h"
  5. #include <utility>
  6. #include "base/check_op.h"
  7. #include "base/notreached.h"
  8. #include "ui/events/event_rewriter_continuation.h"
  9. #include "ui/events/event_source.h"
  10. namespace ui {
  11. namespace {
  12. ui::EventDispatchDetails DispatcherDestroyed() {
  13. ui::EventDispatchDetails details;
  14. details.dispatcher_destroyed = true;
  15. return details;
  16. }
  17. } // anonymous namespace
  18. // Temporary fallback implementation, in terms of the old API,
  19. // factored out of EventSource::SendEventToSinkFromRewriter().
  20. // TODO(kpschoedel): Remove along with old API.
  21. EventDispatchDetails EventRewriter::RewriteEvent(
  22. const Event& event,
  23. const Continuation continuation) {
  24. std::unique_ptr<Event> rewritten_event;
  25. EventRewriteStatus status = RewriteEvent(event, &rewritten_event);
  26. if (status == EVENT_REWRITE_DISCARD) {
  27. CHECK(!rewritten_event);
  28. return continuation->DiscardEvent();
  29. }
  30. if (status == EVENT_REWRITE_CONTINUE) {
  31. CHECK(!rewritten_event);
  32. return continuation->SendEvent(&event);
  33. }
  34. CHECK(rewritten_event);
  35. EventDispatchDetails details =
  36. continuation->SendEventFinally(rewritten_event.get());
  37. while (status == EVENT_REWRITE_DISPATCH_ANOTHER) {
  38. if (details.dispatcher_destroyed)
  39. return details;
  40. std::unique_ptr<Event> new_event;
  41. status = NextDispatchEvent(*rewritten_event, &new_event);
  42. if (status == EVENT_REWRITE_DISCARD)
  43. return continuation->DiscardEvent();
  44. CHECK_NE(EVENT_REWRITE_CONTINUE, status);
  45. CHECK(new_event);
  46. details = continuation->SendEventFinally(new_event.get());
  47. rewritten_event = std::move(new_event);
  48. }
  49. return details;
  50. }
  51. // Temporary default implementation of the old API, so that subclasses'
  52. // implementations can be removed.
  53. // TODO(kpschoedel): Remove old API.
  54. EventRewriteStatus EventRewriter::RewriteEvent(
  55. const Event& event,
  56. std::unique_ptr<Event>* rewritten_event) {
  57. NOTREACHED();
  58. return EVENT_REWRITE_DISCARD;
  59. }
  60. // Temporary default implementation of the old API, so that subclasses'
  61. // implementations can be removed.
  62. // TODO(kpschoedel): Remove old API.
  63. EventRewriteStatus EventRewriter::NextDispatchEvent(
  64. const Event& last_event,
  65. std::unique_ptr<Event>* new_event) {
  66. NOTREACHED();
  67. return EVENT_REWRITE_DISCARD;
  68. }
  69. // TODO(kpschoedel): Remove old API.
  70. EventDispatchDetails EventRewriter::SendEventToEventSource(EventSource* source,
  71. Event* event) const {
  72. return source->SendEventToSinkFromRewriter(event, this);
  73. }
  74. EventDispatchDetails EventRewriter::SendEvent(const Continuation continuation,
  75. const Event* event) {
  76. return continuation ? continuation->SendEvent(event) : DispatcherDestroyed();
  77. }
  78. EventDispatchDetails EventRewriter::SendEventFinally(
  79. const Continuation continuation,
  80. const Event* event) {
  81. return continuation ? continuation->SendEventFinally(event)
  82. : DispatcherDestroyed();
  83. }
  84. EventDispatchDetails EventRewriter::DiscardEvent(
  85. const Continuation continuation) {
  86. return continuation ? continuation->DiscardEvent()
  87. : DispatcherDestroyed();
  88. }
  89. } // namespace ui