summaryrefslogtreecommitdiffstats
path: root/content/browser/renderer_host/tap_suppression_controller_aura.cc
blob: 7d7c2649023d638681c9adabc29bdd1f05e8d3fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/renderer_host/tap_suppression_controller.h"

#include "base/command_line.h"
#include "base/debug/trace_event.h"
#include "base/logging.h"
#include "base/string_number_conversions.h"
#include "content/browser/renderer_host/render_widget_host_impl.h"
#include "content/public/common/content_switches.h"
#include "ui/base/gestures/gesture_configuration.h"

namespace content {

TapSuppressionController::TapSuppressionController(RenderWidgetHostImpl* rwhv)
     : render_widget_host_(rwhv),
       state_(TapSuppressionController::NOTHING) {
}

TapSuppressionController::~TapSuppressionController() { }

bool TapSuppressionController::ShouldSuppressMouseUp() {
  switch (state_) {
    case NOTHING:
    case GFC_IN_PROGRESS:
      return false;
    case MD_STASHED:
      state_ = NOTHING;
      mouse_down_timer_.Stop();
      return true;
    case LAST_CANCEL_STOPPED_FLING:
      NOTREACHED() << "Invalid MouseUp on LAST_CANCEL_STOPPED_FLING state";
  }
  return false;
}

bool TapSuppressionController::ShouldDeferMouseDown(
    const WebKit::WebMouseEvent& event) {
  switch (state_) {
    case NOTHING:
      return false;
    case GFC_IN_PROGRESS:
      mouse_down_timer_.Start(
          FROM_HERE,
          base::TimeDelta::FromMilliseconds(
              ui::GestureConfiguration::fling_max_tap_gap_time_in_ms()),
          this,
          &TapSuppressionController::MouseDownTimerExpired);
      stashed_mouse_down_ = event;
      state_ = MD_STASHED;
      return true;
    case MD_STASHED:
      NOTREACHED() << "MouseDown on MD_STASHED state";
      state_ = NOTHING;
      return false;
    case LAST_CANCEL_STOPPED_FLING:
      if ((base::TimeTicks::Now() - fling_cancel_time_).InMilliseconds()
          < ui::GestureConfiguration::fling_max_cancel_to_down_time_in_ms()) {
        state_ = MD_STASHED;
        mouse_down_timer_.Start(
            FROM_HERE,
            base::TimeDelta::FromMilliseconds(
                ui::GestureConfiguration::fling_max_tap_gap_time_in_ms()),
            this,
            &TapSuppressionController::MouseDownTimerExpired);
        stashed_mouse_down_ = event;
        return true;
      } else {
        state_ = NOTHING;
        return false;
      }
  }
  return false;
}

void TapSuppressionController::GestureFlingCancelAck(bool processed) {
  switch (state_) {
    case NOTHING:
      NOTREACHED() << "GFC_Ack without a GFC";
      break;
    case GFC_IN_PROGRESS:
      if (processed)
        fling_cancel_time_ = base::TimeTicks::Now();
      state_ = LAST_CANCEL_STOPPED_FLING;
      break;
    case MD_STASHED:
      if (!processed) {
        TRACE_EVENT0("browser",
                     "TapSuppressionController::GestureFlingCancelAck");
        mouse_down_timer_.Stop();
        render_widget_host_->ForwardMouseEventImmediately(stashed_mouse_down_);
        state_ = NOTHING;
      } // Else waiting for the timer to release the mouse event.
      break;
    case LAST_CANCEL_STOPPED_FLING:
      break;
  }
}

void TapSuppressionController::GestureFlingCancel(double cancel_time) {
  switch (state_) {
    case NOTHING:
    case GFC_IN_PROGRESS:
    case LAST_CANCEL_STOPPED_FLING:
      state_ = GFC_IN_PROGRESS;
      break;
    case MD_STASHED:
      break;
  }
}

void TapSuppressionController::MouseDownTimerExpired() {
  switch (state_) {
    case NOTHING:
    case GFC_IN_PROGRESS:
    case LAST_CANCEL_STOPPED_FLING:
      NOTREACHED() << "Timer fired on invalid state.";
      state_ = NOTHING;
      break;
    case MD_STASHED:
      TRACE_EVENT0("browser",
                   "TapSuppressionController::MouseDownTimerExpired");
      render_widget_host_->ForwardMouseEventImmediately(stashed_mouse_down_);
      state_ = NOTHING;
      break;
  }
}

} // namespace content