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 #include "media/base/user_input_monitor.h" | |
6 | |
7 #include "third_party/skia/include/core/SkPoint.h" | |
8 | |
9 namespace media { | |
10 | |
11 #ifdef DISABLE_USER_INPUT_MONITOR | |
12 scoped_ptr<UserInputMonitor> UserInputMonitor::Create( | |
13 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | |
14 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) { | |
15 return scoped_ptr<UserInputMonitor>(); | |
16 } | |
17 #endif // DISABLE_USER_INPUT_MONITOR | |
18 | |
19 UserInputMonitor::~UserInputMonitor() { | |
20 DCHECK(!monitoring_mouse_); | |
21 DCHECK(!monitoring_keyboard_); | |
22 } | |
23 | |
24 void UserInputMonitor::AddMouseListener(MouseEventListener* listener) { | |
25 base::AutoLock auto_lock(lock_); | |
26 mouse_listeners_.AddObserver(listener); | |
27 if (!monitoring_mouse_) { | |
28 StartMouseMonitoring(); | |
29 monitoring_mouse_ = true; | |
30 DVLOG(2) << "Started mouse monitoring."; | |
31 } | |
32 } | |
33 void UserInputMonitor::RemoveMouseListener(MouseEventListener* listener) { | |
34 base::AutoLock auto_lock(lock_); | |
35 mouse_listeners_.RemoveObserver(listener); | |
36 if (mouse_listeners_.size() == 0) { | |
37 StopMouseMonitoring(); | |
38 monitoring_mouse_ = false; | |
39 DVLOG(2) << "Stopped mouse monitoring."; | |
40 } | |
41 } | |
42 void UserInputMonitor::AddKeyStrokeListener(KeyStrokeListener* listener) { | |
43 base::AutoLock auto_lock(lock_); | |
44 key_stroke_listeners_.AddObserver(listener); | |
45 if (!monitoring_keyboard_) { | |
46 StartKeyboardMonitoring(); | |
47 monitoring_keyboard_ = true; | |
48 DVLOG(2) << "Started keyboard monitoring."; | |
49 } | |
50 } | |
51 void UserInputMonitor::RemoveKeyStrokeListener(KeyStrokeListener* listener) { | |
52 base::AutoLock auto_lock(lock_); | |
53 key_stroke_listeners_.RemoveObserver(listener); | |
54 if (key_stroke_listeners_.size() == 0) { | |
55 StopKeyboardMonitoring(); | |
56 monitoring_keyboard_ = false; | |
57 DVLOG(2) << "Stopped keyboard monitoring."; | |
58 } | |
59 } | |
60 | |
61 UserInputMonitor::UserInputMonitor() | |
62 : monitoring_mouse_(false), monitoring_keyboard_(false) {} | |
63 | |
64 void UserInputMonitor::OnMouseEvent(const SkIPoint& position) { | |
65 base::AutoLock auto_lock(lock_); | |
66 FOR_EACH_OBSERVER( | |
67 MouseEventListener, mouse_listeners_, OnMouseMoved(position)); | |
68 } | |
69 | |
70 void UserInputMonitor::OnKeyboardEvent(ui::EventType event, | |
71 ui::KeyboardCode key_code) { | |
72 base::AutoLock auto_lock(lock_); | |
73 // Updates the pressed keys and maybe notifies the key_stroke_listeners_. | |
74 if (event == ui::ET_KEY_PRESSED) { | |
75 if (pressed_keys_.find(key_code) != pressed_keys_.end()) | |
76 return; | |
77 pressed_keys_.insert(key_code); | |
78 DVLOG(6) << "Key stroke detected."; | |
79 FOR_EACH_OBSERVER(KeyStrokeListener, key_stroke_listeners_, OnKeyStroke()); | |
80 } else { | |
81 DCHECK_EQ(ui::ET_KEY_RELEASED, event); | |
82 DCHECK(pressed_keys_.find(key_code) != pressed_keys_.end()); | |
83 pressed_keys_.erase(key_code); | |
84 } | |
85 } | |
86 | |
87 } // namespace media | |
OLD | NEW |