Home | History | Annotate | Download | only in renderer_host
      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 
     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   // The result of |DispatchEvent()|, indicating either how the event was
     42   // handled, or how it should be handled by the caller.
     43   enum Disposition {
     44     CONSUMED,
     45     SHOULD_FORWARD_TO_RENDERER,
     46     SHOULD_FORWARD_TO_GESTURE_FILTER
     47   };
     48   // This must be called when dispatching any event from the
     49   // RenderWidgetHostView so that the state of the overscroll gesture can be
     50   // updated properly.
     51   Disposition DispatchEvent(const blink::WebInputEvent& event,
     52                             const ui::LatencyInfo& latency_info);
     53 
     54   // This must be called when the ACK for any event comes in. This updates the
     55   // overscroll gesture status as appropriate.
     56   void ReceivedEventACK(const blink::WebInputEvent& event, bool processed);
     57 
     58   // This must be called when a gesture event is filtered out and not sent to
     59   // the renderer.
     60   void DiscardingGestureEvent(const blink::WebGestureEvent& event);
     61 
     62   OverscrollMode overscroll_mode() const { return overscroll_mode_; }
     63 
     64   void set_delegate(OverscrollControllerDelegate* delegate) {
     65     delegate_ = delegate;
     66   }
     67 
     68   // Resets internal states.
     69   void Reset();
     70 
     71   // Cancels any in-progress overscroll (and calls OnOverscrollModeChange on the
     72   // delegate if necessary), and resets internal states.
     73   void Cancel();
     74 
     75  private:
     76   friend class MockRenderWidgetHost;
     77 
     78   // Different scrolling states.
     79   enum ScrollState {
     80     STATE_UNKNOWN,
     81     STATE_PENDING,
     82     STATE_CONTENT_SCROLLING,
     83     STATE_OVERSCROLLING,
     84   };
     85 
     86   // Returns true if the event indicates that the in-progress overscroll gesture
     87   // can now be completed.
     88   bool DispatchEventCompletesAction(
     89       const blink::WebInputEvent& event) const;
     90 
     91   // Returns true to indicate that dispatching the event should reset the
     92   // overscroll gesture status.
     93   bool DispatchEventResetsState(const blink::WebInputEvent& event) const;
     94 
     95   // Processes an event to update the internal state for overscroll. Returns
     96   // true if the state is updated, false otherwise.
     97   bool ProcessEventForOverscroll(const blink::WebInputEvent& event);
     98 
     99   // Processes horizontal overscroll. This can update both the overscroll mode
    100   // and the over scroll amount (i.e. |overscroll_mode_|, |overscroll_delta_x_|
    101   // and |overscroll_delta_y_|).
    102   void ProcessOverscroll(float delta_x,
    103                          float delta_y,
    104                          blink::WebInputEvent::Type event_type);
    105 
    106   // Completes the desired action from the current gesture.
    107   void CompleteAction();
    108 
    109   // Sets the overscroll mode (and triggers callback in the delegate when
    110   // appropriate).
    111   void SetOverscrollMode(OverscrollMode new_mode);
    112 
    113   // The current state of overscroll gesture.
    114   OverscrollMode overscroll_mode_;
    115 
    116   // Used to keep track of the scrolling state.
    117   // If scrolling starts, and some scroll events are consumed at the beginning
    118   // of the scroll (i.e. some content on the web-page was scrolled), then do not
    119   // process any of the subsequent scroll events for generating overscroll
    120   // gestures.
    121   ScrollState scroll_state_;
    122 
    123   // The amount of overscroll in progress. These values are invalid when
    124   // |overscroll_mode_| is set to OVERSCROLL_NONE.
    125   float overscroll_delta_x_;
    126   float overscroll_delta_y_;
    127 
    128   // The delegate that receives the overscroll updates. The delegate is not
    129   // owned by this controller.
    130   OverscrollControllerDelegate* delegate_;
    131 
    132   DISALLOW_COPY_AND_ASSIGN(OverscrollController);
    133 };
    134 
    135 }  // namespace content
    136 
    137 #endif  // CONTENT_BROWSER_RENDERER_HOST_OVERSCROLL_CONTROLLER_H_
    138