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> | 8 #include <set> |
9 | 9 |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/memory/weak_ptr.h" | |
13 #include "base/observer_list.h" | 12 #include "base/observer_list.h" |
14 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
15 #include "media/base/media_export.h" | 14 #include "media/base/media_export.h" |
16 #include "ui/base/events/event_constants.h" | 15 #include "ui/base/events/event_constants.h" |
17 #include "ui/base/keycodes/keyboard_codes.h" | 16 #include "ui/base/keycodes/keyboard_codes.h" |
18 | 17 |
19 struct SkIPoint; | 18 struct SkIPoint; |
20 | 19 |
21 namespace base { | 20 namespace base { |
22 class SingleThreadTaskRunner; | 21 class SingleThreadTaskRunner; |
23 } // namespace base | 22 } // namespace base |
24 | 23 |
25 namespace media { | 24 namespace media { |
26 | 25 |
27 // Monitors and notifies about mouse movements and keyboard events. | 26 // Monitors and notifies about mouse movements and keyboard events. |
28 // Thread safe. The thread on which the listenters are called is not guaranteed. | 27 // 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 | 28 // The callers should not perform expensive/blocking tasks in the callback since |
30 // it might be called on the browser UI/IO threads. | 29 // it might be called on the browser UI/IO threads. |
30 // The object must outlive the browser UI/IO threads to make sure the callbacks | |
31 // will not access deleted object. | |
31 class MEDIA_EXPORT UserInputMonitor { | 32 class MEDIA_EXPORT UserInputMonitor { |
32 public: | 33 public: |
33 // The interface to receive mouse movement events. | 34 // The interface to receive mouse movement events. |
34 class MEDIA_EXPORT MouseEventListener { | 35 class MEDIA_EXPORT MouseEventListener { |
DaleCurtis
2013/08/22 22:07:34
Have you sat down with sergeyu@ and wez@ to figure
jiayl
2013/08/22 22:23:22
Sergey reviewed the last CL that added these APIs.
DaleCurtis
2013/08/23 01:13:38
Not really the same thing. :) Since you introduced
jiayl
2013/08/23 01:49:33
Yes, there is a plan to migrate remoting to use th
| |
35 public: | 36 public: |
36 // |position| is the new mouse position. | 37 // |position| is the new mouse position. |
37 virtual void OnMouseMoved(const SkIPoint& position) = 0; | 38 virtual void OnMouseMoved(const SkIPoint& position) = 0; |
38 | 39 |
39 protected: | 40 protected: |
40 virtual ~MouseEventListener() {} | 41 virtual ~MouseEventListener() {} |
41 }; | 42 }; |
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 | 43 |
54 virtual ~UserInputMonitor(); | 44 virtual ~UserInputMonitor(); |
55 | 45 |
56 // Creates a platform-specific instance of UserInputMonitor. | 46 // Creates a platform-specific instance of UserInputMonitor. |
57 // |io_task_runner| is the task runner for an IO thread. | 47 // |io_task_runner| is the task runner for an IO thread. |
58 // |ui_task_runner| is the task runner for a UI thread. | 48 // |ui_task_runner| is the task runner for a UI thread. |
59 static scoped_ptr<UserInputMonitor> Create( | 49 static scoped_ptr<UserInputMonitor> Create( |
60 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | 50 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, |
61 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner); | 51 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner); |
62 | 52 |
63 // The same |listener| should only be added once. | 53 // The same |listener| should only be added once. |
64 // The clients should make sure to call Remove*Listener before |listener| is | 54 // The clients should make sure to call Remove*Listener before |listener| is |
65 // destroyed. | 55 // destroyed. |
66 void AddMouseListener(MouseEventListener* listener); | 56 void AddMouseListener(MouseEventListener* listener); |
67 void RemoveMouseListener(MouseEventListener* listener); | 57 void RemoveMouseListener(MouseEventListener* listener); |
68 void AddKeyStrokeListener(KeyStrokeListener* listener); | 58 |
69 void RemoveKeyStrokeListener(KeyStrokeListener* listener); | 59 // The callers must call AddKeyPressCounterReference before calling |
60 // GetKeyPressCount(), and call ReleaseKeyPressCounterReference when it no | |
61 // longer needs it. | |
62 void AddKeyPressCounterReference(); | |
63 void ReleaseKeyPressCounterReference(); | |
64 // Returns the number of Key presses since the first KeyPressCounterReference | |
65 // is added. Auto-repeated key presses are not counted. | |
66 virtual size_t GetKeyPressCount() const; | |
70 | 67 |
71 protected: | 68 protected: |
72 UserInputMonitor(); | 69 UserInputMonitor(); |
73 | 70 |
74 // Called by the platform-specific sub-classes to propagate the events to the | 71 // Called by the platform-specific sub-classes to propagate the events to the |
75 // listeners. | 72 // listeners. |
76 void OnMouseEvent(const SkIPoint& position); | 73 void OnMouseEvent(const SkIPoint& position); |
77 void OnKeyboardEvent(ui::EventType event, ui::KeyboardCode key_code); | 74 void OnKeyboardEvent(ui::EventType event, ui::KeyboardCode key_code); |
78 | 75 |
79 private: | 76 private: |
80 virtual void StartMouseMonitoring() = 0; | 77 virtual void StartMouseMonitoring() = 0; |
81 virtual void StopMouseMonitoring() = 0; | 78 virtual void StopMouseMonitoring() = 0; |
82 virtual void StartKeyboardMonitoring() = 0; | 79 virtual void StartKeyboardMonitoring() = 0; |
83 virtual void StopKeyboardMonitoring() = 0; | 80 virtual void StopKeyboardMonitoring() = 0; |
84 | 81 |
85 base::Lock lock_; | 82 mutable base::Lock lock_; |
86 ObserverList<MouseEventListener, true> mouse_listeners_; | 83 ObserverList<MouseEventListener, true> mouse_listeners_; |
87 ObserverList<KeyStrokeListener, true> key_stroke_listeners_; | |
88 bool monitoring_mouse_; | 84 bool monitoring_mouse_; |
89 bool monitoring_keyboard_; | 85 size_t key_press_counter_references_; |
90 // The set of keys currently held down. Used for convering raw keyboard events | 86 // The set of keys currently held down. Used for convering raw keyboard events |
91 // into KeyStrokeListener callbacks. | 87 // into KeyStrokeListener callbacks. |
92 std::set<ui::KeyboardCode> pressed_keys_; | 88 std::set<int> pressed_keys_; |
89 size_t total_key_presses_; | |
DaleCurtis
2013/08/22 22:07:34
Shouldn't this just be pressed_keys_.size() ?
jiayl
2013/08/22 22:23:22
No. If you press and release a key, pressed_keys_.
| |
93 | 90 |
94 DISALLOW_COPY_AND_ASSIGN(UserInputMonitor); | 91 DISALLOW_COPY_AND_ASSIGN(UserInputMonitor); |
95 }; | 92 }; |
96 | 93 |
97 } // namespace media | 94 } // namespace media |
98 | 95 |
99 #endif // MEDIA_BASE_USER_INPUT_MONITOR_H_ | 96 #endif // MEDIA_BASE_USER_INPUT_MONITOR_H_ |
OLD | NEW |