Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / events / gestures / gesture_provider_aura.cc
blob7742a587d601af542dcac9417093d261dfdd2a5c
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.
5 #include "ui/events/gestures/gesture_provider_aura.h"
7 #include "base/auto_reset.h"
8 #include "base/logging.h"
9 #include "ui/events/event.h"
10 #include "ui/events/gesture_detection/gesture_configuration.h"
11 #include "ui/events/gesture_detection/gesture_event_data.h"
12 #include "ui/events/gesture_detection/gesture_provider_config_helper.h"
14 namespace ui {
16 GestureProviderAura::GestureProviderAura(GestureProviderAuraClient* client)
17 : client_(client),
18 filtered_gesture_provider_(
19 GetGestureProviderConfig(GestureProviderConfigType::CURRENT_PLATFORM),
20 this),
21 handling_event_(false),
22 last_unique_touch_event_id_(
23 std::numeric_limits<unsigned long long>::max()) {
24 filtered_gesture_provider_.SetDoubleTapSupportForPlatformEnabled(false);
27 GestureProviderAura::~GestureProviderAura() {}
29 bool GestureProviderAura::OnTouchEvent(TouchEvent* event) {
30 if (!pointer_state_.OnTouch(*event))
31 return false;
33 last_unique_touch_event_id_ = event->unique_event_id();
35 auto result = filtered_gesture_provider_.OnTouchEvent(pointer_state_);
36 if (!result.succeeded)
37 return false;
39 event->set_may_cause_scrolling(result.did_generate_scroll);
40 pointer_state_.CleanupRemovedTouchPoints(*event);
41 return true;
44 void GestureProviderAura::OnAsyncTouchEventAck(bool event_consumed) {
45 DCHECK(pending_gestures_.empty());
46 DCHECK(!handling_event_);
47 base::AutoReset<bool> handling_event(&handling_event_, true);
48 filtered_gesture_provider_.OnAsyncTouchEventAck(event_consumed);
51 void GestureProviderAura::OnSyncTouchEventAck(const uint64 unique_event_id,
52 bool event_consumed) {
53 DCHECK_EQ(last_unique_touch_event_id_, unique_event_id);
54 DCHECK(pending_gestures_.empty());
55 DCHECK(!handling_event_);
56 base::AutoReset<bool> handling_event(&handling_event_, true);
57 filtered_gesture_provider_.OnSyncTouchEventAck(event_consumed);
60 void GestureProviderAura::OnGestureEvent(
61 const GestureEventData& gesture) {
62 GestureEventDetails details = gesture.details;
63 details.set_oldest_touch_id(gesture.motion_event_id);
65 if (gesture.type() == ET_GESTURE_TAP) {
66 int tap_count = 1;
67 if (previous_tap_ && IsConsideredDoubleTap(*previous_tap_, gesture))
68 tap_count = 1 + (previous_tap_->details.tap_count() % 3);
69 details.set_tap_count(tap_count);
70 if (!previous_tap_)
71 previous_tap_.reset(new GestureEventData(gesture));
72 else
73 *previous_tap_ = gesture;
74 previous_tap_->details = details;
75 } else if (gesture.type() == ET_GESTURE_TAP_CANCEL) {
76 previous_tap_.reset();
79 scoped_ptr<ui::GestureEvent> event(
80 new ui::GestureEvent(gesture.x,
81 gesture.y,
82 gesture.flags,
83 gesture.time - base::TimeTicks(),
84 details));
86 if (!handling_event_) {
87 // Dispatching event caused by timer.
88 client_->OnGestureEvent(event.get());
89 } else {
90 // Memory managed by ScopedVector pending_gestures_.
91 pending_gestures_.push_back(event.release());
95 ScopedVector<GestureEvent>* GestureProviderAura::GetAndResetPendingGestures() {
96 if (pending_gestures_.empty())
97 return NULL;
98 // Caller is responsible for deleting old_pending_gestures.
99 ScopedVector<GestureEvent>* old_pending_gestures =
100 new ScopedVector<GestureEvent>();
101 old_pending_gestures->swap(pending_gestures_);
102 return old_pending_gestures;
105 bool GestureProviderAura::IsConsideredDoubleTap(
106 const GestureEventData& previous_tap,
107 const GestureEventData& current_tap) const {
108 if (current_tap.time - previous_tap.time >
109 base::TimeDelta::FromMilliseconds(
110 GestureConfiguration::GetInstance()
111 ->max_time_between_double_click_in_ms())) {
112 return false;
115 float double_tap_slop_square =
116 GestureConfiguration::GetInstance()
117 ->max_distance_between_taps_for_double_tap();
118 double_tap_slop_square *= double_tap_slop_square;
119 const float delta_x = previous_tap.x - current_tap.x;
120 const float delta_y = previous_tap.y - current_tap.y;
121 return (delta_x * delta_x + delta_y * delta_y < double_tap_slop_square);
124 } // namespace content