summaryrefslogtreecommitdiff
path: root/chromium/ui/events/event_source.cc
blob: e45be7c97b4493948385d6a921827a2e10d0c1df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/events/event_source.h"

#include <algorithm>

#include "base/stl_util.h"
#include "ui/events/event_rewriter.h"
#include "ui/events/event_sink.h"

namespace ui {
namespace {

bool IsLocatedEventWithDifferentLocations(const Event& event) {
  if (!event.IsLocatedEvent())
    return false;
  const LocatedEvent* located_event = event.AsLocatedEvent();
  return located_event->target() &&
         located_event->location_f() != located_event->root_location_f();
}

}  // namespace

EventSource::EventSource() {}

EventSource::~EventSource() {}

void EventSource::AddEventRewriter(EventRewriter* rewriter) {
  DCHECK(rewriter);
  DCHECK(!base::ContainsValue(rewriter_list_, rewriter));
  rewriter_list_.push_back(rewriter);
}

void EventSource::RemoveEventRewriter(EventRewriter* rewriter) {
  EventRewriterList::iterator find =
      std::find(rewriter_list_.begin(), rewriter_list_.end(), rewriter);
  if (find != rewriter_list_.end())
    rewriter_list_.erase(find);
}

EventDispatchDetails EventSource::SendEventToSink(Event* event) {
  std::unique_ptr<ui::Event> event_for_rewriting_ptr;
  Event* event_for_rewriting = event;
  if (!rewriter_list_.empty() && IsLocatedEventWithDifferentLocations(*event)) {
    // EventRewriters don't expect an event with differing location and
    // root-location (because they don't honor the target). Provide such an
    // event for rewriters.
    event_for_rewriting_ptr = ui::Event::Clone(*event);
    event_for_rewriting_ptr->AsLocatedEvent()->set_location_f(
        event_for_rewriting_ptr->AsLocatedEvent()->root_location_f());
    event_for_rewriting = event_for_rewriting_ptr.get();
  }
  std::unique_ptr<Event> rewritten_event;
  EventRewriteStatus status = EVENT_REWRITE_CONTINUE;
  EventRewriterList::const_iterator it = rewriter_list_.begin(),
                                    end = rewriter_list_.end();
  for (; it != end; ++it) {
    status = (*it)->RewriteEvent(*event_for_rewriting, &rewritten_event);
    if (status == EVENT_REWRITE_DISCARD) {
      CHECK(!rewritten_event);
      return EventDispatchDetails();
    }
    if (status == EVENT_REWRITE_CONTINUE) {
      CHECK(!rewritten_event);
      continue;
    }
    break;
  }
  CHECK((it == end && !rewritten_event) || rewritten_event);
  EventDispatchDetails details =
      DeliverEventToSink(rewritten_event ? rewritten_event.get() : event);
  if (details.dispatcher_destroyed)
    return details;

  while (status == EVENT_REWRITE_DISPATCH_ANOTHER) {
    std::unique_ptr<Event> new_event;
    status = (*it)->NextDispatchEvent(*rewritten_event, &new_event);
    if (status == EVENT_REWRITE_DISCARD)
      return EventDispatchDetails();
    CHECK_NE(EVENT_REWRITE_CONTINUE, status);
    CHECK(new_event);
    details = DeliverEventToSink(new_event.get());
    if (details.dispatcher_destroyed)
      return details;
    rewritten_event = std::move(new_event);
  }
  return EventDispatchDetails();
}

EventDispatchDetails EventSource::DeliverEventToSink(Event* event) {
  EventSink* sink = GetEventSink();
  CHECK(sink);
  return sink->OnEventFromSource(event);
}

}  // namespace ui