- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / input / synthetic_gesture_controller_new.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_new.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 SyntheticGestureControllerNew::SyntheticGestureControllerNew(
16     scoped_ptr<SyntheticGestureTarget> gesture_target)
17     : gesture_target_(gesture_target.Pass()) {}
18
19 SyntheticGestureControllerNew::~SyntheticGestureControllerNew() {}
20
21 void SyntheticGestureControllerNew::QueueSyntheticGesture(
22     scoped_ptr<SyntheticGestureNew> synthetic_gesture) {
23   DCHECK(synthetic_gesture);
24
25   pending_gesture_queue_.push_back(synthetic_gesture.release());
26
27   // Start forwarding input events if the queue was previously empty.
28   if (pending_gesture_queue_.size() == 1) {
29     StartGesture(*pending_gesture_queue_.front());
30     last_tick_time_ = base::TimeTicks::Now();
31     timer_.Start(FROM_HERE,
32                  gesture_target_->GetSyntheticGestureUpdateRate(),
33                  this,
34                  &SyntheticGestureControllerNew::ForwardInputEvents);
35   }
36 }
37
38 void SyntheticGestureControllerNew::ForwardInputEvents() {
39   DCHECK(!pending_gesture_queue_.empty());
40   DCHECK(!last_tick_time_.is_null());
41
42   base::TimeTicks now = base::TimeTicks::Now();
43   base::TimeDelta interval = now - last_tick_time_;
44   last_tick_time_ = now;
45   SyntheticGestureNew::Result result =
46       pending_gesture_queue_.front()->ForwardInputEvents(interval,
47                                                          gesture_target_.get());
48
49   if (result != SyntheticGestureNew::GESTURE_RUNNING) {
50
51     StopGesture(*pending_gesture_queue_.front(), result);
52     pending_gesture_queue_.erase(pending_gesture_queue_.begin());
53
54     if (!pending_gesture_queue_.empty())
55       StartGesture(*pending_gesture_queue_.front());
56     else
57       timer_.Stop();
58   }
59 }
60
61 void SyntheticGestureControllerNew::StartGesture(
62     const SyntheticGestureNew& gesture) {
63   TRACE_EVENT_ASYNC_BEGIN0("benchmark", "SyntheticGestureController::running",
64                            &gesture);
65 }
66
67 void SyntheticGestureControllerNew::StopGesture(
68     const SyntheticGestureNew& gesture, SyntheticGestureNew::Result result) {
69   DCHECK_NE(result, SyntheticGestureNew::GESTURE_RUNNING);
70   TRACE_EVENT_ASYNC_END0("benchmark", "SyntheticGestureController::running",
71                          &gesture);
72
73   gesture_target_->OnSyntheticGestureCompleted(result);
74 }
75
76 }  // namespace content