#ifndef CONTENT_COMMON_INPUT_SYNTHETIC_GESTURE_CONTROLLER_H_
#define CONTENT_COMMON_INPUT_SYNTHETIC_GESTURE_CONTROLLER_H_
#include <memory>
#include <utility>
#include <vector>
#include "base/containers/queue.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "components/viz/common/frame_sinks/begin_frame_source.h"
#include "content/common/content_export.h"
#include "content/common/input/synthetic_gesture.h"
#include "content/common/input/synthetic_gesture_params.h"
namespace content {
class SyntheticGestureTarget;
class CONTENT_EXPORT SyntheticGestureController {
public:
class Delegate {
public:
virtual ~Delegate() {}
virtual bool HasGestureStopped() = 0;
virtual bool IsHidden() const = 0;
};
SyntheticGestureController(
Delegate* delegate,
std::unique_ptr<SyntheticGestureTarget> gesture_target,
scoped_refptr<base::SequencedTaskRunner> task_runner);
SyntheticGestureController(const SyntheticGestureController&) = delete;
SyntheticGestureController& operator=(const SyntheticGestureController&) =
delete;
virtual ~SyntheticGestureController();
typedef base::OnceCallback<void(SyntheticGesture::Result)>
OnGestureCompleteCallback;
void QueueSyntheticGesture(
std::unique_ptr<SyntheticGesture> synthetic_gesture,
OnGestureCompleteCallback completion_callback);
void QueueSyntheticGestureCompleteImmediately(
std::unique_ptr<SyntheticGesture> synthetic_gesture);
bool DispatchNextEvent(base::TimeTicks = base::TimeTicks::Now());
void EnsureRendererInitialized(base::OnceClosure on_completed);
bool IsHiddenAndNeedsVisible() const;
void StartIfNeeded();
base::WeakPtr<SyntheticGestureController> GetWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
void SetRendererInitializedForTesting(bool renderer_known_to_be_initialized) {
renderer_known_to_be_initialized_ = renderer_known_to_be_initialized;
}
private:
friend class SyntheticGestureControllerTestBase;
void QueueSyntheticGesture(
std::unique_ptr<SyntheticGesture> synthetic_gesture,
OnGestureCompleteCallback completion_callback,
bool complete_immediately);
void StartOrUpdateTimer();
void StartGesture();
void StopGesture(const SyntheticGesture& gesture,
SyntheticGesture::Result result,
bool complete_immediately);
void GestureCompleted(SyntheticGesture::Result result);
void ResolveCompletionCallback();
raw_ptr<Delegate> delegate_;
std::unique_ptr<SyntheticGestureTarget> gesture_target_;
class GestureAndCallbackQueue {
public:
GestureAndCallbackQueue();
GestureAndCallbackQueue(const GestureAndCallbackQueue&) = delete;
GestureAndCallbackQueue& operator=(const GestureAndCallbackQueue&) = delete;
~GestureAndCallbackQueue();
void Push(std::unique_ptr<SyntheticGesture> gesture,
OnGestureCompleteCallback callback,
bool complete_immediately) {
gestures_.push_back(std::move(gesture));
callbacks_.push(std::move(callback));
complete_immediately_.push(complete_immediately);
}
void Pop() {
gestures_.erase(gestures_.begin());
callbacks_.pop();
complete_immediately_.pop();
result_of_current_gesture_ = SyntheticGesture::GESTURE_RUNNING;
}
SyntheticGesture* FrontGesture() { return gestures_.front().get(); }
const SyntheticGesture* FrontGesture() const {
return gestures_.front().get();
}
OnGestureCompleteCallback FrontCallback() {
return std::move(callbacks_.front());
}
bool CompleteCurrentGestureImmediately() {
return complete_immediately_.front();
}
bool IsEmpty() const {
CHECK(gestures_.empty() == callbacks_.empty());
CHECK(gestures_.empty() == complete_immediately_.empty());
return gestures_.empty();
}
bool is_current_gesture_complete() const {
return result_of_current_gesture_ != SyntheticGesture::GESTURE_RUNNING;
}
SyntheticGesture::Result current_gesture_result() const {
return result_of_current_gesture_;
}
void mark_current_gesture_complete(SyntheticGesture::Result result) {
result_of_current_gesture_ = result;
}
private:
SyntheticGesture::Result result_of_current_gesture_ =
SyntheticGesture::GESTURE_RUNNING;
std::vector<std::unique_ptr<SyntheticGesture>> gestures_;
base::queue<OnGestureCompleteCallback> callbacks_;
base::queue<bool> complete_immediately_;
} pending_gesture_queue_;
bool renderer_known_to_be_initialized_ = false;
bool deferred_start_ = false;
base::TimeTicks event_timebase_;
base::TimeDelta event_interval_{base::Microseconds(8333)};
base::DeadlineTimer dispatch_timer_;
base::WeakPtrFactory<SyntheticGestureController> weak_ptr_factory_{this};
};
}
#endif