Wrapper script for isolating telemetry_gpu_unittests.
[chromium-blink-merge.git] / ash / touch / touch_uma.cc
blob9ab7ba41847aae438fa86ddeb3e082301874615c
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
3 // found in the LICENSE file.
5 #include "ash/touch/touch_uma.h"
7 #include "ash/metrics/user_metrics_recorder.h"
8 #include "ash/shell.h"
9 #include "base/metrics/histogram.h"
10 #include "base/strings/stringprintf.h"
11 #include "ui/aura/env.h"
12 #include "ui/aura/window.h"
13 #include "ui/aura/window_event_dispatcher.h"
14 #include "ui/aura/window_property.h"
15 #include "ui/events/event.h"
16 #include "ui/events/event_utils.h"
17 #include "ui/gfx/geometry/point_conversions.h"
19 #if defined(USE_X11)
20 #include <X11/extensions/XInput2.h>
21 #include <X11/Xlib.h>
22 #endif
24 namespace {
26 struct WindowTouchDetails {
27 // Move and start times of the touch points. The key is the touch-id.
28 std::map<int, base::TimeDelta> last_move_time_;
29 std::map<int, base::TimeDelta> last_start_time_;
31 // The first and last positions of the touch points.
32 std::map<int, gfx::Point> start_touch_position_;
33 std::map<int, gfx::Point> last_touch_position_;
35 // Last time-stamp of the last touch-end event.
36 base::TimeDelta last_release_time_;
38 // Stores the time of the last touch released on this window (if there was a
39 // multi-touch gesture on the window, then this is the release-time of the
40 // last touch on the window).
41 base::TimeDelta last_mt_time_;
44 DEFINE_OWNED_WINDOW_PROPERTY_KEY(WindowTouchDetails,
45 kWindowTouchDetails,
46 NULL);
49 DECLARE_WINDOW_PROPERTY_TYPE(WindowTouchDetails*);
51 namespace ash {
53 // static
54 TouchUMA* TouchUMA::GetInstance() {
55 return Singleton<TouchUMA>::get();
58 void TouchUMA::RecordGestureEvent(aura::Window* target,
59 const ui::GestureEvent& event) {
60 GestureActionType action = FindGestureActionType(target, event);
61 RecordGestureAction(action);
63 if (event.type() == ui::ET_GESTURE_END &&
64 event.details().touch_points() == 2) {
65 WindowTouchDetails* details = target->GetProperty(kWindowTouchDetails);
66 if (!details) {
67 LOG(ERROR) << "Window received gesture events without receiving any touch"
68 " events";
69 return;
71 details->last_mt_time_ = event.time_stamp();
75 void TouchUMA::RecordGestureAction(GestureActionType action) {
76 if (action == GESTURE_UNKNOWN || action >= GESTURE_ACTION_COUNT)
77 return;
78 UMA_HISTOGRAM_ENUMERATION("Ash.GestureTarget", action,
79 GESTURE_ACTION_COUNT);
82 void TouchUMA::RecordTouchEvent(aura::Window* target,
83 const ui::TouchEvent& event) {
84 UMA_HISTOGRAM_CUSTOM_COUNTS("Ash.TouchRadius",
85 static_cast<int>(std::max(event.radius_x(), event.radius_y())),
86 1, 500, 100);
88 UpdateTouchState(event);
90 WindowTouchDetails* details = target->GetProperty(kWindowTouchDetails);
91 if (!details) {
92 details = new WindowTouchDetails;
93 target->SetProperty(kWindowTouchDetails, details);
96 // Record the location of the touch points.
97 const int kBucketCountForLocation = 100;
98 const gfx::Rect bounds = target->GetRootWindow()->bounds();
99 const int bucket_size_x = std::max(1,
100 bounds.width() / kBucketCountForLocation);
101 const int bucket_size_y = std::max(1,
102 bounds.height() / kBucketCountForLocation);
104 gfx::Point position = event.root_location();
106 // Prefer raw event location (when available) over calibrated location.
107 if (event.HasNativeEvent()) {
108 position = ui::EventLocationFromNative(event.native_event());
109 position = gfx::ToFlooredPoint(
110 gfx::ScalePoint(position, 1. / target->layer()->device_scale_factor()));
113 position.set_x(std::min(bounds.width() - 1, std::max(0, position.x())));
114 position.set_y(std::min(bounds.height() - 1, std::max(0, position.y())));
116 UMA_HISTOGRAM_CUSTOM_COUNTS("Ash.TouchPositionX",
117 position.x() / bucket_size_x,
118 0, kBucketCountForLocation, kBucketCountForLocation + 1);
119 UMA_HISTOGRAM_CUSTOM_COUNTS("Ash.TouchPositionY",
120 position.y() / bucket_size_y,
121 0, kBucketCountForLocation, kBucketCountForLocation + 1);
123 if (event.type() == ui::ET_TOUCH_PRESSED) {
124 Shell::GetInstance()->metrics()->RecordUserMetricsAction(
125 UMA_TOUCHSCREEN_TAP_DOWN);
127 details->last_start_time_[event.touch_id()] = event.time_stamp();
128 details->start_touch_position_[event.touch_id()] = event.root_location();
129 details->last_touch_position_[event.touch_id()] = event.location();
131 if (details->last_release_time_.ToInternalValue()) {
132 // Measuring the interval between a touch-release and the next
133 // touch-start is probably less useful when doing multi-touch (e.g.
134 // gestures, or multi-touch friendly apps). So count this only if the user
135 // hasn't done any multi-touch during the last 30 seconds.
136 base::TimeDelta diff = event.time_stamp() - details->last_mt_time_;
137 if (diff.InSeconds() > 30) {
138 base::TimeDelta gap = event.time_stamp() - details->last_release_time_;
139 UMA_HISTOGRAM_COUNTS_10000("Ash.TouchStartAfterEnd",
140 gap.InMilliseconds());
144 // Record the number of touch-points currently active for the window.
145 const int kMaxTouchPoints = 10;
146 UMA_HISTOGRAM_CUSTOM_COUNTS("Ash.ActiveTouchPoints",
147 details->last_start_time_.size(),
148 1, kMaxTouchPoints, kMaxTouchPoints + 1);
149 } else if (event.type() == ui::ET_TOUCH_RELEASED) {
150 if (details->last_start_time_.count(event.touch_id())) {
151 base::TimeDelta duration = event.time_stamp() -
152 details->last_start_time_[event.touch_id()];
153 // Look for touches that were [almost] stationary for a long time.
154 const double kLongStationaryTouchDuration = 10;
155 const int kLongStationaryTouchDistanceSquared = 100;
156 if (duration.InSecondsF() > kLongStationaryTouchDuration) {
157 gfx::Vector2d distance = event.root_location() -
158 details->start_touch_position_[event.touch_id()];
159 if (distance.LengthSquared() < kLongStationaryTouchDistanceSquared) {
160 UMA_HISTOGRAM_CUSTOM_COUNTS("Ash.StationaryTouchDuration",
161 duration.InSeconds(),
162 kLongStationaryTouchDuration,
163 1000,
164 20);
168 details->last_start_time_.erase(event.touch_id());
169 details->last_move_time_.erase(event.touch_id());
170 details->start_touch_position_.erase(event.touch_id());
171 details->last_touch_position_.erase(event.touch_id());
172 details->last_release_time_ = event.time_stamp();
173 } else if (event.type() == ui::ET_TOUCH_MOVED) {
174 int distance = 0;
175 if (details->last_touch_position_.count(event.touch_id())) {
176 gfx::Point lastpos = details->last_touch_position_[event.touch_id()];
177 distance =
178 std::abs(lastpos.x() - event.x()) + std::abs(lastpos.y() - event.y());
181 if (details->last_move_time_.count(event.touch_id())) {
182 base::TimeDelta move_delay = event.time_stamp() -
183 details->last_move_time_[event.touch_id()];
184 UMA_HISTOGRAM_CUSTOM_COUNTS("Ash.TouchMoveInterval",
185 move_delay.InMilliseconds(),
186 1, 50, 25);
189 UMA_HISTOGRAM_CUSTOM_COUNTS("Ash.TouchMoveSteps", distance, 1, 1000, 50);
191 details->last_move_time_[event.touch_id()] = event.time_stamp();
192 details->last_touch_position_[event.touch_id()] = event.location();
196 TouchUMA::TouchUMA()
197 : is_single_finger_gesture_(false),
198 touch_in_progress_(false),
199 burst_length_(0) {
202 TouchUMA::~TouchUMA() {
205 void TouchUMA::UpdateTouchState(const ui::TouchEvent& event) {
206 if (event.type() == ui::ET_TOUCH_PRESSED) {
207 if (!touch_in_progress_) {
208 is_single_finger_gesture_ = true;
209 base::TimeDelta difference = event.time_stamp() - last_touch_down_time_;
210 if (difference > base::TimeDelta::FromMilliseconds(250)) {
211 if (burst_length_) {
212 UMA_HISTOGRAM_COUNTS_100("Ash.TouchStartBurst",
213 std::min(burst_length_, 100));
215 burst_length_ = 1;
216 } else {
217 ++burst_length_;
219 } else {
220 is_single_finger_gesture_ = false;
222 touch_in_progress_ = true;
223 last_touch_down_time_ = event.time_stamp();
224 } else if (event.type() == ui::ET_TOUCH_RELEASED) {
225 if (!aura::Env::GetInstance()->is_touch_down())
226 touch_in_progress_ = false;
230 TouchUMA::GestureActionType TouchUMA::FindGestureActionType(
231 aura::Window* window,
232 const ui::GestureEvent& event) {
233 if (!window || window->GetRootWindow() == window) {
234 if (event.type() == ui::ET_GESTURE_SCROLL_BEGIN)
235 return GESTURE_BEZEL_SCROLL;
236 if (event.type() == ui::ET_GESTURE_BEGIN)
237 return GESTURE_BEZEL_DOWN;
238 return GESTURE_UNKNOWN;
241 std::string name = window ? window->name() : std::string();
243 const char kDesktopBackgroundView[] = "DesktopBackgroundView";
244 if (name == kDesktopBackgroundView) {
245 if (event.type() == ui::ET_GESTURE_SCROLL_BEGIN)
246 return GESTURE_DESKTOP_SCROLL;
247 if (event.type() == ui::ET_GESTURE_PINCH_BEGIN)
248 return GESTURE_DESKTOP_PINCH;
249 return GESTURE_UNKNOWN;
252 const char kWebPage[] = "RenderWidgetHostViewAura";
253 if (name == kWebPage) {
254 if (event.type() == ui::ET_GESTURE_PINCH_BEGIN)
255 return GESTURE_WEBPAGE_PINCH;
256 if (event.type() == ui::ET_GESTURE_SCROLL_BEGIN)
257 return GESTURE_WEBPAGE_SCROLL;
258 if (event.type() == ui::ET_GESTURE_TAP)
259 return GESTURE_WEBPAGE_TAP;
260 return GESTURE_UNKNOWN;
263 views::Widget* widget = views::Widget::GetWidgetForNativeView(window);
264 if (!widget)
265 return GESTURE_UNKNOWN;
267 // |widget| may be in the process of destroying if it has ownership
268 // views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET and |event| was
269 // dispatched as part of gesture state cleanup. In this case the RootView
270 // of |widget| may no longer exist, so check before calling into any
271 // RootView methods.
272 if (!widget->GetRootView())
273 return GESTURE_UNKNOWN;
275 views::View* view = widget->GetRootView()->
276 GetEventHandlerForPoint(event.location());
277 if (!view)
278 return GESTURE_UNKNOWN;
280 name = view->GetClassName();
282 const char kTabStrip[] = "TabStrip";
283 const char kTab[] = "BrowserTab";
284 if (name == kTabStrip || name == kTab) {
285 if (event.type() == ui::ET_GESTURE_SCROLL_BEGIN)
286 return GESTURE_TABSTRIP_SCROLL;
287 if (event.type() == ui::ET_GESTURE_PINCH_BEGIN)
288 return GESTURE_TABSTRIP_PINCH;
289 if (event.type() == ui::ET_GESTURE_TAP)
290 return GESTURE_TABSTRIP_TAP;
291 return GESTURE_UNKNOWN;
294 const char kOmnibox[] = "BrowserOmniboxViewViews";
295 if (name == kOmnibox) {
296 if (event.type() == ui::ET_GESTURE_SCROLL_BEGIN)
297 return GESTURE_OMNIBOX_SCROLL;
298 if (event.type() == ui::ET_GESTURE_PINCH_BEGIN)
299 return GESTURE_OMNIBOX_PINCH;
300 return GESTURE_UNKNOWN;
303 return GESTURE_UNKNOWN;
306 } // namespace ash