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 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 
     55   OverscrollMode overscroll_mode() const { return overscroll_mode_; }
     56 
     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_|).
     95   void ProcessOverscroll(float delta_x,
     96                          float delta_y,
     97                          blink::WebInputEvent::Type event_type);
     98 
     99   // Completes the desired action from the current gesture.
    100   void CompleteAction();
    101 
    102   // Sets the overscroll mode (and triggers callback in the delegate when
    103   // appropriate).
    104   void SetOverscrollMode(OverscrollMode new_mode);
    105 
    106   // The current state of overscroll gesture.
    107   OverscrollMode overscroll_mode_;
    108 
    109   // Used to keep track of the scrolling state.
    110   // If scrolling starts, and some scroll events are consumed at the beginning
    111   // of the scroll (i.e. some content on the web-page was scrolled), then do not
    112   // process any of the subsequent scroll events for generating overscroll
    113   // gestures.
    114   ScrollState scroll_state_;
    115 
    116   // The amount of overscroll in progress. These values are invalid when
    117   // |overscroll_mode_| is set to OVERSCROLL_NONE.
    118   float overscroll_delta_x_;
    119   float overscroll_delta_y_;
    120 
    121   // The delegate that receives the overscroll updates. The delegate is not
    122   // owned by this controller.
    123   OverscrollControllerDelegate* delegate_;
    124 
    125   DISALLOW_COPY_AND_ASSIGN(OverscrollController);
    126 };
    127 
    128 }  // namespace content
    129 
    130 #endif  // CONTENT_BROWSER_RENDERER_HOST_OVERSCROLL_CONTROLLER_H_
    131