Chromium Code Reviews| Index: content/browser/renderer_host/smooth_scroll_gesture_controller.cc |
| diff --git a/content/browser/renderer_host/smooth_scroll_gesture_controller.cc b/content/browser/renderer_host/smooth_scroll_gesture_controller.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0f56b0cff8a4b724c460212af3d10de01f726eaa |
| --- /dev/null |
| +++ b/content/browser/renderer_host/smooth_scroll_gesture_controller.cc |
| @@ -0,0 +1,137 @@ |
| +// Copyright (c) 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 "content/browser/renderer_host/smooth_scroll_gesture_controller.h" |
| + |
| +#include "base/debug/trace_event.h" |
| +#include "base/message_loop.h" |
| +#include "content/common/view_messages.h" |
| +#include "content/port/browser/render_widget_host_view_port.h" |
| +#include "content/port/browser/smooth_scroll_gesture.h" |
| +#include "content/public/browser/render_widget_host.h" |
| + |
| +namespace content { |
| + |
| +namespace { |
| + |
| +// How many milliseconds apart synthetic scroll messages should be sent. |
| +const int kSyntheticScrollMessageIntervalMs = 8; |
| + |
| +} // namespace |
| + |
| +SmoothScrollGestureController::SmoothScrollGestureController() |
| + : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
| + pending_input_event_count_(0), |
| + tick_active_smooth_scroll_gestures_task_posted_(false) { |
| +} |
| + |
| +SmoothScrollGestureController::~SmoothScrollGestureController() { |
| +} |
| + |
| +void SmoothScrollGestureController::OnBeginSmoothScroll( |
| + RenderWidgetHostViewPort* view, int gesture_id, |
| + const ViewHostMsg_BeginSmoothScroll_Params& params) { |
| + active_smooth_scroll_gestures_.insert( |
| + std::make_pair(gesture_id, |
| + view->CreateSmoothScrollGesture( |
|
rjkroege
2013/02/11 22:47:27
I see that this makes wheel events. Note that whee
|
| + params.scroll_down, params.pixels_to_scroll, |
| + params.mouse_event_x, params.mouse_event_y))); |
| + |
| + // If an input ack is pending, then hold off ticking the gesture |
| + // until we get an input ack. |
| + if (pending_input_event_count_) |
| + return; |
| + if (tick_active_smooth_scroll_gestures_task_posted_) |
| + return; |
| + TickActiveSmoothScrollGesture(view->GetRenderWidgetHost()); |
| +} |
| + |
| +void SmoothScrollGestureController::OnForwardInputEvent() { |
| + ++pending_input_event_count_; |
| +} |
| + |
| +void SmoothScrollGestureController::OnInputEventACK(RenderWidgetHost* rwh) { |
| + --pending_input_event_count_; |
|
rjkroege
2013/02/11 22:47:27
should you verify that this has not gone negative?
|
| + // If an input ack is pending, then hold off ticking the gesture |
| + // until we get an input ack. |
| + if (!pending_input_event_count_ && !active_smooth_scroll_gestures_.empty()) |
| + TickActiveSmoothScrollGesture(rwh); |
| +} |
| + |
| +int SmoothScrollGestureController::SyntheticScrollMessageInterval() const { |
| + return kSyntheticScrollMessageIntervalMs; |
| +} |
| + |
| +void SmoothScrollGestureController::TickActiveSmoothScrollGesture( |
| + RenderWidgetHost* rwh) { |
| + TRACE_EVENT0("input", "RenderWidgetHostImpl::TickActiveSmoothScrollGesture"); |
| + tick_active_smooth_scroll_gestures_task_posted_ = false; |
| + if (active_smooth_scroll_gestures_.empty()) { |
| + TRACE_EVENT_INSTANT0("input", "EarlyOut_NoActiveScrollGesture"); |
| + return; |
| + } |
| + |
| + base::TimeTicks now = base::TimeTicks::HighResNow(); |
| + base::TimeDelta preferred_interval = |
| + base::TimeDelta::FromMilliseconds(kSyntheticScrollMessageIntervalMs); |
| + base::TimeDelta time_until_next_ideal_interval = |
| + (last_smooth_scroll_gestures_tick_time_ + preferred_interval) - |
| + now; |
| + if (time_until_next_ideal_interval.InMilliseconds() > 0) { |
| + TRACE_EVENT_INSTANT1( |
| + "input", "EarlyOut_TickedTooRecently", |
| + "delay", time_until_next_ideal_interval.InMilliseconds()); |
| + // Post a task. |
| + tick_active_smooth_scroll_gestures_task_posted_ = true; |
| + MessageLoop::current()->PostDelayedTask( |
| + FROM_HERE, |
| + base::Bind( |
| + &SmoothScrollGestureController::TickActiveSmoothScrollGesture, |
| + weak_factory_.GetWeakPtr(), |
| + rwh), |
| + time_until_next_ideal_interval); |
| + return; |
| + } |
| + |
| + last_smooth_scroll_gestures_tick_time_ = now; |
| + |
| + // Separate ticking of gestures from sending their completion messages. |
| + std::vector<int> ids_that_are_done; |
|
rjkroege
2013/02/11 22:47:27
possibly dumb question: why do you want to support
|
| + for (SmoothScrollGestureMap::iterator it = |
| + active_smooth_scroll_gestures_.begin(); |
| + it != active_smooth_scroll_gestures_.end(); |
| + ++it) { |
| + |
| + bool active = it->second->ForwardInputEvents(now, rwh); |
| + if (!active) |
| + ids_that_are_done.push_back(it->first); |
| + } |
| + |
| + // Delete completed gestures and send their completion event. |
| + for(size_t i = 0; i < ids_that_are_done.size(); i++) { |
| + int id = ids_that_are_done[i]; |
| + SmoothScrollGestureMap::iterator it = |
| + active_smooth_scroll_gestures_.find(id); |
| + DCHECK(it != active_smooth_scroll_gestures_.end()); |
| + active_smooth_scroll_gestures_.erase(it); |
| + |
| + rwh->Send(new ViewMsg_SmoothScrollCompleted(rwh->GetRoutingID(), id)); |
| + } |
| + |
| + // No need to post the next tick if an input is in flight. |
| + if (pending_input_event_count_) |
| + return; |
| + |
| + TRACE_EVENT_INSTANT1("input", "PostTickTask", |
| + "delay", preferred_interval.InMilliseconds()); |
| + tick_active_smooth_scroll_gestures_task_posted_ = true; |
|
rjkroege
2013/02/11 22:47:27
it would be nicer to not replicate this chunk of c
|
| + MessageLoop::current()->PostDelayedTask( |
| + FROM_HERE, |
| + base::Bind(&SmoothScrollGestureController::TickActiveSmoothScrollGesture, |
| + weak_factory_.GetWeakPtr(), |
| + rwh), |
| + preferred_interval); |
| +} |
| + |
| +} // namespace content |