Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef MEDIA_BASE_USER_INPUT_MONITOR_H_ | |
| 6 #define MEDIA_BASE_USER_INPUT_MONITOR_H_ | |
| 7 | |
| 8 #include <set> | |
| 9 | |
| 10 #include "base/memory/ref_counted.h" | |
| 11 #include "base/memory/scoped_ptr.h" | |
| 12 #include "base/memory/weak_ptr.h" | |
| 13 #include "base/observer_list.h" | |
| 14 #include "base/synchronization/lock.h" | |
| 15 #include "ui/base/events/event_constants.h" | |
| 16 #include "ui/base/keycodes/keyboard_codes.h" | |
| 17 | |
| 18 struct SkIPoint; | |
| 19 | |
| 20 namespace base { | |
| 21 class SingleThreadTaskRunner; | |
| 22 } // namespace base | |
| 23 | |
| 24 namespace media { | |
| 25 | |
| 26 // Monitors and notifies about mouse movements and keyboard events. | |
| 27 // Thread safe. | |
| 28 // The thread on which the listenters are called is not guaranteed. | |
|
DaleCurtis
2013/08/12 20:24:11
listeners. Combine with previous line.
jiayl
2013/08/12 23:13:46
Done.
| |
| 29 class UserInputMonitor { | |
|
DaleCurtis
2013/08/12 20:24:11
Since callbacks are delivered on either the UI or
jiayl
2013/08/12 23:13:46
Done.
| |
| 30 public: | |
| 31 // The interface to receive mouse movement events. | |
| 32 class MouseEventListener { | |
| 33 public: | |
| 34 // |position| is the new mouse position. | |
| 35 virtual void OnMouseMoved(const SkIPoint& position) = 0; | |
| 36 | |
| 37 protected: | |
| 38 virtual ~MouseEventListener() {} | |
| 39 }; | |
| 40 // The interface to receive key_down and key_up events. | |
| 41 class RawKeyboardEventListener { | |
| 42 public: | |
| 43 // |event| is the keyboard event, either ui::ET_KEY_PRESSED or | |
| 44 // ui::ET_KEY_RELEASED. | |
| 45 // |key_code| is the virtual key code. | |
| 46 virtual void OnKeyboardEvent(ui::EventType event, | |
| 47 ui::KeyboardCode key_code) = 0; | |
| 48 | |
| 49 protected: | |
| 50 virtual ~RawKeyboardEventListener() {} | |
| 51 }; | |
| 52 // The interface to receive key press events. | |
| 53 class KeyStrokeListener { | |
| 54 public: | |
| 55 // Called when any key is pressed. Called only once until the key is | |
| 56 // released, i.e. holding down a key for a long period will generate one | |
| 57 // callback just when the key is pressed down. | |
| 58 virtual void OnKeyPressed() = 0; | |
| 59 | |
| 60 protected: | |
| 61 virtual ~KeyStrokeListener() {} | |
| 62 }; | |
| 63 | |
| 64 virtual ~UserInputMonitor(); | |
| 65 | |
| 66 // Creates a platform-specific instance of UserInputMonitor. | |
| 67 // |io_task_runner| is the task runner for an IO thread. | |
| 68 // |ui_task_runner| is the task runner for a UI thread. | |
| 69 static scoped_ptr<UserInputMonitor> Create( | |
| 70 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | |
| 71 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner); | |
| 72 | |
| 73 // The same |listener| should only be added once. | |
| 74 // The clients should make sure to call Remove*Listener before |listener| is | |
| 75 // destroyed. | |
| 76 void AddMouseListener(MouseEventListener* listener); | |
| 77 void RemoveMouseListener(MouseEventListener* listener); | |
| 78 void AddRawKeyboardListener(RawKeyboardEventListener* listener); | |
| 79 void RemoveRawKeyboardListener(RawKeyboardEventListener* listener); | |
| 80 void AddKeyStrokeListener(KeyStrokeListener* listener); | |
| 81 void RemoveKeyStrokeListener(KeyStrokeListener* listener); | |
| 82 | |
| 83 protected: | |
| 84 UserInputMonitor(); | |
| 85 | |
| 86 // Called by the platform-specific sub-classes to propagate the events to the | |
| 87 // listeners. | |
| 88 void OnMouseEvent(const SkIPoint& position); | |
| 89 void OnKeyboardEvent(ui::EventType event, ui::KeyboardCode key_code); | |
| 90 | |
| 91 private: | |
| 92 virtual void StartMouseMonitoring() = 0; | |
| 93 virtual void StopMouseMonitoring() = 0; | |
| 94 virtual void StartKeyboardMonitoring() = 0; | |
| 95 virtual void StopKeyboardMonitoring() = 0; | |
| 96 | |
| 97 base::Lock lock_; | |
| 98 ObserverList<MouseEventListener, true> mouse_listeners_; | |
| 99 ObserverList<RawKeyboardEventListener, true> keyboard_listeners_; | |
| 100 ObserverList<KeyStrokeListener, true> key_stroke_listeners_; | |
| 101 bool monitoring_mouse_; | |
| 102 bool monitoring_keyboard_; | |
| 103 // The set of keys currently held down. Used for convering raw keyboard events | |
| 104 // into KeyStrokeListener callbacks. | |
| 105 std::set<ui::KeyboardCode> pressed_keys_; | |
| 106 | |
| 107 DISALLOW_COPY_AND_ASSIGN(UserInputMonitor); | |
| 108 }; | |
| 109 | |
| 110 } // namespace media | |
| 111 | |
| 112 #endif // MEDIA_BASE_USER_INPUT_MONITOR_H_ | |
| OLD | NEW |