1// Copyright 2014 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#include "content/common/input/gesture_event_stream_validator.h"
6
7#include "base/logging.h"
8#include "third_party/WebKit/public/web/WebInputEvent.h"
9
10using blink::WebInputEvent;
11
12namespace content {
13
14GestureEventStreamValidator::GestureEventStreamValidator()
15    : scrolling_(false), pinching_(false), waiting_for_tap_end_(false) {
16}
17
18GestureEventStreamValidator::~GestureEventStreamValidator() {
19}
20
21bool GestureEventStreamValidator::Validate(const blink::WebGestureEvent& event,
22                                           std::string* error_msg) {
23  DCHECK(error_msg);
24  error_msg->clear();
25  switch (event.type) {
26    case WebInputEvent::GestureScrollBegin:
27      if (scrolling_)
28        error_msg->append("Scroll begin during scroll\n");
29      if (pinching_)
30        error_msg->append("Scroll begin during pinch\n");
31      scrolling_ = true;
32      break;
33    case WebInputEvent::GestureScrollUpdate:
34    case WebInputEvent::GestureScrollUpdateWithoutPropagation:
35      if (!scrolling_)
36        error_msg->append("Scroll update outside of scroll\n");
37      break;
38    case WebInputEvent::GestureScrollEnd:
39    case WebInputEvent::GestureFlingStart:
40      if (!scrolling_)
41        error_msg->append("Scroll end outside of scroll\n");
42      if (pinching_)
43        error_msg->append("Ending scroll while pinching\n");
44      scrolling_ = false;
45      break;
46    case WebInputEvent::GesturePinchBegin:
47      if (pinching_)
48        error_msg->append("Pinch begin during pinch\n");
49      pinching_ = true;
50      break;
51    case WebInputEvent::GesturePinchUpdate:
52      if (!pinching_)
53        error_msg->append("Pinch update outside of pinch\n");
54      break;
55    case WebInputEvent::GesturePinchEnd:
56      if (!pinching_)
57        error_msg->append("Pinch end outside of pinch\n");
58      pinching_ = false;
59      break;
60    case WebInputEvent::GestureTapDown:
61      if (waiting_for_tap_end_)
62        error_msg->append("Missing tap end event\n");
63      waiting_for_tap_end_ = true;
64      break;
65    case WebInputEvent::GestureTapUnconfirmed:
66      if (!waiting_for_tap_end_)
67        error_msg->append("Missing TapDown event before TapUnconfirmed\n");
68      break;
69    case WebInputEvent::GestureTapCancel:
70      if (!waiting_for_tap_end_)
71        error_msg->append("Missing TapDown event before TapCancel\n");
72      waiting_for_tap_end_ = false;
73      break;
74    case WebInputEvent::GestureTap:
75    case WebInputEvent::GestureDoubleTap:
76      // Both Tap and DoubleTap gestures may be synthetically inserted, and do
77      // not require a preceding TapDown.
78      waiting_for_tap_end_ = false;
79      break;
80    default:
81      break;
82  }
83  return error_msg->empty();
84}
85
86}  // namespace content
87