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/web_touch_event_traits.h"
6
7 #include "base/logging.h"
8
9 using blink::WebInputEvent;
10 using blink::WebTouchEvent;
11 using blink::WebTouchPoint;
12
13 namespace content {
14
AllTouchPointsHaveState(const WebTouchEvent & event,blink::WebTouchPoint::State state)15 bool WebTouchEventTraits::AllTouchPointsHaveState(
16 const WebTouchEvent& event,
17 blink::WebTouchPoint::State state) {
18 if (!event.touchesLength)
19 return false;
20 for (size_t i = 0; i < event.touchesLength; ++i) {
21 if (event.touches[i].state != state)
22 return false;
23 }
24 return true;
25 }
26
IsTouchSequenceStart(const WebTouchEvent & event)27 bool WebTouchEventTraits::IsTouchSequenceStart(const WebTouchEvent& event) {
28 DCHECK(event.touchesLength);
29 if (event.type != WebInputEvent::TouchStart)
30 return false;
31 return AllTouchPointsHaveState(event, blink::WebTouchPoint::StatePressed);
32 }
33
IsTouchSequenceEnd(const WebTouchEvent & event)34 bool WebTouchEventTraits::IsTouchSequenceEnd(const WebTouchEvent& event) {
35 if (event.type != WebInputEvent::TouchEnd &&
36 event.type != WebInputEvent::TouchCancel)
37 return false;
38 if (!event.touchesLength)
39 return true;
40 for (size_t i = 0; i < event.touchesLength; ++i) {
41 if (event.touches[i].state != blink::WebTouchPoint::StateReleased &&
42 event.touches[i].state != blink::WebTouchPoint::StateCancelled)
43 return false;
44 }
45 return true;
46 }
47
ResetType(WebInputEvent::Type type,double timestamp_sec,WebTouchEvent * event)48 void WebTouchEventTraits::ResetType(WebInputEvent::Type type,
49 double timestamp_sec,
50 WebTouchEvent* event) {
51 DCHECK(WebInputEvent::isTouchEventType(type));
52 event->type = type;
53 event->cancelable = (type != WebInputEvent::TouchCancel);
54 event->timeStampSeconds = timestamp_sec;
55 }
56
ResetTypeAndTouchStates(WebInputEvent::Type type,double timestamp_sec,WebTouchEvent * event)57 void WebTouchEventTraits::ResetTypeAndTouchStates(WebInputEvent::Type type,
58 double timestamp_sec,
59 WebTouchEvent* event) {
60 ResetType(type, timestamp_sec, event);
61
62 WebTouchPoint::State newState = WebTouchPoint::StateUndefined;
63 switch (event->type) {
64 case WebInputEvent::TouchStart:
65 newState = WebTouchPoint::StatePressed;
66 break;
67 case WebInputEvent::TouchMove:
68 newState = WebTouchPoint::StateMoved;
69 break;
70 case WebInputEvent::TouchEnd:
71 newState = WebTouchPoint::StateReleased;
72 break;
73 case WebInputEvent::TouchCancel:
74 newState = WebTouchPoint::StateCancelled;
75 break;
76 default:
77 NOTREACHED();
78 break;
79 }
80 for (size_t i = 0; i < event->touchesLength; ++i)
81 event->touches[i].state = newState;
82 }
83
84 } // namespace content
85