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_aura.h"
      6 
      7 #include "content/browser/renderer_host/render_widget_host_impl.h"
      8 #include "content/browser/renderer_host/render_widget_host_view_aura.h"
      9 #include "content/browser/renderer_host/ui_events_helper.h"
     10 #include "ui/aura/window.h"
     11 #include "ui/aura/window_tree_host.h"
     12 #include "ui/events/event_processor.h"
     13 #include "ui/events/gestures/gesture_configuration.h"
     14 
     15 using blink::WebTouchEvent;
     16 using blink::WebMouseWheelEvent;
     17 
     18 namespace content {
     19 
     20 SyntheticGestureTargetAura::SyntheticGestureTargetAura(
     21     RenderWidgetHostImpl* host)
     22     : SyntheticGestureTargetBase(host) {
     23 }
     24 
     25 void SyntheticGestureTargetAura::DispatchWebTouchEventToPlatform(
     26     const WebTouchEvent& web_touch,
     27     const ui::LatencyInfo& latency_info) {
     28   TouchEventWithLatencyInfo touch_with_latency(web_touch, latency_info);
     29   ScopedVector<ui::TouchEvent> events;
     30   bool conversion_success = MakeUITouchEventsFromWebTouchEvents(
     31       touch_with_latency, &events, LOCAL_COORDINATES);
     32   DCHECK(conversion_success);
     33 
     34   aura::Window* window = GetWindow();
     35   aura::WindowTreeHost* host = window->GetHost();
     36   for (ScopedVector<ui::TouchEvent>::iterator iter = events.begin(),
     37       end = events.end(); iter != end; ++iter) {
     38     (*iter)->ConvertLocationToTarget(window, host->window());
     39     ui::EventDispatchDetails details =
     40         host->event_processor()->OnEventFromSource(*iter);
     41     if (details.dispatcher_destroyed)
     42       break;
     43   }
     44 }
     45 
     46 void SyntheticGestureTargetAura::DispatchWebMouseWheelEventToPlatform(
     47       const blink::WebMouseWheelEvent& web_wheel,
     48       const ui::LatencyInfo&) {
     49   gfx::Point location(web_wheel.x, web_wheel.y);
     50   ui::MouseEvent mouse_event(
     51       ui::ET_MOUSEWHEEL, location, location, ui::EF_NONE, ui::EF_NONE);
     52   ui::MouseWheelEvent wheel_event(
     53       mouse_event, web_wheel.deltaX, web_wheel.deltaY);
     54 
     55   aura::Window* window = GetWindow();
     56   wheel_event.ConvertLocationToTarget(window, window->GetRootWindow());
     57   ui::EventDispatchDetails details =
     58       window->GetHost()->event_processor()->OnEventFromSource(&wheel_event);
     59   if (details.dispatcher_destroyed)
     60     return;
     61 }
     62 
     63 namespace {
     64 
     65 ui::EventType
     66 WebMouseEventTypeToEventType(blink::WebInputEvent::Type web_type) {
     67   switch (web_type) {
     68     case blink::WebInputEvent::MouseDown:
     69       return ui::ET_MOUSE_PRESSED;
     70 
     71     case blink::WebInputEvent::MouseUp:
     72       return ui::ET_MOUSE_RELEASED;
     73 
     74     case blink::WebInputEvent::MouseMove:
     75       return ui::ET_MOUSE_MOVED;
     76 
     77     case blink::WebInputEvent::MouseEnter:
     78       return ui::ET_MOUSE_ENTERED;
     79 
     80     case blink::WebInputEvent::MouseLeave:
     81       return ui::ET_MOUSE_EXITED;
     82 
     83     case blink::WebInputEvent::ContextMenu:
     84       NOTREACHED() << "WebInputEvent::ContextMenu not supported by"
     85           "SyntheticGestureTargetAura";
     86 
     87     default:
     88       NOTREACHED();
     89   }
     90 
     91   return ui::ET_UNKNOWN;
     92 }
     93 
     94 int WebMouseEventButtonToFlags(blink::WebMouseEvent::Button button) {
     95   switch (button) {
     96     case blink::WebMouseEvent::ButtonLeft:
     97       return ui::EF_LEFT_MOUSE_BUTTON;
     98 
     99     case blink::WebMouseEvent::ButtonMiddle:
    100       return ui::EF_MIDDLE_MOUSE_BUTTON;
    101 
    102     case blink::WebMouseEvent::ButtonRight:
    103       return ui::EF_RIGHT_MOUSE_BUTTON;
    104 
    105     default:
    106       NOTREACHED();
    107   }
    108 
    109   return 0;
    110 }
    111 
    112 }  // namespace
    113 
    114 void SyntheticGestureTargetAura::DispatchWebMouseEventToPlatform(
    115       const blink::WebMouseEvent& web_mouse,
    116       const ui::LatencyInfo& latency_info) {
    117   gfx::Point location(web_mouse.x, web_mouse.y);
    118   ui::EventType event_type = WebMouseEventTypeToEventType(web_mouse.type);
    119   int flags = WebMouseEventButtonToFlags(web_mouse.button);
    120   ui::MouseEvent mouse_event(event_type, location, location, flags, flags);
    121 
    122   aura::Window* window = GetWindow();
    123   mouse_event.ConvertLocationToTarget(window, window->GetRootWindow());
    124   ui::EventDispatchDetails details =
    125       window->GetHost()->event_processor()->OnEventFromSource(&mouse_event);
    126   if (details.dispatcher_destroyed)
    127     return;
    128 }
    129 
    130 SyntheticGestureParams::GestureSourceType
    131 SyntheticGestureTargetAura::GetDefaultSyntheticGestureSourceType() const {
    132   return SyntheticGestureParams::TOUCH_INPUT;
    133 }
    134 
    135 float SyntheticGestureTargetAura::GetTouchSlopInDips() const {
    136   // - 1 because Aura considers a pointer to be moving if it has moved at least
    137   // 'max_touch_move_in_pixels_for_click' pixels.
    138   return ui::GestureConfiguration::max_touch_move_in_pixels_for_click() - 1;
    139 }
    140 
    141 float SyntheticGestureTargetAura::GetMinScalingSpanInDips() const {
    142   return ui::GestureConfiguration::min_distance_for_pinch_scroll_in_pixels();
    143 }
    144 
    145 aura::Window* SyntheticGestureTargetAura::GetWindow() const {
    146   aura::Window* window = render_widget_host()->GetView()->GetNativeView();
    147   DCHECK(window);
    148   return window;
    149 }
    150 
    151 }  // namespace content
    152