OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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_AUDIO_KEY_PRESS_MONITOR_H_ |
| 6 #define MEDIA_AUDIO_KEY_PRESS_MONITOR_H_ |
| 7 |
| 8 #include <set> |
| 9 |
| 10 #include "base/basictypes.h" |
| 11 #include "base/observer_list.h" |
| 12 #include "base/synchronization/lock.h" |
| 13 #include "media/base/user_input_monitor.h" |
| 14 |
| 15 namespace base { |
| 16 class MessageLoopProxy; |
| 17 } // namespace base |
| 18 |
| 19 namespace media { |
| 20 |
| 21 // This class monitors the system-wide key presses and notifies the listeners. |
| 22 // Not thread safe. All methods except ctor and dtor should be called on the |
| 23 // thread whose message loop is passed to the ctor. |
| 24 class KeyPressMonitor : public UserInputObserver { |
| 25 public: |
| 26 class KeyPressListener { |
| 27 public: |
| 28 // This is called on |message_loop_|'s thread. |
| 29 // If a key is held down, OnKeyPressed will be called only once before the |
| 30 // key is released. |
| 31 virtual void OnKeyPressed() = 0; |
| 32 |
| 33 protected: |
| 34 virtual ~KeyPressListener() {} |
| 35 }; |
| 36 |
| 37 // |message_loop| is the message loop that this object runs on and the |
| 38 // listeners are called on. |
| 39 // |io_message_loop| and |ui_message_loop| are the IO and UI message loops |
| 40 // used to receive system input events. |
| 41 KeyPressMonitor(const scoped_refptr<base::MessageLoopProxy>& message_loop, |
| 42 const scoped_refptr<base::MessageLoopProxy>& io_message_loop, |
| 43 const scoped_refptr<base::MessageLoopProxy>& ui_message_loop); |
| 44 virtual ~KeyPressMonitor(); |
| 45 |
| 46 // Impl of UserInputObserver. |
| 47 virtual void OnMouseMoved(const SkIPoint& position) OVERRIDE; |
| 48 virtual void OnKeyboardEvent(ui::EventType event, |
| 49 ui::KeyboardCode key_code) OVERRIDE; |
| 50 |
| 51 // Adds |listener| to receive key press notifications. A listener should not |
| 52 // be added more than once. |
| 53 // The callers should make sure to call RemoveKeyPressListener before |
| 54 // |listener| is destroyed. |
| 55 void AddKeyPressListener(KeyPressListener* listener); |
| 56 // Removes |listener| from receiving key press notifications. Does nothing if |
| 57 // |listener| has not been added or has already been removed. |
| 58 void RemoveKeyPressListener(KeyPressListener* listener); |
| 59 |
| 60 private: |
| 61 // Raw pointers of the listeners. |
| 62 ObserverList<KeyPressListener> listeners_; |
| 63 // The message loop of the thread that this object runs on. |
| 64 scoped_refptr<base::MessageLoopProxy> message_loop_; |
| 65 // The browser IO thread message loop. |
| 66 scoped_refptr<base::MessageLoopProxy> io_message_loop_; |
| 67 // The browser UI thread message loop. |
| 68 scoped_refptr<base::MessageLoopProxy> ui_message_loop_; |
| 69 // The object monitoring system events. |
| 70 scoped_ptr<UserInputMonitor> monitor_; |
| 71 // The WeakPtrFactory used to generate WeakPtr to pass to |monitor_|. |
| 72 // Must be invalidated on the |message_loop_| thread. |
| 73 base::WeakPtrFactory<KeyPressMonitor> weak_ptr_factory_; |
| 74 // The set of keys currently held down. |
| 75 std::set<ui::KeyboardCode> pressed_keys_; |
| 76 |
| 77 DISALLOW_COPY_AND_ASSIGN(KeyPressMonitor); |
| 78 }; |
| 79 |
| 80 } // namespace media |
| 81 |
| 82 #endif // MEDIA_AUDIO_KEY_PRESS_MONITOR_H_ |
OLD | NEW |