Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(765)

Unified Diff: media/audio/key_press_monitor.h

Issue 21183002: Adding key press detection in the browser process. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..7ea6f5cf0a55dd37a2cbf8dcc1720e1b5c909be6
--- /dev/null
+++ b/media/audio/key_press_monitor.h
@@ -0,0 +1,82 @@
+// 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
+ // used to receive system input events.
+ 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_

Powered by Google App Engine
This is Rietveld 408576698