1 // Copyright (c) 2012 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 #ifndef CONTENT_BROWSER_RENDERER_HOST_OVERSCROLL_CONTROLLER_H_ 6 #define CONTENT_BROWSER_RENDERER_HOST_OVERSCROLL_CONTROLLER_H_ 7 8 #include "base/basictypes.h" 9 #include "base/compiler_specific.h" 10 #include "third_party/WebKit/public/web/WebInputEvent.h" 11 12 namespace ui { 13 struct LatencyInfo; 14 } 15 16 namespace content { 17 18 class MockRenderWidgetHost; 19 class OverscrollControllerDelegate; 20 class RenderWidgetHostImpl; 21 22 // Indicates the direction that the scroll is heading in relative to the screen, 23 // with the top being NORTH. 24 enum OverscrollMode { 25 OVERSCROLL_NONE, 26 OVERSCROLL_NORTH, 27 OVERSCROLL_SOUTH, 28 OVERSCROLL_WEST, 29 OVERSCROLL_EAST, 30 OVERSCROLL_COUNT 31 }; 32 33 // When a page is scrolled beyond the scrollable region, it will trigger an 34 // overscroll gesture. This controller receives the events that are dispatched 35 // to the renderer, and the ACKs of events, and updates the overscroll gesture 36 // status accordingly. 37 class OverscrollController { 38 public: 39 // Creates an overscroll controller for the specified RenderWidgetHost. 40 // The RenderWidgetHost owns this overscroll controller. 41 explicit OverscrollController(RenderWidgetHostImpl* widget_host); 42 virtual ~OverscrollController(); 43 44 // This must be called when dispatching any event from the 45 // RenderWidgetHostView so that the state of the overscroll gesture can be 46 // updated properly. 47 // Returns true if the event should be dispatched, false otherwise. 48 bool WillDispatchEvent(const WebKit::WebInputEvent& event, 49 const ui::LatencyInfo& latency_info); 50 51 // This must be called when the ACK for any event comes in. This updates the 52 // overscroll gesture status as appropriate. 53 void ReceivedEventACK(const WebKit::WebInputEvent& event, bool processed); 54 55 // This must be called when a gesture event is filtered out and not sent to 56 // the renderer. 57 void DiscardingGestureEvent(const WebKit::WebGestureEvent& event); 58 59 OverscrollMode overscroll_mode() const { return overscroll_mode_; } 60 61 void set_delegate(OverscrollControllerDelegate* delegate) { 62 delegate_ = delegate; 63 } 64 65 // Resets internal states. 66 void Reset(); 67 68 // Cancels any in-progress overscroll (and calls OnOverscrollModeChange on the 69 // delegate if necessary), and resets internal states. 70 void Cancel(); 71 72 private: 73 friend class MockRenderWidgetHost; 74 75 // Different scrolling states. 76 enum ScrollState { 77 STATE_UNKNOWN, 78 STATE_PENDING, 79 STATE_CONTENT_SCROLLING, 80 STATE_OVERSCROLLING, 81 }; 82 83 // Returns true if the event indicates that the in-progress overscroll gesture 84 // can now be completed. 85 bool DispatchEventCompletesAction( 86 const WebKit::WebInputEvent& event) const; 87 88 // Returns true to indicate that dispatching the event should reset the 89 // overscroll gesture status. 90 bool DispatchEventResetsState(const WebKit::WebInputEvent& event) const; 91 92 // Processes an event and updates internal state for overscroll. 93 void ProcessEventForOverscroll(const WebKit::WebInputEvent& event); 94 95 // Processes horizontal overscroll. This can update both the overscroll mode 96 // and the over scroll amount (i.e. |overscroll_mode_|, |overscroll_delta_x_| 97 // and |overscroll_delta_y_|). 98 void ProcessOverscroll(float delta_x, float delta_y); 99 100 // Completes the desired action from the current gesture. 101 void CompleteAction(); 102 103 // Sets the overscroll mode (and triggers callback in the delegate when 104 // appropriate). 105 void SetOverscrollMode(OverscrollMode new_mode); 106 107 // Returns whether the input event should be forwarded to the 108 // RenderWidgetHost. 109 bool ShouldForwardToHost(const WebKit::WebInputEvent& event) const; 110 111 // The RenderWidgetHost that owns this overscroll controller. 112 RenderWidgetHostImpl* render_widget_host_; 113 114 // The current state of overscroll gesture. 115 OverscrollMode overscroll_mode_; 116 117 // Used to keep track of the scrolling state. 118 // If scrolling starts, and some scroll events are consumed at the beginning 119 // of the scroll (i.e. some content on the web-page was scrolled), then do not 120 // process any of the subsequent scroll events for generating overscroll 121 // gestures. 122 ScrollState scroll_state_; 123 124 // The amount of overscroll in progress. These values are invalid when 125 // |overscroll_mode_| is set to OVERSCROLL_NONE. 126 float overscroll_delta_x_; 127 float overscroll_delta_y_; 128 129 // The delegate that receives the overscroll updates. The delegate is not 130 // owned by this controller. 131 OverscrollControllerDelegate* delegate_; 132 133 DISALLOW_COPY_AND_ASSIGN(OverscrollController); 134 }; 135 136 } // namespace content 137 138 #endif // CONTENT_BROWSER_RENDERER_HOST_OVERSCROLL_CONTROLLER_H_ 139