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

Side by Side 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: add linux impl 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 unified diff | Download patch
OLDNEW
(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 // to receive system events. Linux requires the IO message loop, and Mac and
41 // Windows require the UI message loop.
42 KeyPressMonitor(const scoped_refptr<base::MessageLoopProxy>& message_loop,
43 const scoped_refptr<base::MessageLoopProxy>& io_message_loop,
44 const scoped_refptr<base::MessageLoopProxy>& ui_message_loop);
45 virtual ~KeyPressMonitor();
46
47 // Impl of UserInputObserver.
48 virtual void OnMouseMoved(const SkIPoint& position) OVERRIDE;
49 virtual void OnKeyboardEvent(ui::EventType event,
50 ui::KeyboardCode key_code) OVERRIDE;
51
52 // Adds |listener| to receive key press notifications. A listener should not
53 // be added more than once.
54 // The callers should make sure to call RemoveKeyPressListener before
55 // |listener| is destroyed.
56 void AddKeyPressListener(KeyPressListener* listener);
57 // Removes |listener| from receiving key press notifications. Does nothing if
58 // |listener| has not been added or has already been removed.
59 void RemoveKeyPressListener(KeyPressListener* listener);
60
61 private:
62 // Raw pointers of the listeners.
63 ObserverList<KeyPressListener> listeners_;
64 // The message loop of the thread that this object runs on.
65 scoped_refptr<base::MessageLoopProxy> message_loop_;
66 // The browser IO thread message loop.
67 scoped_refptr<base::MessageLoopProxy> io_message_loop_;
68 // The browser UI thread message loop.
69 scoped_refptr<base::MessageLoopProxy> ui_message_loop_;
70 // The object monitoring system events.
71 scoped_ptr<UserInputMonitor> monitor_;
72 // The WeakPtrFactory used to generate WeakPtr to pass to |monitor_|.
73 // Must be invalidated on the |message_loop_| thread.
74 base::WeakPtrFactory<KeyPressMonitor> weak_ptr_factory_;
75 // The set of keys currently held down.
76 std::set<ui::KeyboardCode> pressed_keys_;
77
78 DISALLOW_COPY_AND_ASSIGN(KeyPressMonitor);
79 };
80
81 } // namespace media
82
83 #endif // MEDIA_AUDIO_KEY_PRESS_MONITOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698