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 #ifndef CONTENT_BROWSER_RENDERER_HOST_INPUT_SYNTHETIC_GESTURE_CONTROLLER_H_ | 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_INPUT_SYNTHETIC_GESTURE_CONTROLLER_H_ |
6 #define CONTENT_BROWSER_RENDERER_HOST_INPUT_SYNTHETIC_GESTURE_CONTROLLER_H_ | 6 #define CONTENT_BROWSER_RENDERER_HOST_INPUT_SYNTHETIC_GESTURE_CONTROLLER_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <queue> | 9 #include <queue> |
10 #include <utility> | 10 #include <utility> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
17 #include "content/browser/renderer_host/input/synthetic_gesture.h" | 17 #include "content/browser/renderer_host/input/synthetic_gesture.h" |
18 #include "content/common/content_export.h" | 18 #include "content/common/content_export.h" |
19 #include "content/common/input/synthetic_gesture_params.h" | 19 #include "content/common/input/synthetic_gesture_params.h" |
20 | 20 |
21 namespace content { | 21 namespace content { |
22 | 22 |
23 class SyntheticGestureTarget; | 23 class SyntheticGestureTarget; |
24 | 24 |
25 // Controls a synthetic gesture. | 25 // Controls a synthetic gesture. |
26 // Repeatedly invokes the gesture object's ForwardInputEvent method to send | 26 // Repeatedly invokes the gesture object's ForwardInputEvent method to send |
27 // input events to the platform until the gesture has finished. | 27 // input events to the platform until the gesture has finished. |
28 class CONTENT_EXPORT SyntheticGestureController { | 28 class CONTENT_EXPORT SyntheticGestureController { |
29 public: | 29 public: |
30 using BeginFrameCallback = base::OnceClosure; | 30 class Delegate { |
31 using BeginFrameRequestCallback = | 31 public: |
32 base::RepeatingCallback<void(BeginFrameCallback)>; | 32 virtual ~Delegate() {} |
| 33 |
| 34 // Requests a single begin frame. The passed callback is run on the next |
| 35 // begin frame message. |
| 36 virtual void RequestBeginFrameForSynthesizedInput( |
| 37 base::OnceClosure callback) = 0; |
| 38 |
| 39 // Returns whether any gesture created by dispatched input events has |
| 40 // completed or not. |
| 41 virtual bool HasGestureStopped() = 0; |
| 42 }; |
33 SyntheticGestureController( | 43 SyntheticGestureController( |
34 std::unique_ptr<SyntheticGestureTarget> gesture_target, | 44 Delegate* delegate, |
35 BeginFrameRequestCallback begin_frame_callback); | 45 std::unique_ptr<SyntheticGestureTarget> gesture_target); |
36 virtual ~SyntheticGestureController(); | 46 virtual ~SyntheticGestureController(); |
37 | 47 |
38 typedef base::Callback<void(SyntheticGesture::Result)> | 48 typedef base::Callback<void(SyntheticGesture::Result)> |
39 OnGestureCompleteCallback; | 49 OnGestureCompleteCallback; |
40 void QueueSyntheticGesture( | 50 void QueueSyntheticGesture( |
41 std::unique_ptr<SyntheticGesture> synthetic_gesture, | 51 std::unique_ptr<SyntheticGesture> synthetic_gesture, |
42 const OnGestureCompleteCallback& completion_callback); | 52 const OnGestureCompleteCallback& completion_callback); |
43 | 53 |
44 bool DispatchNextEvent(base::TimeTicks = base::TimeTicks::Now()); | 54 bool DispatchNextEvent(base::TimeTicks = base::TimeTicks::Now()); |
45 | 55 |
46 private: | 56 private: |
| 57 void RequestBeginFrame(); |
47 void OnBeginFrame(); | 58 void OnBeginFrame(); |
48 | 59 |
49 void StartGesture(const SyntheticGesture& gesture); | 60 void StartGesture(const SyntheticGesture& gesture); |
50 void StopGesture(const SyntheticGesture& gesture, | 61 void StopGesture(const SyntheticGesture& gesture, |
51 const OnGestureCompleteCallback& completion_callback, | 62 const OnGestureCompleteCallback& completion_callback, |
52 SyntheticGesture::Result result); | 63 SyntheticGesture::Result result); |
53 | 64 |
| 65 Delegate* const delegate_; |
54 std::unique_ptr<SyntheticGestureTarget> gesture_target_; | 66 std::unique_ptr<SyntheticGestureTarget> gesture_target_; |
55 | 67 |
56 // A queue of gesture/callback pairs. Implemented as two queues to | 68 // A queue of gesture/callback pairs. Implemented as two queues to |
57 // simplify the ownership of SyntheticGesture pointers. | 69 // simplify the ownership of SyntheticGesture pointers. |
58 class GestureAndCallbackQueue { | 70 class GestureAndCallbackQueue { |
59 public: | 71 public: |
60 GestureAndCallbackQueue(); | 72 GestureAndCallbackQueue(); |
61 ~GestureAndCallbackQueue(); | 73 ~GestureAndCallbackQueue(); |
62 void Push(std::unique_ptr<SyntheticGesture> gesture, | 74 void Push(std::unique_ptr<SyntheticGesture> gesture, |
63 const OnGestureCompleteCallback& callback) { | 75 const OnGestureCompleteCallback& callback) { |
64 gestures_.push_back(std::move(gesture)); | 76 gestures_.push_back(std::move(gesture)); |
65 callbacks_.push(callback); | 77 callbacks_.push(callback); |
66 } | 78 } |
67 void Pop() { | 79 void Pop() { |
68 gestures_.erase(gestures_.begin()); | 80 gestures_.erase(gestures_.begin()); |
69 callbacks_.pop(); | 81 callbacks_.pop(); |
| 82 result_of_current_gesture_ = SyntheticGesture::GESTURE_RUNNING; |
70 } | 83 } |
71 SyntheticGesture* FrontGesture() { return gestures_.front().get(); } | 84 SyntheticGesture* FrontGesture() { return gestures_.front().get(); } |
72 OnGestureCompleteCallback& FrontCallback() { | 85 OnGestureCompleteCallback& FrontCallback() { |
73 return callbacks_.front(); | 86 return callbacks_.front(); |
74 } | 87 } |
75 bool IsEmpty() { | 88 bool IsEmpty() const { |
76 CHECK(gestures_.empty() == callbacks_.empty()); | 89 CHECK(gestures_.empty() == callbacks_.empty()); |
77 return gestures_.empty(); | 90 return gestures_.empty(); |
78 } | 91 } |
| 92 |
| 93 bool is_current_gesture_complete() const { |
| 94 return result_of_current_gesture_ != SyntheticGesture::GESTURE_RUNNING; |
| 95 } |
| 96 |
| 97 SyntheticGesture::Result current_gesture_result() const { |
| 98 return result_of_current_gesture_; |
| 99 } |
| 100 |
| 101 void mark_current_gesture_complete(SyntheticGesture::Result result) { |
| 102 result_of_current_gesture_ = result; |
| 103 } |
| 104 |
79 private: | 105 private: |
| 106 SyntheticGesture::Result result_of_current_gesture_ = |
| 107 SyntheticGesture::GESTURE_RUNNING; |
80 std::vector<std::unique_ptr<SyntheticGesture>> gestures_; | 108 std::vector<std::unique_ptr<SyntheticGesture>> gestures_; |
81 std::queue<OnGestureCompleteCallback> callbacks_; | 109 std::queue<OnGestureCompleteCallback> callbacks_; |
82 | 110 |
83 DISALLOW_COPY_AND_ASSIGN(GestureAndCallbackQueue); | 111 DISALLOW_COPY_AND_ASSIGN(GestureAndCallbackQueue); |
84 } pending_gesture_queue_; | 112 } pending_gesture_queue_; |
85 | 113 |
86 BeginFrameRequestCallback begin_frame_callback_; | |
87 | |
88 base::WeakPtrFactory<SyntheticGestureController> weak_ptr_factory_; | 114 base::WeakPtrFactory<SyntheticGestureController> weak_ptr_factory_; |
89 | 115 |
90 DISALLOW_COPY_AND_ASSIGN(SyntheticGestureController); | 116 DISALLOW_COPY_AND_ASSIGN(SyntheticGestureController); |
91 }; | 117 }; |
92 | 118 |
93 } // namespace content | 119 } // namespace content |
94 | 120 |
95 #endif // CONTENT_BROWSER_RENDERER_HOST_INPUT_SYNTHETIC_GESTURE_CONTROLLER_H_ | 121 #endif // CONTENT_BROWSER_RENDERER_HOST_INPUT_SYNTHETIC_GESTURE_CONTROLLER_H_ |
OLD | NEW |