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 "media/base/media_export.h" |
| 16 #include "ui/base/events/event_constants.h" |
| 17 #include "ui/base/keycodes/keyboard_codes.h" |
| 18 |
| 19 struct SkIPoint; |
| 20 |
| 21 namespace base { |
| 22 class SingleThreadTaskRunner; |
| 23 } // namespace base |
| 24 |
| 25 namespace media { |
| 26 |
| 27 // Monitors and notifies about mouse movements and keyboard events. |
| 28 // 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 |
| 30 // it might be called on the browser UI/IO threads. |
| 31 class MEDIA_EXPORT UserInputMonitor { |
| 32 public: |
| 33 // The interface to receive mouse movement events. |
| 34 class MEDIA_EXPORT MouseEventListener { |
| 35 public: |
| 36 // |position| is the new mouse position. |
| 37 virtual void OnMouseMoved(const SkIPoint& position) = 0; |
| 38 |
| 39 protected: |
| 40 virtual ~MouseEventListener() {} |
| 41 }; |
| 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 |
| 54 virtual ~UserInputMonitor(); |
| 55 |
| 56 // Creates a platform-specific instance of UserInputMonitor. |
| 57 // |io_task_runner| is the task runner for an IO thread. |
| 58 // |ui_task_runner| is the task runner for a UI thread. |
| 59 static scoped_ptr<UserInputMonitor> Create( |
| 60 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, |
| 61 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner); |
| 62 |
| 63 // The same |listener| should only be added once. |
| 64 // The clients should make sure to call Remove*Listener before |listener| is |
| 65 // destroyed. |
| 66 void AddMouseListener(MouseEventListener* listener); |
| 67 void RemoveMouseListener(MouseEventListener* listener); |
| 68 void AddKeyStrokeListener(KeyStrokeListener* listener); |
| 69 void RemoveKeyStrokeListener(KeyStrokeListener* listener); |
| 70 |
| 71 protected: |
| 72 UserInputMonitor(); |
| 73 |
| 74 // Called by the platform-specific sub-classes to propagate the events to the |
| 75 // listeners. |
| 76 void OnMouseEvent(const SkIPoint& position); |
| 77 void OnKeyboardEvent(ui::EventType event, ui::KeyboardCode key_code); |
| 78 |
| 79 private: |
| 80 virtual void StartMouseMonitoring() = 0; |
| 81 virtual void StopMouseMonitoring() = 0; |
| 82 virtual void StartKeyboardMonitoring() = 0; |
| 83 virtual void StopKeyboardMonitoring() = 0; |
| 84 |
| 85 base::Lock lock_; |
| 86 ObserverList<MouseEventListener, true> mouse_listeners_; |
| 87 ObserverList<KeyStrokeListener, true> key_stroke_listeners_; |
| 88 bool monitoring_mouse_; |
| 89 bool monitoring_keyboard_; |
| 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 |
| 94 DISALLOW_COPY_AND_ASSIGN(UserInputMonitor); |
| 95 }; |
| 96 |
| 97 } // namespace media |
| 98 |
| 99 #endif // MEDIA_BASE_USER_INPUT_MONITOR_H_ |
OLD | NEW |