| 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() |
| 20 : monitoring_mouse_(false), key_press_counter_references_(0) {} |
| 21 |
| 19 UserInputMonitor::~UserInputMonitor() { | 22 UserInputMonitor::~UserInputMonitor() { |
| 20 DCHECK(!monitoring_mouse_); | 23 DCHECK(!monitoring_mouse_); |
| 21 DCHECK(!monitoring_keyboard_); | 24 DCHECK(!key_press_counter_references_); |
| 22 } | 25 } |
| 23 | 26 |
| 24 void UserInputMonitor::AddMouseListener(MouseEventListener* listener) { | 27 void UserInputMonitor::AddMouseListener(MouseEventListener* listener) { |
| 25 base::AutoLock auto_lock(lock_); | 28 base::AutoLock auto_lock(lock_); |
| 26 mouse_listeners_.AddObserver(listener); | 29 mouse_listeners_.AddObserver(listener); |
| 27 if (!monitoring_mouse_) { | 30 if (!monitoring_mouse_) { |
| 28 StartMouseMonitoring(); | 31 StartMouseMonitoring(); |
| 29 monitoring_mouse_ = true; | 32 monitoring_mouse_ = true; |
| 30 DVLOG(2) << "Started mouse monitoring."; | 33 DVLOG(2) << "Started mouse monitoring."; |
| 31 } | 34 } |
| 32 } | 35 } |
| 36 |
| 33 void UserInputMonitor::RemoveMouseListener(MouseEventListener* listener) { | 37 void UserInputMonitor::RemoveMouseListener(MouseEventListener* listener) { |
| 34 base::AutoLock auto_lock(lock_); | 38 base::AutoLock auto_lock(lock_); |
| 35 mouse_listeners_.RemoveObserver(listener); | 39 mouse_listeners_.RemoveObserver(listener); |
| 36 if (!mouse_listeners_.might_have_observers()) { | 40 if (!mouse_listeners_.might_have_observers()) { |
| 37 StopMouseMonitoring(); | 41 StopMouseMonitoring(); |
| 38 monitoring_mouse_ = false; | 42 monitoring_mouse_ = false; |
| 39 DVLOG(2) << "Stopped mouse monitoring."; | 43 DVLOG(2) << "Stopped mouse monitoring."; |
| 40 } | 44 } |
| 41 } | 45 } |
| 42 void UserInputMonitor::AddKeyStrokeListener(KeyStrokeListener* listener) { | 46 |
| 47 void UserInputMonitor::EnableKeyPressMonitoring() { |
| 43 base::AutoLock auto_lock(lock_); | 48 base::AutoLock auto_lock(lock_); |
| 44 key_stroke_listeners_.AddObserver(listener); | 49 ++key_press_counter_references_; |
| 45 if (!monitoring_keyboard_) { | 50 if (key_press_counter_references_ == 1) { |
| 46 StartKeyboardMonitoring(); | 51 StartKeyboardMonitoring(); |
| 47 monitoring_keyboard_ = true; | |
| 48 DVLOG(2) << "Started keyboard monitoring."; | 52 DVLOG(2) << "Started keyboard monitoring."; |
| 49 } | 53 } |
| 50 } | 54 } |
| 51 void UserInputMonitor::RemoveKeyStrokeListener(KeyStrokeListener* listener) { | 55 |
| 56 void UserInputMonitor::DisableKeyPressMonitoring() { |
| 52 base::AutoLock auto_lock(lock_); | 57 base::AutoLock auto_lock(lock_); |
| 53 key_stroke_listeners_.RemoveObserver(listener); | 58 DCHECK_NE(key_press_counter_references_, 0u); |
| 54 if (!key_stroke_listeners_.might_have_observers()) { | 59 --key_press_counter_references_; |
| 60 if (key_press_counter_references_ == 0) { |
| 55 StopKeyboardMonitoring(); | 61 StopKeyboardMonitoring(); |
| 56 monitoring_keyboard_ = false; | |
| 57 DVLOG(2) << "Stopped keyboard monitoring."; | 62 DVLOG(2) << "Stopped keyboard monitoring."; |
| 58 } | 63 } |
| 59 } | 64 } |
| 60 | 65 |
| 61 UserInputMonitor::UserInputMonitor() | |
| 62 : monitoring_mouse_(false), monitoring_keyboard_(false) {} | |
| 63 | |
| 64 void UserInputMonitor::OnMouseEvent(const SkIPoint& position) { | 66 void UserInputMonitor::OnMouseEvent(const SkIPoint& position) { |
| 65 base::AutoLock auto_lock(lock_); | 67 base::AutoLock auto_lock(lock_); |
| 68 if (!monitoring_mouse_) |
| 69 return; |
| 66 FOR_EACH_OBSERVER( | 70 FOR_EACH_OBSERVER( |
| 67 MouseEventListener, mouse_listeners_, OnMouseMoved(position)); | 71 MouseEventListener, mouse_listeners_, OnMouseMoved(position)); |
| 68 } | 72 } |
| 69 | 73 |
| 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 | 74 } // namespace media |
| OLD | NEW |