Home | History | Annotate | Download | only in input
      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_target_base.h"
      6 
      7 #include "content/browser/renderer_host/render_widget_host_impl.h"
      8 #include "content/browser/renderer_host/render_widget_host_view_base.h"
      9 #include "content/browser/renderer_host/ui_events_helper.h"
     10 #include "content/common/input_messages.h"
     11 #include "third_party/WebKit/public/web/WebInputEvent.h"
     12 #include "ui/events/event.h"
     13 #include "ui/events/latency_info.h"
     14 
     15 using blink::WebInputEvent;
     16 using blink::WebTouchEvent;
     17 using blink::WebTouchPoint;
     18 using blink::WebMouseEvent;
     19 using blink::WebMouseWheelEvent;
     20 
     21 namespace content {
     22 namespace {
     23 
     24 // This value was determined experimentally. It was sufficient to not cause a
     25 // fling on Android and Aura.
     26 const int kPointerAssumedStoppedTimeMs = 100;
     27 
     28 // SyntheticGestureTargetBase passes input events straight on to the renderer
     29 // without going through a gesture recognition framework. There is thus no touch
     30 // slop.
     31 const float kTouchSlopInDips = 0.0f;
     32 
     33 }  // namespace
     34 
     35 SyntheticGestureTargetBase::SyntheticGestureTargetBase(
     36     RenderWidgetHostImpl* host)
     37     : host_(host) {
     38   DCHECK(host);
     39 }
     40 
     41 SyntheticGestureTargetBase::~SyntheticGestureTargetBase() {
     42 }
     43 
     44 void SyntheticGestureTargetBase::DispatchInputEventToPlatform(
     45     const WebInputEvent& event) {
     46   TRACE_EVENT1("input",
     47                "SyntheticGestureTarget::DispatchInputEventToPlatform",
     48                "type", WebInputEventTraits::GetName(event.type));
     49 
     50   ui::LatencyInfo latency_info;
     51   latency_info.AddLatencyNumber(ui::INPUT_EVENT_LATENCY_UI_COMPONENT, 0, 0);
     52 
     53   if (WebInputEvent::isTouchEventType(event.type)) {
     54     const WebTouchEvent& web_touch =
     55         static_cast<const WebTouchEvent&>(event);
     56 
     57     // Check that all touch pointers are within the content bounds.
     58     if (web_touch.type == WebInputEvent::TouchStart) {
     59       for (unsigned i = 0; i < web_touch.touchesLength; i++)
     60         CHECK(web_touch.touches[i].state != WebTouchPoint::StatePressed ||
     61               PointIsWithinContents(web_touch.touches[i].position.x,
     62                                     web_touch.touches[i].position.y))
     63             << "Touch coordinates are not within content bounds on TouchStart.";
     64     }
     65 
     66     DispatchWebTouchEventToPlatform(web_touch, latency_info);
     67   } else if (event.type == WebInputEvent::MouseWheel) {
     68     const WebMouseWheelEvent& web_wheel =
     69         static_cast<const WebMouseWheelEvent&>(event);
     70     CHECK(PointIsWithinContents(web_wheel.x, web_wheel.y))
     71         << "Mouse wheel position is not within content bounds.";
     72     DispatchWebMouseWheelEventToPlatform(web_wheel, latency_info);
     73   } else if (WebInputEvent::isMouseEventType(event.type)) {
     74     const WebMouseEvent& web_mouse =
     75         static_cast<const WebMouseEvent&>(event);
     76     CHECK(event.type != WebInputEvent::MouseDown ||
     77           PointIsWithinContents(web_mouse.x, web_mouse.y))
     78         << "Mouse pointer is not within content bounds on MouseDown.";
     79     DispatchWebMouseEventToPlatform(web_mouse, latency_info);
     80   } else {
     81     NOTREACHED();
     82   }
     83 }
     84 
     85 void SyntheticGestureTargetBase::DispatchWebTouchEventToPlatform(
     86       const blink::WebTouchEvent& web_touch,
     87       const ui::LatencyInfo& latency_info) {
     88   // We assume that platforms supporting touch have their own implementation of
     89   // SyntheticGestureTarget to route the events through their respective input
     90   // stack.
     91   CHECK(false) << "Touch events not supported for this browser.";
     92 }
     93 
     94 void SyntheticGestureTargetBase::DispatchWebMouseWheelEventToPlatform(
     95       const blink::WebMouseWheelEvent& web_wheel,
     96       const ui::LatencyInfo& latency_info) {
     97   host_->ForwardWheelEventWithLatencyInfo(web_wheel, latency_info);
     98 }
     99 
    100 void SyntheticGestureTargetBase::DispatchWebMouseEventToPlatform(
    101       const blink::WebMouseEvent& web_mouse,
    102       const ui::LatencyInfo& latency_info) {
    103   host_->ForwardMouseEventWithLatencyInfo(web_mouse, latency_info);
    104 }
    105 
    106 void SyntheticGestureTargetBase::SetNeedsFlush() {
    107   host_->SetNeedsFlush();
    108 }
    109 
    110 SyntheticGestureParams::GestureSourceType
    111 SyntheticGestureTargetBase::GetDefaultSyntheticGestureSourceType() const {
    112   return SyntheticGestureParams::MOUSE_INPUT;
    113 }
    114 
    115 base::TimeDelta SyntheticGestureTargetBase::PointerAssumedStoppedTime()
    116     const {
    117   return base::TimeDelta::FromMilliseconds(kPointerAssumedStoppedTimeMs);
    118 }
    119 
    120 float SyntheticGestureTargetBase::GetTouchSlopInDips() const {
    121   return kTouchSlopInDips;
    122 }
    123 
    124 float SyntheticGestureTargetBase::GetMinScalingSpanInDips() const {
    125   // The minimum scaling distance is only relevant for touch gestures and the
    126   // base target doesn't support touch.
    127   NOTREACHED();
    128   return 0.0f;
    129 }
    130 
    131 bool SyntheticGestureTargetBase::PointIsWithinContents(int x, int y) const {
    132   gfx::Rect bounds = host_->GetView()->GetViewBounds();
    133   bounds -= bounds.OffsetFromOrigin();  // Translate the bounds to (0,0).
    134   return bounds.Contains(x, y);
    135 }
    136 
    137 }  // namespace content
    138