OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/renderer_host/input/synthetic_gesture_controller.h" | 5 #include "content/browser/renderer_host/input/synthetic_gesture_controller.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/threading/thread_task_runner_handle.h" | 9 #include "base/threading/thread_task_runner_handle.h" |
10 #include "base/trace_event/trace_event.h" | 10 #include "base/trace_event/trace_event.h" |
(...skipping 22 matching lines...) Expand all Loading... | |
33 | 33 |
34 bool was_empty = pending_gesture_queue_.IsEmpty(); | 34 bool was_empty = pending_gesture_queue_.IsEmpty(); |
35 | 35 |
36 pending_gesture_queue_.Push(std::move(synthetic_gesture), | 36 pending_gesture_queue_.Push(std::move(synthetic_gesture), |
37 completion_callback); | 37 completion_callback); |
38 | 38 |
39 if (was_empty) | 39 if (was_empty) |
40 StartGesture(*pending_gesture_queue_.FrontGesture()); | 40 StartGesture(*pending_gesture_queue_.FrontGesture()); |
41 } | 41 } |
42 | 42 |
43 void SyntheticGestureController::RequestBeginFrame() { | 43 void SyntheticGestureController::RequestBeginFrameIfNecessary() { |
44 if (dispatch_timer_.IsRunning()) | |
45 return; | |
44 delegate_->RequestBeginFrameForSynthesizedInput( | 46 delegate_->RequestBeginFrameForSynthesizedInput( |
45 base::BindOnce(&SyntheticGestureController::OnBeginFrame, | 47 base::BindOnce(&SyntheticGestureController::OnBeginFrame, |
46 weak_ptr_factory_.GetWeakPtr())); | 48 weak_ptr_factory_.GetWeakPtr())); |
47 } | 49 } |
48 | 50 |
49 void SyntheticGestureController::OnBeginFrame() { | 51 void SyntheticGestureController::OnBeginFrame() { |
50 // TODO(sad): Instead of dispatching the events immediately, dispatch after an | 52 constexpr base::TimeDelta kSynthesizedDispatchDelay = |
dtapuska
2017/05/19 18:32:27
I think we need an explanation around why 2 is a *
sadrul
2017/05/19 18:46:03
Added a note. PTAL.
| |
51 // offset. | 53 base::TimeDelta::FromMilliseconds(2); |
52 DispatchNextEvent(); | 54 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
55 FROM_HERE, | |
56 base::BindOnce(&SyntheticGestureController::StartTimer, | |
57 weak_ptr_factory_.GetWeakPtr()), | |
58 kSynthesizedDispatchDelay); | |
59 } | |
60 | |
61 void SyntheticGestureController::StartTimer() { | |
62 // TODO(sad): Change the interval to allow sending multiple events per begin | |
63 // frame. | |
64 dispatch_timer_.Start( | |
65 FROM_HERE, base::TimeDelta::FromMicroseconds(16666), | |
66 base::BindRepeating( | |
67 [](base::WeakPtr<SyntheticGestureController> weak_ptr) { | |
68 if (weak_ptr) | |
69 weak_ptr->DispatchNextEvent(base::TimeTicks::Now()); | |
70 }, | |
71 weak_ptr_factory_.GetWeakPtr())); | |
53 } | 72 } |
54 | 73 |
55 bool SyntheticGestureController::DispatchNextEvent(base::TimeTicks timestamp) { | 74 bool SyntheticGestureController::DispatchNextEvent(base::TimeTicks timestamp) { |
56 TRACE_EVENT0("input", "SyntheticGestureController::Flush"); | 75 TRACE_EVENT0("input", "SyntheticGestureController::Flush"); |
57 if (pending_gesture_queue_.IsEmpty()) | 76 if (pending_gesture_queue_.IsEmpty()) |
58 return false; | 77 return false; |
59 | 78 |
60 if (!pending_gesture_queue_.is_current_gesture_complete()) { | 79 if (!pending_gesture_queue_.is_current_gesture_complete()) { |
61 SyntheticGesture::Result result = | 80 SyntheticGesture::Result result = |
62 pending_gesture_queue_.FrontGesture()->ForwardInputEvents( | 81 pending_gesture_queue_.FrontGesture()->ForwardInputEvents( |
63 timestamp, gesture_target_.get()); | 82 timestamp, gesture_target_.get()); |
64 | 83 |
65 if (result == SyntheticGesture::GESTURE_RUNNING) { | 84 if (result == SyntheticGesture::GESTURE_RUNNING) { |
66 RequestBeginFrame(); | 85 RequestBeginFrameIfNecessary(); |
dtapuska
2017/05/19 18:32:27
Could this be a DCHECK(dispatch_timer_.IsRunning()
sadrul
2017/05/19 18:46:03
I had a DCHECK() at the beginning of the function
sadrul
2017/05/19 19:02:49
I have gone ahead and added the DCHECK() back, and
| |
67 return true; | 86 return true; |
68 } | 87 } |
69 pending_gesture_queue_.mark_current_gesture_complete(result); | 88 pending_gesture_queue_.mark_current_gesture_complete(result); |
70 } | 89 } |
71 | 90 |
72 if (!delegate_->HasGestureStopped()) { | 91 if (!delegate_->HasGestureStopped()) { |
73 RequestBeginFrame(); | 92 RequestBeginFrameIfNecessary(); |
dtapuska
2017/05/19 18:32:27
same here.
| |
74 return true; | 93 return true; |
75 } | 94 } |
76 | 95 |
77 StopGesture(*pending_gesture_queue_.FrontGesture(), | 96 StopGesture(*pending_gesture_queue_.FrontGesture(), |
78 pending_gesture_queue_.FrontCallback(), | 97 pending_gesture_queue_.FrontCallback(), |
79 pending_gesture_queue_.current_gesture_result()); | 98 pending_gesture_queue_.current_gesture_result()); |
80 pending_gesture_queue_.Pop(); | 99 pending_gesture_queue_.Pop(); |
81 if (pending_gesture_queue_.IsEmpty()) | 100 if (pending_gesture_queue_.IsEmpty()) { |
101 dispatch_timer_.Stop(); | |
82 return false; | 102 return false; |
103 } | |
83 StartGesture(*pending_gesture_queue_.FrontGesture()); | 104 StartGesture(*pending_gesture_queue_.FrontGesture()); |
84 return true; | 105 return true; |
85 } | 106 } |
86 | 107 |
87 void SyntheticGestureController::StartGesture(const SyntheticGesture& gesture) { | 108 void SyntheticGestureController::StartGesture(const SyntheticGesture& gesture) { |
88 TRACE_EVENT_ASYNC_BEGIN0("input,benchmark", | 109 TRACE_EVENT_ASYNC_BEGIN0("input,benchmark", |
89 "SyntheticGestureController::running", | 110 "SyntheticGestureController::running", |
90 &gesture); | 111 &gesture); |
91 RequestBeginFrame(); | 112 RequestBeginFrameIfNecessary(); |
92 } | 113 } |
93 | 114 |
94 void SyntheticGestureController::StopGesture( | 115 void SyntheticGestureController::StopGesture( |
95 const SyntheticGesture& gesture, | 116 const SyntheticGesture& gesture, |
96 const OnGestureCompleteCallback& completion_callback, | 117 const OnGestureCompleteCallback& completion_callback, |
97 SyntheticGesture::Result result) { | 118 SyntheticGesture::Result result) { |
98 DCHECK_NE(result, SyntheticGesture::GESTURE_RUNNING); | 119 DCHECK_NE(result, SyntheticGesture::GESTURE_RUNNING); |
99 TRACE_EVENT_ASYNC_END0("input,benchmark", | 120 TRACE_EVENT_ASYNC_END0("input,benchmark", |
100 "SyntheticGestureController::running", | 121 "SyntheticGestureController::running", |
101 &gesture); | 122 &gesture); |
102 | 123 |
103 completion_callback.Run(result); | 124 completion_callback.Run(result); |
104 } | 125 } |
105 | 126 |
106 SyntheticGestureController::GestureAndCallbackQueue::GestureAndCallbackQueue() { | 127 SyntheticGestureController::GestureAndCallbackQueue::GestureAndCallbackQueue() { |
107 } | 128 } |
108 | 129 |
109 SyntheticGestureController::GestureAndCallbackQueue:: | 130 SyntheticGestureController::GestureAndCallbackQueue:: |
110 ~GestureAndCallbackQueue() { | 131 ~GestureAndCallbackQueue() { |
111 } | 132 } |
112 | 133 |
113 } // namespace content | 134 } // namespace content |
OLD | NEW |