- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / touch_smooth_scroll_gesture_aura.cc
1 // Copyright (c) 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/touch_smooth_scroll_gesture_aura.h"
6
7 #include "content/browser/renderer_host/render_widget_host_impl.h"
8 #include "ui/aura/root_window.h"
9 #include "ui/events/event.h"
10 #include "ui/events/event_utils.h"
11 #include "ui/gfx/transform.h"
12
13 namespace {
14
15 void InjectTouchEvent(const gfx::Point& location,
16                       ui::EventType type,
17                       aura::Window* window) {
18   gfx::Point screen_location = location;
19   aura::Window* root_window = window->GetRootWindow();
20   // First convert the location from Window to RootWindow.
21   aura::Window::ConvertPointToTarget(window, root_window, &screen_location);
22   // Then convert the location from RootWindow to screen.
23   aura::WindowEventDispatcher* dispatcher = root_window->GetDispatcher();
24   dispatcher->ConvertPointToHost(&screen_location);
25   ui::TouchEvent touch(type, screen_location, 0, 0, ui::EventTimeForNow(),
26                        1.0f, 1.0f, 1.0f, 1.0f);
27   dispatcher->AsRootWindowHostDelegate()->OnHostTouchEvent(&touch);
28 }
29
30 }  // namespace
31
32 namespace content {
33
34 TouchSmoothScrollGestureAura::TouchSmoothScrollGestureAura(bool scroll_down,
35                                                            int pixels_to_scroll,
36                                                            int mouse_event_x,
37                                                            int mouse_event_y,
38                                                            aura::Window* window)
39     : scroll_down_(scroll_down),
40       pixels_to_scroll_(pixels_to_scroll),
41       pixels_scrolled_(0),
42       location_(mouse_event_x, mouse_event_y),
43       window_(window) {
44 }
45
46 TouchSmoothScrollGestureAura::~TouchSmoothScrollGestureAura() {}
47
48 bool TouchSmoothScrollGestureAura::ForwardInputEvents(
49     base::TimeTicks now,
50     RenderWidgetHost* host) {
51   if (pixels_scrolled_ >= pixels_to_scroll_)
52     return false;
53
54   RenderWidgetHostImpl* host_impl = RenderWidgetHostImpl::From(host);
55   float position_delta = synthetic_gesture_calculator_.GetDelta(now,
56       host_impl->GetSyntheticGestureMessageInterval());
57
58   if (pixels_scrolled_ == 0) {
59     InjectTouchEvent(location_, ui::ET_TOUCH_PRESSED, window_);
60   }
61
62   location_.Offset(0, scroll_down_ ? -position_delta : position_delta);
63     InjectTouchEvent(location_, ui::ET_TOUCH_MOVED, window_);
64
65   pixels_scrolled_ += abs(position_delta);
66
67   if (pixels_scrolled_ >= pixels_to_scroll_) {
68     InjectTouchEvent(location_, ui::ET_TOUCH_RELEASED, window_);
69   }
70
71   return true;
72 }
73
74 }  // namespace content