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 deleted object. | |
Mark Mentovai
2013/08/23 20:18:11
access a deleted object.
jiayl
2013/08/23 23:47:58
Done.
| |
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 | |
50 protected: | |
51 virtual ~KeyStrokeListener() {} | |
52 }; | |
53 | 38 |
54 virtual ~UserInputMonitor(); | 39 virtual ~UserInputMonitor(); |
55 | 40 |
56 // Creates a platform-specific instance of UserInputMonitor. | 41 // Creates a platform-specific instance of UserInputMonitor. |
57 // |io_task_runner| is the task runner for an IO thread. | 42 // |io_task_runner| is the task runner for an IO thread. |
58 // |ui_task_runner| is the task runner for a UI thread. | 43 // |ui_task_runner| is the task runner for a UI thread. |
59 static scoped_ptr<UserInputMonitor> Create( | 44 static scoped_ptr<UserInputMonitor> Create( |
60 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | 45 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, |
61 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner); | 46 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner); |
62 | 47 |
63 // The same |listener| should only be added once. | 48 // The same |listener| should only be added once. |
64 // The clients should make sure to call Remove*Listener before |listener| is | 49 // The clients should make sure to call Remove*Listener before |listener| is |
65 // destroyed. | 50 // destroyed. |
66 void AddMouseListener(MouseEventListener* listener); | 51 void AddMouseListener(MouseEventListener* listener); |
67 void RemoveMouseListener(MouseEventListener* listener); | 52 void RemoveMouseListener(MouseEventListener* listener); |
68 void AddKeyStrokeListener(KeyStrokeListener* listener); | 53 |
69 void RemoveKeyStrokeListener(KeyStrokeListener* listener); | 54 // A caller must call EnableKeyPressMonitoring before calling |
55 // GetKeyPressCount(), and call DisableKeyPressMonitoring when it no | |
56 // longer needs it. | |
57 void EnableKeyPressMonitoring(); | |
58 void DisableKeyPressMonitoring(); | |
59 | |
60 // Returns the number of Keypresses since EnableKeyPressMonitoring is called | |
61 // for the first time. | |
Mark Mentovai
2013/08/23 20:18:11
This comment is still confusing, because Enable-Di
jiayl
2013/08/23 23:47:58
Done.
| |
62 virtual size_t GetKeyPressCount() const = 0; | |
70 | 63 |
71 protected: | 64 protected: |
72 UserInputMonitor(); | 65 UserInputMonitor(); |
73 | 66 |
74 // Called by the platform-specific sub-classes to propagate the events to the | 67 // Called by the platform-specific sub-classes to propagate the events to the |
75 // listeners. | 68 // listeners. |
76 void OnMouseEvent(const SkIPoint& position); | 69 void OnMouseEvent(const SkIPoint& position); |
77 void OnKeyboardEvent(ui::EventType event, ui::KeyboardCode key_code); | |
78 | 70 |
79 private: | 71 private: |
80 virtual void StartMouseMonitoring() = 0; | 72 virtual void StartMouseMonitoring() = 0; |
81 virtual void StopMouseMonitoring() = 0; | 73 virtual void StopMouseMonitoring() = 0; |
82 virtual void StartKeyboardMonitoring() = 0; | 74 virtual void StartKeyboardMonitoring() = 0; |
83 virtual void StopKeyboardMonitoring() = 0; | 75 virtual void StopKeyboardMonitoring() = 0; |
84 | 76 |
85 base::Lock lock_; | 77 base::Lock lock_; |
86 ObserverList<MouseEventListener, true> mouse_listeners_; | 78 ObserverList<MouseEventListener, true> mouse_listeners_; |
87 ObserverList<KeyStrokeListener, true> key_stroke_listeners_; | |
88 bool monitoring_mouse_; | 79 bool monitoring_mouse_; |
89 bool monitoring_keyboard_; | 80 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 | 81 |
94 DISALLOW_COPY_AND_ASSIGN(UserInputMonitor); | 82 DISALLOW_COPY_AND_ASSIGN(UserInputMonitor); |
95 }; | 83 }; |
96 | 84 |
97 } // namespace media | 85 } // namespace media |
98 | 86 |
99 #endif // MEDIA_BASE_USER_INPUT_MONITOR_H_ | 87 #endif // MEDIA_BASE_USER_INPUT_MONITOR_H_ |
OLD | NEW |