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
3// found in the LICENSE file.
4
5#ifndef CONTENT_BROWSER_RENDERER_HOST_INPUT_SYNTHETIC_GESTURE_CONTROLLER_H_
6#define CONTENT_BROWSER_RENDERER_HOST_INPUT_SYNTHETIC_GESTURE_CONTROLLER_H_
7
8#include <queue>
9#include <utility>
10
11#include "base/callback.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/memory/scoped_vector.h"
14#include "base/time/time.h"
15#include "content/browser/renderer_host/input/synthetic_gesture.h"
16#include "content/common/content_export.h"
17#include "content/common/input/synthetic_gesture_params.h"
18
19namespace content {
20
21class SyntheticGestureTarget;
22
23// Controls a synthetic gesture.
24// Repeatedly invokes the gesture object's ForwardInputEvent method to send
25// input events to the platform until the gesture has finished.
26class CONTENT_EXPORT SyntheticGestureController {
27 public:
28  explicit SyntheticGestureController(
29      scoped_ptr<SyntheticGestureTarget> gesture_target);
30  virtual ~SyntheticGestureController();
31
32  typedef base::Callback<void(SyntheticGesture::Result)>
33      OnGestureCompleteCallback;
34  void QueueSyntheticGesture(
35      scoped_ptr<SyntheticGesture> synthetic_gesture,
36      const OnGestureCompleteCallback& completion_callback);
37
38  // Forward input events of the currently processed gesture.
39  void Flush(base::TimeTicks timestamp);
40
41  // To be called when all events generated from the current gesture have been
42  // fully flushed from the input pipeline (i.e., sent, processed and ack'ed).
43  void OnDidFlushInput();
44
45 private:
46  void StartGesture(const SyntheticGesture& gesture);
47  void StopGesture(const SyntheticGesture& gesture,
48                   const OnGestureCompleteCallback& completion_callback,
49                   SyntheticGesture::Result result);
50
51  scoped_ptr<SyntheticGestureTarget> gesture_target_;
52  scoped_ptr<SyntheticGesture::Result> pending_gesture_result_;
53
54  // A queue of gesture/callback pairs.  Implemented as two queues to
55  // simplify the ownership of SyntheticGesture pointers.
56  class GestureAndCallbackQueue {
57  public:
58    GestureAndCallbackQueue();
59    ~GestureAndCallbackQueue();
60    void Push(scoped_ptr<SyntheticGesture> gesture,
61        const OnGestureCompleteCallback& callback) {
62      gestures_.push_back(gesture.release());
63      callbacks_.push(callback);
64    }
65    void Pop() {
66      gestures_.erase(gestures_.begin());
67      callbacks_.pop();
68    }
69    SyntheticGesture* FrontGesture() {
70      return gestures_.front();
71    }
72    OnGestureCompleteCallback& FrontCallback() {
73      return callbacks_.front();
74    }
75    bool IsEmpty() {
76      CHECK(gestures_.empty() == callbacks_.empty());
77      return gestures_.empty();
78    }
79   private:
80    ScopedVector<SyntheticGesture> gestures_;
81    std::queue<OnGestureCompleteCallback> callbacks_;
82  } pending_gesture_queue_;
83
84  DISALLOW_COPY_AND_ASSIGN(SyntheticGestureController);
85};
86
87}  // namespace content
88
89#endif  // CONTENT_BROWSER_RENDERER_HOST_INPUT_SYNTHETIC_GESTURE_CONTROLLER_H_
90