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