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_gestures_task_posted_(false) { | |
27 } | |
28 | |
29 SmoothScrollGestureController::~SmoothScrollGestureController() { | |
30 } | |
31 | |
32 void SmoothScrollGestureController::OnBeginSmoothScroll( | |
33 RenderWidgetHostViewPort* view, int gesture_id, | |
34 const ViewHostMsg_BeginSmoothScroll_Params& params) { | |
35 active_smooth_scroll_gestures_.insert( | |
36 std::make_pair(gesture_id, | |
37 view->CreateSmoothScrollGesture( | |
rjkroege
2013/02/11 22:47:27
I see that this makes wheel events. Note that whee
| |
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; | |
45 if (tick_active_smooth_scroll_gestures_task_posted_) | |
46 return; | |
47 TickActiveSmoothScrollGesture(view->GetRenderWidgetHost()); | |
48 } | |
49 | |
50 void SmoothScrollGestureController::OnForwardInputEvent() { | |
51 ++pending_input_event_count_; | |
52 } | |
53 | |
54 void SmoothScrollGestureController::OnInputEventACK(RenderWidgetHost* rwh) { | |
55 --pending_input_event_count_; | |
rjkroege
2013/02/11 22:47:27
should you verify that this has not gone negative?
| |
56 // If an input ack is pending, then hold off ticking the gesture | |
57 // until we get an input ack. | |
58 if (!pending_input_event_count_ && !active_smooth_scroll_gestures_.empty()) | |
59 TickActiveSmoothScrollGesture(rwh); | |
60 } | |
61 | |
62 int SmoothScrollGestureController::SyntheticScrollMessageInterval() const { | |
63 return kSyntheticScrollMessageIntervalMs; | |
64 } | |
65 | |
66 void SmoothScrollGestureController::TickActiveSmoothScrollGesture( | |
67 RenderWidgetHost* rwh) { | |
68 TRACE_EVENT0("input", "RenderWidgetHostImpl::TickActiveSmoothScrollGesture"); | |
69 tick_active_smooth_scroll_gestures_task_posted_ = false; | |
70 if (active_smooth_scroll_gestures_.empty()) { | |
71 TRACE_EVENT_INSTANT0("input", "EarlyOut_NoActiveScrollGesture"); | |
72 return; | |
73 } | |
74 | |
75 base::TimeTicks now = base::TimeTicks::HighResNow(); | |
76 base::TimeDelta preferred_interval = | |
77 base::TimeDelta::FromMilliseconds(kSyntheticScrollMessageIntervalMs); | |
78 base::TimeDelta time_until_next_ideal_interval = | |
79 (last_smooth_scroll_gestures_tick_time_ + preferred_interval) - | |
80 now; | |
81 if (time_until_next_ideal_interval.InMilliseconds() > 0) { | |
82 TRACE_EVENT_INSTANT1( | |
83 "input", "EarlyOut_TickedTooRecently", | |
84 "delay", time_until_next_ideal_interval.InMilliseconds()); | |
85 // Post a task. | |
86 tick_active_smooth_scroll_gestures_task_posted_ = true; | |
87 MessageLoop::current()->PostDelayedTask( | |
88 FROM_HERE, | |
89 base::Bind( | |
90 &SmoothScrollGestureController::TickActiveSmoothScrollGesture, | |
91 weak_factory_.GetWeakPtr(), | |
92 rwh), | |
93 time_until_next_ideal_interval); | |
94 return; | |
95 } | |
96 | |
97 last_smooth_scroll_gestures_tick_time_ = now; | |
98 | |
99 // Separate ticking of gestures from sending their completion messages. | |
100 std::vector<int> ids_that_are_done; | |
rjkroege
2013/02/11 22:47:27
possibly dumb question: why do you want to support
| |
101 for (SmoothScrollGestureMap::iterator it = | |
102 active_smooth_scroll_gestures_.begin(); | |
103 it != active_smooth_scroll_gestures_.end(); | |
104 ++it) { | |
105 | |
106 bool active = it->second->ForwardInputEvents(now, rwh); | |
107 if (!active) | |
108 ids_that_are_done.push_back(it->first); | |
109 } | |
110 | |
111 // Delete completed gestures and send their completion event. | |
112 for(size_t i = 0; i < ids_that_are_done.size(); i++) { | |
113 int id = ids_that_are_done[i]; | |
114 SmoothScrollGestureMap::iterator it = | |
115 active_smooth_scroll_gestures_.find(id); | |
116 DCHECK(it != active_smooth_scroll_gestures_.end()); | |
117 active_smooth_scroll_gestures_.erase(it); | |
118 | |
119 rwh->Send(new ViewMsg_SmoothScrollCompleted(rwh->GetRoutingID(), id)); | |
120 } | |
121 | |
122 // No need to post the next tick if an input is in flight. | |
123 if (pending_input_event_count_) | |
124 return; | |
125 | |
126 TRACE_EVENT_INSTANT1("input", "PostTickTask", | |
127 "delay", preferred_interval.InMilliseconds()); | |
128 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
| |
129 MessageLoop::current()->PostDelayedTask( | |
130 FROM_HERE, | |
131 base::Bind(&SmoothScrollGestureController::TickActiveSmoothScrollGesture, | |
132 weak_factory_.GetWeakPtr(), | |
133 rwh), | |
134 preferred_interval); | |
135 } | |
136 | |
137 } // namespace content | |
OLD | NEW |