Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/base/user_input_monitor.h" | 5 #include "media/base/user_input_monitor.h" |
| 6 | 6 |
| 7 #include "third_party/skia/include/core/SkPoint.h" | 7 #include "third_party/skia/include/core/SkPoint.h" |
| 8 | 8 |
| 9 namespace media { | 9 namespace media { |
| 10 | 10 |
| 11 #ifdef DISABLE_USER_INPUT_MONITOR | 11 #ifdef DISABLE_USER_INPUT_MONITOR |
| 12 scoped_ptr<UserInputMonitor> UserInputMonitor::Create( | 12 scoped_ptr<UserInputMonitor> UserInputMonitor::Create( |
| 13 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | 13 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, |
| 14 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) { | 14 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) { |
| 15 return scoped_ptr<UserInputMonitor>(); | 15 return scoped_ptr<UserInputMonitor>(); |
| 16 } | 16 } |
| 17 #endif // DISABLE_USER_INPUT_MONITOR | 17 #endif // DISABLE_USER_INPUT_MONITOR |
| 18 | 18 |
| 19 UserInputMonitor::~UserInputMonitor() { | 19 UserInputMonitor::~UserInputMonitor() { |
| 20 DCHECK(!monitoring_mouse_); | 20 DCHECK(!monitoring_mouse_); |
| 21 DCHECK(!monitoring_keyboard_); | 21 DCHECK(!key_press_counter_references_); |
| 22 } | 22 } |
| 23 | 23 |
| 24 void UserInputMonitor::AddMouseListener(MouseEventListener* listener) { | 24 void UserInputMonitor::AddMouseListener(MouseEventListener* listener) { |
| 25 base::AutoLock auto_lock(lock_); | 25 base::AutoLock auto_lock(lock_); |
| 26 mouse_listeners_.AddObserver(listener); | 26 mouse_listeners_.AddObserver(listener); |
| 27 if (!monitoring_mouse_) { | 27 if (!monitoring_mouse_) { |
| 28 StartMouseMonitoring(); | 28 StartMouseMonitoring(); |
| 29 monitoring_mouse_ = true; | 29 monitoring_mouse_ = true; |
| 30 DVLOG(2) << "Started mouse monitoring."; | 30 DVLOG(2) << "Started mouse monitoring."; |
| 31 } | 31 } |
| 32 } | 32 } |
| 33 | |
| 33 void UserInputMonitor::RemoveMouseListener(MouseEventListener* listener) { | 34 void UserInputMonitor::RemoveMouseListener(MouseEventListener* listener) { |
| 34 base::AutoLock auto_lock(lock_); | 35 base::AutoLock auto_lock(lock_); |
| 35 mouse_listeners_.RemoveObserver(listener); | 36 mouse_listeners_.RemoveObserver(listener); |
| 36 if (mouse_listeners_.size() == 0) { | 37 if (mouse_listeners_.size() == 0) { |
| 37 StopMouseMonitoring(); | 38 StopMouseMonitoring(); |
| 38 monitoring_mouse_ = false; | 39 monitoring_mouse_ = false; |
| 39 DVLOG(2) << "Stopped mouse monitoring."; | 40 DVLOG(2) << "Stopped mouse monitoring."; |
| 40 } | 41 } |
| 41 } | 42 } |
| 42 void UserInputMonitor::AddKeyStrokeListener(KeyStrokeListener* listener) { | 43 |
| 44 void UserInputMonitor::EnableKeyPressMonitoring() { | |
| 43 base::AutoLock auto_lock(lock_); | 45 base::AutoLock auto_lock(lock_); |
| 44 key_stroke_listeners_.AddObserver(listener); | 46 key_press_counter_references_++; |
|
DaleCurtis
2013/08/23 02:16:22
style perfers ++var or you can inline it in the if
| |
| 45 if (!monitoring_keyboard_) { | 47 if (key_press_counter_references_ == 1) { |
| 46 StartKeyboardMonitoring(); | 48 StartKeyboardMonitoring(); |
| 47 monitoring_keyboard_ = true; | |
| 48 DVLOG(2) << "Started keyboard monitoring."; | 49 DVLOG(2) << "Started keyboard monitoring."; |
| 49 } | 50 } |
| 50 } | 51 } |
| 51 void UserInputMonitor::RemoveKeyStrokeListener(KeyStrokeListener* listener) { | 52 |
| 53 void UserInputMonitor::DisableKeyPressMonitoring() { | |
| 52 base::AutoLock auto_lock(lock_); | 54 base::AutoLock auto_lock(lock_); |
| 53 key_stroke_listeners_.RemoveObserver(listener); | 55 DCHECK(key_press_counter_references_ > 0); |
|
DaleCurtis
2013/08/23 02:16:22
DCHECK_GT
| |
| 54 if (key_stroke_listeners_.size() == 0) { | 56 key_press_counter_references_--; |
|
DaleCurtis
2013/08/23 02:16:22
prefer --var ditto on inline if you want.
| |
| 57 if (key_press_counter_references_ == 0) { | |
| 55 StopKeyboardMonitoring(); | 58 StopKeyboardMonitoring(); |
| 56 monitoring_keyboard_ = false; | 59 pressed_keys_.clear(); |
| 60 total_key_presses_ = 0; | |
| 57 DVLOG(2) << "Stopped keyboard monitoring."; | 61 DVLOG(2) << "Stopped keyboard monitoring."; |
| 58 } | 62 } |
| 59 } | 63 } |
| 60 | 64 |
| 65 size_t UserInputMonitor::GetKeyPressCount() const { | |
| 66 base::AutoLock auto_lock(lock_); | |
| 67 DCHECK(key_press_counter_references_); | |
| 68 return total_key_presses_; | |
| 69 } | |
| 70 | |
| 61 UserInputMonitor::UserInputMonitor() | 71 UserInputMonitor::UserInputMonitor() |
| 62 : monitoring_mouse_(false), monitoring_keyboard_(false) {} | 72 : monitoring_mouse_(false), |
| 73 key_press_counter_references_(0), | |
| 74 total_key_presses_(0) {} | |
| 63 | 75 |
| 64 void UserInputMonitor::OnMouseEvent(const SkIPoint& position) { | 76 void UserInputMonitor::OnMouseEvent(const SkIPoint& position) { |
| 65 base::AutoLock auto_lock(lock_); | 77 base::AutoLock auto_lock(lock_); |
| 78 if (!monitoring_mouse_) | |
| 79 return; | |
| 66 FOR_EACH_OBSERVER( | 80 FOR_EACH_OBSERVER( |
| 67 MouseEventListener, mouse_listeners_, OnMouseMoved(position)); | 81 MouseEventListener, mouse_listeners_, OnMouseMoved(position)); |
| 68 } | 82 } |
| 69 | 83 |
| 70 void UserInputMonitor::OnKeyboardEvent(ui::EventType event, | 84 void UserInputMonitor::OnKeyboardEvent(ui::EventType event, |
| 71 ui::KeyboardCode key_code) { | 85 ui::KeyboardCode key_code) { |
| 72 base::AutoLock auto_lock(lock_); | 86 base::AutoLock auto_lock(lock_); |
| 73 // Updates the pressed keys and maybe notifies the key_stroke_listeners_. | 87 if (!key_press_counter_references_) |
| 88 return; | |
| 89 // Updates the pressed keys and the total count of key presses. | |
| 74 if (event == ui::ET_KEY_PRESSED) { | 90 if (event == ui::ET_KEY_PRESSED) { |
| 75 if (pressed_keys_.find(key_code) != pressed_keys_.end()) | 91 if (pressed_keys_.find(key_code) != pressed_keys_.end()) |
| 76 return; | 92 return; |
| 77 pressed_keys_.insert(key_code); | 93 pressed_keys_.insert(key_code); |
| 78 DVLOG(6) << "Key stroke detected."; | 94 total_key_presses_++; |
|
DaleCurtis
2013/08/23 02:16:22
Prefer ++var.
| |
| 79 FOR_EACH_OBSERVER(KeyStrokeListener, key_stroke_listeners_, OnKeyStroke()); | 95 DVLOG(6) << "Key press detected, total count: " << total_key_presses_; |
| 80 } else { | 96 } else { |
| 81 DCHECK_EQ(ui::ET_KEY_RELEASED, event); | 97 DCHECK_EQ(ui::ET_KEY_RELEASED, event); |
| 82 DCHECK(pressed_keys_.find(key_code) != pressed_keys_.end()); | 98 DCHECK(pressed_keys_.find(key_code) != pressed_keys_.end()); |
| 83 pressed_keys_.erase(key_code); | 99 pressed_keys_.erase(key_code); |
| 84 } | 100 } |
| 85 } | 101 } |
| 86 | 102 |
| 87 } // namespace media | 103 } // namespace media |
| OLD | NEW |