Index: media/audio/key_press_monitor.h |
diff --git a/media/audio/key_press_monitor.h b/media/audio/key_press_monitor.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9e6acae51b9a4100b707aa69b08a1c05a2b484f5 |
--- /dev/null |
+++ b/media/audio/key_press_monitor.h |
@@ -0,0 +1,83 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef MEDIA_AUDIO_KEY_PRESS_MONITOR_H_ |
+#define MEDIA_AUDIO_KEY_PRESS_MONITOR_H_ |
+ |
+#include <set> |
+ |
+#include "base/basictypes.h" |
+#include "base/observer_list.h" |
+#include "base/synchronization/lock.h" |
+#include "media/base/user_input_monitor.h" |
+ |
+namespace base { |
+class MessageLoopProxy; |
+} // namespace base |
+ |
+namespace media { |
+ |
+// This class monitors the system-wide key presses and notifies the listeners. |
+// Not thread safe. All methods except ctor and dtor should be called on the |
+// thread whose message loop is passed to the ctor. |
+class KeyPressMonitor : public UserInputObserver { |
+ public: |
+ class KeyPressListener { |
+ public: |
+ // This is called on |message_loop_|'s thread. |
+ // If a key is held down, OnKeyPressed will be called only once before the |
+ // key is released. |
+ virtual void OnKeyPressed() = 0; |
+ |
+ protected: |
+ virtual ~KeyPressListener() {} |
+ }; |
+ |
+ // |message_loop| is the message loop that this object runs on and the |
+ // listeners are called on. |
+ // |io_message_loop| and |ui_message_loop| are the IO and UI message loops |
+ // to receive system events. Linux requires the IO message loop, and Mac and |
+ // Windows require the UI message loop. |
+ KeyPressMonitor(const scoped_refptr<base::MessageLoopProxy>& message_loop, |
+ const scoped_refptr<base::MessageLoopProxy>& io_message_loop, |
+ const scoped_refptr<base::MessageLoopProxy>& ui_message_loop); |
+ virtual ~KeyPressMonitor(); |
+ |
+ // Impl of UserInputObserver. |
+ virtual void OnMouseMoved(const SkIPoint& position) OVERRIDE; |
+ virtual void OnKeyboardEvent(ui::EventType event, |
+ ui::KeyboardCode key_code) OVERRIDE; |
+ |
+ // Adds |listener| to receive key press notifications. A listener should not |
+ // be added more than once. |
+ // The callers should make sure to call RemoveKeyPressListener before |
+ // |listener| is destroyed. |
+ void AddKeyPressListener(KeyPressListener* listener); |
+ // Removes |listener| from receiving key press notifications. Does nothing if |
+ // |listener| has not been added or has already been removed. |
+ void RemoveKeyPressListener(KeyPressListener* listener); |
+ |
+ private: |
+ // Raw pointers of the listeners. |
+ ObserverList<KeyPressListener> listeners_; |
+ // The message loop of the thread that this object runs on. |
+ scoped_refptr<base::MessageLoopProxy> message_loop_; |
+ // The browser IO thread message loop. |
+ scoped_refptr<base::MessageLoopProxy> io_message_loop_; |
+ // The browser UI thread message loop. |
+ scoped_refptr<base::MessageLoopProxy> ui_message_loop_; |
+ // The object monitoring system events. |
+ scoped_ptr<UserInputMonitor> monitor_; |
+ // The WeakPtrFactory used to generate WeakPtr to pass to |monitor_|. |
+ // Must be invalidated on the |message_loop_| thread. |
+ base::WeakPtrFactory<KeyPressMonitor> weak_ptr_factory_; |
+ // The set of keys currently held down. |
+ std::set<ui::KeyboardCode> pressed_keys_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(KeyPressMonitor); |
+}; |
+ |
+} // namespace media |
+ |
+#endif // MEDIA_AUDIO_KEY_PRESS_MONITOR_H_ |