Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / input / synthetic_gesture_controller.cc
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 #include "content/browser/renderer_host/input/synthetic_gesture_controller.h"
6
7 #include "base/debug/trace_event.h"
8 #include "content/browser/renderer_host/input/synthetic_gesture_target.h"
9 #include "content/common/input/synthetic_smooth_scroll_gesture_params.h"
10 #include "content/common/input_messages.h"
11 #include "content/public/browser/render_widget_host.h"
12
13 namespace content {
14
15 SyntheticGestureController::SyntheticGestureController(
16     scoped_ptr<SyntheticGestureTarget> gesture_target)
17     : gesture_target_(gesture_target.Pass()) {}
18
19 SyntheticGestureController::~SyntheticGestureController() {}
20
21 void SyntheticGestureController::QueueSyntheticGesture(
22     scoped_ptr<SyntheticGesture> synthetic_gesture,
23     const OnGestureCompleteCallback& completion_callback) {
24   DCHECK(synthetic_gesture);
25
26   bool was_empty = pending_gesture_queue_.IsEmpty();
27
28   pending_gesture_queue_.Push(synthetic_gesture.Pass(), completion_callback);
29
30   if (was_empty)
31     StartGesture(*pending_gesture_queue_.FrontGesture());
32 }
33
34 void SyntheticGestureController::Flush(base::TimeTicks timestamp) {
35   TRACE_EVENT0("input", "SyntheticGestureController::Flush");
36   if (pending_gesture_queue_.IsEmpty())
37     return;
38
39   if (pending_gesture_result_)
40     return;
41
42   SyntheticGesture* gesture = pending_gesture_queue_.FrontGesture();
43   SyntheticGesture::Result result =
44       gesture->ForwardInputEvents(timestamp, gesture_target_.get());
45
46   if (result == SyntheticGesture::GESTURE_RUNNING) {
47     gesture_target_->SetNeedsFlush();
48     return;
49   }
50
51   // It's possible that all events generated by the gesture have been fully
52   // dispatched at this point, in which case |OnDidFlushInput()| was called
53   // before |pending_gesture_result_| was initialized. Requesting another flush
54   // will trigger the necessary gesture-ending call to |OnDidFlushInput()|.
55   pending_gesture_result_.reset(new SyntheticGesture::Result(result));
56   gesture_target_->SetNeedsFlush();
57 }
58
59 void SyntheticGestureController::OnDidFlushInput() {
60   if (!pending_gesture_result_)
61     return;
62
63   DCHECK(!pending_gesture_queue_.IsEmpty());
64   auto pending_gesture_result = pending_gesture_result_.Pass();
65   StopGesture(*pending_gesture_queue_.FrontGesture(),
66               pending_gesture_queue_.FrontCallback(),
67               *pending_gesture_result);
68   pending_gesture_queue_.Pop();
69
70   if (!pending_gesture_queue_.IsEmpty())
71     StartGesture(*pending_gesture_queue_.FrontGesture());
72 }
73
74 void SyntheticGestureController::StartGesture(const SyntheticGesture& gesture) {
75   TRACE_EVENT_ASYNC_BEGIN0("input,benchmark",
76                            "SyntheticGestureController::running",
77                            &gesture);
78   gesture_target_->SetNeedsFlush();
79 }
80
81 void SyntheticGestureController::StopGesture(
82     const SyntheticGesture& gesture,
83     const OnGestureCompleteCallback& completion_callback,
84     SyntheticGesture::Result result) {
85   DCHECK_NE(result, SyntheticGesture::GESTURE_RUNNING);
86   TRACE_EVENT_ASYNC_END0("input,benchmark",
87                          "SyntheticGestureController::running",
88                          &gesture);
89
90   completion_callback.Run(result);
91 }
92
93 SyntheticGestureController::GestureAndCallbackQueue::GestureAndCallbackQueue() {
94 }
95
96 SyntheticGestureController::GestureAndCallbackQueue::
97     ~GestureAndCallbackQueue() {
98 }
99
100 }  // namespace content