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. The thread on which the listenters are called is not guaranteed. |
| 28 // The callers should not perform expensive/blocking tasks in the callback since |
| 29 // it might be called on the browser UI/IO threads. |
| 30 class UserInputMonitor { |
| 31 public: |
| 32 // The interface to receive mouse movement events. |
| 33 class MouseEventListener { |
| 34 public: |
| 35 // |position| is the new mouse position. |
| 36 virtual void OnMouseMoved(const SkIPoint& position) = 0; |
| 37 |
| 38 protected: |
| 39 virtual ~MouseEventListener() {} |
| 40 }; |
| 41 // The interface to receive key stroke events. |
| 42 class KeyStrokeListener { |
| 43 public: |
| 44 // Called when any key is pressed. Called only once until the key is |
| 45 // released, i.e. holding down a key for a long period will generate one |
| 46 // callback just when the key is pressed down. |
| 47 virtual void OnKeyStroke() = 0; |
| 48 |
| 49 protected: |
| 50 virtual ~KeyStrokeListener() {} |
| 51 }; |
| 52 |
| 53 virtual ~UserInputMonitor(); |
| 54 |
| 55 // Creates a platform-specific instance of UserInputMonitor. |
| 56 // |io_task_runner| is the task runner for an IO thread. |
| 57 // |ui_task_runner| is the task runner for a UI thread. |
| 58 static scoped_ptr<UserInputMonitor> Create( |
| 59 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, |
| 60 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner); |
| 61 |
| 62 // The same |listener| should only be added once. |
| 63 // The clients should make sure to call Remove*Listener before |listener| is |
| 64 // destroyed. |
| 65 void AddMouseListener(MouseEventListener* listener); |
| 66 void RemoveMouseListener(MouseEventListener* listener); |
| 67 void AddKeyStrokeListener(KeyStrokeListener* listener); |
| 68 void RemoveKeyStrokeListener(KeyStrokeListener* listener); |
| 69 |
| 70 protected: |
| 71 UserInputMonitor(); |
| 72 |
| 73 // Called by the platform-specific sub-classes to propagate the events to the |
| 74 // listeners. |
| 75 void OnMouseEvent(const SkIPoint& position); |
| 76 void OnKeyboardEvent(ui::EventType event, ui::KeyboardCode key_code); |
| 77 |
| 78 private: |
| 79 virtual void StartMouseMonitoring() = 0; |
| 80 virtual void StopMouseMonitoring() = 0; |
| 81 virtual void StartKeyboardMonitoring() = 0; |
| 82 virtual void StopKeyboardMonitoring() = 0; |
| 83 |
| 84 base::Lock lock_; |
| 85 ObserverList<MouseEventListener, true> mouse_listeners_; |
| 86 ObserverList<KeyStrokeListener, true> key_stroke_listeners_; |
| 87 bool monitoring_mouse_; |
| 88 bool monitoring_keyboard_; |
| 89 // The set of keys currently held down. Used for convering raw keyboard events |
| 90 // into KeyStrokeListener callbacks. |
| 91 std::set<ui::KeyboardCode> pressed_keys_; |
| 92 |
| 93 DISALLOW_COPY_AND_ASSIGN(UserInputMonitor); |
| 94 }; |
| 95 |
| 96 } // namespace media |
| 97 |
| 98 #endif // MEDIA_BASE_USER_INPUT_MONITOR_H_ |
OLD | NEW |