Chromium Code Reviews| 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 DCHECK(pending_input_event_count_ > 0); | |
|
bulach
2013/02/22 16:55:14
I changed this to an "if" rather than a DCHECK.
th
| |
| 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 return; | |
| 75 } | |
| 76 | |
| 77 base::TimeTicks now = base::TimeTicks::HighResNow(); | |
| 78 base::TimeDelta preferred_interval = | |
| 79 base::TimeDelta::FromMilliseconds(kSyntheticScrollMessageIntervalMs); | |
| 80 base::TimeDelta time_until_next_ideal_interval = | |
| 81 (last_smooth_scroll_gesture_tick_time_ + preferred_interval) - | |
| 82 now; | |
| 83 if (time_until_next_ideal_interval.InMilliseconds() > 0) { | |
| 84 TRACE_EVENT_INSTANT1( | |
| 85 "input", "EarlyOut_TickedTooRecently", | |
| 86 "delay", time_until_next_ideal_interval.InMilliseconds()); | |
| 87 // Post a task. | |
| 88 tick_active_smooth_scroll_gesture_task_posted_ = true; | |
| 89 MessageLoop::current()->PostDelayedTask( | |
| 90 FROM_HERE, | |
| 91 base::Bind( | |
| 92 &SmoothScrollGestureController::TickActiveSmoothScrollGesture, | |
| 93 weak_factory_.GetWeakPtr(), | |
| 94 rwh), | |
| 95 time_until_next_ideal_interval); | |
| 96 return; | |
| 97 } | |
| 98 | |
| 99 last_smooth_scroll_gesture_tick_time_ = now; | |
| 100 | |
| 101 | |
| 102 if (!pending_smooth_scroll_gesture_->ForwardInputEvents(now, rwh)) { | |
| 103 pending_smooth_scroll_gesture_ = NULL; | |
| 104 rwh->Send(new ViewMsg_SmoothScrollCompleted(rwh->GetRoutingID())); | |
| 105 } | |
| 106 | |
| 107 // No need to post the next tick if an input is in flight. | |
| 108 if (pending_input_event_count_) | |
| 109 return; | |
| 110 | |
| 111 TRACE_EVENT_INSTANT1("input", "PostTickTask", | |
| 112 "delay", preferred_interval.InMilliseconds()); | |
| 113 tick_active_smooth_scroll_gesture_task_posted_ = true; | |
| 114 MessageLoop::current()->PostDelayedTask( | |
| 115 FROM_HERE, | |
| 116 base::Bind(&SmoothScrollGestureController::TickActiveSmoothScrollGesture, | |
| 117 weak_factory_.GetWeakPtr(), | |
| 118 rwh), | |
| 119 preferred_interval); | |
| 120 } | |
| 121 | |
| 122 } // namespace content | |
| OLD | NEW |