| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/renderer_host/overscroll_controller.h" | 5 #include "content/browser/renderer_host/overscroll_controller.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "content/browser/renderer_host/overscroll_controller_delegate.h" | 9 #include "content/browser/renderer_host/overscroll_controller_delegate.h" |
| 10 #include "content/public/browser/overscroll_configuration.h" | 10 #include "content/public/browser/overscroll_configuration.h" |
| 11 #include "content/public/common/content_switches.h" | 11 #include "content/public/common/content_switches.h" |
| 12 #include "third_party/WebKit/public/platform/WebMouseWheelEvent.h" | 12 #include "third_party/WebKit/public/platform/WebMouseWheelEvent.h" |
| 13 | 13 |
| 14 using blink::WebInputEvent; | 14 using blink::WebInputEvent; |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 bool IsScrollEndEffectEnabled() { | 18 bool IsScrollEndEffectEnabled() { |
| 19 return base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 19 return base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 20 switches::kScrollEndEffect) == "1"; | 20 switches::kScrollEndEffect) == "1"; |
| 21 } | 21 } |
| 22 | 22 |
| 23 bool IsGestureEventFromTouchpad(const blink::WebInputEvent& event) { | 23 bool IsGestureEventFromTouchpad(const blink::WebInputEvent& event) { |
| 24 DCHECK(blink::WebInputEvent::isGestureEventType(event.type)); | 24 DCHECK(blink::WebInputEvent::isGestureEventType(event.type())); |
| 25 const blink::WebGestureEvent& gesture = | 25 const blink::WebGestureEvent& gesture = |
| 26 static_cast<const blink::WebGestureEvent&>(event); | 26 static_cast<const blink::WebGestureEvent&>(event); |
| 27 return gesture.sourceDevice == blink::WebGestureDeviceTouchpad; | 27 return gesture.sourceDevice == blink::WebGestureDeviceTouchpad; |
| 28 } | 28 } |
| 29 | 29 |
| 30 } // namespace | 30 } // namespace |
| 31 | 31 |
| 32 namespace content { | 32 namespace content { |
| 33 | 33 |
| 34 OverscrollController::OverscrollController() | 34 OverscrollController::OverscrollController() |
| 35 : overscroll_mode_(OVERSCROLL_NONE), | 35 : overscroll_mode_(OVERSCROLL_NONE), |
| 36 scroll_state_(STATE_UNKNOWN), | 36 scroll_state_(STATE_UNKNOWN), |
| 37 overscroll_delta_x_(0.f), | 37 overscroll_delta_x_(0.f), |
| 38 overscroll_delta_y_(0.f), | 38 overscroll_delta_y_(0.f), |
| 39 delegate_(NULL) {} | 39 delegate_(NULL) {} |
| 40 | 40 |
| 41 OverscrollController::~OverscrollController() { | 41 OverscrollController::~OverscrollController() { |
| 42 } | 42 } |
| 43 | 43 |
| 44 bool OverscrollController::ShouldProcessEvent( | 44 bool OverscrollController::ShouldProcessEvent( |
| 45 const blink::WebInputEvent& event) { | 45 const blink::WebInputEvent& event) { |
| 46 switch (event.type) { | 46 switch (event.type()) { |
| 47 case blink::WebInputEvent::MouseWheel: | 47 case blink::WebInputEvent::MouseWheel: |
| 48 return false; | 48 return false; |
| 49 case blink::WebInputEvent::GestureScrollBegin: | 49 case blink::WebInputEvent::GestureScrollBegin: |
| 50 case blink::WebInputEvent::GestureScrollUpdate: | 50 case blink::WebInputEvent::GestureScrollUpdate: |
| 51 case blink::WebInputEvent::GestureScrollEnd: { | 51 case blink::WebInputEvent::GestureScrollEnd: { |
| 52 const blink::WebGestureEvent& gesture = | 52 const blink::WebGestureEvent& gesture = |
| 53 static_cast<const blink::WebGestureEvent&>(event); | 53 static_cast<const blink::WebGestureEvent&>(event); |
| 54 blink::WebGestureEvent::ScrollUnits scrollUnits; | 54 blink::WebGestureEvent::ScrollUnits scrollUnits; |
| 55 switch (event.type) { | 55 switch (event.type()) { |
| 56 case blink::WebInputEvent::GestureScrollBegin: | 56 case blink::WebInputEvent::GestureScrollBegin: |
| 57 scrollUnits = gesture.data.scrollBegin.deltaHintUnits; | 57 scrollUnits = gesture.data.scrollBegin.deltaHintUnits; |
| 58 break; | 58 break; |
| 59 case blink::WebInputEvent::GestureScrollUpdate: | 59 case blink::WebInputEvent::GestureScrollUpdate: |
| 60 scrollUnits = gesture.data.scrollUpdate.deltaUnits; | 60 scrollUnits = gesture.data.scrollUpdate.deltaUnits; |
| 61 break; | 61 break; |
| 62 case blink::WebInputEvent::GestureScrollEnd: | 62 case blink::WebInputEvent::GestureScrollEnd: |
| 63 scrollUnits = gesture.data.scrollEnd.deltaUnits; | 63 scrollUnits = gesture.data.scrollEnd.deltaUnits; |
| 64 break; | 64 break; |
| 65 default: | 65 default: |
| 66 scrollUnits = blink::WebGestureEvent::Pixels; | 66 scrollUnits = blink::WebGestureEvent::Pixels; |
| 67 break; | 67 break; |
| 68 } | 68 } |
| 69 | 69 |
| 70 return scrollUnits == blink::WebGestureEvent::PrecisePixels; | 70 return scrollUnits == blink::WebGestureEvent::PrecisePixels; |
| 71 } | |
| 72 default: | |
| 73 break; | |
| 74 } | 71 } |
| 72 default: |
| 73 break; |
| 74 } |
| 75 return true; | 75 return true; |
| 76 } | 76 } |
| 77 | 77 |
| 78 bool OverscrollController::WillHandleEvent(const blink::WebInputEvent& event) { | 78 bool OverscrollController::WillHandleEvent(const blink::WebInputEvent& event) { |
| 79 if (!ShouldProcessEvent(event)) | 79 if (!ShouldProcessEvent(event)) |
| 80 return false; | 80 return false; |
| 81 | 81 |
| 82 bool reset_scroll_state = false; | 82 bool reset_scroll_state = false; |
| 83 if (scroll_state_ != STATE_UNKNOWN || | 83 if (scroll_state_ != STATE_UNKNOWN || |
| 84 overscroll_delta_x_ || overscroll_delta_y_) { | 84 overscroll_delta_x_ || overscroll_delta_y_) { |
| 85 switch (event.type) { | 85 switch (event.type()) { |
| 86 case blink::WebInputEvent::GestureScrollEnd: | 86 case blink::WebInputEvent::GestureScrollEnd: |
| 87 // Avoid resetting the state on GestureScrollEnd generated | 87 // Avoid resetting the state on GestureScrollEnd generated |
| 88 // from the touchpad since it is sent based on a timeout. | 88 // from the touchpad since it is sent based on a timeout. |
| 89 reset_scroll_state = !IsGestureEventFromTouchpad(event); | 89 reset_scroll_state = !IsGestureEventFromTouchpad(event); |
| 90 break; | 90 break; |
| 91 | 91 |
| 92 case blink::WebInputEvent::GestureFlingStart: | 92 case blink::WebInputEvent::GestureFlingStart: |
| 93 reset_scroll_state = true; | 93 reset_scroll_state = true; |
| 94 break; | 94 break; |
| 95 | 95 |
| 96 case blink::WebInputEvent::MouseWheel: { | 96 case blink::WebInputEvent::MouseWheel: { |
| 97 const blink::WebMouseWheelEvent& wheel = | 97 const blink::WebMouseWheelEvent& wheel = |
| 98 static_cast<const blink::WebMouseWheelEvent&>(event); | 98 static_cast<const blink::WebMouseWheelEvent&>(event); |
| 99 if (!wheel.hasPreciseScrollingDeltas || | 99 if (!wheel.hasPreciseScrollingDeltas || |
| 100 wheel.phase == blink::WebMouseWheelEvent::PhaseEnded || | 100 wheel.phase == blink::WebMouseWheelEvent::PhaseEnded || |
| 101 wheel.phase == blink::WebMouseWheelEvent::PhaseCancelled) { | 101 wheel.phase == blink::WebMouseWheelEvent::PhaseCancelled) { |
| 102 reset_scroll_state = true; | 102 reset_scroll_state = true; |
| 103 } | 103 } |
| 104 break; | 104 break; |
| 105 } | 105 } |
| 106 | 106 |
| 107 default: | 107 default: |
| 108 if (blink::WebInputEvent::isMouseEventType(event.type) || | 108 if (blink::WebInputEvent::isMouseEventType(event.type()) || |
| 109 blink::WebInputEvent::isKeyboardEventType(event.type)) { | 109 blink::WebInputEvent::isKeyboardEventType(event.type())) { |
| 110 reset_scroll_state = true; | 110 reset_scroll_state = true; |
| 111 } | 111 } |
| 112 break; | 112 break; |
| 113 } | 113 } |
| 114 } | 114 } |
| 115 | 115 |
| 116 if (reset_scroll_state) | 116 if (reset_scroll_state) |
| 117 scroll_state_ = STATE_UNKNOWN; | 117 scroll_state_ = STATE_UNKNOWN; |
| 118 | 118 |
| 119 if (DispatchEventCompletesAction(event)) { | 119 if (DispatchEventCompletesAction(event)) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 145 void OverscrollController::ReceivedEventACK(const blink::WebInputEvent& event, | 145 void OverscrollController::ReceivedEventACK(const blink::WebInputEvent& event, |
| 146 bool processed) { | 146 bool processed) { |
| 147 if (!ShouldProcessEvent(event)) | 147 if (!ShouldProcessEvent(event)) |
| 148 return; | 148 return; |
| 149 | 149 |
| 150 if (processed) { | 150 if (processed) { |
| 151 // If a scroll event is consumed by the page, i.e. some content on the page | 151 // If a scroll event is consumed by the page, i.e. some content on the page |
| 152 // has been scrolled, then there is not going to be an overscroll gesture, | 152 // has been scrolled, then there is not going to be an overscroll gesture, |
| 153 // until the current scroll ends, and a new scroll gesture starts. | 153 // until the current scroll ends, and a new scroll gesture starts. |
| 154 if (scroll_state_ == STATE_UNKNOWN && | 154 if (scroll_state_ == STATE_UNKNOWN && |
| 155 (event.type == blink::WebInputEvent::MouseWheel || | 155 (event.type() == blink::WebInputEvent::MouseWheel || |
| 156 event.type == blink::WebInputEvent::GestureScrollUpdate)) { | 156 event.type() == blink::WebInputEvent::GestureScrollUpdate)) { |
| 157 scroll_state_ = STATE_CONTENT_SCROLLING; | 157 scroll_state_ = STATE_CONTENT_SCROLLING; |
| 158 } | 158 } |
| 159 return; | 159 return; |
| 160 } | 160 } |
| 161 ProcessEventForOverscroll(event); | 161 ProcessEventForOverscroll(event); |
| 162 } | 162 } |
| 163 | 163 |
| 164 void OverscrollController::DiscardingGestureEvent( | 164 void OverscrollController::DiscardingGestureEvent( |
| 165 const blink::WebGestureEvent& gesture) { | 165 const blink::WebGestureEvent& gesture) { |
| 166 if (scroll_state_ != STATE_UNKNOWN && | 166 if (scroll_state_ != STATE_UNKNOWN && |
| 167 (gesture.type == blink::WebInputEvent::GestureScrollEnd || | 167 (gesture.type() == blink::WebInputEvent::GestureScrollEnd || |
| 168 gesture.type == blink::WebInputEvent::GestureFlingStart)) { | 168 gesture.type() == blink::WebInputEvent::GestureFlingStart)) { |
| 169 scroll_state_ = STATE_UNKNOWN; | 169 scroll_state_ = STATE_UNKNOWN; |
| 170 } | 170 } |
| 171 } | 171 } |
| 172 | 172 |
| 173 void OverscrollController::Reset() { | 173 void OverscrollController::Reset() { |
| 174 overscroll_mode_ = OVERSCROLL_NONE; | 174 overscroll_mode_ = OVERSCROLL_NONE; |
| 175 overscroll_delta_x_ = overscroll_delta_y_ = 0.f; | 175 overscroll_delta_x_ = overscroll_delta_y_ = 0.f; |
| 176 scroll_state_ = STATE_UNKNOWN; | 176 scroll_state_ = STATE_UNKNOWN; |
| 177 } | 177 } |
| 178 | 178 |
| 179 void OverscrollController::Cancel() { | 179 void OverscrollController::Cancel() { |
| 180 SetOverscrollMode(OVERSCROLL_NONE); | 180 SetOverscrollMode(OVERSCROLL_NONE); |
| 181 overscroll_delta_x_ = overscroll_delta_y_ = 0.f; | 181 overscroll_delta_x_ = overscroll_delta_y_ = 0.f; |
| 182 scroll_state_ = STATE_UNKNOWN; | 182 scroll_state_ = STATE_UNKNOWN; |
| 183 } | 183 } |
| 184 | 184 |
| 185 bool OverscrollController::DispatchEventCompletesAction ( | 185 bool OverscrollController::DispatchEventCompletesAction ( |
| 186 const blink::WebInputEvent& event) const { | 186 const blink::WebInputEvent& event) const { |
| 187 if (overscroll_mode_ == OVERSCROLL_NONE) | 187 if (overscroll_mode_ == OVERSCROLL_NONE) |
| 188 return false; | 188 return false; |
| 189 | 189 |
| 190 // Complete the overscroll gesture if there was a mouse move or a scroll-end | 190 // Complete the overscroll gesture if there was a mouse move or a scroll-end |
| 191 // after the threshold. | 191 // after the threshold. |
| 192 if (event.type != blink::WebInputEvent::MouseMove && | 192 if (event.type() != blink::WebInputEvent::MouseMove && |
| 193 event.type != blink::WebInputEvent::GestureScrollEnd && | 193 event.type() != blink::WebInputEvent::GestureScrollEnd && |
| 194 event.type != blink::WebInputEvent::GestureFlingStart) | 194 event.type() != blink::WebInputEvent::GestureFlingStart) |
| 195 return false; | 195 return false; |
| 196 | 196 |
| 197 // Avoid completing the action on GestureScrollEnd generated | 197 // Avoid completing the action on GestureScrollEnd generated |
| 198 // from the touchpad since it is sent based on a timeout not | 198 // from the touchpad since it is sent based on a timeout not |
| 199 // when the user has stopped interacting. | 199 // when the user has stopped interacting. |
| 200 if (event.type == blink::WebInputEvent::GestureScrollEnd && | 200 if (event.type() == blink::WebInputEvent::GestureScrollEnd && |
| 201 IsGestureEventFromTouchpad(event)) | 201 IsGestureEventFromTouchpad(event)) |
| 202 return false; | 202 return false; |
| 203 | 203 |
| 204 if (!delegate_) | 204 if (!delegate_) |
| 205 return false; | 205 return false; |
| 206 | 206 |
| 207 gfx::Rect bounds = delegate_->GetVisibleBounds(); | 207 gfx::Rect bounds = delegate_->GetVisibleBounds(); |
| 208 if (bounds.IsEmpty()) | 208 if (bounds.IsEmpty()) |
| 209 return false; | 209 return false; |
| 210 | 210 |
| 211 if (event.type == blink::WebInputEvent::GestureFlingStart) { | 211 if (event.type() == blink::WebInputEvent::GestureFlingStart) { |
| 212 // Check to see if the fling is in the same direction of the overscroll. | 212 // Check to see if the fling is in the same direction of the overscroll. |
| 213 const blink::WebGestureEvent gesture = | 213 const blink::WebGestureEvent gesture = |
| 214 static_cast<const blink::WebGestureEvent&>(event); | 214 static_cast<const blink::WebGestureEvent&>(event); |
| 215 switch (overscroll_mode_) { | 215 switch (overscroll_mode_) { |
| 216 case OVERSCROLL_EAST: | 216 case OVERSCROLL_EAST: |
| 217 if (gesture.data.flingStart.velocityX < 0) | 217 if (gesture.data.flingStart.velocityX < 0) |
| 218 return false; | 218 return false; |
| 219 break; | 219 break; |
| 220 case OVERSCROLL_WEST: | 220 case OVERSCROLL_WEST: |
| 221 if (gesture.data.flingStart.velocityX > 0) | 221 if (gesture.data.flingStart.velocityX > 0) |
| (...skipping 20 matching lines...) Expand all Loading... |
| 242 } else { | 242 } else { |
| 243 ratio = fabs(overscroll_delta_y_) / bounds.height(); | 243 ratio = fabs(overscroll_delta_y_) / bounds.height(); |
| 244 threshold = GetOverscrollConfig(OVERSCROLL_CONFIG_VERT_THRESHOLD_COMPLETE); | 244 threshold = GetOverscrollConfig(OVERSCROLL_CONFIG_VERT_THRESHOLD_COMPLETE); |
| 245 } | 245 } |
| 246 | 246 |
| 247 return ratio >= threshold; | 247 return ratio >= threshold; |
| 248 } | 248 } |
| 249 | 249 |
| 250 bool OverscrollController::DispatchEventResetsState( | 250 bool OverscrollController::DispatchEventResetsState( |
| 251 const blink::WebInputEvent& event) const { | 251 const blink::WebInputEvent& event) const { |
| 252 switch (event.type) { | 252 switch (event.type()) { |
| 253 case blink::WebInputEvent::MouseWheel: { | 253 case blink::WebInputEvent::MouseWheel: { |
| 254 // Only wheel events with precise deltas (i.e. from trackpad) contribute | 254 // Only wheel events with precise deltas (i.e. from trackpad) contribute |
| 255 // to the overscroll gesture. | 255 // to the overscroll gesture. |
| 256 const blink::WebMouseWheelEvent& wheel = | 256 const blink::WebMouseWheelEvent& wheel = |
| 257 static_cast<const blink::WebMouseWheelEvent&>(event); | 257 static_cast<const blink::WebMouseWheelEvent&>(event); |
| 258 return !wheel.hasPreciseScrollingDeltas; | 258 return !wheel.hasPreciseScrollingDeltas; |
| 259 } | 259 } |
| 260 | 260 |
| 261 // Avoid resetting overscroll on GestureScrollBegin/End generated | 261 // Avoid resetting overscroll on GestureScrollBegin/End generated |
| 262 // from the touchpad since it is sent based on a timeout. | 262 // from the touchpad since it is sent based on a timeout. |
| 263 case blink::WebInputEvent::GestureScrollBegin: | 263 case blink::WebInputEvent::GestureScrollBegin: |
| 264 case blink::WebInputEvent::GestureScrollEnd: | 264 case blink::WebInputEvent::GestureScrollEnd: |
| 265 return !IsGestureEventFromTouchpad(event); | 265 return !IsGestureEventFromTouchpad(event); |
| 266 | 266 |
| 267 case blink::WebInputEvent::GestureScrollUpdate: | 267 case blink::WebInputEvent::GestureScrollUpdate: |
| 268 case blink::WebInputEvent::GestureFlingCancel: | 268 case blink::WebInputEvent::GestureFlingCancel: |
| 269 return false; | 269 return false; |
| 270 | 270 |
| 271 default: | 271 default: |
| 272 // Touch events can arrive during an overscroll gesture initiated by | 272 // Touch events can arrive during an overscroll gesture initiated by |
| 273 // touch-scrolling. These events should not reset the overscroll state. | 273 // touch-scrolling. These events should not reset the overscroll state. |
| 274 return !blink::WebInputEvent::isTouchEventType(event.type); | 274 return !blink::WebInputEvent::isTouchEventType(event.type()); |
| 275 } | 275 } |
| 276 } | 276 } |
| 277 | 277 |
| 278 bool OverscrollController::ProcessEventForOverscroll( | 278 bool OverscrollController::ProcessEventForOverscroll( |
| 279 const blink::WebInputEvent& event) { | 279 const blink::WebInputEvent& event) { |
| 280 bool event_processed = false; | 280 bool event_processed = false; |
| 281 switch (event.type) { | 281 switch (event.type()) { |
| 282 case blink::WebInputEvent::MouseWheel: { | 282 case blink::WebInputEvent::MouseWheel: { |
| 283 const blink::WebMouseWheelEvent& wheel = | 283 const blink::WebMouseWheelEvent& wheel = |
| 284 static_cast<const blink::WebMouseWheelEvent&>(event); | 284 static_cast<const blink::WebMouseWheelEvent&>(event); |
| 285 if (!wheel.hasPreciseScrollingDeltas) | 285 if (!wheel.hasPreciseScrollingDeltas) |
| 286 break; | 286 break; |
| 287 event_processed = | 287 event_processed = |
| 288 ProcessOverscroll(wheel.deltaX * wheel.accelerationRatioX, | 288 ProcessOverscroll(wheel.deltaX * wheel.accelerationRatioX, |
| 289 wheel.deltaY * wheel.accelerationRatioY, true); | 289 wheel.deltaY * wheel.accelerationRatioY, true); |
| 290 break; | 290 break; |
| 291 } | 291 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 318 break; | 318 break; |
| 319 } | 319 } |
| 320 } | 320 } |
| 321 | 321 |
| 322 // Reset overscroll state if fling didn't complete the overscroll gesture. | 322 // Reset overscroll state if fling didn't complete the overscroll gesture. |
| 323 SetOverscrollMode(OVERSCROLL_NONE); | 323 SetOverscrollMode(OVERSCROLL_NONE); |
| 324 break; | 324 break; |
| 325 } | 325 } |
| 326 | 326 |
| 327 default: | 327 default: |
| 328 DCHECK(blink::WebInputEvent::isGestureEventType(event.type) || | 328 DCHECK(blink::WebInputEvent::isGestureEventType(event.type()) || |
| 329 blink::WebInputEvent::isTouchEventType(event.type)) | 329 blink::WebInputEvent::isTouchEventType(event.type())) |
| 330 << "Received unexpected event: " << event.type; | 330 << "Received unexpected event: " << event.type(); |
| 331 } | 331 } |
| 332 return event_processed; | 332 return event_processed; |
| 333 } | 333 } |
| 334 | 334 |
| 335 bool OverscrollController::ProcessOverscroll(float delta_x, | 335 bool OverscrollController::ProcessOverscroll(float delta_x, |
| 336 float delta_y, | 336 float delta_y, |
| 337 bool is_touchpad) { | 337 bool is_touchpad) { |
| 338 if (scroll_state_ != STATE_CONTENT_SCROLLING) | 338 if (scroll_state_ != STATE_CONTENT_SCROLLING) |
| 339 overscroll_delta_x_ += delta_x; | 339 overscroll_delta_x_ += delta_x; |
| 340 overscroll_delta_y_ += delta_y; | 340 overscroll_delta_y_ += delta_y; |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 419 overscroll_mode_ = mode; | 419 overscroll_mode_ = mode; |
| 420 if (overscroll_mode_ == OVERSCROLL_NONE) | 420 if (overscroll_mode_ == OVERSCROLL_NONE) |
| 421 overscroll_delta_x_ = overscroll_delta_y_ = 0.f; | 421 overscroll_delta_x_ = overscroll_delta_y_ = 0.f; |
| 422 else | 422 else |
| 423 scroll_state_ = STATE_OVERSCROLLING; | 423 scroll_state_ = STATE_OVERSCROLLING; |
| 424 if (delegate_) | 424 if (delegate_) |
| 425 delegate_->OnOverscrollModeChange(old_mode, overscroll_mode_); | 425 delegate_->OnOverscrollModeChange(old_mode, overscroll_mode_); |
| 426 } | 426 } |
| 427 | 427 |
| 428 } // namespace content | 428 } // namespace content |
| OLD | NEW |