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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
// Copyright 2013 The Chromium Authors
// 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 "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "ui/events/event_rewriter.h"
#include "ui/events/event_rewriter_continuation.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
class EventSource::EventRewriterContinuationImpl
: public EventRewriterContinuation {
public:
// Constructs an EventRewriterContinuationImpl at the end of the source's
// rewriter list.
static void Create(EventSource* const source, EventRewriter* rewriter) {
DCHECK(source);
DCHECK(rewriter);
DCHECK(source->FindContinuation(rewriter) == source->rewriter_list_.end());
source->rewriter_list_.push_back(
std::make_unique<EventRewriterContinuationImpl>(source, rewriter));
EventRewriterList::iterator it = source->rewriter_list_.end();
--it;
CHECK((*it)->rewriter() == rewriter);
(*it)->self_ = it;
}
EventRewriterContinuationImpl(EventSource* const source,
EventRewriter* rewriter)
: source_(source),
rewriter_(rewriter),
self_(source->rewriter_list_.end()) {}
EventRewriterContinuationImpl(const EventRewriterContinuationImpl&) = delete;
EventRewriterContinuationImpl& operator=(
const EventRewriterContinuationImpl&) = delete;
~EventRewriterContinuationImpl() override {}
EventRewriter* rewriter() const { return rewriter_; }
base::WeakPtr<EventRewriterContinuationImpl> GetWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
// EventRewriterContinuation overrides:
EventDispatchDetails SendEvent(const Event* event) override {
EventRewriterList::iterator next = self_;
++next;
if (next == source_->rewriter_list_.end())
return SendEventFinally(event);
return (*next)->rewriter_->RewriteEvent(*event, (*next)->GetWeakPtr());
}
EventDispatchDetails SendEventFinally(const Event* event) override {
return source_->DeliverEventToSink(const_cast<Event*>(event));
}
EventDispatchDetails DiscardEvent() override {
ui::EventDispatchDetails details;
details.event_discarded = true;
return details;
}
private:
const raw_ptr<EventSource> source_;
raw_ptr<EventRewriter, DanglingUntriaged> rewriter_;
EventRewriterList::iterator self_;
base::WeakPtrFactory<EventRewriterContinuationImpl> weak_ptr_factory_{this};
};
EventSource::EventSource() = default;
EventSource::~EventSource() = default;
void EventSource::AddEventRewriter(EventRewriter* rewriter) {
EventRewriterContinuationImpl::Create(this, rewriter);
}
void EventSource::RemoveEventRewriter(EventRewriter* rewriter) {
EventRewriterList::iterator it = FindContinuation(rewriter);
if (it == rewriter_list_.end()) {
// We need to tolerate attempting to remove an unregistered
// EventRewriter, because many unit tests currently do so:
// the rewriter gets added to the current root window source
// on construction, and removed from the current root window
// source on destruction, but the root window changes in
// between.
LOG(WARNING) << "EventRewriter not registered";
return;
}
rewriter_list_.erase(it);
}
EventDispatchDetails EventSource::SendEventToSink(const Event* event) {
return SendEventToSinkFromRewriter(event, nullptr);
}
EventDispatchDetails EventSource::DeliverEventToSink(Event* event) {
EventSink* sink = GetEventSink();
CHECK(sink);
return sink->OnEventFromSource(event);
}
EventDispatchDetails EventSource::SendEventToSinkFromRewriter(
const Event* event,
const EventRewriter* rewriter) {
std::unique_ptr<Event> event_clone;
EventRewriterList::iterator it = rewriter_list_.begin();
if (rewriter) {
// If a rewriter reposted |event|, only send it to subsequent rewriters.
it = FindContinuation(rewriter);
CHECK(it != rewriter_list_.end());
++it;
}
if (it == rewriter_list_.end()) {
return DeliverEventToSink(const_cast<Event*>(event));
}
const Event* event_for_rewriting = event;
EventRewriterContinuationImpl* const next = it->get();
// Historically, EventRewriters are not expected to honor the event target.
// Though, due to observed crashes (see https://crbug.com/1347192), rewriters
// are being refactored to honor it. Thus, unless the next rewriter supports
// such behavior, event location must be equal to its root_location and target
// unset, which is not copied by Event::Clone() call below.
if (IsLocatedEventWithDifferentLocations(*event) &&
!next->rewriter()->SupportsNonRootLocation()) {
event_clone = event->Clone();
event_clone->AsLocatedEvent()->set_location_f(
event_clone->AsLocatedEvent()->root_location_f());
event_for_rewriting = event_clone.get();
}
return next->rewriter()->RewriteEvent(*event_for_rewriting,
next->GetWeakPtr());
}
EventSource::EventRewriterList::iterator EventSource::FindContinuation(
const EventRewriter* rewriter) {
auto it = find_if(
rewriter_list_.begin(), rewriter_list_.end(),
[rewriter](const std::unique_ptr<EventRewriterContinuationImpl>& p)
-> bool { return p->rewriter() == rewriter; });
return it;
}
} // namespace ui
|