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 RenderWidgetHostViewAuraOverscrollTest; 19 class OverscrollControllerDelegate; 20 21 // Indicates the direction that the scroll is heading in relative to the screen, 22 // with the top being NORTH. 23 enum OverscrollMode { 24 OVERSCROLL_NONE, 25 OVERSCROLL_NORTH, 26 OVERSCROLL_SOUTH, 27 OVERSCROLL_WEST, 28 OVERSCROLL_EAST, 29 OVERSCROLL_COUNT 30 }; 31 32 // When a page is scrolled beyond the scrollable region, it will trigger an 33 // overscroll gesture. This controller receives the events that are dispatched 34 // to the renderer, and the ACKs of events, and updates the overscroll gesture 35 // status accordingly. 36 class OverscrollController { 37 public: 38 OverscrollController(); 39 virtual ~OverscrollController(); 40 41 // This must be called when dispatching any event from the 42 // RenderWidgetHostView so that the state of the overscroll gesture can be 43 // updated properly. Returns true if the event was handled, in which case 44 // further processing should cease. 45 bool WillHandleEvent(const blink::WebInputEvent& event); 46 47 // This must be called when the ACK for any event comes in. This updates the 48 // overscroll gesture status as appropriate. 49 void ReceivedEventACK(const blink::WebInputEvent& event, bool processed); 50 51 // This must be called when a gesture event is filtered out and not sent to 52 // the renderer. 53 void DiscardingGestureEvent(const blink::WebGestureEvent& event); 54 overscroll_mode()55 OverscrollMode overscroll_mode() const { return overscroll_mode_; } 56 set_delegate(OverscrollControllerDelegate * delegate)57 void set_delegate(OverscrollControllerDelegate* delegate) { 58 delegate_ = delegate; 59 } 60 61 // Resets internal states. 62 void Reset(); 63 64 // Cancels any in-progress overscroll (and calls OnOverscrollModeChange on the 65 // delegate if necessary), and resets internal states. 66 void Cancel(); 67 68 private: 69 friend class RenderWidgetHostViewAuraOverscrollTest; 70 71 // Different scrolling states. 72 enum ScrollState { 73 STATE_UNKNOWN, 74 STATE_PENDING, 75 STATE_CONTENT_SCROLLING, 76 STATE_OVERSCROLLING, 77 }; 78 79 // Returns true if the event indicates that the in-progress overscroll gesture 80 // can now be completed. 81 bool DispatchEventCompletesAction( 82 const blink::WebInputEvent& event) const; 83 84 // Returns true to indicate that dispatching the event should reset the 85 // overscroll gesture status. 86 bool DispatchEventResetsState(const blink::WebInputEvent& event) const; 87 88 // Processes an event to update the internal state for overscroll. Returns 89 // true if the state is updated, false otherwise. 90 bool ProcessEventForOverscroll(const blink::WebInputEvent& event); 91 92 // Processes horizontal overscroll. This can update both the overscroll mode 93 // and the over scroll amount (i.e. |overscroll_mode_|, |overscroll_delta_x_| 94 // and |overscroll_delta_y_|). Returns true if overscroll was handled by the 95 // delegate. 96 bool ProcessOverscroll(float delta_x, 97 float delta_y, 98 blink::WebInputEvent::Type event_type); 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 // The current state of overscroll gesture. 108 OverscrollMode overscroll_mode_; 109 110 // Used to keep track of the scrolling state. 111 // If scrolling starts, and some scroll events are consumed at the beginning 112 // of the scroll (i.e. some content on the web-page was scrolled), then do not 113 // process any of the subsequent scroll events for generating overscroll 114 // gestures. 115 ScrollState scroll_state_; 116 117 // The amount of overscroll in progress. These values are invalid when 118 // |overscroll_mode_| is set to OVERSCROLL_NONE. 119 float overscroll_delta_x_; 120 float overscroll_delta_y_; 121 122 // The delegate that receives the overscroll updates. The delegate is not 123 // owned by this controller. 124 OverscrollControllerDelegate* delegate_; 125 126 DISALLOW_COPY_AND_ASSIGN(OverscrollController); 127 }; 128 129 } // namespace content 130 131 #endif // CONTENT_BROWSER_RENDERER_HOST_OVERSCROLL_CONTROLLER_H_ 132