OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_BASE_USER_INPUT_MONITOR_H_ | 5 #ifndef MEDIA_BASE_USER_INPUT_MONITOR_H_ |
6 #define MEDIA_BASE_USER_INPUT_MONITOR_H_ | 6 #define MEDIA_BASE_USER_INPUT_MONITOR_H_ |
7 | 7 |
8 #include <set> | |
9 | |
10 #include "base/memory/ref_counted.h" | |
11 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
12 #include "base/memory/weak_ptr.h" | |
13 #include "base/observer_list.h" | 9 #include "base/observer_list.h" |
14 #include "base/synchronization/lock.h" | 10 #include "base/synchronization/lock.h" |
15 #include "media/base/media_export.h" | 11 #include "media/base/media_export.h" |
16 #include "ui/base/events/event_constants.h" | |
17 #include "ui/base/keycodes/keyboard_codes.h" | |
18 | 12 |
19 struct SkIPoint; | 13 struct SkIPoint; |
20 | 14 |
21 namespace base { | 15 namespace base { |
22 class SingleThreadTaskRunner; | 16 class SingleThreadTaskRunner; |
23 } // namespace base | 17 } // namespace base |
24 | 18 |
25 namespace media { | 19 namespace media { |
26 | 20 |
27 // Monitors and notifies about mouse movements and keyboard events. | 21 // Monitors and notifies about mouse movements and keyboard events. |
28 // Thread safe. The thread on which the listenters are called is not guaranteed. | 22 // Thread safe. The thread on which the listenters are called is not guaranteed. |
29 // The callers should not perform expensive/blocking tasks in the callback since | 23 // The callers should not perform expensive/blocking tasks in the callback since |
30 // it might be called on the browser UI/IO threads. | 24 // it might be called on the browser UI/IO threads. |
| 25 // The object must outlive the browser UI/IO threads to make sure the callbacks |
| 26 // will not access a deleted object. |
31 class MEDIA_EXPORT UserInputMonitor { | 27 class MEDIA_EXPORT UserInputMonitor { |
32 public: | 28 public: |
33 // The interface to receive mouse movement events. | 29 // The interface to receive mouse movement events. |
34 class MEDIA_EXPORT MouseEventListener { | 30 class MEDIA_EXPORT MouseEventListener { |
35 public: | 31 public: |
36 // |position| is the new mouse position. | 32 // |position| is the new mouse position. |
37 virtual void OnMouseMoved(const SkIPoint& position) = 0; | 33 virtual void OnMouseMoved(const SkIPoint& position) = 0; |
38 | 34 |
39 protected: | 35 protected: |
40 virtual ~MouseEventListener() {} | 36 virtual ~MouseEventListener() {} |
41 }; | 37 }; |
42 // The interface to receive key stroke events. | |
43 class MEDIA_EXPORT KeyStrokeListener { | |
44 public: | |
45 // Called when any key is pressed. Called only once until the key is | |
46 // released, i.e. holding down a key for a long period will generate one | |
47 // callback just when the key is pressed down. | |
48 virtual void OnKeyStroke() = 0; | |
49 | 38 |
50 protected: | 39 UserInputMonitor(); |
51 virtual ~KeyStrokeListener() {} | |
52 }; | |
53 | |
54 virtual ~UserInputMonitor(); | 40 virtual ~UserInputMonitor(); |
55 | 41 |
56 // Creates a platform-specific instance of UserInputMonitor. | 42 // Creates a platform-specific instance of UserInputMonitor. |
57 // |io_task_runner| is the task runner for an IO thread. | 43 // |io_task_runner| is the task runner for an IO thread. |
58 // |ui_task_runner| is the task runner for a UI thread. | 44 // |ui_task_runner| is the task runner for a UI thread. |
59 static scoped_ptr<UserInputMonitor> Create( | 45 static scoped_ptr<UserInputMonitor> Create( |
60 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | 46 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, |
61 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner); | 47 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner); |
62 | 48 |
63 // The same |listener| should only be added once. | 49 // The same |listener| should only be added once. |
64 // The clients should make sure to call Remove*Listener before |listener| is | 50 // The clients should make sure to call Remove*Listener before |listener| is |
65 // destroyed. | 51 // destroyed. |
66 void AddMouseListener(MouseEventListener* listener); | 52 void AddMouseListener(MouseEventListener* listener); |
67 void RemoveMouseListener(MouseEventListener* listener); | 53 void RemoveMouseListener(MouseEventListener* listener); |
68 void AddKeyStrokeListener(KeyStrokeListener* listener); | 54 |
69 void RemoveKeyStrokeListener(KeyStrokeListener* listener); | 55 // A caller must call EnableKeyPressMonitoring and |
| 56 // DisableKeyPressMonitoring in pair. |
| 57 void EnableKeyPressMonitoring(); |
| 58 void DisableKeyPressMonitoring(); |
| 59 |
| 60 // Returns the number of keypresses. The starting point from when it is |
| 61 // counted is not guaranteed, but consistent within the pair of calls of |
| 62 // EnableKeyPressMonitoring and DisableKeyPressMonitoring. So a caller can |
| 63 // use the difference between the values returned at two times to get the |
| 64 // number of keypresses happened within that time period, but should not make |
| 65 // any assumption on the initial value. |
| 66 virtual size_t GetKeyPressCount() const = 0; |
70 | 67 |
71 protected: | 68 protected: |
72 UserInputMonitor(); | |
73 | |
74 // Called by the platform-specific sub-classes to propagate the events to the | 69 // Called by the platform-specific sub-classes to propagate the events to the |
75 // listeners. | 70 // listeners. |
76 void OnMouseEvent(const SkIPoint& position); | 71 void OnMouseEvent(const SkIPoint& position); |
77 void OnKeyboardEvent(ui::EventType event, ui::KeyboardCode key_code); | |
78 | 72 |
79 private: | 73 private: |
80 virtual void StartMouseMonitoring() = 0; | 74 virtual void StartMouseMonitoring() = 0; |
81 virtual void StopMouseMonitoring() = 0; | 75 virtual void StopMouseMonitoring() = 0; |
82 virtual void StartKeyboardMonitoring() = 0; | 76 virtual void StartKeyboardMonitoring() = 0; |
83 virtual void StopKeyboardMonitoring() = 0; | 77 virtual void StopKeyboardMonitoring() = 0; |
84 | 78 |
85 base::Lock lock_; | 79 base::Lock lock_; |
86 ObserverList<MouseEventListener, true> mouse_listeners_; | 80 ObserverList<MouseEventListener, true> mouse_listeners_; |
87 ObserverList<KeyStrokeListener, true> key_stroke_listeners_; | |
88 bool monitoring_mouse_; | 81 bool monitoring_mouse_; |
89 bool monitoring_keyboard_; | 82 size_t key_press_counter_references_; |
90 // The set of keys currently held down. Used for convering raw keyboard events | |
91 // into KeyStrokeListener callbacks. | |
92 std::set<ui::KeyboardCode> pressed_keys_; | |
93 | 83 |
94 DISALLOW_COPY_AND_ASSIGN(UserInputMonitor); | 84 DISALLOW_COPY_AND_ASSIGN(UserInputMonitor); |
95 }; | 85 }; |
96 | 86 |
97 } // namespace media | 87 } // namespace media |
98 | 88 |
99 #endif // MEDIA_BASE_USER_INPUT_MONITOR_H_ | 89 #endif // MEDIA_BASE_USER_INPUT_MONITOR_H_ |
OLD | NEW |