Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / input / motion_event_web.cc
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/browser/renderer_host/input/motion_event_web.h"
6
7 #include "base/logging.h"
8 #include "content/common/input/web_touch_event_traits.h"
9
10 using blink::WebInputEvent;
11 using blink::WebTouchEvent;
12 using blink::WebTouchPoint;
13
14 namespace content {
15 namespace {
16
17 ui::MotionEvent::Action GetActionFrom(const WebTouchEvent& event) {
18   DCHECK(event.touchesLength);
19   switch (event.type) {
20     case WebInputEvent::TouchStart:
21       if (WebTouchEventTraits::AllTouchPointsHaveState(
22               event, WebTouchPoint::StatePressed))
23         return ui::MotionEvent::ACTION_DOWN;
24       else
25         return ui::MotionEvent::ACTION_POINTER_DOWN;
26     case WebInputEvent::TouchEnd:
27       if (WebTouchEventTraits::AllTouchPointsHaveState(
28               event, WebTouchPoint::StateReleased))
29         return ui::MotionEvent::ACTION_UP;
30       else
31         return ui::MotionEvent::ACTION_POINTER_UP;
32     case WebInputEvent::TouchCancel:
33       DCHECK(WebTouchEventTraits::AllTouchPointsHaveState(
34           event, WebTouchPoint::StateCancelled));
35       return ui::MotionEvent::ACTION_CANCEL;
36     case WebInputEvent::TouchMove:
37       return ui::MotionEvent::ACTION_MOVE;
38     default:
39       break;
40   };
41   NOTREACHED()
42       << "Unable to derive a valid MotionEvent::Action from the WebTouchEvent.";
43   return ui::MotionEvent::ACTION_CANCEL;
44 }
45
46 int GetActionIndexFrom(const WebTouchEvent& event) {
47   for (size_t i = 0; i < event.touchesLength; ++i) {
48     if (event.touches[i].state != WebTouchPoint::StateUndefined &&
49         event.touches[i].state != WebTouchPoint::StateStationary)
50       return i;
51   }
52   return -1;
53 }
54
55 }  // namespace
56
57 MotionEventWeb::MotionEventWeb(const WebTouchEvent& event)
58     : event_(event),
59       cached_action_(GetActionFrom(event)),
60       cached_action_index_(GetActionIndexFrom(event)) {
61   DCHECK_GT(GetPointerCount(), 0U);
62 }
63
64 MotionEventWeb::~MotionEventWeb() {}
65
66 int MotionEventWeb::GetId() const {
67   return 0;
68 }
69
70 MotionEventWeb::Action MotionEventWeb::GetAction() const {
71   return cached_action_;
72 }
73
74 int MotionEventWeb::GetActionIndex() const { return cached_action_index_; }
75
76 size_t MotionEventWeb::GetPointerCount() const { return event_.touchesLength; }
77
78 int MotionEventWeb::GetPointerId(size_t pointer_index) const {
79   DCHECK_LT(pointer_index, GetPointerCount());
80   return event_.touches[pointer_index].id;
81 }
82
83 float MotionEventWeb::GetX(size_t pointer_index) const {
84   DCHECK_LT(pointer_index, GetPointerCount());
85   return event_.touches[pointer_index].position.x;
86 }
87
88 float MotionEventWeb::GetY(size_t pointer_index) const {
89   DCHECK_LT(pointer_index, GetPointerCount());
90   return event_.touches[pointer_index].position.y;
91 }
92
93 float MotionEventWeb::GetTouchMajor(size_t pointer_index) const {
94   DCHECK_LT(pointer_index, GetPointerCount());
95   // TODO(jdduke): We should be a bit more careful about axes here.
96   return 2.f * std::max(event_.touches[pointer_index].radiusX,
97                         event_.touches[pointer_index].radiusY);
98 }
99
100 float MotionEventWeb::GetPressure(size_t pointer_index) const {
101   return 0.f;
102 }
103
104 base::TimeTicks MotionEventWeb::GetEventTime() const {
105   return base::TimeTicks() +
106          base::TimeDelta::FromMicroseconds(event_.timeStampSeconds *
107                                            base::Time::kMicrosecondsPerSecond);
108 }
109
110 size_t MotionEventWeb::GetHistorySize() const { return 0; }
111
112 base::TimeTicks MotionEventWeb::GetHistoricalEventTime(
113     size_t historical_index) const {
114   NOTIMPLEMENTED();
115   return base::TimeTicks();
116 }
117
118 float MotionEventWeb::GetHistoricalTouchMajor(size_t pointer_index,
119                                               size_t historical_index) const {
120   NOTIMPLEMENTED();
121   return 0.f;
122 }
123
124 float MotionEventWeb::GetHistoricalX(size_t pointer_index,
125                                      size_t historical_index) const {
126   NOTIMPLEMENTED();
127   return 0.f;
128 }
129
130 float MotionEventWeb::GetHistoricalY(size_t pointer_index,
131                                      size_t historical_index) const {
132   NOTIMPLEMENTED();
133   return 0.f;
134 }
135
136 scoped_ptr<ui::MotionEvent> MotionEventWeb::Clone() const {
137   return scoped_ptr<MotionEvent>(new MotionEventWeb(event_));
138 }
139
140 scoped_ptr<ui::MotionEvent> MotionEventWeb::Cancel() const {
141   WebTouchEvent cancel_event(event_);
142   WebTouchEventTraits::ResetTypeAndTouchStates(
143       blink::WebInputEvent::TouchCancel,
144       // TODO(rbyers): Shouldn't we use a fresh timestamp?
145       event_.timeStampSeconds,
146       &cancel_event);
147   return scoped_ptr<MotionEvent>(new MotionEventWeb(cancel_event));
148 }
149
150 }  // namespace content