177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt// Copyright 2013 The Chromium Authors. All rights reserved.
277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt// Use of this source code is governed by a BSD-style license that can be
377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt// found in the LICENSE file.
477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt#include "content/browser/renderer_host/input/synthetic_gesture_controller.h"
677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt#include "base/debug/trace_event.h"
877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt#include "content/browser/renderer_host/input/synthetic_gesture_target.h"
977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt#include "content/common/input/synthetic_smooth_scroll_gesture_params.h"
1077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt#include "content/common/input_messages.h"
1177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt#include "content/public/browser/render_widget_host.h"
1277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
1377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtnamespace content {
1477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
1577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric AnholtSyntheticGestureController::SyntheticGestureController(
1677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt    scoped_ptr<SyntheticGestureTarget> gesture_target)
1777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt    : gesture_target_(gesture_target.Pass()) {}
1877a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
1977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric AnholtSyntheticGestureController::~SyntheticGestureController() {}
2077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
2177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholtvoid SyntheticGestureController::QueueSyntheticGesture(
2277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt    scoped_ptr<SyntheticGesture> synthetic_gesture) {
2377a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt  DCHECK(synthetic_gesture);
2477a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
2577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt  pending_gesture_queue_.push_back(synthetic_gesture.release());
2677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
2777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt  // Start forwarding input events if the queue was previously empty.
28ecadb51bbcb972a79f3ed79e65a7986b9396e757Brian Paul  if (pending_gesture_queue_.size() == 1)
29ecadb51bbcb972a79f3ed79e65a7986b9396e757Brian Paul    StartGesture(*pending_gesture_queue_.front());
30ecadb51bbcb972a79f3ed79e65a7986b9396e757Brian Paul}
31ecadb51bbcb972a79f3ed79e65a7986b9396e757Brian Paul
322d99588b3556928a0879b4160210ac771dbf1f0bKristian Høgsbergvoid SyntheticGestureController::Flush(base::TimeTicks timestamp) {
33d7322c9d420e484bc3c7cecb873b04cf7da7f33aKristian Høgsberg  if (pending_gesture_queue_.empty())
346c244b0f326504ae6add1ddcb407e73c3e72da78Brian Paul    return;
3577a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt
3677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt  if (last_tick_time_.is_null()) {
3777a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt    last_tick_time_ = timestamp;
386c244b0f326504ae6add1ddcb407e73c3e72da78Brian Paul    gesture_target_->SetNeedsFlush();
3977a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt    return;
406c244b0f326504ae6add1ddcb407e73c3e72da78Brian Paul  }
416c244b0f326504ae6add1ddcb407e73c3e72da78Brian Paul
4277a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt  base::TimeDelta interval = timestamp - last_tick_time_;
436c244b0f326504ae6add1ddcb407e73c3e72da78Brian Paul  last_tick_time_ = timestamp;
446c244b0f326504ae6add1ddcb407e73c3e72da78Brian Paul  SyntheticGesture::Result result =
45c26247100bfd453a7ec013f630abe366c12fbd8bKristian Høgsberg      pending_gesture_queue_.front()->ForwardInputEvents(interval,
4677a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt                                                         gesture_target_.get());
47bea6b5fe5aa3138cec8d057766ae48da4aa57deeEric Anholt
486c244b0f326504ae6add1ddcb407e73c3e72da78Brian Paul  if (result == SyntheticGesture::GESTURE_RUNNING) {
491ba96651e12b3c74fb9c8f5a61b183ef36a27b1eEric Anholt    gesture_target_->SetNeedsFlush();
5077a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt    return;
5177a5bcaff43df8d54e0e0ef833726e4b41d7eb36Eric Anholt  }
52a0e453a5eca7ed4b57a7f4c1e418d368815e3957Eric Anholt
53a0e453a5eca7ed4b57a7f4c1e418d368815e3957Eric Anholt  StopGesture(*pending_gesture_queue_.front(), result);
54e9bf3e4cc9a7e4bcd4c45bd707541d26ecdf0409Jesse Barnes  pending_gesture_queue_.erase(pending_gesture_queue_.begin());
55fe91c05b5494b889c8adda77ff562712116d2e59Eric Anholt
56fe91c05b5494b889c8adda77ff562712116d2e59Eric Anholt  if (!pending_gesture_queue_.empty()) {
57fe91c05b5494b889c8adda77ff562712116d2e59Eric Anholt    StartGesture(*pending_gesture_queue_.front());
58f75843a517bd188639e6866db2a7b04de3524e16Dave Airlie  } else {
59fe91c05b5494b889c8adda77ff562712116d2e59Eric Anholt    // Reset last_tick_time_ so that we don't use an old value when a new
60fe91c05b5494b889c8adda77ff562712116d2e59Eric Anholt    // gestures is queued.
61fe91c05b5494b889c8adda77ff562712116d2e59Eric Anholt    last_tick_time_ = base::TimeTicks();
62fe91c05b5494b889c8adda77ff562712116d2e59Eric Anholt  }
63fe91c05b5494b889c8adda77ff562712116d2e59Eric Anholt}
641ba96651e12b3c74fb9c8f5a61b183ef36a27b1eEric Anholt
658e7a8d65931a650534e0f5c4e0d8118cd6f7636eEric Anholtvoid SyntheticGestureController::StartGesture(const SyntheticGesture& gesture) {
668e7a8d65931a650534e0f5c4e0d8118cd6f7636eEric Anholt  TRACE_EVENT_ASYNC_BEGIN0("benchmark", "SyntheticGestureController::running",
678e7a8d65931a650534e0f5c4e0d8118cd6f7636eEric Anholt                           &gesture);
681ba96651e12b3c74fb9c8f5a61b183ef36a27b1eEric Anholt  gesture_target_->SetNeedsFlush();
69b30dc2c66aeaad6661eef515a08a3da89aa07cb2Eric Anholt}
70b30dc2c66aeaad6661eef515a08a3da89aa07cb2Eric Anholt
71b30dc2c66aeaad6661eef515a08a3da89aa07cb2Eric Anholtvoid SyntheticGestureController::StopGesture(
72b30dc2c66aeaad6661eef515a08a3da89aa07cb2Eric Anholt    const SyntheticGesture& gesture, SyntheticGesture::Result result) {
73862a2a55b35d1dec9224b025a6e7a0cf8593a6a7Eric Anholt  DCHECK_NE(result, SyntheticGesture::GESTURE_RUNNING);
74862a2a55b35d1dec9224b025a6e7a0cf8593a6a7Eric Anholt  TRACE_EVENT_ASYNC_END0("benchmark", "SyntheticGestureController::running",
75862a2a55b35d1dec9224b025a6e7a0cf8593a6a7Eric Anholt                         &gesture);
76862a2a55b35d1dec9224b025a6e7a0cf8593a6a7Eric Anholt
77a0e453a5eca7ed4b57a7f4c1e418d368815e3957Eric Anholt  gesture_target_->OnSyntheticGestureCompleted(result);
78a0e453a5eca7ed4b57a7f4c1e418d368815e3957Eric Anholt}
79a0e453a5eca7ed4b57a7f4c1e418d368815e3957Eric Anholt
80acba9c1771d653126fd6f604cb80c050b9e8ffb3Michel Dänzer}  // namespace content
81a0e453a5eca7ed4b57a7f4c1e418d368815e3957Eric Anholt