OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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 |
| 5 #include "content/browser/renderer_host/smooth_scroll_gesture_controller.h" |
| 6 |
| 7 #include "base/debug/trace_event.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "content/common/view_messages.h" |
| 10 #include "content/port/browser/render_widget_host_view_port.h" |
| 11 #include "content/port/browser/smooth_scroll_gesture.h" |
| 12 #include "content/public/browser/render_widget_host.h" |
| 13 |
| 14 namespace content { |
| 15 |
| 16 namespace { |
| 17 |
| 18 // How many milliseconds apart synthetic scroll messages should be sent. |
| 19 const int kSyntheticScrollMessageIntervalMs = 8; |
| 20 |
| 21 } // namespace |
| 22 |
| 23 SmoothScrollGestureController::SmoothScrollGestureController() |
| 24 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
| 25 pending_input_event_count_(0), |
| 26 tick_active_smooth_scroll_gesture_task_posted_(false) { |
| 27 } |
| 28 |
| 29 SmoothScrollGestureController::~SmoothScrollGestureController() { |
| 30 } |
| 31 |
| 32 bool SmoothScrollGestureController::OnBeginSmoothScroll( |
| 33 RenderWidgetHostViewPort* view, |
| 34 const ViewHostMsg_BeginSmoothScroll_Params& params) { |
| 35 if (pending_smooth_scroll_gesture_) |
| 36 return false; |
| 37 pending_smooth_scroll_gesture_ = view->CreateSmoothScrollGesture( |
| 38 params.scroll_down, params.pixels_to_scroll, |
| 39 params.mouse_event_x, params.mouse_event_y); |
| 40 |
| 41 // If an input ack is pending, then hold off ticking the gesture |
| 42 // until we get an input ack. |
| 43 if (pending_input_event_count_) |
| 44 return true; |
| 45 if (tick_active_smooth_scroll_gesture_task_posted_) |
| 46 return true; |
| 47 TickActiveSmoothScrollGesture(view->GetRenderWidgetHost()); |
| 48 return true; |
| 49 } |
| 50 |
| 51 void SmoothScrollGestureController::OnForwardInputEvent() { |
| 52 ++pending_input_event_count_; |
| 53 } |
| 54 |
| 55 void SmoothScrollGestureController::OnInputEventACK(RenderWidgetHost* rwh) { |
| 56 if (pending_input_event_count_ > 0) |
| 57 --pending_input_event_count_; |
| 58 // If an input ack is pending, then hold off ticking the gesture |
| 59 // until we get an input ack. |
| 60 if (!pending_input_event_count_ && pending_smooth_scroll_gesture_) |
| 61 TickActiveSmoothScrollGesture(rwh); |
| 62 } |
| 63 |
| 64 int SmoothScrollGestureController::SyntheticScrollMessageInterval() const { |
| 65 return kSyntheticScrollMessageIntervalMs; |
| 66 } |
| 67 |
| 68 void SmoothScrollGestureController::TickActiveSmoothScrollGesture( |
| 69 RenderWidgetHost* rwh) { |
| 70 TRACE_EVENT0("input", "RenderWidgetHostImpl::TickActiveSmoothScrollGesture"); |
| 71 tick_active_smooth_scroll_gesture_task_posted_ = false; |
| 72 if (!pending_smooth_scroll_gesture_) { |
| 73 TRACE_EVENT_INSTANT0("input", "EarlyOut_NoActiveScrollGesture", |
| 74 TRACE_EVENT_SCOPE_THREAD); |
| 75 return; |
| 76 } |
| 77 |
| 78 base::TimeTicks now = base::TimeTicks::HighResNow(); |
| 79 base::TimeDelta preferred_interval = |
| 80 base::TimeDelta::FromMilliseconds(kSyntheticScrollMessageIntervalMs); |
| 81 base::TimeDelta time_until_next_ideal_interval = |
| 82 (last_smooth_scroll_gesture_tick_time_ + preferred_interval) - |
| 83 now; |
| 84 if (time_until_next_ideal_interval.InMilliseconds() > 0) { |
| 85 TRACE_EVENT_INSTANT1( |
| 86 "input", "EarlyOut_TickedTooRecently", TRACE_EVENT_SCOPE_THREAD, |
| 87 "delay", time_until_next_ideal_interval.InMilliseconds()); |
| 88 // Post a task. |
| 89 tick_active_smooth_scroll_gesture_task_posted_ = true; |
| 90 MessageLoop::current()->PostDelayedTask( |
| 91 FROM_HERE, |
| 92 base::Bind( |
| 93 &SmoothScrollGestureController::TickActiveSmoothScrollGesture, |
| 94 weak_factory_.GetWeakPtr(), |
| 95 rwh), |
| 96 time_until_next_ideal_interval); |
| 97 return; |
| 98 } |
| 99 |
| 100 last_smooth_scroll_gesture_tick_time_ = now; |
| 101 |
| 102 |
| 103 if (!pending_smooth_scroll_gesture_->ForwardInputEvents(now, rwh)) { |
| 104 pending_smooth_scroll_gesture_ = NULL; |
| 105 rwh->Send(new ViewMsg_SmoothScrollCompleted(rwh->GetRoutingID())); |
| 106 } |
| 107 |
| 108 // No need to post the next tick if an input is in flight. |
| 109 if (pending_input_event_count_) |
| 110 return; |
| 111 |
| 112 TRACE_EVENT_INSTANT1("input", "PostTickTask", TRACE_EVENT_SCOPE_THREAD, |
| 113 "delay", preferred_interval.InMilliseconds()); |
| 114 tick_active_smooth_scroll_gesture_task_posted_ = true; |
| 115 MessageLoop::current()->PostDelayedTask( |
| 116 FROM_HERE, |
| 117 base::Bind(&SmoothScrollGestureController::TickActiveSmoothScrollGesture, |
| 118 weak_factory_.GetWeakPtr(), |
| 119 rwh), |
| 120 preferred_interval); |
| 121 } |
| 122 |
| 123 } // namespace content |
OLD | NEW |