• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef UI_EVENTS_GESTURES_GESTURE_RECOGNIZER_IMPL_H_
6 #define UI_EVENTS_GESTURES_GESTURE_RECOGNIZER_IMPL_H_
7 
8 #include <map>
9 #include <vector>
10 
11 #include "base/memory/linked_ptr.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "ui/events/event_constants.h"
14 #include "ui/events/events_export.h"
15 #include "ui/events/gestures/gesture_provider_aura.h"
16 #include "ui/events/gestures/gesture_recognizer.h"
17 #include "ui/gfx/point.h"
18 
19 namespace ui {
20 class GestureConsumer;
21 class GestureEvent;
22 class GestureEventHelper;
23 class TouchEvent;
24 
25 // TODO(tdresser): Once the unified gesture recognition process sticks
26 // (crbug.com/332418), GestureRecognizerImpl can be cleaned up
27 // significantly.
28 class EVENTS_EXPORT GestureRecognizerImpl : public GestureRecognizer,
29                                             public GestureProviderAuraClient {
30  public:
31   typedef std::map<int, GestureConsumer*> TouchIdToConsumerMap;
32 
33   GestureRecognizerImpl();
34   virtual ~GestureRecognizerImpl();
35 
helpers()36   std::vector<GestureEventHelper*>& helpers() { return helpers_; }
37 
38   // Overridden from GestureRecognizer
39   virtual GestureConsumer* GetTouchLockedTarget(
40       const TouchEvent& event) OVERRIDE;
41   virtual GestureConsumer* GetTargetForGestureEvent(
42       const GestureEvent& event) OVERRIDE;
43   virtual GestureConsumer* GetTargetForLocation(
44       const gfx::PointF& location, int source_device_id) OVERRIDE;
45   virtual void TransferEventsTo(GestureConsumer* current_consumer,
46                                 GestureConsumer* new_consumer) OVERRIDE;
47   virtual bool GetLastTouchPointForTarget(GestureConsumer* consumer,
48                                           gfx::PointF* point) OVERRIDE;
49   virtual bool CancelActiveTouches(GestureConsumer* consumer) OVERRIDE;
50 
51  protected:
52   virtual GestureProviderAura* GetGestureProviderForConsumer(
53       GestureConsumer* c);
54 
55  private:
56   // Sets up the target consumer for gestures based on the touch-event.
57   void SetupTargets(const TouchEvent& event, GestureConsumer* consumer);
58 
59   void DispatchGestureEvent(GestureEvent* event);
60 
61   // Overridden from GestureRecognizer
62   virtual bool ProcessTouchEventPreDispatch(const TouchEvent& event,
63                                             GestureConsumer* consumer) OVERRIDE;
64 
65   virtual Gestures* ProcessTouchEventPostDispatch(
66       const TouchEvent& event,
67       ui::EventResult result,
68       GestureConsumer* consumer) OVERRIDE;
69 
70   virtual Gestures* ProcessTouchEventOnAsyncAck(
71       const TouchEvent& event,
72       ui::EventResult result,
73       GestureConsumer* consumer) OVERRIDE;
74 
75   virtual bool CleanupStateForConsumer(GestureConsumer* consumer)
76       OVERRIDE;
77   virtual void AddGestureEventHelper(GestureEventHelper* helper) OVERRIDE;
78   virtual void RemoveGestureEventHelper(GestureEventHelper* helper) OVERRIDE;
79 
80   // Overridden from GestureProviderAuraClient
81   virtual void OnGestureEvent(GestureEvent* event) OVERRIDE;
82 
83   // Convenience method to find the GestureEventHelper that can dispatch events
84   // to a specific |consumer|.
85   GestureEventHelper* FindDispatchHelperForConsumer(GestureConsumer* consumer);
86   std::map<GestureConsumer*, GestureProviderAura*> consumer_gesture_provider_;
87 
88   // Both |touch_id_target_| and |touch_id_target_for_gestures_| map a touch-id
89   // to its target window.  touch-ids are removed from |touch_id_target_| on
90   // ET_TOUCH_RELEASE and ET_TOUCH_CANCEL. |touch_id_target_for_gestures_| are
91   // removed in ConsumerDestroyed().
92   TouchIdToConsumerMap touch_id_target_;
93   TouchIdToConsumerMap touch_id_target_for_gestures_;
94 
95   std::vector<GestureEventHelper*> helpers_;
96 
97   DISALLOW_COPY_AND_ASSIGN(GestureRecognizerImpl);
98 };
99 
100 // Provided only for testing:
101 EVENTS_EXPORT void SetGestureRecognizerForTesting(
102     GestureRecognizer* gesture_recognizer);
103 
104 }  // namespace ui
105 
106 #endif  // UI_EVENTS_GESTURES_GESTURE_RECOGNIZER_IMPL_H_
107