summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/widget/input/event_with_callback.cc
blob: b9842eff91eb327e5820f187ccd85fb9f9613de8 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2016 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 "third_party/blink/renderer/platform/widget/input/event_with_callback.h"

#include "base/time/time.h"
#include "base/trace_event/trace_event.h"
#include "third_party/blink/public/common/input/web_input_event_attribution.h"

namespace blink {

EventWithCallback::EventWithCallback(
    std::unique_ptr<WebCoalescedInputEvent> event,
    base::TimeTicks timestamp_now,
    InputHandlerProxy::EventDispositionCallback callback)
    : event_(std::make_unique<WebCoalescedInputEvent>(*event)),
      creation_timestamp_(timestamp_now),
      last_coalesced_timestamp_(timestamp_now) {
  original_events_.emplace_back(std::move(event), std::move(callback));
}

EventWithCallback::EventWithCallback(
    std::unique_ptr<WebCoalescedInputEvent> event,
    base::TimeTicks creation_timestamp,
    base::TimeTicks last_coalesced_timestamp,
    std::unique_ptr<OriginalEventList> original_events)
    : event_(std::move(event)),
      creation_timestamp_(creation_timestamp),
      last_coalesced_timestamp_(last_coalesced_timestamp) {
  if (original_events)
    original_events_.splice(original_events_.end(), *original_events);
}

EventWithCallback::~EventWithCallback() {}

bool EventWithCallback::CanCoalesceWith(const EventWithCallback& other) const {
  return event().CanCoalesce(other.event());
}

void EventWithCallback::SetScrollbarManipulationHandledOnCompositorThread() {
  for (auto& original_event : original_events_) {
    original_event.event_->EventPointer()
        ->SetScrollbarManipulationHandledOnCompositorThread();
  }
}

void EventWithCallback::CoalesceWith(EventWithCallback* other,
                                     base::TimeTicks timestamp_now) {
  event_->CoalesceWith(*other->event_);

  // Move original events.
  original_events_.splice(original_events_.end(), other->original_events_);
  last_coalesced_timestamp_ = timestamp_now;
}

static bool HandledOnCompositorThread(
    InputHandlerProxy::EventDisposition disposition) {
  return (disposition != InputHandlerProxy::DID_NOT_HANDLE &&
          disposition !=
              InputHandlerProxy::DID_NOT_HANDLE_NON_BLOCKING_DUE_TO_FLING &&
          disposition != InputHandlerProxy::DID_HANDLE_NON_BLOCKING);
}

void EventWithCallback::RunCallbacks(
    InputHandlerProxy::EventDisposition disposition,
    const ui::LatencyInfo& latency,
    std::unique_ptr<InputHandlerProxy::DidOverscrollParams>
        did_overscroll_params,
    const WebInputEventAttribution& attribution) {
  // |original_events_| could be empty if this is the scroll event extracted
  // from the matrix multiplication.
  if (original_events_.size() == 0)
    return;

  // Ack the oldest event with original latency.
  original_events_.front().event_->latency_info() = latency;
  std::move(original_events_.front().callback_)
      .Run(disposition, std::move(original_events_.front().event_),
           did_overscroll_params
               ? std::make_unique<InputHandlerProxy::DidOverscrollParams>(
                     *did_overscroll_params)
               : nullptr,
           attribution);
  original_events_.pop_front();

  // If the event was handled on compositor thread, ack other events with
  // coalesced latency to avoid redundant tracking. If not, the event should
  // be handle on main thread, use the original latency instead.
  //
  // We overwrite the trace_id to ensure proper flow events along the critical
  // path.
  bool handled = HandledOnCompositorThread(disposition);
  for (auto& coalesced_event : original_events_) {
    if (handled) {
      int64_t original_trace_id =
          coalesced_event.event_->latency_info().trace_id();
      coalesced_event.event_->latency_info() = latency;
      coalesced_event.event_->latency_info().set_trace_id(original_trace_id);
      coalesced_event.event_->latency_info().set_coalesced();
    }
    std::move(coalesced_event.callback_)
        .Run(disposition, std::move(coalesced_event.event_),
             did_overscroll_params
                 ? std::make_unique<InputHandlerProxy::DidOverscrollParams>(
                       *did_overscroll_params)
                 : nullptr,
             attribution);
  }
}

EventWithCallback::OriginalEventWithCallback::OriginalEventWithCallback(
    std::unique_ptr<WebCoalescedInputEvent> event,
    InputHandlerProxy::EventDispositionCallback callback)
    : event_(std::move(event)), callback_(std::move(callback)) {}

EventWithCallback::OriginalEventWithCallback::~OriginalEventWithCallback() {}

}  // namespace blink